pry 0.10.2-i386-mingw32 → 1.0.0.pre1-i386-mingw32

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 (189) hide show
  1. data/.document +2 -0
  2. data/.gitignore +16 -0
  3. data/.travis.yml +21 -0
  4. data/.yardopts +3 -0
  5. data/CHANGELOG +503 -0
  6. data/CONTRIBUTORS +55 -0
  7. data/Gemfile +9 -0
  8. data/Guardfile +62 -0
  9. data/LICENSE +2 -2
  10. data/{README.md → README.markdown} +31 -37
  11. data/Rakefile +144 -0
  12. data/TODO +117 -0
  13. data/lib/pry.rb +146 -33
  14. data/lib/pry/cli.rb +13 -35
  15. data/lib/pry/code.rb +63 -24
  16. data/lib/pry/code/loc.rb +2 -2
  17. data/lib/pry/code_object.rb +21 -40
  18. data/lib/pry/command.rb +6 -9
  19. data/lib/pry/command_set.rb +37 -80
  20. data/lib/pry/commands.rb +1 -1
  21. data/lib/pry/commands/amend_line.rb +1 -1
  22. data/lib/pry/commands/bang.rb +1 -1
  23. data/lib/pry/commands/cat.rb +2 -11
  24. data/lib/pry/commands/cat/abstract_formatter.rb +1 -1
  25. data/lib/pry/commands/cat/exception_formatter.rb +7 -6
  26. data/lib/pry/commands/cat/file_formatter.rb +32 -15
  27. data/lib/pry/commands/cat/input_expression_formatter.rb +1 -1
  28. data/lib/pry/commands/cd.rb +3 -14
  29. data/lib/pry/commands/code_collector.rb +4 -4
  30. data/lib/pry/commands/easter_eggs.rb +3 -3
  31. data/lib/pry/commands/edit.rb +22 -10
  32. data/lib/pry/commands/edit/exception_patcher.rb +1 -1
  33. data/lib/pry/commands/edit/file_and_line_locator.rb +2 -0
  34. data/lib/pry/{method/patcher.rb → commands/edit/method_patcher.rb} +37 -40
  35. data/lib/pry/commands/find_method.rb +22 -16
  36. data/lib/pry/commands/gem_install.rb +2 -5
  37. data/lib/pry/commands/gem_open.rb +1 -1
  38. data/lib/pry/commands/gist.rb +11 -10
  39. data/lib/pry/commands/help.rb +14 -14
  40. data/lib/pry/commands/hist.rb +5 -24
  41. data/lib/pry/commands/ls.rb +287 -56
  42. data/lib/pry/commands/play.rb +10 -44
  43. data/lib/pry/commands/pry_backtrace.rb +2 -1
  44. data/lib/pry/commands/raise_up.rb +1 -1
  45. data/lib/pry/commands/reload_code.rb +15 -31
  46. data/lib/pry/commands/ri.rb +3 -7
  47. data/lib/pry/commands/shell_command.rb +12 -17
  48. data/lib/pry/commands/shell_mode.rb +2 -2
  49. data/lib/pry/commands/show_doc.rb +0 -5
  50. data/lib/pry/commands/show_info.rb +10 -11
  51. data/lib/pry/commands/show_source.rb +3 -15
  52. data/lib/pry/commands/simple_prompt.rb +1 -1
  53. data/lib/pry/commands/toggle_color.rb +4 -8
  54. data/lib/pry/commands/whereami.rb +10 -18
  55. data/lib/pry/completion.rb +293 -0
  56. data/lib/pry/config.rb +233 -20
  57. data/lib/pry/core_extensions.rb +19 -29
  58. data/lib/pry/custom_completions.rb +6 -0
  59. data/lib/pry/editor.rb +103 -109
  60. data/lib/pry/helpers/base_helpers.rb +109 -22
  61. data/lib/pry/helpers/command_helpers.rb +8 -10
  62. data/lib/pry/helpers/documentation_helpers.rb +2 -1
  63. data/lib/pry/helpers/text.rb +5 -4
  64. data/lib/pry/history.rb +10 -21
  65. data/lib/pry/history_array.rb +0 -5
  66. data/lib/pry/hooks.rb +29 -9
  67. data/lib/pry/indent.rb +10 -5
  68. data/lib/pry/method.rb +86 -81
  69. data/lib/pry/method/weird_method_locator.rb +2 -4
  70. data/lib/pry/module_candidate.rb +14 -5
  71. data/lib/pry/pager.rb +48 -193
  72. data/lib/pry/plugins.rb +2 -2
  73. data/lib/pry/pry_class.rb +193 -104
  74. data/lib/pry/pry_instance.rb +154 -152
  75. data/lib/pry/rbx_method.rb +13 -0
  76. data/lib/pry/rbx_path.rb +1 -1
  77. data/lib/pry/repl.rb +14 -17
  78. data/lib/pry/repl_file_loader.rb +3 -8
  79. data/lib/pry/rubygem.rb +3 -3
  80. data/lib/pry/terminal.rb +3 -4
  81. data/lib/pry/test/helper.rb +11 -6
  82. data/lib/pry/version.rb +1 -1
  83. data/lib/pry/wrapped_module.rb +56 -49
  84. data/man/pry.1 +195 -0
  85. data/man/pry.1.html +204 -0
  86. data/man/pry.1.ronn +141 -0
  87. data/pry.gemspec +31 -0
  88. data/spec/Procfile +3 -0
  89. data/spec/cli_spec.rb +78 -0
  90. data/spec/code_object_spec.rb +277 -0
  91. data/spec/code_spec.rb +219 -0
  92. data/spec/command_helpers_spec.rb +29 -0
  93. data/spec/command_integration_spec.rb +562 -0
  94. data/spec/command_set_spec.rb +627 -0
  95. data/spec/command_spec.rb +821 -0
  96. data/spec/commands/amend_line_spec.rb +247 -0
  97. data/spec/commands/bang_spec.rb +18 -0
  98. data/spec/commands/cat_spec.rb +164 -0
  99. data/spec/commands/cd_spec.rb +250 -0
  100. data/spec/commands/disable_pry_spec.rb +25 -0
  101. data/spec/commands/edit_spec.rb +725 -0
  102. data/spec/commands/exit_all_spec.rb +27 -0
  103. data/spec/commands/exit_program_spec.rb +19 -0
  104. data/spec/commands/exit_spec.rb +28 -0
  105. data/spec/commands/find_method_spec.rb +70 -0
  106. data/spec/commands/gem_list_spec.rb +26 -0
  107. data/spec/commands/gist_spec.rb +79 -0
  108. data/spec/commands/help_spec.rb +56 -0
  109. data/spec/commands/hist_spec.rb +172 -0
  110. data/spec/commands/jump_to_spec.rb +15 -0
  111. data/spec/commands/ls_spec.rb +189 -0
  112. data/spec/commands/play_spec.rb +136 -0
  113. data/spec/commands/raise_up_spec.rb +56 -0
  114. data/spec/commands/save_file_spec.rb +177 -0
  115. data/spec/commands/show_doc_spec.rb +488 -0
  116. data/spec/commands/show_input_spec.rb +17 -0
  117. data/spec/commands/show_source_spec.rb +760 -0
  118. data/spec/commands/whereami_spec.rb +203 -0
  119. data/spec/completion_spec.rb +221 -0
  120. data/spec/control_d_handler_spec.rb +62 -0
  121. data/spec/documentation_helper_spec.rb +73 -0
  122. data/spec/editor_spec.rb +79 -0
  123. data/spec/exception_whitelist_spec.rb +21 -0
  124. data/spec/fixtures/candidate_helper1.rb +11 -0
  125. data/spec/fixtures/candidate_helper2.rb +8 -0
  126. data/spec/fixtures/example.erb +5 -0
  127. data/spec/fixtures/example_nesting.rb +33 -0
  128. data/spec/fixtures/show_source_doc_examples.rb +15 -0
  129. data/spec/fixtures/testlinkrc +2 -0
  130. data/spec/fixtures/testrc +2 -0
  131. data/spec/fixtures/testrcbad +2 -0
  132. data/spec/fixtures/whereami_helper.rb +6 -0
  133. data/spec/helper.rb +35 -0
  134. data/spec/helpers/bacon.rb +86 -0
  135. data/spec/helpers/mock_pry.rb +44 -0
  136. data/spec/helpers/repl_tester.rb +112 -0
  137. data/spec/helpers/table_spec.rb +105 -0
  138. data/spec/history_array_spec.rb +67 -0
  139. data/spec/hooks_spec.rb +522 -0
  140. data/spec/indent_spec.rb +301 -0
  141. data/spec/method_spec.rb +482 -0
  142. data/spec/prompt_spec.rb +61 -0
  143. data/spec/pry_defaults_spec.rb +420 -0
  144. data/spec/pry_history_spec.rb +69 -0
  145. data/spec/pry_output_spec.rb +95 -0
  146. data/spec/pry_repl_spec.rb +86 -0
  147. data/spec/pry_spec.rb +394 -0
  148. data/spec/pryrc_spec.rb +97 -0
  149. data/spec/run_command_spec.rb +25 -0
  150. data/spec/sticky_locals_spec.rb +147 -0
  151. data/spec/syntax_checking_spec.rb +81 -0
  152. data/spec/wrapped_module_spec.rb +261 -0
  153. data/wiki/Customizing-pry.md +397 -0
  154. data/wiki/Home.md +4 -0
  155. metadata +272 -61
  156. checksums.yaml +0 -7
  157. data/CHANGELOG.md +0 -714
  158. data/lib/pry/code/code_file.rb +0 -103
  159. data/lib/pry/color_printer.rb +0 -55
  160. data/lib/pry/commands/change_inspector.rb +0 -27
  161. data/lib/pry/commands/change_prompt.rb +0 -26
  162. data/lib/pry/commands/list_inspectors.rb +0 -35
  163. data/lib/pry/commands/list_prompts.rb +0 -35
  164. data/lib/pry/commands/ls/constants.rb +0 -47
  165. data/lib/pry/commands/ls/formatter.rb +0 -49
  166. data/lib/pry/commands/ls/globals.rb +0 -48
  167. data/lib/pry/commands/ls/grep.rb +0 -21
  168. data/lib/pry/commands/ls/instance_vars.rb +0 -39
  169. data/lib/pry/commands/ls/interrogatable.rb +0 -18
  170. data/lib/pry/commands/ls/jruby_hacks.rb +0 -49
  171. data/lib/pry/commands/ls/local_names.rb +0 -35
  172. data/lib/pry/commands/ls/local_vars.rb +0 -39
  173. data/lib/pry/commands/ls/ls_entity.rb +0 -70
  174. data/lib/pry/commands/ls/methods.rb +0 -57
  175. data/lib/pry/commands/ls/methods_helper.rb +0 -46
  176. data/lib/pry/commands/ls/self_methods.rb +0 -32
  177. data/lib/pry/commands/watch_expression.rb +0 -105
  178. data/lib/pry/commands/watch_expression/expression.rb +0 -38
  179. data/lib/pry/config/behavior.rb +0 -139
  180. data/lib/pry/config/convenience.rb +0 -25
  181. data/lib/pry/config/default.rb +0 -161
  182. data/lib/pry/exceptions.rb +0 -78
  183. data/lib/pry/input_completer.rb +0 -242
  184. data/lib/pry/input_lock.rb +0 -132
  185. data/lib/pry/inspector.rb +0 -27
  186. data/lib/pry/last_exception.rb +0 -61
  187. data/lib/pry/object_path.rb +0 -82
  188. data/lib/pry/output.rb +0 -50
  189. data/lib/pry/prompt.rb +0 -26
