Skip to content

Commit

Permalink
Refactor jump_to and spec
Browse files Browse the repository at this point in the history
Update jump_to command's spec to RSpec 3 (#1294)
Perform some refactorings along the way.
  • Loading branch information
JoshCheek committed Sep 1, 2014
1 parent 6d5eb08 commit bf24df0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 18 deletions.
16 changes: 6 additions & 10 deletions lib/pry/commands/jump_to.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,14 @@ class Command::JumpTo < Pry::ClassCommand
BANNER

def process(break_level)
break_level = break_level.to_i
nesting_level = _pry_.binding_stack.size - 1
break_level = break_level.to_i
nesting_level = _pry_.binding_stack.size - 1
max_nest_level = nesting_level - 1

case break_level
when nesting_level
output.puts "Already at nesting level #{nesting_level}"
when (0...nesting_level)
_pry_.binding_stack.slice!(break_level + 1, _pry_.binding_stack.size)

else
max_nest_level = nesting_level - 1
output.puts "Invalid nest level. Must be between 0 and #{max_nest_level}. Got #{break_level}."
when nesting_level then output.puts "Already at nesting level #{nesting_level}"
when (0..max_nest_level) then _pry_.binding_stack = _pry_.binding_stack[0..break_level]
else output.puts "Invalid nest level. Must be between 0 and #{max_nest_level}. Got #{break_level}."
end
end
end
Expand Down
19 changes: 11 additions & 8 deletions spec/commands/jump_to_spec.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
require_relative '../helper'
require 'helper'

describe "jump-to" do
it 'should jump to the proper binding index in the stack' do
pry_eval('cd 1', 'cd 2', 'jump-to 1', 'self').should == 1
RSpec.describe "jump-to" do
let(:obj) { Object.new }

it 'jumps to the proper binding index in the stack' do
expect(pry_eval obj, "cd 1", "cd 2", "jump-to 0", 'self').to eq obj
expect(pry_eval obj, 'cd 1', 'cd 2', 'jump-to 1', 'self').to eq 1
end

it 'should print error when trying to jump to a non-existent binding index' do
pry_eval("cd 1", "cd 2", "jump-to 100").should =~ /Invalid nest level/
it 'prints an error when trying to jump to the same binding index' do
expect(pry_eval obj, "cd 1", "cd 2", "jump-to 2").to match /Already/
end

it 'should print error when trying to jump to the same binding index' do
pry_eval("cd 1", "cd 2", "jump-to 2").should =~ /Already/
it 'prints error when trying to jump to a non-existent binding index' do
expect(pry_eval obj, "cd 1", "cd 2", "jump-to 3").to match /Invalid nest level/
end
end

0 comments on commit bf24df0

Please sign in to comment.