2012-07-23 16:54:35 -04:00
|
|
|
module Mutant
|
|
|
|
class Matcher
|
|
|
|
class Method
|
2012-07-26 13:25:23 -04:00
|
|
|
# Matcher for singleton methods
|
2012-08-09 17:07:22 -04:00
|
|
|
class Singleton < self
|
2013-01-13 16:30:06 -05:00
|
|
|
SUBJECT_CLASS = Subject::Method::Singleton
|
2012-07-26 13:25:23 -04:00
|
|
|
|
2012-08-15 22:10:54 -04:00
|
|
|
# Return identification
|
|
|
|
#
|
|
|
|
# @return [String]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def identification
|
|
|
|
"#{scope.name}.#{method_name}"
|
|
|
|
end
|
2012-12-07 10:55:53 -05:00
|
|
|
memoize :identification
|
|
|
|
|
2013-06-27 16:18:07 -04:00
|
|
|
RECEIVER_INDEX = 0
|
|
|
|
NAME_INDEX = 1
|
2013-06-04 13:22:33 -04:00
|
|
|
CONST_NAME_INDEX = 1
|
|
|
|
|
2012-07-23 16:54:35 -04:00
|
|
|
private
|
2012-07-26 13:25:23 -04:00
|
|
|
|
2012-08-16 13:42:32 -04:00
|
|
|
# Test for node match
|
2012-07-26 13:25:23 -04:00
|
|
|
#
|
2013-06-04 04:25:13 -04:00
|
|
|
# @param [Parser::AST::Node] node
|
2012-07-26 13:25:23 -04:00
|
|
|
#
|
|
|
|
# @return [true]
|
2013-06-15 11:14:09 -04:00
|
|
|
# if node matches method
|
2012-07-26 13:25:23 -04:00
|
|
|
#
|
|
|
|
# @return [false]
|
2013-06-15 11:14:09 -04:00
|
|
|
# otherwise
|
2012-07-26 13:25:23 -04:00
|
|
|
#
|
|
|
|
# @api private
|
2012-08-16 13:10:24 -04:00
|
|
|
#
|
|
|
|
def match?(node)
|
2013-06-15 11:14:09 -04:00
|
|
|
line?(node) && name?(node) && receiver?(node)
|
2012-08-16 13:42:32 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Test for line match
|
|
|
|
#
|
2013-06-04 04:25:13 -04:00
|
|
|
# @param [Parser::AST::Node] node
|
2012-08-16 13:42:32 -04:00
|
|
|
#
|
|
|
|
# @return [true]
|
2013-06-15 11:14:09 -04:00
|
|
|
# if node matches source line
|
2012-08-16 13:42:32 -04:00
|
|
|
#
|
|
|
|
# @return [false]
|
2013-06-15 11:14:09 -04:00
|
|
|
# otherwise
|
2012-08-16 13:42:32 -04:00
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def line?(node)
|
2013-06-15 11:14:09 -04:00
|
|
|
expression = node.location.expression
|
|
|
|
return false unless expression
|
2013-06-04 13:22:33 -04:00
|
|
|
expression.line == source_line
|
2012-08-16 13:42:32 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Test for name match
|
|
|
|
#
|
2013-06-04 04:25:13 -04:00
|
|
|
# @param [Parser::AST::Node] node
|
2012-08-16 13:42:32 -04:00
|
|
|
#
|
|
|
|
# @return [true]
|
2013-06-15 11:14:09 -04:00
|
|
|
# if node name matches
|
2012-08-16 13:42:32 -04:00
|
|
|
#
|
|
|
|
# @return [false]
|
2013-06-15 11:14:09 -04:00
|
|
|
# otherwise
|
2012-08-16 13:42:32 -04:00
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def name?(node)
|
2013-06-04 13:22:33 -04:00
|
|
|
node.children[NAME_INDEX] == method_name
|
2012-07-26 13:25:23 -04:00
|
|
|
end
|
|
|
|
|
2012-08-16 13:42:32 -04:00
|
|
|
# Test for receiver match
|
2012-07-30 22:10:37 -04:00
|
|
|
#
|
2013-06-04 04:25:13 -04:00
|
|
|
# @param [Parser::AST::Node] node
|
2012-07-26 13:25:23 -04:00
|
|
|
#
|
|
|
|
# @return [true]
|
2013-06-15 11:14:09 -04:00
|
|
|
# when receiver matches
|
2012-07-26 13:25:23 -04:00
|
|
|
#
|
|
|
|
# @return [false]
|
2013-06-15 11:14:09 -04:00
|
|
|
# otherwise
|
2012-07-26 13:25:23 -04:00
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2012-08-16 13:42:32 -04:00
|
|
|
def receiver?(node)
|
2013-06-04 13:22:33 -04:00
|
|
|
receiver = node.children[RECEIVER_INDEX]
|
|
|
|
case receiver.type
|
|
|
|
when :self
|
2012-07-26 13:25:23 -04:00
|
|
|
true
|
2013-06-04 13:22:33 -04:00
|
|
|
when :const
|
2012-08-16 13:42:32 -04:00
|
|
|
receiver_name?(receiver)
|
2012-07-26 13:25:23 -04:00
|
|
|
else
|
2013-06-15 11:14:09 -04:00
|
|
|
$stderr.puts "Can only match self or const, got #{receiver.type}, unable to match receiver of defs node"
|
2012-12-07 21:11:26 -05:00
|
|
|
false
|
2012-07-26 13:25:23 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-08-16 13:42:32 -04:00
|
|
|
# Test if reciver name matches context
|
2012-07-26 13:25:23 -04:00
|
|
|
#
|
2013-06-04 04:25:13 -04:00
|
|
|
# @param [Parser::AST::Node] node
|
2012-07-26 13:25:23 -04:00
|
|
|
#
|
|
|
|
# @return [true]
|
2013-06-15 11:14:09 -04:00
|
|
|
# if node name matches unqualified scope name
|
2012-07-26 13:25:23 -04:00
|
|
|
#
|
|
|
|
# @return [false]
|
2013-06-15 11:14:09 -04:00
|
|
|
# otherwise
|
2012-07-26 13:25:23 -04:00
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2012-08-16 13:42:32 -04:00
|
|
|
def receiver_name?(node)
|
2013-06-04 13:22:33 -04:00
|
|
|
name = node.children[CONST_NAME_INDEX]
|
|
|
|
name.to_s == context.unqualified_name
|
2012-07-26 13:25:23 -04:00
|
|
|
end
|
|
|
|
|
2013-06-04 04:25:13 -04:00
|
|
|
end # Singleton
|
|
|
|
end # Method
|
|
|
|
end # Matcher
|
|
|
|
end # Mutant
|