@@ -0,0 +1,69 @@
1
+ require 'helper'
2
+ require 'tempfile'
3
+
4
+ describe Pry do
5
+ before do
6
+ Pry.history.clear
7
+
8
+ @saved_history = "1\n2\n3\n"
9
+
10
+ Pry.history.loader = proc do |&blk|
11
+ @saved_history.lines.each { |l| blk.call(l) }
12
+ end
13
+
14
+ Pry.load_history
15
+ end
16
+
17
+ after do
18
+ Pry.history.clear
19
+ Pry.history.restore_default_behavior
20
+ end
21
+
22
+ describe '#push' do
23
+ it "should not record duplicated lines" do
24
+ Pry.history << '3'
25
+ Pry.history << '_ += 1'
26
+ Pry.history << '_ += 1'
27
+ Pry.history.to_a.grep('_ += 1').count.should == 1
28
+ end
29
+
30
+ it "should not record empty lines" do
31
+ c = Pry.history.to_a.count
32
+ Pry.history << ''
33
+ Pry.history.to_a.count.should == c
34
+ end
35
+ end
36
+
37
+ describe ".load_history" do
38
+ it "should read the contents of the file" do
39
+ Pry.history.to_a[-2..-1].should == %w(2 3)
40
+ end
41
+ end
42
+
43
+ describe "saving to a file" do
44
+ before do
45
+ @histfile = Tempfile.new(["pryhistory", "txt"])
46
+ @history = Pry::History.new(:file_path => @histfile.path)
47
+ Pry.config.history.should_save = true
48
+ @history.pusher = proc{ }
49
+ end
50
+
51
+ after do
52
+ @histfile.close(true)
53
+ Pry.config.history.should_save = false
54
+ end
55
+
56
+ it "should save lines to a file as they are written" do
57
+ @history.push "5"
58
+ File.read(@histfile.path).should == "5\n"
59
+ end
60
+
61
+ it "should interleave lines from many places" do
62
+ @history.push "5"
63
+ File.open(@histfile.path, 'a'){ |f| f.puts "6" }
64
+ @history.push "7"
65
+
66
+ File.read(@histfile.path).should == "5\n6\n7\n"
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,95 @@
1
+ require 'helper'
2
+
3
+ describe Pry do
4
+ describe "output failsafe" do
5
+ after do
6
+ Pry.config.print = Pry::DEFAULT_PRINT
7
+ end
8
+
9
+ it "should catch serialization exceptions" do
10
+ Pry.config.print = lambda { |*a| raise "catch-22" }
11
+
12
+ lambda {
13
+ mock_pry("1")
14
+ }.should.not.raise
15
+ end
16
+
17
+ it "should display serialization exceptions" do
18
+ Pry.config.print = lambda { |*a| raise "catch-22" }
19
+
20
+ mock_pry("1").should =~ /\(pry\) output error: #<RuntimeError: catch-22>/
21
+ end
22
+
23
+ it "should catch errors serializing exceptions" do
24
+ Pry.config.print = lambda do |*a|
25
+ raise Exception.new("catch-22").tap{ |e| class << e; def inspect; raise e; end; end }
26
+ end
27
+
28
+ mock_pry("1").should =~ /\(pry\) output error: failed to show result/
29
+ end
30
+ end
31
+
32
+ describe "DEFAULT_PRINT" do
33
+ it "should output the right thing" do
34
+ mock_pry("[1]").should =~ /^=> \[1\]/
35
+ end
36
+
37
+ it "should include the =>" do
38
+ accumulator = StringIO.new
39
+ Pry.config.print.call(accumulator, [1])
40
+ accumulator.string.should == "=> \[1\]\n"
41
+ end
42
+
43
+ it "should not be phased by un-inspectable things" do
44
+ mock_pry("class NastyClass; undef pretty_inspect; end", "NastyClass.new").should =~ /#<.*NastyClass:0x.*?>/
45
+ end
46
+ end
47
+
48
+ describe "color" do
49
+ before do
50
+ Pry.color = true
51
+ end
52
+
53
+ after do
54
+ Pry.color = false
55
+ end
56
+
57
+ it "should colorize strings as though they were ruby" do
58
+ accumulator = StringIO.new
59
+ Pry.config.print.call(accumulator, [1])
60
+ accumulator.string.should == "=> [\e[1;34m1\e[0m]\e[0m\n"
61
+ end
62
+
63
+ it "should not colorize strings that already include color" do
64
+ f = Object.new
65
+ def f.inspect
66
+ "\e[1;31mFoo\e[0m"
67
+ end
68
+ accumulator = StringIO.new
69
+ Pry.config.print.call(accumulator, f)
70
+ # We add an extra \e[0m to prevent color leak
71
+ accumulator.string.should == "=> \e[1;31mFoo\e[0m\e[0m\n"
72
+ end
73
+ end
74
+
75
+ describe "output suppression" do
76
+ before do
77
+ @t = pry_tester
78
+ end
79
+ it "should normally output the result" do
80
+ mock_pry("1 + 2").should == "=> 3\n"
81
+ end
82
+
83
+ it "should not output anything if the input ends with a semicolon" do
84
+ mock_pry("1 + 2;").should == ""
85
+ end
86
+
87
+ it "should output something if the input ends with a comment" do
88
+ mock_pry("1 + 2 # basic addition").should == "=> 3\n"
89
+ end
90
+
91
+ it "should not output something if the input is only a comment" do
92
+ mock_pry("# basic addition").should == ""
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,86 @@
1
+ require 'helper'
2
+
3
+ describe "The whole thing" do
4
+ it "should let you run commands in the middle of multiline expressions" do
5
+ ReplTester.start do
6
+ input 'def a'
7
+ input '!'
8
+ output /^Input buffer cleared/
9
+
10
+ input '5'
11
+ output '=> 5'
12
+ end
13
+ end
14
+
15
+ describe "eval_string and binding_stack" do
16
+ it "shouldn't break if we start a nested REPL" do
17
+ ReplTester.start do
18
+ input 'Pry::REPL.new(_pry_, :target => 10).start'
19
+ output ''
20
+ prompt /10.*> $/
21
+
22
+ input 'self'
23
+ output '=> 10'
24
+
25
+ input nil # Ctrl-D
26
+ output ''
27
+
28
+ input 'self'
29
+ output '=> main'
30
+ end
31
+ end
32
+
33
+ it "shouldn't break if we start a nested instance" do
34
+ ReplTester.start do
35
+ input 'Pry.start(10)'
36
+ output ''
37
+ prompt /10.*> $/
38
+
39
+ input 'self'
40
+ output '=> 10'
41
+
42
+ input nil # Ctrl-D
43
+ output '=> nil' # return value of Pry session
44
+
45
+ input 'self'
46
+ output '=> main'
47
+ end
48
+ end
49
+
50
+ it "shouldn't break if we pop bindings in Ruby" do
51
+ ReplTester.start do
52
+ input 'cd 10'
53
+ output ''
54
+ prompt /10.*> $/
55
+
56
+ input '_pry_.binding_stack.pop'
57
+ output /^=> #<Binding/
58
+ prompt /main.*> $/
59
+
60
+ input '_pry_.binding_stack.pop'
61
+ output /^=> #<Binding/
62
+ assert_exited
63
+ end
64
+ end
65
+
66
+ it "should immediately evaluate eval_string after cmd if complete" do
67
+ set = Pry::CommandSet.new do
68
+ import Pry::Commands
69
+
70
+ command 'hello!' do
71
+ eval_string.replace('"hello"')
72
+ end
73
+ end
74
+
75
+ ReplTester.start(:commands => set) do
76
+ input 'def x'
77
+ output ''
78
+ prompt /\* $/
79
+
80
+ input 'hello!'
81
+ output '=> "hello"'
82
+ prompt /> $/
83
+ end
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,394 @@
1
+ require 'helper'
2
+
3
+ describe Pry do
4
+ before do
5
+ @str_output = StringIO.new
6
+ end
7
+
8
+ if RUBY_VERSION =~ /1.9/
9
+ describe "Exotic object support" do
10
+ # regression test for exotic object support
11
+ it "Should not error when return value is a BasicObject instance" do
12
+
13
+ ReplTester.start do
14
+ input('BasicObject.new').should =~ /^=> #<BasicObject:/
15
+ end
16
+
17
+ end
18
+ end
19
+ end
20
+
21
+ describe 'DISABLE_PRY' do
22
+ before do
23
+ ENV['DISABLE_PRY'] = 'true'
24
+ end
25
+
26
+ after do
27
+ ENV.delete 'DISABLE_PRY'
28
+ end
29
+
30
+ it 'should not binding.pry' do
31
+ binding.pry.should == nil
32
+ end
33
+
34
+ it 'should not Pry.start' do
35
+ Pry.start.should == nil
36
+ end
37
+ end
38
+
39
+ describe "Pry.critical_section" do
40
+ it "should prevent Pry being called" do
41
+ output = StringIO.new
42
+ Pry.output = output
43
+ Pry.critical_section do
44
+ Pry.start
45
+ end
46
+ output.string.should =~ /Pry started inside Pry/
47
+ end
48
+ end
49
+
50
+ describe "Pry.binding_for" do
51
+
52
+ # regression test for burg's bug (see git history)
53
+ it "Should not error when object doesn't have a valid == method" do
54
+ o = Object.new
55
+ def o.==(other)
56
+ raise
57
+ end
58
+
59
+ lambda { Pry.binding_for(o) }.should.not.raise Exception
60
+ end
61
+
62
+ it "should not leak local variables" do
63
+ [Object.new, Array, 3].each do |obj|
64
+ Pry.binding_for(obj).eval("local_variables").should.be.empty
65
+ end
66
+ end
67
+ end
68
+
69
+ describe "open a Pry session on an object" do
70
+ describe "rep" do
71
+ before do
72
+ class Hello
73
+ end
74
+ end
75
+
76
+ after do
77
+ Object.send(:remove_const, :Hello)
78
+ end
79
+
80
+ # bug fix for https://github.com/banister/pry/issues/93
81
+ it 'should not leak pry constants into Object namespace' do
82
+ lambda{
83
+ pry_eval(Object.new, "Command")
84
+ }.should.raise(NameError)
85
+ end
86
+
87
+ if defined?(BasicObject)
88
+ it 'should be able to operate inside the BasicObject class' do
89
+ pry_eval(BasicObject, ":foo", "Pad.obj = _")
90
+ Pad.obj.should == :foo
91
+ end
92
+ end
93
+
94
+ it 'should set an ivar on an object' do
95
+ o = Object.new
96
+ pry_eval(o, "@x = 10")
97
+ o.instance_variable_get(:@x).should == 10
98
+ end
99
+
100
+ it 'should display error if Pry instance runs out of input' do
101
+ redirect_pry_io(StringIO.new, @str_output) do
102
+ Pry.start
103
+ end
104
+ @str_output.string.should =~ /Error: Pry ran out of things to read/
105
+ end
106
+
107
+ it 'should make self evaluate to the receiver of the rep session' do
108
+ o = :john
109
+ pry_eval(o, "self").should == o
110
+ end
111
+
112
+ it 'should define a nested class under Hello and not on top-level or Pry' do
113
+ mock_pry(Pry.binding_for(Hello), "class Nested", "end")
114
+ Hello.const_defined?(:Nested).should == true
115
+ end
116
+
117
+ it 'should suppress output if input ends in a ";" and is an Exception object (single line)' do
118
+ mock_pry("Exception.new;").should == ""
119
+ end
120
+
121
+ it 'should suppress output if input ends in a ";" (single line)' do
122
+ mock_pry("x = 5;").should == ""
123
+ end
124
+
125
+ it 'should be able to evaluate exceptions normally' do
126
+ was_called = false
127
+ mock_pry("RuntimeError.new", :exception_handler => proc{ was_called = true })
128
+ was_called.should == false
129
+ end
130
+
131
+ it 'should notice when exceptions are raised' do
132
+ was_called = false
133
+ mock_pry("raise RuntimeError", :exception_handler => proc{ was_called = true })
134
+ was_called.should == true
135
+ end
136
+
137
+ it 'should not try to catch intended exceptions' do
138
+ lambda { mock_pry("raise SystemExit") }.should.raise SystemExit
139
+ # SIGTERM
140
+ lambda { mock_pry("raise SignalException.new(15)") }.should.raise SignalException
141
+ end
142
+
143
+ describe "multi-line input" do
144
+ it "works" do
145
+ mock_pry('x = ', '1 + 4').should =~ /5/
146
+ end
147
+
148
+ it 'should suppress output if input ends in a ";" (multi-line)' do
149
+ mock_pry('def self.blah', ':test', 'end;').should == ''
150
+ end
151
+
152
+ describe "newline stripping from an empty string" do
153
+ it "with double quotes" do
154
+ mock_pry('"', '"').should =~ %r|"\\n"|
155
+ mock_pry('"', "\n", "\n", "\n", '"').should =~ %r|"\\n\\n\\n\\n"|
156
+ end
157
+
158
+ it "with single quotes" do
159
+ mock_pry("'", "'").should =~ %r|"\\n"|
160
+ mock_pry("'", "\n", "\n", "\n", "'").should =~ %r|"\\n\\n\\n\\n"|
161
+ end
162
+
163
+ it "with fancy delimiters" do
164
+ mock_pry('%(', ')').should =~ %r|"\\n"|
165
+ mock_pry('%|', "\n", "\n", '|').should =~ %r|"\\n\\n\\n"|
166
+ mock_pry('%q[', "\n", "\n", ']').should =~ %r|"\\n\\n\\n"|
167
+ end
168
+ end
169
+
170
+ describe "newline stripping from an empty regexp" do
171
+ it "with regular regexp delimiters" do
172
+ mock_pry('/', '/').should =~ %r{/\n/}
173
+ end
174
+
175
+ it "with fancy delimiters" do
176
+ mock_pry('%r{', "\n", "\n", '}').should =~ %r{/\n\n\n/}
177
+ mock_pry('%r<', "\n", '>').should =~ %r{/\n\n/}
178
+ end
179
+ end
180
+
181
+ describe "newline from an empty heredoc" do
182
+ it "works" do
183
+ mock_pry('<<HERE', 'HERE').should =~ %r|""|
184
+ mock_pry("<<'HERE'", "\n", 'HERE').should =~ %r|"\\n"|
185
+ mock_pry("<<-'HERE'", "\n", "\n", 'HERE').should =~ %r|"\\n\\n"|
186
+ end
187
+ end
188
+ end
189
+ end
190
+
191
+ describe "repl" do
192
+ describe "basic functionality" do
193
+ it 'should set an ivar on an object and exit the repl' do
194
+ input_strings = ["@x = 10", "exit-all"]
195
+ input = InputTester.new(*input_strings)
196
+
197
+ o = Object.new
198
+
199
+ pry_tester = Pry.start(o, :input => input, :output => StringIO.new)
200
+
201
+ o.instance_variable_get(:@x).should == 10
202
+ end
203
+ end
204
+
205
+ describe "complete_expression?" do
206
+ it "should not mutate the input!" do
207
+ clean = "puts <<-FOO\nhi\nFOO\n"
208
+ a = clean.dup
209
+ Pry::Code.complete_expression?(a)
210
+ a.should == clean
211
+ end
212
+ end
213
+
214
+ describe "history arrays" do
215
+ it 'sets _ to the last result' do
216
+ t = pry_tester
217
+ t.eval ":foo"
218
+ t.eval("_").should == :foo
219
+ t.eval "42"
220
+ t.eval("_").should == 42
221
+ end
222
+
223
+ it 'sets out to an array with the result' do
224
+ t = pry_tester
225
+ t.eval ":foo"
226
+ t.eval "42"
227
+ res = t.eval "_out_"
228
+
229
+ res.should.be.kind_of Pry::HistoryArray
230
+ res[1..2].should == [:foo, 42]
231
+ end
232
+
233
+ it 'sets _in_ to an array with the entered lines' do
234
+ t = pry_tester
235
+ t.eval ":foo"
236
+ t.eval "42"
237
+ res = t.eval "_in_"
238
+
239
+ res.should.be.kind_of Pry::HistoryArray
240
+ res[1..2].should == [":foo\n", "42\n"]
241
+ end
242
+
243
+ it 'uses 100 as the size of _in_ and _out_' do
244
+ pry_tester.eval("[_in_.max_size, _out_.max_size]").should == [100, 100]
245
+ end
246
+
247
+ it 'can change the size of the history arrays' do
248
+ pry_tester(:memory_size => 1000).eval("[_out_, _in_].map(&:max_size)").should == [1000, 1000]
249
+ end
250
+
251
+ it 'store exceptions' do
252
+ mock_pry("foo!", "Pad.in = _in_[-1]; Pad.out = _out_[-1]")
253
+
254
+ Pad.in.should == "foo!\n"
255
+ Pad.out.should.be.kind_of NoMethodError
256
+ end
257
+ end
258
+
259
+ describe "last_result" do
260
+ it "should be set to the most recent value" do
261
+ pry_eval("2", "_ + 82").should == 84
262
+ end
263
+
264
+ # This test needs mock_pry because the command retvals work by
265
+ # replacing the eval_string, so _ won't be modified without Pry doing
266
+ # a REPL loop.
267
+ it "should be set to the result of a command with :keep_retval" do
268
+ Pry::Commands.block_command '++', '', :keep_retval => true do |a|
269
+ a.to_i + 1
270
+ end
271
+
272
+ mock_pry('++ 86', '++ #{_}').should =~ /88/
273
+ end
274
+
275
+ it "should be preserved over an empty line" do
276
+ pry_eval("2 + 2", " ", "\t", " ", "_ + 92").should == 96
277
+ end
278
+
279
+ it "should be preserved when evalling a command without :keep_retval" do
280
+ pry_eval("2 + 2", "ls -l", "_ + 96").should == 100
281
+ end
282
+ end
283
+
284
+ describe "nesting" do
285
+ after do
286
+ Pry.reset_defaults
287
+ Pry.color = false
288
+ end
289
+
290
+ it 'should nest properly' do
291
+ Pry.input = InputTester.new("cd 1", "cd 2", "cd 3", "\"nest:\#\{(_pry_.binding_stack.size - 1)\}\"", "exit-all")
292
+
293
+ Pry.output = @str_output
294
+
295
+ o = Object.new
296
+
297
+ pry_tester = o.pry
298
+ @str_output.string.should =~ /nest:3/
299
+ end
300
+ end
301
+
302
+ describe "defining methods" do
303
+ it 'should define a method on the singleton class of an object when performing "def meth;end" inside the object' do
304
+ [Object.new, {}, []].each do |val|
305
+ pry_eval(val, 'def hello; end')
306
+ val.methods(false).map(&:to_sym).include?(:hello).should == true
307
+ end
308
+ end
309
+
310
+ it 'should define an instance method on the module when performing "def meth;end" inside the module' do
311
+ hello = Module.new
312
+ pry_eval(hello, "def hello; end")
313
+ hello.instance_methods(false).map(&:to_sym).include?(:hello).should == true
314
+ end
315
+
316
+ it 'should define an instance method on the class when performing "def meth;end" inside the class' do
317
+ hello = Class.new
318
+ pry_eval(hello, "def hello; end")
319
+ hello.instance_methods(false).map(&:to_sym).include?(:hello).should == true
320
+ end
321
+
322
+ it 'should define a method on the class of an object when performing "def meth;end" inside an immediate value or Numeric' do
323
+ # should include float in here, but test fails for some reason
324
+ # on 1.8.7, no idea why!
325
+ [:test, 0, true, false, nil].each do |val|
326
+ pry_eval(val, "def hello; end");
327
+ val.class.instance_methods(false).map(&:to_sym).include?(:hello).should == true
328
+ end
329
+ end
330
+ end
331
+
332
+ describe "Object#pry" do
333
+
334
+ after do
335
+ Pry.reset_defaults
336
+ Pry.color = false
337
+ end
338
+
339
+ it "should start a pry session on the receiver (first form)" do
340
+ Pry.input = InputTester.new("self", "exit-all")
341
+
342
+ str_output = StringIO.new
343
+ Pry.output = str_output
344
+
345
+ 20.pry
346
+
347
+ str_output.string.should =~ /20/
348
+ end
349
+
350
+ it "should start a pry session on the receiver (second form)" do
351
+ Pry.input = InputTester.new("self", "exit-all")
352
+
353
+ str_output = StringIO.new
354
+ Pry.output = str_output
355
+
356
+ pry 20
357
+
358
+ str_output.string.should =~ /20/
359
+ end
360
+
361
+ it "should raise if more than two arguments are passed to Object#pry" do
362
+ lambda { pry(20, :quiet, :input => Readline) }.should.raise ArgumentError
363
+ end
364
+ end
365
+
366
+ describe "Pry.binding_for" do
367
+ it 'should return TOPLEVEL_BINDING if parameter self is main' do
368
+ _main_ = lambda { TOPLEVEL_BINDING.eval('self') }
369
+ Pry.binding_for(_main_.call).is_a?(Binding).should == true
370
+ Pry.binding_for(_main_.call).should == TOPLEVEL_BINDING
371
+ Pry.binding_for(_main_.call).should == Pry.binding_for(_main_.call)
372
+ end
373
+ end
374
+ end
375
+ end
376
+
377
+ describe 'setting custom options' do
378
+ it 'should not raise for unrecognized options' do
379
+ should.not.raise?(NoMethodError) {
380
+ instance = Pry.new(:custom_option => 'custom value')
381
+ }
382
+ end
383
+ end
384
+
385
+ describe "a fresh instance" do
386
+ it "should use `caller` as its backtrace" do
387
+ location = "#{__FILE__}:#{__LINE__ + 1}"
388
+ backtrace = Pry.new.backtrace
389
+
390
+ backtrace.should.not.be.nil
391
+ backtrace.any? { |l| l.include?(location) }.should.be.true
392
+ end
393
+ end
394
+ end