pry 0.8.4pre1-i386-mswin32 → 0.9.0-i386-mswin32

Sign up to get free protection for your applications and to get access to all the features.
Files changed (60) hide show
  1. data/.gitignore +1 -0
  2. data/CHANGELOG +25 -6
  3. data/README.markdown +11 -4
  4. data/Rakefile +15 -19
  5. data/TODO +28 -2
  6. data/bin/pry +28 -11
  7. data/examples/example_basic.rb +2 -4
  8. data/examples/example_command_override.rb +2 -5
  9. data/examples/example_commands.rb +1 -4
  10. data/examples/example_hooks.rb +2 -5
  11. data/examples/example_image_edit.rb +4 -8
  12. data/examples/example_input.rb +1 -4
  13. data/examples/example_input2.rb +1 -4
  14. data/examples/example_output.rb +1 -4
  15. data/examples/example_print.rb +2 -5
  16. data/examples/example_prompt.rb +2 -5
  17. data/examples/helper.rb +6 -0
  18. data/lib/pry.rb +59 -3
  19. data/lib/pry/command_context.rb +10 -9
  20. data/lib/pry/command_processor.rb +51 -73
  21. data/lib/pry/command_set.rb +79 -28
  22. data/lib/pry/commands.rb +9 -123
  23. data/lib/pry/completion.rb +30 -29
  24. data/lib/pry/config.rb +100 -0
  25. data/lib/pry/default_commands/basic.rb +37 -0
  26. data/lib/pry/default_commands/context.rb +16 -15
  27. data/lib/pry/default_commands/documentation.rb +73 -54
  28. data/lib/pry/default_commands/easter_eggs.rb +1 -20
  29. data/lib/pry/default_commands/gems.rb +31 -40
  30. data/lib/pry/default_commands/input.rb +223 -15
  31. data/lib/pry/default_commands/introspection.rb +108 -73
  32. data/lib/pry/default_commands/ls.rb +25 -11
  33. data/lib/pry/default_commands/shell.rb +29 -39
  34. data/lib/pry/extended_commands/experimental.rb +17 -0
  35. data/lib/pry/extended_commands/user_command_api.rb +22 -0
  36. data/lib/pry/helpers.rb +1 -0
  37. data/lib/pry/helpers/base_helpers.rb +15 -104
  38. data/lib/pry/helpers/command_helpers.rb +96 -59
  39. data/lib/pry/helpers/text.rb +83 -0
  40. data/lib/pry/history_array.rb +105 -0
  41. data/lib/pry/plugins.rb +79 -0
  42. data/lib/pry/pry_class.rb +102 -114
  43. data/lib/pry/pry_instance.rb +123 -55
  44. data/lib/pry/version.rb +1 -1
  45. data/pry.gemspec +45 -0
  46. data/test/helper.rb +57 -7
  47. data/test/test_command_processor.rb +205 -0
  48. data/test/{test_commandset.rb → test_command_set.rb} +18 -12
  49. data/test/test_default_commands.rb +59 -0
  50. data/test/test_default_commands/test_context.rb +64 -0
  51. data/test/test_default_commands/test_documentation.rb +31 -0
  52. data/test/test_default_commands/test_gems.rb +14 -0
  53. data/test/test_default_commands/test_input.rb +327 -0
  54. data/test/test_default_commands/test_introspection.rb +155 -0
  55. data/test/test_history_array.rb +65 -0
  56. data/test/test_pry.rb +548 -313
  57. metadata +48 -15
  58. data/lib/pry/hooks.rb +0 -17
  59. data/lib/pry/print.rb +0 -16
  60. data/lib/pry/prompts.rb +0 -31
@@ -0,0 +1,65 @@
1
+ require 'helper'
2
+
3
+ describe Pry::HistoryArray do
4
+ before do
5
+ @array = Pry::HistoryArray.new 10
6
+ end
7
+
8
+ it 'should have a maximum size specifed at creation time' do
9
+ @array.max_size.should == 10
10
+ end
11
+
12
+ it 'should be able to be added objects to' do
13
+ @array << 1 << 2 << 3
14
+ @array.size.should == 3
15
+ @array.to_a.should == [1, 2, 3]
16
+ end
17
+
18
+ it 'should be able to access single elements' do
19
+ @array << 1 << 2 << 3
20
+ @array[2].should == 3
21
+ end
22
+
23
+ it 'should be able to access negative indices' do
24
+ @array << 1 << 2 << 3
25
+ @array[-1].should == 3
26
+ end
27
+
28
+ it 'should be able to access ranges' do
29
+ @array << 1 << 2 << 3 << 4
30
+ @array[1..2].should == [2, 3]
31
+ end
32
+
33
+ it 'should be able to access ranges starting from a negative index' do
34
+ @array << 1 << 2 << 3 << 4
35
+ @array[-2..3].should == [3, 4]
36
+ end
37
+
38
+ it 'should be able to access ranges ending at a negative index' do
39
+ @array << 1 << 2 << 3 << 4
40
+ @array[2..-1].should == [3, 4]
41
+ end
42
+
43
+ it 'should be able to access ranges using only negative indices' do
44
+ @array << 1 << 2 << 3 << 4
45
+ @array[-2..-1].should == [3, 4]
46
+ end
47
+
48
+ it 'should be able to use range where end is excluded' do
49
+ @array << 1 << 2 << 3 << 4
50
+ @array[-2...-1].should == [3]
51
+ end
52
+
53
+ it 'should be able to access slices using a size' do
54
+ @array << 1 << 2 << 3 << 4
55
+ @array[-3, 2].should == [2, 3]
56
+ end
57
+
58
+ it 'should remove older entries' do
59
+ 11.times { |n| @array << n }
60
+
61
+ @array[0].should == nil
62
+ @array[1].should == 1
63
+ @array[10].should == 10
64
+ end
65
+ end
@@ -6,6 +6,15 @@ puts "With method_source version #{MethodSource::VERSION}"
6
6
  puts "--"
7
7
 
8
8
  describe Pry do
