From 2f53ef0d01b0e86ecc0c634344711ad24265aa38 Mon Sep 17 00:00:00 2001 From: John Backus Date: Thu, 20 Aug 2015 01:42:09 -0700 Subject: [PATCH] Add mutations from `and`/`or` into `if`/`else` Closes #233 --- Changelog.md | 1 + lib/mutant/mutator/node/binary.rb | 17 +++++++++++++++++ meta/and.rb | 1 + meta/or.rb | 1 + 4 files changed, 20 insertions(+) diff --git a/Changelog.md b/Changelog.md index a1b4e6e1..df33fb4c 100644 --- a/Changelog.md +++ b/Changelog.md @@ -3,6 +3,7 @@ * Remove invalid mutation `super(...)` to `super` * Add mutation from `def foo(a = true); end` to `def foo(a = true); a = true; end` #419 * Add mutation from `def foo; end` to `remove_method :foo` #413 +* Add mutation from `a or b` to `b unless a` and `a and b` to `b if a` #430 # v0.8.2 2015-08-11 diff --git a/lib/mutant/mutator/node/binary.rb b/lib/mutant/mutator/node/binary.rb index 41fe8f67..9a46c9d6 100644 --- a/lib/mutant/mutator/node/binary.rb +++ b/lib/mutant/mutator/node/binary.rb @@ -24,6 +24,7 @@ module Mutant emit_singletons emit_promotions emit_operator_mutations + emit_conditional_mutations emit_left_negation emit_left_mutations emit_right_mutations @@ -38,6 +39,22 @@ module Mutant emit(s(INVERSE.fetch(node.type), left, right)) end + # Mutate `and` and `or` into `if` and `unless` + # + # - `a or b` becomes `b unless a` + # - `a and b` becomes `b if a` + # + # @return [undefined] + # + # @api private + def emit_conditional_mutations + if n_or?(node) + emit(s(:if, left, nil, right)) + else + emit(s(:if, left, right, nil)) + end + end + # Emit promotions # # @return [undefined] diff --git a/meta/and.rb b/meta/and.rb index db7c71d2..278dabdb 100644 --- a/meta/and.rb +++ b/meta/and.rb @@ -10,4 +10,5 @@ Mutant::Meta::Example.add do mutation 'false and false' mutation 'true and true' mutation '!true and false' + mutation 'false if true' end diff --git a/meta/or.rb b/meta/or.rb index c452ad7b..833cc06f 100644 --- a/meta/or.rb +++ b/meta/or.rb @@ -10,4 +10,5 @@ Mutant::Meta::Example.add do mutation 'true or true' mutation 'true and false' mutation '!true or false' + mutation 'false unless true' end