Permalink
Browse files

Empty hash assignments no longer make the mutation fail

  • Loading branch information...
1 parent a8fc1de commit 9c56a0daa2e2dba32370851224c185c7aeb94dac @jonathonjones jonathonjones committed Mar 2, 2012
Showing with 49 additions and 2 deletions.
  1. +12 −0 lib/mutant/literal.rb
  2. +2 −1 lib/mutant/mutatee.rb
  3. +1 −1 lib/mutant/mutation.rb
  4. +4 −0 lib/mutant/node.rb
  5. +30 −0 spec/functional/instance_method/hash_spec.rb
View
@@ -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
View
@@ -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
View
@@ -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
View
@@ -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
@@ -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', """

0 comments on commit 9c56a0d

Please sign in to comment.