9
+
10
+ describe 'warning emissions' do
11
+ it 'should emit no warnings' do
12
+ Open4.popen4 'ruby -I lib -rubygems -r"pry" -W -e "exit"' do |pid, stdin, stdout, stderr|
13
+ stderr.read.empty?.should == true
14
+ end
15
+ end
16
+ end
17
+
9
18
  describe "open a Pry session on an object" do
10
19
  describe "rep" do
11
20
  before do
@@ -31,6 +40,17 @@ describe Pry do
31
40
  @excep.is_a?(NameError).should == true
32
41
  end
33
42
 
43
+ if defined?(BasicObject)
44
+ it 'should be able to operate inside the BasicObject class' do
45
+ $obj = nil
46
+ redirect_pry_io(InputTester.new(":foo", "$obj = _", "exit-all"), StringIO.new) do
47
+ BasicObject.pry
48
+ end
49
+ $obj.should == :foo
50
+ $obj = nil
51
+ end
52
+ end
53
+
34
54
  it 'should set an ivar on an object' do
35
55
  input_string = "@x = 10"
36
56
  input = InputTester.new(input_string)
@@ -134,14 +154,80 @@ describe Pry do
134
154
  end
135
155
  end
136
156
 
157
+ describe "history arrays" do
158
+ it 'sets _ to the last result' do
159
+ res = []
160
+ input = InputTester.new *[":foo", "self << _", "42", "self << _"]
161
+ pry = Pry.new(:input => input, :output => Pry::NullOutput)
162
+ pry.repl(res)
163
+
164
+ res.should == [:foo, 42]
165
+ end
166
+
167
+ it 'sets out to an array with the result' do
168
+ res = {}
169
+ input = InputTester.new *[":foo", "42", "self[:res] = out"]
170
+ pry = Pry.new(:input => input, :output => Pry::NullOutput)
171
+ pry.repl(res)
172
+
173
+ res[:res].should.be.kind_of Pry::HistoryArray
174
+ res[:res][1..2].should == [:foo, 42]
175
+ end
176
+
177
+ it 'sets inp to an array with the entered lines' do
178
+ res = {}
179
+ input = InputTester.new *[":foo", "42", "self[:res] = inp"]
180
+ pry = Pry.new(:input => input, :output => Pry::NullOutput)
181
+ pry.repl(res)
182
+
183
+ res[:res].should.be.kind_of Pry::HistoryArray
184
+ res[:res][1..2].should == [":foo\n", "42\n"]
185
+ end
186
+
187
+ it 'uses 100 as the size of inp and out' do
188
+ res = []
189
+ input = InputTester.new *["self << out.max_size << inp.max_size"]
190
+ pry = Pry.new(:input => input, :output => Pry::NullOutput)
191
+ pry.repl(res)
192
+
193
+ res.should == [100, 100]
194
+ end
195
+
196
+ it 'can change the size of the history arrays' do
197
+ res = []
198
+ input = InputTester.new *["self << out.max_size << inp.max_size"]
199
+ pry = Pry.new(:input => input, :output => Pry::NullOutput,
200
+ :memory_size => 1000)
201
+ pry.repl(res)
202
+
203
+ res.should == [1000, 1000]
204
+ end
205
+
206
+ it 'store exceptions' do
207
+ res = []
208
+ input = InputTester.new *["foo!","self << inp[-1] << out[-1]"]
209
+ pry = Pry.new(:input => input, :output => Pry::NullOutput,
210
+ :memory_size => 1000)
211
+ pry.repl(res)
212
+
213
+ res.first.should == "foo!\n"
214
+ res.last.should.be.kind_of NoMethodError
215
+ end
216
+ end
217
+
137
218
  describe "test loading rc files" do
219
+
220
+ before do
221
+ Pry.instance_variable_set(:@initial_session, true)
222
+ end
223
+
138
224
  after do
139
225
  Pry::RC_FILES.clear
140
- Pry.should_load_rc = false
226
+ Pry.config.should_load_rc = false
141
227
  end
142
228
 
143
229
  it "should run the rc file only once" do
144
- Pry.should_load_rc = true
230
+ Pry.config.should_load_rc = true
145
231
  Pry::RC_FILES << File.expand_path("../testrc", __FILE__)
146
232
 
147
233
  Pry.start(self, :input => StringIO.new("exit\n"), :output => Pry::NullOutput)
@@ -153,17 +239,17 @@ describe Pry do
153
239
  Object.remove_const(:TEST_RC)
154
240
  end
155
241
 
156
- it "should not run the rc file at all if Pry.should_load_rc is false" do
157
- Pry.should_load_rc = false
242
+ it "should not run the rc file at all if Pry.config.should_load_rc is false" do
243
+ Pry.config.should_load_rc = false
158
244
  Pry.start(self, :input => StringIO.new("exit\n"), :output => Pry::NullOutput)
159
245
  Object.const_defined?(:TEST_RC).should == false
160
246
  end
161
247
 
162
248
  it "should not load the rc file if #repl method invoked" do
163
- Pry.should_load_rc = true
249
+ Pry.config.should_load_rc = true
164
250
  Pry.new(:input => StringIO.new("exit\n"), :output => Pry::NullOutput).repl(self)
165
251
  Object.const_defined?(:TEST_RC).should == false
166
- Pry.should_load_rc = false
252
+ Pry.config.should_load_rc = false
167
253
  end
168
254
  end
169
255
 
@@ -219,10 +305,8 @@ describe Pry do
219
305
  val.class.instance_methods(false).map(&:to_sym).include?(:hello).should == true
220
306
  end
221
307
  end
222
-
223
308
  end
224
309
 
225
-
226
310
  describe "commands" do
227
311
  it 'should run a command with no parameter' do
228
312
  pry_tester = Pry.new
@@ -386,70 +470,180 @@ describe Pry do
386
470
  str_output2.string.should =~ /7/
387
471
  end
388
472
 
