2014-03-14 00:31:24 -04:00
|
|
|
require_relative '../helper'
|
2012-04-05 15:01:45 -04:00
|
|
|
|
2012-09-15 17:50:15 -04:00
|
|
|
describe 'cd' do
|
2012-06-27 07:54:07 -04:00
|
|
|
before do
|
2012-06-27 11:01:29 -04:00
|
|
|
@o, @obj = Object.new, Object.new
|
|
|
|
@obj.instance_variable_set(:@x, 66)
|
|
|
|
@obj.instance_variable_set(:@y, 79)
|
|
|
|
@o.instance_variable_set(:@obj, @obj)
|
2012-06-27 07:54:07 -04:00
|
|
|
|
2012-07-24 04:36:53 -04:00
|
|
|
@t = pry_tester(@o) do
|
|
|
|
def assert_binding_stack(other)
|
|
|
|
binding_stack.map { |b| b.eval('self') }.should == other
|
|
|
|
end
|
|
|
|
|
|
|
|
def binding_stack
|
2013-12-07 21:23:51 -05:00
|
|
|
pry.binding_stack.dup
|
2012-07-24 04:36:53 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def command_state
|
2013-12-07 21:23:51 -05:00
|
|
|
pry.command_state["cd"]
|
2012-07-24 04:36:53 -04:00
|
|
|
end
|
|
|
|
|
2012-08-17 02:31:03 -04:00
|
|
|
def old_stack
|
2013-12-07 21:23:51 -05:00
|
|
|
pry.command_state['cd'].old_stack.dup
|
2012-08-17 02:31:03 -04:00
|
|
|
end
|
2012-07-24 04:36:53 -04:00
|
|
|
end
|
2012-06-10 09:31:06 -04:00
|
|
|
end
|
|
|
|
|
2012-06-15 19:10:56 -04:00
|
|
|
describe 'state' do
|
|
|
|
it 'should not to be set up in fresh instance' do
|
2012-07-24 04:36:53 -04:00
|
|
|
@t.command_state.should.be.nil
|
2012-06-15 19:10:56 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-06-27 07:54:07 -04:00
|
|
|
describe 'old stack toggling with `cd -`' do
|
|
|
|
describe 'in fresh pry instance' do
|
|
|
|
it 'should not toggle when there is no old stack' do
|
2012-07-23 04:03:05 -04:00
|
|
|
2.times do
|
2012-07-24 04:36:53 -04:00
|
|
|
@t.eval 'cd -'
|
|
|
|
@t.assert_binding_stack [@o]
|
2012-06-15 19:10:56 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-08-17 02:31:03 -04:00
|
|
|
describe 'when an error was raised' do
|
|
|
|
it 'should not toggle and should keep correct stacks' do
|
|
|
|
proc {
|
2013-07-26 19:31:33 -04:00
|
|
|
@t.eval 'cd %'
|
2012-08-17 02:31:03 -04:00
|
|
|
}.should.raise(Pry::CommandError)
|
2012-06-15 19:10:56 -04:00
|
|
|
|
2012-08-17 02:31:03 -04:00
|
|
|
@t.old_stack.should == []
|
|
|
|
@t.assert_binding_stack [@o]
|
2012-06-15 19:10:56 -04:00
|
|
|
|
2012-08-17 02:31:03 -04:00
|
|
|
@t.eval 'cd -'
|
|
|
|
@t.old_stack.should == []
|
|
|
|
@t.assert_binding_stack [@o]
|
|
|
|
end
|
|
|
|
end
|
2012-06-15 19:10:56 -04:00
|
|
|
|
2012-06-27 07:54:07 -04:00
|
|
|
describe 'when using simple cd syntax' do
|
|
|
|
it 'should toggle' do
|
2012-07-24 04:36:53 -04:00
|
|
|
@t.eval 'cd :mon_dogg', 'cd -'
|
|
|
|
@t.assert_binding_stack [@o]
|
2012-06-15 19:10:56 -04:00
|
|
|
|
2012-07-24 04:36:53 -04:00
|
|
|
@t.eval 'cd -'
|
|
|
|
@t.assert_binding_stack [@o, :mon_dogg]
|
2012-06-15 19:10:56 -04:00
|
|
|
end
|
2012-06-27 07:54:07 -04:00
|
|
|
end
|
2012-06-15 19:10:56 -04:00
|
|
|
|
2012-06-27 07:54:07 -04:00
|
|
|
describe "when using complex cd syntax" do
|
|
|
|
it 'should toggle with a complex path (simple case)' do
|
2012-07-24 04:36:53 -04:00
|
|
|
@t.eval 'cd 1/2/3', 'cd -'
|
|
|
|
@t.assert_binding_stack [@o]
|
2012-06-15 19:10:56 -04:00
|
|
|
|
2012-07-24 04:36:53 -04:00
|
|
|
@t.eval 'cd -'
|
|
|
|
@t.assert_binding_stack [@o, 1, 2, 3]
|
2012-06-15 19:10:56 -04:00
|
|
|
end
|
|
|
|
|
2012-06-27 07:54:07 -04:00
|
|
|
it 'should toggle with a complex path (more complex case)' do
|
2012-07-24 04:36:53 -04:00
|
|
|
@t.eval 'cd 1/2/3', 'cd ../4', 'cd -'
|
|
|
|
@t.assert_binding_stack [@o, 1, 2, 3]
|
2012-06-15 19:10:56 -04:00
|
|
|
|
2012-07-24 04:36:53 -04:00
|
|
|
@t.eval 'cd -'
|
|
|
|
@t.assert_binding_stack [@o, 1, 2, 4]
|
2012-06-15 19:10:56 -04:00
|
|
|
end
|
2012-06-27 07:54:07 -04:00
|
|
|
end
|
2012-06-15 19:10:56 -04:00
|
|
|
|
2012-06-27 07:54:07 -04:00
|
|
|
describe 'series of cd calls' do
|
2012-06-15 19:10:56 -04:00
|
|
|
it 'should toggle with fuzzy `cd -` calls' do
|
2012-07-24 04:36:53 -04:00
|
|
|
@t.eval 'cd :mon_dogg', 'cd -', 'cd 42', 'cd -'
|
|
|
|
@t.assert_binding_stack [@o]
|
2012-06-15 19:10:56 -04:00
|
|
|
|
2012-07-24 04:36:53 -04:00
|
|
|
@t.eval 'cd -'
|
|
|
|
@t.assert_binding_stack [@o, 42]
|
2012-06-15 19:10:56 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'when using cd ..' do
|
2012-06-27 07:54:07 -04:00
|
|
|
it 'should toggle with a simple path' do
|
2012-07-24 04:36:53 -04:00
|
|
|
@t.eval 'cd :john_dogg', 'cd ..'
|
|
|
|
@t.assert_binding_stack [@o]
|
2012-06-15 19:10:56 -04:00
|
|
|
|
2012-07-24 04:36:53 -04:00
|
|
|
@t.eval 'cd -'
|
|
|
|
@t.assert_binding_stack [@o, :john_dogg]
|
2012-06-15 19:10:56 -04:00
|
|
|
end
|
|
|
|
|
2012-06-27 07:54:07 -04:00
|
|
|
it 'should toggle with a complex path' do
|
2012-07-24 04:36:53 -04:00
|
|
|
@t.eval 'cd 1/2/3/../4', 'cd -'
|
|
|
|
@t.assert_binding_stack [@o]
|
2012-06-15 19:10:56 -04:00
|
|
|
|
2012-07-24 04:36:53 -04:00
|
|
|
@t.eval 'cd -'
|
|
|
|
@t.assert_binding_stack [@o, 1, 2, 4]
|
2012-06-15 19:10:56 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'when using cd ::' do
|
2012-06-27 07:54:07 -04:00
|
|
|
it 'should toggle' do
|
2012-07-24 04:36:53 -04:00
|
|
|
@t.eval 'cd ::', 'cd -'
|
|
|
|
@t.assert_binding_stack [@o]
|
2012-06-15 19:10:56 -04:00
|
|
|
|
2012-07-24 04:36:53 -04:00
|
|
|
@t.eval 'cd -'
|
|
|
|
@t.assert_binding_stack [@o, TOPLEVEL_BINDING.eval('self')]
|
2012-06-15 19:10:56 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'when using cd /' do
|
2012-06-27 07:54:07 -04:00
|
|
|
it 'should toggle' do
|
2012-07-24 04:36:53 -04:00
|
|
|
@t.eval 'cd /', 'cd -'
|
|
|
|
@t.assert_binding_stack [@o]
|
2012-06-15 19:10:56 -04:00
|
|
|
|
2012-07-24 04:36:53 -04:00
|
|
|
@t.eval 'cd :john_dogg', 'cd /', 'cd -'
|
|
|
|
@t.assert_binding_stack [@o, :john_dogg]
|
2012-06-15 19:10:56 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'when using ^D (Control-D) key press' do
|
|
|
|
it 'should keep correct old binding' do
|
2012-07-24 04:36:53 -04:00
|
|
|
@t.eval 'cd :john_dogg', 'cd :mon_dogg', 'cd :kyr_dogg',
|
|
|
|
'Pry::DEFAULT_CONTROL_D_HANDLER.call("", _pry_)'
|
|
|
|
@t.assert_binding_stack [@o, :john_dogg, :mon_dogg]
|
2012-07-23 04:03:05 -04:00
|
|
|
|
2012-07-24 04:36:53 -04:00
|
|
|
@t.eval 'cd -'
|
|
|
|
@t.assert_binding_stack [@o, :john_dogg, :mon_dogg, :kyr_dogg]
|
2012-06-15 19:10:56 -04:00
|
|
|
|
2012-07-24 04:36:53 -04:00
|
|
|
@t.eval 'cd -'
|
|
|
|
@t.assert_binding_stack [@o, :john_dogg, :mon_dogg]
|
2012-06-15 19:10:56 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-06-10 09:31:06 -04:00
|
|
|
it 'should cd into simple input' do
|
2012-07-24 04:36:53 -04:00
|
|
|
@t.eval 'cd :mon_ouie'
|
|
|
|
@t.eval('self').should == :mon_ouie
|
2012-06-10 09:31:06 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should break out of session with cd ..' do
|
2012-07-24 04:36:53 -04:00
|
|
|
@t.eval 'cd :outer', 'cd :inner'
|
|
|
|
@t.eval('self').should == :inner
|
2012-06-27 11:01:29 -04:00
|
|
|
|
2012-07-24 04:36:53 -04:00
|
|
|
@t.eval 'cd ..'
|
|
|
|
@t.eval('self').should == :outer
|
2012-06-10 09:31:06 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should not leave the REPL session when given 'cd ..'" do
|
2012-07-24 04:36:53 -04:00
|
|
|
@t.eval 'cd ..'
|
|
|
|
@t.eval('self').should == @o
|
2012-06-10 09:31:06 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should break out to outer-most session with cd /' do
|
2012-07-24 04:36:53 -04:00
|
|
|
@t.eval 'cd :inner'
|
|
|
|
@t.eval('self').should == :inner
|
2012-06-27 11:01:29 -04:00
|
|
|
|
2012-07-24 04:36:53 -04:00
|
|
|
@t.eval 'cd 5'
|
|
|
|
@t.eval('self').should == 5
|
2012-07-23 04:03:05 -04:00
|
|
|
|
2012-07-24 04:36:53 -04:00
|
|
|
@t.eval 'cd /'
|
|
|
|
@t.eval('self').should == @o
|
2012-06-10 09:31:06 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should break out to outer-most session with just cd (no args)' do
|
2012-07-24 04:36:53 -04:00
|
|
|
@t.eval 'cd :inner'
|
|
|
|
@t.eval('self').should == :inner
|
2012-06-27 11:01:29 -04:00
|
|
|
|
2012-07-24 04:36:53 -04:00
|
|
|
@t.eval 'cd 5'
|
|
|
|
@t.eval('self').should == 5
|
2012-07-23 04:03:05 -04:00
|
|
|
|
2012-07-24 04:36:53 -04:00
|
|
|
@t.eval 'cd'
|
|
|
|
@t.eval('self').should == @o
|
2012-06-10 09:31:06 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should cd into an object and its ivar using cd obj/@ivar syntax' do
|
2012-07-24 04:36:53 -04:00
|
|
|
@t.eval 'cd @obj/@x'
|
|
|
|
@t.assert_binding_stack [@o, @obj, 66]
|
2012-06-10 09:31:06 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should cd into an object and its ivar using cd obj/@ivar/ syntax (note following /)' do
|
2012-07-24 04:36:53 -04:00
|
|
|
@t.eval 'cd @obj/@x/'
|
|
|
|
@t.assert_binding_stack [@o, @obj, 66]
|
2012-06-10 09:31:06 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should cd into previous object and its local using cd ../local syntax' do
|
2012-07-24 04:36:53 -04:00
|
|
|
@t.eval 'cd @obj', 'local = :local', 'cd @x', 'cd ../local'
|
|
|
|
@t.assert_binding_stack [@o, @obj, :local]
|
2012-06-10 09:31:06 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should cd into an object and its ivar and back again using cd obj/@ivar/.. syntax' do
|
2012-07-24 04:36:53 -04:00
|
|
|
@t.eval 'cd @obj/@x/..'
|
|
|
|
@t.assert_binding_stack [@o, @obj]
|
2012-06-10 09:31:06 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should cd into an object and its ivar and back and then into another ivar using cd obj/@ivar/../@y syntax' do
|
2012-07-24 04:36:53 -04:00
|
|
|
@t.eval 'cd @obj/@x/../@y'
|
|
|
|
@t.assert_binding_stack [@o, @obj, 79]
|
2012-06-10 09:31:06 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should cd back to top-level and then into another ivar using cd /@ivar/ syntax' do
|
2012-07-24 04:36:53 -04:00
|
|
|
@t.eval '@z = 20', 'cd @obj/@x/', 'cd /@z'
|
|
|
|
@t.assert_binding_stack [@o, 20]
|
2012-06-10 09:31:06 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should start a session on TOPLEVEL_BINDING with cd ::' do
|
2012-07-24 04:36:53 -04:00
|
|
|
@t.eval 'cd ::'
|
|
|
|
@t.eval('self').should == TOPLEVEL_BINDING.eval('self')
|
2012-06-10 09:31:06 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should cd into complex input (with spaces)' do
|
2012-06-27 11:01:29 -04:00
|
|
|
def @o.hello(x, y, z)
|
2012-06-10 09:31:06 -04:00
|
|
|
:mon_ouie
|
|
|
|
end
|
|
|
|
|
2012-07-24 04:36:53 -04:00
|
|
|
@t.eval 'cd hello 1, 2, 3'
|
|
|
|
@t.eval('self').should == :mon_ouie
|
2012-06-10 09:31:06 -04:00
|
|
|
end
|
|
|
|
|
2012-06-30 12:59:23 -04:00
|
|
|
it 'should not cd into complex input when it encounters an exception' do
|
2012-07-23 04:03:05 -04:00
|
|
|
proc {
|
2012-07-24 04:36:53 -04:00
|
|
|
@t.eval 'cd 1/2/swoop_a_doop/3'
|
2012-07-23 04:03:05 -04:00
|
|
|
}.should.raise(Pry::CommandError)
|
2012-06-30 12:59:23 -04:00
|
|
|
|
2012-07-24 04:36:53 -04:00
|
|
|
@t.assert_binding_stack [@o]
|
2012-06-30 12:59:23 -04:00
|
|
|
end
|
|
|
|
|
2014-04-28 03:08:23 -04:00
|
|
|
it 'can cd into an expression containing a string with slashes in it' do
|
|
|
|
@t.eval 'cd ["http://google.com"]'
|
|
|
|
@t.eval('self').should == ["http://google.com"]
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'can cd into an expression with division in it' do
|
|
|
|
@t.eval 'cd (10/2)/even?'
|
|
|
|
@t.eval('self').should == false
|
|
|
|
end
|
|
|
|
|
2012-06-10 09:31:06 -04:00
|
|
|
# Regression test for ticket #516.
|
2012-07-23 04:03:05 -04:00
|
|
|
# FIXME: This is actually broken.
|
|
|
|
# it 'should be able to cd into the Object BasicObject' do
|
|
|
|
# proc {
|
2012-07-24 04:36:53 -04:00
|
|
|
# @t.eval 'cd BasicObject.new'
|
2012-07-23 04:03:05 -04:00
|
|
|
# }.should.not.raise
|
|
|
|
# end
|
2012-04-05 15:01:45 -04:00
|
|
|
end
|