summaryrefslogtreecommitdiff
path: root/spec/command_set_spec.rb
blob: 76948f5a0014294d7df67b498975725bc31511eb (plain)
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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
require_relative 'helper'

describe Pry::CommandSet do
  before do
    @set = Pry::CommandSet.new do
      import Pry::Commands
    end

    @ctx = {
      :target => binding,
      :command_set => @set,
      :pry_instance => Pry.new
    }
  end

  describe "[]=" do
    it "removes a command from the command set" do
      @set["help"].should.not == nil
      @set["help"] = nil
      @set["help"].should == nil
      lambda { @set.run_command(TOPLEVEL_BINDING, "help") }.should.raise Pry::NoCommandError
    end

    it "replaces a command" do
      old_help = @set["help"]
      @set["help"] = @set["pry-version"]
      @set["help"].should.not == old_help
    end

    it "rebinds the command with key" do
      @set["help-1"] = @set["help"]
      @set["help-1"].match.should == "help-1"
    end

    it "raises a TypeError when command is not a subclass of Pry::Command" do
      lambda { @set["help"] = "hello" }.should.raise TypeError
    end
  end

  it 'should call the block used for the command when it is called' do
    run = false
    @set.command 'foo' do
      run = true
    end

    @set.run_command @ctx, 'foo'
    run.should.be_true
  end

  it 'should pass arguments of the command to the block' do
    @set.command 'foo' do |*args|
      args.should == [1, 2, 3]
    end

    @set.run_command @ctx, 'foo', 1, 2, 3
  end

  it 'should use the first argument as context' do
    ctx = @ctx

    @set.command 'foo' do
      self.context.should == ctx
    end

    @set.run_command @ctx, 'foo'
  end

  it 'should raise an error when calling an undefined command' do
    @set.command('foo') {}
    lambda {
      @set.run_command @ctx, 'bar'
    }.should.raise(Pry::NoCommandError)
  end

  it 'should be able to remove its own commands' do
    @set.command('foo') {}
    @set.delete 'foo'

    lambda {
      @set.run_command @ctx, 'foo'
    }.should.raise(Pry::NoCommandError)
  end

  it 'should be able to remove its own commands, by listing name' do
    @set.command(/^foo1/, 'desc', :listing => 'foo') {}
    @set.delete 'foo'

    lambda {
      @set.run_command @ctx, /^foo1/
    }.should.raise(Pry::NoCommandError)
  end

  it 'should be able to import some commands from other sets' do
    run = false

    other_set = Pry::CommandSet.new do
      command('foo') { run = true }
      command('bar') {}
    end

    @set.import_from(other_set, 'foo')

    @set.run_command @ctx, 'foo'
    run.should.be_true

    lambda {
      @set.run_command @ctx, 'bar'
    }.should.raise(Pry::NoCommandError)
  end

  it 'should return command set after import' do
    run = false

    other_set = Pry::CommandSet.new do
      command('foo') { run = true }
      command('bar') {}
    end

    @set.import(other_set).should == @set
  end

  it 'should return command set after import_from' do
    run = false

    other_set = Pry::CommandSet.new do
      command('foo') { run = true }
      command('bar') {}
    end

    @set.import_from(other_set, 'foo').should == @set
  end

  it 'should be able to import some commands from other sets using listing name' do
    run = false

    other_set = Pry::CommandSet.new do
      command(/^foo1/, 'desc', :listing => 'foo') { run = true }
    end

    @set.import_from(other_set, 'foo')

    @set.run_command @ctx, /^foo1/
    run.should.be_true
  end

  it 'should be able to import a whole set' do
    run = []

    other_set = Pry::CommandSet.new do
      command('foo') { run << true }
      command('bar') { run << true }
    end

    @set.import other_set

    @set.run_command @ctx, 'foo'
    @set.run_command @ctx, 'bar'
    run.should == [true, true]
  end

  it 'should be able to import sets at creation' do
    run = false
    @set.command('foo') { run = true }

    Pry::CommandSet.new(@set).run_command @ctx, 'foo'
    run.should.be_true
  end

  it 'should set the descriptions of commands' do
    @set.command('foo', 'some stuff') {}
    @set['foo'].description.should == 'some stuff'
  end

  describe "aliases" do
    it 'should be able to alias command' do
      run = false
      @set.command('foo', 'stuff') { run = true }

      @set.alias_command 'bar', 'foo'
      @set['bar'].match.should == 'bar'
      @set['bar'].description.should == 'Alias for `foo`'

      @set.run_command @ctx, 'bar'
      run.should.be_true
    end

    it "should be able to alias command with command_prefix" do
      run = false

      begin
        @set.command('owl', 'stuff') { run = true }
        @set.alias_command 'owlet', 'owl'

        Pry.config.command_prefix = '%'
        @set['%owlet'].match.should == 'owlet'
        @set['%owlet'].description.should == 'Alias for `owl`'

        @set.run_command @ctx, 'owlet'
        run.should == true
      ensure
        Pry.config.command_prefix = ''
      end
    end

    it 'should inherit options from original command' do
      run = false
      @set.command('foo', 'stuff', :shellwords => true, :interpolate => false) { run = true }

      @set.alias_command 'bar', 'foo'
      @set['bar'].options[:shellwords].should == @set['foo'].options[:shellwords]
      @set['bar'].options[:interpolate].should == @set['foo'].options[:interpolate]

      # however some options should not be inherited
      @set['bar'].options[:listing].should.not ==  @set['foo'].options[:listing]
      @set['bar'].options[:listing].should == "bar"
    end

    it 'should be able to specify alias\'s description when aliasing' do
      run = false
      @set.command('foo', 'stuff') { run = true }

      @set.alias_command 'bar', 'foo', :desc => "tobina"
      @set['bar'].match.should == 'bar'
      @set['bar'].description.should == "tobina"

      @set.run_command @ctx, 'bar'
      run.should.be_true
    end

    it "should be able to alias a command by its invocation line" do
      run = false
      @set.command(/^foo1/, 'stuff', :listing => 'foo') { run = true }

      @set.alias_command 'bar', 'foo1'
      @set['bar'].match.should == 'bar'
      @set['bar'].description.should == 'Alias for `foo1`'

      @set.run_command @ctx, 'bar'
      run.should.be_true
    end

    it "should be able to specify options when creating alias" do
      run = false
      @set.command(/^foo1/, 'stuff', :listing => 'foo') { run = true }

      @set.alias_command /^b.r/, 'foo1', :listing => "bar"
      @set.to_hash[/^b.r/].options[:listing].should == "bar"
    end

    it "should set description to default if description parameter is nil" do
      run = false
      @set.command(/^foo1/, 'stuff', :listing => 'foo') { run = true }

      @set.alias_command "bar", 'foo1'
      @set["bar"].description.should == "Alias for `foo1`"
    end
  end

  it 'should be able to change the descriptions of commands' do
    @set.command('foo', 'bar') {}
    @set.desc 'foo', 'baz'

    @set['foo'].description.should == 'baz'
  end

  it 'should get the descriptions of commands' do
    @set.command('foo', 'bar') {}
    @set.desc('foo').should == 'bar'
  end

  it 'should get the descriptions of commands, by listing' do
    @set.command(/^foo1/, 'bar', :listing => 'foo') {}
    @set.desc('foo').should == 'bar'
  end

  it 'should return Pry::Command::VOID_VALUE for commands by default' do
    @set.command('foo') { 3 }
    @set.run_command(@ctx, 'foo').should == Pry::Command::VOID_VALUE
  end

  it 'should be able to keep return values' do
    @set.command('foo', '', :keep_retval => true) { 3 }
    @set.run_command(@ctx, 'foo').should == 3
  end

  it 'should be able to keep return values, even if return value is nil' do
    @set.command('foo', '', :keep_retval => true) { nil }
    @set.run_command(@ctx, 'foo').should == nil
  end

  it 'should be able to have its own helpers' do
    @set.command('foo') do
      should.respond_to :my_helper
    end

    @set.helpers do
      def my_helper; end
    end

    @set.run_command(@ctx, 'foo')
    Pry::Command.subclass('foo', '', {}, Module.new).new({:target => binding}).should.not.respond_to :my_helper
  end

  it 'should not recreate a new helper module when helpers is called' do
    @set.command('foo') do
      should.respond_to :my_helper
      should.respond_to :my_other_helper
    end

    @set.helpers do
      def my_helper; end
    end

    @set.helpers do
      def my_other_helper; end
    end

    @set.run_command(@ctx, 'foo')
  end

  it 'should import helpers from imported sets' do
    imported_set = Pry::CommandSet.new do
      helpers do
        def imported_helper_method; end
      end
    end

    @set.import imported_set
    @set.command('foo') { should.respond_to :imported_helper_method }
    @set.run_command(@ctx, 'foo')
  end

  it 'should import helpers even if only some commands are imported' do
    imported_set = Pry::CommandSet.new do
      helpers do
        def imported_helper_method; end
      end

      command('bar') {}
    end

    @set.import_from imported_set, 'bar'
    @set.command('foo') { should.respond_to :imported_helper_method }
    @set.run_command(@ctx, 'foo')
  end

  it 'should provide a :listing for a command that defaults to its name' do
    @set.command 'foo', "" do;end
    @set['foo'].options[:listing].should == 'foo'
  end

  it 'should provide a :listing for a command that differs from its name' do
    @set.command 'foo', "", :listing => 'bar' do;end
    @set['foo'].options[:listing].should == 'bar'
  end

  it "should provide a 'help' command" do
    @ctx[:command_set] = @set
    @ctx[:output] = StringIO.new

    lambda {
      @set.run_command(@ctx, 'help')
    }.should.not.raise
  end


  describe "renaming a command" do
    it 'should be able to rename and run a command' do
      run = false
      @set.command('foo') { run = true }
      @set.rename_command('bar', 'foo')
      @set.run_command(@ctx, 'bar')
      run.should.be_true
    end

    it 'should accept listing name when renaming a command' do
      run = false
      @set.command('foo', "", :listing => 'love') { run = true }
      @set.rename_command('bar', 'love')
      @set.run_command(@ctx, 'bar')
      run.should.be_true
    end

    it 'should raise exception trying to rename non-existent command' do
      lambda { @set.rename_command('bar', 'foo') }.should.raise ArgumentError
    end

    it 'should make old command name inaccessible' do
      @set.command('foo') { }
      @set.rename_command('bar', 'foo')
      lambda { @set.run_command(@ctx, 'foo') }.should.raise Pry::NoCommandError
    end

    it 'should be able to pass in options when renaming command' do
      desc    = "hello"
      listing = "bing"
      @set.command('foo') { }
      @set.rename_command('bar', 'foo', :description => desc, :listing => listing, :keep_retval => true)
      @set['bar'].description.should           == desc
      @set['bar'].options[:listing].should     == listing
      @set['bar'].options[:keep_retval].should.be_true
    end
  end

  describe "command decorators - before_command and after_command" do
    describe "before_command" do
      it 'should be called before the original command' do
        foo = []
        @set.command('foo') { foo << 1 }
        @set.before_command('foo') { foo << 2 }
        @set.run_command(@ctx, 'foo')

        foo.should == [2, 1]
      end

      it 'should be called before the original command, using listing name' do
        foo = []
        @set.command(/^foo1/, '', :listing => 'foo') { foo << 1 }
        @set.before_command('foo') { foo << 2 }
        @set.run_command(@ctx, /^foo1/)

        foo.should == [2, 1]
      end

      it 'should share the context with the original command' do
        @ctx[:target] = "test target string".__binding__
        before_val  = nil
        orig_val    = nil
        @set.command('foo') { orig_val = target }
        @set.before_command('foo') { before_val = target }
        @set.run_command(@ctx, 'foo')

        before_val.should == @ctx[:target]
        orig_val.should == @ctx[:target]
      end

      it 'should work when applied multiple times' do
        foo = []
        @set.command('foo') { foo << 1 }
        @set.before_command('foo') { foo << 2 }
        @set.before_command('foo') { foo << 3 }
        @set.before_command('foo') { foo << 4 }
        @set.run_command(@ctx, 'foo')

        foo.should == [4, 3, 2, 1]
      end

    end

    describe "after_command" do
      it 'should be called after the original command' do
        foo = []
        @set.command('foo') { foo << 1 }
        @set.after_command('foo') { foo << 2 }
        @set.run_command(@ctx, 'foo')

        foo.should == [1, 2]
      end

      it 'should be called after the original command, using listing name' do
        foo = []
        @set.command(/^foo1/, '', :listing => 'foo') { foo << 1 }
        @set.after_command('foo') { foo << 2 }
        @set.run_command(@ctx, /^foo1/)

        foo.should == [1, 2]
      end

      it 'should share the context with the original command' do
        @ctx[:target] = "test target string".__binding__
        after_val   = nil
        orig_val    = nil
        @set.command('foo') { orig_val = target }
        @set.after_command('foo') { after_val = target }
        @set.run_command(@ctx, 'foo')

        after_val.should == @ctx[:target]
        orig_val.should == @ctx[:target]
      end

      it 'should determine the return value for the command' do
        @set.command('foo', 'bar', :keep_retval => true) { 1 }
        @set.after_command('foo') { 2 }
        @set.run_command(@ctx, 'foo').should == 2
      end

      it 'should work when applied multiple times' do
        foo = []
        @set.command('foo') { foo << 1 }
        @set.after_command('foo') { foo << 2 }
        @set.after_command('foo') { foo << 3 }
        @set.after_command('foo') { foo << 4 }
        @set.run_command(@ctx, 'foo')

        foo.should == [1, 2, 3, 4]
      end
    end

    describe "before_command and after_command" do
      it 'should work when combining both before_command and after_command' do
        foo = []
        @set.command('foo') { foo << 1 }
        @set.after_command('foo') { foo << 2 }
        @set.before_command('foo') { foo << 3 }
        @set.run_command(@ctx, 'foo')

        foo.should == [3, 1, 2]
      end

    end

  end

  describe 'find_command' do
    it 'should find commands with the right string' do
      cmd = @set.command('rincewind'){ }
      @set.find_command('rincewind').should == cmd
    end

    it 'should not find commands with spaces before' do
      cmd = @set.command('luggage'){ }
      @set.find_command(' luggage').should == nil
    end

    it 'should find commands with arguments after' do
      cmd = @set.command('vetinari'){ }
      @set.find_command('vetinari --knock 3').should == cmd
    end

    it 'should find commands with names containing spaces' do
      cmd = @set.command('nobby nobbs'){ }
      @set.find_command('nobby nobbs --steal petty-cash').should == cmd
    end

    it 'should find command defined by regex' do
      cmd = @set.command(/(capt|captain) vimes/i){ }
      @set.find_command('Capt Vimes').should == cmd
    end

    it 'should find commands defined by regex with arguments' do
      cmd = @set.command(/(cpl|corporal) Carrot/i){ }
      @set.find_command('cpl carrot --write-home').should == cmd
    end

    it 'should not find commands by listing' do
      cmd = @set.command(/werewol(f|ve)s?/, 'only once a month', :listing => "angua"){ }
      @set.find_command('angua').should == nil
    end

    it 'should not find commands without command_prefix' do
      begin
        Pry.config.command_prefix = '%'
        cmd = @set.command('detritus'){ }
        @set.find_command('detritus').should == nil
      ensure
        Pry.config.command_prefix = ''
      end
    end

    it "should find commands that don't use the prefix" do
      begin
        Pry.config.command_prefix = '%'
        cmd = @set.command('colon', 'Sergeant Fred', :use_prefix => false){ }
        @set.find_command('colon').should == cmd
      ensure
        Pry.config.command_prefix = ''
      end
    end

    it "should find the command that has the longest match" do
      cmd = @set.command(/\.(.*)/){ }
      cmd2 = @set.command(/\.\|\|(.*)/){ }
      @set.find_command('.||').should == cmd2
    end

    it "should find the command that has the longest name" do
      cmd = @set.command(/\.(.*)/){ }
      cmd2 = @set.command('.||'){ }
      @set.find_command('.||').should == cmd2
    end
  end

  describe '.valid_command?' do
    it 'should be true for commands that can be found' do
      cmd = @set.command('archchancellor')
      @set.valid_command?('archchancellor of_the?(:University)').should.be_true
    end

    it 'should be false for commands that can\'' do
      @set.valid_command?('def monkey(ape)').should.be_false
    end

    it 'should not cause argument interpolation' do
      cmd = @set.command('hello')
      lambda {
        @set.valid_command?('hello #{raise "futz"}')
      }.should.not.raise
    end
  end

  describe '.process_line' do

    it 'should return Result.new(false) if there is no matching command' do
     result = @set.process_line('1 + 42')
     result.command?.should.be_false
     result.void_command?.should.be_false
     result.retval.should == nil
    end

    it 'should return Result.new(true, VOID) if the command is not keep_retval' do
      @set.create_command('mrs-cake') do
        def process; 42; end
      end

      result = @set.process_line('mrs-cake')
      result.command?.should.be_true
      result.void_command?.should.be_true
      result.retval.should == Pry::Command::VOID_VALUE
    end

    it 'should return Result.new(true, retval) if the command is keep_retval' do
      @set.create_command('magrat', 'the maiden', :keep_retval => true) do
        def process; 42; end
      end

      result = @set.process_line('magrat')
      result.command?.should.be_true
      result.void_command?.should.be_false
      result.retval.should == 42
    end

    it 'should pass through context' do
      ctx = {
        :eval_string => "bloomers",
        :pry_instance => Object.new,
        :output => StringIO.new,
        :target => binding
      }
      @set.create_command('agnes') do
        define_method(:process) do
          eval_string.should == ctx[:eval_string]
          output.should == ctx[:output]
          target.should == ctx[:target]
          _pry_.should == ctx[:pry_instance]
        end
      end

      @set.process_line('agnes', ctx)
    end

    it 'should add command_set to context' do
      set = @set
      @set.create_command(/nann+y ogg+/) do
        define_method(:process) do
          command_set.should == set
        end
      end

      @set.process_line('nannnnnny oggggg')
    end
  end

  if defined?(Bond)
    describe '.complete' do
      it "should list all command names" do
        @set.create_command('susan'){ }
        @set.complete('sus').should.include 'susan '
      end

      it "should delegate to commands" do
        @set.create_command('susan'){ def complete(search); ['--foo']; end }
        @set.complete('susan ').should == ['--foo']
      end
    end
  end
end