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
data/spec/hooks_spec.rb DELETED
@@ -1,522 +0,0 @@
1
- require 'helper'
2
-
3
- describe Pry::Hooks do
4
- before do
5
- @hooks = Pry::Hooks.new
6
- end
7
-
8
- describe "adding a new hook" do
9
- it 'should not execute hook while adding it' do
10
- run = false
11
- @hooks.add_hook(:test_hook, :my_name) { run = true }
12
- run.should == false
13
- end
14
-
15
- it 'should not allow adding of a hook with a duplicate name' do
16
- @hooks.add_hook(:test_hook, :my_name) {}
17
-
18
- lambda { @hooks.add_hook(:test_hook, :my_name) {} }.should.raise ArgumentError
19
- end
20
-
21
- it 'should create a new hook with a block' do
22
- @hooks.add_hook(:test_hook, :my_name) { }
23
- @hooks.hook_count(:test_hook).should == 1
24
- end
25
-
26
- it 'should create a new hook with a callable' do
27
- @hooks.add_hook(:test_hook, :my_name, proc { })
28
- @hooks.hook_count(:test_hook).should == 1
29
- end
30
-
31
- it 'should use block if given both block and callable' do
32
- run = false
33
- foo = false
34
- @hooks.add_hook(:test_hook, :my_name, proc { foo = true }) { run = true }
35
- @hooks.hook_count(:test_hook).should == 1
36
- @hooks.exec_hook(:test_hook)
37
- run.should == true
38
- foo.should == false
39
- end
40
-
41
- it 'should raise if not given a block or any other object' do
42
- lambda { @hooks.add_hook(:test_hook, :my_name) }.should.raise ArgumentError
43
- end
44
-
45
- it 'should create multiple hooks for an event' do
46
- @hooks.add_hook(:test_hook, :my_name) {}
47
- @hooks.add_hook(:test_hook, :my_name2) {}
48
- @hooks.hook_count(:test_hook).should == 2
49
- end
50
-
51
- it 'should return a count of 0 for an empty hook' do
52
- @hooks.hook_count(:test_hook).should == 0
53
- end
54
- end
55
-
56
- describe "Pry::Hooks#merge" do
57
- describe "merge!" do
58
- it 'should merge in the Pry::Hooks' do
59
- h1 = Pry::Hooks.new.add_hook(:test_hook, :testing) {}
60
- h2 = Pry::Hooks.new
61
-
62
- h2.merge!(h1)
63
- h2.get_hook(:test_hook, :testing).should == h1.get_hook(:test_hook, :testing)
64
- end
65
-
66
- it 'should not share merged elements with original' do
67
- h1 = Pry::Hooks.new.add_hook(:test_hook, :testing) {}
68
- h2 = Pry::Hooks.new
69
-
70
- h2.merge!(h1)
71
- h2.add_hook(:test_hook, :testing2) {}
72
- h2.get_hook(:test_hook, :testing2).should.not == h1.get_hook(:test_hook, :testing2)
73
- end
74
-
75
- it 'should NOT overwrite hooks belonging to shared event in receiver' do
76
- h1 = Pry::Hooks.new.add_hook(:test_hook, :testing) {}
77
- callable = proc {}
78
- h2 = Pry::Hooks.new.add_hook(:test_hook, :testing2, callable)
79
-
80
- h2.merge!(h1)
81
- h2.get_hook(:test_hook, :testing2).should == callable
82
- end
83
-
84
- it 'should overwrite identical hook in receiver' do
85
- callable1 = proc { :one }
86
- h1 = Pry::Hooks.new.add_hook(:test_hook, :testing, callable1)
87
- callable2 = proc { :two }
88
- h2 = Pry::Hooks.new.add_hook(:test_hook, :testing, callable2)
89
-
90
- h2.merge!(h1)
91
- h2.get_hook(:test_hook, :testing).should == callable1
92
- h2.hook_count(:test_hook).should == 1
93
- end
94
-
95
- it 'should preserve hook order' do
96
- name = ""
97
- h1 = Pry::Hooks.new
98
- h1.add_hook(:test_hook, :testing3) { name << "h" }
99
- h1.add_hook(:test_hook, :testing4) { name << "n" }
100
-
101
- h2 = Pry::Hooks.new
102
- h2.add_hook(:test_hook, :testing1) { name << "j" }
103
- h2.add_hook(:test_hook, :testing2) { name << "o" }
104
-
105
- h2.merge!(h1)
106
- h2.exec_hook(:test_hook)
107
-
108
- name.should == "john"
109
- end
110
-
111
- describe "merge" do
112
- it 'should return a fresh, independent instance' do
113
- h1 = Pry::Hooks.new.add_hook(:test_hook, :testing) {}
114
- h2 = Pry::Hooks.new
115
-
116
- h3 = h2.merge(h1)
117
- h3.should.not == h1
118
- h3.should.not == h2
119
- end
120
-
121
- it 'should contain hooks from original instance' do
122
- h1 = Pry::Hooks.new.add_hook(:test_hook, :testing) {}
123
- h2 = Pry::Hooks.new.add_hook(:test_hook2, :testing) {}
124
-
125
- h3 = h2.merge(h1)
126
- h3.get_hook(:test_hook, :testing).should == h1.get_hook(:test_hook, :testing)
127
- h3.get_hook(:test_hook2, :testing).should == h2.get_hook(:test_hook2, :testing)
128
- end
129
-
130
- it 'should not affect original instances when new hooks are added' do
131
- h1 = Pry::Hooks.new.add_hook(:test_hook, :testing) {}
132
- h2 = Pry::Hooks.new.add_hook(:test_hook2, :testing) {}
133
-
134
- h3 = h2.merge(h1)
135
- h3.add_hook(:test_hook3, :testing) {}
136
-
137
- h1.get_hook(:test_hook3, :testing).should == nil
138
- h2.get_hook(:test_hook3, :testing).should == nil
139
- end
140
- end
141
-
142
- end
143
- end
144
-
145
- describe "dupping a Pry::Hooks instance" do
146
- it 'should share hooks with original' do
147
- @hooks.add_hook(:test_hook, :testing) do
148
- :none_such
149
- end
150
-
151
- hooks_dup = @hooks.dup
152
- hooks_dup.get_hook(:test_hook, :testing).should == @hooks.get_hook(:test_hook, :testing)
153
- end
154
-
155
- it 'adding a new event to dupped instance should not affect original' do
156
- @hooks.add_hook(:test_hook, :testing) { :none_such }
157
- hooks_dup = @hooks.dup
158
-
159
- hooks_dup.add_hook(:other_test_hook, :testing) { :okay_man }
160
-
161
- hooks_dup.get_hook(:other_test_hook, :testing).should.not == @hooks.get_hook(:other_test_hook, :testing)
162
- end
163
-
164
- it 'adding a new hook to dupped instance should not affect original' do
165
- @hooks.add_hook(:test_hook, :testing) { :none_such }
166
- hooks_dup = @hooks.dup
167
-
168
- hooks_dup.add_hook(:test_hook, :testing2) { :okay_man }
169
-
170
- hooks_dup.get_hook(:test_hook, :testing2).should.not == @hooks.get_hook(:test_hook, :testing2)
171
- end
172
-
173
- end
174
-
175
- describe "getting hooks" do
176
- describe "get_hook" do
177
- it 'should return the correct requested hook' do
178
- run = false
179
- fun = false
180
- @hooks.add_hook(:test_hook, :my_name) { run = true }
181
- @hooks.add_hook(:test_hook, :my_name2) { fun = true }
182
- @hooks.get_hook(:test_hook, :my_name).call
183
- run.should == true
184
- fun.should == false
185
- end
186
-
187
- it 'should return nil if hook does not exist' do
188
- @hooks.get_hook(:test_hook, :my_name).should == nil
189
- end
190
- end
191
-
192
- describe "get_hooks" do
193
- it 'should return a hash of hook names/hook functions for an event' do
194
- hook1 = proc { 1 }
195
- hook2 = proc { 2 }
196
- @hooks.add_hook(:test_hook, :my_name1, hook1)
197
- @hooks.add_hook(:test_hook, :my_name2, hook2)
198
- hash = @hooks.get_hooks(:test_hook)
199
- hash.size.should == 2
200
- hash[:my_name1].should == hook1
201
- hash[:my_name2].should == hook2
202
- end
203
-
204
- it 'should return an empty hash if no hooks defined' do
205
- @hooks.get_hooks(:test_hook).should == {}
206
- end
207
- end
208
- end
209
-
210
- describe "clearing all hooks for an event" do
211
- it 'should clear all hooks' do
212
- @hooks.add_hook(:test_hook, :my_name) { }
213
- @hooks.add_hook(:test_hook, :my_name2) { }
214
- @hooks.add_hook(:test_hook, :my_name3) { }
215
- @hooks.clear(:test_hook)
216
- @hooks.hook_count(:test_hook).should == 0
217
- end
218
- end
219
-
220
- describe "deleting a hook" do
221
- it 'should successfully delete a hook' do
222
- @hooks.add_hook(:test_hook, :my_name) {}
223
- @hooks.delete_hook(:test_hook, :my_name)
224
- @hooks.hook_count(:test_hook).should == 0
225
- end
226
-
227
- it 'should return the deleted hook' do
228
- run = false
229
- @hooks.add_hook(:test_hook, :my_name) { run = true }
230
- @hooks.delete_hook(:test_hook, :my_name).call
231
- run.should == true
232
- end
233
-
234
- it 'should return nil if hook does not exist' do
235
- @hooks.delete_hook(:test_hook, :my_name).should == nil
236
- end
237
- end
238
-
239
- describe "executing a hook" do
240
- it 'should execute block hook' do
241
- run = false
242
- @hooks.add_hook(:test_hook, :my_name) { run = true }
243
- @hooks.exec_hook(:test_hook)
244
- run.should == true
245
- end
246
-
247
- it 'should execute proc hook' do
248
- run = false
249
- @hooks.add_hook(:test_hook, :my_name, proc { run = true })
250
- @hooks.exec_hook(:test_hook)
251
- run.should == true
252
- end
253
-
254
- it 'should execute a general callable hook' do
255
- callable = Object.new.tap do |obj|
256
- obj.instance_variable_set(:@test_var, nil)
257
- class << obj
258
- attr_accessor :test_var
259
- def call() @test_var = true; end
260
- end
261
- end
262
-
263
- @hooks.add_hook(:test_hook, :my_name, callable)
264
- @hooks.exec_hook(:test_hook)
265
- callable.test_var.should == true
266
- end
267
-
268
- it 'should execute all hooks for an event if more than one is defined' do
269
- x = nil
270
- y = nil
271
- @hooks.add_hook(:test_hook, :my_name1) { y = true }
272
- @hooks.add_hook(:test_hook, :my_name2) { x = true }
273
- @hooks.exec_hook(:test_hook)
274
- x.should == true
275
- y.should == true
276
- end
277
-
278
- it 'should execute hooks in order' do
279
- array = []
280
- @hooks.add_hook(:test_hook, :my_name1) { array << 1 }
281
- @hooks.add_hook(:test_hook, :my_name2) { array << 2 }
282
- @hooks.add_hook(:test_hook, :my_name3) { array << 3 }
283
- @hooks.exec_hook(:test_hook)
284
- array.should == [1, 2, 3]
285
- end
286
-
287
- it 'return value of exec_hook should be that of last executed hook' do
288
- @hooks.add_hook(:test_hook, :my_name1) { 1 }
289
- @hooks.add_hook(:test_hook, :my_name2) { 2 }
290
- @hooks.add_hook(:test_hook, :my_name3) { 3 }
291
- @hooks.exec_hook(:test_hook).should == 3
292
- end
293
-
294
- it 'should add exceptions to the errors array' do
295
- @hooks.add_hook(:test_hook, :foo1) { raise 'one' }
296
- @hooks.add_hook(:test_hook, :foo2) { raise 'two' }
297
- @hooks.add_hook(:test_hook, :foo3) { raise 'three' }
298
- @hooks.exec_hook(:test_hook)
299
- @hooks.errors.map(&:message).should == ['one', 'two', 'three']
300
- end
301
-
302
- it 'should return the last exception raised as the return value' do
303
- @hooks.add_hook(:test_hook, :foo1) { raise 'one' }
304
- @hooks.add_hook(:test_hook, :foo2) { raise 'two' }
305
- @hooks.add_hook(:test_hook, :foo3) { raise 'three' }
306
- @hooks.exec_hook(:test_hook).should == @hooks.errors.last
307
- end
308
- end
309
-
310
- describe "integration tests" do
311
- describe "when_started hook" do
312
- it 'should yield options to the hook' do
313
- options = nil
314
- Pry.config.hooks.add_hook(:when_started, :test_hook) { |target, opt, _| options = opt }
315
-
316
- redirect_pry_io(StringIO.new("exit"), out=StringIO.new) do
317
- Pry.start binding, :hello => :baby
318
- end
319
-
320
- options[:hello].should == :baby
321
-
322
- Pry.config.hooks.delete_hook(:when_started, :test_hook)
323
- end
324
-
325
- describe "target" do
326
-
327
- it 'should yield the target, as a binding ' do
328
- b = nil
329
- Pry.config.hooks.add_hook(:when_started, :test_hook) { |target, opt, _| b = target }
330
-
331
- redirect_pry_io(StringIO.new("exit"), out=StringIO.new) do
332
- Pry.start 5, :hello => :baby
333
- end
334
-
335
- b.is_a?(Binding).should == true
336
- Pry.config.hooks.delete_hook(:when_started, :test_hook)
337
- end
338
-
339
- it 'should yield the target to the hook' do
340
- b = nil
341
- Pry.config.hooks.add_hook(:when_started, :test_hook) { |target, opt, _| b = target }
342
-
343
- redirect_pry_io(StringIO.new("exit"), out=StringIO.new) do
344
- Pry.start 5, :hello => :baby
345
- end
346
-
347
- b.eval('self').should == 5
348
- Pry.config.hooks.delete_hook(:when_started, :test_hook)
349
- end
350
- end
351
-
352
- it 'should allow overriding of target (and binding_stack)' do
353
- options = nil
354
- o = Object.new
355
- class << o; attr_accessor :value; end
356
-
357
- Pry.config.hooks.add_hook(:when_started, :test_hook) { |target, opt, _pry_| _pry_.binding_stack = [Pry.binding_for(o)] }
358
-
359
- redirect_pry_io(InputTester.new("@value = true","exit-all")) do
360
- Pry.start binding, :hello => :baby
361
- end
362
-
363
- o.value.should == true
364
- Pry.config.hooks.delete_hook(:when_started, :test_hook)
365
- end
366
-
367
- end
368
-
369
- describe "after_session hook" do
370
- it 'should always run, even if uncaught exception bubbles out of repl' do
371
- o = OpenStruct.new
372
- o.great_escape = Class.new(StandardError)
373
-
374
- old_ew = Pry.config.exception_whitelist
375
- Pry.config.exception_whitelist << o.great_escape
376
-
377
- array = [1, 2, 3, 4, 5]
378
-
379
- begin
380
- redirect_pry_io(StringIO.new("raise great_escape"), out=StringIO.new) do
381
- Pry.start o, :hooks => Pry::Hooks.new.add_hook(:after_session, :cleanup) { array = nil }
382
- end
383
- rescue => ex
384
- exception = ex
385
- end
386
-
387
- # ensure that an exception really was raised and it broke out
388
- # of the repl
389
- exception.is_a?(o.great_escape).should == true
390
-
391
- # check that after_session hook ran
392
- array.should == nil
393
-
394
- # cleanup after test
395
- Pry.config.exception_whitelist = old_ew
396
- end
397
-
398
- describe "before_eval hook" do
399
- describe "modifying input code" do
400
- it 'should replace input code with code determined by hook' do
401
- hooks = Pry::Hooks.new.add_hook(:before_eval, :quirk) { |code, pry| code.replace(":little_duck") }
402
- redirect_pry_io(InputTester.new(":jemima", "exit-all"), out = StringIO.new) do
403
- Pry.start(self, :hooks => hooks)
404
- end
405
- out.string.should =~ /little_duck/
406
- out.string.should.not =~ /jemima/
407
- end
408
-
409
- it 'should not interfere with command processing when replacing input code' do
410
- commands = Pry::CommandSet.new do
411
- import_from Pry::Commands, "exit-all"
412
-
413
- command "how-do-you-like-your-blue-eyed-boy-now-mister-death" do
414
- output.puts "in hours of bitterness i imagine balls of sapphire, of metal"
415
- end
416
- end
417
-
418
- hooks = Pry::Hooks.new.add_hook(:before_eval, :quirk) { |code, pry| code.replace(":little_duck") }
419
- redirect_pry_io(InputTester.new("how-do-you-like-your-blue-eyed-boy-now-mister-death", "exit-all"), out = StringIO.new) do
420
- Pry.start(self, :hooks => hooks, :commands => commands)
421
- end
422
- out.string.should =~ /in hours of bitterness i imagine balls of sapphire, of metal/
423
- out.string.should.not =~ /little_duck/
424
- end
425
- end
426
-
427
- end
428
-
429
- describe "exceptions" do
430
- before do
431
- Pry.config.hooks.add_hook(:after_eval, :baddums){ raise "Baddums" }
432
- Pry.config.hooks.add_hook(:after_eval, :simbads){ raise "Simbads" }
433
- end
434
-
435
- after do
436
- Pry.config.hooks.delete_hook(:after_eval, :baddums)
437
- Pry.config.hooks.delete_hook(:after_eval, :simbads)
438
- end
439
- it "should not raise exceptions" do
440
- lambda{
441
- mock_pry("1", "2", "3")
442
- }.should.not.raise
443
- end
444
-
445
- it "should print out a notice for each exception raised" do
446
- mock_pry("1").should =~ /after_eval hook failed: RuntimeError: Baddums\n.*after_eval hook failed: RuntimeError: Simbads/m
447
- end
448
- end
449
- end
450
- end
451
-
452
- describe "anonymous hooks" do
453
- it 'should allow adding of hook without a name' do
454
- @hooks.add_hook(:test_hook, nil) {}
455
- @hooks.hook_count(:test_hook).should == 1
456
- end
457
-
458
- it 'should only allow one anonymous hook to exist' do
459
- @hooks.add_hook(:test_hook, nil) { }
460
- @hooks.add_hook(:test_hook, nil) { }
461
- @hooks.hook_count(:test_hook).should == 1
462
- end
463
-
464
- it 'should execute most recently added anonymous hook' do
465
- x = nil
466
- y = nil
467
- @hooks.add_hook(:test_hook, nil) { y = 1 }
468
- @hooks.add_hook(:test_hook, nil) { x = 2 }
469
- @hooks.exec_hook(:test_hook)
470
- y.should == nil
471
- x.should == 2
472
- end
473
- end
474
-
475
- describe "deprecated hash-based API" do
476
- after do
477
- Pry.config.hooks.clear_all if Pry.config.hooks
478
- end
479
-
480
- describe "Pry.config.hooks" do
481
- it 'should allow a hash-assignment' do
482
- Pry.config.hooks = { :before_session => proc { :hello } }
483
- Pry.config.hooks.get_hook(:before_session, nil).call.should == :hello
484
- end
485
-
486
- describe "Pry.config.hooks[]" do
487
- it 'should return the only anonymous hook' do
488
- Pry.config.hooks = { :before_session => proc { :hello } }
489
- Pry.config.hooks[:before_session].call.should == :hello
490
- end
491
-
492
- it 'should add an anonymous hook when using Pry.config.hooks[]=' do
493
- Pry.config.hooks[:before_session] = proc { :bing }
494
- Pry.config.hooks.hook_count(:before_session).should == 1
495
- end
496
-
497
- it 'should add overwrite previous anonymous hooks with new one when calling Pry.config.hooks[]= multiple times' do
498
- x = nil
499
- Pry.config.hooks[:before_session] = proc { x = 1 }
500
- Pry.config.hooks[:before_session] = proc { x = 2 }
501
-
502
- Pry.config.hooks.exec_hook(:before_session)
503
- Pry.config.hooks.hook_count(:before_session).should == 1
504
- x.should == 2
505
- end
506
- end
507
- end
508
-
509
- describe "Pry.start" do
510
- it 'should accept a hash for :hooks parameter' do
511
-
512
- redirect_pry_io(InputTester.new("exit-all"), out=StringIO.new) do
513
- Pry.start binding, :hooks => { :before_session => proc { |output, _, _| output.puts 'hello friend' } }
514
- end
515
-
516
- out.string.should =~ /hello friend/
517
- end
518
-
519
- end
520
- end
521
-
522
- end