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
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
#
2012-08-16 19:10:24 +02:00
# @param [Rubinius::AST::Node] node
2012-07-26 19:25:23 +02:00
#
# @return [true]
2012-08-16 19:10:24 +02:00
# returns true if node matches method
2012-07-26 19:25:23 +02:00
#
# @return [false]
2012-08-16 19:10:24 +02:00
# returns false if node NOT matches method
2012-07-26 19:25:23 +02:00
#
# @api private
2012-08-16 19:10:24 +02:00
#
def match? ( node )
node . class == Rubinius :: AST :: DefineSingleton &&
2013-04-17 20:31:21 -07:00
line? ( node ) &&
name ? ( node ) &&
2012-08-16 19:42:32 +02:00
receiver? ( node )
end
# Test for line match
#
# @param [Rubinius::AST::Node] node
#
# @return [true]
# returns true if node matches source line
#
# @return [false]
# returns false otherwise
#
# @api private
#
def line? ( node )
node . line == source_line
end
# Test for name match
#
# @param [Rubinius::AST::DefineSingleton] node
#
# @return [true]
# returns true if node name matches
#
# @return [false]
# returns false otherwise
#
# @api private
#
def name? ( node )
2013-04-17 20:31:21 -07:00
node . body . name == 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
#
2012-07-26 19:25:23 +02:00
# @param [Rubinius::AST::DefineSingleton] node
#
# @return [true]
2012-08-16 04:10:54 +02:00
# returns true when receiver is self or scope from pattern
2012-07-26 19:25:23 +02:00
#
# @return [false]
# returns false otherwise
#
# @api private
#
2012-08-16 19:42:32 +02:00
def receiver? ( node )
2012-07-26 19:25:23 +02:00
receiver = node . receiver
case receiver
when Rubinius :: AST :: Self
true
when Rubinius :: AST :: ConstantAccess
2012-08-16 19:42:32 +02:00
receiver_name? ( receiver )
2012-07-26 19:25:23 +02:00
else
2013-01-13 22:30:06 +01:00
$stderr . puts " Unable to find singleton method definition can only match receiver on Rubinius::AST::Self or Rubinius::AST::ConstantAccess, got #{ receiver . class } "
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
#
# @param [Rubinius::AST::Node] node
#
# @return [true]
2012-08-16 04:10:54 +02:00
# returns true when node name matches unqualified scope name
2012-07-26 19:25:23 +02:00
#
# @return [false]
# returns false otherwise
#
# @api private
#
2012-08-16 19:42:32 +02:00
def receiver_name? ( node )
2012-07-26 19:25:23 +02:00
node . name . to_s == context . unqualified_name
end
2012-07-23 22:54:35 +02:00
end
end
end
end