From 6547412f6187c958a3ddcf8e994d60e3a0a95faf Mon Sep 17 00:00:00 2001 From: Dan Kubb Date: Sat, 22 Jun 2013 22:17:50 -0700 Subject: [PATCH 1/3] Remove unused let declarations --- spec/unit/mutant/mutator/node/case/mutation_spec.rb | 6 ------ 1 file changed, 6 deletions(-) diff --git a/spec/unit/mutant/mutator/node/case/mutation_spec.rb b/spec/unit/mutant/mutator/node/case/mutation_spec.rb index 34acc5a0..4ce895b8 100644 --- a/spec/unit/mutant/mutator/node/case/mutation_spec.rb +++ b/spec/unit/mutant/mutator/node/case/mutation_spec.rb @@ -3,12 +3,6 @@ require 'spec_helper' describe Mutant::Mutator::Node::Case do let(:random_string) { 'random' } - let(:source) { ':foo' } - - let(:mutations) do - %w(nil) << ":s#{random_string}" - end - before do Mutant::Random.stub(:hex_string => random_string) end From 4e641859ee646915e0e3053008bbe2b083caa1a5 Mon Sep 17 00:00:00 2001 From: Dan Kubb Date: Sat, 22 Jun 2013 22:18:10 -0700 Subject: [PATCH 2/3] Add context for multiple when branches --- .../mutant/mutator/node/case/mutation_spec.rb | 420 +++++++++--------- 1 file changed, 211 insertions(+), 209 deletions(-) diff --git a/spec/unit/mutant/mutator/node/case/mutation_spec.rb b/spec/unit/mutant/mutator/node/case/mutation_spec.rb index 4ce895b8..362306ea 100644 --- a/spec/unit/mutant/mutator/node/case/mutation_spec.rb +++ b/spec/unit/mutant/mutator/node/case/mutation_spec.rb @@ -7,214 +7,216 @@ describe Mutant::Mutator::Node::Case do Mutant::Random.stub(:hex_string => random_string) end - let(:source) do - <<-RUBY - case :condition - when :foo - :foo - when :bar, :baz - :barbaz - else - :else - end - RUBY + context 'with multiple when branches' do + let(:source) do + <<-RUBY + case :condition + when :foo + :foo + when :bar, :baz + :barbaz + else + :else + end + RUBY + end + + let(:mutations) do + mutations = [] + + # Presence of branches + mutations << <<-RUBY + case :condition + when :bar, :baz + :barbaz + else + :else + end + RUBY + mutations << <<-RUBY + case :condition + when :foo + :foo + else + :else + end + RUBY + mutations << <<-RUBY + case :condition + when :foo + :foo + when :bar, :baz + :barbaz + end + RUBY + + # Mutations of condition + mutations << <<-RUBY + case nil + when :foo + :foo + when :bar, :baz + :barbaz + else + :else + end + RUBY + mutations << <<-RUBY + case :srandom + when :foo + :foo + when :bar, :baz + :barbaz + else + :else + end + RUBY + + # Mutations of branch bodies + mutations << <<-RUBY + case :condition + when :foo + nil + when :bar, :baz + :barbaz + else + :else + end + RUBY + mutations << <<-RUBY + case :condition + when :foo + :srandom + when :bar, :baz + :barbaz + else + :else + end + RUBY + mutations << <<-RUBY + case :condition + when :foo + :foo + when :bar, :baz + :srandom + else + :else + end + RUBY + mutations << <<-RUBY + case :condition + when :foo + :foo + when :bar, :baz + nil + else + :else + end + RUBY + mutations << <<-RUBY + case :condition + when :foo + :foo + when :bar, :baz + :barbaz + else + :srandom + end + RUBY + mutations << <<-RUBY + case :condition + when :foo + :foo + when :bar, :baz + :barbaz + else + nil + end + RUBY + + # Mutations of when conditions + mutations << <<-RUBY + case :condition + when :srandom + :foo + when :bar, :baz + :barbaz + else + :else + end + RUBY + mutations << <<-RUBY + case :condition + when nil + :foo + when :bar, :baz + :barbaz + else + :else + end + RUBY + mutations << <<-RUBY + case :condition + when :foo + :foo + when :srandom, :baz + :barbaz + else + :else + end + RUBY + mutations << <<-RUBY + case :condition + when :foo + :foo + when nil, :baz + :barbaz + else + :else + end + RUBY + mutations << <<-RUBY + case :condition + when :foo + :foo + when :bar, nil + :barbaz + else + :else + end + RUBY + mutations << <<-RUBY + case :condition + when :foo + :foo + when :bar, :srandom + :barbaz + else + :else + end + RUBY + mutations << <<-RUBY + case :condition + when :foo + :foo + when :baz + :barbaz + else + :else + end + RUBY + mutations << <<-RUBY + case :condition + when :foo + :foo + when :bar + :barbaz + else + :else + end + RUBY + end + + it_should_behave_like 'a mutator' end - - let(:mutations) do - mutations = [] - - # Presence of branches - mutations << <<-RUBY - case :condition - when :bar, :baz - :barbaz - else - :else - end - RUBY - mutations << <<-RUBY - case :condition - when :foo - :foo - else - :else - end - RUBY - mutations << <<-RUBY - case :condition - when :foo - :foo - when :bar, :baz - :barbaz - end - RUBY - - # Mutations of condition - mutations << <<-RUBY - case nil - when :foo - :foo - when :bar, :baz - :barbaz - else - :else - end - RUBY - mutations << <<-RUBY - case :srandom - when :foo - :foo - when :bar, :baz - :barbaz - else - :else - end - RUBY - - # Mutations of branch bodies - mutations << <<-RUBY - case :condition - when :foo - nil - when :bar, :baz - :barbaz - else - :else - end - RUBY - mutations << <<-RUBY - case :condition - when :foo - :srandom - when :bar, :baz - :barbaz - else - :else - end - RUBY - mutations << <<-RUBY - case :condition - when :foo - :foo - when :bar, :baz - :srandom - else - :else - end - RUBY - mutations << <<-RUBY - case :condition - when :foo - :foo - when :bar, :baz - nil - else - :else - end - RUBY - mutations << <<-RUBY - case :condition - when :foo - :foo - when :bar, :baz - :barbaz - else - :srandom - end - RUBY - mutations << <<-RUBY - case :condition - when :foo - :foo - when :bar, :baz - :barbaz - else - nil - end - RUBY - - # Mutations of when conditions - mutations << <<-RUBY - case :condition - when :srandom - :foo - when :bar, :baz - :barbaz - else - :else - end - RUBY - mutations << <<-RUBY - case :condition - when nil - :foo - when :bar, :baz - :barbaz - else - :else - end - RUBY - mutations << <<-RUBY - case :condition - when :foo - :foo - when :srandom, :baz - :barbaz - else - :else - end - RUBY - mutations << <<-RUBY - case :condition - when :foo - :foo - when nil, :baz - :barbaz - else - :else - end - RUBY - mutations << <<-RUBY - case :condition - when :foo - :foo - when :bar, nil - :barbaz - else - :else - end - RUBY - mutations << <<-RUBY - case :condition - when :foo - :foo - when :bar, :srandom - :barbaz - else - :else - end - RUBY - mutations << <<-RUBY - case :condition - when :foo - :foo - when :baz - :barbaz - else - :else - end - RUBY - mutations << <<-RUBY - case :condition - when :foo - :foo - when :bar - :barbaz - else - :else - end - RUBY - end - - it_should_behave_like 'a mutator' end From d3470d08cbd743f80cc30a2ddd96a82299d9c009 Mon Sep 17 00:00:00 2001 From: Dan Kubb Date: Sat, 22 Jun 2013 23:00:40 -0700 Subject: [PATCH 3/3] Fix mutation with one when clause in a case statement * When there was only one when clause, one mutation would remove it which would cause the case statement to become invalid. --- lib/mutant/mutator/node/case.rb | 1 + .../mutant/mutator/node/case/mutation_spec.rb | 97 +++++++++++++++++++ 2 files changed, 98 insertions(+) diff --git a/lib/mutant/mutator/node/case.rb b/lib/mutant/mutator/node/case.rb index c3ede50d..04907b7a 100644 --- a/lib/mutant/mutator/node/case.rb +++ b/lib/mutant/mutator/node/case.rb @@ -47,6 +47,7 @@ module Mutant mutate_child(index) dup_children = children.dup dup_children.delete_at(index) + return if dup_children.none? { |child| child.type == :when } if dup_children.last.type == :when dup_children << nil end diff --git a/spec/unit/mutant/mutator/node/case/mutation_spec.rb b/spec/unit/mutant/mutator/node/case/mutation_spec.rb index 362306ea..28eb269b 100644 --- a/spec/unit/mutant/mutator/node/case/mutation_spec.rb +++ b/spec/unit/mutant/mutator/node/case/mutation_spec.rb @@ -219,4 +219,101 @@ describe Mutant::Mutator::Node::Case do it_should_behave_like 'a mutator' end + + context 'with one when branch' do + let(:source) do + <<-RUBY + case :condition + when :foo + :foo + else + :else + end + RUBY + end + + let(:mutations) do + mutations = [] + + # Presence of branches + mutations << <<-RUBY + case :condition + when :foo + :foo + end + RUBY + + # Mutations of condition + mutations << <<-RUBY + case nil + when :foo + :foo + else + :else + end + RUBY + mutations << <<-RUBY + case :srandom + when :foo + :foo + else + :else + end + RUBY + + # Mutations of branch bodies + mutations << <<-RUBY + case :condition + when :foo + nil + else + :else + end + RUBY + mutations << <<-RUBY + case :condition + when :foo + :srandom + else + :else + end + RUBY + mutations << <<-RUBY + case :condition + when :foo + :foo + else + :srandom + end + RUBY + mutations << <<-RUBY + case :condition + when :foo + :foo + else + nil + end + RUBY + + # Mutations of when conditions + mutations << <<-RUBY + case :condition + when :srandom + :foo + else + :else + end + RUBY + mutations << <<-RUBY + case :condition + when nil + :foo + else + :else + end + RUBY + end + + it_should_behave_like 'a mutator' + end end