2012-08-01 07:27:35 -04:00
|
|
|
module Mutant
|
|
|
|
class Mutator
|
|
|
|
# Registry for mutators
|
2014-11-17 12:05:02 -05:00
|
|
|
class Registry
|
2013-07-28 18:14:39 -04:00
|
|
|
|
2014-11-17 12:05:02 -05:00
|
|
|
# Initialize object
|
|
|
|
#
|
|
|
|
# @return [undefined]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
def initialize
|
|
|
|
@registry = {}
|
|
|
|
end
|
2013-07-28 18:14:39 -04:00
|
|
|
|
2014-11-17 12:05:02 -05:00
|
|
|
# Raised when the type is an invalid type
|
|
|
|
RegistryError = Class.new(TypeError)
|
2012-08-01 07:27:35 -04:00
|
|
|
|
|
|
|
# Register mutator class for AST node class
|
|
|
|
#
|
2013-06-21 12:09:46 -04:00
|
|
|
# @param [Symbol] type
|
2014-11-17 12:05:02 -05:00
|
|
|
# @param [Class:Mutator] mutator
|
2012-08-01 07:27:35 -04:00
|
|
|
#
|
|
|
|
# @return [self]
|
|
|
|
#
|
2015-07-01 23:35:54 -04:00
|
|
|
# @api private
|
2014-11-17 12:05:02 -05:00
|
|
|
def register(type, mutator)
|
|
|
|
fail RegistryError, "Invalid type registration: #{type}" unless AST::Types::ALL.include?(type)
|
|
|
|
fail RegistryError, "Duplicate type registration: #{type}" if @registry.key?(type)
|
|
|
|
@registry[type] = mutator
|
2012-08-01 07:27:35 -04:00
|
|
|
self
|
|
|
|
end
|
|
|
|
|
2013-06-14 14:54:02 -04:00
|
|
|
# Lookup mutator class for node
|
2012-08-01 07:27:35 -04:00
|
|
|
#
|
2013-06-04 13:26:53 -04:00
|
|
|
# @param [Parser::AST::Node] node
|
2012-08-01 07:27:35 -04:00
|
|
|
#
|
|
|
|
# @return [Class]
|
|
|
|
#
|
|
|
|
# @raise [ArgumentError]
|
|
|
|
# raises argument error when mutator class cannot be found
|
|
|
|
#
|
|
|
|
# @api private
|
2014-11-17 12:05:02 -05:00
|
|
|
def lookup(node)
|
2013-06-04 13:26:53 -04:00
|
|
|
type = node.type
|
2014-11-17 12:05:02 -05:00
|
|
|
@registry.fetch(type) do
|
|
|
|
fail RegistryError, "No mutator to handle: #{type.inspect}"
|
2012-08-01 07:27:35 -04:00
|
|
|
end
|
|
|
|
end
|
2013-06-14 14:54:02 -04:00
|
|
|
|
2014-11-17 12:05:02 -05:00
|
|
|
end # Registry
|
2013-07-28 18:14:39 -04:00
|
|
|
|
2014-11-17 12:05:02 -05:00
|
|
|
REGISTRY = Registry.new
|
2013-07-28 18:14:39 -04:00
|
|
|
|
2013-06-14 14:54:02 -04:00
|
|
|
end # Mutator
|
|
|
|
end # Mutant
|