Class: Mutineer::Mutators::SafeNavigation

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

Overview

Safe-navigation mutator (Tier-2).

Replaces &. with ., one mutation per occurrence. The mutant raises NoMethodError on a nil receiver, so it survives when no test passes nil.

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 when the node's call operator is &..

Parameters:

  • node (Prism::Node) —

    a node with safe_navigation? and call_operator_loc.



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/mutineer/mutators/safe_navigation.rb', line 63

def emit(node)
  return unless node.safe_navigation?

  loc = node.call_operator_loc
  @mutations << Mutation.new(
    start_offset: loc.start_offset,
    end_offset: loc.end_offset,
    replacement: ".",
    operator: :safe_navigation
  )
end

#visit_call_and_write_node(node) ⇒ void

This method returns an undefined value.

Visits a&.b &&= 1.

Parameters:

  • node (Prism::CallAndWriteNode) —

    node to inspect.



43
44
45
46
# File 'lib/mutineer/mutators/safe_navigation.rb', line 43

def visit_call_and_write_node(node)
  emit(node)
  super
end

#visit_call_node(node) ⇒ void

This method returns an undefined value.

Visits call nodes and emits safe-navigation mutations.

Parameters:

  • node (Prism::CallNode) —

    call node to inspect.



16
17
18
19
# File 'lib/mutineer/mutators/safe_navigation.rb', line 16

def visit_call_node(node)
  emit(node)
  super # chained calls (a&.b&.c) each get their own mutation
end

#visit_call_operator_write_node(node) ⇒ void

This method returns an undefined value.

Visits a&.b += 1, which Prism parses as its own node, not a CallNode.

Parameters:

  • node (Prism::CallOperatorWriteNode) —

    node to inspect.



25
26
27
28
# File 'lib/mutineer/mutators/safe_navigation.rb', line 25

def visit_call_operator_write_node(node)
  emit(node)
  super
end

#visit_call_or_write_node(node) ⇒ void

This method returns an undefined value.

Visits a&.b ||= 1.

Parameters:

  • node (Prism::CallOrWriteNode) —

    node to inspect.



34
35
36
37
# File 'lib/mutineer/mutators/safe_navigation.rb', line 34

def visit_call_or_write_node(node)
  emit(node)
  super
end

#visit_call_target_node(node) ⇒ void

This method returns an undefined value.

Visits a call used as an assignment target, as in for a&.b in list.

Parameters:

  • node (Prism::CallTargetNode) —

    node to inspect.



52
53
54
55
# File 'lib/mutineer/mutators/safe_navigation.rb', line 52

def visit_call_target_node(node)
  emit(node)
  super
end