/
test.rb
681 lines (525 loc) · 22.7 KB
/
test.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
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
677
678
679
680
681
direc = File.dirname(__FILE__)
require 'rubygems'
require 'bacon'
require "#{direc}/../lib/pry"
require "#{direc}/test_helper"
puts "Ruby Version #{RUBY_VERSION}"
puts "Testing Pry #{Pry::VERSION}"
puts "With method_source version #{MethodSource::VERSION}"
puts "--"
describe Pry do
describe "open a Pry session on an object" do
describe "rep" do
before do
class Hello
end
end
after do
Object.send(:remove_const, :Hello)
end
it 'should set an ivar on an object' do
input_string = "@x = 10"
input = InputTester.new(input_string)
o = Object.new
pry_tester = Pry.new(:input => input, :output => Pry::NullOutput)
pry_tester.rep(o)
o.instance_variable_get(:@x).should == 10
end
it 'should make self evaluate to the receiver of the rep session' do
o = Object.new
str_output = StringIO.new
pry_tester = Pry.new(:input => InputTester.new("self"), :output => str_output)
pry_tester.rep(o)
str_output.string.should =~ /#{o.to_s}/
end
it 'should work with multi-line input' do
o = Object.new
str_output = StringIO.new
pry_tester = Pry.new(:input => InputTester.new("x = ", "1 + 4"), :output => str_output)
pry_tester.rep(o)
str_output.string.should =~ /5/
end
it 'should define a nested class under Hello and not on top-level or Pry' do
pry_tester = Pry.new(:input => InputTester.new("class Nested", "end"), :output => Pry::NullOutput)
pry_tester.rep(Hello)
Hello.const_defined?(:Nested).should == true
end
end
describe "repl" do
describe "basic functionality" do
it 'should set an ivar on an object and exit the repl' do
input_strings = ["@x = 10", "exit"]
input = InputTester.new(*input_strings)
o = Object.new
pry_tester = Pry.start(o, :input => input, :output => Pry::NullOutput)
o.instance_variable_get(:@x).should == 10
end
it 'should execute start session and end session hooks' do
input = InputTester.new("exit")
str_output = StringIO.new
o = Object.new
pry_tester = Pry.start(o, :input => input, :output => str_output)
str_output.string.should =~ /Beginning.*#{o}/
str_output.string.should =~ /Ending.*#{o}/
end
end
describe "nesting" do
after do
Pry.reset_defaults
end
it 'should nest properly' do
Pry.input = InputTester.new("pry", "pry", "pry", "\"nest:\#\{Pry.nesting.level\}\"", "exit_all")
str_output = StringIO.new
Pry.output = str_output
o = Object.new
pry_tester = o.pry
str_output.string.should =~ /nest:3/
end
end
describe "defining methods" do
it 'should define a method on the singleton class of an object when performing "def meth;end" inside the object' do
[Object.new, {}, []].each do |val|
str_input = StringIO.new("def hello;end")
Pry.new(:input => str_input, :output => StringIO.new).rep(val)
val.methods(false).map(&:to_sym).include?(:hello).should == true
end
end
it 'should define an instance method on the module when performing "def meth;end" inside the module' do
str_input = StringIO.new("def hello;end")
hello = Module.new
Pry.new(:input => str_input, :output => StringIO.new).rep(hello)
hello.instance_methods(false).map(&:to_sym).include?(:hello).should == true
end
it 'should define an instance method on the class when performing "def meth;end" inside the class' do
str_input = StringIO.new("def hello;end")
hello = Class.new
Pry.new(:input => str_input, :output => StringIO.new).rep(hello)
hello.instance_methods(false).map(&:to_sym).include?(:hello).should == true
end
it 'should define a method on the class of an object when performing "def meth;end" inside an immediate value or Numeric' do
# should include float in here, but test fails for some reason
# on 1.8.7, no idea why!
[:test, 0, true, false, nil].each do |val|
str_input = StringIO.new("def hello;end")
Pry.new(:input => str_input, :output => StringIO.new).rep(val)
val.class.instance_methods(false).map(&:to_sym).include?(:hello).should == true
end
end
end
describe "commands" do
it 'should run a command with no parameter' do
pry_tester = Pry.new
pry_tester.commands = CommandTester
pry_tester.input = InputTester.new("command1", "exit_all")
pry_tester.commands = CommandTester
str_output = StringIO.new
pry_tester.output = str_output
pry_tester.rep
str_output.string.should =~ /command1/
end
it 'should run a command with one parameter' do
pry_tester = Pry.new
pry_tester.commands = CommandTester
pry_tester.input = InputTester.new("command2 horsey", "exit_all")
pry_tester.commands = CommandTester
str_output = StringIO.new
pry_tester.output = str_output
pry_tester.rep
str_output.string.should =~ /horsey/
end
end
describe "Object#pry" do
after do
Pry.reset_defaults
end
it "should start a pry session on the receiver (first form)" do
Pry.input = InputTester.new("self", "exit")
str_output = StringIO.new
Pry.output = str_output
20.pry
str_output.string.should =~ /20/
end
it "should start a pry session on the receiver (second form)" do
Pry.input = InputTester.new("self", "exit")
str_output = StringIO.new
Pry.output = str_output
pry 20
str_output.string.should =~ /20/
end
it "should error if more than one argument is passed to Object#pry" do
lambda { pry(20, :input => Readline) }.should.raise ArgumentError
end
end
describe "Pry.binding_for" do
it 'should return TOPLEVEL_BINDING if parameter self is main' do
_main_ = lambda { TOPLEVEL_BINDING.eval('self') }
Pry.binding_for(_main_.call).is_a?(Binding).should == true
Pry.binding_for(_main_.call).should == TOPLEVEL_BINDING
Pry.binding_for(_main_.call).should == Pry.binding_for(_main_.call)
end
end
describe "test Pry defaults" do
after do
Pry.reset_defaults
end
describe "input" do
after do
Pry.reset_defaults
end
it 'should set the input default, and the default should be overridable' do
Pry.input = InputTester.new("5")
str_output = StringIO.new
Pry.output = str_output
Pry.new.rep
str_output.string.should =~ /5/
Pry.new(:input => InputTester.new("6")).rep
str_output.string.should =~ /6/
end
it 'should pass in the prompt if readline arity is 1' do
Pry.prompt = proc { "A" }
arity_one_input = Class.new do
attr_accessor :prompt
def readline(prompt)
@prompt = prompt
"exit"
end
end.new
Pry.start(self, :input => arity_one_input, :output => Pry::NullOutput)
arity_one_input.prompt.should == Pry.prompt.call
end
it 'should not pass in the prompt if the arity is 0' do
Pry.prompt = proc { "A" }
arity_zero_input = Class.new do
def readline
"exit"
end
end.new
lambda { Pry.start(self, :input => arity_zero_input, :output => Pry::NullOutput) }.should.not.raise Exception
end
it 'should not pass in the prompt if the arity is -1' do
Pry.prompt = proc { "A" }
arity_multi_input = Class.new do
attr_accessor :prompt
def readline(*args)
@prompt = args.first
"exit"
end
end.new
Pry.start(self, :input => arity_multi_input, :output => Pry::NullOutput)
arity_multi_input.prompt.should == nil
end
end
it 'should set the output default, and the default should be overridable' do
Pry.input = InputTester.new("5", "6", "7")
str_output = StringIO.new
Pry.output = str_output
Pry.new.rep
str_output.string.should =~ /5/
Pry.new.rep
str_output.string.should =~ /5\n.*6/
str_output2 = StringIO.new
Pry.new(:output => str_output2).rep
str_output2.string.should.not =~ /5\n.*6/
str_output2.string.should =~ /7/
end
describe "Pry.run_command" do
before do
class RCTest
def a() end
B = 20
@x = 10
end
end
after do
Object.remove_const(:RCTest)
end
it "should execute command in the appropriate object context" do
result = Pry.run_command "ls", :context => RCTest
result.map(&:to_sym).should == [:@x]
end
it "should execute command with parameters in the appropriate object context" do
result = Pry.run_command "ls -M", :context => RCTest
result.map(&:to_sym).should == [:a]
end
it "should execute command and show output with :show_output => true flag" do
str = StringIO.new
Pry.output = str
result = Pry.run_command "ls -av", :context => RCTest, :show_output => true
str.string.should =~ /global variables/
Pry.output = $stdout
end
it "should execute command with multiple parameters" do
result = Pry.run_command "ls -c -M RCTest"
result.map(&:to_sym).should == [:a, :B]
end
end
describe "commands" do
it 'should define a command that keeps its return value' do
class Command68 < Pry::CommandBase
command "hello", "", :keep_retval => true do
:kept_hello
end
end
str_output = StringIO.new
Pry.new(:input => StringIO.new("hello"), :output => str_output, :commands => Command68).rep
str_output.string.should =~ /:kept_hello/
Object.remove_const(:Command68)
end
it 'should define a command that does NOT keep its return value' do
class Command68 < Pry::CommandBase
command "hello", "", :keep_retval => false do
:kept_hello
end
end
str_output = StringIO.new
Pry.new(:input => StringIO.new("hello"), :output => str_output, :commands => Command68).rep
(str_output.string =~ /:kept_hello/).should == nil
Object.remove_const(:Command68)
end
it 'should set the commands default, and the default should be overridable' do
class Command0 < Pry::CommandBase
command "hello" do
output.puts "hello world"
end
end
Pry.commands = Command0
str_output = StringIO.new
Pry.new(:input => InputTester.new("hello"), :output => str_output).rep
str_output.string.should =~ /hello world/
class Command1 < Pry::CommandBase
command "goodbye", "" do
output.puts "goodbye world"
end
end
str_output = StringIO.new
Pry.new(:input => InputTester.new("goodbye"), :output => str_output, :commands => Command1).rep
str_output.string.should =~ /goodbye world/
Object.remove_const(:Command0)
Object.remove_const(:Command1)
end
it 'should inherit "help" command from Pry::CommandBase' do
class Command2 < Pry::CommandBase
command "h", "h command" do
end
end
Command2.commands.keys.size.should == 2
Command2.commands.keys.include?("help").should == true
Command2.commands.keys.include?("h").should == true
Object.remove_const(:Command2)
end
it 'should inherit commands from Pry::Commands' do
class Command3 < Pry::Commands
command "v" do
end
end
Command3.commands.include?("nesting").should == true
Command3.commands.include?("jump-to").should == true
Command3.commands.include?("cd").should == true
Command3.commands.include?("v").should == true
Object.remove_const(:Command3)
end
it 'should alias a command with another command' do
class Command6 < Pry::CommandBase
alias_command "help2", "help"
end
Command6.commands["help2"].should == Command6.commands["help"]
# str_output = StringIO.new
# Pry.new(:input => InputTester.new("run_v"), :output => str_output, :commands => Command3).rep
# str_output.string.should =~ /v command/
Object.remove_const(:Command6)
end
it 'should change description of a command using desc' do
class Command7 < Pry::Commands
end
orig = Command7.commands["help"][:description]
class Command7
desc "help", "blah"
end
Command7.commands["help"][:description].should.not == orig
Command7.commands["help"][:description].should == "blah"
Object.remove_const(:Command7)
end
it 'should run a command from within a command' do
class Command01 < Pry::Commands
command "v" do
output.puts "v command"
end
command "run_v" do
run "v"
end
end
str_output = StringIO.new
Pry.new(:input => InputTester.new("run_v"), :output => str_output, :commands => Command01).rep
str_output.string.should =~ /v command/
Object.remove_const(:Command01)
end
it 'should enable an inherited method to access opts and output and target, due to instance_exec' do
class Command3 < Pry::Commands
command "v" do
output.puts "#{target.eval('self')}"
end
end
class Command4 < Command3
end
str_output = StringIO.new
Pry.new(:print => proc {}, :input => InputTester.new("v"),
:output => str_output, :commands => Command4).rep("john")
str_output.string.chomp.should == "john"
Object.remove_const(:Command3)
Object.remove_const(:Command4)
end
it 'should import commands from another command object' do
class Command3 < Pry::CommandBase
import_from Pry::Commands, "status", "jump-to"
end
str_output = StringIO.new
Pry.new(:print => proc {}, :input => InputTester.new("status"),
:output => str_output, :commands => Command3).rep("john")
str_output.string.should =~ /Status:/
Object.remove_const(:Command3)
end
it 'should delete some inherited commands when using delete method' do
class Command3 < Pry::Commands
command "v" do
end
delete "show_doc", "show_method"
delete "ls"
end
Command3.commands.include?("nesting").should == true
Command3.commands.include?("jump-to").should == true
Command3.commands.include?("cd").should == true
Command3.commands.include?("v").should == true
Command3.commands.include?("show_doc").should == false
Command3.commands.include?("show_method").should == false
Command3.commands.include?("ls").should == false
Object.remove_const(:Command3)
end
it 'should override some inherited commands' do
class Command3 < Pry::Commands
command "jump-to" do
output.puts "jump-to the music"
end
command "help" do
output.puts "help to the music"
end
end
# suppress evaluation output
Pry.print = proc {}
str_output = StringIO.new
Pry.new(:input => InputTester.new("jump-to"), :output => str_output, :commands => Command3).rep
str_output.string.chomp.should == "jump-to the music"
str_output = StringIO.new
Pry.new(:input => InputTester.new("help"), :output => str_output, :commands => Command3).rep
str_output.string.chomp.should == "help to the music"
Object.remove_const(:Command3)
Pry.reset_defaults
end
end
it "should set the print default, and the default should be overridable" do
new_print = proc { |out, value| out.puts value }
Pry.print = new_print
Pry.new.print.should == Pry.print
str_output = StringIO.new
Pry.new(:input => InputTester.new("\"test\""), :output => str_output).rep
str_output.string.should == "test\n"
str_output = StringIO.new
Pry.new(:input => InputTester.new("\"test\""), :output => str_output,
:print => proc { |out, value| out.puts value.reverse }).rep
str_output.string.should == "tset\n"
Pry.new.print.should == Pry.print
str_output = StringIO.new
Pry.new(:input => InputTester.new("\"test\""), :output => str_output).rep
str_output.string.should == "test\n"
end
describe "pry return values" do
it 'should return the target object' do
Pry.start(self, :input => StringIO.new("exit"), :output => Pry::NullOutput).should == self
end
it 'should return the parameter given to exit' do
Pry.start(self, :input => StringIO.new("exit 10"), :output => Pry::NullOutput).should == 10
end
it 'should return the parameter (multi word string) given to exit' do
Pry.start(self, :input => StringIO.new("exit \"john mair\""), :output => Pry::NullOutput).should == "john mair"
end
it 'should return the parameter (function call) given to exit' do
Pry.start(self, :input => StringIO.new("exit 'abc'.reverse"), :output => Pry::NullOutput).should == 'cba'
end
it 'should return the parameter (self) given to exit' do
Pry.start("carl", :input => StringIO.new("exit self"), :output => Pry::NullOutput).should == "carl"
end
end
describe "prompts" do
it 'should set the prompt default, and the default should be overridable (single prompt)' do
new_prompt = proc { "test prompt> " }
Pry.prompt = new_prompt
Pry.new.prompt.should == Pry.prompt
Pry.new.select_prompt(true, 0).should == "test prompt> "
Pry.new.select_prompt(false, 0).should == "test prompt> "
new_prompt = proc { "A" }
pry_tester = Pry.new(:prompt => new_prompt)
pry_tester.prompt.should == new_prompt
pry_tester.select_prompt(true, 0).should == "A"
pry_tester.select_prompt(false, 0).should == "A"
Pry.new.prompt.should == Pry.prompt
Pry.new.select_prompt(true, 0).should == "test prompt> "
Pry.new.select_prompt(false, 0).should == "test prompt> "
end
it 'should set the prompt default, and the default should be overridable (multi prompt)' do
new_prompt = [proc { "test prompt> " }, proc { "test prompt* " }]
Pry.prompt = new_prompt
Pry.new.prompt.should == Pry.prompt
Pry.new.select_prompt(true, 0).should == "test prompt> "
Pry.new.select_prompt(false, 0).should == "test prompt* "
new_prompt = [proc { "A" }, proc { "B" }]
pry_tester = Pry.new(:prompt => new_prompt)
pry_tester.prompt.should == new_prompt
pry_tester.select_prompt(true, 0).should == "A"
pry_tester.select_prompt(false, 0).should == "B"
Pry.new.prompt.should == Pry.prompt
Pry.new.select_prompt(true, 0).should == "test prompt> "
Pry.new.select_prompt(false, 0).should == "test prompt* "
end
end
it 'should set the hooks default, and the default should be overridable' do
Pry.input = InputTester.new("exit")
Pry.hooks = {
:before_session => proc { |out,_| out.puts "HELLO" },
:after_session => proc { |out,_| out.puts "BYE" }
}
str_output = StringIO.new
Pry.new(:output => str_output).repl
str_output.string.should =~ /HELLO/
str_output.string.should =~ /BYE/
Pry.input.rewind
str_output = StringIO.new
Pry.new(:output => str_output,
:hooks => {
:before_session => proc { |out,_| out.puts "MORNING" },
:after_session => proc { |out,_| out.puts "EVENING" }
}
).repl
str_output.string.should =~ /MORNING/
str_output.string.should =~ /EVENING/
# try below with just defining one hook
Pry.input.rewind
str_output = StringIO.new
Pry.new(:output => str_output,
:hooks => {
:before_session => proc { |out,_| out.puts "OPEN" }
}
).repl
str_output.string.should =~ /OPEN/
Pry.input.rewind
str_output = StringIO.new
Pry.new(:output => str_output,
:hooks => {
:after_session => proc { |out,_| out.puts "CLOSE" }
}
).repl
str_output.string.should =~ /CLOSE/
Pry.reset_defaults
end
end
end
end
end