a2de0442f6
* No easy way to insert mutants on these via current monkey patching loader * The blocks are typically part of a method building library when those libraries are mutated the def/defs nodes will be part of an mutated block that is included in a subject. [fix #254]
143 lines
3 KiB
Ruby
143 lines
3 KiB
Ruby
module Mutant
|
|
class Matcher
|
|
# Matcher for subjects that are a specific method
|
|
class Method < self
|
|
include Adamantium::Flat, Concord::Public.new(:env, :scope, :target_method)
|
|
include AST::NodePredicates, Equalizer.new(:identification)
|
|
|
|
# Methods within rbx kernel directory are precompiled and their source
|
|
# cannot be accessed via reading source location. Same for methods created by eval.
|
|
BLACKLIST = %r{\Akernel/|(eval)}.freeze
|
|
|
|
# Enumerate matches
|
|
#
|
|
# @return [Enumerable<Subject>]
|
|
# if no block given
|
|
#
|
|
# @return [self]
|
|
# otherwise
|
|
#
|
|
# @api private
|
|
#
|
|
def each
|
|
return to_enum unless block_given?
|
|
|
|
if !skip? && subject
|
|
yield subject
|
|
end
|
|
|
|
self
|
|
end
|
|
|
|
private
|
|
|
|
# Test if method should be skipped
|
|
#
|
|
# @return [Boolean]
|
|
#
|
|
# @api private
|
|
#
|
|
def skip?
|
|
location = source_location
|
|
if location.nil? || BLACKLIST.match(location.first)
|
|
env.warn(format('%s does not have valid source location unable to emit subject', target_method.inspect))
|
|
true
|
|
elsif matched_node_path.any?(&method(:n_block?))
|
|
env.warn(format('%s is defined from a 3rd party lib unable to emit subject', target_method.inspect))
|
|
true
|
|
else
|
|
false
|
|
end
|
|
end
|
|
|
|
# Return method name
|
|
#
|
|
# @return [String]
|
|
#
|
|
# @api private
|
|
#
|
|
def method_name
|
|
target_method.name
|
|
end
|
|
|
|
# Return context
|
|
#
|
|
# @return [Context::Scope]
|
|
#
|
|
# @api private
|
|
#
|
|
def context
|
|
Context::Scope.new(scope, source_path)
|
|
end
|
|
|
|
# Return full ast
|
|
#
|
|
# @return [Parser::AST::Node]
|
|
#
|
|
# @api private
|
|
#
|
|
def ast
|
|
env.cache.parse(source_path)
|
|
end
|
|
|
|
# Return path to source
|
|
#
|
|
# @return [String]
|
|
#
|
|
# @api private
|
|
#
|
|
def source_path
|
|
source_location.first
|
|
end
|
|
|
|
# Return source file line
|
|
#
|
|
# @return [Integer]
|
|
#
|
|
# @api private
|
|
#
|
|
def source_line
|
|
source_location.last
|
|
end
|
|
|
|
# Return source location
|
|
#
|
|
# @return [Array]
|
|
#
|
|
# @api private
|
|
#
|
|
def source_location
|
|
target_method.source_location
|
|
end
|
|
|
|
# Return subject
|
|
#
|
|
# @return [Subject]
|
|
# if there is a matched node
|
|
#
|
|
# @return [nil]
|
|
# otherwise
|
|
#
|
|
# @api private
|
|
#
|
|
def subject
|
|
node = matched_node_path.last
|
|
return unless node
|
|
self.class::SUBJECT_CLASS.new(env.config, context, node)
|
|
end
|
|
memoize :subject
|
|
|
|
# Return matched node path
|
|
#
|
|
# @return [Array<Parser::AST::Node>]
|
|
#
|
|
# @api private
|
|
#
|
|
def matched_node_path
|
|
AST.find_last_path(ast, &method(:match?))
|
|
end
|
|
memoize :matched_node_path
|
|
|
|
end # Method
|
|
end # Matcher
|
|
end # Mutant
|