Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #7 from jonathonjones/skip_empty_hashes

Empty hash assignments no longer make the mutation fail
  • Loading branch information...
commit 68dd0a44a6b4a53db3ae7273386a113b931492e8 2 parents a8fc1de + 9c56a0d
@txus authored
View
12 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
View
3  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
View
2  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
View
4 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
View
30 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', """
Please sign in to comment.
Something went wrong with that request. Please try again.