free_mutant/lib/mutant/mutator/node.rb

154 lines
3 KiB
Ruby
Raw Normal View History

module Mutant
# Generator for mutations
class Mutator
# Abstract base class for node mutators
class Node < self
2013-06-14 13:35:09 -04:00
include AbstractType, NodeHelpers, Unparser::Constants
# Return identity of node
#
# @param [Parser::AST::Node] node
#
# @return [String]
#
# @api private
#
def self.identity(node)
Unparser.unparse(node)
end
private
# Return mutated node
#
# @return [Parser::AST::Node]
#
# @api private
#
alias_method :node, :input
# Return duplicated node
#
# @return [Parser::AST::Node]
#
# @api private
#
alias_method :dup_node, :dup_input
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|
2013-06-10 04:15:59 -04:00
emit_self(*children)
2013-06-04 17:44:17 -04:00
end
end
# Return children
#
# @return [Array<Parser::AST::Node>]
#
# @api private
#
def children
node.children
end
# Dispatch on child index
#
# @param [Fixnum] index
#
# @return [undefined]
#
# @api private
#
def mutate_child(index, mutator = Mutator)
children = node.children
child = children[index]
mutator.each(child) do |mutation|
emit_child_update(index, mutation)
end
end
2013-06-14 12:22:34 -04:00
# Emit delete child mutation
#
# @param [Fixnum] index
#
# @return [undefined]
#
# @api private
#
def delete_child(index)
dup_children = children.dup
dup_children.delete_at(index)
emit_self(*dup_children)
end
# Emit updated child
#
# @param [Fixnum] index
# @param [Object] update
#
# @return [undefined]
#
# @api private
#
def emit_child_update(index, update)
new_children = children.dup
new_children[index]=update
2013-06-10 04:15:59 -04:00
emit_self(*new_children)
end
# Emit a new AST node with same class as wrapped node
#
# @param [Array<Parser::AST::Node>] children
#
2013-06-15 10:37:43 -04:00
# @return [undefined]
#
# @api private
#
2013-06-10 04:15:59 -04:00
def emit_self(*children)
emit(new_self(*children))
end
# Emit a new AST node with NilLiteral class
#
# @return [Rubinius::AST::NilLiteral]
#
# @api private
#
def emit_nil
emit(s(:nil))
end
2013-06-10 04:15:59 -04:00
# Return new self typed child
#
# @return [Parser::AST::Node]
#
# @api private
#
def new_self(*children)
Parser::AST::Node.new(node.type, children)
end
2013-06-12 13:09:43 -04:00
# Emit values
#
# @param [Array<Object>] values
#
# @return [undefined]
#
# @api private
#
def emit_values(values)
values.each do |value|
emit_self(value)
end
end
end # Node
end # Mutator
end # Mutant