Class: Mutineer::Mutators::RescueBody
- Defined in:
- lib/mutineer/mutators/rescue_body.rb
Overview
Rescue-body mutator (Tier-2, #418).
Replaces the whole body of each rescue clause with nil, and the
value of a rescue modifier (x rescue y becomes x rescue nil). The
mutant survives when no test checks what happens after the error.
statement_removal never touches the last statement of a list, so
without this operator a one-statement rescue body gets no mutant.
A re-raise or a retry body is mutated too: nil swallows the error,
which a test that expects the error kills. An empty body and a body
that is already nil, (nil) or () give no mutant, because the edit
would change nothing. An ensure body is not a rescue body. On the
Fizzy copy (#418), 8 of 35 candidates give no mutant this way: 3 empty
bodies, 3 nil bodies and 2 nil modifier values.
The operator declines a body whose value is discarded and that only holds literals and variable reads (see docs/equivalent-skips.md).
Constant Summary collapse
- INERT =
Statements that change no state and raise nothing, so dropping one whose value is discarded changes nothing.
[Prism::IntegerNode, Prism::FloatNode, Prism::RationalNode, Prism::ImaginaryNode, Prism::SymbolNode, Prism::StringNode, Prism::TrueNode, Prism::FalseNode, Prism::NilNode, Prism::SelfNode, Prism::LocalVariableReadNode, Prism::InstanceVariableReadNode].freeze
Instance Method Summary collapse
-
#emit(node, discarded) ⇒ void
private
Emits a mutation that replaces the node with
nil. -
#inert?(node) ⇒ Boolean
private
Returns whether every statement in the node is a literal or a variable read.
-
#mutations_for(subject, source) ⇒ Array<Mutineer::Mutation>
Collects rescue-body mutations for a subject.
-
#nil_value?(node) ⇒ Boolean
private
Returns whether the node is
nil, alone or in parentheses, or(). -
#visit_begin_node(node) ⇒ void
Tracks whether this
beginblock's value, and so its rescue values, is discarded. -
#visit_rescue_modifier_node(node) ⇒ void
Visits rescue modifiers and emits a value removal.
-
#visit_rescue_node(node) ⇒ void
Visits rescue clauses and emits a body removal.
-
#visit_statements_node(node) ⇒ void
Marks every statement but the last as discarded: nothing reads its value.
Methods inherited from Base
#dangling_heredoc?, #decline, #declined, #drop_dangling_heredocs, #heredoc?, #visit_def_node, #visit_singleton_class_node
Instance Method Details
#emit(node, discarded) ⇒ void (private)
This method returns an undefined value.
Emits a mutation that replaces the node with nil.
88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/mutineer/mutators/rescue_body.rb', line 88 def emit(node, discarded) return if node.nil? || nil_value?(node) return decline if discarded && inert?(node) @mutations << Mutation.new( start_offset: node.location.start_offset, end_offset: node.location.end_offset, replacement: "nil", operator: :rescue_body ) end |
#inert?(node) ⇒ Boolean (private)
Returns whether every statement in the node is a literal or a variable read.
117 118 119 120 121 122 123 |
# File 'lib/mutineer/mutators/rescue_body.rb', line 117 def inert?(node) case node when Prism::StatementsNode then node.body.all? { |statement| inert?(statement) } when Prism::ParenthesesNode then node.body.nil? || inert?(node.body) else INERT.any? { |klass| node.is_a?(klass) } end end |
#mutations_for(subject, source) ⇒ Array<Mutineer::Mutation>
Collects rescue-body mutations for a subject.
36 37 38 39 40 |
# File 'lib/mutineer/mutators/rescue_body.rb', line 36 def mutations_for(subject, source) @discarded = {}.compare_by_identity @value_discarded = false super end |
#nil_value?(node) ⇒ Boolean (private)
Returns whether the node is nil, alone or in parentheses, or ().
104 105 106 107 108 109 110 111 |
# File 'lib/mutineer/mutators/rescue_body.rb', line 104 def nil_value?(node) case node when Prism::NilNode then true when Prism::StatementsNode then node.body.size == 1 && nil_value?(node.body.first) when Prism::ParenthesesNode then node.body.nil? || nil_value?(node.body) else false end end |
#visit_begin_node(node) ⇒ void
This method returns an undefined value.
Tracks whether this begin block's value, and so its rescue values, is discarded.
55 56 57 58 59 60 61 |
# File 'lib/mutineer/mutators/rescue_body.rb', line 55 def visit_begin_node(node) outer = @value_discarded @value_discarded = @discarded.key?(node) super ensure @value_discarded = outer end |
#visit_rescue_modifier_node(node) ⇒ void
This method returns an undefined value.
Visits rescue modifiers and emits a value removal.
76 77 78 79 |
# File 'lib/mutineer/mutators/rescue_body.rb', line 76 def visit_rescue_modifier_node(node) emit(node.rescue_expression, @discarded.key?(node)) super end |
#visit_rescue_node(node) ⇒ void
This method returns an undefined value.
Visits rescue clauses and emits a body removal.
67 68 69 70 |
# File 'lib/mutineer/mutators/rescue_body.rb', line 67 def visit_rescue_node(node) emit(node.statements, @value_discarded) super # the next clause and nested rescues each get their own mutation end |
#visit_statements_node(node) ⇒ void
This method returns an undefined value.
Marks every statement but the last as discarded: nothing reads its value.
46 47 48 49 |
# File 'lib/mutineer/mutators/rescue_body.rb', line 46 def visit_statements_node(node) node.body[0...-1].each { |statement| @discarded[statement] = true } super end |