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

74 lines
1.4 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2018-09-12 13:15:43 +00:00
2013-06-04 23:44:17 +02:00
module Mutant
class Mutator
class Node
# Mutator for required arguments
class Argument < self
2016-04-05 01:17:00 -07:00
handle(:arg, :kwarg)
2013-06-04 23:44:17 +02:00
UNDERSCORE = '_'.freeze
2013-06-21 15:04:44 +02:00
children :name
2013-06-04 23:44:17 +02:00
private
# Emit mutations
2013-06-04 23:44:17 +02:00
#
# @return [undefined]
def dispatch
emit_name_mutation
end
# Emit name mutations
#
# @return [undefined]
def emit_name_mutation
return if skip?
emit_name(:"#{UNDERSCORE}#{name}")
2013-06-04 23:44:17 +02:00
end
# Test if argument mutation is skipped
#
2014-06-15 19:27:57 +00:00
# @return [Boolean]
2013-06-04 23:44:17 +02:00
def skip?
name.to_s.start_with?(UNDERSCORE)
end
# Mutator for optional arguments
class Optional < self
2016-04-05 01:17:45 -07:00
TYPE_MAP = IceNine.deep_freeze(
optarg: :arg,
kwoptarg: :kwarg
)
handle(:optarg, :kwoptarg)
2013-06-04 23:44:17 +02:00
children :name, :default
2013-06-04 23:44:17 +02:00
private
# Emit mutations
2013-06-04 23:44:17 +02:00
#
# @return [undefined]
def dispatch
emit_name_mutation
emit_required_mutation
emit_default_mutations
end
# Emit required mutation
#
# @return [undefined]
def emit_required_mutation
2016-04-05 01:17:45 -07:00
emit(s(TYPE_MAP.fetch(node.type), name))
2013-06-04 23:44:17 +02:00
end
end # Optional
end # Argument
end # Node
end # Mutator
end # Mutant