/
test_context.rb
183 lines (149 loc) · 5.4 KB
/
test_context.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
require 'helper'
describe "Pry::DefaultCommands::Context" do
describe "exit-all" do
it 'should break out of the repl loop of Pry instance (returning target of session)' do
redirect_pry_io(InputTester.new("exit-all"), StringIO.new) do
Pry.new.repl(0).should == 0
end
end
it 'should break out of the repl loop of Pry instance wth a user specified value' do
redirect_pry_io(InputTester.new("exit-all 'message'"), StringIO.new) do
Pry.new.repl(0).should == 'message'
end
end
it 'should break of the repl loop even if multiple bindings still on stack' do
ins = nil
redirect_pry_io(InputTester.new("cd 1", "cd 2", "exit-all 'message'"), StringIO.new) do
ins = Pry.new.tap { |v| v.repl(0).should == 'message' }
end
end
it 'binding_stack should be empty after breaking out of the repl loop' do
ins = nil
redirect_pry_io(InputTester.new("cd 1", "cd 2", "exit-all"), StringIO.new) do
ins = Pry.new.tap { |v| v.repl(0) }
end
ins.binding_stack.empty?.should == true
end
end
describe "exit" do
it 'should pop a binding with exit' do
b = Pry.binding_for(:outer)
b.eval("x = :inner")
redirect_pry_io(InputTester.new("cd x", "$inner = self;", "exit", "$outer = self", "exit-all"), StringIO.new) do
b.pry
end
$inner.should == :inner
$outer.should == :outer
end
it 'should break out of the repl loop of Pry instance when binding_stack has only one binding with exit' do
Pry.start(0, :input => StringIO.new("exit")).should == 0
end
it 'should break out of the repl loop of Pry instance when binding_stack has only one binding with exit, and return user-given value' do
Pry.start(0, :input => StringIO.new("exit :john")).should == :john
end
end
describe "jump-to" do
it 'should jump to the proper binding index in the stack' do
outp = StringIO.new
redirect_pry_io(InputTester.new("cd 1", "cd 2", "jump-to 1", "$blah = self", "exit-all"), outp) do
Pry.start(0)
end
$blah.should == 1
end
it 'should print error when trying to jump to a non-existent binding index' do
outp = StringIO.new
redirect_pry_io(InputTester.new("cd 1", "cd 2", "jump-to 100", "exit-all"), outp) do
Pry.start(0)
end
outp.string.should =~ /Invalid nest level/
end
it 'should print error when trying to jump to the same binding index' do
outp = StringIO.new
redirect_pry_io(InputTester.new("cd 1", "cd 2", "jump-to 2", "exit-all"), outp) do
Pry.new.repl(0)
end
outp.string.should =~ /Already/
end
end
describe "exit-program" do
it 'should raise SystemExit' do
redirect_pry_io(InputTester.new("exit-program"), StringIO.new) do
lambda { Pry.new.repl(0).should == 0 }.should.raise SystemExit
end
end
it 'should exit the program with the provided value' do
redirect_pry_io(InputTester.new("exit-program 66"), StringIO.new) do
begin
Pry.new.repl(0)
rescue SystemExit => e
e.status.should == 66
end
end
end
end
describe "cd" do
after do
$obj = nil
end
it 'should cd into simple input' do
b = Pry.binding_for(Object.new)
b.eval("x = :mon_ouie")
redirect_pry_io(InputTester.new("cd x", "$obj = self", "exit-all"), StringIO.new) do
b.pry
end
$obj.should == :mon_ouie
end
it 'should break out of session with cd ..' do
b = Pry.binding_for(:outer)
b.eval("x = :inner")
redirect_pry_io(InputTester.new("cd x", "$inner = self;", "cd ..", "$outer = self", "exit-all"), StringIO.new) do
b.pry
end
$inner.should == :inner
$outer.should == :outer
end
it 'should break out of the repl loop of Pry instance when binding_stack has only one binding with cd ..' do
# redirect_pry_io(InputTester.new("ls"), StringIO.new) do
# o = Pry.new.tap { |v| v.repl(0) }
# end
Pry.start(0, :input => StringIO.new("cd ..")).should == 0
end
it 'should break out to outer-most session with cd /' do
b = Pry.binding_for(:outer)
b.eval("x = :inner")
redirect_pry_io(InputTester.new("cd x", "$inner = self;", "cd 5", "$five = self", "cd /", "$outer = self", "exit-all"), StringIO.new) do
b.pry
end
$inner.should == :inner
$five.should == 5
$outer.should == :outer
end
it 'should break out to outer-most session with just cd (no args)' do
b = Pry.binding_for(:outer)
b.eval("x = :inner")
redirect_pry_io(InputTester.new("cd x", "$inner = self;", "cd 5", "$five = self", "cd", "$outer = self", "exit-all"), StringIO.new) do
b.pry
end
$inner.should == :inner
$five.should == 5
$outer.should == :outer
end
it 'should start a session on TOPLEVEL_BINDING with cd ::' do
b = Pry.binding_for(:outer)
redirect_pry_io(InputTester.new("cd ::", "$obj = self", "exit-all"), StringIO.new) do
5.pry
end
$obj.should == TOPLEVEL_BINDING.eval('self')
end
it 'should cd into complex input (with spaces)' do
o = Object.new
def o.hello(x, y, z)
:mon_ouie
end
redirect_pry_io(InputTester.new("cd hello 1, 2, 3", "$obj = self", "exit-all"), StringIO.new) do
o.pry
end
$obj.should == :mon_ouie
end
end
end