pry 0.9.12.2 → 0.14.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (237) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +1141 -0
  3. data/LICENSE +2 -2
  4. data/README.md +466 -0
  5. data/bin/pry +4 -7
  6. data/lib/pry/basic_object.rb +10 -0
  7. data/lib/pry/block_command.rb +22 -0
  8. data/lib/pry/class_command.rb +194 -0
  9. data/lib/pry/cli.rb +97 -92
  10. data/lib/pry/code/code_file.rb +114 -0
  11. data/lib/pry/code/code_range.rb +7 -4
  12. data/lib/pry/code/loc.rb +27 -14
  13. data/lib/pry/code.rb +62 -90
  14. data/lib/pry/code_object.rb +83 -39
  15. data/lib/pry/color_printer.rb +66 -0
  16. data/lib/pry/command.rb +202 -371
  17. data/lib/pry/command_set.rb +151 -133
  18. data/lib/pry/command_state.rb +31 -0
  19. data/lib/pry/commands/amend_line.rb +86 -82
  20. data/lib/pry/commands/bang.rb +18 -14
  21. data/lib/pry/commands/bang_pry.rb +15 -11
  22. data/lib/pry/commands/cat/abstract_formatter.rb +23 -18
  23. data/lib/pry/commands/cat/exception_formatter.rb +85 -73
  24. data/lib/pry/commands/cat/file_formatter.rb +56 -63
  25. data/lib/pry/commands/cat/input_expression_formatter.rb +35 -30
  26. data/lib/pry/commands/cat.rb +64 -47
  27. data/lib/pry/commands/cd.rb +42 -26
  28. data/lib/pry/commands/change_inspector.rb +34 -0
  29. data/lib/pry/commands/change_prompt.rb +51 -0
  30. data/lib/pry/commands/clear_screen.rb +20 -0
  31. data/lib/pry/commands/code_collector.rb +148 -131
  32. data/lib/pry/commands/disable_pry.rb +23 -19
  33. data/lib/pry/commands/easter_eggs.rb +23 -34
  34. data/lib/pry/commands/edit/exception_patcher.rb +21 -17
  35. data/lib/pry/commands/edit/file_and_line_locator.rb +33 -24
  36. data/lib/pry/commands/edit.rb +183 -167
  37. data/lib/pry/commands/exit.rb +40 -35
  38. data/lib/pry/commands/exit_all.rb +24 -20
  39. data/lib/pry/commands/exit_program.rb +20 -17
  40. data/lib/pry/commands/find_method.rb +167 -167
  41. data/lib/pry/commands/fix_indent.rb +16 -12
  42. data/lib/pry/commands/help.rb +140 -133
  43. data/lib/pry/commands/hist.rb +153 -132
  44. data/lib/pry/commands/import_set.rb +20 -15
  45. data/lib/pry/commands/jump_to.rb +25 -21
  46. data/lib/pry/commands/list_inspectors.rb +42 -0
  47. data/lib/pry/commands/ls/constants.rb +75 -0
  48. data/lib/pry/commands/ls/formatter.rb +55 -0
  49. data/lib/pry/commands/ls/globals.rb +50 -0
  50. data/lib/pry/commands/ls/grep.rb +23 -0
  51. data/lib/pry/commands/ls/instance_vars.rb +40 -0
  52. data/lib/pry/commands/ls/interrogatable.rb +24 -0
  53. data/lib/pry/commands/ls/jruby_hacks.rb +55 -0
  54. data/lib/pry/commands/ls/local_names.rb +37 -0
  55. data/lib/pry/commands/ls/local_vars.rb +47 -0
  56. data/lib/pry/commands/ls/ls_entity.rb +65 -0
  57. data/lib/pry/commands/ls/methods.rb +55 -0
  58. data/lib/pry/commands/ls/methods_helper.rb +50 -0
  59. data/lib/pry/commands/ls/self_methods.rb +34 -0
  60. data/lib/pry/commands/ls.rb +100 -303
  61. data/lib/pry/commands/nesting.rb +21 -17
  62. data/lib/pry/commands/play.rb +93 -49
  63. data/lib/pry/commands/pry_backtrace.rb +22 -18
  64. data/lib/pry/commands/pry_version.rb +15 -11
  65. data/lib/pry/commands/raise_up.rb +33 -27
  66. data/lib/pry/commands/reload_code.rb +57 -48
  67. data/lib/pry/commands/reset.rb +16 -12
  68. data/lib/pry/commands/ri.rb +57 -38
  69. data/lib/pry/commands/save_file.rb +45 -43
  70. data/lib/pry/commands/shell_command.rb +66 -34
  71. data/lib/pry/commands/shell_mode.rb +22 -20
  72. data/lib/pry/commands/show_doc.rb +80 -65
  73. data/lib/pry/commands/show_info.rb +193 -159
  74. data/lib/pry/commands/show_input.rb +16 -11
  75. data/lib/pry/commands/show_source.rb +113 -33
  76. data/lib/pry/commands/stat.rb +35 -31
  77. data/lib/pry/commands/switch_to.rb +21 -15
  78. data/lib/pry/commands/toggle_color.rb +21 -13
  79. data/lib/pry/commands/watch_expression/expression.rb +43 -0
  80. data/lib/pry/commands/watch_expression.rb +110 -0
  81. data/lib/pry/commands/whereami.rb +157 -134
  82. data/lib/pry/commands/wtf.rb +78 -40
  83. data/lib/pry/config/attributable.rb +22 -0
  84. data/lib/pry/config/lazy_value.rb +29 -0
  85. data/lib/pry/config/memoized_value.rb +34 -0
  86. data/lib/pry/config/value.rb +24 -0
  87. data/lib/pry/config.rb +290 -220
  88. data/lib/pry/control_d_handler.rb +28 -0
  89. data/lib/pry/core_extensions.rb +50 -27
  90. data/lib/pry/editor.rb +130 -102
  91. data/lib/pry/env.rb +18 -0
  92. data/lib/pry/exception_handler.rb +43 -0
  93. data/lib/pry/exceptions.rb +73 -0
  94. data/lib/pry/forwardable.rb +27 -0
  95. data/lib/pry/helpers/base_helpers.rb +22 -151
  96. data/lib/pry/helpers/command_helpers.rb +55 -63
  97. data/lib/pry/helpers/documentation_helpers.rb +21 -13
  98. data/lib/pry/helpers/options_helpers.rb +15 -8
  99. data/lib/pry/helpers/platform.rb +55 -0
  100. data/lib/pry/helpers/table.rb +44 -32
  101. data/lib/pry/helpers/text.rb +96 -86
  102. data/lib/pry/helpers.rb +3 -0
  103. data/lib/pry/history.rb +101 -70
  104. data/lib/pry/hooks.rb +67 -137
  105. data/lib/pry/indent.rb +79 -73
  106. data/lib/pry/input_completer.rb +283 -0
  107. data/lib/pry/input_lock.rb +129 -0
  108. data/lib/pry/inspector.rb +39 -0
  109. data/lib/pry/last_exception.rb +61 -0
  110. data/lib/pry/method/disowned.rb +19 -5
  111. data/lib/pry/{commands/edit/method_patcher.rb → method/patcher.rb} +51 -42
  112. data/lib/pry/method/weird_method_locator.rb +80 -44
  113. data/lib/pry/method.rb +225 -176
  114. data/lib/pry/object_path.rb +91 -0
  115. data/lib/pry/output.rb +136 -0
  116. data/lib/pry/pager.rb +227 -68
  117. data/lib/pry/prompt.rb +214 -0
  118. data/lib/pry/pry_class.rb +216 -289
  119. data/lib/pry/pry_instance.rb +438 -500
  120. data/lib/pry/repl.rb +256 -0
  121. data/lib/pry/repl_file_loader.rb +34 -35
  122. data/lib/pry/ring.rb +89 -0
  123. data/lib/pry/slop/LICENSE +20 -0
  124. data/lib/pry/slop/commands.rb +190 -0
  125. data/lib/pry/slop/option.rb +210 -0
  126. data/lib/pry/slop.rb +672 -0
  127. data/lib/pry/syntax_highlighter.rb +26 -0
  128. data/lib/pry/system_command_handler.rb +17 -0
  129. data/lib/pry/testable/evalable.rb +24 -0
  130. data/lib/pry/testable/mockable.rb +22 -0
  131. data/lib/pry/testable/pry_tester.rb +88 -0
  132. data/lib/pry/testable/utility.rb +34 -0
  133. data/lib/pry/testable/variables.rb +52 -0
  134. data/lib/pry/testable.rb +68 -0
  135. data/lib/pry/version.rb +3 -1
  136. data/lib/pry/warning.rb +20 -0
  137. data/lib/pry/{module_candidate.rb → wrapped_module/candidate.rb} +36 -43
  138. data/lib/pry/wrapped_module.rb +102 -103
  139. data/lib/pry.rb +135 -261
  140. metadata +94 -283
  141. data/.document +0 -2
  142. data/.gitignore +0 -16
  143. data/.travis.yml +0 -21
  144. data/.yardopts +0 -1
  145. data/CHANGELOG +0 -534
  146. data/CONTRIBUTORS +0 -55
  147. data/Gemfile +0 -9
  148. data/Guardfile +0 -62
  149. data/README.markdown +0 -400
  150. data/Rakefile +0 -140
  151. data/TODO +0 -117
  152. data/lib/pry/commands/disabled_commands.rb +0 -2
  153. data/lib/pry/commands/gem_cd.rb +0 -26
  154. data/lib/pry/commands/gem_install.rb +0 -29
  155. data/lib/pry/commands/gem_list.rb +0 -33
  156. data/lib/pry/commands/gem_open.rb +0 -29
  157. data/lib/pry/commands/gist.rb +0 -102
  158. data/lib/pry/commands/install_command.rb +0 -51
  159. data/lib/pry/commands/simple_prompt.rb +0 -22
  160. data/lib/pry/commands.rb +0 -6
  161. data/lib/pry/completion.rb +0 -304
  162. data/lib/pry/custom_completions.rb +0 -6
  163. data/lib/pry/history_array.rb +0 -116
  164. data/lib/pry/plugins.rb +0 -103
  165. data/lib/pry/rbx_method.rb +0 -13
  166. data/lib/pry/rbx_path.rb +0 -22
  167. data/lib/pry/rubygem.rb +0 -74
  168. data/lib/pry/terminal.rb +0 -78
  169. data/lib/pry/test/helper.rb +0 -185
  170. data/man/pry.1 +0 -195
  171. data/man/pry.1.html +0 -204
  172. data/man/pry.1.ronn +0 -141
  173. data/pry.gemspec +0 -30
  174. data/spec/Procfile +0 -3
  175. data/spec/cli_spec.rb +0 -78
  176. data/spec/code_object_spec.rb +0 -277
  177. data/spec/code_spec.rb +0 -219
  178. data/spec/command_helpers_spec.rb +0 -29
  179. data/spec/command_integration_spec.rb +0 -644
  180. data/spec/command_set_spec.rb +0 -627
  181. data/spec/command_spec.rb +0 -821
  182. data/spec/commands/amend_line_spec.rb +0 -247
  183. data/spec/commands/bang_spec.rb +0 -19
  184. data/spec/commands/cat_spec.rb +0 -164
  185. data/spec/commands/cd_spec.rb +0 -250
  186. data/spec/commands/disable_pry_spec.rb +0 -25
  187. data/spec/commands/edit_spec.rb +0 -727
  188. data/spec/commands/exit_all_spec.rb +0 -34
  189. data/spec/commands/exit_program_spec.rb +0 -19
  190. data/spec/commands/exit_spec.rb +0 -34
  191. data/spec/commands/find_method_spec.rb +0 -70
  192. data/spec/commands/gem_list_spec.rb +0 -26
  193. data/spec/commands/gist_spec.rb +0 -79
  194. data/spec/commands/help_spec.rb +0 -56
  195. data/spec/commands/hist_spec.rb +0 -181
  196. data/spec/commands/jump_to_spec.rb +0 -15
  197. data/spec/commands/ls_spec.rb +0 -181
  198. data/spec/commands/play_spec.rb +0 -140
  199. data/spec/commands/raise_up_spec.rb +0 -56
  200. data/spec/commands/save_file_spec.rb +0 -177
  201. data/spec/commands/show_doc_spec.rb +0 -510
  202. data/spec/commands/show_input_spec.rb +0 -17
  203. data/spec/commands/show_source_spec.rb +0 -782
  204. data/spec/commands/whereami_spec.rb +0 -203
  205. data/spec/completion_spec.rb +0 -239
  206. data/spec/control_d_handler_spec.rb +0 -58
  207. data/spec/documentation_helper_spec.rb +0 -73
  208. data/spec/editor_spec.rb +0 -79
  209. data/spec/exception_whitelist_spec.rb +0 -21
  210. data/spec/fixtures/candidate_helper1.rb +0 -11
  211. data/spec/fixtures/candidate_helper2.rb +0 -8
  212. data/spec/fixtures/example.erb +0 -5
  213. data/spec/fixtures/example_nesting.rb +0 -33
  214. data/spec/fixtures/show_source_doc_examples.rb +0 -15
  215. data/spec/fixtures/testrc +0 -2
  216. data/spec/fixtures/testrcbad +0 -2
  217. data/spec/fixtures/whereami_helper.rb +0 -6
  218. data/spec/helper.rb +0 -34
  219. data/spec/helpers/bacon.rb +0 -86
  220. data/spec/helpers/mock_pry.rb +0 -43
  221. data/spec/helpers/table_spec.rb +0 -105
  222. data/spec/history_array_spec.rb +0 -67
  223. data/spec/hooks_spec.rb +0 -522
  224. data/spec/indent_spec.rb +0 -301
  225. data/spec/input_stack_spec.rb +0 -90
  226. data/spec/method_spec.rb +0 -482
  227. data/spec/prompt_spec.rb +0 -60
  228. data/spec/pry_defaults_spec.rb +0 -419
  229. data/spec/pry_history_spec.rb +0 -99
  230. data/spec/pry_output_spec.rb +0 -95
  231. data/spec/pry_spec.rb +0 -504
  232. data/spec/run_command_spec.rb +0 -25
  233. data/spec/sticky_locals_spec.rb +0 -157
  234. data/spec/syntax_checking_spec.rb +0 -81
  235. data/spec/wrapped_module_spec.rb +0 -261
  236. data/wiki/Customizing-pry.md +0 -397
  237. data/wiki/Home.md +0 -4
