free_mutant/lib/mutant/mutation.rb

202 lines
3.9 KiB
Ruby
Raw Normal View History

2012-08-15 22:09:14 -04:00
module Mutant
# Represent a mutated node with its subject
class Mutation
2013-07-28 13:04:42 -04:00
include AbstractType, Adamantium::Flat
include Concord::Public.new(:subject, :node)
CODE_DELIMITER = "\0".freeze
CODE_RANGE = (0..4).freeze
2014-10-23 07:37:53 -04:00
# Kill mutation via isolation
#
# @param [Isolation] isolation
#
# @return [Result::Mutation]
#
# @api private
#
def kill(isolation)
result = Result::Mutation.new(
index: nil,
mutation: self,
test_results: []
)
subject.tests.reduce(result) do |current, test|
return current unless current.continue?
test_result = test.kill(isolation, self)
current.update(
test_results: current.test_results.dup << test_result
)
end
end
2012-08-15 22:09:14 -04:00
# Insert mutated node
#
# FIXME: Cache subject visibility in a better way! Ideally dont mutate it
# implicitly. Also subject.public? should NOT be a public interface it
# is a detail of method mutations.
2013-06-23 16:20:48 -04:00
#
2012-08-15 22:09:14 -04:00
# @return [self]
#
# @api private
#
def insert
2013-06-23 16:20:48 -04:00
subject.public?
2014-11-17 15:31:12 -05:00
subject.prepare
2014-04-22 15:02:33 -04:00
Loader::Eval.call(root, subject)
2012-08-15 22:09:14 -04:00
self
end
# Return identification
#
# @return [String]
#
# @api private
#
def identification
"#{self.class::SYMBOL}:#{subject.identification}:#{code}"
end
memoize :identification
# Return mutation code
#
# @return [String]
#
# @api private
#
def code
sha1[CODE_RANGE]
2012-08-15 22:09:14 -04:00
end
memoize :code
2012-08-15 22:09:14 -04:00
# Return source
#
# @return [String]
#
# @api private
#
def source
Unparser.unparse(node)
2012-08-15 22:09:14 -04:00
end
memoize :source
# Return original source
#
# @return [String]
#
# @api private
#
def original_source
subject.source
2012-08-15 22:09:14 -04:00
end
2014-10-23 07:37:53 -04:00
# Test if mutation is killed by test reports
#
2014-10-23 07:37:53 -04:00
# @param [Array<Report::Test>] test_reports
#
# @return [Boolean]
#
# @api private
#
2014-10-23 07:37:53 -04:00
abstract_singleton_method :success?
# Test if execution can be continued
#
# @return [Boolean]
#
# @api private
#
abstract_singleton_method :continue?
private
# Return sha1 sum of source and subject identification
#
# @return [String]
#
# @api private
#
def sha1
Digest::SHA1.hexdigest(subject.identification + CODE_DELIMITER + source)
end
memoize :sha1
2014-11-17 15:31:12 -05:00
# Return mutated root node
#
# @return [Parser::AST::Node]
#
# @api private
#
def root
2014-11-17 16:40:39 -05:00
subject.context.root(node)
2014-11-17 15:31:12 -05:00
end
# Evil mutation that should case mutations to fail tests
class Evil < self
2014-10-23 07:37:53 -04:00
SYMBOL = 'evil'.freeze
# Test if mutation is killed by test reports
#
# @param [Array<Report::Test>] test_reports
#
# @return [Boolean]
#
# @api private
#
def self.success?(test_results)
!test_results.all?(&:passed)
end
# Test if mutation execution can be continued
#
# @return [Boolean]
#
# @api private
#
def self.continue?(test_results)
!success?(test_results)
end
end # Evil
# Neutral mutation that should not cause mutations to fail tests
class Neutral < self
2014-10-23 07:37:53 -04:00
SYMBOL = 'neutral'.freeze
# Test if mutation is killed by test reports
#
# @param [Array<Report::Test>] test_reports
#
# @return [Boolean]
#
# @api private
#
def self.success?(test_results)
test_results.any? && test_results.all?(&:passed)
end
# Test if mutation execution can be continued
#
# @return [Boolean] _test_results
#
# @api private
#
def self.continue?(_test_results)
true
end
end # Neutral
# Noop mutation, special case of neutral
2014-10-23 07:37:53 -04:00
class Noop < Neutral
2014-10-23 07:37:53 -04:00
SYMBOL = 'noop'.freeze
end # Noop
end # Mutation
end # Mutant