389
- # describe "Pry.run_command" do
390
- # before do
391
- # class RCTest
392
- # def a() end
393
- # B = 20
394
- # @x = 10
395
- # end
396
- # end
397
-
398
- # after do
399
- # Object.remove_const(:RCTest)
400
- # end
401
-
402
- # it "should execute command in the appropriate object context" do
403
- # result = Pry.run_command "ls", :context => RCTest
404
- # result.map(&:to_sym).should == [:@x]
405
- # end
406
-
407
- # it "should execute command with parameters in the appropriate object context" do
408
- # result = Pry.run_command "ls -M", :context => RCTest
409
- # result.map(&:to_sym).should == [:a]
410
- # end
411
-
412
- # it "should execute command and show output with :show_output => true flag" do
413
- # str = StringIO.new
414
- # Pry.output = str
415
- # result = Pry.run_command "ls -afv", :context => RCTest, :show_output => true
416
- # str.string.should =~ /global variables/
417
- # Pry.output = $stdout
418
- # end
419
-
420
- # it "should execute command with multiple parameters" do
421
- # result = Pry.run_command "ls -c -M RCTest"
422
- # result.map(&:to_sym).should == [:a, :B]
423
- # end
424
- # end
425
-
426
473
  describe "commands" do
427
474
  it 'should interpolate ruby code into commands' do
428
- klass = Pry::CommandSet.new :test do
475
+ klass = Pry::CommandSet.new do
429
476
  command "hello", "", :keep_retval => true do |arg|
430
477
  arg
431
478
  end
432
479
  end
433
480
 
434
- @test_interpolation = "bing"
481
+ $test_interpolation = "bing"
435
482
  str_output = StringIO.new
436
- Pry.new(:input => StringIO.new("hello #{@test_interpolation}"), :output => str_output, :commands => klass).rep
483
+ Pry.new(:input => StringIO.new('hello #{$test_interpolation}'), :output => str_output, :commands => klass).rep
437
484
  str_output.string.should =~ /bing/
438
- @test_interpolation = nil
485
+ $test_interpolation = nil
486
+ end
487
+
488
+ it 'should NOT interpolate ruby code into commands if :interpolate => false' do
489
+ klass = Pry::CommandSet.new do
490
+ command "hello", "", :keep_retval => true, :interpolate => false do |arg|
491
+ arg
492
+ end
493
+ end
494
+
495
+ $test_interpolation = "bing"
496
+ str_output = StringIO.new
497
+ Pry.new(:input => StringIO.new('hello #{$test_interpolation}'), :output => str_output, :commands => klass).rep
498
+ str_output.string.should =~ /test_interpolation/
499
+ $test_interpolation = nil
500
+ end
501
+
502
+ it 'should NOT try to interpolate pure ruby code (no commands) ' do
503
+ str_output = StringIO.new
504
+ Pry.new(:input => StringIO.new('puts \'#{aggy}\''), :output => str_output).rep
505
+ str_output.string.should.not =~ /NameError/
506
+
507
+ Pry.new(:input => StringIO.new('puts #{aggy}'), :output => str_output).rep
508
+ str_output.string.should.not =~ /NameError/
509
+
510
+ $test_interpolation = "blah"
511
+ Pry.new(:input => StringIO.new('puts \'#{$test_interpolation}\''), :output => str_output).rep
512
+
513
+ str_output.string.should.not =~ /blah/
514
+ $test_interpolation = nil
515
+ end
516
+
517
+ it 'should create a command with a space in its name' do
518
+ set = Pry::CommandSet.new do
519
+ command "hello baby", "" do
520
+ output.puts "hello baby command"
521
+ end
522
+ end
523
+
524
+ str_output = StringIO.new
525
+ redirect_pry_io(InputTester.new("hello baby", "exit-all"), str_output) do
526
+ Pry.new(:commands => set).rep
527
+ end
528
+
529
+ str_output.string.should =~ /hello baby command/
530
+ end
531
+
532
+ it 'should create a command with a space in its name and pass an argument' do
533
+ set = Pry::CommandSet.new do
534
+ command "hello baby", "" do |arg|
535
+ output.puts "hello baby command #{arg}"
536
+ end
537
+ end
538
+
539
+ str_output = StringIO.new
540
+ redirect_pry_io(InputTester.new("hello baby john"), str_output) do
541
+ Pry.new(:commands => set).rep
542
+ end
543
+
544
+ str_output.string.should =~ /hello baby command john/
545
+ end
546
+
547
+ it 'should create a regex command and be able to invoke it' do
548
+ set = Pry::CommandSet.new do
549
+ command /hello(.)/, "" do
550
+ c = captures.first
551
+ output.puts "hello#{c}"
552
+ end
553
+ end
554
+
555
+ str_output = StringIO.new
556
+ redirect_pry_io(InputTester.new("hello1"), str_output) do
557
+ Pry.new(:commands => set).rep
558
+ end
559
+
560
+ str_output.string.should =~ /hello1/
561
+ end
562
+
563
+ it 'should create a regex command and pass captures into the args list before regular arguments' do
564
+ set = Pry::CommandSet.new do
565
+ command /hello(.)/, "" do |c1, a1|
566
+ output.puts "hello #{c1} #{a1}"
567
+ end
568
+ end
569
+
570
+ str_output = StringIO.new
571
+ redirect_pry_io(InputTester.new("hello1 baby"), str_output) do
572
+ Pry.new(:commands => set).rep
573
+ end
574
+
575
+ str_output.string.should =~ /hello 1 baby/
439
576
  end
440
577
 
