2012-07-23 16:54:35 -04:00
|
|
|
module Mutant
|
|
|
|
class Matcher
|
2012-07-26 13:25:23 -04:00
|
|
|
# Matcher to find AST for method
|
2012-07-23 16:54:35 -04:00
|
|
|
class Method < Matcher
|
2012-07-26 13:25:23 -04:00
|
|
|
include Veritas::Immutable
|
|
|
|
|
2012-07-23 16:54:35 -04:00
|
|
|
# Parse a method string into filter
|
|
|
|
#
|
|
|
|
# @param [String] input
|
|
|
|
#
|
|
|
|
# @return [Matcher::Method]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def self.parse(input)
|
2012-07-23 16:57:29 -04:00
|
|
|
Classifier.run(input)
|
2012-07-23 16:54:35 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Enumerate matches
|
|
|
|
#
|
|
|
|
# @return [Enumerable]
|
|
|
|
# returns enumerable when no block given
|
|
|
|
#
|
|
|
|
# @return [self]
|
|
|
|
# returns self when block given
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2012-07-23 19:41:08 -04:00
|
|
|
def each(&block)
|
2012-07-29 16:44:53 -04:00
|
|
|
return to_enum unless block_given?
|
2012-07-26 13:25:23 -04:00
|
|
|
mutatee.tap do |mutatee|
|
|
|
|
yield mutatee if mutatee
|
|
|
|
end
|
|
|
|
|
2012-07-23 16:54:35 -04:00
|
|
|
self
|
|
|
|
end
|
|
|
|
|
2012-07-26 13:25:23 -04:00
|
|
|
# Return context of matcher
|
2012-07-23 16:54:35 -04:00
|
|
|
#
|
2012-07-26 13:25:23 -04:00
|
|
|
# @return [Context]
|
|
|
|
# returns the context this matcher matches AST nodes
|
2012-07-23 16:54:35 -04:00
|
|
|
#
|
2012-07-23 19:41:08 -04:00
|
|
|
# @api private
|
2012-07-23 16:54:35 -04:00
|
|
|
#
|
2012-07-26 13:25:23 -04:00
|
|
|
def context
|
|
|
|
Context::Constant.build(constant)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
2012-07-23 19:41:08 -04:00
|
|
|
|
|
|
|
# Return method name
|
|
|
|
#
|
|
|
|
# @return [String]
|
2012-07-23 16:54:35 -04:00
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2012-07-23 19:41:08 -04:00
|
|
|
attr_reader :method_name
|
|
|
|
private :method_name
|
2012-07-23 16:54:35 -04:00
|
|
|
|
2012-07-26 13:25:23 -04:00
|
|
|
# Return constant name
|
|
|
|
#
|
|
|
|
# @return [String]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
attr_reader :constant_name
|
|
|
|
private :constant_name
|
|
|
|
|
|
|
|
|
2012-07-23 19:41:08 -04:00
|
|
|
# Initialize method filter
|
|
|
|
#
|
|
|
|
# @param [String] constant_name
|
|
|
|
# @param [Symbol] method_name
|
|
|
|
#
|
|
|
|
# @return [undefined]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def initialize(constant_name, method_name)
|
2012-07-26 13:25:23 -04:00
|
|
|
@constant_name,@method_name = constant_name, method_name
|
2012-07-23 19:41:08 -04:00
|
|
|
end
|
2012-07-23 16:54:35 -04:00
|
|
|
|
|
|
|
# Return method
|
|
|
|
#
|
|
|
|
# @return [UnboundMethod]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2012-07-30 22:00:05 -04:00
|
|
|
abstract :method
|
2012-07-23 16:54:35 -04:00
|
|
|
|
|
|
|
# Return node classes this matcher matches
|
|
|
|
#
|
2012-07-23 19:41:08 -04:00
|
|
|
# @return [Rubinius::AST::Node]
|
2012-07-23 16:54:35 -04:00
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2012-07-30 22:00:05 -04:00
|
|
|
abstract :node_class
|
2012-07-23 16:54:35 -04:00
|
|
|
|
2012-07-23 19:41:08 -04:00
|
|
|
# Check if node is matched
|
|
|
|
#
|
|
|
|
# @param [Rubinius::AST::Node] node
|
|
|
|
#
|
|
|
|
# @return [true]
|
|
|
|
# returns true if node matches method
|
2012-07-23 16:54:35 -04:00
|
|
|
#
|
2012-07-23 19:41:08 -04:00
|
|
|
# @return [false]
|
|
|
|
# returns false if node NOT matches method
|
2012-07-23 16:54:35 -04:00
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2012-07-23 19:41:08 -04:00
|
|
|
def match?(node)
|
|
|
|
node.line == source_file_line &&
|
|
|
|
node.class == node_class &&
|
|
|
|
node.name == method_name
|
2012-07-23 16:54:35 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Return full ast
|
|
|
|
#
|
|
|
|
# @return [Rubinius::AST::Node]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def ast
|
|
|
|
File.read(source_filename).to_ast
|
|
|
|
end
|
|
|
|
|
|
|
|
# Return source filename
|
|
|
|
#
|
|
|
|
# @return [String]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def source_filename
|
|
|
|
source_location.first
|
|
|
|
end
|
|
|
|
|
|
|
|
# Return source file line
|
|
|
|
#
|
|
|
|
# @return [Integer]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def source_file_line
|
|
|
|
source_location.last
|
|
|
|
end
|
|
|
|
|
|
|
|
# Return source location
|
|
|
|
#
|
|
|
|
# @return [Array]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def source_location
|
|
|
|
method.source_location
|
|
|
|
end
|
|
|
|
|
2012-07-26 13:25:23 -04:00
|
|
|
# Return matched node
|
|
|
|
#
|
|
|
|
# @return [Rubinis::AST::Node]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2012-07-30 22:00:05 -04:00
|
|
|
abstract :matched_node
|
2012-07-26 13:25:23 -04:00
|
|
|
|
|
|
|
# Return mutatee
|
2012-07-23 19:41:08 -04:00
|
|
|
#
|
2012-07-26 13:25:23 -04:00
|
|
|
# @return [Mutatee]
|
2012-07-23 19:41:08 -04:00
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2012-07-26 13:25:23 -04:00
|
|
|
def mutatee
|
|
|
|
node = matched_node
|
|
|
|
if node
|
|
|
|
Mutatee.new(context,node)
|
2012-07-23 19:41:08 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-07-23 16:54:35 -04:00
|
|
|
# Return constant
|
|
|
|
#
|
|
|
|
# @return [Class|Module]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def constant
|
2012-07-26 13:25:23 -04:00
|
|
|
constant_name.split('::').inject(::Object) do |parent,name|
|
|
|
|
parent.const_get(name)
|
2012-07-23 16:54:35 -04:00
|
|
|
end
|
|
|
|
end
|
2012-07-26 13:25:23 -04:00
|
|
|
|
|
|
|
memoize :mutatee
|
2012-07-23 16:54:35 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|