free_mutant/lib/mutant/matcher/method/instance.rb
Markus Schirp 3390abc675 Use environment object inside matchers
This will allow to use reporters from matchers avoiding stupid direct
writes to $stderr.

Also it will allow to remove matching from CLI altogether in a phase of
the runner. Allowing to decouple Mutant::Config from VM environment
allowing to serialize it ;)

Step by step. Takes a while.
2014-06-30 13:08:52 +00:00

79 lines
1.9 KiB
Ruby

module Mutant
class Matcher
class Method
# Matcher for instance methods
class Instance < self
SUBJECT_CLASS = Subject::Method::Instance
# Dispatching builder, detects memoizable case
#
# @param [Env] env
# @param [Class, Module] scope
# @param [UnboundMethod] method
#
# @return [Matcher::Method::Instance]
#
# @api private
#
def self.build(env, scope, method)
name = method.name
if scope.ancestors.include?(::Memoizable) && scope.memoized?(name)
return Memoized.new(env, scope, method)
end
super
end
# Return identification
#
# @return [String]
#
# @api private
#
def identification
"#{scope.name}##{method_name}"
end
memoize :identification
NAME_INDEX = 0
private
# Check if node is matched
#
# @param [Parser::AST::Node] node
#
# @return [Boolean]
#
# @api private
#
def match?(node)
location = node.location || return
expression = location.expression || return
expression.line.equal?(source_line) &&
node.type.equal?(:def) &&
node.children[NAME_INDEX].equal?(method_name)
end
# Matcher for memoized instance methods
class Memoized < self
SUBJECT_CLASS = Subject::Method::Instance::Memoized
private
# Return source location
#
# @return [Array]
#
# @api private
#
def source_location
scope.unmemoized_instance_method(method.name).source_location
end
end # Memoized
end # Instance
end # Method
end # Matcher
end # Mutant