441
- it 'should create a comand in a nested context and that command should be accessible from the parent' do
442
- str_input = StringIO.new("@x=nil\ncd 7\n_pry_.commands.instance_eval {\ncommand('bing') { |arg| run arg }\n}\ncd ..\nbing ls\nexit")
578
+ it 'should create a regex command and interpolate the captures' do
579
+ set = Pry::CommandSet.new do
580
+ command /hello (.*)/, "" do |c1|
581
+ output.puts "hello #{c1}"
582
+ end
583
+ end
584
+
585
+ str_output = StringIO.new
586
+ $obj = "bing"
587
+ redirect_pry_io(InputTester.new('hello #{$obj}'), str_output) do
588
+ Pry.new(:commands => set).rep
589
+ end
590
+
591
+ str_output.string.should =~ /hello bing/
592
+ $obj = nil
593
+ end
594
+
595
+ it 'should create a regex command and arg_string should be interpolated' do
596
+ set = Pry::CommandSet.new do
597
+ command /hello(\w+)/, "" do |c1, a1, a2, a3|
598
+ output.puts "hello #{c1} #{a1} #{a2} #{a3}"
599
+ end
600
+ end
601
+
602
+ str_output = StringIO.new
603
+ $a1 = "bing"
604
+ $a2 = "bong"
605
+ $a3 = "bang"
606
+ redirect_pry_io(InputTester.new('hellojohn #{$a1} #{$a2} #{$a3}'), str_output) do
607
+ Pry.new(:commands => set).rep
608
+ end
609
+
610
+ str_output.string.should =~ /hello john bing bong bang/
611
+
612
+ $a1 = nil
613
+ $a2 = nil
614
+ $a3 = nil
615
+ end
616
+
617
+
618
+ it 'if a regex capture is missing it should be nil' do
619
+ set = Pry::CommandSet.new do
620
+ command /hello(.)?/, "" do |c1, a1|
621
+ output.puts "hello #{c1.inspect} #{a1}"
622
+ end
623
+ end
624
+
443
625
  str_output = StringIO.new
444
- Pry.input = str_input
445
- obj = Object.new
446
- Pry.new(:output => str_output).repl(obj)
447
- Pry.input = Readline
448
- str_output.string.should =~ /@x/
626
+ redirect_pry_io(InputTester.new("hello baby"), str_output) do
627
+ Pry.new(:commands => set).rep
628
+ end
629
+
630
+ str_output.string.should =~ /hello nil baby/
631
+ end
632
+
633
+ it 'should create a command in a nested context and that command should be accessible from the parent' do
634
+ redirect_pry_io(StringIO.new, StringIO.new) do
635
+ str_input = StringIO.new("@x=nil\ncd 7\n_pry_.commands.instance_eval {\ncommand('bing') { |arg| run arg }\n}\ncd ..\nbing ls\nexit")
636
+ str_output = StringIO.new
637
+ Pry.input = str_input
638
+ obj = Object.new
639
+ Pry.new(:output => str_output).repl(obj)
640
+ Pry.input = Readline
641
+ str_output.string.should =~ /@x/
642
+ end
449
643
  end
450
644
 
451
645
  it 'should define a command that keeps its return value' do
452
- klass = Pry::CommandSet.new :test do
646
+ klass = Pry::CommandSet.new do
453
647
  command "hello", "", :keep_retval => true do
454
648
  :kept_hello
455
649
  end
@@ -461,7 +655,7 @@ describe Pry do
461
655
  end
462
656
 
463
657
  it 'should define a command that does NOT keep its return value' do
464
- klass = Pry::CommandSet.new :test do
658
+ klass = Pry::CommandSet.new do
465
659
  command "hello", "", :keep_retval => false do
466
660
  :kept_hello
467
661
  end
@@ -469,11 +663,11 @@ describe Pry do
469
663
  str_output = StringIO.new
470
664
  Pry.new(:input => StringIO.new("hello\n"), :output => str_output, :commands => klass).rep
471
665
  (str_output.string =~ /:kept_hello/).should == nil
472
- str_output.string !~ /=>/
666
+ str_output.string !~ /=>/
473
667
  end
474
668
 
475
669
  it 'should set the commands default, and the default should be overridable' do
476
- klass = Pry::CommandSet.new :test do
670
+ klass = Pry::CommandSet.new do
477
671
  command "hello" do
478
672
  output.puts "hello world"
479
673
  end
@@ -485,342 +679,383 @@ describe Pry do
485
679
  Pry.new(:input => InputTester.new("hello"), :output => str_output).rep
486
680
  str_output.string.should =~ /hello world/
487
681
 
488
- other_klass = Pry::CommandSet.new :test2 do
489
- command "goodbye", "" do
490
- output.puts "goodbye world"
682
+ other_klass = Pry::CommandSet.new do
683
+ command "goodbye", "" do
684
+ output.puts "goodbye world"
685
+ end
491
686
  end
492
- end
493
687
 
494
- str_output = StringIO.new
688
+ str_output = StringIO.new
495
689
 
496
- Pry.new(:input => InputTester.new("goodbye"), :output => str_output, :commands => other_klass).rep
497
- str_output.string.should =~ /goodbye world/
498
- end
690
+ Pry.new(:input => InputTester.new("goodbye"), :output => str_output, :commands => other_klass).rep
691
+ str_output.string.should =~ /goodbye world/
692
+ end
499
693
 
500
- it 'should inherit "help" command from Pry::CommandBase' do
501
- klass = Pry::CommandSet.new :test do
502
- command "h", "h command" do
694
+ it 'should inherit "help" command from Pry::CommandBase' do
695
+ klass = Pry::CommandSet.new do
696
+ command "h", "h command" do
697
+ end
503
698
  end
504
- end
505
699
 
506
- klass.commands.keys.size.should == 3
507
- klass.commands.keys.include?("help").should == true
508
- klass.commands.keys.include?("install").should == true
509
- klass.commands.keys.include?("h").should == true
510
- end
700
+ klass.commands.keys.size.should == 3
701
+ klass.commands.keys.include?("help").should == true
702
+ klass.commands.keys.include?("install").should == true
703
+ klass.commands.keys.include?("h").should == true
704
+ end
511
705
 
512
- it 'should inherit commands from Pry::Commands' do
513
- klass = Pry::CommandSet.new :test, Pry::Commands do
514
- command "v" do
706
+ it 'should inherit commands from Pry::Commands' do
707
+ klass = Pry::CommandSet.new Pry::Commands do
708
+ command "v" do
709
+ end
515
710
  end
711
+
712
+ klass.commands.include?("nesting").should == true
713
+ klass.commands.include?("jump-to").should == true
714
+ klass.commands.include?("cd").should == true
715
+ klass.commands.include?("v").should == true
516
716
  end
517
717
 
