pry 0.9.7.4-java → 0.9.8-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (65) hide show
  1. data/.gitignore +2 -3
  2. data/CHANGELOG +43 -0
  3. data/README.markdown +3 -1
  4. data/Rakefile +51 -32
  5. data/bin/pry +2 -80
  6. data/lib/pry.rb +33 -26
  7. data/lib/pry/cli.rb +152 -0
  8. data/lib/pry/code.rb +351 -0
  9. data/lib/pry/command.rb +422 -0
  10. data/lib/pry/command_set.rb +259 -129
  11. data/lib/pry/commands.rb +0 -1
  12. data/lib/pry/config.rb +43 -9
  13. data/lib/pry/default_commands/context.rb +109 -92
  14. data/lib/pry/default_commands/documentation.rb +174 -63
  15. data/lib/pry/default_commands/easter_eggs.rb +26 -2
  16. data/lib/pry/default_commands/gems.rb +65 -37
  17. data/lib/pry/default_commands/input.rb +175 -243
  18. data/lib/pry/default_commands/introspection.rb +173 -112
  19. data/lib/pry/default_commands/ls.rb +96 -114
  20. data/lib/pry/default_commands/shell.rb +175 -70
  21. data/lib/pry/helpers/base_helpers.rb +7 -2
  22. data/lib/pry/helpers/command_helpers.rb +71 -77
  23. data/lib/pry/helpers/options_helpers.rb +10 -41
  24. data/lib/pry/helpers/text.rb +24 -4
  25. data/lib/pry/history.rb +55 -17
  26. data/lib/pry/history_array.rb +2 -0
  27. data/lib/pry/hooks.rb +252 -0
  28. data/lib/pry/indent.rb +9 -5
  29. data/lib/pry/method.rb +149 -50
  30. data/lib/pry/plugins.rb +12 -4
  31. data/lib/pry/pry_class.rb +69 -26
  32. data/lib/pry/pry_instance.rb +187 -115
  33. data/lib/pry/version.rb +1 -1
  34. data/lib/pry/wrapped_module.rb +73 -0
  35. data/man/pry.1 +195 -0
  36. data/man/pry.1.html +204 -0
  37. data/man/pry.1.ronn +141 -0
  38. data/pry.gemspec +29 -32
  39. data/test/helper.rb +32 -36
  40. data/test/test_cli.rb +78 -0
  41. data/test/test_code.rb +201 -0
  42. data/test/test_command.rb +327 -0
  43. data/test/test_command_integration.rb +512 -0
  44. data/test/test_command_set.rb +338 -12
  45. data/test/test_completion.rb +1 -1
  46. data/test/test_default_commands.rb +1 -2
  47. data/test/test_default_commands/test_context.rb +27 -5
  48. data/test/test_default_commands/test_documentation.rb +20 -8
  49. data/test/test_default_commands/test_input.rb +84 -45
  50. data/test/test_default_commands/test_introspection.rb +74 -17
  51. data/test/test_default_commands/test_ls.rb +9 -36
  52. data/test/test_default_commands/test_shell.rb +240 -13
  53. data/test/test_hooks.rb +490 -0
  54. data/test/test_indent.rb +2 -0
  55. data/test/test_method.rb +60 -0
  56. data/test/test_pry.rb +29 -904
  57. data/test/test_pry_defaults.rb +380 -0
  58. data/test/test_pry_history.rb +24 -24
  59. data/test/test_syntax_checking.rb +63 -0
  60. data/test/test_wrapped_module.rb +71 -0
  61. metadata +50 -39
  62. data/lib/pry/command_context.rb +0 -53
  63. data/lib/pry/command_processor.rb +0 -181
  64. data/lib/pry/extended_commands/user_command_api.rb +0 -65
  65. data/test/test_command_processor.rb +0 -176
