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 predicate method mutations #1192

Merged
merged 1 commit into from
Jan 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Unreleased

* [#1192](https://github.com/mbj/mutant/pull/1192)

* Add mutations from predicate-like methods (methods ending in ?) to `true`/`false`
* `a.b?` -> `false`
* `a.b?` -> `true`

* [#1186](https://github.com/mbj/mutant/pull/1186)

Add additional `*` -> `+` regexp quantifier mutations:
Expand Down
8 changes: 8 additions & 0 deletions lib/mutant/mutator/node/send.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ def normal_dispatch
end

def emit_selector_specific_mutations
emit_predicate_mutations
emit_array_mutation
emit_static_send
emit_const_get_mutation
Expand All @@ -93,6 +94,13 @@ def emit_selector_specific_mutations
emit_lambda_mutation
end

def emit_predicate_mutations
return unless selector.match?(/\?\z/) && !selector.equal?(:defined?)
Copy link
Owner

Choose a reason for hiding this comment

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

I think we should go for selector.to_s.end_with?('?') instead of the regexp.

Rationale: Asking if a string is a suffix is provided natively by String. And regexp are a "too powerful tool" for this operation. We should use the simplest API that gets the job done and not use "untapped power" (of regexp in this case).

Copy link
Collaborator Author

@dgollahon dgollahon Jan 2, 2021

Choose a reason for hiding this comment

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

I agree but this was to workaround to adding an --ignore-subject. Later rubies support end_with?('?') directly on Symbol.

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 am actually working on adding a mutation to #start_with? and #end_with? (there is an issue for the former) so this may become an alive again later anyway. How would you like me to resolve this?

Copy link
Owner

Choose a reason for hiding this comment

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

lets stay with regexp. For the moment, till mutant forces us to think about it again when we add the static suffix and prefix regexp to native method call mutations.


emit(s(:true))
emit(s(:false))
end

def emit_array_mutation
return unless selector.equal?(:Array) && possible_kernel_method?

Expand Down
10 changes: 10 additions & 0 deletions meta/send.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,17 @@

singleton_mutations
mutation 'any?'
mutation 'false'
mutation 'true'
end

Mutant::Meta::Example.add :send do
source 'any?'

singleton_mutations
mutation 'all?'
mutation 'false'
mutation 'true'
end

Mutant::Meta::Example.add :send do
Expand Down Expand Up @@ -211,6 +215,8 @@
mutation 'foo.is_a?(self)'
mutation 'self.is_a?(bar)'
mutation 'foo.instance_of?(bar)'
mutation 'false'
mutation 'true'
end

Mutant::Meta::Example.add :send do
Expand All @@ -224,6 +230,8 @@
mutation 'foo.is_a?(self)'
mutation 'self.is_a?(bar)'
mutation 'foo.instance_of?(bar)'
mutation 'false'
mutation 'true'
end

Mutant::Meta::Example.add :send do
Expand All @@ -237,6 +245,8 @@
mutation 'foo.kind_of?(self)'
mutation 'self.kind_of?(bar)'
mutation 'foo.instance_of?(bar)'
mutation 'false'
mutation 'true'
end

Mutant::Meta::Example.add :send do
Expand Down