518
- klass.commands.include?("nesting").should == true
519
- klass.commands.include?("jump-to").should == true
520
- klass.commands.include?("cd").should == true
521
- klass.commands.include?("v").should == true
522
- end
718
+ it 'should alias a command with another command' do
719
+ klass = Pry::CommandSet.new do
720
+ alias_command "help2", "help"
721
+ end
722
+ klass.commands["help2"].block.should == klass.commands["help"].block
723
+ end
523
724
 
524
- it 'should alias a command with another command' do
525
- klass = Pry::CommandSet.new :test do
526
- alias_command "help2", "help"
725
+ it 'should change description of a command using desc' do
726
+ klass = Pry::CommandSet.new do; end
727
+ orig = klass.commands["help"].description
728
+ klass.instance_eval do
729
+ desc "help", "blah"
730
+ end
731
+ klass.commands["help"].description.should.not == orig
732
+ klass.commands["help"].description.should == "blah"
527
733
  end
528
734
 
735
+ it 'should run a command from within a command' do
736
+ klass = Pry::CommandSet.new do
737
+ command "v" do
738
+ output.puts "v command"
739
+ end
529
740
 
530
- klass.commands["help2"].block.should == klass.commands["help"].block
531
- end
741
+ command "run_v" do
742
+ run "v"
743
+ end
744
+ end
532
745
 
533
- it 'should change description of a command using desc' do
534
- klass = Pry::CommandSet.new :test do; end
535
- orig = klass.commands["help"].description
536
- klass.instance_eval do
537
- desc "help", "blah"
746
+ str_output = StringIO.new
747
+ Pry.new(:input => InputTester.new("run_v"), :output => str_output, :commands => klass).rep
748
+ str_output.string.should =~ /v command/
538
749
  end
539
- klass.commands["help"].description.should.not == orig
540
- klass.commands["help"].description.should == "blah"
541
- end
542
750
 
543
- it 'should run a command from within a command' do
544
- klass = Pry::CommandSet.new :test do
545
- command "v" do
546
- output.puts "v command"
751
+ it 'should run a regex command from within a command' do
752
+ klass = Pry::CommandSet.new do
753
+ command /v(.*)?/ do |arg|
754
+ output.puts "v #{arg}"
755
+ end
756
+
757
+ command "run_v" do
758
+ run "vbaby"
759
+ end
547
760
  end
548
761
 
549
- command "run_v" do
550
- run "v"
762
+ str_output = StringIO.new
763
+ redirect_pry_io(InputTester.new("run_v"), str_output) do
764
+ Pry.new(:commands => klass).rep
551
765
  end
766
+
767
+ str_output.string.should =~ /v baby/
552
768
  end
553
769
 
554
- str_output = StringIO.new
555
- Pry.new(:input => InputTester.new("run_v"), :output => str_output, :commands => klass).rep
556
- str_output.string.should =~ /v command/
557
- end
770
+ it 'should run a command from within a command with arguments' do
771
+ klass = Pry::CommandSet.new do
772
+ command /v(\w+)/ do |arg1, arg2|
773
+ output.puts "v #{arg1} #{arg2}"
774
+ end
558
775
 
559
- it 'should enable an inherited method to access opts and output and target, due to instance_exec' do
560
- klass = Pry::CommandSet.new :test do
561
- command "v" do
562
- output.puts "#{target.eval('self')}"
776
+ command "run_v_explicit_parameter" do
777
+ run "vbaby", "param"
778
+ end
779
+
780
+ command "run_v_embedded_parameter" do
781
+ run "vbaby param"
782
+ end
563
783
  end
564
- end
565
784
 
566
- child_klass = Pry::CommandSet.new :test2, klass do
785
+ ["run_v_explicit_parameter", "run_v_embedded_parameter"].each do |cmd|
786
+ str_output = StringIO.new
787
+ redirect_pry_io(InputTester.new(cmd), str_output) do
788
+ Pry.new(:commands => klass).rep
789
+ end
790
+ str_output.string.should =~ /v baby param/
791
+ end
567
792
  end
568
793
 
569
- str_output = StringIO.new
570
- Pry.new(:print => proc {}, :input => InputTester.new("v"),
571
- :output => str_output, :commands => child_klass).rep("john")
794
+ it 'should enable an inherited method to access opts and output and target, due to instance_exec' do
795
+ klass = Pry::CommandSet.new do
796
+ command "v" do
797
+ output.puts "#{target.eval('self')}"
798
+ end
799
+ end
572
800
 
573
- str_output.string.rstrip.should == "john"
574
- end
801
+ child_klass = Pry::CommandSet.new klass do
802
+ end
803
+
804
+ str_output = StringIO.new
805
+ Pry.new(:print => proc {}, :input => InputTester.new("v"),
806
+ :output => str_output, :commands => child_klass).rep("john")
575
807
 
576
- it 'should import commands from another command object' do
577
- klass = Pry::CommandSet.new :test do
578
- import_from Pry::Commands, "ls", "jump-to"
808
+ str_output.string.rstrip.should == "john"
579
809
  end
580
810
 
811
+ it 'should import commands from another command object' do
812
+ klass = Pry::CommandSet.new do
813
+ import_from Pry::Commands, "ls", "jump-to"
814
+ end
815
+
581
816
  klass.commands.include?("ls").should == true
582
817
  klass.commands.include?("jump-to").should == true
583
818
  end
584
819
 
585
- it 'should delete some inherited commands when using delete method' do
586
- klass = Pry::CommandSet.new :test, Pry::Commands do
587
- command "v" do
820
+ it 'should delete some inherited commands when using delete method' do
821
+ klass = Pry::CommandSet.new Pry::Commands do
822
+ command "v" do
823
+ end
824
+
825
+ delete "show-doc", "show-method"
826
+ delete "ls"
588
827
  end
589
828
 
590
- delete "show-doc", "show-method"
591
- delete "ls"
829
+ klass.commands.include?("nesting").should == true
830
+ klass.commands.include?("jump-to").should == true
831
+ klass.commands.include?("cd").should == true
832
+ klass.commands.include?("v").should == true
833
+ klass.commands.include?("show-doc").should == false
834
+ klass.commands.include?("show-method").should == false
835
+ klass.commands.include?("ls").should == false
592
836
  end