@@ -0,0 +1,380 @@
1
+ require 'helper'
2
+ describe "test Pry defaults" do
3
+
4
+ after do
5
+ Pry.reset_defaults
6
+ Pry.color = false
7
+ end
8
+
9
+ describe "input" do
10
+
11
+ after do
12
+ Pry.reset_defaults
13
+ Pry.color = false
14
+ end
15
+
16
+ it 'should set the input default, and the default should be overridable' do
17
+ Pry.input = InputTester.new("5")
18
+
19
+ str_output = StringIO.new
20
+ Pry.output = str_output
21
+ Pry.new.rep
22
+ str_output.string.should =~ /5/
23
+
24
+ Pry.new(:input => InputTester.new("6")).rep
25
+ str_output.string.should =~ /6/
26
+ end
27
+
28
+ it 'should pass in the prompt if readline arity is 1' do
29
+ Pry.prompt = proc { "A" }
30
+
31
+ arity_one_input = Class.new do
32
+ attr_accessor :prompt
33
+ def readline(prompt)
34
+ @prompt = prompt
35
+ "exit-all"
36
+ end
37
+ end.new
38
+
39
+ Pry.start(self, :input => arity_one_input, :output => Pry::NullOutput)
40
+ arity_one_input.prompt.should == Pry.prompt.call
41
+ end
42
+
43
+ it 'should not pass in the prompt if the arity is 0' do
44
+ Pry.prompt = proc { "A" }
45
+
46
+ arity_zero_input = Class.new do
47
+ def readline
48
+ "exit-all"
49
+ end
50
+ end.new
51
+
52
+ lambda { Pry.start(self, :input => arity_zero_input, :output => Pry::NullOutput) }.should.not.raise Exception
53
+ end
54
+
55
+ it 'should not pass in the prompt if the arity is -1' do
56
+ Pry.prompt = proc { "A" }
57
+
58
+ arity_multi_input = Class.new do
59
+ attr_accessor :prompt
60
+
61
+ def readline(*args)
62
+ @prompt = args.first
63
+ "exit-all"
64
+ end
65
+ end.new
66
+
67
+ Pry.start(self, :input => arity_multi_input, :output => Pry::NullOutput)
68
+ arity_multi_input.prompt.should == nil
69
+ end
70
+
71
+ end
72
+
73
+ it 'should set the output default, and the default should be overridable' do
74
+ Pry.input = InputTester.new("5", "6", "7")
75
+
76
+ str_output = StringIO.new
77
+ Pry.output = str_output
78
+
79
+ Pry.new.rep
80
+ str_output.string.should =~ /5/
81
+
82
+ Pry.new.rep
83
+ str_output.string.should =~ /5\n.*6/
84
+
85
+ str_output2 = StringIO.new
86
+ Pry.new(:output => str_output2).rep
87
+ str_output2.string.should.not =~ /5\n.*6/
88
+ str_output2.string.should =~ /7/
89
+ end
90
+
91
+ it "should set the print default, and the default should be overridable" do
92
+ new_print = proc { |out, value| out.puts value }
93
+ Pry.print = new_print
94
+
95
+ Pry.new.print.should == Pry.print
96
+ str_output = StringIO.new
97
+ Pry.new(:input => InputTester.new("\"test\""), :output => str_output).rep
98
+ str_output.string.should == "test\n"
99
+
100
+ str_output = StringIO.new
101
+ Pry.new(:input => InputTester.new("\"test\""), :output => str_output,
102
+ :print => proc { |out, value| out.puts value.reverse }).rep
103
+ str_output.string.should == "tset\n"
104
+
105
+ Pry.new.print.should == Pry.print
106
+ str_output = StringIO.new
107
+ Pry.new(:input => InputTester.new("\"test\""), :output => str_output).rep
108
+ str_output.string.should == "test\n"
109
+ end
110
+
111
+ describe "pry return values" do
112
+ it 'should return nil' do
113
+ Pry.start(self, :input => StringIO.new("exit-all"), :output => Pry::NullOutput).should == nil
114
+ end
115
+
116
+ it 'should return the parameter given to exit-all' do
117
+ Pry.start(self, :input => StringIO.new("exit-all 10"), :output => Pry::NullOutput).should == 10
118
+ end
119
+
120
+ it 'should return the parameter (multi word string) given to exit-all' do
121
+ Pry.start(self, :input => StringIO.new("exit-all \"john mair\""), :output => Pry::NullOutput).should == "john mair"
122
+ end
123
+
124
+ it 'should return the parameter (function call) given to exit-all' do
125
+ Pry.start(self, :input => StringIO.new("exit-all 'abc'.reverse"), :output => Pry::NullOutput).should == 'cba'
126
+ end
127
+
128
+ it 'should return the parameter (self) given to exit-all' do
129
+ Pry.start("carl", :input => StringIO.new("exit-all self"), :output => Pry::NullOutput).should == "carl"
130
+ end
131
+ end
132
+
133
+ describe "prompts" do
134
+ before do
135
+ @empty_input_buffer = ""
136
+ @non_empty_input_buffer = "def hello"
137
+ @context = Pry.binding_for(0)
138
+ end
139
+
140
+ it 'should set the prompt default, and the default should be overridable (single prompt)' do
141
+ new_prompt = proc { "test prompt> " }
142
+ Pry.prompt = new_prompt
143
+
144
+ Pry.new.prompt.should == Pry.prompt
145
+ Pry.new.select_prompt(@empty_input_buffer, @context).should == "test prompt> "
146
+ Pry.new.select_prompt(@non_empty_input_buffer, @context).should == "test prompt> "
147
+
148
+ new_prompt = proc { "A" }
149
+ pry_tester = Pry.new(:prompt => new_prompt)
150
+ pry_tester.prompt.should == new_prompt
151
+ pry_tester.select_prompt(@empty_input_buffer, @context).should == "A"
152
+ pry_tester.select_prompt(@non_empty_input_buffer, @context).should == "A"
153
+
154
+ Pry.new.prompt.should == Pry.prompt
155
+ Pry.new.select_prompt(@empty_input_buffer, @context).should == "test prompt> "
156
+ Pry.new.select_prompt(@non_empty_input_buffer, @context).should == "test prompt> "
157
+ end
158
+
159
+ it 'should set the prompt default, and the default should be overridable (multi prompt)' do
160
+ new_prompt = [proc { "test prompt> " }, proc { "test prompt* " }]
161
+ Pry.prompt = new_prompt
162
+
163
+ Pry.new.prompt.should == Pry.prompt
164
+ Pry.new.select_prompt(@empty_input_buffer, @context).should == "test prompt> "
165
+ Pry.new.select_prompt(@non_empty_input_buffer, @context).should == "test prompt* "
166
+
167
+ new_prompt = [proc { "A" }, proc { "B" }]
168
+ pry_tester = Pry.new(:prompt => new_prompt)
169
+ pry_tester.prompt.should == new_prompt
170
+ pry_tester.select_prompt(@empty_input_buffer, @context).should == "A"
171
+ pry_tester.select_prompt(@non_empty_input_buffer, @context).should == "B"
172
+
173
+ Pry.new.prompt.should == Pry.prompt
174
+ Pry.new.select_prompt(@empty_input_buffer, @context).should == "test prompt> "
175
+ Pry.new.select_prompt(@non_empty_input_buffer, @context).should == "test prompt* "
176
+ end
177
+
178
+ describe 'storing and restoring the prompt' do
179
+ before do
180
+ make = lambda do |name,i|
181
+ prompt = [ proc { "#{i}>" } , proc { "#{i+1}>" } ]
182
+ (class << prompt; self; end).send(:define_method, :inspect) { "<Prompt-#{name}>" }
183
+ prompt
184
+ end
185
+ @a , @b , @c = make[:a,0] , make[:b,1] , make[:c,2]
186
+ @pry = Pry.new :prompt => @a
187
+ end
188
+ it 'should have a prompt stack' do
189
+ @pry.push_prompt @b
190
+ @pry.push_prompt @c
191
+ @pry.prompt.should == @c
192
+ @pry.pop_prompt
193
+ @pry.prompt.should == @b
194
+ @pry.pop_prompt
195
+ @pry.prompt.should == @a
196
+ end
197
+
198
+ it 'should restore overridden prompts when returning from file-mode' do
199
+ pry = Pry.new :input => InputTester.new('shell-mode', 'shell-mode'),
200
+ :prompt => [ proc { 'P>' } ] * 2
201
+ pry.select_prompt(@empty_input_buffer, @context).should == "P>"
202
+ pry.re
203
+ pry.select_prompt(@empty_input_buffer, @context).should =~ /\Apry .* \$ \z/
204
+ pry.re
205
+ pry.select_prompt(@empty_input_buffer, @context).should == "P>"
206
+ end
207
+
208
+ it '#pop_prompt should return the popped prompt' do
209
+ @pry.push_prompt @b
210
+ @pry.push_prompt @c
211
+ @pry.pop_prompt.should == @c
212
+ @pry.pop_prompt.should == @b
213
+ end
214
+
215
+ it 'should not pop the last prompt' do
216
+ @pry.push_prompt @b
217
+ @pry.pop_prompt.should == @b
218
+ @pry.pop_prompt.should == @a
219
+ @pry.pop_prompt.should == @a
220
+ @pry.prompt.should == @a
221
+ end
222
+
223
+ describe '#prompt= should replace the current prompt with the new prompt' do
224
+ it 'when only one prompt on the stack' do
225
+ @pry.prompt = @b
226
+ @pry.prompt.should == @b
227
+ @pry.pop_prompt.should == @b
228
+ @pry.pop_prompt.should == @b
229
+ end
230
+ it 'when several prompts on the stack' do
231
+ @pry.push_prompt @b
232
+ @pry.prompt = @c
233
+ @pry.pop_prompt.should == @c
234
+ @pry.pop_prompt.should == @a
235
+ end
236
+ end
237
+ end
238
+ end
239
+
240
+ describe "view_clip used for displaying an object in a truncated format" do
241
+
242
+ VC_MAX_LENGTH = 60
243
+
244
+ describe "given an object with an #inspect string" do
245
+ it "returns the #<> format of the object (never use inspect)" do
246
+ o = Object.new
247
+ def o.inspect; "a" * VC_MAX_LENGTH; end
248
+
249
+ Pry.view_clip(o, VC_MAX_LENGTH).should =~ /#<Object/
250
+ end
251
+ end
252
+
253
+ describe "given the 'main' object" do
254
+ it "returns the #to_s of main (special case)" do
255
+ o = TOPLEVEL_BINDING.eval('self')
256
+ Pry.view_clip(o, VC_MAX_LENGTH).should == o.to_s
257
+ end
258
+ end
259
+
260
+ describe "given the a Numeric, String or Symbol object" do
261
+ [1, 2.0, -5, "hello", :test].each do |o|
262
+ it "returns the #inspect of the special-cased immediate object: #{o}" do
263
+ Pry.view_clip(o, VC_MAX_LENGTH).should == o.inspect
264
+ end
265
+ end
266
+
267
+ # only testing with String here :)
268
+ it "returns #<> format of the special-cased immediate object if #inspect is longer than maximum" do
269
+ o = "o" * (VC_MAX_LENGTH + 1)
270
+ Pry.view_clip(o, VC_MAX_LENGTH).should =~ /#<String/
271
+ end
272
+ end
273
+
274
+ describe "given an object with an #inspect string as long as the maximum specified" do
275
+ it "returns the #<> format of the object (never use inspect)" do
276
+ o = Object.new
277
+ def o.inspect; "a" * VC_MAX_LENGTH; end
278
+
279
+ Pry.view_clip(o, VC_MAX_LENGTH).should =~ /#<Object/
280
+ end
281
+ end
282
+
283
+ describe "given a regular object with an #inspect string longer than the maximum specified" do
284
+
285
+ describe "when the object is a regular one" do
286
+ it "returns a string of the #<class name:object idish> format" do
287
+ o = Object.new
288
+ def o.inspect; "a" * (VC_MAX_LENGTH + 1); end
289
+
290
+ Pry.view_clip(o, VC_MAX_LENGTH).should =~ /#<Object/
291
+ end
292
+ end
293
+
294
+ describe "when the object is a Class or a Module" do
295
+ describe "without a name (usually a c = Class.new)" do
296
+ it "returns a string of the #<class name:object idish> format" do
297
+ c, m = Class.new, Module.new
298
+
299
+ Pry.view_clip(c, VC_MAX_LENGTH).should =~ /#<Class/
300
+ Pry.view_clip(m, VC_MAX_LENGTH).should =~ /#<Module/
301
+ end
302
+ end
303
+
304
+ describe "with a #name longer than the maximum specified" do
305
+ it "returns a string of the #<class name:object idish> format" do
306
+ c, m = Class.new, Module.new
307
+
308
+
309
+ def c.name; "a" * (VC_MAX_LENGTH + 1); end
310
+ def m.name; "a" * (VC_MAX_LENGTH + 1); end
311
+
312
+ Pry.view_clip(c, VC_MAX_LENGTH).should =~ /#<Class/
313
+ Pry.view_clip(m, VC_MAX_LENGTH).should =~ /#<Module/
314
+ end
315
+ end
316
+
317
+ describe "with a #name shorter than or equal to the maximum specified" do
318
+ it "returns a string of the #<class name:object idish> format" do
319
+ c, m = Class.new, Module.new
320
+
321
+ def c.name; "a" * VC_MAX_LENGTH; end
322
+ def m.name; "a" * VC_MAX_LENGTH; end
323
+
324
+ Pry.view_clip(c, VC_MAX_LENGTH).should == c.name
325
+ Pry.view_clip(m, VC_MAX_LENGTH).should == m.name
326
+ end
327
+ end
328
+
329
+ end
330
+
331
+ end
332
+
333
+ end
334
+
335
+ it 'should set the hooks default, and the default should be overridable' do
336
+ Pry.input = InputTester.new("exit-all")
337
+ Pry.hooks = Pry::Hooks.new.
338
+ add_hook(:before_session, :my_name) { |out,_,_| out.puts "HELLO" }.
339
+ add_hook(:after_session, :my_name) { |out,_,_| out.puts "BYE" }
340
+
341
+ str_output = StringIO.new
342
+ Pry.new(:output => str_output).repl
343
+ str_output.string.should =~ /HELLO/
344
+ str_output.string.should =~ /BYE/
345
+
346
+ Pry.input.rewind
347
+
348
+ str_output = StringIO.new
349
+ Pry.new(:output => str_output,
350
+ :hooks => Pry::Hooks.new.
351
+ add_hook( :before_session, :my_name) { |out,_,_| out.puts "MORNING" }.
352
+ add_hook(:after_session, :my_name) { |out,_,_| out.puts "EVENING" }
353
+ ).repl
354
+
355
+ str_output.string.should =~ /MORNING/
356
+ str_output.string.should =~ /EVENING/
357
+
358
+ # try below with just defining one hook
359
+ Pry.input.rewind
360
+ str_output = StringIO.new
361
+ Pry.new(:output => str_output,
362
+ :hooks => Pry::Hooks.new.
363
+ add_hook(:before_session, :my_name) { |out,_,_| out.puts "OPEN" }
364
+ ).repl
365
+
366
+ str_output.string.should =~ /OPEN/
367
+
368
+ Pry.input.rewind
369
+ str_output = StringIO.new
370
+ Pry.new(:output => str_output,
371
+ :hooks => Pry::Hooks.new.
372
+ add_hook(:after_session, :my_name) { |out,_,_| out.puts "CLOSE" }
373
+ ).repl
374
+
375
+ str_output.string.should =~ /CLOSE/
376
+
377
+ Pry.reset_defaults
378
+ Pry.color = false
379
+ end
380
+ end
@@ -2,26 +2,30 @@ require 'helper'
2
2
  require 'tempfile'
