2013-07-28 19:03:06 -04:00
|
|
|
# encoding: utf-8
|
|
|
|
|
2012-08-01 07:27:35 -04:00
|
|
|
module Mutant
|
|
|
|
class Mutator
|
|
|
|
# Registry for mutators
|
|
|
|
module Registry
|
2013-07-28 18:14:39 -04:00
|
|
|
|
|
|
|
# Raised when the type is an invalid type
|
|
|
|
InvalidTypeError = Class.new(TypeError)
|
|
|
|
|
|
|
|
# Raised when the type is a duplicate
|
|
|
|
DuplicateTypeError = Class.new(ArgumentError)
|
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
|
2012-08-01 07:27:35 -04:00
|
|
|
# @param [Class] mutator_class
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
# @return [self]
|
|
|
|
#
|
2013-06-21 12:09:46 -04:00
|
|
|
def self.register(type, mutator_class)
|
2013-07-28 18:14:39 -04:00
|
|
|
assert_valid_type(type)
|
|
|
|
assert_unique_type(type)
|
2013-07-27 14:21:27 -04:00
|
|
|
registry[type] = mutator_class
|
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
|
|
|
|
#
|
2013-06-04 13:26:53 -04:00
|
|
|
def self.lookup(node)
|
|
|
|
type = node.type
|
|
|
|
registry.fetch(type) do
|
2013-07-27 14:21:27 -04:00
|
|
|
raise ArgumentError, "No mutator to handle: #{type.inspect}"
|
2012-08-01 07:27:35 -04:00
|
|
|
end
|
|
|
|
end
|
2013-06-14 14:54:02 -04:00
|
|
|
|
2013-07-28 18:14:39 -04:00
|
|
|
# Return registry state
|
|
|
|
#
|
|
|
|
# @return [Hash]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def self.registry
|
|
|
|
@registry ||= {}
|
|
|
|
end
|
|
|
|
private_class_method :registry
|
|
|
|
|
|
|
|
# Assert the node type is valid
|
|
|
|
#
|
2013-07-28 18:19:01 -04:00
|
|
|
# @return [undefined]
|
|
|
|
#
|
2013-07-28 18:14:39 -04:00
|
|
|
# @raise [InvalidTypeError]
|
|
|
|
# raised when the node type is invalid
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def self.assert_valid_type(type)
|
|
|
|
unless NODE_TYPES.include?(type) || type.kind_of?(Class)
|
|
|
|
raise InvalidTypeError, "invalid type registration: #{type}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
private_class_method :assert_valid_type
|
|
|
|
|
|
|
|
# Assert the node type is unique and not already registered
|
|
|
|
#
|
2013-07-28 18:19:01 -04:00
|
|
|
# @return [undefined]
|
|
|
|
#
|
2013-07-28 18:14:39 -04:00
|
|
|
# @raise [DuplcateTypeError]
|
|
|
|
# raised when the node type is a duplicate
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def self.assert_unique_type(type)
|
|
|
|
if registry.key?(type)
|
|
|
|
raise DuplicateTypeError, "duplicate type registration: #{type}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
private_class_method :assert_unique_type
|
|
|
|
|
2013-06-14 14:54:02 -04:00
|
|
|
end # Registry
|
|
|
|
end # Mutator
|
|
|
|
end # Mutant
|