2011-05-06 06:44:59 -04:00
|
|
|
require 'helper'
|
|
|
|
|
2011-05-18 23:28:38 -04:00
|
|
|
describe "Pry::Commands" do
|
|
|
|
|
2011-05-22 20:30:36 -04:00
|
|
|
after do
|
|
|
|
$obj = nil
|
|
|
|
end
|
|
|
|
|
2011-05-22 19:57:34 -04:00
|
|
|
describe "hist" do
|
|
|
|
before do
|
2011-05-24 22:09:31 -04:00
|
|
|
Readline::HISTORY.shift until Readline::HISTORY.empty?
|
2011-05-22 19:57:34 -04:00
|
|
|
@hist = Readline::HISTORY
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should display the correct history' do
|
2011-05-24 22:09:31 -04:00
|
|
|
@hist.push "'bug in 1.8 means this line is ignored'" if RUBY_VERSION =~ /1.8/
|
2011-05-22 19:57:34 -04:00
|
|
|
@hist.push "hello"
|
|
|
|
@hist.push "world"
|
|
|
|
str_output = StringIO.new
|
|
|
|
redirect_pry_io(InputTester.new("hist", "exit-all"), str_output) do
|
|
|
|
pry
|
|
|
|
end
|
|
|
|
str_output.string.should =~ /hello\n.*world/
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should replay history correctly (single item)' do
|
2011-05-24 22:09:31 -04:00
|
|
|
@hist.push "'bug in 1.8 means this line is ignored'" if RUBY_VERSION =~ /1.8/
|
2011-05-22 19:57:34 -04:00
|
|
|
@hist.push "cd :hello"
|
|
|
|
str_output = StringIO.new
|
|
|
|
redirect_pry_io(InputTester.new("hist --replay 0", "self", "exit-all"), str_output) do
|
|
|
|
pry
|
|
|
|
end
|
|
|
|
str_output.string.should =~ /hello/
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should replay a range of history correctly (range of items)' do
|
2011-05-24 22:09:31 -04:00
|
|
|
@hist.push "'bug in 1.8 means this line is ignored'" if RUBY_VERSION =~ /1.8/
|
2011-05-22 19:57:34 -04:00
|
|
|
@hist.push ":hello"
|
|
|
|
@hist.push ":carl"
|
|
|
|
str_output = StringIO.new
|
|
|
|
redirect_pry_io(InputTester.new("hist --replay 0..1", "exit-all"), str_output) do
|
|
|
|
pry
|
|
|
|
end
|
|
|
|
str_output.string.should =~ /:hello\n.*:carl/
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2011-05-18 23:28:38 -04:00
|
|
|
describe "show-method" do
|
|
|
|
it 'should output a method\'s source' do
|
|
|
|
str_output = StringIO.new
|
2011-05-22 19:57:34 -04:00
|
|
|
redirect_pry_io(InputTester.new("show-method sample_method", "exit-all"), str_output) do
|
2011-05-18 23:28:38 -04:00
|
|
|
Pry.new.repl(TOPLEVEL_BINDING)
|
|
|
|
end
|
|
|
|
|
|
|
|
str_output.string.should =~ /def sample/
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should output a method\'s source with line numbers' do
|
|
|
|
str_output = StringIO.new
|
2011-05-22 19:57:34 -04:00
|
|
|
redirect_pry_io(InputTester.new("show-method -l sample_method", "exit-all"), str_output) do
|
2011-05-18 23:28:38 -04:00
|
|
|
Pry.new.repl(TOPLEVEL_BINDING)
|
|
|
|
end
|
|
|
|
|
|
|
|
str_output.string.should =~ /\d+: def sample/
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should output a method\'s source if inside method without needing to use method name' do
|
|
|
|
$str_output = StringIO.new
|
|
|
|
|
|
|
|
o = Object.new
|
|
|
|
def o.sample
|
2011-05-22 19:57:34 -04:00
|
|
|
redirect_pry_io(InputTester.new("show-method", "exit-all"), $str_output) do
|
2011-05-18 23:28:38 -04:00
|
|
|
binding.pry
|
|
|
|
end
|
|
|
|
end
|
|
|
|
o.sample
|
|
|
|
|
|
|
|
$str_output.string.should =~ /def o.sample/
|
|
|
|
$str_output = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should output a method\'s source if inside method without needing to use method name, and using the -l switch' do
|
|
|
|
$str_output = StringIO.new
|
|
|
|
|
|
|
|
o = Object.new
|
|
|
|
def o.sample
|
2011-05-22 19:57:34 -04:00
|
|
|
redirect_pry_io(InputTester.new("show-method -l", "exit-all"), $str_output) do
|
2011-05-18 23:28:38 -04:00
|
|
|
binding.pry
|
|
|
|
end
|
|
|
|
end
|
|
|
|
o.sample
|
|
|
|
|
|
|
|
$str_output.string.should =~ /\d+: def o.sample/
|
|
|
|
$str_output = nil
|
|
|
|
end
|
|
|
|
|
2011-05-24 22:09:31 -04:00
|
|
|
if RUBY_VERSION =~ /1.9/
|
|
|
|
it 'should output a method\'s source for a method defined inside pry' do
|
|
|
|
str_output = StringIO.new
|
|
|
|
redirect_pry_io(InputTester.new("def dyna_method", ":testing", "end", "show-method dyna_method"), str_output) do
|
|
|
|
Pry.new.repl(TOPLEVEL_BINDING)
|
|
|
|
end
|
|
|
|
|
|
|
|
str_output.string.should =~ /def dyna_method/
|
|
|
|
Object.remove_method :dyna_method
|
2011-05-24 10:57:18 -04:00
|
|
|
end
|
|
|
|
|
2011-05-24 22:09:31 -04:00
|
|
|
it 'should output a method\'s source for a method defined inside pry, even if exceptions raised before hand' do
|
|
|
|
str_output = StringIO.new
|
|
|
|
redirect_pry_io(InputTester.new("bad code", "123", "bad code 2", "1 + 2", "def dyna_method", ":testing", "end", "show-method dyna_method"), str_output) do
|
|
|
|
Pry.new.repl(TOPLEVEL_BINDING)
|
|
|
|
end
|
2011-05-24 10:57:18 -04:00
|
|
|
|
2011-05-24 22:09:31 -04:00
|
|
|
str_output.string.should =~ /def dyna_method/
|
|
|
|
Object.remove_method :dyna_method
|
2011-05-24 10:57:18 -04:00
|
|
|
end
|
|
|
|
|
2011-05-24 22:09:31 -04:00
|
|
|
it 'should output an instance method\'s source for a method defined inside pry' do
|
|
|
|
str_output = StringIO.new
|
|
|
|
redirect_pry_io(InputTester.new("class A", "def yo", "end", "end", "show-method A#yo"), str_output) do
|
|
|
|
Pry.new.repl(TOPLEVEL_BINDING)
|
|
|
|
end
|
2011-05-24 10:57:18 -04:00
|
|
|
|
2011-05-24 22:09:31 -04:00
|
|
|
str_output.string.should =~ /def yo/
|
|
|
|
Object.remove_const :A
|
2011-05-24 10:57:18 -04:00
|
|
|
end
|
|
|
|
|
2011-05-24 22:09:31 -04:00
|
|
|
it 'should output an instance method\'s source for a method defined inside pry using define_method' do
|
|
|
|
str_output = StringIO.new
|
|
|
|
redirect_pry_io(InputTester.new("class A", "define_method(:yup) {}", "end", "end", "show-method A#yup"), str_output) do
|
|
|
|
Pry.new.repl(TOPLEVEL_BINDING)
|
|
|
|
end
|
2011-05-24 10:57:18 -04:00
|
|
|
|
2011-05-24 22:09:31 -04:00
|
|
|
str_output.string.should =~ /define_method\(:yup\)/
|
|
|
|
Object.remove_const :A
|
2011-05-24 10:57:18 -04:00
|
|
|
end
|
|
|
|
end
|
2011-05-18 23:28:38 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "show-doc" do
|
|
|
|
it 'should output a method\'s documentation' do
|
|
|
|
str_output = StringIO.new
|
2011-05-22 19:57:34 -04:00
|
|
|
redirect_pry_io(InputTester.new("show-doc sample_method", "exit-all"), str_output) do
|
2011-05-18 23:28:38 -04:00
|
|
|
Pry.new.repl(TOPLEVEL_BINDING)
|
|
|
|
end
|
|
|
|
|
|
|
|
str_output.string.should =~ /sample doc/
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should output a method\'s documentation if inside method without needing to use method name' do
|
|
|
|
$str_output = StringIO.new
|
|
|
|
|
|
|
|
o = Object.new
|
|
|
|
def o.sample
|
2011-05-22 19:57:34 -04:00
|
|
|
redirect_pry_io(InputTester.new("show-doc", "exit-all"), $str_output) do
|
2011-05-18 23:28:38 -04:00
|
|
|
binding.pry
|
|
|
|
end
|
|
|
|
end
|
|
|
|
o.sample
|
|
|
|
|
|
|
|
$str_output.string.should =~ /sample doc/
|
|
|
|
$str_output = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2011-05-06 06:44:59 -04:00
|
|
|
describe "cd" do
|
|
|
|
it 'should cd into simple input' do
|
|
|
|
b = Pry.binding_for(Object.new)
|
|
|
|
b.eval("x = :mon_ouie")
|
|
|
|
|
2011-05-22 20:30:36 -04:00
|
|
|
redirect_pry_io(InputTester.new("cd x", "$obj = self", "exit-all"), StringIO.new) do
|
2011-05-18 23:28:38 -04:00
|
|
|
Pry.new.repl(b)
|
2011-05-06 06:44:59 -04:00
|
|
|
end
|
2011-05-22 20:30:36 -04:00
|
|
|
|
|
|
|
$obj.should == :mon_ouie
|
2011-05-18 23:28:38 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should break out of session with cd ..' do
|
|
|
|
b = Pry.binding_for(:outer)
|
|
|
|
b.eval("x = :inner")
|
2011-05-06 06:44:59 -04:00
|
|
|
|
2011-05-22 20:30:36 -04:00
|
|
|
redirect_pry_io(InputTester.new("cd x", "$inner = self;", "cd ..", "$outer = self", "exit-all"), StringIO.new) do
|
2011-05-18 23:28:38 -04:00
|
|
|
Pry.new.repl(b)
|
|
|
|
end
|
2011-05-22 20:30:36 -04:00
|
|
|
$inner.should == :inner
|
|
|
|
$outer.should == :outer
|
2011-05-18 23:28:38 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should break out to outer-most session with cd /' do
|
|
|
|
b = Pry.binding_for(:outer)
|
|
|
|
b.eval("x = :inner")
|
|
|
|
|
2011-05-22 20:30:36 -04:00
|
|
|
redirect_pry_io(InputTester.new("cd x", "$inner = self;", "cd 5", "$five = self", "cd /", "$outer = self", "exit-all"), StringIO.new) do
|
2011-05-18 23:28:38 -04:00
|
|
|
Pry.new.repl(b)
|
|
|
|
end
|
2011-05-22 20:30:36 -04:00
|
|
|
$inner.should == :inner
|
|
|
|
$five.should == 5
|
|
|
|
$outer.should == :outer
|
2011-05-18 23:28:38 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should start a session on TOPLEVEL_BINDING with cd ::' do
|
|
|
|
b = Pry.binding_for(:outer)
|
|
|
|
|
2011-05-22 20:30:36 -04:00
|
|
|
redirect_pry_io(InputTester.new("cd ::", "$obj = self", "exit-all"), StringIO.new) do
|
2011-05-18 23:28:38 -04:00
|
|
|
Pry.new.repl(5)
|
|
|
|
end
|
2011-05-22 20:30:36 -04:00
|
|
|
$obj.should == TOPLEVEL_BINDING.eval('self')
|
2011-05-06 06:44:59 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should cd into complex input (with spaces)' do
|
|
|
|
o = Object.new
|
|
|
|
def o.hello(x, y, z)
|
|
|
|
:mon_ouie
|
|
|
|
end
|
|
|
|
|
2011-05-22 20:30:36 -04:00
|
|
|
redirect_pry_io(InputTester.new("cd hello 1, 2, 3", "$obj = self", "exit-all"), StringIO.new) do
|
2011-05-18 23:28:38 -04:00
|
|
|
Pry.new.repl(o)
|
2011-05-06 06:44:59 -04:00
|
|
|
end
|
2011-05-22 20:30:36 -04:00
|
|
|
$obj.should == :mon_ouie
|
2011-05-06 06:44:59 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|