From 9c56a0daa2e2dba32370851224c185c7aeb94dac Mon Sep 17 00:00:00 2001 From: Jonathon Jones Date: Fri, 2 Mar 2012 10:44:42 -0500 Subject: [PATCH] Empty hash assignments no longer make the mutation fail --- lib/mutant/literal.rb | 12 ++++++++ lib/mutant/mutatee.rb | 3 +- lib/mutant/mutation.rb | 2 +- lib/mutant/node.rb | 4 +++ spec/functional/instance_method/hash_spec.rb | 30 ++++++++++++++++++++ 5 files changed, 49 insertions(+), 2 deletions(-) diff --git a/lib/mutant/literal.rb b/lib/mutant/literal.rb index 4bd9277..c84fde9 100644 --- a/lib/mutant/literal.rb +++ b/lib/mutant/literal.rb @@ -13,7 +13,15 @@ def swap @class.new(@node).swap end + def changes_on_swap? + @class.changes_on_swap?(@node) + end + class BaseLiteral + def self.changes_on_swap?(node) + true + end + def initialize(node) @node = node end @@ -83,6 +91,10 @@ def swap end class HashLiteral < BaseLiteral + def self.changes_on_swap?(node) + node.array != [] + end + def swap new_body = @node.array.each_slice(2).inject([]) do |body, (key, value)| new_value = literal_class(value).new(value.clone).swap diff --git a/lib/mutant/mutatee.rb b/lib/mutant/mutatee.rb index d44cc09..0cf138e 100644 --- a/lib/mutant/mutatee.rb +++ b/lib/mutant/mutatee.rb @@ -21,7 +21,8 @@ def clean def set_mutations nodes.each do |node| - @mutations << Mutation.new(node, body.array) + mutation = Mutation.new(node, body.array) + @mutations << mutation if mutation.mutatable? end end diff --git a/lib/mutant/mutation.rb b/lib/mutant/mutation.rb index fa38ee1..67bbf5f 100644 --- a/lib/mutant/mutation.rb +++ b/lib/mutant/mutation.rb @@ -10,7 +10,7 @@ def initialize(node, array) @mutated = false end - def_delegators :@node, :line, :from, :to + def_delegators :@node, :line, :from, :to, :mutatable? def mutated? @mutated diff --git a/lib/mutant/node.rb b/lib/mutant/node.rb index 82db97b..ca9a9ef 100644 --- a/lib/mutant/node.rb +++ b/lib/mutant/node.rb @@ -22,5 +22,9 @@ def to def swap @copy = Literal.new(copy).swap end + + def mutatable? + Literal.new(@copy).changes_on_swap? + end end end diff --git a/spec/functional/instance_method/hash_spec.rb b/spec/functional/instance_method/hash_spec.rb index 8af30c1..65891ec 100644 --- a/spec/functional/instance_method/hash_spec.rb +++ b/spec/functional/instance_method/hash_spec.rb @@ -2,6 +2,36 @@ describe 'Mutating hashes' do context 'for an instance method' do + context 'that contains {}' do + before do + write_file 'thing.rb', """ + class Thing + def to_hash + {} + end + end + """ + end + + context 'with an expectation that the method returns {}' do + before do + write_file 'spec/thing_spec.rb', """ + $: << '.' + require 'thing' + + describe 'Thing#to_hash' do + specify { Thing.new.to_hash.should === {} } + end + """ + mutate 'Thing#to_hash spec/thing_spec.rb' + end + + specify 'there are no possible mutations' do + all_output.should include('no possible mutations') + end + end + end + context 'that contains {:foo => {:bar => 3}}' do before do write_file 'thing.rb', """