3
3
 
4
4
  describe Pry do
5
-
6
5
  before do
7
6
  Pry.history.clear
8
- @file = Tempfile.new(["tmp", ".pry_history"])
9
- @hist = @file.path
10
- File.open(@hist, 'w') {|f| f << "1\n2\n3\n" }
11
- @old_hist = Pry.config.history.file
12
- Pry.config.history.file = @hist
7
+
8
+ @saved_history = "1\n2\n3\n"
9
+
10
+ Pry.history.loader = proc do |&blk|
11
+ @saved_history.lines.each { |l| blk.call(l) }
12
+ end
13
+
14
+ Pry.history.saver = proc do |lines|
15
+ @saved_history << lines.map { |l| "#{l}\n" }.join
16
+ end
17
+
13
18
  Pry.load_history
14
19
  end
15
20
 
16
21
  after do
17
- @file.close
18
- File.unlink(@hist)
19
- Pry.config.history.file = @old_hist
22
+ Pry.history.clear
23
+ Pry.history.restore_default_behavior
20
24
  end
21
25
 
22
26
  describe ".load_history" do
23
27
  it "should read the contents of the file" do
24
- Pry.history.to_a[-2..-1].should === ["2", "3"]
28
+ Pry.history.to_a[-2..-1].should == %w(2 3)
25
29
  end
