free_mutant/lib/mutant/matcher/methods.rb
Markus Schirp 8335cccafb Fix YARD summary line
* Reduce redunrant 'Return' prefix on summaries
* Improve summary line to reflect the semantics of operation better
2015-07-03 15:21:39 +00:00

139 lines
2.9 KiB
Ruby

module Mutant
class Matcher
# Abstract base class for matcher that returns method subjects from scope
class Methods < self
include AbstractType, Concord::Public.new(:env, :scope)
# Enumerate subjects
#
# @return [self]
# if block given
#
# @return [Enumerator<Subject>]
# otherwise
#
# @api private
def each(&block)
return to_enum unless block_given?
subjects.each(&block)
self
end
private
# method matcher class
#
# @return [Class:Matcher::Method]
#
# @api private
def matcher
self.class::MATCHER
end
# Available methods scope
#
# @return [Enumerable<Method, UnboundMethod>]
#
# @api private
def methods
candidate_names.each_with_object([]) do |name, methods|
method = access(name)
methods << method if method.owner.equal?(candidate_scope)
end
end
memoize :methods
# Subjects detected on scope
#
# @return [Array<Subject>]
#
# @api private
def subjects
methods.map do |method|
matcher.build(env, scope, method)
end.flat_map(&:to_a)
end
memoize :subjects
# Candidate method names on target scope
#
# @return [Enumerable<Symbol>]
#
# @api private
def candidate_names
(
candidate_scope.public_instance_methods(false) +
candidate_scope.private_instance_methods(false) +
candidate_scope.protected_instance_methods(false)
).sort
end
# Candidate scope
#
# @return [Class, Module]
#
# @api private
abstract_method :candidate_scope
# Matcher for singleton methods
class Singleton < self
MATCHER = Matcher::Method::Singleton
private
# Method object on scope
#
# @param [Symbol] method_name
#
# @return [Method]
#
# @api private
def access(method_name)
scope.method(method_name)
end
# Candidate scope
#
# @return [Class]
#
# @api private
def candidate_scope
scope.singleton_class
end
memoize :candidate_scope, freezer: :noop
end # Singleton
# Matcher for instance methods
class Instance < self
MATCHER = Matcher::Method::Instance
private
# Method object on scope
#
# @param [Symbol] method_name
#
# @return [UnboundMethod]
#
# @api private
def access(method_name)
scope.instance_method(method_name)
end
# Candidate scope
#
# @return [Class, Module]
#
# @api private
def candidate_scope
scope
end
end # Instance
end # Methods
end # Matcher
end # Mutant