pry 0.9.10pre1-java → 0.9.11-java

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (194) hide show
  1. data/.travis.yml +3 -1
  2. data/CHANGELOG +63 -2
  3. data/CONTRIBUTORS +43 -25
  4. data/Gemfile +7 -0
  5. data/Guardfile +62 -0
  6. data/README.markdown +4 -4
  7. data/Rakefile +34 -35
  8. data/lib/pry.rb +107 -54
  9. data/lib/pry/cli.rb +34 -11
  10. data/lib/pry/code.rb +165 -182
  11. data/lib/pry/code/code_range.rb +70 -0
  12. data/lib/pry/code/loc.rb +92 -0
  13. data/lib/pry/code_object.rb +153 -0
  14. data/lib/pry/command.rb +160 -22
  15. data/lib/pry/command_set.rb +37 -26
  16. data/lib/pry/commands.rb +4 -27
  17. data/lib/pry/commands/amend_line.rb +99 -0
  18. data/lib/pry/commands/bang.rb +20 -0
  19. data/lib/pry/commands/bang_pry.rb +17 -0
  20. data/lib/pry/commands/cat.rb +53 -0
  21. data/lib/pry/commands/cat/abstract_formatter.rb +27 -0
  22. data/lib/pry/commands/cat/exception_formatter.rb +78 -0
  23. data/lib/pry/commands/cat/file_formatter.rb +84 -0
  24. data/lib/pry/commands/cat/input_expression_formatter.rb +43 -0
  25. data/lib/pry/commands/cd.rb +30 -0
  26. data/lib/pry/commands/code_collector.rb +165 -0
  27. data/lib/pry/commands/deprecated_commands.rb +2 -0
  28. data/lib/pry/commands/disable_pry.rb +27 -0
  29. data/lib/pry/commands/easter_eggs.rb +112 -0
  30. data/lib/pry/commands/edit.rb +206 -0
  31. data/lib/pry/commands/edit/exception_patcher.rb +25 -0
  32. data/lib/pry/commands/edit/file_and_line_locator.rb +38 -0
  33. data/lib/pry/commands/edit/method_patcher.rb +122 -0
  34. data/lib/pry/commands/exit.rb +42 -0
  35. data/lib/pry/commands/exit_all.rb +29 -0
  36. data/lib/pry/commands/exit_program.rb +24 -0
  37. data/lib/pry/commands/find_method.rb +199 -0
  38. data/lib/pry/commands/fix_indent.rb +19 -0
  39. data/lib/pry/commands/gem_cd.rb +26 -0
  40. data/lib/pry/commands/gem_install.rb +29 -0
  41. data/lib/pry/commands/gem_list.rb +33 -0
  42. data/lib/pry/commands/gem_open.rb +29 -0
  43. data/lib/pry/commands/gist.rb +95 -0
  44. data/lib/pry/commands/help.rb +164 -0
  45. data/lib/pry/commands/hist.rb +161 -0
  46. data/lib/pry/commands/import_set.rb +22 -0
  47. data/lib/pry/commands/install_command.rb +51 -0
  48. data/lib/pry/commands/jump_to.rb +29 -0
  49. data/lib/pry/commands/ls.rb +339 -0
  50. data/lib/pry/commands/nesting.rb +25 -0
  51. data/lib/pry/commands/play.rb +69 -0
  52. data/lib/pry/commands/pry_backtrace.rb +26 -0
  53. data/lib/pry/commands/pry_version.rb +17 -0
  54. data/lib/pry/commands/raise_up.rb +32 -0
  55. data/lib/pry/commands/reload_code.rb +39 -0
  56. data/lib/pry/commands/reset.rb +18 -0
  57. data/lib/pry/commands/ri.rb +56 -0
  58. data/lib/pry/commands/save_file.rb +61 -0
  59. data/lib/pry/commands/shell_command.rb +43 -0
  60. data/lib/pry/commands/shell_mode.rb +27 -0
  61. data/lib/pry/commands/show_doc.rb +78 -0
  62. data/lib/pry/commands/show_info.rb +139 -0
  63. data/lib/pry/commands/show_input.rb +17 -0
  64. data/lib/pry/commands/show_source.rb +37 -0
  65. data/lib/pry/commands/simple_prompt.rb +22 -0
  66. data/lib/pry/commands/stat.rb +40 -0
  67. data/lib/pry/commands/switch_to.rb +23 -0
  68. data/lib/pry/commands/toggle_color.rb +20 -0
  69. data/lib/pry/commands/whereami.rb +114 -0
  70. data/lib/pry/commands/wtf.rb +57 -0
  71. data/lib/pry/completion.rb +120 -46
  72. data/lib/pry/config.rb +11 -0
  73. data/lib/pry/core_extensions.rb +30 -19
  74. data/lib/pry/editor.rb +129 -0
  75. data/lib/pry/helpers.rb +1 -0
  76. data/lib/pry/helpers/base_helpers.rb +89 -119
  77. data/lib/pry/helpers/command_helpers.rb +7 -122
  78. data/lib/pry/helpers/table.rb +100 -0
  79. data/lib/pry/helpers/text.rb +4 -4
  80. data/lib/pry/history_array.rb +5 -0
  81. data/lib/pry/hooks.rb +1 -3
  82. data/lib/pry/indent.rb +104 -30
  83. data/lib/pry/method.rb +66 -22
  84. data/lib/pry/module_candidate.rb +26 -15
  85. data/lib/pry/pager.rb +70 -0
  86. data/lib/pry/plugins.rb +1 -2
  87. data/lib/pry/pry_class.rb +63 -22
  88. data/lib/pry/pry_instance.rb +58 -37
  89. data/lib/pry/rubygem.rb +74 -0
  90. data/lib/pry/terminal_info.rb +43 -0
  91. data/lib/pry/test/helper.rb +185 -0
  92. data/lib/pry/version.rb +1 -1
  93. data/lib/pry/wrapped_module.rb +58 -24
  94. data/pry.gemspec +21 -37
  95. data/{test/test_cli.rb → spec/cli_spec.rb} +0 -0
  96. data/spec/code_object_spec.rb +277 -0
  97. data/{test/test_code.rb → spec/code_spec.rb} +19 -1
  98. data/{test/test_command_helpers.rb → spec/command_helpers_spec.rb} +0 -0
  99. data/{test/test_command_integration.rb → spec/command_integration_spec.rb} +38 -46
  100. data/{test/test_command_set.rb → spec/command_set_spec.rb} +18 -1
  101. data/{test/test_command.rb → spec/command_spec.rb} +250 -149
  102. data/spec/commands/amend_line_spec.rb +247 -0
  103. data/spec/commands/bang_spec.rb +19 -0
  104. data/spec/commands/cat_spec.rb +164 -0
  105. data/spec/commands/cd_spec.rb +250 -0
  106. data/spec/commands/disable_pry_spec.rb +25 -0
  107. data/spec/commands/edit_spec.rb +727 -0
  108. data/spec/commands/exit_all_spec.rb +34 -0
  109. data/spec/commands/exit_program_spec.rb +19 -0
  110. data/spec/commands/exit_spec.rb +34 -0
  111. data/{test/test_default_commands/test_find_method.rb → spec/commands/find_method_spec.rb} +27 -7
  112. data/spec/commands/gem_list_spec.rb +26 -0
  113. data/spec/commands/gist_spec.rb +75 -0
  114. data/{test/test_default_commands/test_help.rb → spec/commands/help_spec.rb} +8 -9
  115. data/spec/commands/hist_spec.rb +181 -0
  116. data/spec/commands/jump_to_spec.rb +15 -0
  117. data/spec/commands/ls_spec.rb +177 -0
  118. data/spec/commands/play_spec.rb +140 -0
  119. data/spec/commands/raise_up_spec.rb +56 -0
  120. data/spec/commands/save_file_spec.rb +177 -0
  121. data/spec/commands/show_doc_spec.rb +378 -0
  122. data/spec/commands/show_input_spec.rb +17 -0
  123. data/spec/commands/show_source_spec.rb +597 -0
  124. data/spec/commands/whereami_spec.rb +154 -0
  125. data/spec/completion_spec.rb +233 -0
  126. data/spec/control_d_handler_spec.rb +58 -0
  127. data/spec/editor_spec.rb +79 -0
  128. data/{test/test_exception_whitelist.rb → spec/exception_whitelist_spec.rb} +0 -0
  129. data/{test → spec/fixtures}/candidate_helper1.rb +0 -0
  130. data/{test → spec/fixtures}/candidate_helper2.rb +0 -0
  131. data/{test/test_default_commands → spec/fixtures}/example.erb +0 -0
  132. data/spec/fixtures/example_nesting.rb +33 -0
  133. data/spec/fixtures/show_source_doc_examples.rb +15 -0
  134. data/{test → spec/fixtures}/testrc +0 -0
  135. data/{test → spec/fixtures}/testrcbad +0 -0
  136. data/spec/helper.rb +34 -0
  137. data/spec/helpers/bacon.rb +86 -0
  138. data/spec/helpers/mock_pry.rb +43 -0
  139. data/spec/helpers/table_spec.rb +83 -0
  140. data/{test/test_history_array.rb → spec/history_array_spec.rb} +21 -19
  141. data/{test/test_hooks.rb → spec/hooks_spec.rb} +0 -0
  142. data/{test/test_indent.rb → spec/indent_spec.rb} +24 -0
  143. data/{test/test_input_stack.rb → spec/input_stack_spec.rb} +4 -0
  144. data/{test/test_method.rb → spec/method_spec.rb} +65 -1
  145. data/{test/test_prompt.rb → spec/prompt_spec.rb} +0 -0
  146. data/{test/test_pry_defaults.rb → spec/pry_defaults_spec.rb} +14 -14
  147. data/{test/test_pry_history.rb → spec/pry_history_spec.rb} +15 -0
  148. data/spec/pry_output_spec.rb +95 -0
  149. data/{test/test_pry.rb → spec/pry_spec.rb} +74 -32
  150. data/{test/test_sticky_locals.rb → spec/sticky_locals_spec.rb} +27 -25
  151. data/{test/test_syntax_checking.rb → spec/syntax_checking_spec.rb} +17 -1
  152. data/{test/test_wrapped_module.rb → spec/wrapped_module_spec.rb} +92 -5
  153. metadata +239 -115
  154. data/examples/example_basic.rb +0 -15
  155. data/examples/example_command_override.rb +0 -32
  156. data/examples/example_commands.rb +0 -36
  157. data/examples/example_hooks.rb +0 -9
  158. data/examples/example_image_edit.rb +0 -67
  159. data/examples/example_input.rb +0 -7
  160. data/examples/example_input2.rb +0 -29
  161. data/examples/example_output.rb +0 -11
  162. data/examples/example_print.rb +0 -6
  163. data/examples/example_prompt.rb +0 -9
  164. data/examples/helper.rb +0 -6
  165. data/lib/pry/default_commands/cd.rb +0 -81
  166. data/lib/pry/default_commands/commands.rb +0 -62
  167. data/lib/pry/default_commands/context.rb +0 -98
  168. data/lib/pry/default_commands/easter_eggs.rb +0 -95
  169. data/lib/pry/default_commands/editing.rb +0 -420
  170. data/lib/pry/default_commands/find_method.rb +0 -169
  171. data/lib/pry/default_commands/gems.rb +0 -84
  172. data/lib/pry/default_commands/gist.rb +0 -187
  173. data/lib/pry/default_commands/help.rb +0 -127
  174. data/lib/pry/default_commands/hist.rb +0 -120
  175. data/lib/pry/default_commands/input_and_output.rb +0 -306
  176. data/lib/pry/default_commands/introspection.rb +0 -410
  177. data/lib/pry/default_commands/ls.rb +0 -272
  178. data/lib/pry/default_commands/misc.rb +0 -38
  179. data/lib/pry/default_commands/navigating_pry.rb +0 -110
  180. data/lib/pry/default_commands/whereami.rb +0 -92
  181. data/lib/pry/extended_commands/experimental.rb +0 -7
  182. data/test/helper.rb +0 -223
  183. data/test/test_completion.rb +0 -62
  184. data/test/test_control_d_handler.rb +0 -45
  185. data/test/test_default_commands/test_cd.rb +0 -321
  186. data/test/test_default_commands/test_context.rb +0 -288
  187. data/test/test_default_commands/test_documentation.rb +0 -315
  188. data/test/test_default_commands/test_gems.rb +0 -18
  189. data/test/test_default_commands/test_input.rb +0 -428
  190. data/test/test_default_commands/test_introspection.rb +0 -511
  191. data/test/test_default_commands/test_ls.rb +0 -151
  192. data/test/test_default_commands/test_shell.rb +0 -343
  193. data/test/test_default_commands/test_show_source.rb +0 -432
  194. data/test/test_pry_output.rb +0 -41
