2012-11-21 16:28:08 -05:00
|
|
|
module Mutant
|
|
|
|
class Matcher
|
2013-07-28 13:59:25 -04:00
|
|
|
# Abstract base class for matcher that returns method subjects from scope
|
2013-01-21 14:08:30 -05:00
|
|
|
class Methods < self
|
2014-06-30 09:06:57 -04:00
|
|
|
include AbstractType, Concord::Public.new(:env, :scope)
|
2013-01-21 16:56:52 -05:00
|
|
|
|
2012-11-21 16:28:08 -05:00
|
|
|
# Enumerate subjects
|
|
|
|
#
|
|
|
|
# @return [self]
|
|
|
|
# if block given
|
|
|
|
#
|
2013-04-17 23:31:21 -04:00
|
|
|
# @return [Enumerator<Subject>]
|
2012-11-21 16:28:08 -05:00
|
|
|
# otherwise
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
def each(&block)
|
|
|
|
return to_enum unless block_given?
|
2012-12-07 17:27:21 -05:00
|
|
|
|
2014-06-30 08:53:48 -04:00
|
|
|
subjects.each(&block)
|
2012-11-21 16:28:08 -05:00
|
|
|
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
2015-06-21 10:44:33 -04:00
|
|
|
private
|
|
|
|
|
2015-07-03 11:21:39 -04:00
|
|
|
# method matcher class
|
2013-01-21 16:56:52 -05:00
|
|
|
#
|
|
|
|
# @return [Class:Matcher::Method]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
def matcher
|
|
|
|
self.class::MATCHER
|
|
|
|
end
|
|
|
|
|
2015-07-03 11:21:39 -04:00
|
|
|
# Available methods scope
|
2012-11-21 16:28:08 -05:00
|
|
|
#
|
2013-04-27 12:21:38 -04:00
|
|
|
# @return [Enumerable<Method, UnboundMethod>]
|
2012-11-21 16:28:08 -05:00
|
|
|
#
|
|
|
|
# @api private
|
2013-04-27 12:21:38 -04:00
|
|
|
def methods
|
|
|
|
candidate_names.each_with_object([]) do |name, methods|
|
|
|
|
method = access(name)
|
2014-06-16 06:50:32 -04:00
|
|
|
methods << method if method.owner.equal?(candidate_scope)
|
2012-11-21 16:28:08 -05:00
|
|
|
end
|
|
|
|
end
|
2013-04-27 12:21:38 -04:00
|
|
|
memoize :methods
|
2012-11-21 16:28:08 -05:00
|
|
|
|
2015-07-03 11:21:39 -04:00
|
|
|
# Subjects detected on scope
|
2013-04-27 12:21:38 -04:00
|
|
|
#
|
2014-06-30 08:53:48 -04:00
|
|
|
# @return [Array<Subject>]
|
2013-04-27 09:52:49 -04:00
|
|
|
#
|
|
|
|
# @api private
|
2014-06-30 08:53:48 -04:00
|
|
|
def subjects
|
|
|
|
methods.map do |method|
|
2014-06-30 09:06:57 -04:00
|
|
|
matcher.build(env, scope, method)
|
2014-06-30 08:53:48 -04:00
|
|
|
end.flat_map(&:to_a)
|
2013-04-27 09:52:49 -04:00
|
|
|
end
|
2014-06-30 08:53:48 -04:00
|
|
|
memoize :subjects
|
2013-04-27 09:52:49 -04:00
|
|
|
|
2015-07-03 11:21:39 -04:00
|
|
|
# Candidate method names on target scope
|
2013-04-27 09:52:49 -04:00
|
|
|
#
|
2013-04-17 23:31:21 -04:00
|
|
|
# @return [Enumerable<Symbol>]
|
2012-11-21 16:28:08 -05:00
|
|
|
#
|
|
|
|
# @api private
|
2013-04-27 12:21:38 -04:00
|
|
|
def candidate_names
|
2014-06-30 08:53:48 -04:00
|
|
|
(
|
2013-06-15 11:14:33 -04:00
|
|
|
candidate_scope.public_instance_methods(false) +
|
|
|
|
candidate_scope.private_instance_methods(false) +
|
|
|
|
candidate_scope.protected_instance_methods(false)
|
2014-06-30 08:53:48 -04:00
|
|
|
).sort
|
2013-04-27 09:52:49 -04:00
|
|
|
end
|
2012-11-21 16:28:08 -05:00
|
|
|
|
2015-07-03 11:21:39 -04:00
|
|
|
# Candidate scope
|
2013-04-27 12:21:38 -04:00
|
|
|
#
|
|
|
|
# @return [Class, Module]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
abstract_method :candidate_scope
|
|
|
|
|
2013-06-15 11:13:01 -04:00
|
|
|
# Matcher for singleton methods
|
2012-11-21 16:28:08 -05:00
|
|
|
class Singleton < self
|
2013-01-03 17:55:30 -05:00
|
|
|
MATCHER = Matcher::Method::Singleton
|
2012-11-21 16:28:08 -05:00
|
|
|
|
2013-04-27 12:21:38 -04:00
|
|
|
private
|
|
|
|
|
2015-07-03 11:21:39 -04:00
|
|
|
# Method object on scope
|
2012-12-07 17:27:21 -05:00
|
|
|
#
|
|
|
|
# @param [Symbol] method_name
|
|
|
|
#
|
|
|
|
# @return [Method]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
def access(method_name)
|
|
|
|
scope.method(method_name)
|
|
|
|
end
|
|
|
|
|
2015-07-03 11:21:39 -04:00
|
|
|
# Candidate scope
|
2012-11-21 16:28:08 -05:00
|
|
|
#
|
2013-04-27 12:21:38 -04:00
|
|
|
# @return [Class]
|
2012-11-21 16:28:08 -05:00
|
|
|
#
|
|
|
|
# @api private
|
2013-04-27 12:21:38 -04:00
|
|
|
def candidate_scope
|
|
|
|
scope.singleton_class
|
2012-11-21 16:28:08 -05:00
|
|
|
end
|
2013-09-07 14:27:06 -04:00
|
|
|
memoize :candidate_scope, freezer: :noop
|
2013-04-27 12:21:38 -04:00
|
|
|
|
2013-06-14 14:54:02 -04:00
|
|
|
end # Singleton
|
2012-11-21 16:28:08 -05:00
|
|
|
|
2013-06-15 11:13:01 -04:00
|
|
|
# Matcher for instance methods
|
2012-11-21 16:28:08 -05:00
|
|
|
class Instance < self
|
2013-01-03 17:55:30 -05:00
|
|
|
MATCHER = Matcher::Method::Instance
|
2012-11-21 16:28:08 -05:00
|
|
|
|
2013-04-27 12:21:38 -04:00
|
|
|
private
|
|
|
|
|
2015-07-03 11:21:39 -04:00
|
|
|
# Method object on scope
|
2012-12-07 17:27:21 -05:00
|
|
|
#
|
|
|
|
# @param [Symbol] method_name
|
|
|
|
#
|
|
|
|
# @return [UnboundMethod]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
def access(method_name)
|
|
|
|
scope.instance_method(method_name)
|
|
|
|
end
|
|
|
|
|
2015-07-03 11:21:39 -04:00
|
|
|
# Candidate scope
|
2012-11-21 16:28:08 -05:00
|
|
|
#
|
2013-04-27 12:21:38 -04:00
|
|
|
# @return [Class, Module]
|
2013-06-14 14:54:02 -04:00
|
|
|
#
|
2012-12-12 16:31:14 -05:00
|
|
|
# @api private
|
2013-04-27 12:21:38 -04:00
|
|
|
def candidate_scope
|
|
|
|
scope
|
2012-11-21 16:28:08 -05:00
|
|
|
end
|
2013-04-27 12:21:38 -04:00
|
|
|
|
2013-06-14 14:54:02 -04:00
|
|
|
end # Instance
|
|
|
|
|
|
|
|
end # Methods
|
|
|
|
end # Matcher
|
|
|
|
end # Mutant
|