593
837
 
594
- klass.commands.include?("nesting").should == true
595
- klass.commands.include?("jump-to").should == true
596
- klass.commands.include?("cd").should == true
597
- klass.commands.include?("v").should == true
598
- klass.commands.include?("show-doc").should == false
599
- klass.commands.include?("show-method").should == false
600
- klass.commands.include?("ls").should == false
601
- end
602
-
603
- it 'should override some inherited commands' do
604
- klass = Pry::CommandSet.new :test, Pry::Commands do
605
- command "jump-to" do
606
- output.puts "jump-to the music"
607
- end
838
+ it 'should override some inherited commands' do
839
+ klass = Pry::CommandSet.new Pry::Commands do
840
+ command "jump-to" do
841
+ output.puts "jump-to the music"
842
+ end
608
843
 
609
- command "help" do
610
- output.puts "help to the music"
844
+ command "help" do
845
+ output.puts "help to the music"
846
+ end
611
847
  end
612
- end
613
848
 
614
- # suppress evaluation output
615
- Pry.print = proc {}
849
+ # suppress evaluation output
850
+ Pry.print = proc {}
616
851
 
617
- str_output = StringIO.new
618
- Pry.new(:input => InputTester.new("jump-to"), :output => str_output, :commands => klass).rep
619
- str_output.string.rstrip.should == "jump-to the music"
852
+ str_output = StringIO.new
853
+ Pry.new(:input => InputTester.new("jump-to"), :output => str_output, :commands => klass).rep
854
+ str_output.string.rstrip.should == "jump-to the music"
620
855
 
621
- str_output = StringIO.new
622
- Pry.new(:input => InputTester.new("help"), :output => str_output, :commands => klass).rep
623
- str_output.string.rstrip.should == "help to the music"
856
+ str_output = StringIO.new
857
+ Pry.new(:input => InputTester.new("help"), :output => str_output, :commands => klass).rep
858
+ str_output.string.rstrip.should == "help to the music"
624
859
 
625
860
 
626
- Pry.reset_defaults
627
- Pry.color = false
861
+ Pry.reset_defaults
862
+ Pry.color = false
863
+ end
628
864
  end
629
- end
630
-
631
- it "should set the print default, and the default should be overridable" do
632
- new_print = proc { |out, value| out.puts value }
633
- Pry.print = new_print
634
865
 
635
- Pry.new.print.should == Pry.print
636
- str_output = StringIO.new
637
- Pry.new(:input => InputTester.new("\"test\""), :output => str_output).rep
638
- str_output.string.should == "test\n"
866
+ it "should set the print default, and the default should be overridable" do
867
+ new_print = proc { |out, value| out.puts value }
868
+ Pry.print = new_print
639
869
 
640
- str_output = StringIO.new
641
- Pry.new(:input => InputTester.new("\"test\""), :output => str_output,
642
- :print => proc { |out, value| out.puts value.reverse }).rep
643
- str_output.string.should == "tset\n"
870
+ Pry.new.print.should == Pry.print
871
+ str_output = StringIO.new
872
+ Pry.new(:input => InputTester.new("\"test\""), :output => str_output).rep
873
+ str_output.string.should == "test\n"
644
874
 
645
- Pry.new.print.should == Pry.print
646
- str_output = StringIO.new
647
- Pry.new(:input => InputTester.new("\"test\""), :output => str_output).rep
648
- str_output.string.should == "test\n"
649
- end
875
+ str_output = StringIO.new
876
+ Pry.new(:input => InputTester.new("\"test\""), :output => str_output,
877
+ :print => proc { |out, value| out.puts value.reverse }).rep
878
+ str_output.string.should == "tset\n"
650
879
 
651
- describe "pry return values" do
652
- it 'should return the target object' do
653
- Pry.start(self, :input => StringIO.new("exit"), :output => Pry::NullOutput).should == self
880
+ Pry.new.print.should == Pry.print
881
+ str_output = StringIO.new
882
+ Pry.new(:input => InputTester.new("\"test\""), :output => str_output).rep
883
+ str_output.string.should == "test\n"
654
884
  end
655
885
 
656
- it 'should return the parameter given to exit' do
657
- Pry.start(self, :input => StringIO.new("exit 10"), :output => Pry::NullOutput).should == 10
658
- end
886
+ describe "pry return values" do
887
+ it 'should return the target object' do
888
+ Pry.start(self, :input => StringIO.new("exit"), :output => Pry::NullOutput).should == self
889
+ end
659
890
 
660
- it 'should return the parameter (multi word string) given to exit' do
661
- Pry.start(self, :input => StringIO.new("exit \"john mair\""), :output => Pry::NullOutput).should == "john mair"
662
- end
891
+ it 'should return the parameter given to exit' do
892
+ Pry.start(self, :input => StringIO.new("exit 10"), :output => Pry::NullOutput).should == 10
893
+ end
663
894
 
664
- it 'should return the parameter (function call) given to exit' do
665
- Pry.start(self, :input => StringIO.new("exit 'abc'.reverse"), :output => Pry::NullOutput).should == 'cba'
666
- end
895
+ it 'should return the parameter (multi word string) given to exit' do
896
+ Pry.start(self, :input => StringIO.new("exit \"john mair\""), :output => Pry::NullOutput).should == "john mair"
897
+ end
667
898
 
668
- it 'should return the parameter (self) given to exit' do
669
- Pry.start("carl", :input => StringIO.new("exit self"), :output => Pry::NullOutput).should == "carl"
899
+ it 'should return the parameter (function call) given to exit' do
900
+ Pry.start(self, :input => StringIO.new("exit 'abc'.reverse"), :output => Pry::NullOutput).should == 'cba'
901
+ end
902
+
903
+ it 'should return the parameter (self) given to exit' do
904
+ Pry.start("carl", :input => StringIO.new("exit self"), :output => Pry::NullOutput).should == "carl"
905
+ end
670
906
  end
671
- end
672
907
 
