Remove warning expectations

* Warnings do not disturb master output anymore as we redirect stderr of
  children to dev null. So the main reason for adding this feature is
  gone. BTW Its planned to capture the stderr of childrens later.
* Warning expectations do not work very well under zombification as line
  numbers change.
This commit is contained in:
Markus Schirp 2014-11-27 16:34:08 +00:00
parent f54c7c42cf
commit ae7284f39a
6 changed files with 5 additions and 172 deletions

View file

@ -1,3 +1,3 @@
--- ---
threshold: 18 threshold: 18
total_score: 1132 total_score: 1114

View file

@ -103,7 +103,6 @@ require 'mutant/ast/meta'
require 'mutant/cache' require 'mutant/cache'
require 'mutant/delegator' require 'mutant/delegator'
require 'mutant/warning_filter' require 'mutant/warning_filter'
require 'mutant/warning_expectation'
require 'mutant/require_highjack' require 'mutant/require_highjack'
require 'mutant/isolation' require 'mutant/isolation'
require 'mutant/mutator' require 'mutant/mutator'

View file

@ -7,10 +7,6 @@ module Mutant
NAME_INDEX = 0 NAME_INDEX = 0
SYMBOL = '#'.freeze SYMBOL = '#'.freeze
# A list of methods that will warn when they are undefined
WARN_METHODS_UNDEFINED =
RUBY_ENGINE.eql?('ruby') ? [:initialize, :__send__, :object_id].freeze : EMPTY_ARRAY
# Test if method is public # Test if method is public
# #
# @return [Boolean] # @return [Boolean]
@ -22,8 +18,6 @@ module Mutant
end end
memoize :public? memoize :public?
LINE_INCREMENT = defined?(Zombie) ? -11 : 5
# Prepare subject for mutation insertion # Prepare subject for mutation insertion
# #
# @return [self] # @return [self]
@ -31,15 +25,7 @@ module Mutant
# @api private # @api private
# #
def prepare def prepare
expected_warnings = scope.send(:undef_method, name)
if WARN_METHODS_UNDEFINED.include?(name)
["#{__FILE__}:#{__LINE__ + LINE_INCREMENT}: warning: undefining `#{name}' may cause serious problems\n"]
else
EMPTY_ARRAY
end
WarningExpectation.new(expected_warnings).execute do
scope.send(:undef_method, name)
end
self self
end end

View file

@ -1,47 +0,0 @@
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)
# Return exception message
#
# @return [String]
#
# @api private
#
def message
"Unexpected warnings: #{unexpected.inspect}, expected: #{expected.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 unexpected.any?
fail ExpectationError.new(unexpected, expected)
end
if missing.any?
$stderr.puts("Expected but missing warnings: #{missing}")
end
self
end
end # WarningExpectation
end # Mutant

View file

@ -48,36 +48,11 @@ RSpec.describe Mutant::Subject::Method::Instance do
subject { object.prepare } subject { object.prepare }
context 'on non initialize methods' do it 'undefines method on scope' do
expect { subject }.to change { scope.instance_methods.include?(:foo) }.from(true).to(false)
it 'undefines method on scope' do
expect { subject }.to change { scope.instance_methods.include?(:foo) }.from(true).to(false)
end
it_should_behave_like 'a command method'
end end
context 'on initialize method' do it_should_behave_like 'a command method'
let(:node) do
s(:def, :initialize, s(:args))
end
it 'does not write warnings' do
warnings = Mutant::WarningFilter.use do
subject
end
expect(warnings).to eql([])
end
it 'undefines method on scope' do
subject
expect { scope.new }.to raise_error(NoMethodError)
end
it_should_behave_like 'a command method'
end
end end
describe '#source' do describe '#source' do

View file

@ -1,80 +0,0 @@
RSpec.describe Mutant::WarningExpectation do
let(:object) { described_class.new(expected_warnings) }
let(:expected_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" }
describe '#execute' do
subject { object.execute(&block) }
before do
@called = false
end
let(:block) do
lambda do
@called = true
actual_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] }
before do
expect($stderr).to receive(:puts).with("Expected but missing warnings: #{expected_warnings}")
end
it_should_behave_like 'a command method'
end
end
context 'when warnings occur during block execution' do
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], expected_warnings))
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(:actual_warnings) { [warning_b] }
it 'raises an expectation error' do
expect { subject }.to raise_error(Mutant::WarningExpectation::ExpectationError.new([warning_b], expected_warnings))
end
end
end
end
end
RSpec.describe Mutant::WarningExpectation::ExpectationError do
describe '#message' do
subject { described_class.new(['unexpected-a'], ['expected-b']).message }
it { should eql('Unexpected warnings: ["unexpected-a"], expected: ["expected-b"]') }
end
end