free_mutant/lib/mutant/mutator/registry.rb

56 lines
1.3 KiB
Ruby
Raw Normal View History

module Mutant
class Mutator
# Registry for mutators
class Registry
# Initialize object
#
# @return [undefined]
#
# @api private
def initialize
@registry = {}
end
# Raised when the type is an invalid type
RegistryError = Class.new(TypeError)
# Register mutator class for AST node class
#
2013-06-21 12:09:46 -04:00
# @param [Symbol] type
# @param [Class:Mutator] mutator
#
# @return [self]
#
2015-07-01 23:35:54 -04:00
# @api private
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
self
end
2013-06-14 14:54:02 -04:00
# Lookup mutator class for node
#
# @param [Parser::AST::Node] node
#
# @return [Class]
#
# @raise [ArgumentError]
# raises argument error when mutator class cannot be found
#
# @api private
def lookup(node)
type = node.type
@registry.fetch(type) do
fail RegistryError, "No mutator to handle: #{type.inspect}"
end
end
2013-06-14 14:54:02 -04:00
end # Registry
REGISTRY = Registry.new
2013-06-14 14:54:02 -04:00
end # Mutator
end # Mutant