diff --git a/.rspec b/.rspec new file mode 100644 index 0000000..16f9cdb --- /dev/null +++ b/.rspec @@ -0,0 +1,2 @@ +--color +--format documentation diff --git a/pry-rescue.gemspec b/pry-rescue.gemspec index e0b40b1..18dbc8e 100644 --- a/pry-rescue.gemspec +++ b/pry-rescue.gemspec @@ -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 diff --git a/spec/commands_spec.rb b/spec/commands_spec.rb index fd156c5..c88acf7 100644 --- a/spec/commands_spec.rb +++ b/spec/commands_spec.rb @@ -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 + 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 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 diff --git a/spec/core_ext_spec.rb b/spec/core_ext_spec.rb index 5b2c5cf..f9644ff 100644 --- a/spec/core_ext_spec.rb +++ b/spec/core_ext_spec.rb @@ -1,41 +1,44 @@ -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" @@ -43,55 +46,56 @@ 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 diff --git a/spec/peek_spec.rb b/spec/peek_spec.rb index 71eb39c..48d0170 100644 --- a/spec/peek_spec.rb +++ b/spec/peek_spec.rb @@ -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 @@ -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 diff --git a/spec/pry_rescue_spec.rb b/spec/pry_rescue_spec.rb index 4bd931d..45ef021 100644 --- a/spec/pry_rescue_spec.rb +++ b/spec/pry_rescue_spec.rb @@ -1,114 +1,123 @@ -require File.expand_path('../../lib/pry-rescue.rb', __FILE__) -require 'uri' +require 'spec_helper' -describe "PryRescue.load" do +RSpec.describe "#PryRescue.load" do if defined?(PryStackExplorer) - it "should open at the correct point" do - PryRescue.should_receive(:pry).once{ |opts| - opts[:call_stack].first.eval("__FILE__").should end_with('spec/fixtures/simple.rb') - } - lambda{ + it "expect to open at the correct point" do + expect(PryRescue).to receive(:pry).once do |opts| + expect(opts[:call_stack].first.eval("__FILE__")).to end_with('spec/fixtures/simple.rb') + end + + expect do PryRescue.load("spec/fixtures/simple.rb") - }.should raise_error(/simple-exception/) + end.to raise_error(/simple-exception/) end - it "should open above the standard library" do - PryRescue.should_receive(:pry).once do |opts| - opts[:call_stack][opts[:initial_frame]].eval("__FILE__").should end_with('spec/fixtures/uri.rb') + it "expect open above the standard library" do + expect(PryRescue).to receive(:pry).once do |opts| + expect(opts[:call_stack][opts[:initial_frame]].eval("__FILE__")).to end_with('spec/fixtures/uri.rb') end - lambda{ + + expect do PryRescue.load("spec/fixtures/uri.rb") - }.should raise_error(URI::InvalidURIError) + end.to raise_error(URI::InvalidURIError) end - it "should keep the standard library on the binding stack" do - PryRescue.should_receive(:pry).once do |opts| - opts[:call_stack].first.eval("__FILE__").should start_with(RbConfig::CONFIG['libdir']) + it "expect to keep the standard library on the binding stack" do + expect(PryRescue).to receive(:pry).once do |opts| + expect(opts[:call_stack].first.eval("__FILE__")).to start_with(RbConfig::CONFIG['libdir']) end - lambda{ + + expect do PryRescue.load("spec/fixtures/uri.rb") - }.should raise_error(URI::InvalidURIError) + end.to raise_error(URI::InvalidURIError) end - it "should open above gems" do - PryRescue.should_receive(:pry).once do |opts| - opts[:call_stack][opts[:initial_frame]].eval("__FILE__").should end_with('spec/fixtures/coderay.rb') + it "expect to open above gems" do + expect(PryRescue).to receive(:pry).once do |opts| + expect(opts[:call_stack][opts[:initial_frame]].eval("__FILE__")).to end_with('spec/fixtures/coderay.rb') end - lambda{ + + expect do PryRescue.load("spec/fixtures/coderay.rb") - }.should raise_error(ArgumentError) + end.to raise_error(ArgumentError) end - it "should open above gems" do - PryRescue.should_receive(:pry).once do |opts| + it "expect to open above gems" do + expect(PryRescue).to receive(:pry).once do |opts| coderay_path = Gem::Specification.respond_to?(:detect) ? Gem::Specification.detect{|x| x.name == 'coderay' }.full_gem_path : Gem.all_load_paths.grep(/coderay/).last - opts[:call_stack].first.eval("__FILE__").should start_with(coderay_path) + expect(opts[:call_stack].first.eval("__FILE__")).to start_with(coderay_path) end - lambda{ + + expect do PryRescue.load("spec/fixtures/coderay.rb") - }.should raise_error(ArgumentError) + end.to raise_error(ArgumentError) end - it "should skip pwd, even if it is a gem (but not vendor stuff)" do - Gem::Specification.stub :any? do true end - PryRescue.send(:user_path?, Dir.pwd + '/asdf.rb').should be_true - PryRescue.send(:user_path?, Dir.pwd + '/vendor/asdf.rb').should be_false + it "expect to skip pwd, even if it is a gem (but not vendor stuff)" do + allow(Gem::Specification).to receive(:any?).and_return(true) + expect(PryRescue.send(:user_path?, Dir.pwd + '/asdf.rb')).to be_truthy + expect(PryRescue.send(:user_path?, Dir.pwd + '/vendor/asdf.rb')).to be_falsey end - it "should filter out duplicate stack frames" do - PryRescue.should_receive(:pry).once do |opts| - opts[:call_stack][0].eval("__LINE__").should == 4 - opts[:call_stack][1].eval("__LINE__").should == 12 + it "expect to filter out duplicate stack frames" do + expect(PryRescue).to receive(:pry).once do |opts| + expect(opts[:call_stack][0].eval("__LINE__")).to eq(4) + expect(opts[:call_stack][1].eval("__LINE__")).to eq(12) end - lambda{ + + expect do PryRescue.load("spec/fixtures/super.rb") - }.should raise_error(/super-exception/) + end.to raise_error(/super-exception/) end - it "should calculate correct initial frame even when duplicates are present" do - PryRescue.should_receive(:pry).once do |opts| - opts[:call_stack][0].eval("__FILE__").should end_with('fake.rb') - opts[:call_stack][opts[:initial_frame]].eval("__FILE__").should end_with('spec/fixtures/initial.rb') + it "expect calculate correct initial frame even when duplicates are present" do + expect(PryRescue).to receive(:pry).once do |opts| + expect(opts[:call_stack][0].eval("__FILE__")).to end_with('fake.rb') + expect(opts[:call_stack][opts[:initial_frame]].eval("__FILE__")).to end_with('spec/fixtures/initial.rb') end - lambda{ + + expect do PryRescue.load("spec/fixtures/initial.rb") - }.should raise_error(/no :baz please/) + end.to raise_error(/no :baz please/) end - it "should skip over reraises from within gems" do - PryRescue.should_receive(:pry).once do |opts| - opts[:call_stack][0].eval("__FILE__").should end_with('spec/fixtures/reraise.rb') + it "expect to skip over reraises from within gems" do + expect(PryRescue).to receive(:pry).once do |opts| + expect(opts[:call_stack][0].eval("__FILE__")).to end_with('spec/fixtures/reraise.rb') end - lambda{ + + expect do PryRescue.load("spec/fixtures/reraise.rb") - }.should raise_error(/reraise-exception/) + end.to raise_error(/reraise-exception/) end - it "should not skip over independent raises within gems" do - PryRescue.should_receive(:pry).once do |opts| - opts[:call_stack][0].eval("__FILE__").should end_with('fake.rb') + it "expect to not skip over independent raises within gems" do + expect(PryRescue).to receive(:pry).once do |opts| + expect(opts[:call_stack][0].eval("__FILE__")).to end_with('fake.rb') end - lambda{ + + expect do PryRescue.load("spec/fixtures/raiseother.rb") - }.should raise_error(/raiseother_exception/) + end.to raise_error(/raiseother_exception/) end - it "should output a warning if the exception was not raised" do - PryRescue.should_not_receive(:enter_exception_context) - Pry.should_receive(:warn).once + it "expect output a warning if the exception was not raised" do + expect(PryRescue).to_not receive(:enter_exception_context) + expect(Pry).to receive(:warn).once Pry.rescued(RuntimeError.new("foo")) end else - it "should open at the correct point" do - Pry.should_receive(:start).once{ |binding, h| - binding.eval("__FILE__").should end_with('spec/fixtures/simple.rb') - } - lambda{ + it "expect to open at the correct point" do + expect(Pry).to receive(:start).once do |binding, h| + expect(binding.eval("__FILE__")).to end_with('spec/fixtures/simple.rb') + end + + expect do PryRescue.load("spec/fixtures/simple.rb") - }.should raise_error(/simple-exception/) + end.to raise_error(/simple-exception/) end end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index c4256be..ce32b29 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,6 +1,33 @@ -require 'rspec' -require 'rspec/autorun' - require 'pry/test/helper' - require './lib/pry-rescue' + +require 'uri' + +RSpec.configure do |config| + config.expect_with :rspec do |expectations| + expectations.include_chain_clauses_in_custom_matcher_descriptions = true + end + + config.mock_with :rspec do |mocks| + mocks.verify_partial_doubles = true + end + + begin + config.filter_run :focus + config.run_all_when_everything_filtered = true + + config.disable_monkey_patching! + + config.warnings = true + + if config.files_to_run.one? + config.default_formatter = 'doc' + end + + config.profile_examples = 10 + + config.order = :random + + Kernel.srand config.seed + end +end