Support label for aggregate_failures block

This commit is contained in:
Michael Miller 2021-07-31 12:04:43 -06:00
parent 4c125d98d4
commit f53ffabf6b
No known key found for this signature in database
GPG key ID: FB9F12F7C646A4AD
3 changed files with 17 additions and 6 deletions

View file

@ -28,5 +28,14 @@ Spectator.describe Spectator do
end end
end.to_not raise_error(Spectator::ExpectationFailed) end.to_not raise_error(Spectator::ExpectationFailed)
end end
it "supports naming the block" do
expect do
aggregate_failures "contradiction" do
expect(true).to be_false
expect(false).to be_true
end
end.to raise_error(Spectator::MultipleExpectationsFailed, /contradiction/)
end
end end
end end

View file

@ -172,8 +172,8 @@ module Spectator::DSL
# expect(false).to be_true # expect(false).to be_true
# end # end
# ``` # ```
def aggregate_failures def aggregate_failures(label = nil)
::Spectator::Harness.current.aggregate_failures do ::Spectator::Harness.current.aggregate_failures(label) do
yield yield
end end
end end

View file

@ -100,20 +100,20 @@ module Spectator
@deferred << block @deferred << block
end end
def aggregate_failures def aggregate_failures(label = nil)
previous = @aggregate previous = @aggregate
@aggregate = aggregate = [] of Expectation @aggregate = aggregate = [] of Expectation
begin begin
yield.tap do yield.tap do
# If there's an nested aggregate (for some reason), allow the top-level one to handle things. # If there's an nested aggregate (for some reason), allow the top-level one to handle things.
check_aggregate(aggregate) unless previous check_aggregate(aggregate, label) unless previous
end end
ensure ensure
@aggregate = previous @aggregate = previous
end end
end end
private def check_aggregate(aggregate) private def check_aggregate(aggregate, label)
failures = aggregate.select(&.failed?) failures = aggregate.select(&.failed?)
case failures.size case failures.size
when 0 then return when 0 then return
@ -121,7 +121,9 @@ module Spectator
expectation = failures.first expectation = failures.first
raise ExpectationFailed.new(expectation, expectation.failure_message) raise ExpectationFailed.new(expectation, expectation.failure_message)
else else
raise MultipleExpectationsFailed.new(failures, "Got #{failures.size} failures from failure aggregation block") message = "Got #{failures.size} failures from failure aggregation block"
message += " \"#{label}\"" if label
raise MultipleExpectationsFailed.new(failures, message)
end end
end end