Use a type widening lookup strategy to DRY up printers

This commit is contained in:
Markus Schirp 2013-09-07 14:08:07 +02:00
parent 9bea135f3e
commit 76b8e3bbe7
4 changed files with 39 additions and 19 deletions

View file

@ -63,10 +63,34 @@ module Mutant
# @api private
#
def self.visit(object, output)
printer = REGISTRY.fetch(object.class)
printer = lookup(object.class)
printer.run(object, output)
end
# Lookup printer class
#
# @param [Class] klass
#
# @return [Class:Printer]
# if found
#
# @raise [RuntimeError]
# otherwise
#
# @api private
#
def self.lookup(klass)
current = klass
until current == Object
if REGISTRY.key?(current)
return REGISTRY.fetch(current)
end
current = current.superclass
end
raise "No printer for: #{klass}"
end
private_class_method :lookup
abstract_method :run
private

View file

@ -8,7 +8,7 @@ module Mutant
# Printer for killer results
class Killer < self
handle(Mutant::Killer::Forked)
handle(Mutant::Killer)
SUCCESS = '.'.freeze
FAILURE = 'F'.freeze

View file

@ -20,14 +20,7 @@ module Mutant
#
def self.build(runner, output)
mutation = runner.mutation
case mutation
when Mutant::Mutation::Neutral::Noop
Noop
when Mutant::Mutation::Evil, Mutant::Mutation::Neutral
Diff
else
raise "Unknown mutation: #{mutation}"
end.new(runner, output)
lookup(mutation.class).new(runner, output)
end
# Run mutation printer
@ -56,12 +49,14 @@ module Mutant
# Reporter for noop mutations
class Noop < self
MESSAGE = [
'Parsed subject AST:',
'%s',
'Unparsed source:',
'%s',
].join("\n")
handle(Mutant::Mutation::Neutral::Noop)
MESSAGE = [
'Parsed subject AST:',
'%s',
'Unparsed source:',
'%s',
].join("\n")
private
@ -84,6 +79,9 @@ module Mutant
# Reporter for neutral and evil mutations
class Diff < self
handle(Mutant::Mutation::Neutral)
handle(Mutant::Mutation::Evil)
# Return diff
#
# @return [String]

View file

@ -8,9 +8,7 @@ module Mutant
# Subject results printer
class Subject < self
handle(Mutant::Subject::Method::Instance)
handle(Mutant::Subject::Method::Instance::Memoized)
handle(Mutant::Subject::Method::Singleton)
handle(Mutant::Subject)
# Run subject results printer
#