2012-07-26 19:25:23 +02:00
|
|
|
module Mutant
|
2012-07-31 04:10:37 +02:00
|
|
|
class Context
|
2012-08-16 04:10:54 +02:00
|
|
|
# Scope context for mutation (Class or Module)
|
|
|
|
class Scope < self
|
2012-12-12 22:11:35 +01:00
|
|
|
include Adamantium::Flat, Equalizer.new(:scope, :source_path)
|
2013-06-04 19:22:33 +02:00
|
|
|
extend NodeHelpers
|
2012-07-26 19:25:23 +02:00
|
|
|
|
2012-12-12 22:11:35 +01:00
|
|
|
# Return AST wrapping mutated node
|
|
|
|
#
|
2013-06-04 10:25:13 +02:00
|
|
|
# @return [Parser::AST::Node]
|
2012-12-12 22:11:35 +01:00
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def root(node)
|
|
|
|
nesting.reverse.inject(node) do |current, scope|
|
|
|
|
self.class.wrap(scope, current)
|
|
|
|
end
|
2012-08-14 12:26:56 +02:00
|
|
|
end
|
2012-07-26 19:25:23 +02:00
|
|
|
|
2013-01-13 22:27:03 +01:00
|
|
|
# Return identification
|
|
|
|
#
|
|
|
|
# @return [String]
|
|
|
|
#
|
|
|
|
# @ai private
|
|
|
|
#
|
|
|
|
def identification
|
|
|
|
scope.name
|
|
|
|
end
|
|
|
|
|
2012-12-12 22:11:35 +01:00
|
|
|
# Wrap node into ast node
|
2012-07-26 19:25:23 +02:00
|
|
|
#
|
2013-04-17 20:31:21 -07:00
|
|
|
# @param [Class, Module] scope
|
2013-06-04 10:25:13 +02:00
|
|
|
# @param [Parser::AST::Node] node
|
2012-07-26 19:25:23 +02:00
|
|
|
#
|
2013-06-04 10:25:13 +02:00
|
|
|
# @return [Parser::AST::Class]
|
2012-12-12 22:11:35 +01:00
|
|
|
# if scope is of kind Class
|
2012-08-14 12:26:56 +02:00
|
|
|
#
|
2013-06-04 10:25:13 +02:00
|
|
|
# @return [Parser::AST::Module]
|
2012-12-12 22:11:35 +01:00
|
|
|
# if scope is of kind module
|
2012-07-26 19:25:23 +02:00
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2012-12-12 22:11:35 +01:00
|
|
|
def self.wrap(scope, node)
|
2013-06-04 19:22:33 +02:00
|
|
|
name = s(:const, nil, scope.name.split('::').last.to_sym)
|
2012-12-12 22:11:35 +01:00
|
|
|
case scope
|
|
|
|
when ::Class
|
2013-06-04 19:22:33 +02:00
|
|
|
s(:class, name, nil, node)
|
2012-12-12 22:11:35 +01:00
|
|
|
when ::Module
|
2013-06-04 19:22:33 +02:00
|
|
|
s(:module, name, node)
|
2012-12-12 22:11:35 +01:00
|
|
|
else
|
|
|
|
raise "Cannot wrap scope: #{scope.inspect}"
|
|
|
|
end
|
2012-07-26 19:25:23 +02:00
|
|
|
end
|
|
|
|
|
2012-12-12 22:11:35 +01:00
|
|
|
# Return nesting
|
2012-07-26 19:25:23 +02:00
|
|
|
#
|
2012-12-12 22:11:35 +01:00
|
|
|
# @return [Enumerable<Class,Module>]
|
2012-07-26 19:25:23 +02:00
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2012-12-12 22:11:35 +01:00
|
|
|
def nesting
|
|
|
|
const = ::Object
|
|
|
|
name_nesting.each_with_object([]) do |name, nesting|
|
|
|
|
const = const.const_get(name)
|
|
|
|
nesting << const
|
|
|
|
end
|
2012-07-26 19:25:23 +02:00
|
|
|
end
|
2012-12-12 22:11:35 +01:00
|
|
|
memoize :nesting
|
2012-07-26 19:25:23 +02:00
|
|
|
|
2012-08-16 04:10:54 +02:00
|
|
|
# Return unqualified name of scope
|
2012-07-26 19:25:23 +02:00
|
|
|
#
|
2012-07-31 04:10:37 +02:00
|
|
|
# @return [String]
|
2012-07-26 19:25:23 +02:00
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def unqualified_name
|
|
|
|
name_nesting.last
|
|
|
|
end
|
|
|
|
|
2013-01-21 23:54:25 +01:00
|
|
|
# Return name
|
|
|
|
#
|
|
|
|
# @return [String]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def name
|
|
|
|
scope.name
|
|
|
|
end
|
|
|
|
|
2012-08-16 04:10:54 +02:00
|
|
|
# Return scope wrapped by context
|
2012-07-26 19:25:23 +02:00
|
|
|
#
|
|
|
|
# @return [::Module|::Class]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2012-10-26 11:24:29 +02:00
|
|
|
attr_reader :scope
|
2012-08-20 17:53:41 +02:00
|
|
|
|
|
|
|
private
|
2012-07-26 19:25:23 +02:00
|
|
|
|
|
|
|
# Initialize object
|
|
|
|
#
|
2012-08-16 04:10:54 +02:00
|
|
|
# @param [Object] scope
|
|
|
|
# @param [String] source_path
|
2012-07-26 19:25:23 +02:00
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2012-08-16 04:10:54 +02:00
|
|
|
def initialize(scope, source_path)
|
|
|
|
super(source_path)
|
|
|
|
@scope = scope
|
2012-08-14 12:26:56 +02:00
|
|
|
end
|
|
|
|
|
2012-08-16 04:10:54 +02:00
|
|
|
# Return nesting of names of scope
|
2012-07-26 19:25:23 +02:00
|
|
|
#
|
|
|
|
# @return [Array<String>]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def name_nesting
|
2012-10-26 11:24:29 +02:00
|
|
|
scope.name.split('::')
|
2012-07-26 19:25:23 +02:00
|
|
|
end
|
2012-10-26 11:24:29 +02:00
|
|
|
memoize :name_nesting
|
2013-06-04 10:25:13 +02:00
|
|
|
|
|
|
|
end # Scope
|
|
|
|
end # Context
|
|
|
|
end # Mutant
|