Skip to content
This repository has been archived by the owner on Jun 2, 2023. It is now read-only.

Commit

Permalink
Merge pull request #7 from jonathonjones/skip_empty_hashes
Browse files Browse the repository at this point in the history
Empty hash assignments no longer make the mutation fail
  • Loading branch information
Josep M. Bach committed Mar 5, 2012
2 parents a8fc1de + 9c56a0d commit 68dd0a4
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 2 deletions.
12 changes: 12 additions & 0 deletions lib/mutant/literal.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion lib/mutant/mutatee.rb
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion lib/mutant/mutation.rb
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions lib/mutant/node.rb
Expand Up @@ -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
30 changes: 30 additions & 0 deletions spec/functional/instance_method/hash_spec.rb
Expand Up @@ -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', """
Expand Down

0 comments on commit 68dd0a4

Please sign in to comment.