26
30
  end
27
31
 
@@ -29,56 +33,52 @@ describe Pry do
29
33
  it "should include a trailing newline" do
30
34
  Pry.history << "4"
31
35
  Pry.save_history
32
- File.read(@hist).should =~ /4\n\z/
36
+ @saved_history.should =~ /4\n\z/
33
37
  end
34
38
 
35
39
  it "should not change anything if history is not changed" do
36
- File.open(@hist, 'w') {|f| f << "4\n5\n6\n" }
40
+ @saved_history = "4\n5\n6\n"
37
41
  Pry.save_history
38
- File.read(@hist).should == "4\n5\n6\n"
42
+ @saved_history.should == "4\n5\n6\n"
39
43
  end
40
44
 
41
45
  it "should append new lines to the file" do
42
46
  Pry.history << "4"
43
47
  Pry.save_history
44
- File.read(@hist).should == "1\n2\n3\n4\n"
48
+ @saved_history.should == "1\n2\n3\n4\n"
45
49
  end
46
50
 
47
51
  it "should not clobber lines written by other Pry's in the meantime" do
48
52
  Pry.history << "5"
49
- File.open(@hist, 'a') {|f| f << "4\n" }
53
+ @saved_history << "4\n"
50
54
  Pry.save_history
51
55
 
