Skip to content

Commit

Permalink
Add add method to `RSpec::Expectations::MultipleExpectationsNotMetE…
Browse files Browse the repository at this point in the history
…rror`.

It's a little weird ot add it from rspec-core, but we only need it here
and this allows us to provide a common implementation.
  • Loading branch information
myronmarston committed Jun 6, 2015
1 parent f989168 commit 019d640
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 25 deletions.
27 changes: 14 additions & 13 deletions lib/rspec/core/formatters/exception_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,20 @@ class MultipleExceptionError < StandardError
# and `RSpec::Expectations::MultipleExpectationsNotMetError`, which allows
# code to detect exceptions that are instances of either, without first
# checking to see if rspec-expectations is loaded.
InterfaceTag = Module.new
module InterfaceTag
# Appends the provided exception to the list.
# @param exception [Exception] Exception to append to the list.
# @private
def add(exception)
all_exceptions << exception

if exception.class.name =~ /RSpec/
failures << exception
else
other_errors << exception
end
end
end

include InterfaceTag

Expand Down Expand Up @@ -333,18 +346,6 @@ def initialize(*exceptions)
exceptions.each { |e| add e }
end

# Appends the provided exception to the list.
# @param exception [Exception] Exception to append to the list.
def add(exception)
@all_exceptions << exception

if exception.class.name =~ /RSpec/
@failures << exception
else
@other_errors << exception
end
end

# @return [String] Combines all the exception messages into a single string.
# @note RSpec does not actually use this -- instead it formats each exception
# individually.
Expand Down
46 changes: 34 additions & 12 deletions spec/rspec/core/formatters/exception_presenter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -307,16 +307,7 @@ def exception_with(backtrace)
end
end

RSpec.describe MultipleExceptionError do
it 'supports the same interface as `RSpec::Expectations::MultipleExpectationsNotMetError`' do
skip "Skipping to allow an rspec-expectations PR to add a new method and remain green" if ENV['NEW_MUTLI_EXCEPTION_METHOD']

aggregate_failures { } # force autoload

interface = RSpec::Expectations::MultipleExpectationsNotMetError.instance_methods - Exception.instance_methods
expect(MultipleExceptionError.new).to respond_to(*interface)
end

RSpec.shared_examples_for "a class satisfying the common multiple exception error interface" do
def new_failure(*a)
RSpec::Expectations::ExpectationNotMetError.new(*a)
end
Expand All @@ -326,7 +317,7 @@ def new_error(*a)
end

it 'allows you to keep track of failures and other errors in order' do
mee = MultipleExceptionError.new
mee = new_multiple_exception_error

f1 = new_failure
e1 = new_error
Expand All @@ -340,12 +331,43 @@ def new_error(*a)
end

it 'allows you to add exceptions of an anonymous class' do
mee = MultipleExceptionError.new
mee = new_multiple_exception_error

expect {
mee.add(Class.new(StandardError).new)
}.to change(mee.other_errors, :count).by 1
end

it 'is tagged with a common module so it is clear it has the interface for multiple exceptions' do
expect(MultipleExceptionError::InterfaceTag).to be === new_multiple_exception_error
end
end

RSpec.describe RSpec::Expectations::ExpectationNotMetError do
include_examples "a class satisfying the common multiple exception error interface" do
def new_multiple_exception_error
failure_aggregator = RSpec::Expectations::FailureAggregator.new(nil, {})
RSpec::Expectations::MultipleExpectationsNotMetError.new(failure_aggregator)
end
end
end

RSpec.describe MultipleExceptionError do
include_examples "a class satisfying the common multiple exception error interface" do
def new_multiple_exception_error
MultipleExceptionError.new
end
end

it 'supports the same interface as `RSpec::Expectations::MultipleExpectationsNotMetError`' do
skip "Skipping to allow an rspec-expectations PR to add a new method and remain green" if ENV['NEW_MUTLI_EXCEPTION_METHOD']

aggregate_failures { } # force autoload

interface = RSpec::Expectations::MultipleExpectationsNotMetError.instance_methods - Exception.instance_methods
expect(MultipleExceptionError.new).to respond_to(*interface)
end

it 'allows you to instantiate it with an initial list of exceptions' do
mee = MultipleExceptionError.new(f1 = new_failure, e1 = new_error)

Expand Down

0 comments on commit 019d640

Please sign in to comment.