Skip to content

Commit

Permalink
Add /static\z/ -> #end_with? mutations
Browse files Browse the repository at this point in the history
- 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')`
  • Loading branch information
dgollahon committed Jan 3, 2021
1 parent 0a5ade5 commit cfbe4f1
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
4 changes: 4 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
15 changes: 10 additions & 5 deletions lib/mutant/mutator/node/send.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions meta/send.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit cfbe4f1

Please sign in to comment.