From 353e25c740f5c31d202a67444b3973108e787554 Mon Sep 17 00:00:00 2001 From: Markus Schirp Date: Sun, 12 May 2024 00:16:44 +0000 Subject: [PATCH] Add ordering mutations [fix #1442] --- Changelog.md | 12 ++++++++ lib/mutant/mutation/operators.rb | 10 +++++++ meta/send.rb | 51 ++++++++++++++++++++++++++++++++ 3 files changed, 73 insertions(+) diff --git a/Changelog.md b/Changelog.md index e59c9a057..eb6e4da36 100644 --- a/Changelog.md +++ b/Changelog.md @@ -1,5 +1,17 @@ # v0.12.1 2024-05-11 +* [#1444](https://github.com/mbj/mutant/pull/1444) + + Add ordering mutations to full and light operator set, + `#find/#detect/#min/#max/#min_by/#max_by -> #first/#last`. These mutations force + to provide test cases with more than one element to observe the extra + semantics by these methods over just `#first` and `#last`. + + Add `#first -> #last` and `#last -> #first` to full operator set. These where + highly controversial in the past and thus where removed. People who do not care + about ordering of their collections can now choose the light operator set, so people who care + (and should care) can now care (again). + * [#1443](https://github.com/mbj/mutant/pull/1443) Change mutants test runner to always report in test definition order deterministically. diff --git a/lib/mutant/mutation/operators.rb b/lib/mutant/mutation/operators.rb index 770c5e201..49e7c8498 100644 --- a/lib/mutant/mutation/operators.rb +++ b/lib/mutant/mutation/operators.rb @@ -21,14 +21,22 @@ class Full < self all?: %i[any?], any?: %i[all?], at: %i[fetch key?], + detect: %i[first last], fetch: %i[key?], + find: %i[first last], + first: %i[last], flat_map: %i[map], gsub: %i[sub], is_a?: %i[instance_of?], kind_of?: %i[instance_of?], + last: %i[first], map: %i[each], match: %i[match?], + max: %i[first last], + max_by: %i[first last], method: %i[public_method], + min: %i[first last], + min_by: %i[first last], reverse_each: %i[each], reverse_map: %i[map each], reverse_merge: %i[merge], @@ -49,6 +57,8 @@ class Light < self .tap do |replacements| replacements.delete(:==) replacements.delete(:eql?) + replacements.delete(:first) + replacements.delete(:last) end .freeze end diff --git a/meta/send.rb b/meta/send.rb index 022e2d526..c6a0ec6c7 100644 --- a/meta/send.rb +++ b/meta/send.rb @@ -884,3 +884,54 @@ mutation 'foo(nil)' mutation 'foo(:"+__mutant__")' end + +Mutant::Meta::Example.add :send, operators: :light do + source 'first' + + singleton_mutations +end + +Mutant::Meta::Example.add :send, operators: :light do + source 'last' + + singleton_mutations +end + +Mutant::Meta::Example.add :send, operators: :full do + source 'first' + + singleton_mutations + + mutation 'last' +end + +Mutant::Meta::Example.add :send, operators: :full do + source 'last' + + singleton_mutations + + mutation 'first' +end + +%w[detect find max max_by min min_by].each do |selector| + Mutant::Meta::Example.add :send do + source selector + + singleton_mutations + + mutation 'first' + mutation 'last' + end + + Mutant::Meta::Example.add :send do + source "#{selector}(&:block)" + + singleton_mutations + + mutation "#{selector}(&:block__mutant__)" + mutation "#{selector}(&nil)" + mutation 'first(&:block)' + mutation 'last(&:block)' + mutation selector + end +end