From 85061dee96ff82efce3df8723093581357256ae9 Mon Sep 17 00:00:00 2001 From: Markus Schirp Date: Tue, 22 Apr 2014 18:59:38 +0000 Subject: [PATCH] Add warning expecatation filter for undef initialize * Closes #175 --- Changelog.md | 4 +- lib/mutant/mutation.rb | 4 +- lib/mutant/subject/method/instance.rb | 10 ++++- .../mutant/subject/method/instance_spec.rb | 37 +++++++++++++++++-- 4 files changed, 49 insertions(+), 6 deletions(-) diff --git a/Changelog.md b/Changelog.md index c8c127b3..73e58e38 100644 --- a/Changelog.md +++ b/Changelog.md @@ -1,4 +1,4 @@ -# v0.5.11 2014-04-xx +# v0.5.11 2014-04-21 Changes: @@ -6,6 +6,8 @@ Changes: * Better require highjack based zombifier * Do not mutate nthref $1 to gvar $0 * Use faster duplicate guarding hashing AST::Node intances +* Fix lots of shadowed invalid ASTs +* Fix undefine initialize warnings, Closes #175 # v0.5.10 2014-04-06 diff --git a/lib/mutant/mutation.rb b/lib/mutant/mutation.rb index 69f86784..bd136dac 100644 --- a/lib/mutant/mutation.rb +++ b/lib/mutant/mutation.rb @@ -43,7 +43,9 @@ module Mutant # def insert subject.public? - Loader::Eval.call(root, subject) + subject.warning_expectation.execute do + Loader::Eval.call(root, subject) + end self end diff --git a/lib/mutant/subject/method/instance.rb b/lib/mutant/subject/method/instance.rb index cceb4124..8b6bdd6a 100644 --- a/lib/mutant/subject/method/instance.rb +++ b/lib/mutant/subject/method/instance.rb @@ -31,7 +31,15 @@ module Mutant # @api private # def prepare - scope.send(:undef_method, name) + expected_warnings = + if name.equal?(:initialize) + ["#{__FILE__}:#{__LINE__ + 5}: warning: undefining `initialize' may cause serious problems\n"] + else + [] + end + WarningExpectation.new(expected_warnings).execute do + scope.send(:undef_method, name) + end self end diff --git a/spec/unit/mutant/subject/method/instance_spec.rb b/spec/unit/mutant/subject/method/instance_spec.rb index 344e3d88..679b8250 100644 --- a/spec/unit/mutant/subject/method/instance_spec.rb +++ b/spec/unit/mutant/subject/method/instance_spec.rb @@ -20,6 +20,12 @@ describe Mutant::Subject::Method::Instance do let(:scope) do Class.new do + attr_reader :bar + + def initialize + @bar = :boo + end + def foo end end @@ -27,11 +33,36 @@ describe Mutant::Subject::Method::Instance do subject { object.prepare } - it 'undefines method on scope' do - expect { subject }.to change { scope.instance_methods.include?(:foo) }.from(true).to(false) + 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) + end + + it_should_behave_like 'a command method' + end - it_should_behave_like 'a command method' + context 'on initialize method' do + + 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 describe '#source' do