Class: Mutineer::Mutators::CallToReceiver
- Defined in:
- lib/mutineer/mutators/call_to_receiver.rb
Overview
Call-to-receiver mutator (Tier-2).
Replaces the last call of a chain with its receiver, with the arguments
and the block of that call: user.full_name becomes user, and
ids.uniq.sort becomes ids.uniq. A chain begins at a receiver that is
not a dotted call, as in ChainLink. A call in an argument or a block
starts a chain of its own.
The mutant survives when no test checks what the last call does: a strip, a uniq, a compact or a lookup the tests never see.
The operator does not overlap with ChainLink. chain_link drops a link
in the middle of a chain, and this operator drops the last call. A call
that is the receiver of a dotted call, or of a call write or a call
target, is a link, so it gets no mutant here. The two operators give the
same source only when the last call repeats the link before it, as in
node.name.name. When both edits start on the same line, the job plan
runs one copy, the chain_link one (#275).
Calls in SKIPPED are never replaced; each one counts as declined. The
list is shorter than Mutineer::Mutators::ChainLink::SKIPPED: on the last call, a conversion
such as to_s or to_i decides the type of the value that the
expression returns, so m[1].to_i becomes m[1]. Foo.new becomes the
class, and almost any use of the class raises.
A call is not replaced when its name is an operator (a.+(1)), or a
setter (a.b = 1). A call with no dot (a + b, a[0]) is not replaced.
Constant Summary collapse
- WORD_NAME =
Matches the name of a call written as a word, not as an operator.
/\A[[:alpha:]_]/- SKIPPED =
Method names whose last call is never replaced. A name is here when its mutant is most often equivalent, or when almost any test that runs the line kills it.
freezeanditselfreturn the receiver.dupandclonereturn a copy, so the mutant shows when code later changes the copy or the original, or compares the result with the original by identity. Droppingto_setmost often leaves an Array, which answersinclude?andempty?as a Set does (it keeps duplicates, sosizeandeachcan differ).newleaves the class in place of an instance, which almost any test that runs the line kills. %i[new dup clone freeze itself to_set].freeze
Instance Method Summary collapse
-
#dotted?(node) ⇒ Boolean
private
Returns whether a node is a call through
.,&.or::. -
#mark(node) ⇒ void
private
Records the receiver of a node as a link when the node is a dotted call or a call write or target.
-
#mutations_for(subject, source) ⇒ Array<Mutineer::Mutation>
Resets the per-subject record of calls that are links.
-
#replace(node) ⇒ void
private
Emits the replacement of a call by its receiver, unless the name is an operator, a setter, or in SKIPPED.
-
#visit_call_and_write_node(node) ⇒ void
Visits
a.b.c &&= 1. -
#visit_call_node(node) ⇒ void
Visits a call.
-
#visit_call_operator_write_node(node) ⇒ void
Visits
a.b.c += 1. -
#visit_call_or_write_node(node) ⇒ void
Visits
a.b.c ||= 1. -
#visit_call_target_node(node) ⇒ void
Visits a call used as an assignment target, as in
a.b.c, d = 1, 2.
Methods inherited from Base
#dangling_heredoc?, #decline, #declined, #drop_dangling_heredocs, #heredoc?, #visit_def_node, #visit_singleton_class_node
Instance Method Details
#dotted?(node) ⇒ Boolean (private)
Returns whether a node is a call through ., &. or ::.
143 144 145 |
# File 'lib/mutineer/mutators/call_to_receiver.rb', line 143 def dotted?(node) node.is_a?(Prism::CallNode) && !node.call_operator_loc.nil? end |
#mark(node) ⇒ void (private)
This method returns an undefined value.
Records the receiver of a node as a link when the node is a dotted call or a call write or target. The receiver of a call with no dot is not a link. It is the last call of a chain of its own.
115 116 117 |
# File 'lib/mutineer/mutators/call_to_receiver.rb', line 115 def mark(node) @links[node.receiver] = true if dotted?(node) || !node.is_a?(Prism::CallNode) end |
#mutations_for(subject, source) ⇒ Array<Mutineer::Mutation>
Resets the per-subject record of calls that are links.
54 55 56 57 |
# File 'lib/mutineer/mutators/call_to_receiver.rb', line 54 def mutations_for(subject, source) @links = {}.compare_by_identity super end |
#replace(node) ⇒ void (private)
This method returns an undefined value.
Emits the replacement of a call by its receiver, unless the name is an operator, a setter, or in SKIPPED.
124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/mutineer/mutators/call_to_receiver.rb', line 124 def replace(node) name = node.name.to_s return unless name.match?(WORD_NAME) return if name.end_with?("=") return decline if SKIPPED.include?(node.name) receiver = node.receiver.location @mutations << Mutation.new( start_offset: node.location.start_offset, end_offset: node.location.end_offset, replacement: @source.byteslice(receiver.start_offset, receiver.end_offset - receiver.start_offset), operator: :call_to_receiver ) end |
#visit_call_and_write_node(node) ⇒ void
This method returns an undefined value.
Visits a.b.c &&= 1. Its receiver is a link.
92 93 94 95 |
# File 'lib/mutineer/mutators/call_to_receiver.rb', line 92 def visit_call_and_write_node(node) mark(node) super end |
#visit_call_node(node) ⇒ void
This method returns an undefined value.
Visits a call. A dotted call that is not a link is the last call of its chain.
64 65 66 67 68 |
# File 'lib/mutineer/mutators/call_to_receiver.rb', line 64 def visit_call_node(node) mark(node) replace(node) if dotted?(node) && !@links.key?(node) super end |
#visit_call_operator_write_node(node) ⇒ void
This method returns an undefined value.
Visits a.b.c += 1. Its receiver is a link.
74 75 76 77 |
# File 'lib/mutineer/mutators/call_to_receiver.rb', line 74 def visit_call_operator_write_node(node) mark(node) super end |
#visit_call_or_write_node(node) ⇒ void
This method returns an undefined value.
Visits a.b.c ||= 1. Its receiver is a link.
83 84 85 86 |
# File 'lib/mutineer/mutators/call_to_receiver.rb', line 83 def visit_call_or_write_node(node) mark(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 a.b.c, d = 1, 2.
Its receiver is a link.
102 103 104 105 |
# File 'lib/mutineer/mutators/call_to_receiver.rb', line 102 def visit_call_target_node(node) mark(node) super end |