Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add #start_with? and #end_with? mutations #1201

Merged
merged 2 commits into from
Jan 7, 2021

Conversation

dgollahon
Copy link
Collaborator

@dgollahon dgollahon commented Jan 3, 2021

  • Adds the following mutations:
    • a.match(/\Atext/) -> b.start_with?('text')
    • a.match?(/\Atext/) -> b.start_with?('text')
    • a =~ /\Atext/ -> b.start_with?('text')
  • NOTE: Adds a Mutant::AST::Regexp.expand_regexp_ast to avoid repeating AST expansion logic in the send mutator. At that level, the node appears as a simple parser s(:regexp ...) node so I chose to fully parse the regexp rather than try to do a string pattern.
  • Also adds 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')
  • Closes Mutate regexps in form /\Astatic/ to String#start_with? #169.

@dgollahon dgollahon force-pushed the start_with-end_with-mutations branch from 515e3de to 0a5ade5 Compare January 3, 2021 05:34
@dgollahon dgollahon changed the title Add /\Astatic/ -> #start_with? mutations Add #start_with? and #end_with? mutations Jan 3, 2021
@dgollahon dgollahon requested a review from mbj January 3, 2021 05:35
#
# @return [Parser::AST::Node]
def self.expand_regexp_ast(node)
*body, _opts = node.children
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that destructuring instead of using #slice avoids the mutation we chose to ignore before.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a followup we could also promote interpolations.

a.match?(/\Afoo#{dynamic}/)

to:

a.end_with?(dynamic)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, something like that might be interesting but I am not sure I will go that deep right away. I have a couple of other small improvements for similar cases I am thinking of as well that are pending.

@dgollahon dgollahon force-pushed the start_with-end_with-mutations branch from cfbe4f1 to 6533a8d Compare January 3, 2021 09:15

RSpec.describe Mutant::AST::Regexp, '.expand_regexp_ast' do
it 'returns the expanded AST' do
# /foo/
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non blocking: Instead of this comment: what about running the regexp parser / AST transform to get the parser_ast ? This way this test is more meaningful and would have canary properties for future changes?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that makes sense--I have updated this to match. When I was originally writing the spec it felt like it might be clearer to have the raw AST so I started with that but now that I have just swapped it out for Unparser.parse I think it's a lot simpler to read and, as you say, it has a slight "canary" property.

@dgollahon dgollahon force-pushed the start_with-end_with-mutations branch from dd5ab04 to e0042ad Compare January 7, 2021 03:54
- Adds the following mutations:
  * `a.match(/\Atext/)` -> `b.start_with?('text')`
  * `a.match?(/\Atext/)` -> `b.start_with?('text')`
  * `a =~ /\Atext/` -> `b.start_with?('text')`
- NOTE: Adds a `Mutant::AST::Regexp.expand_regexp_ast` to avoid repeating AST expansion logic in the `send` mutator. At that level, the node appears as a simple `parser` `s(:regexp ...)` node so I chose to fully parse the regexp rather than try to do a string pattern.
- Closes #169
- 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')`
@dgollahon dgollahon force-pushed the start_with-end_with-mutations branch from c106234 to 635a529 Compare January 7, 2021 09:21
@dgollahon dgollahon merged commit 4ac7151 into master Jan 7, 2021
@dgollahon dgollahon deleted the start_with-end_with-mutations branch January 7, 2021 19:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Mutate regexps in form /\Astatic/ to String#start_with?
2 participants