free_mutant/lib/mutant/warning_expectation.rb

48 lines
1 KiB
Ruby
Raw Normal View History

2014-04-22 14:40:23 -04:00
module Mutant
# A class to expect some warning message raising on absence of unexpected warnings
class WarningExpectation
include Adamantium::Flat, Concord.new(:expected)
# Error raised on expectation miss
class ExpectationError < RuntimeError
include Concord.new(:unexpected, :expected)
2014-04-22 14:40:23 -04:00
# Return exception message
#
# @return [String]
#
# @api private
#
def message
"Unexpected warnings: #{unexpected.inspect}, expected: #{expected.inspect}"
2014-04-22 14:40:23 -04:00
end
end
# Execute blocks with warning expectations
#
# @return [self]
#
# @api private
#
def execute(&block)
warnings = WarningFilter.use do
block.call
end
missing = expected - warnings
2014-04-22 14:40:23 -04:00
unexpected = warnings - expected
if unexpected.any?
fail ExpectationError.new(unexpected, expected)
2014-04-22 14:40:23 -04:00
end
if missing.any?
$stderr.puts("Expected but missing warnings: #{missing}")
end
2014-04-22 14:40:23 -04:00
self
end
end # WarningExpectation
end # Mutant