Sha256: a36f3ccb103d8c48fc91e1b02f9b2da9cec898f2e4c76129cf39d4e0ce7f7cb8

Contents?: true

Size: 1.83 KB

Versions: 17

Compression:

Stored size: 1.83 KB

Contents

# frozen_string_literal: true

module Mutant
  # Generator for mutations
  class Mutator

    REGISTRY = Registry.new

    include Adamantium::Flat, Concord.new(:input, :parent), AbstractType, Procto.call(:output)

    # Lookup and invoke dedicated AST mutator
    #
    # @param node [Parser::AST::Node]
    # @param parent [nil,Mutant::Mutator::Node]
    #
    # @return [Set<Parser::AST::Node>]
    def self.mutate(node, parent = nil)
      self::REGISTRY.lookup(node.type).call(node, parent)
    end

    # Register node class handler
    #
    # @return [undefined]
    def self.handle(*types)
      types.each do |type|
        self::REGISTRY.register(type, self)
      end
    end
    private_class_method :handle

    # Return output
    #
    # @return [Set<Parser::AST::Node>]
    attr_reader :output

  private

    # Initialize object
    #
    # @param [Object] input
    # @param [Object] parent
    # @param [#call(node)] block
    #
    # @return [undefined]
    def initialize(_input, _parent = nil)
      super

      @output = Set.new

      dispatch
    end

    # Test if generated object is not guarded from emitting
    #
    # @param [Object] object
    #
    # @return [Boolean]
    def new?(object)
      !object.eql?(input)
    end

    # Dispatch node generations
    #
    # @return [undefined]
    abstract_method :dispatch

    # Emit generated mutation if object is not equivalent to input
    #
    # @param [Object] object
    #
    # @return [undefined]
    def emit(object)
      return unless new?(object)

      output << object
    end

    # Run input with mutator
    #
    # @return [undefined]
    def run(mutator)
      mutator.call(input).each(&method(:emit))
    end

    # Shortcut to create a new unfrozen duplicate of input
    #
    # @return [Object]
    def dup_input
      input.dup
    end

  end # Mutator
end # Mutant

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
mutant-0.9.8 lib/mutant/mutator.rb
mutant-0.9.7 lib/mutant/mutator.rb
mutant-0.9.6 lib/mutant/mutator.rb
mutant-0.9.5 lib/mutant/mutator.rb
mutant-0.9.4 lib/mutant/mutator.rb
mutant-0.9.3 lib/mutant/mutator.rb
mutant-0.9.2 lib/mutant/mutator.rb
mutant-0.9.1 lib/mutant/mutator.rb
mutant-0.9.0 lib/mutant/mutator.rb
mutant-0.8.24 lib/mutant/mutator.rb
mutant-0.8.23 lib/mutant/mutator.rb
mutant-0.8.22 lib/mutant/mutator.rb
mutant-0.8.21 lib/mutant/mutator.rb
mutant-0.8.20 lib/mutant/mutator.rb
mutant-0.8.19 lib/mutant/mutator.rb
mutant-0.8.18 lib/mutant/mutator.rb
mutant-0.8.17 lib/mutant/mutator.rb