2012-12-06 15:30:28 -05:00
|
|
|
module Mutant
|
|
|
|
# Generator for mutations
|
|
|
|
class Mutator
|
|
|
|
# Abstract base class for node mutators
|
|
|
|
class Node < self
|
2013-06-04 14:23:06 -04:00
|
|
|
include AbstractType, NodeHelpers
|
2012-12-06 15:30:28 -05:00
|
|
|
|
2012-12-10 18:17:19 -05:00
|
|
|
# Return identity of node
|
2012-12-06 15:30:28 -05:00
|
|
|
#
|
2013-06-04 04:25:13 -04:00
|
|
|
# @param [Parser::AST::Node] node
|
2012-12-06 15:30:28 -05:00
|
|
|
#
|
2012-12-10 18:17:19 -05:00
|
|
|
# @return [String]
|
2012-12-06 15:30:28 -05:00
|
|
|
#
|
2012-12-12 16:31:14 -05:00
|
|
|
# @api private
|
|
|
|
#
|
2012-12-10 18:17:19 -05:00
|
|
|
def self.identity(node)
|
2013-06-04 14:23:06 -04:00
|
|
|
Unparser.unparse(node)
|
2012-12-06 15:30:28 -05:00
|
|
|
end
|
|
|
|
|
2012-12-29 10:34:39 -05:00
|
|
|
private
|
2012-12-10 18:17:19 -05:00
|
|
|
|
2012-12-12 16:31:14 -05:00
|
|
|
# Return mutated node
|
|
|
|
#
|
2013-06-04 04:25:13 -04:00
|
|
|
# @return [Parser::AST::Node]
|
2012-12-12 16:31:14 -05:00
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2012-12-10 18:17:19 -05:00
|
|
|
alias_method :node, :input
|
2012-12-12 16:31:14 -05:00
|
|
|
|
|
|
|
# Return duplicated node
|
|
|
|
#
|
2013-06-04 04:25:13 -04:00
|
|
|
# @return [Parser::AST::Node]
|
2012-12-12 16:31:14 -05:00
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2012-12-10 18:17:19 -05:00
|
|
|
alias_method :dup_node, :dup_input
|
2012-12-06 15:30:28 -05:00
|
|
|
|
2013-06-04 17:44:17 -04:00
|
|
|
# Emit children mutations
|
|
|
|
#
|
|
|
|
# @return [undefined]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def emit_children_mutations
|
|
|
|
Mutator::Util::Array.each(children) do |children|
|
|
|
|
emit_self(children)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-06-04 14:23:06 -04:00
|
|
|
# Return children
|
2012-12-06 15:30:28 -05:00
|
|
|
#
|
2013-06-04 14:23:06 -04:00
|
|
|
# @return [Array<Parser::AST::Node>]
|
2012-12-06 15:30:28 -05:00
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2013-06-04 14:23:06 -04:00
|
|
|
def children
|
|
|
|
node.children
|
2012-12-06 15:30:28 -05:00
|
|
|
end
|
|
|
|
|
2013-06-04 14:23:06 -04:00
|
|
|
# Dispatch on child index
|
2012-12-06 15:30:28 -05:00
|
|
|
#
|
2013-06-04 14:23:06 -04:00
|
|
|
# @param [Fixnum] index
|
2012-12-06 15:30:28 -05:00
|
|
|
#
|
2013-06-04 14:23:06 -04:00
|
|
|
# @return [undefined]
|
2012-12-06 15:30:28 -05:00
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2013-06-04 14:23:06 -04:00
|
|
|
def mutate_child(index, mutator = Mutator)
|
|
|
|
children = node.children
|
|
|
|
child = children[index]
|
|
|
|
mutator.each(child) do |mutation|
|
|
|
|
emit_child_update(index, mutation)
|
|
|
|
end
|
2012-12-06 15:30:28 -05:00
|
|
|
end
|
|
|
|
|
2013-06-04 14:23:06 -04:00
|
|
|
# Emit updated child
|
2012-12-06 15:30:28 -05:00
|
|
|
#
|
2013-06-04 14:23:06 -04:00
|
|
|
# @param [Fixnum] index
|
|
|
|
# @param [Object] update
|
2012-12-06 15:30:28 -05:00
|
|
|
#
|
|
|
|
# @return [undefined]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2013-06-04 14:23:06 -04:00
|
|
|
def emit_child_update(index, update)
|
|
|
|
new_children = children.dup
|
|
|
|
new_children[index]=update
|
|
|
|
emit_self(new_children)
|
2012-12-06 15:30:28 -05:00
|
|
|
end
|
|
|
|
|
2013-06-04 14:23:06 -04:00
|
|
|
# Emit a new AST node with same class as wrapped node
|
2012-12-06 15:30:28 -05:00
|
|
|
#
|
2013-06-04 14:23:06 -04:00
|
|
|
# @param [Array<Parser::AST::Node>] children
|
2012-12-06 15:30:28 -05:00
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2013-06-04 14:23:06 -04:00
|
|
|
def emit_self(children)
|
|
|
|
emit(Parser::AST::Node.new(node.type, children))
|
2012-12-06 15:30:28 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# Emit a new AST node with NilLiteral class
|
|
|
|
#
|
|
|
|
# @return [Rubinius::AST::NilLiteral]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def emit_nil
|
2013-06-04 14:23:06 -04:00
|
|
|
emit(s(:nil))
|
2012-12-06 15:30:28 -05:00
|
|
|
end
|
|
|
|
|
2013-06-04 04:25:13 -04:00
|
|
|
end # Node
|
|
|
|
end # Mutator
|
|
|
|
end # Mutant
|