52
56
  Pry.history.to_a[-3..-1].should == ["2", "3", "5"]
53
- File.read(@hist).should == "1\n2\n3\n4\n5\n"
57
+ @saved_history.should == "1\n2\n3\n4\n5\n"
54
58
  end
55
59
 
56
60
  it "should not delete lines from the file if this session's history was cleared" do
57
61
  Pry.history.clear
58
62
  Pry.save_history
59
- File.read(@hist).should == "1\n2\n3\n"
63
+ @saved_history.should == "1\n2\n3\n"
60
64
  end
61
65
 
62
66
  it "should save new lines that are added after the history was cleared" do
63
67
  Pry.history.clear
64
68
  Pry.history << "4"
65
-
66
- # doing this twice as libedit on 1.8.7 has bugs and sometimes ignores the
67
- # first line in history
68
- Pry.history << "4"
69
69
  Pry.save_history
70
- File.read(@hist).should =~ /1\n2\n3\n4\n/
70
+ @saved_history.should =~ /1\n2\n3\n4\n/
71
71
  end
72
72
 
73
73
  it "should only append new lines the second time it is saved" do
74
74
  Pry.history << "4"
75
75
  Pry.save_history
76
- File.open(@hist, 'a') {|f| f << "5\n" }
76
+ @saved_history << "5\n"
77
77
  Pry.history << "6"
78
78
  Pry.save_history
79
79
 
80
80
  Pry.history.to_a[-4..-1].should == ["2", "3", "4", "6"]
81
- File.read(@hist).should == "1\n2\n3\n4\n5\n6\n"
81
+ @saved_history.should == "1\n2\n3\n4\n5\n6\n"
82
82
  end
83
83
  end
84
84
  end