free_mutant/lib/mutant/subject.rb

145 lines
2.4 KiB
Ruby
Raw Normal View History

module Mutant
# Subject of a mutation
2012-08-01 18:34:03 +02:00
class Subject
2013-01-13 22:25:49 +01:00
include AbstractType, Adamantium::Flat, Enumerable, Equalizer.new(:context, :node)
# Return context
#
# @return [Context]
#
# @api private
#
attr_reader :context
# Return AST node
#
# @return [Parser::AST::Node]
#
# @api private
#
attr_reader :node
# Enumerate possible mutations
#
# @return [self]
# returns self if block given
#
# @return [Enumerator<Mutation>]
# returns eumerator if no block given
#
# @api private
#
2012-08-01 18:19:12 +02:00
def each
return to_enum unless block_given?
2012-08-01 18:19:12 +02:00
Mutator.each(node) do |mutant|
2013-01-15 23:46:05 +01:00
yield Mutation::Evil.new(self, mutant)
2012-08-01 18:19:12 +02:00
end
self
end
# Return noop mutation
#
# @return [Mutation::Noop]
#
# @api private
#
def noop
2013-01-15 23:46:05 +01:00
Mutation::Neutral.new(self, node)
end
memoize :noop
# Return source path
#
# @return [String]
#
# @api private
#
def source_path
context.source_path
end
# Return source line
#
# @return [Fixnum]
#
# @api private
#
def source_line
2013-06-14 20:23:46 +02:00
node.location.expression.line
end
# Return subject identicication
#
# @return [String]
#
# @api private
#
def identification
"#{subtype}:#{source_path}:#{source_line}"
end
memoize :identification
# Return source representation of ast
#
# @return [Source]
#
# @api private
2013-04-17 20:31:21 -07:00
#
def source
ToSource.to_source(node)
end
memoize :source
2012-08-14 22:45:34 +02:00
# Return root AST for node
#
# @param [Parser::AST::Node] node
2012-08-14 22:45:34 +02:00
#
# @return [Parser::AST::Node]
2012-08-14 22:45:34 +02:00
#
# @api private
#
def root(node)
context.root(node)
end
# Return root AST node for original AST ndoe
#
# @return [Parser::AST::Node]
#
# @api private
#
def original_root
root(node)
end
memoize :original_root
private
2012-08-01 18:34:03 +02:00
# Initialize subject
#
2013-01-13 22:25:49 +01:00
# @param [Mutant::Context] context
#
# @param [Parser::AST::Node] node
2013-01-13 22:25:49 +01:00
# the original node to be mutated
#
# @return [unkown]
#
# @api private
#
2013-01-13 22:25:49 +01:00
def initialize(context, node)
@context, @node = context, node
end
2012-08-29 13:38:14 +02:00
2013-01-13 22:25:49 +01:00
# Return subtype identifier
#
# @return [String]
#
# @api private
#
abstract_method :subtype
private :subtype
end # Subject
end # Mutant