2012-12-06 15:30:28 -05:00
|
|
|
module Mutant
|
2013-07-29 20:34:02 -04:00
|
|
|
|
2012-12-06 15:30:28 -05:00
|
|
|
# Generator for mutations
|
|
|
|
class Mutator
|
2013-07-29 20:34:02 -04:00
|
|
|
|
2012-12-06 15:30:28 -05:00
|
|
|
# Abstract base class for node mutators
|
|
|
|
class Node < self
|
2014-06-29 17:25:17 -04:00
|
|
|
include AbstractType, Unparser::Constants
|
|
|
|
include AST::NamedChildren, AST::NodePredicates, AST::Sexp, AST::Nodes
|
2012-12-06 15:30:28 -05:00
|
|
|
|
2014-08-16 16:23:51 -04:00
|
|
|
TAUTOLOGY = ->(_input) { true }
|
|
|
|
|
2014-06-29 17:25:17 -04:00
|
|
|
# Helper to define a named child
|
|
|
|
#
|
|
|
|
# @param [Parser::AST::Node] node
|
2013-06-21 09:02:47 -04:00
|
|
|
#
|
|
|
|
# @param [Fixnum] index
|
|
|
|
#
|
|
|
|
# @return [undefined]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
def self.define_named_child(name, index)
|
2014-06-29 17:25:17 -04:00
|
|
|
super
|
2013-06-21 09:02:47 -04:00
|
|
|
|
2014-06-05 12:37:31 -04:00
|
|
|
define_method("emit_#{name}_mutations") do |&block|
|
|
|
|
mutate_child(index, &block)
|
2013-06-21 09:02:47 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
define_method("emit_#{name}") do |node|
|
|
|
|
emit_child_update(index, node)
|
|
|
|
end
|
|
|
|
end
|
2014-07-14 09:46:55 -04:00
|
|
|
private_class_method :define_named_child
|
2013-06-21 09:02:47 -04:00
|
|
|
|
2012-12-29 10:34:39 -05:00
|
|
|
private
|
2012-12-10 18:17:19 -05:00
|
|
|
|
2015-07-03 11:21:39 -04:00
|
|
|
# Node to mutate
|
2012-12-12 16:31:14 -05:00
|
|
|
#
|
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
|
|
|
|
2015-07-03 11:21:39 -04:00
|
|
|
# Duplicate of original
|
2012-12-12 16:31:14 -05:00
|
|
|
#
|
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
|
|
|
|
2015-07-03 11:21:39 -04:00
|
|
|
# Original nodes 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
|
2014-06-05 12:37:31 -04:00
|
|
|
def mutate_child(index, mutator = Mutator, &block)
|
2014-08-16 16:23:51 -04:00
|
|
|
block ||= TAUTOLOGY
|
2013-07-04 21:01:59 -04:00
|
|
|
child = children.at(index)
|
2013-06-24 16:10:40 -04:00
|
|
|
mutator.each(child, self) do |mutation|
|
2014-06-08 09:01:26 -04:00
|
|
|
next unless block.call(mutation)
|
|
|
|
emit_child_update(index, mutation)
|
2013-06-04 14:23:06 -04:00
|
|
|
end
|
2012-12-06 15:30:28 -05:00
|
|
|
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)
|
2014-06-02 23:51:22 -04:00
|
|
|
emit_type(*dup_children)
|
2013-06-14 12:22:34 -04: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
|
2013-07-04 21:01:59 -04:00
|
|
|
# @param [Parser::AST::Node] node
|
2012-12-06 15:30:28 -05:00
|
|
|
#
|
|
|
|
# @return [undefined]
|
|
|
|
#
|
|
|
|
# @api private
|
2013-07-04 21:01:59 -04:00
|
|
|
def emit_child_update(index, node)
|
2013-06-04 14:23:06 -04:00
|
|
|
new_children = children.dup
|
2013-07-27 14:21:27 -04:00
|
|
|
new_children[index] = node
|
2014-06-02 23:51:22 -04:00
|
|
|
emit_type(*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
|
|
|
#
|
2013-06-15 10:37:43 -04:00
|
|
|
# @return [undefined]
|
|
|
|
#
|
2012-12-06 15:30:28 -05:00
|
|
|
# @api private
|
2014-06-02 23:51:22 -04:00
|
|
|
def emit_type(*children)
|
2014-06-06 19:05:27 -04:00
|
|
|
emit(Parser::AST::Node.new(node.type, children))
|
2012-12-06 15:30:28 -05:00
|
|
|
end
|
|
|
|
|
2014-06-05 12:37:31 -04:00
|
|
|
# Emit singleton literals
|
|
|
|
#
|
|
|
|
# @return [undefined]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
def emit_singletons
|
|
|
|
emit_nil
|
|
|
|
emit_self
|
|
|
|
end
|
|
|
|
|
|
|
|
# Emit a literal self
|
|
|
|
#
|
|
|
|
# @return [undefined]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
def emit_self
|
|
|
|
emit(N_SELF)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Emit a literal nil
|
2012-12-06 15:30:28 -05:00
|
|
|
#
|
2013-06-25 03:33:28 -04:00
|
|
|
# @return [undefined]
|
2012-12-06 15:30:28 -05:00
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
def emit_nil
|
2014-04-04 14:35:42 -04:00
|
|
|
emit(N_NIL) unless asgn_left?
|
2012-12-06 15:30:28 -05:00
|
|
|
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|
|
2014-06-02 23:51:22 -04:00
|
|
|
emit_type(value)
|
2013-06-12 13:09:43 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-07-03 11:21:39 -04:00
|
|
|
# Parent node
|
2014-06-10 09:42:47 -04:00
|
|
|
#
|
|
|
|
# @return [Parser::AST::Node] node
|
2014-08-07 12:35:30 -04:00
|
|
|
# if parent with node is present
|
2014-06-10 09:42:47 -04:00
|
|
|
#
|
|
|
|
# @return [nil]
|
|
|
|
# otherwise
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
def parent_node
|
2014-06-10 11:43:38 -04:00
|
|
|
parent.node if parent
|
2014-06-10 09:42:47 -04:00
|
|
|
end
|
|
|
|
|
2015-07-03 11:21:39 -04:00
|
|
|
# Parent type
|
2013-07-14 15:37:22 -04:00
|
|
|
#
|
|
|
|
# @return [Symbol] type
|
2014-08-07 12:00:31 -04:00
|
|
|
# if parent with type is present
|
2013-07-14 15:37:22 -04:00
|
|
|
#
|
|
|
|
# @return [nil]
|
|
|
|
# otherwise
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
def parent_type
|
2014-06-10 11:43:38 -04:00
|
|
|
parent_node.type if parent_node
|
2013-07-14 15:37:22 -04:00
|
|
|
end
|
|
|
|
|
2013-09-08 02:10:25 -04:00
|
|
|
# Test if the node is the left of an or_asgn or op_asgn
|
|
|
|
#
|
|
|
|
# @return [Boolean]
|
|
|
|
#
|
|
|
|
# @api private
|
2014-04-04 14:35:42 -04:00
|
|
|
def asgn_left?
|
2014-06-29 17:25:17 -04:00
|
|
|
AST::Types::OP_ASSIGN.include?(parent_type) && parent.node.children.first.equal?(node)
|
2013-09-08 02:10:25 -04:00
|
|
|
end
|
|
|
|
|
2015-07-03 11:21:39 -04:00
|
|
|
# Children indices
|
2014-08-16 16:23:51 -04:00
|
|
|
#
|
|
|
|
# @param [Range] range
|
|
|
|
#
|
|
|
|
# @return [Enumerable<Fixnum>]
|
|
|
|
#
|
2014-12-01 13:33:47 -05:00
|
|
|
# @api private
|
2014-08-16 16:23:51 -04:00
|
|
|
def children_indices(range)
|
|
|
|
range_end = range.end
|
|
|
|
last_index = range_end >= 0 ? range_end : children.length + range_end
|
|
|
|
range.begin.upto(last_index)
|
|
|
|
end
|
|
|
|
|
2013-06-04 04:25:13 -04:00
|
|
|
end # Node
|
|
|
|
end # Mutator
|
|
|
|
end # Mutant
|