1
0
Fork 0
mirror of https://github.com/pry/pry.git synced 2022-11-09 12:35:05 -05:00
pry--pry/spec/commands/cd_spec.rb

262 lines
7.2 KiB
Ruby
Raw Normal View History

describe 'cd' do
Change behavior of `cd -` command Since banister begged me to do that... completely rewrite `cd -` command (implemetation is much simpler now). This commit brings such changes: * completely rewrite behavior of `cd -` command; * implement ScratchPad aka Pad for unit testing purposes (by banister); * use Pad riches in the unit tests for `cd -` command; * remove verbose and clunky unit tests; This commit brings new meaning to the `cd -` command. The main difference is that the new command saves entire binding stack, not just the last binding. Let me show you an example of the variance between these two implemetations: * Old `cd -` implementation saves *only* last binding. With our next `cd -` invocation our interjacent results are lost: [1] pry(main)> cd 1/2/3/../4 [2] pry(4):3> cd - [3] pry(main)> cd - [4] pry(4):1> nesting Nesting status: -- 0. main (Pry top level) 1. 4 Also, there are a few bugs in old `cd -` command: * you type `cd :foo`, `cd 1/2/3` and `cd -`. The last command relocates you to the scope of `3` (leaves you where you was), when `:foo` is expected; * you type `cd :foo`, `cd 1/2/3/../4`, `cd -`. The last command relocates you to the scope of `3`, when `:foo` is expected. * New and shiny `cd -` is devoid of those shortcomings: [1] pry(main)> cd 1/2/3/../4 [2] pry(4):3> cd - [3] pry(main)> cd - [4] pry(4):3> nesting Nesting status: -- 0. main (Pry top level) 1. 1 2. 2 3. 4 As I said before, this solution is *much* simpler and less error-prone. Signed-off-by: Kyrylo Silin <kyrylosilin@gmail.com>
2012-06-27 07:54:07 -04:00
before do
@o = Object.new
@obj = Object.new
@obj.instance_variable_set(:@x, 66)
@obj.instance_variable_set(:@y, 79)
@o.instance_variable_set(:@obj, @obj)
Change behavior of `cd -` command Since banister begged me to do that... completely rewrite `cd -` command (implemetation is much simpler now). This commit brings such changes: * completely rewrite behavior of `cd -` command; * implement ScratchPad aka Pad for unit testing purposes (by banister); * use Pad riches in the unit tests for `cd -` command; * remove verbose and clunky unit tests; This commit brings new meaning to the `cd -` command. The main difference is that the new command saves entire binding stack, not just the last binding. Let me show you an example of the variance between these two implemetations: * Old `cd -` implementation saves *only* last binding. With our next `cd -` invocation our interjacent results are lost: [1] pry(main)> cd 1/2/3/../4 [2] pry(4):3> cd - [3] pry(main)> cd - [4] pry(4):1> nesting Nesting status: -- 0. main (Pry top level) 1. 4 Also, there are a few bugs in old `cd -` command: * you type `cd :foo`, `cd 1/2/3` and `cd -`. The last command relocates you to the scope of `3` (leaves you where you was), when `:foo` is expected; * you type `cd :foo`, `cd 1/2/3/../4`, `cd -`. The last command relocates you to the scope of `3`, when `:foo` is expected. * New and shiny `cd -` is devoid of those shortcomings: [1] pry(main)> cd 1/2/3/../4 [2] pry(4):3> cd - [3] pry(main)> cd - [4] pry(4):3> nesting Nesting status: -- 0. main (Pry top level) 1. 1 2. 2 3. 4 As I said before, this solution is *much* simpler and less error-prone. Signed-off-by: Kyrylo Silin <kyrylosilin@gmail.com>
2012-06-27 07:54:07 -04:00
2012-07-24 04:36:53 -04:00
@t = pry_tester(@o) do
2015-01-23 04:30:41 -05:00
def mapped_binding_stack
binding_stack.map { |b| b.eval('self') }
2012-07-24 04:36:53 -04:00
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
end
describe 'state' do
it 'should not to be set up in fresh instance' do
2015-03-10 16:49:29 -04:00
expect(@t.command_state).to equal nil
end
end
Change behavior of `cd -` command Since banister begged me to do that... completely rewrite `cd -` command (implemetation is much simpler now). This commit brings such changes: * completely rewrite behavior of `cd -` command; * implement ScratchPad aka Pad for unit testing purposes (by banister); * use Pad riches in the unit tests for `cd -` command; * remove verbose and clunky unit tests; This commit brings new meaning to the `cd -` command. The main difference is that the new command saves entire binding stack, not just the last binding. Let me show you an example of the variance between these two implemetations: * Old `cd -` implementation saves *only* last binding. With our next `cd -` invocation our interjacent results are lost: [1] pry(main)> cd 1/2/3/../4 [2] pry(4):3> cd - [3] pry(main)> cd - [4] pry(4):1> nesting Nesting status: -- 0. main (Pry top level) 1. 4 Also, there are a few bugs in old `cd -` command: * you type `cd :foo`, `cd 1/2/3` and `cd -`. The last command relocates you to the scope of `3` (leaves you where you was), when `:foo` is expected; * you type `cd :foo`, `cd 1/2/3/../4`, `cd -`. The last command relocates you to the scope of `3`, when `:foo` is expected. * New and shiny `cd -` is devoid of those shortcomings: [1] pry(main)> cd 1/2/3/../4 [2] pry(4):3> cd - [3] pry(main)> cd - [4] pry(4):3> nesting Nesting status: -- 0. main (Pry top level) 1. 1 2. 2 3. 4 As I said before, this solution is *much* simpler and less error-prone. Signed-off-by: Kyrylo Silin <kyrylosilin@gmail.com>
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
2.times do
2012-07-24 04:36:53 -04:00
@t.eval 'cd -'
2015-03-10 16:49:29 -04:00
expect(@t.mapped_binding_stack).to eq [@o]
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
expect { @t.eval 'cd %' }.to raise_error Pry::CommandError
2015-03-10 16:49:29 -04:00
expect(@t.old_stack).to eq []
expect(@t.mapped_binding_stack).to eq [@o]
2012-08-17 02:31:03 -04:00
@t.eval 'cd -'
2015-03-10 16:49:29 -04:00
expect(@t.old_stack).to eq []
expect(@t.mapped_binding_stack).to eq [@o]
2012-08-17 02:31:03 -04:00
end
end
Change behavior of `cd -` command Since banister begged me to do that... completely rewrite `cd -` command (implemetation is much simpler now). This commit brings such changes: * completely rewrite behavior of `cd -` command; * implement ScratchPad aka Pad for unit testing purposes (by banister); * use Pad riches in the unit tests for `cd -` command; * remove verbose and clunky unit tests; This commit brings new meaning to the `cd -` command. The main difference is that the new command saves entire binding stack, not just the last binding. Let me show you an example of the variance between these two implemetations: * Old `cd -` implementation saves *only* last binding. With our next `cd -` invocation our interjacent results are lost: [1] pry(main)> cd 1/2/3/../4 [2] pry(4):3> cd - [3] pry(main)> cd - [4] pry(4):1> nesting Nesting status: -- 0. main (Pry top level) 1. 4 Also, there are a few bugs in old `cd -` command: * you type `cd :foo`, `cd 1/2/3` and `cd -`. The last command relocates you to the scope of `3` (leaves you where you was), when `:foo` is expected; * you type `cd :foo`, `cd 1/2/3/../4`, `cd -`. The last command relocates you to the scope of `3`, when `:foo` is expected. * New and shiny `cd -` is devoid of those shortcomings: [1] pry(main)> cd 1/2/3/../4 [2] pry(4):3> cd - [3] pry(main)> cd - [4] pry(4):3> nesting Nesting status: -- 0. main (Pry top level) 1. 1 2. 2 3. 4 As I said before, this solution is *much* simpler and less error-prone. Signed-off-by: Kyrylo Silin <kyrylosilin@gmail.com>
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 -'
2015-03-10 16:49:29 -04:00
expect(@t.mapped_binding_stack).to eq [@o]
2012-07-24 04:36:53 -04:00
@t.eval 'cd -'
2015-03-10 16:49:29 -04:00
expect(@t.mapped_binding_stack).to eq [@o, :mon_dogg]
end
Change behavior of `cd -` command Since banister begged me to do that... completely rewrite `cd -` command (implemetation is much simpler now). This commit brings such changes: * completely rewrite behavior of `cd -` command; * implement ScratchPad aka Pad for unit testing purposes (by banister); * use Pad riches in the unit tests for `cd -` command; * remove verbose and clunky unit tests; This commit brings new meaning to the `cd -` command. The main difference is that the new command saves entire binding stack, not just the last binding. Let me show you an example of the variance between these two implemetations: * Old `cd -` implementation saves *only* last binding. With our next `cd -` invocation our interjacent results are lost: [1] pry(main)> cd 1/2/3/../4 [2] pry(4):3> cd - [3] pry(main)> cd - [4] pry(4):1> nesting Nesting status: -- 0. main (Pry top level) 1. 4 Also, there are a few bugs in old `cd -` command: * you type `cd :foo`, `cd 1/2/3` and `cd -`. The last command relocates you to the scope of `3` (leaves you where you was), when `:foo` is expected; * you type `cd :foo`, `cd 1/2/3/../4`, `cd -`. The last command relocates you to the scope of `3`, when `:foo` is expected. * New and shiny `cd -` is devoid of those shortcomings: [1] pry(main)> cd 1/2/3/../4 [2] pry(4):3> cd - [3] pry(main)> cd - [4] pry(4):3> nesting Nesting status: -- 0. main (Pry top level) 1. 1 2. 2 3. 4 As I said before, this solution is *much* simpler and less error-prone. Signed-off-by: Kyrylo Silin <kyrylosilin@gmail.com>
2012-06-27 07:54:07 -04:00
end
Change behavior of `cd -` command Since banister begged me to do that... completely rewrite `cd -` command (implemetation is much simpler now). This commit brings such changes: * completely rewrite behavior of `cd -` command; * implement ScratchPad aka Pad for unit testing purposes (by banister); * use Pad riches in the unit tests for `cd -` command; * remove verbose and clunky unit tests; This commit brings new meaning to the `cd -` command. The main difference is that the new command saves entire binding stack, not just the last binding. Let me show you an example of the variance between these two implemetations: * Old `cd -` implementation saves *only* last binding. With our next `cd -` invocation our interjacent results are lost: [1] pry(main)> cd 1/2/3/../4 [2] pry(4):3> cd - [3] pry(main)> cd - [4] pry(4):1> nesting Nesting status: -- 0. main (Pry top level) 1. 4 Also, there are a few bugs in old `cd -` command: * you type `cd :foo`, `cd 1/2/3` and `cd -`. The last command relocates you to the scope of `3` (leaves you where you was), when `:foo` is expected; * you type `cd :foo`, `cd 1/2/3/../4`, `cd -`. The last command relocates you to the scope of `3`, when `:foo` is expected. * New and shiny `cd -` is devoid of those shortcomings: [1] pry(main)> cd 1/2/3/../4 [2] pry(4):3> cd - [3] pry(main)> cd - [4] pry(4):3> nesting Nesting status: -- 0. main (Pry top level) 1. 1 2. 2 3. 4 As I said before, this solution is *much* simpler and less error-prone. Signed-off-by: Kyrylo Silin <kyrylosilin@gmail.com>
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 -'
2015-03-10 16:49:29 -04:00
expect(@t.mapped_binding_stack).to eq [@o]
2012-07-24 04:36:53 -04:00
@t.eval 'cd -'
2015-03-10 16:49:29 -04:00
expect(@t.mapped_binding_stack).to eq [@o, 1, 2, 3]
end
Change behavior of `cd -` command Since banister begged me to do that... completely rewrite `cd -` command (implemetation is much simpler now). This commit brings such changes: * completely rewrite behavior of `cd -` command; * implement ScratchPad aka Pad for unit testing purposes (by banister); * use Pad riches in the unit tests for `cd -` command; * remove verbose and clunky unit tests; This commit brings new meaning to the `cd -` command. The main difference is that the new command saves entire binding stack, not just the last binding. Let me show you an example of the variance between these two implemetations: * Old `cd -` implementation saves *only* last binding. With our next `cd -` invocation our interjacent results are lost: [1] pry(main)> cd 1/2/3/../4 [2] pry(4):3> cd - [3] pry(main)> cd - [4] pry(4):1> nesting Nesting status: -- 0. main (Pry top level) 1. 4 Also, there are a few bugs in old `cd -` command: * you type `cd :foo`, `cd 1/2/3` and `cd -`. The last command relocates you to the scope of `3` (leaves you where you was), when `:foo` is expected; * you type `cd :foo`, `cd 1/2/3/../4`, `cd -`. The last command relocates you to the scope of `3`, when `:foo` is expected. * New and shiny `cd -` is devoid of those shortcomings: [1] pry(main)> cd 1/2/3/../4 [2] pry(4):3> cd - [3] pry(main)> cd - [4] pry(4):3> nesting Nesting status: -- 0. main (Pry top level) 1. 1 2. 2 3. 4 As I said before, this solution is *much* simpler and less error-prone. Signed-off-by: Kyrylo Silin <kyrylosilin@gmail.com>
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 -'
2015-03-10 16:49:29 -04:00
expect(@t.mapped_binding_stack).to eq [@o, 1, 2, 3]
2012-07-24 04:36:53 -04:00
@t.eval 'cd -'
2015-03-10 16:49:29 -04:00
expect(@t.mapped_binding_stack).to eq [@o, 1, 2, 4]
end
Change behavior of `cd -` command Since banister begged me to do that... completely rewrite `cd -` command (implemetation is much simpler now). This commit brings such changes: * completely rewrite behavior of `cd -` command; * implement ScratchPad aka Pad for unit testing purposes (by banister); * use Pad riches in the unit tests for `cd -` command; * remove verbose and clunky unit tests; This commit brings new meaning to the `cd -` command. The main difference is that the new command saves entire binding stack, not just the last binding. Let me show you an example of the variance between these two implemetations: * Old `cd -` implementation saves *only* last binding. With our next `cd -` invocation our interjacent results are lost: [1] pry(main)> cd 1/2/3/../4 [2] pry(4):3> cd - [3] pry(main)> cd - [4] pry(4):1> nesting Nesting status: -- 0. main (Pry top level) 1. 4 Also, there are a few bugs in old `cd -` command: * you type `cd :foo`, `cd 1/2/3` and `cd -`. The last command relocates you to the scope of `3` (leaves you where you was), when `:foo` is expected; * you type `cd :foo`, `cd 1/2/3/../4`, `cd -`. The last command relocates you to the scope of `3`, when `:foo` is expected. * New and shiny `cd -` is devoid of those shortcomings: [1] pry(main)> cd 1/2/3/../4 [2] pry(4):3> cd - [3] pry(main)> cd - [4] pry(4):3> nesting Nesting status: -- 0. main (Pry top level) 1. 1 2. 2 3. 4 As I said before, this solution is *much* simpler and less error-prone. Signed-off-by: Kyrylo Silin <kyrylosilin@gmail.com>
2012-06-27 07:54:07 -04:00
end
Change behavior of `cd -` command Since banister begged me to do that... completely rewrite `cd -` command (implemetation is much simpler now). This commit brings such changes: * completely rewrite behavior of `cd -` command; * implement ScratchPad aka Pad for unit testing purposes (by banister); * use Pad riches in the unit tests for `cd -` command; * remove verbose and clunky unit tests; This commit brings new meaning to the `cd -` command. The main difference is that the new command saves entire binding stack, not just the last binding. Let me show you an example of the variance between these two implemetations: * Old `cd -` implementation saves *only* last binding. With our next `cd -` invocation our interjacent results are lost: [1] pry(main)> cd 1/2/3/../4 [2] pry(4):3> cd - [3] pry(main)> cd - [4] pry(4):1> nesting Nesting status: -- 0. main (Pry top level) 1. 4 Also, there are a few bugs in old `cd -` command: * you type `cd :foo`, `cd 1/2/3` and `cd -`. The last command relocates you to the scope of `3` (leaves you where you was), when `:foo` is expected; * you type `cd :foo`, `cd 1/2/3/../4`, `cd -`. The last command relocates you to the scope of `3`, when `:foo` is expected. * New and shiny `cd -` is devoid of those shortcomings: [1] pry(main)> cd 1/2/3/../4 [2] pry(4):3> cd - [3] pry(main)> cd - [4] pry(4):3> nesting Nesting status: -- 0. main (Pry top level) 1. 1 2. 2 3. 4 As I said before, this solution is *much* simpler and less error-prone. Signed-off-by: Kyrylo Silin <kyrylosilin@gmail.com>
2012-06-27 07:54:07 -04:00
describe 'series of cd calls' do
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 -'
2015-03-10 16:49:29 -04:00
expect(@t.mapped_binding_stack).to eq [@o]
2012-07-24 04:36:53 -04:00
@t.eval 'cd -'
2015-03-10 16:49:29 -04:00
expect(@t.mapped_binding_stack).to eq [@o, 42]
end
end
describe 'when using cd ..' do
Change behavior of `cd -` command Since banister begged me to do that... completely rewrite `cd -` command (implemetation is much simpler now). This commit brings such changes: * completely rewrite behavior of `cd -` command; * implement ScratchPad aka Pad for unit testing purposes (by banister); * use Pad riches in the unit tests for `cd -` command; * remove verbose and clunky unit tests; This commit brings new meaning to the `cd -` command. The main difference is that the new command saves entire binding stack, not just the last binding. Let me show you an example of the variance between these two implemetations: * Old `cd -` implementation saves *only* last binding. With our next `cd -` invocation our interjacent results are lost: [1] pry(main)> cd 1/2/3/../4 [2] pry(4):3> cd - [3] pry(main)> cd - [4] pry(4):1> nesting Nesting status: -- 0. main (Pry top level) 1. 4 Also, there are a few bugs in old `cd -` command: * you type `cd :foo`, `cd 1/2/3` and `cd -`. The last command relocates you to the scope of `3` (leaves you where you was), when `:foo` is expected; * you type `cd :foo`, `cd 1/2/3/../4`, `cd -`. The last command relocates you to the scope of `3`, when `:foo` is expected. * New and shiny `cd -` is devoid of those shortcomings: [1] pry(main)> cd 1/2/3/../4 [2] pry(4):3> cd - [3] pry(main)> cd - [4] pry(4):3> nesting Nesting status: -- 0. main (Pry top level) 1. 1 2. 2 3. 4 As I said before, this solution is *much* simpler and less error-prone. Signed-off-by: Kyrylo Silin <kyrylosilin@gmail.com>
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 ..'
2015-03-10 16:49:29 -04:00
expect(@t.mapped_binding_stack).to eq [@o]
2012-07-24 04:36:53 -04:00
@t.eval 'cd -'
2015-03-10 16:49:29 -04:00
expect(@t.mapped_binding_stack).to eq [@o, :john_dogg]
end
Change behavior of `cd -` command Since banister begged me to do that... completely rewrite `cd -` command (implemetation is much simpler now). This commit brings such changes: * completely rewrite behavior of `cd -` command; * implement ScratchPad aka Pad for unit testing purposes (by banister); * use Pad riches in the unit tests for `cd -` command; * remove verbose and clunky unit tests; This commit brings new meaning to the `cd -` command. The main difference is that the new command saves entire binding stack, not just the last binding. Let me show you an example of the variance between these two implemetations: * Old `cd -` implementation saves *only* last binding. With our next `cd -` invocation our interjacent results are lost: [1] pry(main)> cd 1/2/3/../4 [2] pry(4):3> cd - [3] pry(main)> cd - [4] pry(4):1> nesting Nesting status: -- 0. main (Pry top level) 1. 4 Also, there are a few bugs in old `cd -` command: * you type `cd :foo`, `cd 1/2/3` and `cd -`. The last command relocates you to the scope of `3` (leaves you where you was), when `:foo` is expected; * you type `cd :foo`, `cd 1/2/3/../4`, `cd -`. The last command relocates you to the scope of `3`, when `:foo` is expected. * New and shiny `cd -` is devoid of those shortcomings: [1] pry(main)> cd 1/2/3/../4 [2] pry(4):3> cd - [3] pry(main)> cd - [4] pry(4):3> nesting Nesting status: -- 0. main (Pry top level) 1. 1 2. 2 3. 4 As I said before, this solution is *much* simpler and less error-prone. Signed-off-by: Kyrylo Silin <kyrylosilin@gmail.com>
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 -'
2015-03-10 16:49:29 -04:00
expect(@t.mapped_binding_stack).to eq [@o]
2012-07-24 04:36:53 -04:00
@t.eval 'cd -'
2015-03-10 16:49:29 -04:00
expect(@t.mapped_binding_stack).to eq [@o, 1, 2, 4]
end
end
describe 'when using cd ::' do
Change behavior of `cd -` command Since banister begged me to do that... completely rewrite `cd -` command (implemetation is much simpler now). This commit brings such changes: * completely rewrite behavior of `cd -` command; * implement ScratchPad aka Pad for unit testing purposes (by banister); * use Pad riches in the unit tests for `cd -` command; * remove verbose and clunky unit tests; This commit brings new meaning to the `cd -` command. The main difference is that the new command saves entire binding stack, not just the last binding. Let me show you an example of the variance between these two implemetations: * Old `cd -` implementation saves *only* last binding. With our next `cd -` invocation our interjacent results are lost: [1] pry(main)> cd 1/2/3/../4 [2] pry(4):3> cd - [3] pry(main)> cd - [4] pry(4):1> nesting Nesting status: -- 0. main (Pry top level) 1. 4 Also, there are a few bugs in old `cd -` command: * you type `cd :foo`, `cd 1/2/3` and `cd -`. The last command relocates you to the scope of `3` (leaves you where you was), when `:foo` is expected; * you type `cd :foo`, `cd 1/2/3/../4`, `cd -`. The last command relocates you to the scope of `3`, when `:foo` is expected. * New and shiny `cd -` is devoid of those shortcomings: [1] pry(main)> cd 1/2/3/../4 [2] pry(4):3> cd - [3] pry(main)> cd - [4] pry(4):3> nesting Nesting status: -- 0. main (Pry top level) 1. 1 2. 2 3. 4 As I said before, this solution is *much* simpler and less error-prone. Signed-off-by: Kyrylo Silin <kyrylosilin@gmail.com>
2012-06-27 07:54:07 -04:00
it 'should toggle' do
2012-07-24 04:36:53 -04:00
@t.eval 'cd ::', 'cd -'
2015-03-10 16:49:29 -04:00
expect(@t.mapped_binding_stack).to eq [@o]
2012-07-24 04:36:53 -04:00
@t.eval 'cd -'
2015-03-10 16:49:29 -04:00
expect(@t.mapped_binding_stack).to eq [@o, TOPLEVEL_BINDING.eval('self')]
end
end
describe 'when using cd /' do
Change behavior of `cd -` command Since banister begged me to do that... completely rewrite `cd -` command (implemetation is much simpler now). This commit brings such changes: * completely rewrite behavior of `cd -` command; * implement ScratchPad aka Pad for unit testing purposes (by banister); * use Pad riches in the unit tests for `cd -` command; * remove verbose and clunky unit tests; This commit brings new meaning to the `cd -` command. The main difference is that the new command saves entire binding stack, not just the last binding. Let me show you an example of the variance between these two implemetations: * Old `cd -` implementation saves *only* last binding. With our next `cd -` invocation our interjacent results are lost: [1] pry(main)> cd 1/2/3/../4 [2] pry(4):3> cd - [3] pry(main)> cd - [4] pry(4):1> nesting Nesting status: -- 0. main (Pry top level) 1. 4 Also, there are a few bugs in old `cd -` command: * you type `cd :foo`, `cd 1/2/3` and `cd -`. The last command relocates you to the scope of `3` (leaves you where you was), when `:foo` is expected; * you type `cd :foo`, `cd 1/2/3/../4`, `cd -`. The last command relocates you to the scope of `3`, when `:foo` is expected. * New and shiny `cd -` is devoid of those shortcomings: [1] pry(main)> cd 1/2/3/../4 [2] pry(4):3> cd - [3] pry(main)> cd - [4] pry(4):3> nesting Nesting status: -- 0. main (Pry top level) 1. 1 2. 2 3. 4 As I said before, this solution is *much* simpler and less error-prone. Signed-off-by: Kyrylo Silin <kyrylosilin@gmail.com>
2012-06-27 07:54:07 -04:00
it 'should toggle' do
2012-07-24 04:36:53 -04:00
@t.eval 'cd /', 'cd -'
2015-03-10 16:49:29 -04:00
expect(@t.mapped_binding_stack).to eq [@o]
2012-07-24 04:36:53 -04:00
@t.eval 'cd :john_dogg', 'cd /', 'cd -'
2015-03-10 16:49:29 -04:00
expect(@t.mapped_binding_stack).to eq [@o, :john_dogg]
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::Config.defaults.control_d_handler.call("", _pry_)'
2015-03-10 16:49:29 -04:00
expect(@t.mapped_binding_stack).to eq [@o, :john_dogg, :mon_dogg]
2012-07-24 04:36:53 -04:00
@t.eval 'cd -'
2015-03-10 16:49:29 -04:00
expect(@t.mapped_binding_stack).to eq [@o, :john_dogg, :mon_dogg, :kyr_dogg]
2012-07-24 04:36:53 -04:00
@t.eval 'cd -'
2015-03-10 16:49:29 -04:00
expect(@t.mapped_binding_stack).to eq [@o, :john_dogg, :mon_dogg]
end
end
end
it 'should cd into simple input' do
2012-07-24 04:36:53 -04:00
@t.eval 'cd :mon_ouie'
2015-03-10 16:49:29 -04:00
expect(@t.eval('self')).to eq :mon_ouie
end
it 'should break out of session with cd ..' do
2012-07-24 04:36:53 -04:00
@t.eval 'cd :outer', 'cd :inner'
2015-03-10 16:49:29 -04:00
expect(@t.eval('self')).to eq :inner
2012-07-24 04:36:53 -04:00
@t.eval 'cd ..'
2015-03-10 16:49:29 -04:00
expect(@t.eval('self')).to eq :outer
end
it "should not leave the REPL session when given 'cd ..'" do
2012-07-24 04:36:53 -04:00
@t.eval 'cd ..'
2015-03-10 16:49:29 -04:00
expect(@t.eval('self')).to eq @o
end
it 'should break out to outer-most session with cd /' do
2012-07-24 04:36:53 -04:00
@t.eval 'cd :inner'
2015-03-10 16:49:29 -04:00
expect(@t.eval('self')).to eq :inner
2012-07-24 04:36:53 -04:00
@t.eval 'cd 5'
2015-03-10 16:49:29 -04:00
expect(@t.eval('self')).to eq 5
2012-07-24 04:36:53 -04:00
@t.eval 'cd /'
2015-03-10 16:49:29 -04:00
expect(@t.eval('self')).to eq @o
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'
2015-03-10 16:49:29 -04:00
expect(@t.eval('self')).to eq :inner
2012-07-24 04:36:53 -04:00
@t.eval 'cd 5'
2015-03-10 16:49:29 -04:00
expect(@t.eval('self')).to eq 5
2012-07-24 04:36:53 -04:00
@t.eval 'cd'
2015-03-10 16:49:29 -04:00
expect(@t.eval('self')).to eq @o
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'
2015-03-10 16:49:29 -04:00
expect(@t.mapped_binding_stack).to eq [@o, @obj, 66]
end
it 'cds 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/'
2015-03-10 16:49:29 -04:00
expect(@t.mapped_binding_stack).to eq [@o, @obj, 66]
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'
2015-03-10 16:49:29 -04:00
expect(@t.mapped_binding_stack).to eq [@o, @obj, :local]
end
it 'cds 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/..'
2015-03-10 16:49:29 -04:00
expect(@t.mapped_binding_stack).to eq [@o, @obj]
end
it(
'cds 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'
2015-03-10 16:49:29 -04:00
expect(@t.mapped_binding_stack).to eq [@o, @obj, 79]
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'
2015-03-10 16:49:29 -04:00
expect(@t.mapped_binding_stack).to eq [@o, 20]
end
it 'should start a session on TOPLEVEL_BINDING with cd ::' do
2012-07-24 04:36:53 -04:00
@t.eval 'cd ::'
2015-03-10 16:49:29 -04:00
expect(@t.eval('self')).to eq TOPLEVEL_BINDING.eval('self')
end
it 'should cd into complex input (with spaces)' do
def @o.hello(_x, _y, _z)
:mon_ouie
end
2012-07-24 04:36:53 -04:00
@t.eval 'cd hello 1, 2, 3'
2015-03-10 16:49:29 -04:00
expect(@t.eval('self')).to eq :mon_ouie
end
it 'should not cd into complex input when it encounters an exception' do
expect { @t.eval 'cd 1/2/swoop_a_doop/3' }.to raise_error Pry::CommandError
2015-03-10 16:49:29 -04:00
expect(@t.mapped_binding_stack).to eq [@o]
end
it 'can cd into an expression containing a string with slashes in it' do
@t.eval 'cd ["http://google.com"]'
2015-03-10 16:49:29 -04:00
expect(@t.eval('self')).to eq ["http://google.com"]
end
it 'can cd into an expression with division in it' do
@t.eval 'cd (10/2)/even?'
2015-03-10 16:49:29 -04:00
expect(@t.eval('self')).to eq false
end
# Regression test for ticket #516.
2014-04-28 03:50:29 -04:00
it 'should be able to cd into the Object BasicObject' do
expect { @t.eval 'cd BasicObject.new' }.to_not raise_error
2014-04-28 03:50:29 -04:00
end
# https://github.com/pry/pry/issues/1596
it "can cd into objects that redefine #respond_to? to return true" do
expect { @t.eval('cd Class.new { def respond_to?(m) true end }.new') }
.to_not raise_error
end
end