@@ -0,0 +1,378 @@
1
+ require 'helper'
2
+ require "fixtures/show_source_doc_examples"
3
+
4
+ if !PryTestHelpers.mri18_and_no_real_source_location?
5
+ describe "show-doc" do
6
+ before do
7
+ @o = Object.new
8
+
9
+ # sample doc
10
+ def @o.sample_method
11
+ :sample
12
+ end
13
+ end
14
+
15
+ it 'should output a method\'s documentation' do
16
+ pry_eval(binding, "show-doc @o.sample_method").should =~ /sample doc/
17
+ end
18
+
19
+ it 'should output a method\'s documentation with line numbers' do
20
+ pry_eval(binding, "show-doc @o.sample_method -l").should =~ /\d: sample doc/
21
+ end
22
+
23
+ it 'should output a method\'s documentation with line numbers (base one)' do
24
+ pry_eval(binding, "show-doc @o.sample_method -b").should =~ /1: sample doc/
25
+ end
26
+
27
+ it 'should output a method\'s documentation if inside method without needing to use method name' do
28
+ # sample comment
29
+ def @o.sample
30
+ pry_eval(binding, 'show-doc').should =~ /sample comment/
31
+ end
32
+ @o.sample
33
+ end
34
+
35
+ it "should be able to find super methods" do
36
+ b = Class.new{
37
+ # daddy initialize!
38
+ def initialize(*args) ;end
39
+ }
40
+
41
+ c = Class.new(b){
42
+ # classy initialize!
43
+ def initialize(*args); end
44
+ }
45
+
46
+ d = Class.new(c){
47
+ # grungy initialize??
48
+ def initialize(*args, &block); end
49
+ }
50
+
51
+ o = d.new
52
+
53
+ # instancey initialize!
54
+ def o.initialize; end
55
+
56
+ t = pry_tester(binding)
57
+
58
+ t.eval("show-doc o.initialize").should =~ /instancey initialize/
59
+ t.eval("show-doc --super o.initialize").should =~ /grungy initialize/
60
+ t.eval("show-doc o.initialize -ss").should =~ /classy initialize/
61
+ t.eval("show-doc o.initialize -sss").should =~ /daddy initialize/
62
+ end
63
+
64
+ describe "rdoc highlighting" do
65
+ it "should syntax highlight code in rdoc" do
66
+ c = Class.new{
67
+ # This can initialize your class:
68
+ #
69
+ # a = c.new :foo
70
+ #
71
+ # @param foo
72
+ def initialize(foo); end
73
+ }
74
+
75
+ begin
76
+ t = pry_tester(binding)
77
+ t.eval("show-doc c#initialize").should =~ /c.new :foo/
78
+ Pry.config.color = true
79
+ # I don't want the test to rely on which colour codes are there, just to
80
+ # assert that "something" is being colourized.
81
+ t.eval("show-doc c#initialize").should.not =~ /c.new :foo/
82
+ ensure
83
+ Pry.config.color = false
84
+ end
85
+ end
86
+
87
+ it "should syntax highlight `code` in rdoc" do
88
+ c = Class.new{
89
+ # After initializing your class with `c.new(:foo)`, go have fun!
90
+ #
91
+ # @param foo
92
+ def initialize(foo); end
93
+ }
94
+
95
+ begin
96
+ t = pry_tester(binding)
97
+ t.eval("show-doc c#initialize").should =~ /c.new\(:foo\)/
98
+ Pry.config.color = true
99
+ # I don't want the test to rely on which colour codes are there, just to
100
+ # assert that "something" is being colourized.
101
+ t.eval("show-doc c#initialize").should.not =~ /c.new\(:foo\)/
102
+ ensure
103
+ Pry.config.color = false
104
+ end
105
+
106
+ end
107
+
108
+ it "should not syntax highlight `` inside code" do
109
+ c = Class.new{
110
+ # Convert aligned output (from many shell commands) into nested arrays:
111
+ #
112
+ # a = decolumnize `ls -l $HOME`
113
+ #
114
+ # @param output
115
+ def decolumnize(output); end
116
+ }
117
+
118
+ begin
119
+ t = pry_tester(binding)
120
+ Pry.config.color = true
121
+ t.eval("show-doc c#decolumnize").should =~ /ls -l \$HOME/
122
+ t.eval("show-doc c#decolumnize").should.not =~ /`ls -l \$HOME`/
123
+ ensure
124
+ Pry.config.color = false
125
+ end
126
+ end
127
+ end
128
+
129
+ describe "on sourcable objects" do
130
+ it "should show documentation for object" do
131
+ # this is a documentation
132
+ hello = proc { puts 'hello world!' }
133
+ mock_pry(binding, "show-doc hello").should =~ /this is a documentation/
134
+ end
135
+ end
136
+
137
+ describe "on modules" do
138
+ before do
139
+ # god this is boring1
140
+ class ShowSourceTestClass
141
+ def alpha
142
+ end
143
+ end
144
+
145
+ # god this is boring2
146
+ module ShowSourceTestModule
147
+ def alpha
148
+ end
149
+ end
150
+
151
+ # god this is boring3
152
+ ShowSourceTestClassWeirdSyntax = Class.new do
153
+ def beta
154
+ end
155
+ end
156
+
157
+ # god this is boring4
158
+ ShowSourceTestModuleWeirdSyntax = Module.new do
159
+ def beta
160
+ end
161
+ end
162
+ end
163
+
164
+ after do
165
+ Object.remove_const :ShowSourceTestClass
166
+ Object.remove_const :ShowSourceTestClassWeirdSyntax
167
+ Object.remove_const :ShowSourceTestModule
168
+ Object.remove_const :ShowSourceTestModuleWeirdSyntax
169
+ end
170
+
171
+ describe "basic functionality, should show docs for top-level module definitions" do
172
+ it 'should show docs for a class' do
173
+ pry_eval("show-doc ShowSourceTestClass").should =~
174
+ /god this is boring1/
175
+ end
176
+
177
+ it 'should show docs for a module' do
178
+ pry_eval("show-doc ShowSourceTestModule").should =~
179
+ /god this is boring2/
180
+ end
181
+
182
+ it 'should show docs for a class when Const = Class.new syntax is used' do
183
+ pry_eval("show-doc ShowSourceTestClassWeirdSyntax").should =~
184
+ /god this is boring3/
185
+ end
186
+
187
+ it 'should show docs for a module when Const = Module.new syntax is used' do
188
+ pry_eval("show-doc ShowSourceTestModuleWeirdSyntax").should =~
189
+ /god this is boring4/
190
+ end
191
+ end
192
+
193
+ if !Pry::Helpers::BaseHelpers.mri_18?
194
+ describe "in REPL" do
195
+ it 'should find class defined in repl' do
196
+ t = pry_tester
197
+ t.eval <<-RUBY
198
+ # hello tobina
199
+ class TobinaMyDog
200
+ def woof
201
+ end
202
+ end
203
+ RUBY
204
+ t.eval('show-doc TobinaMyDog').should =~ /hello tobina/
205
+ Object.remove_const :TobinaMyDog
206
+ end
207
+ end
208
+ end
209
+
210
+ it 'should lookup module name with respect to current context' do
211
+ constant_scope(:AlphaClass, :BetaClass) do
212
+ # top-level beta
213
+ class BetaClass
214
+ def alpha
215
+ end
216
+ end
217
+
218
+ class AlphaClass
219
+ # nested beta
220
+ class BetaClass
221
+ def beta
222
+ end
223
+ end
224
+ end
225
+
226
+ pry_eval(AlphaClass, "show-doc BetaClass").should =~ /nested beta/
227
+ end
228
+ end
229
+
230
+ it 'should look up nested modules' do
231
+ constant_scope(:AlphaClass) do
232
+ class AlphaClass
233
+ # nested beta
234
+ class BetaClass
235
+ def beta
236
+ end
237
+ end
238
+ end
239
+
240
+ pry_eval("show-doc AlphaClass::BetaClass").should =~
241
+ /nested beta/
242
+ end
243
+ end
244
+
245
+ describe "show-doc -a" do
246
+ it 'should show the docs for all monkeypatches defined in different files' do
247
+ # local monkeypatch
248
+ class TestClassForShowSource
249
+ def beta
250
+ end
251
+ end
252
+
253
+ result = pry_eval("show-doc TestClassForShowSource -a")
254
+ result.should =~ /used by/
255
+ result.should =~ /local monkeypatch/
256
+ end
257
+ end
258
+
259
+ describe "when no class/module arg is given" do
260
+ before do
261
+ module TestHost
262
+
263
+ # hello there froggy
264
+ module M
265
+ def d; end
266
+ def e; end
267
+ end
268
+ end
269
+ end
270
+
271
+ after do
272
+ Object.remove_const(:TestHost)
273
+ end
274
+
275
+ it 'should return doc for current module' do
276
+ pry_eval(TestHost::M, "show-doc").should =~ /hello there froggy/
277
+ end
278
+ end
279
+
280
+ # FIXME: THis is nto a good spec anyway, because i dont think it
281
+ # SHOULD skip!
282
+ # describe "should skip over broken modules" do
283
+ # before do
284
+ # module TestHost
285
+ # # hello
286
+ # module M
287
+ # binding.eval("def a; end", "dummy.rb", 1)
288
+ # binding.eval("def b; end", "dummy.rb", 2)
289
+ # binding.eval("def c; end", "dummy.rb", 3)
290
+ # end
291
+
292
+ # # goodbye
293
+ # module M
294
+ # def d; end
295
+ # def e; end
296
+ # end
297
+ # end
298
+ # end
299
+
300
+ # after do
301
+ # Object.remove_const(:TestHost)
302
+ # end
303
+
304
+ # it 'should return doc for first valid module' do
305
+ # result = pry_eval("show-doc TestHost::M")
306
+ # result.should =~ /goodbye/
307
+ # result.should.not =~ /hello/
308
+ # end
309
+ # end
310
+ end
311
+
312
+ describe "on commands" do
313
+ # mostly copied & modified from test_help.rb
314
+ before do
315
+ @oldset = Pry.config.commands
316
+ @set = Pry.config.commands = Pry::CommandSet.new do
317
+ import Pry::Commands
318
+ end
319
+ end
320
+
321
+ after do
322
+ Pry.config.commands = @oldset
323
+ end
324
+
325
+ it 'should display help for a specific command' do
326
+ pry_eval('show-doc ls').should =~ /Usage: ls/
327
+ end
328
+
329
+ it 'should display help for a regex command with a "listing"' do
330
+ @set.command /bar(.*)/, "Test listing", :listing => "foo" do; end
331
+ pry_eval('show-doc foo').should =~ /Test listing/
332
+ end
333
+
334
+ it 'should display help for a command with a spaces in its name' do
335
+ @set.command "command with spaces", "description of a command with spaces" do; end
336
+ pry_eval('show-doc command with spaces').should =~ /description of a command with spaces/
337
+ end
338
+
339
+ describe "class commands" do
340
+ before do
341
+ # pretty pink pincers
342
+ class LobsterLady < Pry::ClassCommand
343
+ match "lobster-lady"
344
+ description "nada."
345
+ def process
346
+ "lobster"
347
+ end
348
+ end
349
+
350
+ Pry.commands.add_command(LobsterLady)
351
+ end
352
+
353
+ after do
354
+ Object.remove_const(:LobsterLady)
355
+ end
356
+
357
+ it 'should display "help" when looking up by command name' do
358
+ pry_eval('show-doc lobster-lady').should =~ /nada/
359
+ Pry.commands.delete("lobster-lady")
360
+ end
361
+
362
+ it 'should display actual preceding comment for a class command, when class is used (rather than command name) when looking up' do
363
+ pry_eval('show-doc LobsterLady').should =~ /pretty pink pincers/
364
+ Pry.commands.delete("lobster-lady")
365
+ end
366
+ end
367
+ end
368
+
369
+ describe "should set _file_ and _dir_" do
370
+ it 'should set _file_ and _dir_ to file containing method source' do
371
+ t = pry_tester
372
+ t.process_command "show-doc TestClassForShowSource#alpha"
373
+ t.pry.last_file.should =~ /show_source_doc_examples/
374
+ t.pry.last_dir.should =~ /fixtures/
375
+ end
376
+ end
377
+ end
378
+ end
@@ -0,0 +1,17 @@
1
+ require 'helper'
2
+
3
+ describe "show-input" do
4
+ before do
5
+ @t = pry_tester
6
+ end
7
+
8
+ it 'should correctly show the current lines in the input buffer' do
9
+ eval_str = unindent(<<-STR)
10
+ def hello
11
+ puts :bing
12
+ STR
13
+
14
+ @t.process_command 'show-input', eval_str
15
+ @t.last_output.should =~ /\A\d+: def hello\n\d+: puts :bing/
16
+ end
17
+ end
@@ -0,0 +1,597 @@
1
+ require 'helper'
2
+ require "fixtures/show_source_doc_examples"
3
+
4
+ if !PryTestHelpers.mri18_and_no_real_source_location?
5
+ describe "show-source" do
6
+ before do
7
+ @o = Object.new
8
+ def @o.sample_method
9
+ :sample
10
+ end
11
+
12
+ Object.const_set(:Test, Module.new)
13
+ end
14
+
15
+ after do
16
+ Pad.clear
17
+ end
18
+
19
+ it "should output a method's source" do
20
+ pry_eval(binding, 'show-source @o.sample_method').should =~ /def @o.sample/
21
+ end
22
+
23
+ it "should output help" do
24
+ pry_eval('show-source -h').should =~ /Usage:\s+show-source/
25
+ end
26
+
27
+ it "should output a method's source with line numbers" do
28
+ pry_eval(binding, 'show-source -l @o.sample_method').should =~ /\d+: def @o.sample/
29
+ end
30
+
31
+ it "should output a method's source with line numbers starting at 1" do
32
+ pry_eval(binding, 'show-source -b @o.sample_method').should =~ /1: def @o.sample/
33
+ end
34
+
35
+ it "should output a method's source if inside method and no name given" do
36
+ def @o.sample
37
+ pry_eval(binding, 'show-source').should =~ /def @o.sample/
38
+ end
39
+ @o.sample
40
+ end
41
+
42
+ it "should output a method's source inside method using the -l switch" do
43
+ def @o.sample
44
+ pry_eval(binding, 'show-source -l').should =~ /def @o.sample/
45
+ end
46
+ @o.sample
47
+ end
48
+
49
+ it "should find methods even if there are spaces in the arguments" do
50
+ def @o.foo(*bars)
51
+ "Mr flibble"
52
+ self
53
+ end
54
+
55
+ out = pry_eval(binding, "show-source @o.foo('bar', 'baz bam').foo")
56
+ out.should =~ /Mr flibble/
57
+ end
58
+
59
+ it "should find methods even if the object overrides method method" do
60
+ c = Class.new{
61
+ def method;
62
+ 98
63
+ end
64
+ }
65
+
66
+ pry_eval(binding, "show-source c.new.method").should =~ /98/
67
+ end
68
+
69
+ it "should not show the source when a non-extant method is requested" do
70
+ c = Class.new{ def method; 98; end }
71
+ mock_pry(binding, "show-source c#wrongmethod").should =~ /Couldn't locate/
72
+ end
73
+
74
+ it "should find instance_methods if the class overrides instance_method" do
75
+ c = Class.new{
76
+ def method;
77
+ 98
78
+ end
79
+
80
+ def self.instance_method; 789; end
81
+ }
82
+
83
+ pry_eval(binding, "show-source c#method").should =~ /98/
84
+ end
85
+
86
+ it "should find instance methods with self#moo" do
87
+ c = Class.new{ def moo; "ve over!"; end }
88
+
89
+ pry_eval(binding, "cd c", "show-source self#moo").should =~ /ve over/
90
+ end
91
+
92
+ it "should not find instance methods with self.moo" do
93
+ c = Class.new{ def moo; "ve over!"; end }
94
+
95
+ proc {
96
+ pry_eval(binding, 'cd c', 'show-source self.moo')
97
+ }.should.raise(Pry::CommandError).message.should =~ /Couldn't locate/
98
+ end
99
+
100
+ it "should find normal methods with self.moo" do
101
+ c = Class.new{ def self.moo; "ve over!"; end }
102
+
103
+ pry_eval(binding, 'cd c', 'show-source self.moo').should =~ /ve over/
104
+ end
105
+
106
+ it "should not find normal methods with self#moo" do
107
+ c = Class.new{ def self.moo; "ve over!"; end }
108
+
109
+ proc {
110
+ pry_eval(binding, 'cd c', 'show-source self#moo')
111
+ }.should.raise(Pry::CommandError).message.should =~ /Couldn't locate/
112
+ end
113
+
114
+ it "should find normal methods (i.e non-instance methods) by default" do
115
+ c = Class.new{ def self.moo; "ve over!"; end }
116
+
117
+ pry_eval(binding, "cd c", "show-source moo").should =~ /ve over/
118
+ end
119
+
120
+ it "should find instance methods if no normal methods available" do
121
+ c = Class.new{ def moo; "ve over!"; end }
122
+
123
+ pry_eval(binding, "cd c", "show-source moo").should =~ /ve over/
124
+ end
125
+
126
+ it "should find super methods" do
127
+ class Foo
128
+ def foo(*bars)
129
+ :super_wibble
130
+ end
131
+ end
132
+ o = Foo.new
133
+ Object.remove_const(:Foo)
134
+ def o.foo(*bars)
135
+ :wibble
136
+ end
137
+
138
+ pry_eval(binding, "show-source --super o.foo").
139
+ should =~ /:super_wibble/
140
+ end
141
+
142
+ it "should raise a CommandError when super method doesn't exist" do
143
+ def @o.foo(*bars); end
144
+
145
+ proc {
146
+ pry_eval(binding, "show-source --super @o.foo")
147
+ }.should.raise(Pry::CommandError).message.should =~ /No superclass found/
148
+ end
149
+
150
+ # dynamically defined method source retrieval is only supported in
151
+ # 1.9 - where Method#source_location is native
152
+ if RUBY_VERSION =~ /1.9/
153
+ it "should output the source of a method defined inside Pry" do
154
+ out = pry_eval("def dyn_method\n:test\nend", 'show-source dyn_method')
155
+ out.should =~ /def dyn_method/
156
+ Object.remove_method :dyn_method
157
+ end
158
+
159
+ it 'should output source for an instance method defined inside pry' do
160
+ pry_tester.tap do |t|
161
+ t.eval "class Test::A\n def yo\n end\nend"
162
+ t.eval('show-source Test::A#yo').should =~ /def yo/
163
+ end
164
+ end
165
+
166
+ it 'should output source for a repl method defined using define_method' do
167
+ pry_tester.tap do |t|
168
+ t.eval "class Test::A\n define_method(:yup) {}\nend"
169
+ t.eval('show-source Test::A#yup').should =~ /define_method\(:yup\)/
170
+ end
171
+ end
172
+
173
+ it "should output the source of a command defined inside Pry" do
174
+ command_definition = %{
175
+ Pry.commands.command "hubba-hubba" do
176
+ puts "that's what she said!"
177
+ end
178
+ }
179
+ out = pry_eval(command_definition, 'show-source hubba-hubba')
180
+ out.should =~ /what she said/
181
+ Pry.commands.delete "hubba-hubba"
182
+ end
183
+ end
184
+
185
+ describe "on sourcable objects" do
186
+ if RUBY_VERSION =~ /1.9/
187
+ it "should output source defined inside pry" do
188
+ pry_tester.tap do |t|
189
+ t.eval "hello = proc { puts 'hello world!' }"
190
+ t.eval("show-source hello").should =~ /proc \{ puts/
191
+ end
192
+ end
193
+ end
194
+
195
+ it "should output source for procs/lambdas stored in variables" do
196
+ hello = proc { puts 'hello world!' }
197
+ pry_eval(binding, 'show-source hello').should =~ /proc \{ puts/
198
+ end
199
+
200
+ it "should output source for procs/lambdas stored in constants" do
201
+ HELLO = proc { puts 'hello world!' }
202
+ pry_eval(binding, "show-source HELLO").should =~ /proc \{ puts/
203
+ Object.remove_const(:HELLO)
204
+ end
205
+
206
+ it "should output source for method objects" do
207
+ def @o.hi; puts 'hi world'; end
208
+ meth = @o.method(:hi)
209
+ pry_eval(binding, "show-source meth").should =~ /puts 'hi world'/
210
+ end
211
+
212
+ describe "on variables that shadow methods" do
213
+ before do
214
+ @t = pry_tester.eval unindent(<<-EOS)
215
+ class ::TestHost
216
+ def hello
217
+ hello = proc { ' smile ' }
218
+ pry_tester(binding)
219
+ end
220
+ end
221
+ ::TestHost.new.hello
222
+ EOS
223
+ end
224
+
225
+ after do
226
+ Object.remove_const(:TestHost)
227
+ end
228
+
229
+ it "source of variable should take precedence over method that is being shadowed" do
230
+ source = @t.eval('show-source hello')
231
+ source.should.not =~ /def hello/
232
+ source.should =~ /proc \{ ' smile ' \}/
233
+ end
234
+
235
+ it "source of method being shadowed should take precedence over variable
236
+ if given self.meth_name syntax" do
237
+ @t.eval('show-source self.hello').should =~ /def hello/
238
+ end
239
+ end
240
+
241
+ end
242
+
243
+ describe "on variable or constant" do
244
+ before do
245
+ class TestHost
246
+ def hello
247
+ "hi there"
248
+ end
249
+ end
250
+ end
251
+
252
+ after do
253
+ Object.remove_const(:TestHost)
254
+ end
255
+
256
+ it "should output source of its class if variable doesn't respond to source_location" do
257
+ test_host = TestHost.new
258
+ pry_eval(binding, 'show-source test_host').
259
+ should =~ /class TestHost\n.*def hello/
260
+ end
261
+
262
+ it "should output source of its class if constant doesn't respond to source_location" do
263
+ TEST_HOST = TestHost.new
264
+ pry_eval(binding, 'show-source TEST_HOST').
265
+ should =~ /class TestHost\n.*def hello/
266
+ Object.remove_const(:TEST_HOST)
267
+ end
268
+ end
269
+
270
+ describe "on modules" do
271
+ before do
272
+ class ShowSourceTestSuperClass
273
+ def alpha
274
+ end
275
+ end
276
+
277
+ class ShowSourceTestClass<ShowSourceTestSuperClass
278
+ def alpha
279
+ end
280
+ end
281
+
282
+ module ShowSourceTestSuperModule
283
+ def alpha
284
+ end
285
+ end
286
+
287
+ module ShowSourceTestModule
288
+ include ShowSourceTestSuperModule
289
+ def alpha
290
+ end
291
+ end
292
+
293
+ ShowSourceTestClassWeirdSyntax = Class.new do
294
+ def beta
295
+ end
296
+ end
297
+
298
+ ShowSourceTestModuleWeirdSyntax = Module.new do
299
+ def beta
300
+ end
301
+ end
302
+ end
303
+
304
+ after do
305
+ Object.remove_const :ShowSourceTestSuperClass
306
+ Object.remove_const :ShowSourceTestClass
307
+ Object.remove_const :ShowSourceTestClassWeirdSyntax
308
+ Object.remove_const :ShowSourceTestSuperModule
309
+ Object.remove_const :ShowSourceTestModule
310
+ Object.remove_const :ShowSourceTestModuleWeirdSyntax
311
+ end
312
+
313
+ describe "basic functionality, should find top-level module definitions" do
314
+ it 'should show source for a class' do
315
+ pry_eval('show-source ShowSourceTestClass').
316
+ should =~ /class ShowSourceTestClass.*?def alpha/m
317
+ end
318
+
319
+ it 'should show source for a super class' do
320
+ pry_eval('show-source -s ShowSourceTestClass').
321
+ should =~ /class ShowSourceTestSuperClass.*?def alpha/m
322
+ end
323
+
324
+ it 'should show source for a module' do
325
+ pry_eval('show-source ShowSourceTestModule').
326
+ should =~ /module ShowSourceTestModule/
327
+ end
328
+
329
+ it 'should show source for an ancestor module' do
330
+ pry_eval('show-source -s ShowSourceTestModule').
331
+ should =~ /module ShowSourceTestSuperModule/
332
+ end
333
+
334
+ it 'should show source for a class when Const = Class.new syntax is used' do
335
+ pry_eval('show-source ShowSourceTestClassWeirdSyntax').
336
+ should =~ /ShowSourceTestClassWeirdSyntax = Class.new/
337
+ end
338
+
339
+ it 'should show source for a super class when Const = Class.new syntax is used' do
340
+ pry_eval('show-source -s ShowSourceTestClassWeirdSyntax').
341
+ should =~ /class Object/
342
+ end
343
+
344
+ it 'should show source for a module when Const = Module.new syntax is used' do
345
+ pry_eval('show-source ShowSourceTestModuleWeirdSyntax').
346
+ should =~ /ShowSourceTestModuleWeirdSyntax = Module.new/
347
+ end
348
+ end
349
+
350
+ if !Pry::Helpers::BaseHelpers.mri_18?
351
+ before do
352
+ pry_eval unindent(<<-EOS)
353
+ class Dog
354
+ def woof
355
+ end
356
+ end
357
+
358
+ class TobinaMyDog < Dog
359
+ def woof
360
+ end
361
+ end
362
+ EOS
363
+ end
364
+
365
+ after do
366
+ Object.remove_const :Dog
367
+ Object.remove_const :TobinaMyDog
368
+ end
369
+
370
+ describe "in REPL" do
371
+ it 'should find class defined in repl' do
372
+ pry_eval('show-source TobinaMyDog').should =~ /class TobinaMyDog/
373
+ end
374
+ it 'should find superclass defined in repl' do
375
+ pry_eval('show-source -s TobinaMyDog').should =~ /class Dog/
376
+ end
377
+ end
378
+ end
379
+
380
+ it 'should lookup module name with respect to current context' do
381
+
382
+ constant_scope(:AlphaClass, :BetaClass) do
383
+ class BetaClass
384
+ def alpha
385
+ end
386
+ end
387
+
388
+ class AlphaClass
389
+ class BetaClass
390
+ def beta
391
+ end
392
+ end
393
+ end
394
+
395
+ pry_eval(AlphaClass, 'show-source BetaClass').should =~ /def beta/
396
+ end
397
+ end
398
+
399
+ it 'should lookup nested modules' do
400
+ constant_scope(:AlphaClass) do
401
+ class AlphaClass
402
+ class BetaClass
403
+ def beta
404
+ end
405
+ end
406
+ end
407
+
408
+ pry_eval('show-source AlphaClass::BetaClass').should =~ /class Beta/
409
+ end
410
+ end
411
+
412
+ # note that pry assumes a class is only monkey-patched at most
413
+ # ONCE per file, so will not find multiple monkeypatches in the
414
+ # SAME file.
415
+ describe "show-source -a" do
416
+ it 'should show the source for all monkeypatches defined in different files' do
417
+ class TestClassForShowSource
418
+ def beta
419
+ end
420
+ end
421
+
422
+ result = pry_eval('show-source TestClassForShowSource -a')
423
+ result.should =~ /def alpha/
424
+ result.should =~ /def beta/
425
+ end
426
+
427
+ it 'should show the source for a class_eval-based monkeypatch' do
428
+ TestClassForShowSourceClassEval.class_eval do
429
+ def class_eval_method
430
+ end
431
+ end
432
+
433
+ result = pry_eval('show-source TestClassForShowSourceClassEval -a')
434
+ result.should =~ /def class_eval_method/
435
+ end
436
+
437
+ it 'should ignore -a when object is not a module' do
438
+ TestClassForShowSourceClassEval.class_eval do
439
+ def class_eval_method
440
+ :bing
441
+ end
442
+ end
443
+
444
+ result = pry_eval('show-source TestClassForShowSourceClassEval#class_eval_method -a')
445
+ result.should =~ /bing/
446
+ end
447
+
448
+ it 'should show the source for an instance_eval-based monkeypatch' do
449
+ TestClassForShowSourceInstanceEval.instance_eval do
450
+ def instance_eval_method
451
+ end
452
+ end
453
+
454
+ result = pry_eval('show-source TestClassForShowSourceInstanceEval -a')
455
+ result.should =~ /def instance_eval_method/
456
+ end
457
+ end
458
+
459
+ describe "when show-source is invoked without a method or class argument" do
460
+ before do
461
+ module TestHost
462
+ class M
463
+ def alpha; end
464
+ def beta; end
465
+ end
466
+
467
+ module C
468
+ end
469
+
470
+ module D
471
+ def self.invoked_in_method
472
+ pry_eval(binding, 'show-source')
473
+ end
474
+ end
475
+ end
476
+ end
477
+
478
+ after do
479
+ Object.remove_const(:TestHost)
480
+ end
481
+
482
+ describe "inside a module" do
483
+ it 'should display module source by default' do
484
+ out = pry_eval(TestHost::M, 'show-source')
485
+ out.should =~ /class M/
486
+ out.should =~ /def alpha/
487
+ out.should =~ /def beta/
488
+ end
489
+
490
+ it 'should be unable to find module source if no methods defined' do
491
+ proc {
492
+ pry_eval(TestHost::C, 'show-source')
493
+ }.should.raise(Pry::CommandError).
494
+ message.should =~ /Cannot find a definition for/
495
+ end
496
+
497
+ it 'should display method code (rather than class) if Pry started inside method binding' do
498
+ out = TestHost::D.invoked_in_method
499
+ out.should =~ /invoked_in_method/
500
+ out.should.not =~ /module D/
501
+ end
502
+
503
+ it 'should display class source when inside instance' do
504
+ out = pry_eval(TestHost::M.new, 'show-source')
505
+ out.should =~ /class M/
506
+ out.should =~ /def alpha/
507
+ out.should =~ /def beta/
508
+ end
509
+
510
+ it 'should allow options to be passed' do
511
+ out = pry_eval(TestHost::M, 'show-source -b')
512
+ out.should =~ /\d:\s*class M/
513
+ out.should =~ /\d:\s*def alpha/
514
+ out.should =~ /\d:\s*def beta/
515
+ end
516
+
517
+ describe "should skip over broken modules" do
518
+ before do
519
+ module BabyDuck
520
+
521
+ module Muesli
522
+ binding.eval("def a; end", "dummy.rb", 1)
523
+ binding.eval("def b; end", "dummy.rb", 2)
524
+ binding.eval("def c; end", "dummy.rb", 3)
525
+ end
526
+
527
+ module Muesli
528
+ def d; end
529
+ def e; end
530
+ end
531
+ end
532
+ end
533
+
534
+ after do
535
+ Object.remove_const(:BabyDuck)
536
+ end
537
+
538
+ # TODO: !!! This is a bad spec, should not be expected behaviour?!?!
539
+ #
540
+ # it 'should return source for first valid module' do
541
+ # out = pry_eval('show-source BabyDuck::Muesli')
542
+ # out.should =~ /def d; end/
543
+ # out.should.not =~ /def a; end/
544
+ # end
545
+
546
+ end
547
+ end
548
+ end
549
+ end
550
+
551
+ describe "on commands" do
552
+ before do
553
+ @oldset = Pry.config.commands
554
+ @set = Pry.config.commands = Pry::CommandSet.new do
555
+ import Pry::Commands
556
+ end
557
+ end
558
+
559
+ after do
560
+ Pry.config.commands = @oldset
561
+ end
562
+
563
+ it 'should show source for an ordinary command' do
564
+ @set.command "foo", :body_of_foo do; end
565
+
566
+ pry_eval('show-source foo').should =~ /:body_of_foo/
567
+ end
568
+
569
+ it "should output source of commands using special characters" do
570
+ @set.command "!%$", "I gots the yellow fever" do; end
571
+
572
+ pry_eval('show-source !%$').should =~ /yellow fever/
573
+ end
574
+
575
+ it 'should show source for a command with spaces in its name' do
576
+ @set.command "foo bar", :body_of_foo_bar do; end
577
+
578
+ pry_eval('show-source foo bar').should =~ /:body_of_foo_bar/
579
+ end
580
+
581
+ it 'should show source for a command by listing name' do
582
+ @set.command /foo(.*)/, :body_of_foo_bar_regex, :listing => "bar" do; end
583
+
584
+ pry_eval('show-source bar').should =~ /:body_of_foo_bar_regex/
585
+ end
586
+ end
587
+
588
+ describe "should set _file_ and _dir_" do
589
+ it 'should set _file_ and _dir_ to file containing method source' do
590
+ t = pry_tester
591
+ t.process_command "show-source TestClassForShowSource#alpha"
592
+ t.pry.last_file.should =~ /show_source_doc_examples/
593
+ t.pry.last_dir.should =~ /fixtures/
594
+ end
595
+ end
596
+ end
597
+ end