Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test fixes #84

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--color
--format documentation
1 change: 1 addition & 0 deletions pry-rescue.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ Gem::Specification.new do |s|
s.add_development_dependency 'yard'
s.add_development_dependency 'redcarpet'
s.add_development_dependency 'capybara'
s.add_development_dependency 'coderay'
end
167 changes: 87 additions & 80 deletions spec/commands_spec.rb
Original file line number Diff line number Diff line change
@@ -1,121 +1,128 @@
require './spec/spec_helper'
require 'spec_helper'

describe "pry-rescue commands" do
describe "try-again" do
it "should throw try_again" do
PryRescue.should_receive(:in_exception_context?).and_return{ true }
RSpec.describe "#Pry-rescue commands" do
describe "#try-again" do
it "expect to throw try_again" do
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@magicalbanana I'm stuffy, but it "should ..." reads way better than it "expect to ...".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hehe.

It should be it "is expected" since expect commands more assertion than should in english. But when I find time will work on this and message you when it's ready.

expect(PryRescue).to receive(:in_exception_context?).and_return(true)

lambda{
Pry.new.process_command "try-again", '', TOPLEVEL_BINDING
}.should throw_symbol :try_again
expect( lambda{ Pry.new.process_command "try-again TOPLEVEL_BINDING" } ).to throw_symbol :try_again
end

it "should raise a CommandError if not in Pry::rescue" do
PryRescue.should_receive(:in_exception_context?).and_return{ false }
it "expect to raise a CommandError if not in Pry::rescue" do
# expect(PryRescue).to receive(:in_exception_context?).and_return(false)
allow(PryRescue).to receive(:in_exception_context?).and_return(false)

lambda{
Pry.new.process_command "try-again", '', TOPLEVEL_BINDING
}.should raise_error Pry::CommandError
expect{ Pry.new.process_command "try-again TOPLEVEL_BINDING" }.to raise_error Pry::CommandError
end
end

describe "cd-cause" do
it "should enter the context of an explicit exception" do
describe "#cd-cause" do
it "expect to not enter the context of an explicit exception" do
begin
b1 = binding
raise "original"
rescue => e1
rescue => ErrorOne
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@magicalbanana This creates a new global constant, which you almost certainly don't want to do (it will leak between tests). If you need to extend the scope of the variable use a local that's set to nil at the start of the test. Same applies everywhere you are rescuing into a constant.

b2 = binding
end

Pry.should_receive(:rescued).once.with{ |raised|
raised.should == e1
}

Pry.new.process_command 'cd-cause e1', '', binding
end

it "should enter the context of _ex_ if no exception is given" do
b2 = nil
_ex_ = nil
Pry::rescue do
begin
b1 = binding
raise "original"
rescue => _ex_
b2 = binding
end
expect(Pry).to receive(:rescued) do |raised|
expect(raised).to eq(ErrorOne)
end

Pry.should_receive(:rescued).once.with{ |raised|
raised.should == _ex_
}

Pry.new.process_command 'cd-cause', '', b2
Pry.new.process_command 'cd-cause ErrorOne binding'
end
end

describe "cd-cause" do
it "should enter the next exception's context" do
_ex_ = nil
e1 = nil
Pry::rescue do
begin
context "when no exception is given" do
it "expect to enter the context of _ex_" do
b2 = nil
_ex_ = nil
Pry::rescue do
begin
b1 = binding
raise "original"
rescue => e1
rescue => ErrorOne
b2 = binding
raise # similar to dubious re-raises you'll find in the wild
end
rescue => e2
_ex_ = e2
end
end

PryRescue.should_receive(:enter_exception_context).once.with(e1)
expect(Pry).to receive(:rescued) do |raised|
expect(raised).to eq(ErrorOne)
end

Pry.new.process_command 'cd-cause', '', binding
Pry.new.process_command 'cd-cause ErrorOne binding'
end
end

it "should raise a CommandError if no previous commands" do
begin
b1 = binding
raise "original"
rescue => e1
# Hacks due to us not really entering a pry session here
_rescued_ = e1
_ex_ = e1
end
context "when it has nested exceptions" do
it "expect to enter the next exception's context" do
_ex_ = nil
e1 = nil
Pry::rescue do
begin
begin
b1 = binding
raise "original"
rescue => DeepException
b2 = binding
raise # similar to dubious re-raises you'll find in the wild
end
rescue => ErrorOne
_ex_ = ErrorOne
end
end

expect(PryRescue).to receive(:enter_exception_context) do |raised|
expect(raised).to eq(DeepException)
end

lambda{
Pry.new.process_command 'cd-cause', '', binding
}.should raise_error Pry::CommandError, /No previous exception/
Pry.new.process_command 'cd-cause DeepException binding'
end
end

it "should raise a CommandError on a re-raise" do
_ex_ = nil
Pry::rescue do
context "when there are no previous commands" do
it "expect to raise a CommandError" do
begin
b1 = binding
raise "original"
rescue => ErrorOne
# Hacks due to us not really entering a pry session here
_rescued_ = ErrorOne
_ex_ = ErrorOne
end

expect {
Pry.new.process_command 'cd-cause'
}.to raise_error Pry::CommandError, /No previous exception/
end
end

context "when a re-raise occurs" do
it "expect to raise a CommandError" do
_ex_ = nil
Pry::rescue do
begin
raise "oops"
rescue => e
raise e
begin
raise "oops"
rescue => e
raise e
end
rescue => _ex_
end
rescue => _ex_
end
end
_rescued_ = _ex_
_rescued_ = _ex_

lambda{
Pry.new.process_command 'cd-cause', '', binding
}.should raise_error Pry::CommandError, /No previous exception/
expect {
Pry.new.process_command 'cd-cause'
}.to raise_error Pry::CommandError, /No previous exception/
end
end

