Port block mutators to parser

This commit is contained in:
Markus Schirp 2013-06-14 20:19:53 +02:00
parent 7b4eea36d8
commit 7aa8fafed9
5 changed files with 118 additions and 7 deletions

View file

@ -62,8 +62,9 @@ require 'mutant/mutator/node/send/binary'
require 'mutant/mutator/node/when'
require 'mutant/mutator/node/assignment'
require 'mutant/mutator/node/define'
require 'mutant/mutator/node/mlhs'
require 'mutant/mutator/node/return'
require 'mutant/mutator/node/iter_19'
require 'mutant/mutator/node/block'
require 'mutant/mutator/node/if'
require 'mutant/mutator/node/case'
require 'mutant/config'

View file

@ -15,6 +15,23 @@ module Mutant
#
def dispatch
emit_children_mutations
emit_mlhs_expansion
end
# Emit mlhs expansions
#
# @return [undefined]
#
# @api private
#
def emit_mlhs_expansion
mlhs = children.each_with_index.select { |child, index| child.type == :mlhs }
mlhs.each do |child, index|
dup_children = children.dup
dup_children.delete_at(index)
dup_children.insert(index, *child.children)
emit_self(*dup_children)
end
end
end # Arguments

View file

@ -6,6 +6,10 @@ module Mutant
handle(:block)
SEND_INDEX, ARGUMENTS_INDEX, BODY_INDEX = 0, 1, 2
private
# Emit mutants
#
# @return [undefined]
@ -13,12 +17,10 @@ module Mutant
# @api private
#
def dispatch
emit_attribute_mutations(:body)
emit_attribute_mutations(:arguments) do |mutation|
arguments = mutation.arguments
arguments.names = arguments.required + arguments.optional
mutation
end if node.arguments
emit(children[SEND_INDEX])
mutate_child(SEND_INDEX)
mutate_child(ARGUMENTS_INDEX)
mutate_child(BODY_INDEX)
end
end # Block

View file

@ -0,0 +1,26 @@
module Mutant
class Mutator
class Node
class MLHS < self
handle(:mlhs)
private
# Emit mutations
#
# @return [undefined]
#
# @api private
#
def dispatch
children.each_index do |index|
mutate_child(index)
delete_child(index)
end
end
end # MLHS
end # Node
end # Mutator
end # Mutant

View file

@ -0,0 +1,65 @@
require 'spec_helper'
describe Mutant::Mutator, 'block' do
context 'with block' do
let(:source) { 'foo() { a; b }' }
let(:mutations) do
mutations = []
mutations << 'foo { a }'
mutations << 'foo { b }'
mutations << 'foo { nil }'
mutations << 'foo'
end
it_should_behave_like 'a mutator'
end
context 'with block args' do
let(:source) { 'foo { |a, b| }' }
before do
Mutant::Random.stub(:hex_string => :random)
end
let(:mutations) do
mutations = []
mutations << 'foo'
mutations << 'foo { |a, b| ::Object.new }'
mutations << 'foo { |a, srandom| }'
mutations << 'foo { |srandom, b| }'
mutations << 'foo { |a| }'
mutations << 'foo { |b| }'
mutations << 'foo { || }'
end
it_should_behave_like 'a mutator'
end
context 'with block pattern args' do
before do
Mutant::Random.stub(:hex_string => :random)
end
let(:source) { 'foo { |(a, b), c| }' }
let(:mutations) do
mutations = []
mutations << 'foo { || }'
mutations << 'foo { |a, b, c| }'
mutations << 'foo { |(a, b), c| ::Object.new }'
mutations << 'foo { |(a), c| }'
mutations << 'foo { |(b), c| }'
mutations << 'foo { |(a, b)| }'
mutations << 'foo { |c| }'
mutations << 'foo { |(srandom, b), c| }'
mutations << 'foo { |(a, srandom), c| }'
mutations << 'foo { |(a, b), srandom| }'
mutations << 'foo'
end
it_should_behave_like 'a mutator'
end
end