free_mutant/lib/mutant/warning_expectation.rb

48 lines
1,007 B
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)
2014-04-22 14:40:23 -04:00
# Return exception message
#
# @return [String]
#
# @api private
#
def message
"Unexpected warnings: #{unexpected.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, unexpected
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