it "should raise a CommandError if not in Pry::rescue" do
lambda{
Pry.new.process_command 'cd-cause', '', binding
}.should raise_error Pry::CommandError, /No previous exception/
context "when not in Pry::rescue" do
it "should raise CommandError" do
expect {
Pry.new.process_command 'cd-cause'
}.to raise_error Pry::CommandError, /No previous exception/
end
end
end
end
84 changes: 44 additions & 40 deletions spec/core_ext_spec.rb
Original file line number Diff line number Diff line change
@@ -1,97 +1,101 @@
require File.expand_path('../../lib/pry-rescue.rb', __FILE__)
require 'spec_helper'

describe 'Pry.rescue' do
it 'should call PryRescue.enter_exception_context' do
lambda{
PryRescue.should_receive(:enter_exception_context).once
RSpec.describe '#Pry.rescue' do
it 'expect to call PryRescue.enter_exception_context' do
expect {
expect(PryRescue).to receive(:enter_exception_context).once
Pry::rescue{ raise "foobar" }
}.should raise_error(/foobar/)
}.to raise_error(/foobar/)
end

it "should retry on try-again" do
@called = 0
PryRescue.should_receive(:enter_exception_context).once{ throw :try_again }
it "expect to retry on try-again" do
called = 0
expect(PryRescue).to receive(:enter_exception_context).once{ throw :try_again }
Pry::rescue do
@called += 1
raise "foobar" if @called == 1
called += 1
raise "foobar" if called == 1
end
@called.should == 2
expect(called).to eq(2)
end

it "should try-again from innermost block" do
@outer = @inner = 0
PryRescue.should_receive(:enter_exception_context).once{ throw :try_again }
it "expect to try-again from innermost block" do
outer = inner = 0
expect(PryRescue).to receive(:enter_exception_context).once{ throw :try_again }
Pry::rescue do
@outer += 1
outer += 1
Pry::rescue do
@inner += 1
raise "oops" if @inner == 1
inner += 1
raise "oops" if inner == 1
end
end

@outer.should == 1
@inner.should == 2
expect(outer).to eq(1)
expect(inner).to eq(2)
end

it "should enter the first occurence of an exception that is re-raised" do
PryRescue.should_receive(:enter_exception_context).once{ |raised| raised.size.should == 1 }
lambda do
it "expect to enter the first occurence of an exception that is re-raised" do
expect(PryRescue).to receive(:enter_exception_context) do |raised|
expect(raised.size).to eq(1)
end

expect do
Pry::rescue do
begin
raise "first_occurance"
rescue => e
raise
end
end
end.should raise_error(/first_occurance/)
end.to raise_error(/first_occurance/)
end

it "should not catch SystemExit" do
PryRescue.should_not_receive(:enter_exception_context)
it "expect to not catch SystemExit" do
expect(PryRescue).to_not receive(:enter_exception_context)

lambda do
expect do
Pry::rescue do
exit
end
end.should raise_error SystemExit
end.to raise_error SystemExit
end

it 'should not catch Ctrl+C' do
PryRescue.should_not_receive(:enter_exception_context)
lambda do
it 'expect to not catch Ctrl+C' do
expect(PryRescue).to_not receive(:enter_exception_context)

expect do
Pry::rescue do
raise Interrupt, "ctrl+c (fake)"
end
end.should raise_error Interrupt
end.to raise_error Interrupt
end
end

describe "Pry.rescued" do
RSpec.describe "Pry.rescued" do

it "should raise an error if used outwith Pry::rescue" do
it "expect to raise an error if used outwith Pry::rescue" do
begin
raise "foo"
rescue => e
Pry.should_receive(:warn)
expect(Pry).to receive(:warn)
Pry.rescued(e)
end
end

it "should raise an error if used on an exception not raised" do
it "expect to raise an error if used on an exception not raised" do
Pry::rescue do
Pry.should_receive(:warn) do |message|
message.should =~ /^WARNING: Tried to inspect exception outside of Pry::rescue/
expect(Pry).to receive(:warn) do |message|
expect(message).to match(/^WARNING: Tried to inspect exception outside of Pry::rescue/)
end
Pry.rescued(RuntimeError.new("foo").exception)
end
end

it "should call Pry.enter_exception_context" do
it "expect to call Pry.enter_exception_context" do
Pry::rescue do
begin
raise "foo"
rescue => e
PryRescue.should_receive(:enter_exception_context).once
expect(PryRescue).to receive(:enter_exception_context)
Pry::rescued(e)
end
end
Expand Down
17 changes: 9 additions & 8 deletions spec/peek_spec.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
require 'spec_helper'

ENV['NO_PEEK_STARTUP_MESSAGE'] = 'true'

describe "PryRescue.peek!" do
it "should open a pry in the binding of caller" do
RSpec.describe "#PryRescue.peek!" do
it "expect to open a pry in the binding of caller" do
Pry.config.input = StringIO.new("foo = 6\nexit\n")
Pry.config.output = StringIO.new
foo = 5

lambda do
expect do
PryRescue.peek!
end.should change{ foo }.from(5).to(6)
end.to change{ foo }.from(5).to(6)
end

# this will fail, or not?
it 'should include the entire call stack' do
it 'expect to include the entire call stack' do
Pry.config.input = StringIO.new("up\nfoo = 6\nexit\n")
Pry.config.output = StringIO.new

Expand All @@ -22,8 +23,8 @@ def example_method

foo = 5

lambda do
expect do
PryRescue.peek!
end.should change{ foo }.from(5).to(6)
end.to change{ foo }.from(5).to(6)
end
end
Loading