commander 5.0.0 → 6.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/spec/command_spec.rb DELETED
@@ -1,198 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'spec_helper'
4
-
5
- describe Commander::Command do
6
- include Commander::Methods
7
-
8
- before :each do
9
- mock_terminal
10
- create_test_command
11
- end
12
-
13
- describe 'Options' do
14
- before :each do
15
- @options = Commander::Command::Options.new
16
- end
17
-
18
- it 'should act like an open struct' do
19
- @options.send = 'mail'
20
- @options.call = true
21
- expect(@options.send).to eq('mail')
22
- expect(@options.call).to eq(true)
23
- end
24
-
25
- it 'should allow __send__ to function as always' do
26
- @options.send = 'foo'
27
- expect(@options.__send__(:send)).to eq('foo')
28
- end
29
- end
30
-
31
- describe '#option' do
32
- it 'should add options' do
33
- expect { @command.option '--recursive' }.to change(@command.options, :length).from(1).to(2)
34
- end
35
-
36
- it 'should allow procs as option handlers' do
37
- @command.option('--recursive') { |recursive| expect(recursive).to be true }
38
- @command.run '--recursive'
39
- end
40
-
41
- it 'should allow usage of common method names' do
42
- @command.option '--open file'
43
- @command.when_called { |_, options| expect(options.open).to eq('foo') }
44
- @command.run '--open', 'foo'
45
- end
46
- end
47
-
48
- describe '#run' do
49
- describe 'should invoke #when_called' do
50
- it 'with arguments seperated from options' do
51
- @command.when_called { |args, _options| expect(args.join(' ')).to eq('just some args') }
52
- @command.run '--verbose', 'just', 'some', 'args'
53
- end
54
-
55
- it 'calling the #call method by default when an object is called' do
56
- object = double 'Object'
57
- expect(object).to receive(:call).once
58
- @command.when_called object
59
- @command.run 'foo'
60
- end
61
-
62
- it 'should allow #action as an alias to #when_called' do
63
- object = double 'Object'
64
- expect(object).to receive(:call).once
65
- @command.action object
66
- @command.run 'foo'
67
- end
68
-
69
- it 'calling an arbitrary method when an object is called' do
70
- object = double 'Object'
71
- expect(object).to receive(:foo).once
72
- @command.when_called object, :foo
73
- @command.run 'foo'
74
- end
75
-
76
- it 'should raise an error when no handler is present' do
77
- expect { @command.when_called }.to raise_error(ArgumentError)
78
- end
79
-
80
- it 'should be able to be run more than once' do
81
- expect(@command.run('once')).to eql('test once')
82
- expect(@command.run('twice')).to eql('test twice')
83
- end
84
-
85
- it 'should not accumulate entries in @proxy_options when run twice' do
86
- expect(@command.run('--verbose')).to eql('test ')
87
- expect(@command.proxy_options).to eq([[:verbose, true]])
88
- expect(@command.run('foo')).to eql('test foo')
89
- expect(@command.proxy_options).to eq([])
90
- end
91
- end
92
-
93
- describe 'should populate options with' do
94
- it 'boolean values' do
95
- @command.option '--[no-]toggle'
96
- @command.when_called { |_, options| expect(options.toggle).to be true }
97
- @command.run '--toggle'
98
- @command.when_called { |_, options| expect(options.toggle).to be false }
99
- @command.run '--no-toggle'
100
- end
101
-
102
- it 'mandatory arguments' do
103
- @command.option '--file FILE'
104
- @command.when_called { |_, options| expect(options.file).to eq('foo') }
105
- @command.run '--file', 'foo'
106
- expect { @command.run '--file' }.to raise_error(OptionParser::MissingArgument)
107
- end
108
-
109
- describe 'optional arguments' do
110
- before do
111
- @command.option '--use-config [file] '
112
- end
113
-
114
- it 'should return the argument when provided' do
115
- @command.when_called { |_, options| expect(options.use_config).to eq('foo') }
116
- @command.run '--use-config', 'foo'
117
- end
118
-
119
- it 'should return true when present without an argument' do
120
- @command.when_called { |_, options| expect(options.use_config).to be true }
121
- @command.run '--use-config'
122
- end
123
-
124
- it 'should return nil when not present' do
125
- @command.when_called { |_, options| expect(options.use_config).to be_nil }
126
- @command.run
127
- end
128
- end
129
-
130
- describe 'typed arguments' do
131
- before do
132
- @command.option '--interval N', Integer
133
- end
134
-
135
- it 'should parse valid values' do
136
- @command.when_called { |_, options| expect(options.interval).to eq(5) }
137
- @command.run '--interval', '5'
138
- end
139
-
140
- it 'should reject invalid values' do
141
- expect { @command.run '--interval', 'invalid' }.to raise_error(OptionParser::InvalidArgument)
142
- end
143
- end
144
-
145
- it 'lists' do
146
- @command.option '--fav COLORS', Array
147
- @command.when_called { |_, options| expect(options.fav).to eq(%w(red green blue)) }
148
- @command.run '--fav', 'red,green,blue'
149
- end
150
-
151
- it 'lists with multi-word items' do
152
- @command.option '--fav MOVIES', Array
153
- @command.when_called { |_, options| expect(options.fav).to eq(['super\ bad', 'nightmare']) }
154
- @command.run '--fav', 'super\ bad,nightmare'
155
- end
156
-
157
- it 'defaults' do
158
- @command.option '--files LIST', Array
159
- @command.option '--interval N', Integer
160
- @command.when_called do |_, options|
161
- options.default \
162
- files: %w(foo bar),
163
- interval: 5
164
- expect(options.files).to eq(%w(foo bar))
165
- expect(options.interval).to eq(15)
166
- end
167
- @command.run '--interval', '15'
168
- end
169
-
170
- describe 'given a global option' do
171
- before do
172
- @command.global_options << [:global_option, 'gvalue']
173
- end
174
-
175
- describe 'and no command specific arguments' do
176
- it 'provides the global option to the command action' do
177
- @command.when_called { |_, options| expect(options.global_option).to eq('gvalue') }
178
- @command.run
179
- end
180
- end
181
-
182
- describe 'and a command specific option' do
183
- it 'provides the global option to the command action' do
184
- @command.when_called { |_, options| expect(options.global_option).to eq('gvalue') }
185
- @command.run '--verbose'
186
- end
187
- end
188
-
189
- describe 'and a command specific argument' do
190
- it 'provides the global option to the command action' do
191
- @command.when_called { |_, options| expect(options.global_option).to eq('gvalue') }
192
- @command.run 'argument'
193
- end
194
- end
195
- end
196
- end
197
- end
198
- end
@@ -1,39 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'spec_helper'
4
- require 'commander/configure'
5
-
6
- describe Commander do
7
- describe '.configure' do
8
- it 'calls the given block' do
9
- expect { Commander.configure { throw :block_called } }.to throw_symbol(:block_called)
10
- end
11
-
12
- describe 'called block' do
13
- before(:each) do
14
- allow(Commander::Runner.instance).to receive(:run!)
15
- end
16
-
17
- it 'provides Commander configuration methods' do
18
- Commander.configure do
19
- program :name, 'test'
20
- end
21
-
22
- expect(Commander::Runner.instance.program(:name)).to eq('test')
23
- end
24
-
25
- it 'passes all arguments to the block' do
26
- Commander.configure('foo') do |first_arg|
27
- program :name, first_arg
28
- end
29
-
30
- expect(Commander::Runner.instance.program(:name)).to eq('foo')
31
- end
32
- end
33
-
34
- it 'calls Runner#run! after calling the configuration block' do
35
- expect(Commander::Runner.instance).to receive(:run!)
36
- Commander.configure {}
37
- end
38
- end
39
- end
@@ -1,20 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'spec_helper'
4
-
5
- describe Array do
6
- describe '#parse' do
7
- it 'should seperate a list of words into an array' do
8
- expect(Array.parse('just a test')).to eq(%w(just a test))
9
- end
10
-
11
- it 'should preserve escaped whitespace' do
12
- expect(Array.parse('just a\ test')).to eq(['just', 'a test'])
13
- end
14
-
15
- it 'should match %w behavior with multiple backslashes' do
16
- str = 'just a\\ test'
17
- expect(Array.parse(str)).to eq(['just', 'a test'])
18
- end
19
- end
20
- end
@@ -1,21 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'spec_helper'
4
-
5
- describe Object do
6
- describe '#get_binding' do
7
- it 'should return the objects binding' do
8
- expect(-> {}.get_binding).to be_instance_of(Binding)
9
- end
10
- end
11
-
12
- describe '#method_missing' do
13
- it 'should preserve its original behavior for missing methods' do
14
- expect { send(:i_am_a_missing_method) }.to raise_error(NoMethodError)
15
- end
16
-
17
- it 'should preserve its original behavior for missing variables' do
18
- expect { i_am_a_missing_variable }.to raise_error(NameError)
19
- end
20
- end
21
- end
@@ -1,71 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'spec_helper'
4
-
5
- describe Commander::HelpFormatter::TerminalCompact do
6
- include Commander::Methods
7
-
8
- before :each do
9
- mock_terminal
10
- end
11
-
12
- describe 'global help' do
13
- before :each do
14
- new_command_runner 'help' do
15
- program :help_formatter, :compact
16
- command :'install gem' do |c|
17
- c.syntax = 'foo install gem [options]'
18
- c.summary = 'Install some gem'
19
- end
20
- end.run!
21
- @global_help = @output.string
22
- end
23
-
24
- describe 'should display' do
25
- it 'the command name' do
26
- expect(@global_help).to include('install gem')
27
- end
28
-
29
- it 'the summary' do
30
- expect(@global_help).to include('Install some gem')
31
- end
32
- end
33
- end
34
-
35
- describe 'command help' do
36
- before :each do
37
- new_command_runner 'help', 'install', 'gem' do
38
- program :help_formatter, :compact
39
- command :'install gem' do |c|
40
- c.syntax = 'foo install gem [options]'
41
- c.summary = 'Install some gem'
42
- c.description = 'Install some gem, blah blah blah'
43
- c.example 'one', 'two'
44
- c.example 'three', 'four'
45
- end
46
- end.run!
47
- @command_help = @output.string
48
- end
49
-
50
- describe 'should display' do
51
- it 'the command name' do
52
- expect(@command_help).to include('install gem')
53
- end
54
-
55
- it 'the description' do
56
- expect(@command_help).to include('Install some gem, blah blah blah')
57
- end
58
-
59
- it 'all examples' do
60
- expect(@command_help).to include('# one')
61
- expect(@command_help).to include('two')
62
- expect(@command_help).to include('# three')
63
- expect(@command_help).to include('four')
64
- end
65
-
66
- it 'the syntax' do
67
- expect(@command_help).to include('foo install gem [options]')
68
- end
69
- end
70
- end
71
- end
@@ -1,69 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'spec_helper'
4
-
5
- describe Commander::HelpFormatter::Terminal do
6
- include Commander::Methods
7
-
8
- before :each do
9
- mock_terminal
10
- end
11
-
12
- describe 'global help' do
13
- before :each do
14
- new_command_runner 'help' do
15
- command :'install gem' do |c|
16
- c.syntax = 'foo install gem [options]'
17
- c.summary = 'Install some gem'
18
- end
19
- end.run!
20
- @global_help = @output.string
21
- end
22
-
23
- describe 'should display' do
24
- it 'the command name' do
25
- expect(@global_help).to include('install gem')
26
- end
27
-
28
- it 'the summary' do
29
- expect(@global_help).to include('Install some gem')
30
- end
31
- end
32
- end
33
-
34
- describe 'command help' do
35
- before :each do
36
- new_command_runner 'help', 'install', 'gem' do
37
- command :'install gem' do |c|
38
- c.syntax = 'foo install gem [options]'
39
- c.summary = 'Install some gem'
40
- c.description = 'Install some gem, blah blah blah'
41
- c.example 'one', 'two'
42
- c.example 'three', 'four'
43
- end
44
- end.run!
45
- @command_help = @output.string
46
- end
47
-
48
- describe 'should display' do
49
- it 'the command name' do
50
- expect(@command_help).to include('install gem')
51
- end
52
-
53
- it 'the description' do
54
- expect(@command_help).to include('Install some gem, blah blah blah')
55
- end
56
-
57
- it 'all examples' do
58
- expect(@command_help).to include('# one')
59
- expect(@command_help).to include('two')
60
- expect(@command_help).to include('# three')
61
- expect(@command_help).to include('four')
62
- end
63
-
64
- it 'the syntax' do
65
- expect(@command_help).to include('foo install gem [options]')
66
- end
67
- end
68
- end
69
- end
data/spec/methods_spec.rb DELETED
@@ -1,63 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'spec_helper'
4
- require 'commander/methods'
5
-
6
- describe Commander::Methods do
7
- it 'includes Commander::UI' do
8
- expect(subject.ancestors).to include(Commander::UI)
9
- end
10
-
11
- describe 'AskForClass' do
12
- it 'includes Commander::UI::AskForClass' do
13
- expect(subject.ancestors).to include(Commander::UI::AskForClass)
14
- end
15
-
16
- describe 'defining methods' do
17
- let(:terminal) { double }
18
-
19
- before do
20
- allow(terminal).to receive(:ask)
21
- @old_highline = HighLine.default_instance
22
- HighLine.default_instance = terminal
23
- end
24
-
25
- after do
26
- HighLine.default_instance = @old_highline
27
- end
28
-
29
- subject do
30
- Class.new do
31
- include Commander::UI::AskForClass
32
- end.new
33
- end
34
-
35
- it 'defines common "ask_for_*" methods' do
36
- expect(subject.respond_to?(:ask_for_float)).to be_truthy
37
- end
38
-
39
- it 'responds to "ask_for_*" methods for classes that implement #parse' do
40
- expect(subject.respond_to?(:ask_for_datetime)).to be_truthy
41
- end
42
-
43
- it 'fails "ask_for_*" method invocations without a prompt' do
44
- expect do
45
- subject.ask_for_datetime
46
- end.to raise_error(ArgumentError)
47
- end
48
-
49
- it 'implements "ask_for_*"' do
50
- expect(terminal).to receive(:ask)
51
- subject.ask_for_datetime('hi')
52
- end
53
- end
54
- end
55
-
56
- it 'includes Commander::Delegates' do
57
- expect(subject.ancestors).to include(Commander::Delegates)
58
- end
59
-
60
- it 'does not change the Object ancestors' do
61
- expect(Object.ancestors).not_to include(Commander::UI)
62
- end
63
- end