free_mutant/lib/mutant/mutation.rb
Dan Kubb 4856ed5251 Change sha1 mutation hash to use a separator between strings
* When concatenating strings together to hash you need to use a
  separator that does not exist in the strings, otherwise you can
  easily get duplicate hashes, eg:

    Digest::SHA1.hexdigest('ab' + 'c')  # => "a9993e364706816aba3e25717850c26c9cd0d89d"
    Digest::SHA1.hexdigest('a' + 'bc')  # => "a9993e364706816aba3e25717850c26c9cd0d89d"

  Using a null character as a separator works around this problem:

    Digest::SHA1.hexdigest('a' + 0.chr + 'bc')  # => "0b2749668f0ea8df8a630da13f0d218709efd5ca"
    Digest::SHA1.hexdigest('ab' + 0.chr + 'c')  # => "dbdd4f85d8a56500aa5c9c8a0d456f96280c92e5"
2013-04-17 20:34:24 -07:00

126 lines
2 KiB
Ruby

module Mutant
# Represent a mutated node with its subject
class Mutation
include AbstractType, Adamantium::Flat, Equalizer.new(:sha1)
# Initialize mutation object
#
# @param [Subject] subject
# @param [Rubinius::Node::AST] node
#
# @return [undefined]
#
# @api private
#
def initialize(subject, node)
@subject, @node = subject, node
end
# Return mutation subject
#
# @return [Subject]
#
# @api private
#
attr_reader :subject
# Return mutated node
#
# @return [Rubinius::AST::Node]
#
# @api private
#
attr_reader :node
# Return mutated root node
#
# @return [Rubinius::AST::Node]
#
# @api private
#
def root
subject.root(node)
end
memoize :root
# Test if killer is successful
#
# @param [Killer] killer
#
# @return [true]
# if killer is successful
#
# @return [false]
# otherwise
#
# @api private
#
abstract_method :success?
# Insert mutated node
#
# @return [self]
#
# @api private
#
def insert
Loader::Eval.run(root, subject)
self
end
# Return identification
#
# @return [String]
#
# @api private
#
def identification
"#{subject.identification}:#{code}"
end
memoize :identification
# Return mutation code
#
# @return [String]
#
# @api private
#
def code
sha1[0..4]
end
memoize :code
# Return sha1 sum of source and subject identification
#
# @return [String]
#
# @api private
#
def sha1
Digest::SHA1.hexdigest(subject.identification + 0.chr + source)
end
memoize :sha1
# Return source
#
# @return [String]
#
# @api private
#
def source
ToSource.to_source(node)
end
memoize :source
# Return original source
#
# @return [String]
#
# @api private
#
def original_source
subject.source
end
end
end