From 05331a0ab374b293e41800f8f74d38d431e8444c Mon Sep 17 00:00:00 2001 From: Markus Schirp Date: Sat, 24 May 2014 12:45:38 +0000 Subject: [PATCH] Emit item promotion in case literal array has only one item Closes #113 --- Changelog.md | 1 + config/flay.yml | 2 +- lib/mutant/mutator/node/literal/array.rb | 3 ++ .../mutant/mutator/node/literal/array_spec.rb | 54 ++++++++++++------- 4 files changed, 41 insertions(+), 19 deletions(-) diff --git a/Changelog.md b/Changelog.md index d6a45645..07f64ded 100644 --- a/Changelog.md +++ b/Changelog.md @@ -2,6 +2,7 @@ Changes: +* Add mutation from [item] to item * Add mutation from #reverse_each to #each * Add mutation from #reverse_map to #each, #map * Add mutation from #map to #each diff --git a/config/flay.yml b/config/flay.yml index adb02e8a..7a366f06 100644 --- a/config/flay.yml +++ b/config/flay.yml @@ -1,3 +1,3 @@ --- threshold: 18 -total_score: 791 +total_score: 794 diff --git a/lib/mutant/mutator/node/literal/array.rb b/lib/mutant/mutator/node/literal/array.rb index c95ec2ca..2a5ef5e2 100644 --- a/lib/mutant/mutator/node/literal/array.rb +++ b/lib/mutant/mutator/node/literal/array.rb @@ -21,6 +21,9 @@ module Mutant emit_nil emit_self mutate_body + if children.one? + emit(children.first) + end end # Mutate body diff --git a/spec/unit/mutant/mutator/node/literal/array_spec.rb b/spec/unit/mutant/mutator/node/literal/array_spec.rb index 32798e69..07a213d8 100644 --- a/spec/unit/mutant/mutator/node/literal/array_spec.rb +++ b/spec/unit/mutant/mutator/node/literal/array_spec.rb @@ -3,27 +3,45 @@ require 'spec_helper' describe Mutant::Mutator::Node::Literal, 'array' do - let(:source) { '[true, false]' } - let(:mutations) do - mutations = [] + context 'on one item' do + let(:source) { '[true]' } - # Literal replaced with nil - mutations << 'nil' + let(:mutations) do + mutations = [] + mutations << 'nil' + mutations << 'true' + mutations << '[false]' + mutations << '[nil]' + mutations << '[]' + end - # Mutation of each element in array - mutations << '[nil, false]' - mutations << '[false, false]' - mutations << '[true, nil]' - mutations << '[true, true]' - - # Remove each element of array once - mutations << '[true]' - mutations << '[false]' - - # Empty array - mutations << '[]' + it_should_behave_like 'a mutator' end - it_should_behave_like 'a mutator' + context 'on arrays with more than one item' do + let(:source) { '[true, false]' } + + let(:mutations) do + mutations = [] + + # Literal replaced with nil + mutations << 'nil' + + # Mutation of each element in array + mutations << '[nil, false]' + mutations << '[false, false]' + mutations << '[true, nil]' + mutations << '[true, true]' + + # Remove each element of array once + mutations << '[true]' + mutations << '[false]' + + # Empty array + mutations << '[]' + end + + it_should_behave_like 'a mutator' + end end