2012-07-26 13:25:23 -04:00
|
|
|
module Mutant
|
2012-07-30 22:00:05 -04:00
|
|
|
# Generator for mutations
|
2012-07-26 13:25:23 -04:00
|
|
|
class Mutator
|
2012-11-26 05:30:00 -05:00
|
|
|
include Adamantium::Flat, AbstractType
|
2012-07-27 16:39:31 -04:00
|
|
|
|
2012-12-06 15:30:28 -05:00
|
|
|
# Run mutator on input
|
2012-07-27 16:39:31 -04:00
|
|
|
#
|
2013-06-04 13:26:53 -04:00
|
|
|
# @param [Parser::AST::Node] node
|
2012-07-30 22:00:05 -04:00
|
|
|
# @param [#call] block
|
2012-07-27 16:39:31 -04:00
|
|
|
#
|
2012-07-30 22:00:05 -04:00
|
|
|
# @return [self]
|
2012-07-27 16:39:31 -04:00
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2013-06-04 13:26:53 -04:00
|
|
|
def self.each(node, &block)
|
|
|
|
return to_enum(__method__, node) unless block_given?
|
|
|
|
Registry.lookup(node).new(node, block)
|
2012-07-30 22:00:05 -04:00
|
|
|
|
|
|
|
self
|
2012-07-27 16:39:31 -04:00
|
|
|
end
|
|
|
|
|
2012-07-31 13:45:46 -04:00
|
|
|
# Register node class handler
|
|
|
|
#
|
|
|
|
# @return [undefined]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2013-06-04 04:25:13 -04:00
|
|
|
def self.handle(*types)
|
|
|
|
types.each do |type|
|
|
|
|
Registry.register(type, self)
|
2013-06-04 09:27:16 -04:00
|
|
|
end
|
2012-07-27 16:39:31 -04:00
|
|
|
end
|
2012-07-31 13:45:46 -04:00
|
|
|
private_class_method :handle
|
2012-07-27 16:39:31 -04:00
|
|
|
|
2012-12-10 18:17:19 -05:00
|
|
|
# Return identity of object (for deduplication)
|
|
|
|
#
|
2012-12-12 16:31:14 -05:00
|
|
|
# @param [Object] object
|
2012-12-10 18:17:19 -05:00
|
|
|
#
|
|
|
|
# @return [Object]
|
|
|
|
#
|
2012-12-12 16:31:14 -05:00
|
|
|
# @api private
|
|
|
|
#
|
2012-12-10 18:17:19 -05:00
|
|
|
def self.identity(object)
|
|
|
|
object
|
|
|
|
end
|
|
|
|
|
2012-12-06 15:30:28 -05:00
|
|
|
# Return input
|
2012-10-26 05:24:29 -04:00
|
|
|
#
|
2012-12-06 15:30:28 -05:00
|
|
|
# @return [Object]
|
2012-10-26 05:24:29 -04:00
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2012-12-06 15:30:28 -05:00
|
|
|
attr_reader :input
|
2012-10-26 05:24:29 -04:00
|
|
|
|
2012-07-30 22:00:05 -04:00
|
|
|
private
|
|
|
|
|
2012-12-06 15:30:28 -05:00
|
|
|
# Initialize object
|
2012-07-30 22:00:05 -04:00
|
|
|
#
|
2012-12-06 15:30:28 -05:00
|
|
|
# @param [Object] input
|
2012-07-30 22:00:05 -04:00
|
|
|
# @param [#call(node)] block
|
|
|
|
#
|
|
|
|
# @return [undefined]
|
2012-07-27 16:39:31 -04:00
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2012-12-06 15:30:28 -05:00
|
|
|
def initialize(input, block)
|
2012-12-10 18:17:19 -05:00
|
|
|
@input, @block = IceNine.deep_freeze(input), block
|
|
|
|
@seen = Set.new
|
|
|
|
guard(input)
|
2012-07-30 22:00:05 -04:00
|
|
|
dispatch
|
|
|
|
end
|
|
|
|
|
2012-12-10 18:17:19 -05:00
|
|
|
# Test if generated object is not guarded from emmitting
|
2012-12-06 15:30:28 -05:00
|
|
|
#
|
|
|
|
# @param [Object] object
|
|
|
|
#
|
|
|
|
# @return [true]
|
|
|
|
# if generated object is different
|
|
|
|
#
|
|
|
|
# @return [false]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def new?(object)
|
2012-12-10 18:17:19 -05:00
|
|
|
!@seen.include?(self.class.identity(object))
|
|
|
|
end
|
|
|
|
|
|
|
|
# Add object to guarded values
|
|
|
|
#
|
|
|
|
# @param [Object] object
|
|
|
|
#
|
|
|
|
# @return [undefined]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def guard(object)
|
|
|
|
@seen << self.class.identity(object)
|
2012-12-06 15:30:28 -05:00
|
|
|
end
|
|
|
|
|
2012-07-30 22:00:05 -04:00
|
|
|
# Dispatch node generations
|
|
|
|
#
|
|
|
|
# @return [undefined]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2012-08-02 19:28:15 -04:00
|
|
|
abstract_method :dispatch
|
2012-07-30 22:00:05 -04:00
|
|
|
|
2012-12-06 15:30:28 -05:00
|
|
|
# Emit generated mutation if object is not equivalent to input
|
2012-07-30 22:00:05 -04:00
|
|
|
#
|
2012-12-06 15:30:28 -05:00
|
|
|
# @param [Object] object
|
2012-07-30 22:00:05 -04:00
|
|
|
#
|
2012-12-06 15:30:28 -05:00
|
|
|
# @return [undefined]
|
2012-07-30 22:00:05 -04:00
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2012-12-06 15:30:28 -05:00
|
|
|
def emit(object)
|
2013-06-15 11:15:37 -04:00
|
|
|
return unless new?(object)
|
2012-12-06 15:30:28 -05:00
|
|
|
|
2012-12-10 18:17:19 -05:00
|
|
|
guard(object)
|
|
|
|
|
2012-12-06 15:30:28 -05:00
|
|
|
emit!(object)
|
2012-07-27 16:39:31 -04:00
|
|
|
end
|
|
|
|
|
2012-12-06 15:30:28 -05:00
|
|
|
# Maximum amount of tries to generate a new object
|
2012-07-30 22:00:05 -04:00
|
|
|
MAX_TRIES = 3
|
2012-07-27 16:39:31 -04:00
|
|
|
|
2012-12-06 15:30:28 -05:00
|
|
|
# Call block until it generates a mutation
|
2012-07-27 16:39:31 -04:00
|
|
|
#
|
2012-12-06 15:30:28 -05:00
|
|
|
# The primary use of this method is to give the random generated object
|
2012-07-30 22:00:05 -04:00
|
|
|
# a nice interface for retring generation when generation accidentally generated the
|
2012-12-06 15:30:28 -05:00
|
|
|
# input
|
2012-07-30 22:00:05 -04:00
|
|
|
#
|
2012-07-30 22:10:37 -04:00
|
|
|
# @yield
|
2012-12-06 15:30:28 -05:00
|
|
|
# Execute block until object is generated where new?(object) returns true
|
2012-07-30 22:00:05 -04:00
|
|
|
#
|
|
|
|
# @return [self]
|
|
|
|
#
|
|
|
|
# @raise [RuntimeError]
|
|
|
|
# raises RuntimeError in case no new ast node can be generated after MAX_TRIES.
|
2012-07-27 16:39:31 -04:00
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2012-07-30 22:00:05 -04:00
|
|
|
def emit_new
|
2012-07-30 22:10:37 -04:00
|
|
|
MAX_TRIES.times do
|
2012-12-06 15:30:28 -05:00
|
|
|
object = yield
|
|
|
|
|
|
|
|
if new?(object)
|
|
|
|
emit!(object)
|
2012-07-30 22:00:05 -04:00
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
2012-07-27 16:39:31 -04:00
|
|
|
|
2012-07-30 22:00:05 -04:00
|
|
|
raise "New AST could not be generated after #{MAX_TRIES} attempts"
|
|
|
|
end
|
|
|
|
|
|
|
|
# Call block with node
|
2012-07-30 22:10:37 -04:00
|
|
|
#
|
2013-06-04 04:25:13 -04:00
|
|
|
# @param [Parser::AST::Node] node
|
2012-07-30 22:00:05 -04:00
|
|
|
#
|
|
|
|
# @return [self]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2012-08-15 22:10:54 -04:00
|
|
|
def emit!(node)
|
2012-07-30 22:00:05 -04:00
|
|
|
@block.call(node)
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
2012-12-29 10:53:52 -05:00
|
|
|
# Run input with mutator
|
|
|
|
#
|
|
|
|
# @return [undefined]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def run(mutator)
|
2012-12-29 14:12:48 -05:00
|
|
|
mutator.new(input, method(:emit))
|
2012-12-29 10:53:52 -05:00
|
|
|
end
|
|
|
|
|
2012-12-06 15:30:28 -05:00
|
|
|
# Shortcut to create a new unfrozen duplicate of input
|
2012-07-26 13:25:23 -04:00
|
|
|
#
|
2012-12-06 15:30:28 -05:00
|
|
|
# @return [Object]
|
2012-07-30 22:00:05 -04:00
|
|
|
#
|
2012-07-26 13:25:23 -04:00
|
|
|
# @api private
|
|
|
|
#
|
2012-12-06 15:30:28 -05:00
|
|
|
def dup_input
|
2012-12-10 18:17:19 -05:00
|
|
|
Helper.deep_clone(input)
|
2012-07-26 13:25:23 -04:00
|
|
|
end
|
|
|
|
|
2013-06-04 04:25:13 -04:00
|
|
|
end # Mutator
|
|
|
|
end # Mutant
|