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

added tests for cat --ex N and edit --ex N, all passing except on RBX

This commit is contained in:
John Mair 2011-09-14 05:40:30 +12:00
parent 21287630d5
commit 5af4111846
3 changed files with 158 additions and 6 deletions

View file

@ -27,6 +27,25 @@ class << Pry
end end
end end
class MockPryException
attr_accessor :bt_index
attr_accessor :backtrace
def initialize(*backtrace)
@backtrace = backtrace
@bt_index = 0
end
def message
"mock exception"
end
def bt_source_location_for(index)
backtrace[index] =~ /(.*):(\d+)/
[$1, $2.to_i]
end
end
# are we on Jruby platform? # are we on Jruby platform?
def jruby? def jruby?
defined?(RUBY_ENGINE) && RUBY_ENGINE =~ /jruby/ defined?(RUBY_ENGINE) && RUBY_ENGINE =~ /jruby/
@ -136,6 +155,17 @@ class Pry
end end
end end
# Open a temp file and yield it to the block, closing it after
# @return [String] The path of the temp file
def temp_file
file = Tempfile.new("tmp")
yield file
file.flush
file.path
ensure
file.close
end
CommandTester = Pry::CommandSet.new do CommandTester = Pry::CommandSet.new do
command "command1", "command 1 test" do command "command1", "command 1 test" do

View file

@ -122,6 +122,55 @@ describe "Pry::DefaultCommands::Introspection" do
end end
end end
describe "with --ex NUM" do
before do
Pry.config.editor = proc do |file, line|
@__ex_file__ = file
@__ex_line__ = line
nil
end
end
it 'should start editor on first level of backtrace when --ex used with no argument ' do
pry_instance = Pry.new(:input => StringIO.new("edit -n --ex"), :output => StringIO.new)
pry_instance.last_exception = MockPryException.new("a:1", "b:2", "c:3")
pry_instance.rep(self)
@__ex_file__.should == "a"
@__ex_line__.should == 1
end
it 'should start editor on first level of backtrace when --ex 0 used ' do
pry_instance = Pry.new(:input => StringIO.new("edit -n --ex 0"), :output => StringIO.new)
pry_instance.last_exception = MockPryException.new("a:1", "b:2", "c:3")
pry_instance.rep(self)
@__ex_file__.should == "a"
@__ex_line__.should == 1
end
it 'should start editor on second level of backtrace when --ex 1 used' do
pry_instance = Pry.new(:input => StringIO.new("edit -n --ex 1"), :output => StringIO.new)
pry_instance.last_exception = MockPryException.new("a:1", "b:2", "c:3")
pry_instance.rep(self)
@__ex_file__.should == "b"
@__ex_line__.should == 2
end
it 'should start editor on third level of backtrace when --ex 2 used' do
pry_instance = Pry.new(:input => StringIO.new("edit -n --ex 2"), :output => StringIO.new)
pry_instance.last_exception = MockPryException.new("a:1", "b:2", "c:3")
pry_instance.rep(self)
@__ex_file__.should == "c"
@__ex_line__.should == 3
end
it 'should display error message when backtrace level is out of bounds (using --ex 4)' do
pry_instance = Pry.new(:input => StringIO.new("edit -n --ex 4"), :output => str_output = StringIO.new)
pry_instance.last_exception = MockPryException.new("a:1", "b:2", "c:3")
pry_instance.rep(self)
str_output.string.should =~ /Exception has no associated file/
end
end
describe "without FILE" do describe "without FILE" do
it "should edit the current expression if it's incomplete" do it "should edit the current expression if it's incomplete" do
mock_pry("def a", "edit") mock_pry("def a", "edit")

View file

@ -5,6 +5,7 @@ describe "Pry::DefaultCommands::Shell" do
# this doesnt work so well on rbx due to differences in backtrace # this doesnt work so well on rbx due to differences in backtrace
# so we currently skip rbx until we figure out a workaround # so we currently skip rbx until we figure out a workaround
describe "with --ex" do
if !rbx? if !rbx?
it 'cat --ex should correctly display code that generated exception even if raised in repl' do it 'cat --ex should correctly display code that generated exception even if raised in repl' do
mock_pry("this raises error", "cat --ex").should =~ /\d+:(\s*) this raises error/ mock_pry("this raises error", "cat --ex").should =~ /\d+:(\s*) this raises error/
@ -15,4 +16,76 @@ describe "Pry::DefaultCommands::Shell" do
end end
end end
end end
describe "with --ex N" do
it 'should cat first level of backtrace when --ex used with no argument ' do
pry_instance = Pry.new(:input => StringIO.new("cat --ex"), :output => str_output = StringIO.new)
file_name = temp_file do |f|
f << "bt number 1"
end
pry_instance.last_exception = MockPryException.new("#{file_name}:1", "x", "x")
pry_instance.rep(self)
str_output.string.should =~ /bt number 1/
end
it 'should cat first level of backtrace when --ex 0 used ' do
pry_instance = Pry.new(:input => StringIO.new("cat --ex 0"), :output => str_output = StringIO.new)
file_name = temp_file do |f|
f << "bt number 1"
end
pry_instance.last_exception = MockPryException.new("#{file_name}:1", "x", "x")
pry_instance.rep(self)
str_output.string.should =~ /bt number 1/
end
it 'should cat second level of backtrace when --ex 1 used ' do
pry_instance = Pry.new(:input => StringIO.new("cat --ex 1"), :output => str_output = StringIO.new)
file_name = temp_file do |f|
f << "bt number 2"
end
pry_instance.last_exception = MockPryException.new("x", "#{file_name}:1", "x")
pry_instance.rep(self)
str_output.string.should =~ /bt number 2/
end
it 'should cat third level of backtrace when --ex 2 used ' do
pry_instance = Pry.new(:input => StringIO.new("cat --ex 2"), :output => str_output = StringIO.new)
file_name = temp_file do |f|
f << "bt number 3"
end
pry_instance.last_exception = MockPryException.new("x", "x", "#{file_name}:1")
pry_instance.rep(self)
str_output.string.should =~ /bt number 3/
end
it 'should show error when backtrace level out of bounds ' do
pry_instance = Pry.new(:input => StringIO.new("cat --ex 3"), :output => str_output = StringIO.new)
pry_instance.last_exception = MockPryException.new("x", "x", "x")
pry_instance.rep(self)
str_output.string.should =~ /No Exception or Exception has no associated file/
end
it 'each successive cat --ex should show the next level of backtrace, and going past the final level should return to the first' do
file_names = []
file_names << temp_file { |f| f << "bt number 0" }
file_names << temp_file { |f| f << "bt number 1" }
file_names << temp_file { |f| f << "bt number 2" }
pry_instance = Pry.new(:input => StringIO.new("cat --ex\n" * (file_names.size + 1)),
:output => str_output = StringIO.new)
pry_instance.last_exception = MockPryException.new(*file_names.map { |f| "#{f}:1" })
file_names.each_with_index do |f, idx|
pry_instance.rep(self)
str_output.string.should =~ /bt number #{idx}/
end
str_output.reopen
pry_instance.rep(self)
str_output.string.should =~ /bt number 0/
end
end
end
end end