Add warning expectation class
This commit is contained in:
parent
877e4540d4
commit
58f2bd1985
4 changed files with 113 additions and 1 deletions
|
@ -1,3 +1,3 @@
|
|||
---
|
||||
threshold: 18
|
||||
total_score: 822
|
||||
total_score: 836
|
||||
|
|
|
@ -81,6 +81,7 @@ require 'mutant/version'
|
|||
require 'mutant/cache'
|
||||
require 'mutant/node_helpers'
|
||||
require 'mutant/warning_filter'
|
||||
require 'mutant/warning_expectation'
|
||||
require 'mutant/constants'
|
||||
require 'mutant/random'
|
||||
require 'mutant/walker'
|
||||
|
|
40
lib/mutant/warning_expectation.rb
Normal file
40
lib/mutant/warning_expectation.rb
Normal file
|
@ -0,0 +1,40 @@
|
|||
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, :missing)
|
||||
|
||||
# Return exception message
|
||||
#
|
||||
# @return [String]
|
||||
#
|
||||
# @api private
|
||||
#
|
||||
def message
|
||||
"Unexpected warnings: #{unexpected.inspect} missing warnigns: #{missing.inspect}"
|
||||
end
|
||||
end
|
||||
|
||||
# Execute blocks with warning expectations
|
||||
#
|
||||
# @return [self]
|
||||
#
|
||||
# @api private
|
||||
#
|
||||
def execute(&block)
|
||||
warnings = WarningFilter.use do
|
||||
block.call
|
||||
end
|
||||
missing = expected - warnings
|
||||
unexpected = warnings - expected
|
||||
if missing.any? or unexpected.any?
|
||||
fail ExpectationError.new(unexpected, missing)
|
||||
end
|
||||
self
|
||||
end
|
||||
|
||||
end # WarningExpectation
|
||||
end # Mutant
|
71
spec/unit/mutant/warning_expectation.rb
Normal file
71
spec/unit/mutant/warning_expectation.rb
Normal file
|
@ -0,0 +1,71 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Mutant::WarningExpectation do
|
||||
let(:object) { described_class.new(expected_warnings) }
|
||||
|
||||
let(:expected_warnings) { [] }
|
||||
let(: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" }
|
||||
|
||||
describe '#execute' do
|
||||
subject { object.execute(&block) }
|
||||
|
||||
before do
|
||||
@called = false
|
||||
end
|
||||
|
||||
let(:block) do
|
||||
lambda do
|
||||
@called = true
|
||||
warnings.each(&Kernel.method(:warn))
|
||||
end
|
||||
end
|
||||
|
||||
it 'executes block' do
|
||||
expect { subject }.to change { @called }.from(false).to(true)
|
||||
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
|
||||
|
||||
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]))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context 'when warnings occur during block execution' do
|
||||
let(: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], []))
|
||||
end
|
||||
end
|
||||
|
||||
context 'and all warnings are expected' do
|
||||
let(:expected_warnings) { [warning_a, warning_b] }
|
||||
|
||||
it_should_behave_like 'a command method'
|
||||
end
|
||||
|
||||
context 'and there is an expected warning missing' do
|
||||
let(:expected_warnings) { [warning_a] }
|
||||
let(:warnings) { [warning_b] }
|
||||
|
||||
it 'raises an expectation error' do
|
||||
expect { subject }.to raise_error(Mutant::WarningExpectation::ExpectationError.new([warning_b], [warning_a]))
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue