From 890f02e7fed99149ebace28e8fade62a2aff0110 Mon Sep 17 00:00:00 2001 From: Markus Schirp Date: Wed, 1 Aug 2012 16:58:29 +0200 Subject: [PATCH] Handle 18 vs 19-mode differencies for if-statement --- lib/mutant.rb | 12 ++++++++++++ lib/mutant/mutator/if_statement.rb | 4 ++++ .../mutator/{if => if_statement}/mutation_spec.rb | 8 +++++++- 3 files changed, 23 insertions(+), 1 deletion(-) rename spec/unit/mutant/mutator/{if => if_statement}/mutation_spec.rb (71%) diff --git a/lib/mutant.rb b/lib/mutant.rb index 9148aaa3..df61d78b 100644 --- a/lib/mutant.rb +++ b/lib/mutant.rb @@ -19,6 +19,18 @@ module Mutant def self.deep_clone(object) Marshal.load(Marshal.dump(object)) end + + # Check for ruby-1.8 mode + # + # @return [true] + # returns true if running under 1.8 mode + # + # @return [false] + # returns false otherwise + # + def self.on_18? + RUBY_VERSION == '1.8.7' + end end require 'mutant/support/abstract' diff --git a/lib/mutant/mutator/if_statement.rb b/lib/mutant/mutator/if_statement.rb index 1517c5ec..867ea6f7 100644 --- a/lib/mutant/mutator/if_statement.rb +++ b/lib/mutant/mutator/if_statement.rb @@ -69,6 +69,10 @@ module Mutant # capture literal symbol. # def invert(node) + if Mutant.on_18? + return new(Rubinius::AST::Not,node) + end + new_send(node,:'!') end diff --git a/spec/unit/mutant/mutator/if/mutation_spec.rb b/spec/unit/mutant/mutator/if_statement/mutation_spec.rb similarity index 71% rename from spec/unit/mutant/mutator/if/mutation_spec.rb rename to spec/unit/mutant/mutator/if_statement/mutation_spec.rb index 142ed2bd..140b3472 100644 --- a/spec/unit/mutant/mutator/if/mutation_spec.rb +++ b/spec/unit/mutant/mutator/if_statement/mutation_spec.rb @@ -10,7 +10,13 @@ describe Mutant::Mutator, 'if statement' do mutants << 'if condition; true; else false; end' # Invert condition - mutants << 'if !self.condition; true; else false; end' + if Mutant.on_18? + # Explicitly define ast as 18-mode does swap if and else on parsing when negation condition is + # present in condition. + mutants << [:if, [:not, [:call, [:self], :condition, [:arglist]]], [:true], [:false]] + else + mutants << 'if !self.condition; true; else false; end' + end # Deleted else branch mutants << 'if self.condition; true end'