673
- describe "prompts" do
674
- it 'should set the prompt default, and the default should be overridable (single prompt)' do
675
- new_prompt = proc { "test prompt> " }
676
- Pry.prompt = new_prompt
908
+ describe "prompts" do
909
+ it 'should set the prompt default, and the default should be overridable (single prompt)' do
910
+ new_prompt = proc { "test prompt> " }
911
+ Pry.prompt = new_prompt
677
912
 
678
- Pry.new.prompt.should == Pry.prompt
679
- Pry.new.select_prompt(true, 0).should == "test prompt> "
680
- Pry.new.select_prompt(false, 0).should == "test prompt> "
913
+ Pry.new.prompt.should == Pry.prompt
914
+ Pry.new.select_prompt(true, 0).should == "test prompt> "
915
+ Pry.new.select_prompt(false, 0).should == "test prompt> "
681
916
 
682
- new_prompt = proc { "A" }
683
- pry_tester = Pry.new(:prompt => new_prompt)
684
- pry_tester.prompt.should == new_prompt
685
- pry_tester.select_prompt(true, 0).should == "A"
686
- pry_tester.select_prompt(false, 0).should == "A"
917
+ new_prompt = proc { "A" }
918
+ pry_tester = Pry.new(:prompt => new_prompt)
919
+ pry_tester.prompt.should == new_prompt
920
+ pry_tester.select_prompt(true, 0).should == "A"
921
+ pry_tester.select_prompt(false, 0).should == "A"
687
922
 
688
- Pry.new.prompt.should == Pry.prompt
689
- Pry.new.select_prompt(true, 0).should == "test prompt> "
690
- Pry.new.select_prompt(false, 0).should == "test prompt> "
691
- end
923
+ Pry.new.prompt.should == Pry.prompt
924
+ Pry.new.select_prompt(true, 0).should == "test prompt> "
925
+ Pry.new.select_prompt(false, 0).should == "test prompt> "
926
+ end
692
927
 
693
- it 'should set the prompt default, and the default should be overridable (multi prompt)' do
694
- new_prompt = [proc { "test prompt> " }, proc { "test prompt* " }]
695
- Pry.prompt = new_prompt
928
+ it 'should set the prompt default, and the default should be overridable (multi prompt)' do
929
+ new_prompt = [proc { "test prompt> " }, proc { "test prompt* " }]
930
+ Pry.prompt = new_prompt
696
931
 
697
- Pry.new.prompt.should == Pry.prompt
698
- Pry.new.select_prompt(true, 0).should == "test prompt> "
699
- Pry.new.select_prompt(false, 0).should == "test prompt* "
932
+ Pry.new.prompt.should == Pry.prompt
933
+ Pry.new.select_prompt(true, 0).should == "test prompt> "
934
+ Pry.new.select_prompt(false, 0).should == "test prompt* "
700
935
 
701
- new_prompt = [proc { "A" }, proc { "B" }]
702
- pry_tester = Pry.new(:prompt => new_prompt)
703
- pry_tester.prompt.should == new_prompt
704
- pry_tester.select_prompt(true, 0).should == "A"
705
- pry_tester.select_prompt(false, 0).should == "B"
936
+ new_prompt = [proc { "A" }, proc { "B" }]
937
+ pry_tester = Pry.new(:prompt => new_prompt)
938
+ pry_tester.prompt.should == new_prompt
939
+ pry_tester.select_prompt(true, 0).should == "A"
940
+ pry_tester.select_prompt(false, 0).should == "B"
706
941
 
707
- Pry.new.prompt.should == Pry.prompt
708
- Pry.new.select_prompt(true, 0).should == "test prompt> "
709
- Pry.new.select_prompt(false, 0).should == "test prompt* "
710
- end
942
+ Pry.new.prompt.should == Pry.prompt
943
+ Pry.new.select_prompt(true, 0).should == "test prompt> "
944
+ Pry.new.select_prompt(false, 0).should == "test prompt* "
945
+ end
711
946
 
712
- describe 'storing and restoring the prompt' do
713
- before do
714
- make = lambda do |name,i|
715
- prompt = [ proc { "#{i}>" } , proc { "#{i+1}>" } ]
716
- (class << prompt; self; end).send(:define_method, :inspect) { "<Prompt-#{name}>" }
717
- prompt
718
- end
719
- @a , @b , @c = make[:a,0] , make[:b,1] , make[:c,2]
720
- @pry = Pry.new :prompt => @a
721
- end
722
- it 'should have a prompt stack' do
723
- @pry.push_prompt @b
724
- @pry.push_prompt @c
725
- @pry.prompt.should == @c
726
- @pry.pop_prompt
727
- @pry.prompt.should == @b
728
- @pry.pop_prompt
729
- @pry.prompt.should == @a
730
- end
731
-
732
- it 'should restore overridden prompts when returning from file-mode' do
733
- pry = Pry.new :input => InputTester.new('shell-mode', 'shell-mode'),
734
- :prompt => [ proc { 'P>' } ] * 2
735
- pry.select_prompt(true, 0).should == "P>"
736
- pry.re
737
- pry.select_prompt(true, 0).should =~ /\Apry .* \$ \z/
738
- pry.re
739
- pry.select_prompt(true, 0).should == "P>"
740
- end
741
-
742
- it '#pop_prompt should return the popped prompt' do
743
- @pry.push_prompt @b
744
- @pry.push_prompt @c
745
- @pry.pop_prompt.should == @c
746
- @pry.pop_prompt.should == @b
747
- end
748
-
749
- it 'should not pop the last prompt' do
750
- @pry.push_prompt @b
751
- @pry.pop_prompt.should == @b
752
- @pry.pop_prompt.should == @a
753
- @pry.pop_prompt.should == @a
754
- @pry.prompt.should == @a
755
- end
756
-
757
- describe '#prompt= should replace the current prompt with the new prompt' do
758
- it 'when only one prompt on the stack' do
759
- @pry.prompt = @b
947
+ describe 'storing and restoring the prompt' do
948
+ before do
949
+ make = lambda do |name,i|
950
+ prompt = [ proc { "#{i}>" } , proc { "#{i+1}>" } ]
951
+ (class << prompt; self; end).send(:define_method, :inspect) { "<Prompt-#{name}>" }
952
+ prompt
953
+ end
954
+ @a , @b , @c = make[:a,0] , make[:b,1] , make[:c,2]
955
+ @pry = Pry.new :prompt => @a
956
+ end
957
+ it 'should have a prompt stack' do
958
+ @pry.push_prompt @b
959
+ @pry.push_prompt @c
960
+ @pry.prompt.should == @c
961
+ @pry.pop_prompt
760
962
  @pry.prompt.should == @b
