From cfbe4f1d48ad5b6004bbaae5ea923aa6f74dd06b Mon Sep 17 00:00:00 2001 From: Daniel Gollahon Date: Sat, 2 Jan 2021 20:08:15 -0800 Subject: [PATCH] Add `/static\z/` -> `#end_with?` mutations - Adds the following mutations: * `a.match(/text\z/)` -> `b.end_with?('text')` * `a.match?(/text\z/)` -> `b.end_with?('text')` * `a =~ /text\z/` -> `b.end_with?('text')` --- Changelog.md | 4 ++++ lib/mutant/mutator/node/send.rb | 15 ++++++++++----- meta/send.rb | 15 +++++++++++++++ 3 files changed, 29 insertions(+), 5 deletions(-) diff --git a/Changelog.md b/Changelog.md index 4b7914520..f0c69a11f 100644 --- a/Changelog.md +++ b/Changelog.md @@ -6,6 +6,10 @@ * `a.match(/\Atext/)` -> `b.start_with?('text')` * `a.match?(/\Atext/)` -> `b.start_with?('text')` * `a =~ /\Atext/` -> `b.start_with?('text')` + * Add `/static\z/` -> `#end_with?` mutations: + * `a.match(/text\z/)` -> `b.end_with?('text')` + * `a.match?(/text\z/)` -> `b.end_with?('text')` + * `a =~ /text\z/` -> `b.end_with?('text')` # v0.10.25 2021-01-02-03 diff --git a/lib/mutant/mutator/node/send.rb b/lib/mutant/mutator/node/send.rb index 9945412e7..cb8991bd6 100644 --- a/lib/mutant/mutator/node/send.rb +++ b/lib/mutant/mutator/node/send.rb @@ -107,11 +107,16 @@ def emit_start_with_mutation regexp_ast = AST::Regexp.expand_regexp_ast(argument) ) - return unless regexp_ast.children.map(&:type).eql?(%i[regexp_bos_anchor regexp_literal_literal]) - - literal = Mutant::Util.one(regexp_ast.children.last.children) - - emit(s(:send, receiver, :start_with?, s(:str, literal))) + regexp_children = regexp_ast.children + regexp_node_types = regexp_children.map(&:type) + + if regexp_node_types.eql?(%i[regexp_bos_anchor regexp_literal_literal]) + literal = Mutant::Util.one(regexp_children.last.children) + emit(s(:send, receiver, :start_with?, s(:str, literal))) + elsif regexp_node_types.eql?(%i[regexp_literal_literal regexp_eos_anchor]) + literal = Mutant::Util.one(regexp_children.first.children) + emit(s(:send, receiver, :end_with?, s(:str, literal))) + end end def emit_predicate_mutations diff --git a/meta/send.rb b/meta/send.rb index 79c48d663..ddf530e42 100644 --- a/meta/send.rb +++ b/meta/send.rb @@ -811,3 +811,18 @@ mutation 'foo(//)' mutation 'foo(/nomatch\A/)' end + +Mutant::Meta::Example.add :send do + source 'a.match(/foo\z/)' + + singleton_mutations + + mutation 'a.match?(/foo\z/)' + mutation 'a.match' + mutation 'a' + mutation '/foo\z/' + mutation 'a.match(//)' + mutation 'a.match(/nomatch\A/)' + mutation 'self.match(/foo\z/)' + mutation "a.end_with?('foo')" +end