Sha256: 9ecabe41ab87da992a6498d9391825c11bf55cbd3c872e4d4bd5a4c1e54d6cf3

Contents?: true

Size: 1.62 KB

Versions: 5

Compression:

Stored size: 1.62 KB

Contents

module Mutant
  class Mutator
    # Namespace for utility mutators
    class Util < self

      # Run ulitity mutator
      #
      # @param [Object]
      #
      # @api private
      #
      def self.each(object, &block)
        return to_enum(__method__, object) unless block_given?

        new(object, block)

        self
      end

      # Test if mutation is new
      #
      # @param [Object] generated
      #
      # @return [true]
      #   if object is new
      #
      # @return [false]
      #   otherwise
      #
      def new?(generated)
        input != generated
      end

      # Mutators that mutates an array of inputs
      class Array < self

        handle(::Array)

      private

        # Emit mutations
        # 
        # @return [undefined]
        #
        # @api private
        #
        def dispatch
          emit_element_presence
          emit_element_mutations
          emit([])
        end

        # Emit element mutations
        #
        # @return [undefined]
        #
        # @api private
        #
        def emit_element_mutations
          input.each_with_index do |element, index|
            dup = dup_input

            Mutator.each(element).each do |mutation|
              dup[index]=mutation
              emit(dup)
            end
          end
        end

        # Emit element presence mutations
        #
        # @return [undefined]
        #
        # @api private
        #
        def emit_element_presence
          input.each_index do |index|
            dup = dup_input
            dup.delete_at(index)
            emit(dup)
          end
        end

      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
mutant-0.2.4 lib/mutant/mutator/util.rb
mutant-0.2.3 lib/mutant/mutator/util.rb
mutant-0.2.2 lib/mutant/mutator/util.rb
mutant-0.2.1 lib/mutant/mutator/util.rb
mutant-0.2.0 lib/mutant/mutator/util.rb