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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
|
# frozen_string_literal: true
require 'method_source'
describe "whereami" do
it 'should work with methods that have been undefined' do
class Cor
def blimey!
Cor.send :undef_method, :blimey!
Pad.binding = binding
end
end
Cor.new.blimey!
# using [.] so the regex doesn't match itself
expect(pry_eval(Pad.binding, 'whereami')).to match(/self[.]blimey!/)
Object.remove_const(:Cor)
end
it 'should work in objects with no method methods' do
class Cor
def blimey!
pry_eval(binding, 'whereami')
end
def method
"moo"
end
end
expect(Cor.new.blimey!).to match(/Cor[#]blimey!/)
Object.remove_const(:Cor)
end
it 'should properly set _file_, _line_ and _dir_' do
class Cor
def blimey!
pry_eval(binding, 'whereami', '_file_')
end
end
expect(Cor.new.blimey!).to eq File.expand_path(__FILE__)
Object.remove_const(:Cor)
end
if RUBY_VERSION > "2.0.0"
it 'should work with prepended methods' do
module Cor2
def blimey!
super
end
end
class Cor
prepend Cor2
def blimey!
pry_eval(binding, 'whereami')
end
end
expect(Cor.new.blimey!).to match(/Cor2[#]blimey!/)
Object.remove_const(:Cor)
Object.remove_const(:Cor2)
end
end
it 'should work in BasicObjects' do
cor = Class.new(BasicObject) do
def blimey!
::Kernel.binding # omnom
end
end.new.blimey!
expect(pry_eval(cor, 'whereami')).to match(/::Kernel.binding [#] omnom/)
end
it(
'shows description and corrects code when __LINE__ and __FILE__ are ' \
'outside @method.source_location'
) do
class Cor
def blimey!
eval(<<-WHEREAMI, binding, 'spec/fixtures/example.erb', 1)
pry_eval(binding, 'whereami')
WHEREAMI
end
end
expect(Cor.instance_method(:blimey!).source).to match(/pry_eval/)
expect(Cor.new.blimey!).to match(/Cor#blimey!.*Look at me/m)
Object.remove_const(:Cor)
end
it(
'shows description and corrects code when @method.source_location ' \
'would raise an error'
) do
class Cor
eval <<-WHEREAMI, binding, "spec/fixtures/example.erb", 1
def blimey!
pry_eval(binding, 'whereami')
end
WHEREAMI
end
expect { Cor.instance_method(:blimey!).source }
.to raise_error MethodSource::SourceNotFoundError
expect(Cor.new.blimey!).to match(/Cor#blimey!.*Look at me/m)
Object.remove_const(:Cor)
end
# Now that we use stagger_output (paging output) we no longer get
# the "From: " line, as we output everything in one go (not separate output.puts)
# and so the user just gets a single `Error: Cannot open
# "not.found.file.erb" for reading.`
# which is good enough IMO. Unfortunately we can't test for it
# though, as we don't hook stdout.
#
# it 'should display a description and error if reading the file goes wrong' do
# class Cor
# def blimey!
# eval <<-END, binding, "not.found.file.erb", 7
# Pad.tester = pry_tester(binding)
# Pad.tester.eval('whereami')
# END
# end
# end
# proc { Cor.new.blimey! }.should.raise(MethodSource::SourceNotFoundError)
# Pad.tester.last_output.should =~
# /From: not.found.file.erb:7 Cor#blimey!/
# Object.remove_const(:Cor)
# end
it 'should show code window (not just method source) if parameter passed to whereami' do
class Cor
def blimey!
pry_eval(binding, 'whereami 3')
end
end
expect(Cor.new.blimey!).to match(/class Cor/)
Object.remove_const(:Cor)
end
it 'should show entire method when -m option used' do
old_size = Pry.config.default_window_size
Pry.config.default_window_size = 1
old_cutoff = Pry::Command::Whereami.method_size_cutoff
Pry::Command::Whereami.method_size_cutoff = 1
class Cor
def blimey!
@foo = 1
@bar = 2
pry_eval(binding, 'whereami -m')
end
end
Pry::Command::Whereami.method_size_cutoff = old_cutoff
Pry.config.default_window_size = old_size
result = Cor.new.blimey!
Object.remove_const(:Cor)
expect(result).to match(/def blimey/)
end
it 'should show entire file when -f option used' do
class Cor
def blimey!
pry_eval(binding, 'whereami -f')
end
end
result = Cor.new.blimey!
Object.remove_const(:Cor)
expect(result).to match(/show entire file when -f option used/)
end
describe "-c" do
it 'should show class when -c option used, and locate correct candidate' do
require 'fixtures/whereami_helper'
class Cor
def blimey!
pry_eval(binding, 'whereami -c')
end
end
out = Cor.new.blimey!
Object.remove_const(:Cor)
expect(out).to match(/class Cor/)
expect(out).to match(/blimey/)
end
it 'should show class when -c option used, and locate correct superclass' do
class Cor
def blimey!
pry_eval(binding, 'whereami -c')
end
end
class Horse < Cor
def pig; end
end
out = Horse.new.blimey!
Object.remove_const(:Cor)
Object.remove_const(:Horse)
expect(out).to match(/class Cor/)
expect(out).to match(/blimey/)
end
it 'should show class when -c option used, and binding is outside a method' do
class Cor
extend RSpec::Matchers
def blimey; end
out = pry_eval(binding, 'whereami -c')
expect(out).to match(/class Cor/)
expect(out).to match(/blimey/)
end
Object.remove_const(:Cor)
end
it 'should show class when -c option used, and beginning of the class is on the' \
'same line as another expression' do
out = class Cor
def blimey; end
pry_eval(binding, 'whereami -c')
end
expect(out).to match(/class Cor/)
expect(out).to match(/blimey/)
Object.remove_const(:Cor)
end
end
it 'should not show line numbers or marker when -n switch is used' do
class Cor
def blimey!
pry_eval(binding, 'whereami -n')
end
end
out = Cor.new.blimey!
expect(out).to match(/^\s*def/)
expect(out).to_not match(/\=\>/)
Object.remove_const(:Cor)
end
it(
'uses Pry.config.default_window_size for window size when outside a method context'
) do
old_size = Pry.config.default_window_size
Pry.config.default_window_size = 1
_foo = :litella
_foo = :pig
out = pry_eval(binding, 'whereami')
_foo = :punk
_foo = :sanders
expect(out).not_to match(/:litella/)
expect(out).to match(/:pig/)
expect(out).to match(/:punk/)
expect(out).not_to match(/:sanders/)
Pry.config.default_window_size = old_size
end
it "should work at the top level" do
expect(pry_eval(Pry.toplevel_binding, 'whereami')).to match(
/At the top level/
)
end
it "should work inside a class" do
expect(pry_eval(Pry, 'whereami')).to match(/Inside Pry/)
end
it "should work inside an object" do
expect(pry_eval(Object.new, 'whereami')).to match(/Inside #<Object/)
end
end
|