Class: Mutineer::Mutators::ArrayLiteral

Inherits:
Base
  • Object
show all
Defined in:
lib/mutineer/mutators/array_literal.rb

Overview

Array-literal mutator (Tier-2).

Replaces a non-empty array literal with an empty one, one mutation per literal: [a, b] and %i[a b] become []. The mutant survives when no test checks the contents of the array.

Instance Method Summary collapse

Methods inherited from Base

#heredoc?, #mutations_for

Instance Method Details

#emit(node) ⇒ void (private)

This method returns an undefined value.

Emits a mutation for a non-empty array with brackets.

The operator skips an implicit array (x = 1, 2), because it has no brackets. It also skips an array that holds a heredoc, because the heredoc body stays behind as code.

Parameters:

  • node (Prism::ArrayNode) —

    array node to inspect.



39
40
41
42
43
44
45
46
47
48
# File 'lib/mutineer/mutators/array_literal.rb', line 39

def emit(node)
  return if node.opening_loc.nil? || node.elements.empty? || heredoc?(node)

  @mutations << Mutation.new(
    start_offset: node.location.start_offset,
    end_offset: node.location.end_offset,
    replacement: "[]",
    operator: :array_literal
  )
end

#visit_array_node(node) ⇒ void

This method returns an undefined value.

Visits array nodes and emits array-literal mutations.

Parameters:

  • node (Prism::ArrayNode) —

    array node to inspect.



17
18
19
20
# File 'lib/mutineer/mutators/array_literal.rb', line 17

def visit_array_node(node)
  emit(node)
  super # nested arrays each get their own mutation
end

#visit_def_node(node) ⇒ void

This method returns an undefined value.

Skips a nested method definition. The project finds it as a subject of its own, so a visit here counts its arrays twice.

Parameters:

  • node (Prism::DefNode) —

    nested definition node.



27
# File 'lib/mutineer/mutators/array_literal.rb', line 27

def visit_def_node(node); end