Remove duplication between next and break mutator

This commit is contained in:
Markus Schirp 2013-11-20 19:42:41 +01:00
parent 295588ddee
commit 73aad2f6a3
7 changed files with 48 additions and 73 deletions

View file

@ -1,3 +1,3 @@
---
threshold: 18
total_score: 787
total_score: 784

View file

@ -64,7 +64,6 @@ require 'mutant/mutator/node/argument'
require 'mutant/mutator/node/arguments'
require 'mutant/mutator/node/blockarg'
require 'mutant/mutator/node/begin'
require 'mutant/mutator/node/break'
require 'mutant/mutator/node/connective/binary'
require 'mutant/mutator/node/const'
require 'mutant/mutator/node/dstr'
@ -73,7 +72,7 @@ require 'mutant/mutator/node/kwbegin'
require 'mutant/mutator/node/named_value/access'
require 'mutant/mutator/node/named_value/constant_assignment'
require 'mutant/mutator/node/named_value/variable_assignment'
require 'mutant/mutator/node/next'
require 'mutant/mutator/node/loop_ctrl'
require 'mutant/mutator/node/noop'
require 'mutant/mutator/node/op_asgn'
require 'mutant/mutator/node/while'

View file

@ -1,30 +0,0 @@
# encoding: utf-8
module Mutant
class Mutator
class Node
# Break mutator
class Break < Generic
handle(:break)
private
# Emit mutations
#
# @return [undefined]
#
# @api private
#
def dispatch
super
children.each_index(&method(:delete_child))
emit(s(:next, *children))
emit_nil
end
end # Break
end # Node
end # Mutator
end # Mutant

View file

@ -4,10 +4,15 @@ module Mutant
class Mutator
class Node
# Next mutator
class Next < Generic
# Mutator for loop control keywords
class LoopControl < Generic
handle(:next)
INVERSE = IceNine.deep_freeze(
:next => :break,
:break => :next
)
handle(*INVERSE.keys)
private
@ -20,7 +25,7 @@ module Mutant
def dispatch
super
children.each_index(&method(:delete_child))
emit(s(:break, *children))
emit(s(INVERSE.fetch(node.type), *children))
emit_nil
end

View file

@ -1,18 +0,0 @@
# encoding: utf-8
require 'spec_helper'
describe Mutant::Mutator::Node::Break, 'break' do
let(:source) { 'break true' }
let(:mutations) do
mutations = []
mutations << 'break false'
mutations << 'break nil'
mutations << 'break'
mutations << 'nil'
mutations << 'next true'
end
it_should_behave_like 'a mutator'
end

View file

@ -0,0 +1,37 @@
# encoding: utf-8
require 'spec_helper'
describe Mutant::Mutator::Node::LoopControl do
context 'with break node' do
let(:source) { 'break true' }
let(:mutations) do
mutations = []
mutations << 'break false'
mutations << 'break nil'
mutations << 'break'
mutations << 'nil'
mutations << 'next true'
end
it_should_behave_like 'a mutator'
end
context 'with next node' do
let(:source) { 'next true' }
let(:mutations) do
mutations = []
mutations << 'next false'
mutations << 'next nil'
mutations << 'next'
mutations << 'nil'
mutations << 'break true'
end
it_should_behave_like 'a mutator'
end
end

View file

@ -1,18 +0,0 @@
# encoding: utf-8
require 'spec_helper'
describe Mutant::Mutator::Node::Next, 'next' do
let(:source) { 'next true' }
let(:mutations) do
mutations = []
mutations << 'next false'
mutations << 'next nil'
mutations << 'next'
mutations << 'nil'
mutations << 'break true'
end
it_should_behave_like 'a mutator'
end