761
- @pry.pop_prompt.should == @b
762
- @pry.pop_prompt.should == @b
963
+ @pry.pop_prompt
964
+ @pry.prompt.should == @a
763
965
  end
764
- it 'when several prompts on the stack' do
966
+
967
+ it 'should restore overridden prompts when returning from file-mode' do
968
+ pry = Pry.new :input => InputTester.new('shell-mode', 'shell-mode'),
969
+ :prompt => [ proc { 'P>' } ] * 2
970
+ pry.select_prompt(true, 0).should == "P>"
971
+ pry.re
972
+ pry.select_prompt(true, 0).should =~ /\Apry .* \$ \z/
973
+ pry.re
974
+ pry.select_prompt(true, 0).should == "P>"
975
+ end
976
+
977
+ it '#pop_prompt should return the popped prompt' do
765
978
  @pry.push_prompt @b
766
- @pry.prompt = @c
979
+ @pry.push_prompt @c
767
980
  @pry.pop_prompt.should == @c
981
+ @pry.pop_prompt.should == @b
982
+ end
983
+
984
+ it 'should not pop the last prompt' do
985
+ @pry.push_prompt @b
986
+ @pry.pop_prompt.should == @b
987
+ @pry.pop_prompt.should == @a
768
988
  @pry.pop_prompt.should == @a
989
+ @pry.prompt.should == @a
990
+ end
991
+
992
+ describe '#prompt= should replace the current prompt with the new prompt' do
993
+ it 'when only one prompt on the stack' do
994
+ @pry.prompt = @b
995
+ @pry.prompt.should == @b
996
+ @pry.pop_prompt.should == @b
997
+ @pry.pop_prompt.should == @b
998
+ end
999
+ it 'when several prompts on the stack' do
1000
+ @pry.push_prompt @b
1001
+ @pry.prompt = @c
1002
+ @pry.pop_prompt.should == @c
1003
+ @pry.pop_prompt.should == @a
1004
+ end
769
1005
  end
770
1006
  end
771
1007
  end
772
- end
773
1008
 
774
- it 'should set the hooks default, and the default should be overridable' do
775
- Pry.input = InputTester.new("exit")
776
- Pry.hooks = {
777
- :before_session => proc { |out,_| out.puts "HELLO" },
778
- :after_session => proc { |out,_| out.puts "BYE" }
779
- }
1009
+ it 'should set the hooks default, and the default should be overridable' do
1010
+ Pry.input = InputTester.new("exit")
1011
+ Pry.hooks = {
1012
+ :before_session => proc { |out,_| out.puts "HELLO" },
1013
+ :after_session => proc { |out,_| out.puts "BYE" }
1014
+ }
780
1015
 
781
- str_output = StringIO.new
782
- Pry.new(:output => str_output).repl
783
- str_output.string.should =~ /HELLO/
784
- str_output.string.should =~ /BYE/
1016
+ str_output = StringIO.new
1017
+ Pry.new(:output => str_output).repl
1018
+ str_output.string.should =~ /HELLO/
1019
+ str_output.string.should =~ /BYE/
785
1020
 
786
- Pry.input.rewind
1021
+ Pry.input.rewind
787
1022
 
788
- str_output = StringIO.new
789
- Pry.new(:output => str_output,
790
- :hooks => {
791
- :before_session => proc { |out,_| out.puts "MORNING" },
792
- :after_session => proc { |out,_| out.puts "EVENING" }
793
- }
794
- ).repl
795
-
796
- str_output.string.should =~ /MORNING/
797
- str_output.string.should =~ /EVENING/
798
-
799
- # try below with just defining one hook
800
- Pry.input.rewind
801
- str_output = StringIO.new
802
- Pry.new(:output => str_output,
803
- :hooks => {
804
- :before_session => proc { |out,_| out.puts "OPEN" }
805
- }
806
- ).repl
1023
+ str_output = StringIO.new
1024
+ Pry.new(:output => str_output,
1025
+ :hooks => {
1026
+ :before_session => proc { |out,_| out.puts "MORNING" },
1027
+ :after_session => proc { |out,_| out.puts "EVENING" }
1028
+ }
1029
+ ).repl
1030
+
1031
+ str_output.string.should =~ /MORNING/
1032
+ str_output.string.should =~ /EVENING/
1033
+
1034
+ # try below with just defining one hook
1035
+ Pry.input.rewind
1036
+ str_output = StringIO.new
1037
+ Pry.new(:output => str_output,
1038
+ :hooks => {
1039
+ :before_session => proc { |out,_| out.puts "OPEN" }
1040
+ }
1041
+ ).repl
807
1042
 
808
- str_output.string.should =~ /OPEN/
1043
+ str_output.string.should =~ /OPEN/
809
1044
 
810
- Pry.input.rewind
811
- str_output = StringIO.new
812
- Pry.new(:output => str_output,
813
- :hooks => {
814
- :after_session => proc { |out,_| out.puts "CLOSE" }
815
- }
816
- ).repl
1045
+ Pry.input.rewind
1046
+ str_output = StringIO.new
1047
+ Pry.new(:output => str_output,
1048
+ :hooks => {
1049
+ :after_session => proc { |out,_| out.puts "CLOSE" }
1050
+ }
1051
+ ).repl
817
1052
 
818
- str_output.string.should =~ /CLOSE/
1053
+ str_output.string.should =~ /CLOSE/
819
1054
 
820
- Pry.reset_defaults
821
- Pry.color = false
1055
+ Pry.reset_defaults
1056
+ Pry.color = false
1057
+ end
822
1058
  end
823
1059
  end
824
1060
  end
825
1061
  end
826
- end