clin 0.1.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.
- checksums.yaml +7 -0
- data/.codeclimate.yml +5 -0
- data/.gitignore +15 -0
- data/.rubocop.yml +19 -0
- data/.travis.yml +14 -0
- data/Gemfile +10 -0
- data/LICENSE.txt +22 -0
- data/README.md +98 -0
- data/Rakefile +10 -0
- data/clin.gemspec +27 -0
- data/examples/dispatcher.rb +76 -0
- data/examples/nested_dispatcher.rb +91 -0
- data/examples/optional_argument.rb +24 -0
- data/examples/simple.rb +39 -0
- data/examples/test.rb +2 -0
- data/lib/clin/argument.rb +90 -0
- data/lib/clin/command.rb +195 -0
- data/lib/clin/command_dispatcher.rb +48 -0
- data/lib/clin/command_options_mixin.rb +88 -0
- data/lib/clin/common/help_options.rb +25 -0
- data/lib/clin/errors.rb +31 -0
- data/lib/clin/general_option.rb +15 -0
- data/lib/clin/option.rb +102 -0
- data/lib/clin/version.rb +4 -0
- data/lib/clin.rb +29 -0
- data/spec/cli/argument_spec.rb +117 -0
- data/spec/cli/command_dispacher_spec.rb +84 -0
- data/spec/cli/command_options_mixin_spec.rb +64 -0
- data/spec/cli/command_spec.rb +225 -0
- data/spec/cli/common/help_options_spec.rb +29 -0
- data/spec/cli/option_spec.rb +132 -0
- data/spec/errors_spec.rb +8 -0
- data/spec/examples/simple_spec.rb +31 -0
- data/spec/spec_helper.rb +28 -0
- metadata +171 -0
@@ -0,0 +1,84 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
RSpec.describe Clin::CommandDispatcher do
|
4
|
+
describe '#initialize' do
|
5
|
+
before :all do
|
6
|
+
@cmd1 = Class.new(Clin::Command)
|
7
|
+
@cmd2 = Class.new(Clin::Command)
|
8
|
+
@cmd3 = Class.new(Clin::Command)
|
9
|
+
end
|
10
|
+
|
11
|
+
before do
|
12
|
+
allow(Clin::Command).to receive(:subclasses).and_return([@cmd1, @cmd2, @cmd3])
|
13
|
+
end
|
14
|
+
|
15
|
+
context 'when no argument given' do
|
16
|
+
subject { Clin::CommandDispatcher.new }
|
17
|
+
it { expect(subject.commands.size).to be 3 }
|
18
|
+
it { expect(subject.commands).to include(@cmd1) }
|
19
|
+
it { expect(subject.commands).to include(@cmd2) }
|
20
|
+
it { expect(subject.commands).to include(@cmd3) }
|
21
|
+
end
|
22
|
+
|
23
|
+
context 'when 1 command is given' do
|
24
|
+
subject { Clin::CommandDispatcher.new(@cmd3) }
|
25
|
+
it { expect(subject.commands.size).to be 1 }
|
26
|
+
it { expect(subject.commands).to include(@cmd3) }
|
27
|
+
end
|
28
|
+
|
29
|
+
|
30
|
+
context 'when 2 command is given' do
|
31
|
+
subject { Clin::CommandDispatcher.new(@cmd1, @cmd2) }
|
32
|
+
it { expect(subject.commands.size).to be 2 }
|
33
|
+
it { expect(subject.commands).to include(@cmd1) }
|
34
|
+
it { expect(subject.commands).to include(@cmd2) }
|
35
|
+
end
|
36
|
+
|
37
|
+
context 'when array of command is given' do
|
38
|
+
subject { Clin::CommandDispatcher.new([@cmd1, @cmd2]) }
|
39
|
+
it { expect(subject.commands.size).to be 2 }
|
40
|
+
it { expect(subject.commands).to include(@cmd1) }
|
41
|
+
it { expect(subject.commands).to include(@cmd2) }
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
describe '#parse' do
|
46
|
+
let(:cmd1) { double(:command, parse: 'cmd1', usage: 'cmd1 use') }
|
47
|
+
let(:cmd2) { double(:command, parse: 'cmd2', usage: 'cmd1 use') }
|
48
|
+
let(:args) { %w(some args) }
|
49
|
+
subject { Clin::CommandDispatcher.new(cmd1, cmd2) }
|
50
|
+
context 'when first command match' do
|
51
|
+
before do
|
52
|
+
subject.parse(args)
|
53
|
+
end
|
54
|
+
it { expect(cmd1).to have_received(:parse).with(args, fallback_help: false) }
|
55
|
+
it { expect(subject.parse).to eq('cmd1') }
|
56
|
+
end
|
57
|
+
|
58
|
+
context 'when first command return FixedArgumentError' do
|
59
|
+
before do
|
60
|
+
allow(cmd1).to receive(:parse) { fail Clin::FixedArgumentError, :some }
|
61
|
+
subject.parse(args)
|
62
|
+
end
|
63
|
+
it { expect(cmd1).to have_received(:parse).with(args, fallback_help: false) }
|
64
|
+
it { expect(cmd2).to have_received(:parse).with(args, fallback_help: false) }
|
65
|
+
it { expect(subject.parse).to eq('cmd2') }
|
66
|
+
end
|
67
|
+
|
68
|
+
context 'when first command return ArgumentError' do
|
69
|
+
before do
|
70
|
+
allow(cmd1).to receive(:parse) { fail Clin::ArgumentError, :some }
|
71
|
+
allow(cmd2).to receive(:parse) { fail Clin::ArgumentError, :some }
|
72
|
+
begin
|
73
|
+
subject.parse(args)
|
74
|
+
rescue Clin::CommandLineError => e
|
75
|
+
@error = e
|
76
|
+
end
|
77
|
+
end
|
78
|
+
it { expect { subject.parse }.to raise_error(Clin::CommandLineError) }
|
79
|
+
it { expect(cmd1).to have_received(:parse).with(args, fallback_help: false) }
|
80
|
+
it { expect(cmd2).to have_received(:parse).with(args, fallback_help: false) }
|
81
|
+
it { expect(@error.to_s).to eq(subject.help_message) }
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'clin/command_options_mixin'
|
3
|
+
|
4
|
+
RSpec.describe Clin::CommandOptionsMixin do
|
5
|
+
describe '#add_option' do
|
6
|
+
subject { Class.new(Clin::CommandOptionsMixin) }
|
7
|
+
let(:option) { Clin::Option.new(:name, '-n') }
|
8
|
+
before do
|
9
|
+
subject.add_option option
|
10
|
+
end
|
11
|
+
|
12
|
+
it { expect(subject.options.size).to be 1 }
|
13
|
+
it { expect(subject.options.first).to eq option }
|
14
|
+
it { expect(Clin::CommandOptionsMixin.options.size).to be 0 }
|
15
|
+
end
|
16
|
+
|
17
|
+
describe '#option' do
|
18
|
+
subject { Class.new(Clin::CommandOptionsMixin) }
|
19
|
+
let(:args) { [:name, '-n'] }
|
20
|
+
let(:option) { Clin::Option.new(*args) }
|
21
|
+
|
22
|
+
before do
|
23
|
+
allow(subject).to receive(:add_option)
|
24
|
+
subject.option(*args)
|
25
|
+
end
|
26
|
+
|
27
|
+
it { expect(subject).to have_received(:add_option).with(option) }
|
28
|
+
|
29
|
+
end
|
30
|
+
|
31
|
+
describe '#general_option' do
|
32
|
+
subject { Class.new(Clin::CommandOptionsMixin) }
|
33
|
+
let(:option) { double(:option, register_options: true, new: true) }
|
34
|
+
before do
|
35
|
+
subject.general_option option
|
36
|
+
end
|
37
|
+
it { expect(option).to have_received(:new) }
|
38
|
+
it { expect(subject.general_options.size).to be 1 }
|
39
|
+
it { expect(subject.general_options.values.first).to eq(true) }
|
40
|
+
it { expect(Clin::CommandOptionsMixin.general_options.size).to be 0 }
|
41
|
+
end
|
42
|
+
|
43
|
+
describe '#register_options' do
|
44
|
+
subject { Class.new(Clin::CommandOptionsMixin) }
|
45
|
+
let(:opt1) { double(:option, register: true) }
|
46
|
+
let(:opt2) { double(:option, register: true) }
|
47
|
+
let(:g_opt_cls) { double(:general_option_class, register_options: true) }
|
48
|
+
let(:g_opt) { double(:general_option, class: g_opt_cls) }
|
49
|
+
let(:opts) { double(:options) }
|
50
|
+
let(:out) { double(:out) }
|
51
|
+
before do
|
52
|
+
subject.add_option(opt1)
|
53
|
+
subject.add_option(opt2)
|
54
|
+
subject.general_options = {g_opt_cls => g_opt}
|
55
|
+
|
56
|
+
subject.register_options(opts, out)
|
57
|
+
end
|
58
|
+
|
59
|
+
it { expect(opt1).to have_received(:register).with(opts, out) }
|
60
|
+
it { expect(opt2).to have_received(:register).with(opts, out) }
|
61
|
+
it { expect(g_opt_cls).to have_received(:register_options).with(opts, out) }
|
62
|
+
|
63
|
+
end
|
64
|
+
end
|
@@ -0,0 +1,225 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'clin/command'
|
3
|
+
|
4
|
+
RSpec.describe Clin::Command do
|
5
|
+
describe '#arguments=' do
|
6
|
+
subject { Class.new(Clin::Command) }
|
7
|
+
let(:args) { %w(fix <var> [opt]) }
|
8
|
+
before do
|
9
|
+
allow(Clin::Argument).to receive(:new)
|
10
|
+
end
|
11
|
+
context 'when using string to set arguments' do
|
12
|
+
before do
|
13
|
+
subject.arguments (args.join(' '))
|
14
|
+
end
|
15
|
+
it { expect(subject.args.size).to eq(args.size) }
|
16
|
+
it { expect(Clin::Argument).to have_received(:new).exactly(args.size).times }
|
17
|
+
end
|
18
|
+
|
19
|
+
context 'when using array to set arguments' do
|
20
|
+
before do
|
21
|
+
subject.arguments (args)
|
22
|
+
end
|
23
|
+
it { expect(subject.args.size).to eq(args.size) }
|
24
|
+
it { expect(Clin::Argument).to have_received(:new).exactly(args.size).times }
|
25
|
+
end
|
26
|
+
|
27
|
+
context 'when using array that contains multiple arguments to set arguments' do
|
28
|
+
before do
|
29
|
+
subject.arguments ([args[0], args[1..-1]])
|
30
|
+
end
|
31
|
+
it { expect(subject.args.size).to eq(args.size) }
|
32
|
+
it { expect(Clin::Argument).to have_received(:new).exactly(args.size).times }
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
|
37
|
+
describe '#banner' do
|
38
|
+
subject { Class.new(Clin::Command) }
|
39
|
+
context 'when exe is defined' do
|
40
|
+
let(:exe) { Faker::Lorem.word }
|
41
|
+
before do
|
42
|
+
subject.exe_name(exe)
|
43
|
+
end
|
44
|
+
|
45
|
+
it { expect(subject.banner).to eq("Usage: #{exe} [Options]") }
|
46
|
+
end
|
47
|
+
|
48
|
+
context 'when exe is not defined' do
|
49
|
+
it { expect(subject.banner).to eq('Usage: command [Options]') }
|
50
|
+
end
|
51
|
+
|
52
|
+
context 'when arguments are defined' do
|
53
|
+
let(:arguments) { '<some> [Value]' }
|
54
|
+
before do
|
55
|
+
subject.arguments(arguments)
|
56
|
+
end
|
57
|
+
|
58
|
+
it { expect(subject.banner).to eq("Usage: #{Clin.default_exe_name} #{arguments} [Options]") }
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
describe '#parse_arguments' do
|
63
|
+
subject { Class.new(Clin::Command) }
|
64
|
+
let(:args) { %w(fix <var> [opt]) }
|
65
|
+
|
66
|
+
before do
|
67
|
+
subject.arguments(args)
|
68
|
+
end
|
69
|
+
|
70
|
+
it 'raise argument when fixed in different' do
|
71
|
+
expect { subject.parse_arguments(%w(other val opt)) }.to raise_error(Clin::CommandLineError)
|
72
|
+
end
|
73
|
+
it 'raise error when too few arguments' do
|
74
|
+
expect { subject.parse_arguments(['fix']) }.to raise_error(Clin::CommandLineError)
|
75
|
+
end
|
76
|
+
it 'raise error when too much argument' do
|
77
|
+
expect { subject.parse_arguments(%w(other val opt more)) }
|
78
|
+
.to raise_error(Clin::CommandLineError)
|
79
|
+
end
|
80
|
+
|
81
|
+
it 'map arguments' do
|
82
|
+
expect(subject.parse_arguments(%w(fix val opt))).to eq(fix: 'fix', var: 'val', opt: 'opt')
|
83
|
+
end
|
84
|
+
|
85
|
+
it 'opt argument is nil when not provided' do
|
86
|
+
expect(subject.parse_arguments(%w(fix val))).to eq(fix: 'fix', var: 'val')
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
describe '#parse_options' do
|
91
|
+
subject { Class.new(Clin::Command) }
|
92
|
+
let(:opt1) { Clin::Option.new(:name, 'Set name') }
|
93
|
+
let(:opt2) { Clin::Option.new(:verbose, 'Set verbose', argument: false) }
|
94
|
+
let(:opt3) { Clin::Option.new(:echo, 'Set name', optional_argument: true) }
|
95
|
+
before do
|
96
|
+
subject.add_option opt1
|
97
|
+
subject.add_option opt2
|
98
|
+
subject.add_option opt3
|
99
|
+
end
|
100
|
+
|
101
|
+
it 'raise argument when option value is missing' do
|
102
|
+
expect { subject.parse_options(%w(--name)) }.to raise_error(OptionParser::MissingArgument)
|
103
|
+
end
|
104
|
+
it 'raise error when unknown option' do
|
105
|
+
expect { subject.parse_options(%w(--other)) }.to raise_error(OptionParser::InvalidOption)
|
106
|
+
end
|
107
|
+
|
108
|
+
it { expect(subject.parse_options(%w(--name MyName))).to eq(name: 'MyName') }
|
109
|
+
it { expect(subject.parse_options(%w(--name=MyName))).to eq(name: 'MyName') }
|
110
|
+
it { expect(subject.parse_options(%w(-nMyName))).to eq(name: 'MyName') }
|
111
|
+
|
112
|
+
|
113
|
+
it { expect(subject.parse_options(%w(-v))).to eq(verbose: true) }
|
114
|
+
|
115
|
+
it { expect(subject.parse_options(%w(--echo))).to eq(echo: nil) }
|
116
|
+
it { expect(subject.parse_options(%w(-e EchoThis))).to eq(echo: 'EchoThis') }
|
117
|
+
end
|
118
|
+
|
119
|
+
describe '.handle_dispatch' do
|
120
|
+
subject { Class.new(Clin::Command) }
|
121
|
+
let(:args) { [Faker::Lorem.word, Faker::Lorem.word] }
|
122
|
+
before do
|
123
|
+
subject.arguments(%w(remote <args>...))
|
124
|
+
end
|
125
|
+
|
126
|
+
context 'when only dispatching arguments' do
|
127
|
+
before do
|
128
|
+
subject.dispatch :args
|
129
|
+
allow_any_instance_of(Clin::CommandDispatcher).to receive(:parse)
|
130
|
+
end
|
131
|
+
it 'call the command dispatcher with the right arguments' do
|
132
|
+
expect_any_instance_of(Clin::CommandDispatcher).to receive(:parse).once.with(args)
|
133
|
+
subject.handle_dispatch(remote: 'remote', args: args)
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
context 'when using prefix' do
|
138
|
+
let(:prefix) { 'remote' }
|
139
|
+
before do
|
140
|
+
subject.dispatch :args, prefix: prefix
|
141
|
+
allow_any_instance_of(Clin::CommandDispatcher).to receive(:parse)
|
142
|
+
end
|
143
|
+
it 'call the command dispatcher with the right arguments' do
|
144
|
+
expect_any_instance_of(Clin::CommandDispatcher).to receive(:parse).once.with([prefix] + args)
|
145
|
+
subject.handle_dispatch(remote: 'remote', args: args)
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
149
|
+
context 'when using commands' do
|
150
|
+
let(:cmd1) { double(:command) }
|
151
|
+
let(:cmd2) { double(:command) }
|
152
|
+
before do
|
153
|
+
subject.dispatch :args, commands: [cmd1, cmd2]
|
154
|
+
allow_any_instance_of(Clin::CommandDispatcher).to receive(:initialize)
|
155
|
+
allow_any_instance_of(Clin::CommandDispatcher).to receive(:parse)
|
156
|
+
end
|
157
|
+
it 'call the command dispatcher with the right arguments' do
|
158
|
+
expect_any_instance_of(Clin::CommandDispatcher).to receive(:initialize).once.with([cmd1, cmd2])
|
159
|
+
subject.handle_dispatch(remote: 'remote', args: args)
|
160
|
+
end
|
161
|
+
end
|
162
|
+
|
163
|
+
context 'when dispatcher raise HelpError' do
|
164
|
+
let(:new_message) { Faker::Lorem.sentence }
|
165
|
+
before do
|
166
|
+
subject.dispatch :args
|
167
|
+
allow_any_instance_of(Clin::CommandDispatcher).to receive(:initialize)
|
168
|
+
allow_any_instance_of(Clin::CommandDispatcher).to receive(:parse) do
|
169
|
+
fail Clin::HelpError, 'Dispatcher error'
|
170
|
+
end
|
171
|
+
allow(subject).to receive(:option_parser).and_return(new_message)
|
172
|
+
end
|
173
|
+
it { expect { subject.handle_dispatch(remote: 'remote', args: args) }.to raise_error(Clin::HelpError) }
|
174
|
+
it { expect { subject.handle_dispatch(remote: 'remote', args: args) }.to raise_error(new_message) }
|
175
|
+
end
|
176
|
+
end
|
177
|
+
|
178
|
+
describe '.dispatch_doc' do
|
179
|
+
subject { Class.new(Clin::Command) }
|
180
|
+
before do
|
181
|
+
subject.arguments(%w(remote <args>...))
|
182
|
+
end
|
183
|
+
|
184
|
+
let(:cmd1) { double(:command, usage: 'cmd1') }
|
185
|
+
let(:cmd2) { double(:command, usage: 'cmd2') }
|
186
|
+
let(:cmd3) { double(:command, usage: 'cmd3') }
|
187
|
+
let(:cmds) { [cmd1, cmd2, cmd3] }
|
188
|
+
let(:opts) { double(:option_parser, separator: true) }
|
189
|
+
before do
|
190
|
+
subject.dispatch :args, commands: cmds
|
191
|
+
allow_any_instance_of(Clin::CommandDispatcher).to receive(:initialize)
|
192
|
+
subject.dispatch_doc(opts)
|
193
|
+
end
|
194
|
+
it { expect(opts).to have_received(:separator).at_least(cmds.size).times }
|
195
|
+
end
|
196
|
+
|
197
|
+
describe '.subcommands' do
|
198
|
+
before do
|
199
|
+
@cmd1 = Class.new(Clin::Command)
|
200
|
+
@cmd2 = Class.new(Clin::Command)
|
201
|
+
@abstract_cmd = Class.new(Clin::Command) { abstract true }
|
202
|
+
end
|
203
|
+
|
204
|
+
it { expect(Clin::Command.subcommands).to include(@cmd1) }
|
205
|
+
it { expect(Clin::Command.subcommands).to include(@cmd2) }
|
206
|
+
it { expect(Clin::Command.subcommands).not_to include(@abstract_cmd) }
|
207
|
+
end
|
208
|
+
|
209
|
+
describe '.exe_name' do
|
210
|
+
context 'when not setting the exe_name' do
|
211
|
+
subject { Class.new(Clin::Command) }
|
212
|
+
|
213
|
+
it { expect(subject.exe_name).to eq(Clin.exe_name) }
|
214
|
+
end
|
215
|
+
|
216
|
+
context 'when setting the exe_name' do
|
217
|
+
let(:name) { Faker::Lorem.word }
|
218
|
+
subject { Class.new(Clin::Command) }
|
219
|
+
before do
|
220
|
+
subject.exe_name(name)
|
221
|
+
end
|
222
|
+
it { expect(subject.exe_name).to eq(name) }
|
223
|
+
end
|
224
|
+
end
|
225
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
RSpec.describe Clin::HelpOptions do
|
4
|
+
describe 'options' do
|
5
|
+
let(:out) { {} }
|
6
|
+
it { expect(Clin::HelpOptions.options.size).to be 1 }
|
7
|
+
it 'call the callback' do
|
8
|
+
Clin::HelpOptions.options.first.block.call('opts', out, nil)
|
9
|
+
expect(out).to eq({help: 'opts'})
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
describe '#initialize' do
|
14
|
+
it { expect(Clin::HelpOptions.new(raise: false).instance_variable_get(:@raise)).to be false }
|
15
|
+
it { expect(Clin::HelpOptions.new.instance_variable_get(:@raise)).to be true }
|
16
|
+
end
|
17
|
+
|
18
|
+
describe '#execute' do
|
19
|
+
let(:help) { Faker::Lorem.sentence }
|
20
|
+
context 'when should raise' do
|
21
|
+
subject { Clin::HelpOptions.new(raise: true) }
|
22
|
+
it { expect { subject.execute({help: help}) }.to raise_error(Clin::HelpError) }
|
23
|
+
end
|
24
|
+
context 'when should not raise' do
|
25
|
+
subject { Clin::HelpOptions.new(raise: false) }
|
26
|
+
it { expect { subject.execute({help: help}) }.not_to raise_error }
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,132 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'clin/option'
|
3
|
+
|
4
|
+
RSpec.describe Clin::Option do
|
5
|
+
describe '#initialize' do
|
6
|
+
context 'when initializing with name' do
|
7
|
+
subject { Clin::Option.new(:custom, 'This is my option!') }
|
8
|
+
it { expect(subject.name).to eq(:custom) }
|
9
|
+
it { expect(subject.short).to eq('-c') }
|
10
|
+
it { expect(subject.long).to eq('--custom') }
|
11
|
+
it { expect(subject.argument.to_s).to eq('CUSTOM') }
|
12
|
+
it { expect(subject.block).to be nil }
|
13
|
+
end
|
14
|
+
|
15
|
+
context 'when initializing with block' do
|
16
|
+
let(:block) { proc { puts 'Do stuff' } }
|
17
|
+
subject { Clin::Option.new(:my_option, 'This is my option!', &block) }
|
18
|
+
it { expect(subject.block).to eq(block) }
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
describe '#extract' do
|
23
|
+
let(:out) { Hash.new }
|
24
|
+
let(:opts) { double(:opts) }
|
25
|
+
let(:value) { 'some' }
|
26
|
+
before do
|
27
|
+
allow(opts).to receive(:on) do |*_args, &block|
|
28
|
+
block.call(value)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
context 'when initializing with name' do
|
33
|
+
subject { Clin::Option.new(:my_option, 'This is my option!') }
|
34
|
+
before do
|
35
|
+
subject.register(opts, out)
|
36
|
+
end
|
37
|
+
it { expect(opts).to have_received(:on).once }
|
38
|
+
it { expect(out[:my_option]).to eq(value) }
|
39
|
+
end
|
40
|
+
|
41
|
+
context 'when initializing with block' do
|
42
|
+
let(:block) { proc { |_opts, out, value| out[:some] = value } }
|
43
|
+
subject { Clin::Option.new(:my_option, 'This is my option!', &block) }
|
44
|
+
before do
|
45
|
+
subject.register(opts, out)
|
46
|
+
end
|
47
|
+
it { expect(opts).to have_received(:on).once }
|
48
|
+
it { expect(out[:some]).to eq(value) }
|
49
|
+
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
describe '#on' do
|
54
|
+
let(:out) { Hash.new }
|
55
|
+
let(:value) { 'Some value' }
|
56
|
+
subject { Clin::Option.new(:my_option, 'This is my option!') }
|
57
|
+
|
58
|
+
before do
|
59
|
+
subject.on(value, out)
|
60
|
+
end
|
61
|
+
it { expect(out[:my_option]).to eq(value) }
|
62
|
+
end
|
63
|
+
|
64
|
+
describe '#default_short' do
|
65
|
+
subject { Clin::Option.new(:echo, Faker::Lorem.sentence) }
|
66
|
+
|
67
|
+
it { expect(subject.default_short).to eq('-e') }
|
68
|
+
end
|
69
|
+
|
70
|
+
describe '#default_long' do
|
71
|
+
subject { Clin::Option.new(:echo, Faker::Lorem.sentence) }
|
72
|
+
|
73
|
+
it { expect(subject.default_long).to eq('--echo') }
|
74
|
+
end
|
75
|
+
|
76
|
+
describe '#default_argument' do
|
77
|
+
subject { Clin::Option.new(:echo, Faker::Lorem.sentence) }
|
78
|
+
|
79
|
+
it { expect(subject.default_argument.to_s).to eq('ECHO') }
|
80
|
+
end
|
81
|
+
|
82
|
+
describe '#short' do
|
83
|
+
context 'when short is not specified' do
|
84
|
+
subject { Clin::Option.new(:echo, Faker::Lorem.sentence) }
|
85
|
+
it { expect(subject.short).to eq('-e') }
|
86
|
+
end
|
87
|
+
|
88
|
+
context 'when is set' do
|
89
|
+
subject { Clin::Option.new(:echo, Faker::Lorem.sentence, short: '-c') }
|
90
|
+
it { expect(subject.short).to eq('-c') }
|
91
|
+
end
|
92
|
+
|
93
|
+
context 'when short is set to false' do
|
94
|
+
subject { Clin::Option.new(:echo, Faker::Lorem.sentence, short: false) }
|
95
|
+
it { expect(subject.short).to eq(nil) }
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
describe '#long' do
|
100
|
+
context 'when long is not specified' do
|
101
|
+
subject { Clin::Option.new(:echo, Faker::Lorem.sentence) }
|
102
|
+
it { expect(subject.long).to eq('--echo') }
|
103
|
+
end
|
104
|
+
|
105
|
+
context 'when is set' do
|
106
|
+
subject { Clin::Option.new(:echo, Faker::Lorem.sentence, long: '--eko') }
|
107
|
+
it { expect(subject.long).to eq('--eko') }
|
108
|
+
end
|
109
|
+
|
110
|
+
context 'when long is set to false' do
|
111
|
+
subject { Clin::Option.new(:echo, Faker::Lorem.sentence, long: false) }
|
112
|
+
it { expect(subject.long).to eq(nil) }
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
describe '#argument' do
|
117
|
+
context 'when argument is not specified' do
|
118
|
+
subject { Clin::Option.new(:echo, Faker::Lorem.sentence) }
|
119
|
+
it { expect(subject.argument).to eq('ECHO') }
|
120
|
+
end
|
121
|
+
|
122
|
+
context 'when is set' do
|
123
|
+
subject { Clin::Option.new(:echo, Faker::Lorem.sentence, argument: 'MESSAGE') }
|
124
|
+
it { expect(subject.argument).to eq('MESSAGE') }
|
125
|
+
end
|
126
|
+
|
127
|
+
context 'when argument is set to false' do
|
128
|
+
subject { Clin::Option.new(:echo, Faker::Lorem.sentence, argument: false) }
|
129
|
+
it { expect(subject.argument).to eq(nil) }
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
data/spec/errors_spec.rb
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'examples/simple'
|
3
|
+
|
4
|
+
RSpec.describe 'simple_spec.rb' do
|
5
|
+
suppress_puts
|
6
|
+
it { expect(SimpleCommand.parse('display Some').params).
|
7
|
+
to eq(display: 'display', message: 'Some') }
|
8
|
+
it { expect(SimpleCommand.parse('display "Message with spaces"').params).
|
9
|
+
to eq(display: 'display', message: 'Message with spaces') }
|
10
|
+
|
11
|
+
it { expect(SimpleCommand.parse('display Some -e More').params).
|
12
|
+
to eq(display: 'display', message: 'Some', echo: 'More') }
|
13
|
+
|
14
|
+
it { expect(SimpleCommand.parse('display Some -eMore').params).
|
15
|
+
to eq(display: 'display', message: 'Some', echo: 'More') }
|
16
|
+
|
17
|
+
it { expect(SimpleCommand.parse('display Some -e "Even More"').params).
|
18
|
+
to eq(display: 'display', message: 'Some', echo: 'Even More') }
|
19
|
+
|
20
|
+
it { expect(SimpleCommand.parse('display Some --echo More').params).
|
21
|
+
to eq(display: 'display', message: 'Some', echo: 'More') }
|
22
|
+
|
23
|
+
it { expect(SimpleCommand.parse('display Some --echo=More').params).
|
24
|
+
to eq(display: 'display', message: 'Some', echo: 'More') }
|
25
|
+
|
26
|
+
it { expect(SimpleCommand.parse('display Some --echo "Even More"').params).
|
27
|
+
to eq(display: 'display', message: 'Some', echo: 'Even More') }
|
28
|
+
|
29
|
+
it { expect { SimpleCommand.parse('').params }.to raise_error(Clin::HelpError) }
|
30
|
+
it { expect { SimpleCommand.parse('-h').params }.to raise_error(Clin::HelpError) }
|
31
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'coveralls'
|
2
|
+
Coveralls.wear!
|
3
|
+
$LOAD_PATH.push File.expand_path('../..', __FILE__)
|
4
|
+
$LOAD_PATH.push File.expand_path('../../lib', __FILE__)
|
5
|
+
require 'rspec'
|
6
|
+
require 'faker'
|
7
|
+
require 'clin'
|
8
|
+
|
9
|
+
module Clin::Rspec
|
10
|
+
module Helper
|
11
|
+
|
12
|
+
end
|
13
|
+
|
14
|
+
module Macro
|
15
|
+
def suppress_puts
|
16
|
+
before do
|
17
|
+
allow($stdout).to receive(:puts)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
|
24
|
+
RSpec.configure do |config|
|
25
|
+
config.include Clin::Rspec::Helper
|
26
|
+
config.extend Clin::Rspec::Macro
|
27
|
+
config.order = 'random'
|
28
|
+
end
|