diff --git a/lib/mutant/mutator/node/begin.rb b/lib/mutant/mutator/node/begin.rb index aa3f37f7..4d54b891 100644 --- a/lib/mutant/mutator/node/begin.rb +++ b/lib/mutant/mutator/node/begin.rb @@ -1,6 +1,7 @@ module Mutant class Mutator class Node + # Mutator for begin nodes class Begin < self @@ -15,7 +16,15 @@ module Mutant # @api private # def dispatch - emit_children_mutations + Util::Array.each(children) do |children| + if children.length > 1 + emit_self(*children) + end + end + children.each do |child| + emit(child) + end + emit(nil) end end # Block diff --git a/spec/unit/mutant/mutator/node/begin/mutation_spec.rb b/spec/unit/mutant/mutator/node/begin/mutation_spec.rb index 8b8a8ba4..1fa383be 100644 --- a/spec/unit/mutant/mutator/node/begin/mutation_spec.rb +++ b/spec/unit/mutant/mutator/node/begin/mutation_spec.rb @@ -1,37 +1,33 @@ require 'spec_helper' -describe Mutant::Mutator, 'block' do +describe Mutant::Mutator, 'begin' do - context 'with more than one statement' do - let(:source) { "true\nfalse" } - - let(:mutations) do - mutations = [] - - # Mutation of each statement in block - mutations << "true\ntrue" - mutations << "false\nfalse" - mutations << "nil\nfalse" - mutations << "true\nnil" - - # Remove statement in block - mutations << s(:begin, s(:true)) - mutations << s(:begin, s(:false)) - mutations << 'nil' - end - - it_should_behave_like 'a mutator' + # This mutation and only this mutation can result in + # and empty emit that is parsed into nil, unparser cannot + # handle this so we guard this here! + def generate(node) + return '' if node.nil? + super end - context 'with one statement' do - let(:source) { 'true' } + let(:source) { "true\nfalse" } - let(:mutations) do - mutations = [] - mutations << s(:false) - mutations << s(:nil) - end + let(:mutations) do + mutations = [] - it_should_behave_like 'a mutator' + # Mutation of each statement in block + mutations << "true\ntrue" + mutations << "false\nfalse" + mutations << "nil\nfalse" + mutations << "true\nnil" + + # Remove statement in block + mutations << 'true' + mutations << 'false' + + # Replace block with empty + mutations << '' end + + it_should_behave_like 'a mutator' end