Change warning expectation to not fail on missing warnings

This commit is contained in:
Markus Schirp 2014-06-30 23:46:17 +00:00
parent 1376bf1911
commit 34b8f4a6c1
2 changed files with 23 additions and 13 deletions

View file

@ -5,7 +5,7 @@ module Mutant
# Error raised on expectation miss
class ExpectationError < RuntimeError
include Concord.new(:unexpected, :missing)
include Concord.new(:unexpected)
# Return exception message
#
@ -14,7 +14,7 @@ module Mutant
# @api private
#
def message
"Unexpected warnings: #{unexpected.inspect} missing warnigns: #{missing.inspect}"
"Unexpected warnings: #{unexpected.inspect}"
end
end
@ -28,11 +28,18 @@ module Mutant
warnings = WarningFilter.use do
block.call
end
missing = expected - warnings
missing = expected - warnings
unexpected = warnings - expected
if missing.any? or unexpected.any?
fail ExpectationError.new(unexpected, missing)
if unexpected.any?
fail ExpectationError.new(unexpected)
end
if missing.any?
$stderr.puts("Expected but missing warnings: #{missing}")
end
self
end

View file

@ -4,7 +4,7 @@ describe Mutant::WarningExpectation do
let(:object) { described_class.new(expected_warnings) }
let(:expected_warnings) { [] }
let(:warnings) { [] }
let(:actual_warnings) { [] }
let(:warning_a) { "foo.rb:10: warning: We have a problem!\n" }
let(:warning_b) { "bar.rb:10: warning: We have an other problem!\n" }
@ -19,7 +19,7 @@ describe Mutant::WarningExpectation do
let(:block) do
lambda do
@called = true
warnings.each(&Kernel.method(:warn))
actual_warnings.each(&Kernel.method(:warn))
end
end
@ -28,6 +28,7 @@ describe Mutant::WarningExpectation do
end
context 'when no warnings occur during block execution' do
context 'and no warnings are expected' do
it_should_behave_like 'a command method'
end
@ -35,20 +36,22 @@ describe Mutant::WarningExpectation do
context 'and warnings are expected' do
let(:expected_warnings) { [warning_a] }
it 'raises an expectation error' do
expect { subject }.to raise_error(Mutant::WarningExpectation::ExpectationError.new([], [warning_a]))
before do
expect($stderr).to receive(:puts).with("Expected but missing warnings: #{[warning_a]}")
end
it_should_behave_like 'a command method'
end
end
context 'when warnings occur during block execution' do
let(:warnings) { [warning_a, warning_b] }
let(:actual_warnings) { [warning_a, warning_b] }
context 'and only some no warnings are expected' do
let(:expected_warnings) { [warning_a] }
it 'raises an expectation error' do
expect { subject }.to raise_error(Mutant::WarningExpectation::ExpectationError.new([warning_b], []))
expect { subject }.to raise_error(Mutant::WarningExpectation::ExpectationError.new([warning_b]))
end
end
@ -60,10 +63,10 @@ describe Mutant::WarningExpectation do
context 'and there is an expected warning missing' do
let(:expected_warnings) { [warning_a] }
let(:warnings) { [warning_b] }
let(:actual_warnings) { [warning_b] }
it 'raises an expectation error' do
expect { subject }.to raise_error(Mutant::WarningExpectation::ExpectationError.new([warning_b], [warning_a]))
expect { subject }.to raise_error(Mutant::WarningExpectation::ExpectationError.new([warning_b]))
end
end
end