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