@@ -1,727 +0,0 @@
1
- require 'pathname'
2
- require 'helper'
3
-
4
- describe "edit" do
5
- before do
6
- @old_editor = Pry.config.editor
7
- @file = @line = @contents = nil
8
- Pry.config.editor = lambda do |file, line|
9
- @file = file; @line = line; @contents = File.read(@file)
10
- nil
11
- end
12
- end
13
-
14
- after do
15
- Pry.config.editor = @old_editor
16
- end
17
-
18
- describe "with FILE" do
19
-
20
- before do
21
- # OS-specific tempdir name. For GNU/Linux it's "tmp", for Windows it's
22
- # something "Temp".
23
- @tf_dir =
24
- if Pry::Helpers::BaseHelpers.mri_19?
25
- Pathname.new(Dir::Tmpname.tmpdir)
26
- else
27
- Pathname.new(Dir.tmpdir)
28
- end
29
-
30
- @tf_path = File.expand_path(File.join(@tf_dir.to_s, 'bar.rb'))
31
- FileUtils.touch(@tf_path)
32
- end
33
-
34
- after do
35
- FileUtils.rm(@tf_path) if File.exists?(@tf_path)
36
- end
37
-
38
- it "should invoke Pry.config.editor with absolutified filenames" do
39
- pry_eval 'edit lib/pry.rb'
40
- @file.should == File.expand_path('lib/pry.rb')
41
-
42
- pry_eval "edit #@tf_path"
43
- @file.should == @tf_path
44
- end
45
-
46
- it "should guess the line number from a colon" do
47
- pry_eval 'edit lib/pry.rb:10'
48
- @line.should == 10
49
- end
50
-
51
- it "should use the line number from -l" do
52
- pry_eval 'edit -l 10 lib/pry.rb'
53
- @line.should == 10
54
- end
55
-
56
- it "should not delete the file!" do
57
- pry_eval 'edit Rakefile'
58
- File.exist?(@file).should == true
59
- end
60
-
61
- it "works with files that contain blanks in their names" do
62
- tf_path = File.join(File.dirname(@tf_path), 'swoop and doop.rb')
63
- FileUtils.touch(tf_path)
64
- pry_eval "edit #{ tf_path }"
65
- @file.should == tf_path
66
- FileUtils.rm(tf_path)
67
- end
68
-
69
- describe do
70
- before do
71
- Pad.counter = 0
72
- Pry.config.editor = lambda { |file, line|
73
- File.open(file, 'w') { |f| f << "Pad.counter = Pad.counter + 1" }
74
- nil
75
- }
76
- end
77
-
78
- it "should reload the file if it is a ruby file" do
79
- temp_file do |tf|
80
- counter = Pad.counter
81
- path = tf.path
82
-
83
- pry_eval "edit #{path}"
84
-
85
- Pad.counter.should == counter + 1
86
- end
87
- end
88
-
89
- it "should not reload the file if it is not a ruby file" do
90
- temp_file('.py') do |tf|
91
- counter = Pad.counter
92
- path = tf.path
93
-
94
- pry_eval "edit #{path}"
95
-
96
- Pad.counter.should == counter
97
- end
98
- end
99
-
100
- it "should not reload a ruby file if -n is given" do
101
- temp_file do |tf|
102
- counter = Pad.counter
103
- path = tf.path
104
-
105
- Pad.counter.should == counter
106
- end
107
- end
108
-
109
- it "should reload a non-ruby file if -r is given" do
110
- temp_file('.pryrc') do |tf|
111
- counter = Pad.counter
112
- path = tf.path
113
-
114
- pry_eval "edit -r #{path}"
115
-
116
- Pad.counter.should == counter + 1
117
- end
118
- end
119
- end
120
-
121
- describe do
122
- before do
123
- @reloading = nil
124
- Pry.config.editor = lambda do |file, line, reloading|
125
- @file = file; @line = line; @reloading = reloading
126
- nil
127
- end
128
- end
129
-
130
- it "should pass the editor a reloading arg" do
131
- pry_eval 'edit lib/pry.rb'
132
- @reloading.should == true
133
- pry_eval 'edit -n lib/pry.rb'
134
- @reloading.should == false
135
- end
136
- end
137
- end
138
-
139
- describe "with --ex" do
140
- before do
141
- @t = pry_tester do
142
- def last_exception=(exception)
143
- @pry.last_exception = exception
144
- end
145
- def last_exception; @pry.last_exception; end
146
- end
147
- end
148
-
149
- describe "with a real file" do
150
- before do
151
- @tf = Tempfile.new(["pry", ".rb"])
152
- @path = @tf.path
153
- @tf << "1\n2\nraise RuntimeError"
154
- @tf.flush
155
-
156
- begin
157
- load @path
158
- rescue RuntimeError => e
159
- @t.last_exception = e
160
- end
161
- end
162
-
163
- after do
164
- @tf.close(true)
165
- File.unlink("#{@path}c") if File.exists?("#{@path}c") #rbx
166
- end
167
-
168
- it "should reload the file" do
169
- Pry.config.editor = lambda {|file, line|
170
- File.open(file, 'w'){|f| f << "FOO = 'BAR'" }
171
- nil
172
- }
173
-
174
- defined?(FOO).should.be.nil
175
-
176
- @t.eval 'edit --ex'
177
-
178
- FOO.should == 'BAR'
179
- end
180
-
181
- # regression test (this used to edit the current method instead
182
- # of the exception)
183
- it 'edits the exception even when in a patched method context' do
184
- source_location = nil
185
- Pry.config.editor = lambda {|file, line|
186
- source_location = [file, line]
187
- nil
188
- }
189
-
190
- Pad.le = @t.last_exception
191
- redirect_pry_io(InputTester.new("def broken_method", "binding.pry", "end",
192
- "broken_method",
193
- "_pry_.last_exception = Pad.le",
194
- "edit --ex -n", "exit-all", "exit-all")) do
195
- Object.new.pry
196
- end
197
-
198
- source_location.should == [@path, 3]
199
- Pad.clear
200
- end
201
-
202
- it "should not reload the file if -n is passed" do
203
- Pry.config.editor = lambda {|file, line|
204
- File.open(file, 'w'){|f| f << "FOO2 = 'BAZ'" }
205
- nil
206
- }
207
-
208
- defined?(FOO2).should.be.nil
209
-
210
- @t.eval 'edit -n --ex'
211
-
212
- defined?(FOO2).should.be.nil
213
- end
214
-
215
- describe "with --patch" do
216
- # Original source code must be untouched.
217
- it "should apply changes only in memory (monkey patching)" do
218
- Pry.config.editor = lambda {|file, line|
219
- File.open(file, 'w'){|f| f << "FOO3 = 'PIYO'" }
220
- @patched_def = File.open(file, 'r').read
221
- nil
222
- }
223
-
224
- defined?(FOO3).should.be.nil
225
-
226
- @t.eval 'edit --ex --patch'
227
-
228
- FOO3.should == 'PIYO'
229
-
230
- @tf.rewind
231
- @tf.read.should == "1\n2\nraise RuntimeError"
232
- @patched_def.should == "FOO3 = 'PIYO'"
233
- end
234
- end
235
- end
236
-
237
- describe "with --ex NUM" do
238
- before do
239
- Pry.config.editor = proc do |file, line|
240
- @__ex_file__ = file
241
- @__ex_line__ = line
242
- nil
243
- end
244
-
245
- @t.last_exception = mock_exception('a:1', 'b:2', 'c:3')
246
- end
247
-
248
- it 'should start on first level of backtrace with just --ex' do
249
- @t.eval 'edit -n --ex'
250
- @__ex_file__.should == "a"
251
- @__ex_line__.should == 1
252
- end
253
-
254
- it 'should start editor on first level of backtrace with --ex 0' do
255
- @t.eval 'edit -n --ex 0'
256
- @__ex_file__.should == "a"
257
- @__ex_line__.should == 1
258
- end
259
-
260
- it 'should start editor on second level of backtrace with --ex 1' do
261
- @t.eval 'edit -n --ex 1'
262
- @__ex_file__.should == "b"
263
- @__ex_line__.should == 2
264
- end
265
-
266
- it 'should start editor on third level of backtrace with --ex 2' do
267
- @t.eval 'edit -n --ex 2'
268
- @__ex_file__.should == "c"
269
- @__ex_line__.should == 3
270
- end
271
-
272
- it 'should display error message when backtrace level is invalid' do
273
- proc {
274
- @t.eval 'edit -n --ex 4'
275
- }.should.raise(Pry::CommandError)
276
- end
277
- end
278
- end
279
-
280
- describe "without FILE" do
281
- before do
282
- @t = pry_tester
283
- end
284
-
285
- it "should edit the current expression if it's incomplete" do
286
- eval_str = 'def a'
287
- @t.process_command 'edit', eval_str
288
- @contents.should == "def a\n"
289
- end
290
-
291
- it "should edit the previous expression if the current is empty" do
292
- @t.eval 'def a; 2; end', 'edit'
293
- @contents.should == "def a; 2; end\n"
294
- end
295
-
296
- it "should use a blank file if -t is specified" do
297
- @t.eval 'def a; 5; end', 'edit -t'
298
- @contents.should == "\n"
299
- end
300
-
301
- it "should use a blank file if -t given, even during an expression" do
302
- eval_str = 'def a;'
303
- @t.process_command 'edit -t', eval_str
304
- @contents.should == "\n"
305
- end
306
-
307
- it "should position the cursor at the end of the expression" do
308
- eval_str = "def a; 2;\nend"
309
- @t.process_command 'edit', eval_str
310
- @line.should == 2
311
- end
312
-
313
- it "should evaluate the expression" do
314
- Pry.config.editor = lambda {|file, line|
315
- File.open(file, 'w'){|f| f << "'FOO'\n" }
316
- nil
317
- }
318
- eval_str = ''
319
- @t.process_command 'edit', eval_str
320
- eval_str.should == "'FOO'\n"
321
- end
322
-
323
- it "should not evaluate the expression with -n" do
324
- Pry.config.editor = lambda {|file, line|
325
- File.open(file, 'w'){|f| f << "'FOO'\n" }
326
- nil
327
- }
328
- eval_str = ''
329
- @t.process_command 'edit -n', eval_str
330
- eval_str.should == ''
331
- end
332
- end
333
-
334
- describe "with --in" do
335
- it "should edit the nth line of _in_" do
336
- pry_eval '10', '11', 'edit --in -2'
337
- @contents.should == "10\n"
338
- end
339
-
340
- it "should edit the last line if no argument is given" do
341
- pry_eval '10', '11', 'edit --in'
342
- @contents.should == "11\n"
343
- end
344
-
345
- it "should edit a range of lines if a range is given" do
346
- pry_eval "10", "11", "edit -i 1,2"
347
- @contents.should == "10\n11\n"
348
- end
349
-
350
- it "should edit a multi-line expression as it occupies one line of _in_" do
351
- pry_eval "class Fixnum\n def invert; -self; end\nend", "edit -i 1"
352
- @contents.should == "class Fixnum\n def invert; -self; end\nend\n"
353
- end
354
-
355
- it "should not work with a filename" do
356
- proc {
357
- pry_eval 'edit ruby.rb -i'
358
- }.should.raise(Pry::CommandError).
359
- message.should =~ /Only one of --ex, --temp, --in, --method and FILE/
360
- end
361
-
362
- it "should not work with nonsense" do
363
- proc {
364
- pry_eval 'edit --in three'
365
- }.should.raise(Pry::CommandError).
366
- message.should =~ /Not a valid range: three/
367
- end
368
- end
369
-
370
- describe "old edit-method tests now migrated to edit" do
371
- describe "on a method defined in a file" do
372
- before do
373
- @tempfile = (Tempfile.new(['pry', '.rb']))
374
- @tempfile.puts <<-EOS
375
- module A
376
- def a
377
- :yup
378
- end
379
-
380
- def b
381
- :kinda
382
- end
383
- end
384
-
385
- class X
386
- include A
387
-
388
- def self.x
389
- :double_yup
390
- end
391
-
392
- def x
393
- :nope
394
- end
395
-
396
- def b
397
- super
398
- end
399
- alias c b
400
-
401
- def y?
402
- :because
403
- end
404
-
405
- class B
406
- G = :nawt
407
-
408
- def foo
409
- :possibly
410
- G
411
- end
412
- end
413
- end
414
- EOS
415
- @tempfile.flush
416
- load @tempfile.path
417
-
418
- @tempfile_path = @tempfile.path
419
- end
420
-
421
- after do
422
- @tempfile.close(true)
423
- end
424
-
425
- describe 'without -p' do
426
- before do
427
- @file = @line = @contents = nil
428
- Pry.config.editor = lambda do |file, line|
429
- @file = file; @line = line
430
- nil
431
- end
432
- end
433
-
434
- it "should correctly find a class method" do
435
- pry_eval 'edit X.x'
436
-
437
- @file.should == @tempfile_path
438
- @line.should == 14
439
-
440
- end
441
-
442
- it "should correctly find an instance method" do
443
- pry_eval 'edit X#x'
444
- @file.should == @tempfile_path
445
- @line.should == 18
446
- end
447
-
448
- it "should correctly find a method on an instance" do
449
- pry_eval 'x = X.new', 'edit x.x'
450
- @file.should == @tempfile_path
451
- @line.should == 18
452
- end
453
-
454
- it "should correctly find a method from a module" do
455
- pry_eval 'edit X#a'
456
- @file.should == @tempfile_path
457
- @line.should == 2
458
- end
459
-
460
- it "should correctly find an aliased method" do
461
- pry_eval 'edit X#c'
462
- @file.should == @tempfile_path
463
- @line.should == 22
464
- end
465
- end
466
-
467
- describe 'with -p' do
468
- before do
469
- Pry.config.editor = lambda do |file, line|
470
- lines = File.read(file).lines.to_a
471
- lines[1] = ":maybe\n"
472
- File.open(file, 'w') do |f|
473
- f.write(lines.join)
474
- end
475
- @patched_def = String(lines[1]).chomp
476
- nil
477
- end
478
- end
479
-
480
- it "should successfully replace a class method" do
481
- pry_eval 'edit -p X.x'
482
-
483
- class << X
484
- X.method(:x).owner.should == self
485
- end
486
- X.method(:x).receiver.should == X
487
- X.x.should == :maybe
488
- end
489
-
490
- it "should successfully replace an instance method" do
491
- pry_eval 'edit -p X#x'
492
-
493
- X.instance_method(:x).owner.should == X
494
- X.new.x.should == :maybe
495
- end
496
-
497
- it "should successfully replace a method on an instance" do
498
- pry_eval 'instance = X.new', 'edit -p instance.x'
499
-
500
- instance = X.new
501
- instance.method(:x).owner.should == X
502
- instance.x.should == :maybe
503
- end
504
-
505
- it "should successfully replace a method from a module" do
506
- pry_eval 'edit -p X#a'
507
-
508
- X.instance_method(:a).owner.should == A
509
- X.new.a.should == :maybe
510
- end
511
-
512
- it "should successfully replace a method with a question mark" do
513
- pry_eval 'edit -p X#y?'
514
-
515
- X.instance_method(:y?).owner.should == X
516
- X.new.y?.should == :maybe
517
- end
518
-
519
- it "should preserve module nesting" do
520
- pry_eval 'edit -p X::B#foo'
521
-
522
- X::B.instance_method(:foo).owner.should == X::B
523
- X::B.new.foo.should == :nawt
524
- end
525
-
526
- describe "monkey-patching" do
527
- before do
528
- @edit = 'edit --patch ' # A shortcut.
529
- end
530
-
531
- # @param [Integer] lineno
532
- # @return [String] the stripped line from the tempfile at +lineno+
533
- def stripped_line_at(lineno)
534
- @tempfile.rewind
535
- @tempfile.lines.to_a[lineno].strip
536
- end
537
-
538
- # Applies the monkey patch for +method+ with help of evaluation of
539
- # +eval_strs+. The idea is to capture the initial line number (before
540
- # the monkey patch), because it gets overwritten by the line number from
541
- # the monkey patch. And our goal is to check that the original
542
- # definition hasn't changed.
543
- # @param [UnboundMethod] method
544
- # @param [Array<String>] eval_strs
545
- # @return [Array<String] the lines with definitions of the same line
546
- # before monkey patching and after (normally, they should be equal)
547
- def apply_monkey_patch(method, *eval_strs)
548
- _, lineno = method.source_location
549
- definition_before = stripped_line_at(lineno)
550
-
551
- pry_eval(*eval_strs)
552
-
553
- definition_after = stripped_line_at(lineno)
554
-
555
- [definition_before, definition_after]
556
- end
557
-
558
- it "should work for a class method" do
559
- def_before, def_after =
560
- apply_monkey_patch(X.method(:x), "#@edit X.x")
561
-
562
- def_before.should == ':double_yup'
563
- def_after.should == ':double_yup'
564
- @patched_def.should == ':maybe'
565
- end
566
-
567
- it "should work for an instance method" do
568
- def_before, def_after =
569
- apply_monkey_patch(X.instance_method(:x), "#@edit X#x")
570
-
571
- def_before.should == ':nope'
572
- def_after.should == ':nope'
573
- @patched_def.should == ':maybe'
574
- end
575
-
576
- it "should work for a method on an instance" do
577
- def_before, def_after =
578
- apply_monkey_patch(X.instance_method(:x), 'instance = X.new', "#@edit instance.x")
579
-
580
- def_before.should == ':nope'
581
- def_after.should == ':nope'
582
- @patched_def.should == ':maybe'
583
- end
584
-
585
- it "should work for a method from a module" do
586
- def_before, def_after =
587
- apply_monkey_patch(X.instance_method(:a), "#@edit X#a")
588
-
589
- def_before.should == ':yup'
590
- def_after.should == ':yup'
591
- @patched_def.should == ':maybe'
592
- end
593
-
594
- it "should work for a method with a question mark" do
595
- def_before, def_after =
596
- apply_monkey_patch(X.instance_method(:y?), "#@edit X#y?")
597
-
598
- def_before.should == ':because'
599
- def_after.should == ':because'
600
- @patched_def.should == ':maybe'
601
- end
602
-
603
- it "should work with nesting" do
604
- def_before, def_after =
605
- apply_monkey_patch(X::B.instance_method(:foo), "#@edit X::B#foo")
606
-
607
- def_before.should == ':possibly'
608
- def_after.should == ':possibly'
609
- @patched_def.should == ':maybe'
610
- end
611
- end
612
- end
613
-
614
- describe 'on an aliased method' do
615
- before do
616
- Pry.config.editor = lambda do |file, line|
617
- lines = File.read(file).lines.to_a
618
- lines[1] = '"#{super}aa".to_sym' + "\n"
619
- File.open(file, 'w') do |f|
620
- f.write(lines.join)
621
- end
622
- nil
623
- end
624
- end
625
-
626
- it "should change the alias, but not the original, without breaking super" do
627
-
628
- $x = :bebe
629
- pry_eval 'edit -p X#c'
630
-
631
-
632
- Pry::Method.from_str("X#c").alias?.should == true
633
-
634
- X.new.b.should == :kinda
635
- X.new.c.should == :kindaaa
636
- $x = nil
637
- end
638
- end
639
-
640
- describe 'with three-arg editor' do
641
- before do
642
- @file = @line = @reloading = nil
643
- Pry.config.editor = lambda do |file, line, reloading|
644
- @file = file; @line = line; @reloading = reloading
645
- nil
646
- end
647
- end
648
-
649
- it "should pass the editor a reloading arg" do
650
- pry_eval 'edit X.x'
651
- @reloading.should == true
652
- pry_eval 'edit -n X.x'
653
- @reloading.should == false
654
- end
655
- end
656
- end
657
- end
658
-
659
- describe "--method flag" do
660
- before do
661
- @t = pry_tester
662
- class BinkyWink
663
- eval %{
664
- def tits_macgee
665
- binding
666
- end
667
- }
668
-
669
- def tots_macgee
670
- :jeremy_jones
671
- binding
672
- end
673
- end
674
- end
675
-
676
- after do
677
- Object.remove_const(:BinkyWink)
678
- end
679
-
680
- it 'should edit method context' do
681
- Pry.editor = lambda do |file, line|
682
- [file, line].should == BinkyWink.instance_method(:tots_macgee).source_location
683
- nil
684
- end
685
-
686
- t = pry_tester(BinkyWink.new.tots_macgee)
687
- t.process_command "edit -m -n"
688
- end
689
-
690
- it 'errors when cannot find method context' do
691
- Pry.editor = lambda do |file, line|
692
- [file, line].should == BinkyWink.instance_method(:tits_macgee).source_location
693
- nil
694
- end
695
-
696
- t = pry_tester(BinkyWink.new.tits_macgee)
697
- lambda { t.process_command "edit -m -n" }.should.
698
- raise(Pry::CommandError).message.should.match(/Cannot find a file for/)
699
- end
700
-
701
- it 'errors when a filename arg is passed with --method' do
702
- lambda { @t.process_command "edit -m Pry#repl" }.should.
703
- raise(Pry::CommandError).message.should.match(/Only one of/)
704
- end
705
- end
706
-
707
- describe "pretty error messages" do
708
- before do
709
- @t = pry_tester
710
- class TrinkyDink
711
- eval %{
712
- def claudia_linklater
713
- end
714
- }
715
- end
716
- end
717
-
718
- after do
719
- Object.remove_const(:TrinkyDink)
720
- end
721
-
722
- it 'should display a nice error message when cannot open a file' do
723
- lambda { @t.process_command "edit TrinkyDink#claudia_linklater" }.should.
724
- raise(Pry::CommandError).message.should.match(/Cannot find a file for/)
725
- end
726
- end
727
- end