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 block-pass mutations #1047

Merged
merged 1 commit into from
Sep 6, 2020
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
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Unreleased

* Add block-pass mutations (`foo(&method(:bar))` -> `foo(&public_method(:bar))`) [#1047](https://github.com/mbj/mutant/pull/1047)
* Add new mutation of `Array(foo)` -> `[foo]` [#1043](https://github.com/mbj/mutant/pull/1043)
* Add new mutation to mutate dynamic sends to static sends ({`foo.__send__(:bar)`, `foo.send(:bar)`, `foo.public_send(:bar)`} -> `foo.bar`) [#1040](https://github.com/mbj/mutant/pull/1040)

Expand Down
1 change: 1 addition & 0 deletions lib/mutant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ module Mutant
require 'mutant/mutator/node/masgn'
require 'mutant/mutator/node/return'
require 'mutant/mutator/node/block'
require 'mutant/mutator/node/block_pass'
require 'mutant/mutator/node/if'
require 'mutant/mutator/node/case'
require 'mutant/mutator/node/splat'
Expand Down
20 changes: 20 additions & 0 deletions lib/mutant/mutator/node/block_pass.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true

module Mutant
class Mutator
class Node
class BlockPass < self

handle(:block_pass)

children :argument

private

def dispatch
emit_argument_mutations
end
end # Block
end # Node
end # Mutator
end # Mutant
2 changes: 1 addition & 1 deletion lib/mutant/mutator/node/noop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Node
# Mutation emitter to handle noop nodes
class Noop < self

handle(:__ENCODING__, :block_pass, :cbase, :lambda)
handle(:__ENCODING__, :cbase, :lambda)

private

Expand Down
7 changes: 7 additions & 0 deletions meta/block.rb
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,9 @@
mutation 'foo.baz { }'
mutation 'foo(&:bar).baz'
mutation 'self.baz {}'
mutation 'foo(&nil).baz {}'
mutation 'foo(&self).baz {}'
Comment on lines +187 to +188
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We could optionally skip &nil and &self since I think they are unlikely to yield alives but they are technically valid.

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 this skip is valid and should be done in a followup. Does not block this PR.

Copy link
Owner

Choose a reason for hiding this comment

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

To elaborate on why: They are "static" expressions. The outcome of &nil and &self is known ahead of time to not be useful.

mutation 'foo(&:bar__mutant__).baz {}'
end

Mutant::Meta::Example.add :block do
Expand All @@ -196,4 +199,8 @@
mutation 'foo.baz { }'
mutation 'self.baz { }'
mutation 'foo(nil, &:bar).baz'

mutation 'foo(nil, &self).baz {}'
mutation 'foo(nil, &nil).baz {}'
mutation 'foo(nil, &:bar__mutant__).baz {}'
end
17 changes: 17 additions & 0 deletions meta/block_pass.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,21 @@

singleton_mutations
mutation 'foo'
mutation 'foo(&nil)'
mutation 'foo(&self)'
end

Mutant::Meta::Example.add :block_pass do
source 'foo(&method(:bar))'

singleton_mutations
mutation 'foo'
mutation 'foo(&nil)'
mutation 'foo(&self)'
mutation 'foo(&method)'
mutation 'foo(&method(nil))'
mutation 'foo(&method(self))'
mutation 'foo(&method(:bar__mutant__))'
mutation 'foo(&public_method(:bar))'
mutation 'foo(&:bar)'
end
4 changes: 4 additions & 0 deletions meta/send.rb
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,8 @@
source 'foo.public_send(:bar, 1, two: true, **kwargs, &block)'

singleton_mutations
mutation 'foo.public_send(:bar, 1, { two: true, **kwargs }, &nil)'
mutation 'foo.public_send(:bar, 1, { two: true, **kwargs }, &self)'
mutation 'foo.bar(1, two: true, **kwargs, &block)'
mutation 'foo'
mutation 'self.public_send(:bar, 1, { two: true, **kwargs }, &block)'
Expand Down Expand Up @@ -422,6 +424,8 @@

singleton_mutations
mutation 'foo'
mutation 'foo(&nil)'
mutation 'foo(&self)'
end

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