free_mutant/lib/mutant/mutator/node/send.rb

208 lines
4.5 KiB
Ruby
Raw Normal View History

# encoding: utf-8
module Mutant
class Mutator
class Node
# Namespace for send mutators
class Send < self
handle(:send)
children :receiver, :selector
SELECTOR_REPLACEMENTS = {
send: :public_send,
gsub: :sub
}.freeze
INDEX_REFERENCE = :[]
INDEX_ASSIGN = :[]=
ASSIGN_SUFFIX = :'='
# Base mutator for index operations
class Index < self
# Mutator for index references
class Reference < self
# Perform dispatch
#
# @return [undefined]
#
# @api private
#
def dispatch
emit(receiver)
end
end # Reference
# Mutator for index assignments
class Assign < self
# Perform dispatch
#
# @return [undefined]
#
# @api private
#
def dispatch
emit(receiver)
end
end # Assign
end # Index
private
# Perform dispatch
#
# @return [undefined]
#
# @api private
#
def dispatch
case selector
when INDEX_REFERENCE
run(Index::Reference)
when INDEX_ASSIGN
run(Index::Assign)
else
non_index_dispatch
end
emit_nil
end
# Perform non index dispatch
#
# @return [undefined]
#
# @api private
#
def non_index_dispatch
if binary_operator?
run(Binary)
return
end
normal_dispatch
end
2013-06-21 09:58:35 -04:00
# Return arguments
#
# @return [Enumerable<Parser::AST::Node>]
#
# @api private
#
alias_method :arguments, :remaining_children
# Perform normal, non special case dispatch
#
# @return [undefined]
#
# @api private
#
def normal_dispatch
emit_naked_receiver
2013-09-02 15:29:44 -04:00
emit_selector_replacement
mutate_receiver
2013-06-15 11:16:34 -04:00
emit_argument_propagation
mutate_arguments
end
2013-09-02 15:29:44 -04:00
# Emit selector replacement
#
# @return [undefined]
#
# @api private
#
2013-09-02 15:29:44 -04:00
def emit_selector_replacement
replacement = SELECTOR_REPLACEMENTS.fetch(selector) { return }
emit_selector(replacement)
end
# Emit naked receiver mutation
#
# @return [undefined]
#
# @api private
#
def emit_naked_receiver
return unless receiver
op_assign = OP_ASSIGN.include?(parent_type)
not_assignable = NOT_ASSIGNABLE.include?(receiver.type)
return if op_assign and not_assignable
emit(receiver)
end
# Test for binary operator
#
# @return [true]
# if send is a binary operator
#
# @return [false]
# otherwise
#
# @api private
#
def binary_operator?
arguments.one? && BINARY_METHOD_OPERATORS.include?(selector)
end
# Mutate arguments
#
# @return [undefined]
#
# @api private
#
def mutate_arguments
return if arguments.empty?
emit_self(receiver, selector)
remaining_children_with_index.each do |node, index|
mutate_child(index)
delete_child(index)
end
end
2013-06-15 11:16:34 -04:00
# Emit argument propagation
#
2013-06-17 05:47:27 -04:00
# @return [undefined]
2013-06-15 11:16:34 -04:00
#
# @api private
#
def emit_argument_propagation
return unless arguments.one?
node = arguments.first
return if NOT_STANDALONE.include?(node.type)
2013-06-15 11:16:34 -04:00
emit(arguments.first)
end
# Emit receiver mutations
#
# @return [undefined]
#
# @api private
#
def mutate_receiver
return unless receiver
emit_implicit_self
emit_receiver_mutations
end
# Emit implicit self mutation
#
# @return [undefined]
#
2013-06-15 10:37:43 -04:00
# @api private
#
def emit_implicit_self
if receiver.type == :self and !KEYWORDS.include?(selector)
emit_receiver(nil)
end
end
end # Send
end # Node
end # Mutator
end # Mutant