Fix invalid AST emit

* Mutant prevents double emits via guarding on source representation of
  partial AST nodes. Not all AST nodes are valid as roots, so this
  change creates a valid root around an leave.

* Over the time multiple of these situartions must be solved, we'll end
  up in a more deduplicated solution.

* Closes: #165
This commit is contained in:
Markus Schirp 2014-03-23 00:33:45 +00:00
parent 4b1566caa6
commit e93960ca35
4 changed files with 41 additions and 3 deletions

View file

@ -87,6 +87,7 @@ require 'mutant/mutator/node/if'
require 'mutant/mutator/node/case'
require 'mutant/mutator/node/splat'
require 'mutant/mutator/node/resbody'
require 'mutant/mutator/node/rescue'
require 'mutant/config'
require 'mutant/loader'
require 'mutant/context'

View file

@ -10,9 +10,7 @@ module Mutant
# These nodes still need a dedicated mutator,
# your contribution is that close!
handle(
:ensure,
:rescue, :redo, :defined?,
:regopt, :retry, :arg_expr,
:ensure, :redo, :defined?, :regopt, :retry, :arg_expr,
:kwrestarg, :kwoptarg, :kwarg, :undef, :module, :empty,
:alias, :for, :xstr, :back_ref, :class,
:sclass, :match_with_lvasgn, :match_current_line, :while_post,

View file

@ -0,0 +1,26 @@
# encoding: utf-8
module Mutant
class Mutator
class Node
# Mutator for rescue nodes
class Rescue < Generic
handle :rescue
# Return identity
#
# @param [Parser::AST::Node] node
#
# @return [String]
#
# @api private
#
def self.identity(node)
super(NodeHelpers.s(:kwbegin, node))
end
end # Rescue
end # Node
end # Mutator
end # Mutant

View file

@ -14,6 +14,19 @@ describe Mutant::Mutator, 'def' do
it_should_behave_like 'a mutator'
end
context 'empty rescue body' do
let(:source) { "def foo\nfoo\nrescue\nend" }
let(:mutations) do
mutations = []
mutations << 'def foo; raise; end'
mutations << 'def foo; nil; rescue; end'
mutations << 'def foo; end'
end
it_should_behave_like 'a mutator'
end
context 'with no arguments' do
let(:source) { 'def foo; true; false; end' }