From 630d9e895c31969dfbfacc36660cac2c28d85bc0 Mon Sep 17 00:00:00 2001 From: Daniel Gollahon Date: Sat, 2 Jan 2021 22:17:33 -0800 Subject: [PATCH] Add `#reduce` -> `#sum` mutations Adds: * `a.reduce(:+)` -> `a.sum` * `a.reduce(0, &:+)` -> `a.sum(0)` * ... --- Changelog.md | 6 ++++ lib/mutant/mutator/node/send.rb | 16 +++++++++ meta/send.rb | 58 +++++++++++++++++++++++++++++++++ 3 files changed, 80 insertions(+) diff --git a/Changelog.md b/Changelog.md index 8cefd4e51..427f4157d 100644 --- a/Changelog.md +++ b/Changelog.md @@ -1,5 +1,11 @@ # Unreleased +* [#1202](https://github.com/mbj/mutant/pull/1202) + + * Add `#reduce` -> `#sum` mutations + * `a.reduce(:+)` -> `a.sum` + * `a.reduce(0, &:+)` -> `a.sum(0)` + * [#1201](https://github.com/mbj/mutant/pull/1201) * Add `/\Astatic/` -> `#start_with?` mutations: diff --git a/lib/mutant/mutator/node/send.rb b/lib/mutant/mutator/node/send.rb index 40ccead8c..2f6f1eb4b 100644 --- a/lib/mutant/mutator/node/send.rb +++ b/lib/mutant/mutator/node/send.rb @@ -88,6 +88,7 @@ def normal_dispatch end def emit_selector_specific_mutations + emit_reduce_to_sum_mutation emit_start_end_with_mutations emit_predicate_mutations emit_array_mutation @@ -99,6 +100,21 @@ def emit_selector_specific_mutations emit_lambda_mutation end + def emit_reduce_to_sum_mutation + return unless selector.equal?(:reduce) + + reducer = arguments.last + + return unless reducer.eql?(s(:sym, :+)) || reducer.eql?(s(:block_pass, s(:sym, :+))) + + if arguments.length > 1 + initial_value = arguments.first + emit_type(receiver, :sum, initial_value) + else + emit_type(receiver, :sum) + end + end + def emit_start_end_with_mutations # rubocop:disable Metrics/CyclomaticComplexity, Metrics/MethodLength return unless REGEXP_MATCH_METHODS.include?(selector) && arguments.one? diff --git a/meta/send.rb b/meta/send.rb index ddf530e42..6d639ebc2 100644 --- a/meta/send.rb +++ b/meta/send.rb @@ -826,3 +826,61 @@ mutation 'self.match(/foo\z/)' mutation "a.end_with?('foo')" end + + +Mutant::Meta::Example.add :send do + source 'a.reduce(:+)' + + singleton_mutations + + mutation 'a' + mutation 'self.reduce(:+)' + mutation 'a.reduce' + mutation 'a.reduce(nil)' + mutation 'a.reduce(self)' + mutation 'a.reduce(:"+__mutant__")' + mutation ':+' + mutation 'a.sum' +end + +Mutant::Meta::Example.add :send do + source 'a.reduce(INITIAL, &:+)' + + singleton_mutations + + mutation 'a' + mutation 'a.reduce' + mutation 'a.reduce(nil, &:+)' + mutation 'a.reduce(self, &:+)' + mutation 'a.reduce(INITIAL, &nil)' + mutation 'a.reduce(INITIAL, &self)' + mutation 'a.reduce(INITIAL)' + mutation 'a.reduce(&:+)' + mutation 'self.reduce(INITIAL, &:+)' + mutation 'a.reduce(INITIAL, &:"+__mutant__")' + mutation 'a.sum(INITIAL)' +end + +Mutant::Meta::Example.add :send do + source 'reduce(:*)' + + singleton_mutations + + mutation 'reduce' + mutation ':*' + mutation 'reduce(nil)' + mutation 'reduce(self)' + mutation 'reduce(:"*__mutant__")' +end + +Mutant::Meta::Example.add :send do + source 'foo(:+)' + + singleton_mutations + + mutation 'foo' + mutation ':+' + mutation 'foo(nil)' + mutation 'foo(self)' + mutation 'foo(:"+__mutant__")' +end