free_mutant/lib/mutant/subject.rb

112 lines
1.9 KiB
Ruby
Raw Normal View History

module Mutant
# Subject of mutation
2012-08-01 12:34:03 -04:00
class Subject
2012-08-02 18:19:01 -04:00
include Immutable, Enumerable
# Return context
#
# @return [Context]
#
# @api private
#
attr_reader :context
# Return AST node
#
# @return [Rubinius::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 12:19:12 -04:00
def each
return to_enum unless block_given?
2012-08-01 12:19:12 -04:00
Mutator.each(node) do |mutant|
yield Mutation.new(self, mutant)
2012-08-01 12:19:12 -04:00
end
self
end
# Return subject identicication
#
# @return [String]
#
# @api private
#
def identification
source_path = context.source_path
source_line = node.line
"#{source_path}:#{source_line}"
end
memoize :identification
# Return source representation of ast
#
# @return [Source]
#
# @api private
#
def source
@node.to_source
end
memoize :source
2012-08-14 16:45:34 -04:00
# Return root AST for node
#
# @param [Rubinius::AST::Node] node
#
# @return [Rubinius::AST::Node]
#
# @api private
#
def root(node)
context.root(node)
end
# Return root AST node for original AST ndoe
#
# @return [Rubinius::AST::Node]
#
# @api private
#
def original_root
root(@node)
end
memoize :original_root
# Reset subject into original state
def reset
Loader.run(original_root)
end
private
2012-08-01 12:34:03 -04:00
# Initialize subject
#
# @param [Context] context
# the context of mutations
#
# @param [Rubinius::AST::Node] node
# the node to be mutated
#
# @return [unkown]
#
# @api private
#
def initialize(context, node)
@context, @node = context, node
end
end
end