class Mutest::Mutator::Node

Abstract base class for node mutators

Constants

TAUTOLOGY

Private Class Methods

define_named_child(name, index) click to toggle source

Helper to define a named child

@param [Parser::AST::Node] node

@param [Integer] index

@return [undefined]

Calls superclass method
# File lib/mutest/mutator/node.rb, line 22
def self.define_named_child(name, index)
  super

  define_method(:"emit_#{name}_mutations") do |&block|
    mutate_child(index, &block)
  end

  define_method(:"emit_#{name}") do |node|
    emit_child_update(index, node)
  end
end

Private Instance Methods

asgn_left?() click to toggle source

Test if the node is the left of an or_asgn or op_asgn

@return [Boolean]

# File lib/mutest/mutator/node.rb, line 147
def asgn_left?
  AST::Types::OP_ASSIGN.include?(parent_type) && parent.node.children.first.equal?(node)
end
children_indices(range) click to toggle source

Children indices

@param [Range] range

@return [Enumerable<Integer>]

# File lib/mutest/mutator/node.rb, line 156
def children_indices(range)
  range.begin.upto(children.length + range.end)
end
delete_child(index) click to toggle source

Emit delete child mutation

@param [Integer] index

@return [undefined]

# File lib/mutest/mutator/node.rb, line 66
def delete_child(index)
  dup_children = children.dup
  dup_children.delete_at(index)
  emit_type(*dup_children)
end
emit_child_update(index, node) click to toggle source

Emit updated child

@param [Integer] index @param [Parser::AST::Node] node

@return [undefined]

# File lib/mutest/mutator/node.rb, line 78
def emit_child_update(index, node)
  new_children = children.dup
  new_children[index] = node
  emit_type(*new_children)
end
emit_nil() click to toggle source

Emit a literal nil

@return [undefined]

# File lib/mutest/mutator/node.rb, line 118
def emit_nil
  emit(N_NIL) unless asgn_left?
end
emit_propagation(node) click to toggle source

Emit propagation if node can stand alone

@return [undefined]

# File lib/mutest/mutator/node.rb, line 96
def emit_propagation(node)
  emit(node) unless AST::Types::NOT_STANDALONE.include?(node.type)
end
emit_self() click to toggle source

Emit a literal self

@return [undefined]

# File lib/mutest/mutator/node.rb, line 111
def emit_self
  emit(N_SELF)
end
emit_singletons() click to toggle source

Emit singleton literals

@return [undefined]

# File lib/mutest/mutator/node.rb, line 103
def emit_singletons
  emit_nil
  emit_self
end
emit_type(*children) click to toggle source

Emit a new AST node with same class as wrapped node

@param [Array<Parser::AST::Node>] children

@return [undefined]

# File lib/mutest/mutator/node.rb, line 89
def emit_type(*children)
  emit(::Parser::AST::Node.new(node.type, children))
end
mutate_child(index, &block) click to toggle source

Dispatch on child index

@param [Integer] index

@return [undefined]

# File lib/mutest/mutator/node.rb, line 52
def mutate_child(index, &block)
  block ||= TAUTOLOGY
  mutate(children.fetch(index), self).each do |mutation|
    next unless block.call(mutation)

    emit_child_update(index, mutation)
  end
end
mutate_single_child() { |child, index| ... } click to toggle source

Emit single child mutation

@return [undefined]

# File lib/mutest/mutator/node.rb, line 163
def mutate_single_child
  children.each_with_index do |child, index|
    mutate_child(index)
    yield child, index unless children.one?
  end
end
parent_node() click to toggle source

Parent node

@return [Parser::AST::Node] node

if parent with node is present

@return [nil]

otherwise
# File lib/mutest/mutator/node.rb, line 129
def parent_node
  parent&.node
end
parent_type() click to toggle source

Parent type

@return [Symbol] type

if parent with type is present

@return [nil]

otherwise
# File lib/mutest/mutator/node.rb, line 140
def parent_type
  parent_node&.type
end