-
Notifications
You must be signed in to change notification settings - Fork 0
/
command_set_spec.rb
402 lines (330 loc) · 11.4 KB
/
command_set_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
# frozen_string_literal: true
RSpec.describe Pry::CommandSet do
let(:set) do
Pry::CommandSet.new { import(Pry::Commands) }
end
describe "#new" do
it "merges other set with itself" do
other_set = described_class.new
other_set.command('test') {}
expect(described_class.new(other_set).count).to eq(1)
end
context "when block given" do
it "instance evals the block" do
other = described_class.new do
command('test') {}
end
expect(described_class.new(other).count).to eq(1)
end
end
end
describe "#block_command" do
it "defines a new command" do
subject.block_command('test')
expect(subject.count).to eq(1)
end
it "assigns default description" do
command = subject.block_command('test')
expect(command.description).to eq('No description.')
end
it "can overwrite default description" do
command = subject.block_command('test', 'test description')
expect(command.description).to eq('test description')
end
it "configures command options" do
command = subject.block_command(
'test', 'test description', some_option: 'some value'
)
expect(command.options).to include(some_option: 'some value')
end
context "when description is a hash" do
it "treats description as options" do
command = subject.block_command('test', some_option: 'some value')
expect(command.options).to include(some_option: 'some value')
end
end
end
describe "#create_command" do
it "defines a new class command" do
subject.create_command('test') {}
expect(subject.count).to eq(1)
end
it "assigns default description" do
command = subject.create_command('test') {}
expect(command.description).to eq('No description.')
end
it "can overwrite default description" do
command = subject.create_command('test', 'test description') {}
expect(command.description).to eq('test description')
end
it "configures command options" do
command = subject.create_command(
'test', 'test description', some_option: 'some value'
) {}
expect(command.options).to include(some_option: 'some value')
end
it "class_evals the given block in the command context" do
command = subject.create_command('test') do
description('class eval description')
end
expect(command.description).to eq('class eval description')
end
context "when description is a hash" do
it "treats description as options" do
command = subject.create_command('test', some_option: 'some value') {}
expect(command.options).to include(some_option: 'some value')
end
end
end
describe "#each" do
it "iterates over commands" do
subject.command('test')
expect(subject.each.first.first).to eq('test')
end
end
describe "#delete" do
it "deletes given commands" do
subject.command('peach')
subject.command('kiwi')
subject.command('apple')
subject.delete('kiwi', 'apple')
expect(subject.count).to eq(1)
end
end
describe "#import" do
let(:first_set) { described_class.new.tap { |set| set.command('first') } }
let(:second_set) { described_class.new.tap { |set| set.command('second') } }
it "imports commands from given sets" do
subject.import(first_set, second_set)
expect(subject.count).to eq(2)
end
it "returns self" do
expect(subject.import(first_set)).to eql(subject)
end
it "includes given sets' helper modules" do
subject.import(first_set, second_set)
expect(subject.helper_module.ancestors.size).to eq(3)
end
end
describe "#import_from" do
let(:other_set) do
set = described_class.new
set.command('kiwi')
set.command('peach')
set.command('plum')
set
end
it "imports matching command from a set" do
subject.import_from(other_set, 'kiwi', 'peach')
expect(subject.count).to eq(2)
end
it "returns self" do
expect(subject.import_from(other_set)).to eql(subject)
end
it "includes other set's helper module" do
subject.import_from(other_set)
expect(subject.helper_module.ancestors.size).to eq(2)
end
end
describe "#find_command_by_match_or_listing" do
it "returns a matching by name command" do
subject.command('test')
command = subject.find_command_by_match_or_listing('test')
expect(command.command_name).to eq('test')
end
it "returns a matching by listing command" do
subject.command('test', listing: 'wtf')
command = subject.find_command_by_match_or_listing('wtf')
expect(command.command_name).to eq('wtf')
end
it "raises ArgumentError on non-matching command" do
expect { subject.find_command_by_match_or_listing('test') }
.to raise_error(ArgumentError, "cannot find a command: 'test'")
end
end
describe "#alias_command" do
before { subject.command('test') }
it "returns the aliased command" do
new_command = subject.alias_command('new-test', 'test')
expect(new_command.command_name).to eq('new-test')
end
it "sets description for the aliased command automatically" do
new_command = subject.alias_command('new-test', 'test')
expect(new_command.description).to eq('Alias for `test`')
end
it "sets aliased command's listing for string alias" do
new_command = subject.alias_command('new-test', 'test')
expect(new_command.options).to include(listing: 'new-test')
end
it "sets aliased command's listing for regex alias" do
new_command = subject.alias_command(/test[!?]+/, 'test')
expect(new_command.options[:listing].to_s).to eq('/test[!?]+/')
end
it "sets group for the aliased command automatically" do
new_command = subject.alias_command('new-test', 'test')
expect(new_command.group).to eq('Aliases')
end
context "when string description is provided" do
it "uses the given description for the aliased command" do
new_command = subject.alias_command('new-test', 'test', desc: 'description')
expect(new_command.description).to eq('description')
end
end
context "when non-string description is provided" do
it "uses the string representation of the given object" do
new_command = subject.alias_command('new-test', 'test', desc: Object.new)
expect(new_command.description).to match(/#<Object.+/)
end
end
context "when command doesn't match" do
it "raises RuntimeError" do
expect { subject.alias_command('nonexisting-command', 'action') }
.to raise_error(RuntimeError, "command: 'action' not found")
end
end
end
describe "#rename_command" do
before { subject.command('test') }
it "renames a command" do
subject.rename_command('new-name', 'test')
expect(subject['test']).to be_nil
expect(subject['new-name']).not_to be_nil
end
it "can optionally set custom description" do
subject.rename_command('new-name', 'test', description: 'new description')
expect(subject['new-name'].description).to eq('new description')
end
context "when provided command is not registered" do
it "raises ArgumentError" do
expect { subject.rename_command('new-name', 'unknown') }
.to raise_error(ArgumentError)
end
end
end
describe "#desc" do
before { subject.command('test') }
it "sets command description" do
subject.desc('test', 'test description')
expect(subject['test'].description).to eq('test description')
end
it "gets command description" do
expect(subject.desc('test')).to eq('No description.')
end
end
describe "#list_commands" do
before do
subject.command('test-one')
subject.command('test-two')
end
it "returns the list of commands" do
expect(subject.list_commands).to eq(%w[test-one test-two])
end
end
describe "#to_hash" do
before { subject.command('test') }
it "converts commands to hash" do
expect(subject.to_hash).to include('test' => respond_to(:command_name))
end
it "doesn't mutate original commands" do
hash = subject.to_hash
hash['foo'] = 'bar'
expect(subject.to_hash).not_to include('foo')
end
end
describe "#[]" do
context "when there's an unambiguous command" do
before { subject.command('test') }
it "selects the command according to the given pattern" do
expect(subject['test']).to respond_to(:command_name)
end
end
context "when there's an ambiguous command" do
before do
subject.command(/\.(.*)/)
subject.command(/\.*(.*)/)
end
it "prefers a command with a higher score" do
expect(subject['.foo'].command_name).to eq("/\\.(.*)/")
expect(subject['..foo'].command_name).to eq("/\\.*(.*)/")
end
end
end
describe "#[]=" do
before { subject.command('test') }
it "rebinds the command with key" do
subject['test-1'] = subject['test']
expect(subject['test-1'].match).to eq('test-1')
end
context "when given command is nil" do
it "deletes the command matching the pattern" do
subject['test'] = nil
expect(subject.count).to be_zero
end
end
context "when given command is not a subclass of Pry::Command" do
it "raises TypeError" do
expect { subject['test'] = 1 }
.to raise_error(TypeError, 'command is not a subclass of Pry::Command')
end
end
end
describe "#add_command" do
it "adds a command" do
subject.add_command(Class.new(Pry::Command))
expect(subject.count).to eq(1)
end
end
describe "#find_command_for_help" do
before { subject.command('test') }
context "when the command can be found" do
it "returns the command" do
expect(subject.find_command_for_help('test')).to respond_to(:command_name)
end
end
context "when the command cannot be found" do
it "returns nil" do
expect(subject.find_command_for_help('foo')).to be_nil
end
end
end
describe "#valid_command?" do
before { subject.command('test') }
context "when command can be found" do
it "returns true" do
expect(subject.valid_command?('test')).to be_truthy
end
end
context "when command cannot be found" do
it "returns false" do
expect(subject.valid_command?('foo')).to be_falsey
end
end
end
describe "#process_line" do
before { subject.command('test') {} }
context "when the given line is a command" do
it "returns a command" do
expect(subject.process_line('test')).to be_command
end
it "returns a non-void command" do
expect(subject.process_line('test')).to be_void_command
end
context "and context is provided" do
before { subject.command('test') { output.puts('kiwi') } }
it "passes the context to the command" do
output = StringIO.new
subject.process_line('test', output: output)
expect(output.string).to eq("kiwi\n")
end
end
end
context "when the given line is not a command" do
it "returns not a command" do
expect(subject.process_line('abcdefg')).not_to be_command
end
it "returns a void result" do
expect(subject.process_line('test')).to be_void_command
end
end
end
end