free_mutant/lib/mutant/mutation.rb

109 lines
2.1 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
# Identification string
2012-08-15 22:09:14 -04:00
#
# @return [String]
def identification
"#{self.class::SYMBOL}:#{subject.identification}:#{code}"
end
memoize :identification
# Mutation code
#
# @return [String]
def code
sha1[CODE_RANGE]
2012-08-15 22:09:14 -04:00
end
memoize :code
2012-08-15 22:09:14 -04:00
# Normalized mutation source
2012-08-15 22:09:14 -04:00
#
# @return [String]
def source
Unparser.unparse(node)
2012-08-15 22:09:14 -04:00
end
memoize :source
# Normalized original source
2012-08-15 22:09:14 -04:00
#
# @return [String]
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
#
# @param [Result::Test] test_result
#
# @return [Boolean]
def self.success?(test_result)
self::TEST_PASS_SUCCESS.equal?(test_result.passed)
end
# Insert mutated node
2014-10-23 07:37:53 -04:00
#
2015-11-21 17:02:51 -05:00
# @param kernel [Kernel]
#
# @return [self]
2015-11-21 17:02:51 -05:00
def insert(kernel)
subject.prepare
2015-11-21 17:02:51 -05:00
Loader.call(
binding: TOPLEVEL_BINDING,
kernel: kernel,
node: root,
subject: subject
)
self
end
private
# SHA1 sum of source and subject identification
#
# @return [String]
def sha1
Digest::SHA1.hexdigest(subject.identification + CODE_DELIMITER + source)
end
memoize :sha1
# Mutated root node
2014-11-17 15:31:12 -05:00
#
# @return [Parser::AST::Node]
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
SYMBOL = 'evil'.freeze
TEST_PASS_SUCCESS = false
end # Evil
# Neutral mutation that should not cause mutations to fail tests
class Neutral < self
SYMBOL = 'neutral'.freeze
TEST_PASS_SUCCESS = true
end # Neutral
# Noop mutation, special case of neutral
2014-10-23 07:37:53 -04:00
class Noop < Neutral
SYMBOL = 'noop'.freeze
TEST_PASS_SUCCESS = true
end # Noop
end # Mutation
end # Mutant