From 3dc23c5238356bca5a88bf516abd268b59746c82 Mon Sep 17 00:00:00 2001 From: Markus Schirp Date: Sat, 29 Dec 2012 16:00:27 +0100 Subject: [PATCH] Mutate method call receivers --- Changelog.md | 6 +++ lib/mutant/mutator/node/send.rb | 40 ++++++++++++++++++- spec/shared/mutator_behavior.rb | 5 ++- .../mutant/mutator/node/send/mutation_spec.rb | 25 ++++-------- 4 files changed, 55 insertions(+), 21 deletions(-) diff --git a/Changelog.md b/Changelog.md index dcb7289c..e57184dd 100644 --- a/Changelog.md +++ b/Changelog.md @@ -1,3 +1,9 @@ +# v0.2.8 2012-12-29 + +* [feature] Mutate method call receivers "foo.bar" => "foo" + +[Compare v0.2.7..v0.2.8](https://github.com/mbj/mutant/compare/v0.2.7...v0.2.8) + # v0.2.7 2012-12-21 * [fixed] Use latest adamantium and ice_nine diff --git a/lib/mutant/mutator/node/send.rb b/lib/mutant/mutator/node/send.rb index 34accf3d..e66d4ff4 100644 --- a/lib/mutant/mutator/node/send.rb +++ b/lib/mutant/mutator/node/send.rb @@ -15,11 +15,49 @@ module Mutant # @api private # def dispatch + emit_receiver emit_implicit_self_receiver - emit_attribute_mutations(:block) if node.block + emit_receiver_mutations + emit_block_mutations emit_block_absence_mutation end + # Emit receiver + # + # @return [undefined] + # + # @api pirvate + # + def emit_receiver + unless self? + emit(receiver) + end + end + + # Emit block mutations + # + # @return [undefined] + # + # @api private + # + def emit_block_mutations + if node.block + emit_attribute_mutations(:block) + end + end + + # Emit receiver mutations + # + # @return [undefined] + # + # @api private + # + def emit_receiver_mutations + unless self? + emit_attribute_mutations(:receiver) + end + end + # Emit block absence mutation # # @return [undefined] diff --git a/spec/shared/mutator_behavior.rb b/spec/shared/mutator_behavior.rb index e8cff33d..8c14e1a0 100644 --- a/spec/shared/mutator_behavior.rb +++ b/spec/shared/mutator_behavior.rb @@ -45,13 +45,14 @@ shared_examples_for 'a mutator' do end it 'generates the expected mutations' do - generated = self.subject.map { |mutation| ToSource.to_source(mutation) }.to_set + generated = self.subject.map { |mutation| ToSource.to_source(mutation) }.to_set missing = (expected_mutations - generated).to_a unexpected = (generated - expected_mutations).to_a unless generated == expected_mutations - fail "Missing mutations:\n%s\nUnexpected mutations:\n%s" % [missing.join("\n----\n"), unexpected.join("\n----\n")] + message ="Missing mutations:\n%s\nUnexpected mutations:\n%s" % [missing.join("\n----\n"), unexpected.join("\n----\n")] + fail message end end end diff --git a/spec/unit/mutant/mutator/node/send/mutation_spec.rb b/spec/unit/mutant/mutator/node/send/mutation_spec.rb index 67c5964b..ce291b60 100644 --- a/spec/unit/mutant/mutator/node/send/mutation_spec.rb +++ b/spec/unit/mutant/mutator/node/send/mutation_spec.rb @@ -2,23 +2,7 @@ require 'spec_helper' describe Mutant::Mutator, 'send' do context 'send without arguments' do - # This could not be reproduced in a test case but happens in the mutant source code? - context 'block_given?' do - context 'implicit self' do - let(:source) { 'block_given?' } - - it_should_behave_like 'a noop mutator' - end - - context 'explicit self' do - let(:source) { 'self.block_given?' } - - it_should_behave_like 'a noop mutator' - end - end - context 'with self as receiver' do - context 'implicit' do let(:source) { 'foo' } @@ -39,9 +23,14 @@ describe Mutant::Mutator, 'send' do end context 'to some object' do - let(:source) { '1.foo' } + let(:source) { 'foo.bar' } - it_should_behave_like 'a noop mutator' + let(:mutations) do + mutations = [] + mutations << 'foo' + end + + it_should_behave_like 'a mutator' end end