-
Notifications
You must be signed in to change notification settings - Fork 0
/
pry_defaults_spec.rb
414 lines (330 loc) · 13.5 KB
/
pry_defaults_spec.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
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
require_relative 'helper'
_version = 1
describe "test Pry defaults" do
before do
@str_output = StringIO.new
end
after do
Pry.reset_defaults
Pry.config.color = false
end
describe "input" do
it 'should set the input default, and the default should be overridable' do
Pry.config.input = InputTester.new("5")
Pry.config.output = @str_output
Object.new.pry
expect(@str_output.string).to match(/5/)
Pry.config.output = @str_output
Object.new.pry input: InputTester.new("6")
expect(@str_output.string).to match(/6/)
end
it 'should pass in the prompt if readline arity is 1' do
Pry.prompt = proc { "A" }
arity_one_input = Class.new do
attr_accessor :prompt
def readline(prompt)
@prompt = prompt
"exit-all"
end
end.new
Pry.start(self, input: arity_one_input, output: StringIO.new)
expect(arity_one_input.prompt).to eq Pry.prompt.call
end
it 'should not pass in the prompt if the arity is 0' do
Pry.prompt = proc { "A" }
arity_zero_input = Class.new do
def readline
"exit-all"
end
end.new
expect { Pry.start(self, input: arity_zero_input, output: StringIO.new) }.to_not raise_error
end
it 'should not pass in the prompt if the arity is -1' do
Pry.prompt = proc { "A" }
arity_multi_input = Class.new do
attr_accessor :prompt
def readline(*args)
@prompt = args.first
"exit-all"
end
end.new
Pry.start(self, input: arity_multi_input, output: StringIO.new)
expect(arity_multi_input.prompt).to eq nil
end
end
it 'should set the output default, and the default should be overridable' do
Pry.config.output = @str_output
Pry.config.input = InputTester.new("5")
Object.new.pry
expect(@str_output.string).to match(/5/)
Pry.config.input = InputTester.new("6")
Object.new.pry
expect(@str_output.string).to match(/5\n.*6/)
Pry.config.input = InputTester.new("7")
@str_output = StringIO.new
Object.new.pry output: @str_output
expect(@str_output.string).not_to match(/5\n.*6/)
expect(@str_output.string).to match(/7/)
end
it "should set the print default, and the default should be overridable" do
new_print = proc { |out, _value| out.puts "=> LOL" }
Pry.config.print = new_print
expect(Pry.new.print).to eq Pry.config.print
Object.new.pry input: InputTester.new("\"test\""), output: @str_output
expect(@str_output.string).to eq "=> LOL\n"
@str_output = StringIO.new
Object.new.pry input: InputTester.new("\"test\""), output: @str_output,
print: proc { |out, value| out.puts value.reverse }
expect(@str_output.string).to eq "tset\n"
expect(Pry.new.print).to eq Pry.config.print
@str_output = StringIO.new
Object.new.pry input: InputTester.new("\"test\""), output: @str_output
expect(@str_output.string).to eq "=> LOL\n"
end
describe "pry return values" do
it 'should return nil' do
expect(Pry.start(self, input: StringIO.new("exit-all"), output: StringIO.new)).to eq nil
end
it 'should return the parameter given to exit-all' do
expect(Pry.start(self, input: StringIO.new("exit-all 10"), output: StringIO.new)).to eq 10
end
it 'should return the parameter (multi word string) given to exit-all' do
expect(Pry.start(self, input: StringIO.new("exit-all \"john mair\""), output: StringIO.new)).to eq "john mair"
end
it 'should return the parameter (function call) given to exit-all' do
expect(Pry.start(self, input: StringIO.new("exit-all 'abc'.reverse"), output: StringIO.new)).to eq 'cba'
end
it 'should return the parameter (self) given to exit-all' do
expect(Pry.start("carl", input: StringIO.new("exit-all self"), output: StringIO.new)).to eq "carl"
end
end
describe "prompts" do
before do
Pry.config.output = StringIO.new
end
def get_prompts(pry)
a = pry.select_prompt
pry.eval "["
b = pry.select_prompt
pry.eval "]"
[a, b]
end
it 'should set the prompt default, and the default should be overridable (single prompt)' do
Pry.prompt = Pry::Prompt.new(:test, '', Array.new(2) { proc { '>' } })
new_prompt = Pry::Prompt.new(:new_test, '', Array.new(2) { proc { 'A' } })
pry = Pry.new
expect(pry.prompt).to eq Pry.prompt
expect(get_prompts(pry)).to eq(%w[> >])
pry = Pry.new(prompt: new_prompt)
expect(pry.prompt).to eq(new_prompt)
expect(get_prompts(pry)).to eq(%w[A A])
pry = Pry.new
expect(pry.prompt).to eq Pry.prompt
expect(get_prompts(pry)).to eq(%w[> >])
end
it 'should set the prompt default, and the default should be overridable (multi prompt)' do
Pry.prompt = Pry::Prompt.new(:test, '', [proc { '>' }, proc { '*' }])
new_prompt = Pry::Prompt.new(:new_test, '', [proc { 'A' }, proc { 'B' }])
pry = Pry.new
expect(pry.prompt).to eq Pry.prompt
expect(get_prompts(pry)).to eq(%w[> *])
pry = Pry.new(prompt: new_prompt)
expect(pry.prompt).to eq(new_prompt)
expect(get_prompts(pry)).to eq(%w[A B])
pry = Pry.new
expect(pry.prompt).to eq(Pry.prompt)
expect(get_prompts(pry)).to eq(%w[> *])
end
describe 'storing and restoring the prompt' do
let(:prompt1) { Pry::Prompt.new(:test1, '', Array.new(2) { proc { '' } }) }
let(:prompt2) { Pry::Prompt.new(:test2, '', Array.new(2) { proc { '' } }) }
let(:prompt3) { Pry::Prompt.new(:test3, '', Array.new(2) { proc { '' } }) }
let(:pry) { Pry.new(prompt: prompt1) }
it 'should have a prompt stack' do
pry.push_prompt(prompt2)
pry.push_prompt(prompt3)
expect(pry.prompt).to eq(prompt3)
pry.pop_prompt
expect(pry.prompt).to match(prompt2)
pry.pop_prompt
expect(pry.prompt).to eq(prompt1)
end
it 'should restore overridden prompts when returning from shell-mode' do
pry = Pry.new(
prompt: Pry::Prompt.new(:test, '', Array.new(2) { proc { 'P>' } })
)
expect(pry.select_prompt).to eq('P>')
pry.process_command('shell-mode')
expect(pry.select_prompt).to match(/\Apry .* \$ \z/)
pry.process_command('shell-mode')
expect(pry.select_prompt).to eq('P>')
end
it '#pop_prompt should return the popped prompt' do
pry.push_prompt(prompt2)
pry.push_prompt(prompt3)
expect(pry.pop_prompt).to eq(prompt3)
expect(pry.pop_prompt).to eq(prompt2)
end
it 'should not pop the last prompt' do
pry.push_prompt(prompt2)
expect(pry.pop_prompt).to eq(prompt2)
expect(pry.pop_prompt).to eq(prompt1)
expect(pry.pop_prompt).to eq(prompt1)
expect(pry.prompt).to eq(prompt1)
end
describe '#prompt= should replace the current prompt with the new prompt' do
it 'when only one prompt on the stack' do
pry.prompt = prompt2
expect(pry.prompt).to eq(prompt2)
expect(pry.pop_prompt).to eq(prompt2)
expect(pry.pop_prompt).to eq(prompt2)
end
it 'when several prompts on the stack' do
pry.push_prompt(prompt2)
pry.prompt = prompt3
expect(pry.pop_prompt).to eq(prompt3)
expect(pry.pop_prompt).to eq(prompt1)
end
end
end
end
describe "view_clip used for displaying an object in a truncated format" do
DEFAULT_OPTIONS = {
max_length: 60
}
MAX_LENGTH = DEFAULT_OPTIONS[:max_length]
describe "given an object with an #inspect string" do
it "returns the #<> format of the object (never use inspect)" do
o = Object.new
def o.inspect; "a" * MAX_LENGTH; end
expect(Pry.view_clip(o, DEFAULT_OPTIONS)).to match(/#<Object/)
end
end
describe "given the 'main' object" do
it "returns the #to_s of main (special case)" do
o = TOPLEVEL_BINDING.eval('self')
expect(Pry.view_clip(o, DEFAULT_OPTIONS)).to eq o.to_s
end
end
describe "the list of prompt safe objects" do
[1, 2.0, -5, "hello", :test].each do |o|
it "returns the #inspect of the special-cased immediate object: #{o}" do
expect(Pry.view_clip(o, DEFAULT_OPTIONS)).to eq o.inspect
end
end
it "returns #<> format of the special-cased immediate object if #inspect is longer than maximum" do
o = "o" * (MAX_LENGTH + 1)
expect(Pry.view_clip(o, DEFAULT_OPTIONS)).to match(/#<String/)
end
it "returns the #inspect of the custom prompt safe objects" do
Barbie = Class.new { def inspect; "life is plastic, it's fantastic" end }
Pry.config.prompt_safe_contexts << Barbie
output = Pry.view_clip(Barbie.new, DEFAULT_OPTIONS)
expect(output).to eq "life is plastic, it's fantastic"
end
end
describe "given an object with an #inspect string as long as the maximum specified" do
it "returns the #<> format of the object (never use inspect)" do
o = Object.new
def o.inspect; "a" * DEFAULT_OPTIONS; end
expect(Pry.view_clip(o, DEFAULT_OPTIONS)).to match(/#<Object/)
end
end
describe "given a regular object with an #inspect string longer than the maximum specified" do
describe "when the object is a regular one" do
it "returns a string of the #<class name:object idish> format" do
o = Object.new
def o.inspect; "a" * (DEFAULT_OPTIONS + 1); end
expect(Pry.view_clip(o, DEFAULT_OPTIONS)).to match(/#<Object/)
end
end
describe "when the object is a Class or a Module" do
describe "without a name (usually a c = Class.new)" do
it "returns a string of the #<class name:object idish> format" do
c, m = Class.new, Module.new
expect(Pry.view_clip(c, DEFAULT_OPTIONS)).to match(/#<Class/)
expect(Pry.view_clip(m, DEFAULT_OPTIONS)).to match(/#<Module/)
end
end
describe "with a #name longer than the maximum specified" do
it "returns a string of the #<class name:object idish> format" do
c, m = Class.new, Module.new
def c.name; "a" * (MAX_LENGTH + 1); end
def m.name; "a" * (MAX_LENGTH + 1); end
expect(Pry.view_clip(c, DEFAULT_OPTIONS)).to match(/#<Class/)
expect(Pry.view_clip(m, DEFAULT_OPTIONS)).to match(/#<Module/)
end
end
describe "with a #name shorter than or equal to the maximum specified" do
it "returns a string of the #<class name:object idish> format" do
c, m = Class.new, Module.new
def c.name; "a" * MAX_LENGTH; end
def m.name; "a" * MAX_LENGTH; end
expect(Pry.view_clip(c, DEFAULT_OPTIONS)).to eq c.name
expect(Pry.view_clip(m, DEFAULT_OPTIONS)).to eq m.name
end
end
end
end
end
describe 'quiet' do
it 'should show whereami by default' do
Pry.start(binding, input: InputTester.new("1", "exit-all"),
output: @str_output,
hooks: Pry::DEFAULT_HOOKS)
expect(@str_output.string).to match(/[w]hereami by default/)
end
it 'should hide whereami if quiet is set' do
Pry.new(input: InputTester.new("exit-all"),
output: @str_output,
quiet: true,
hooks: Pry::DEFAULT_HOOKS)
expect(@str_output.string).to eq ""
end
end
describe 'toplevel_binding' do
it 'should be devoid of local variables' do
expect(pry_eval(Pry.toplevel_binding, "ls -l")).not_to match(/_version/)
end
it 'should have self the same as TOPLEVEL_BINDING' do
expect(Pry.toplevel_binding.eval('self')).to equal(TOPLEVEL_BINDING.eval('self'))
end
it 'should define private methods on Object' do
TOPLEVEL_BINDING.eval 'def gooey_fooey; end'
expect(method(:gooey_fooey).owner).to eq Object
expect(Pry::Method(method(:gooey_fooey)).visibility).to eq :private
end
end
it 'should set the hooks default, and the default should be overridable' do
Pry.config.input = InputTester.new("exit-all")
Pry.config.hooks = Pry::Hooks.new.
add_hook(:before_session, :my_name) { |out,_,_| out.puts "HELLO" }.
add_hook(:after_session, :my_name) { |out,_,_| out.puts "BYE" }
Object.new.pry output: @str_output
expect(@str_output.string).to match(/HELLO/)
expect(@str_output.string).to match(/BYE/)
Pry.config.input.rewind
@str_output = StringIO.new
Object.new.pry output: @str_output,
hooks: Pry::Hooks.new.
add_hook( :before_session, :my_name) { |out,_,_| out.puts "MORNING" }.
add_hook(:after_session, :my_name) { |out,_,_| out.puts "EVENING" }
expect(@str_output.string).to match(/MORNING/)
expect(@str_output.string).to match(/EVENING/)
# try below with just defining one hook
Pry.config.input.rewind
@str_output = StringIO.new
Object.new.pry output: @str_output,
hooks: Pry::Hooks.new.
add_hook(:before_session, :my_name) { |out,_,_| out.puts "OPEN" }
expect(@str_output.string).to match(/OPEN/)
Pry.config.input.rewind
@str_output = StringIO.new
Object.new.pry output: @str_output,
hooks: Pry::Hooks.new.
add_hook(:after_session, :my_name) { |out,_,_| out.puts "CLOSE" }
expect(@str_output.string).to match(/CLOSE/)
Pry.reset_defaults
Pry.config.color = false
end
end