From a58c6ff73281a8d48851fbd6480252b8d1dcda5a Mon Sep 17 00:00:00 2001 From: Daniel Gollahon Date: Sat, 5 Sep 2020 21:28:14 -0600 Subject: [PATCH] Add block-pass mutations - Adds `foo(&method(:bar))` -> `foo(&public_method(:bar))` and similar mutations. --- Changelog.md | 1 + lib/mutant.rb | 1 + lib/mutant/mutator/node/block_pass.rb | 20 ++++++++++++++++++++ lib/mutant/mutator/node/noop.rb | 2 +- meta/block.rb | 7 +++++++ meta/block_pass.rb | 17 +++++++++++++++++ meta/send.rb | 4 ++++ 7 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 lib/mutant/mutator/node/block_pass.rb diff --git a/Changelog.md b/Changelog.md index 69685bb88..8d05affa2 100644 --- a/Changelog.md +++ b/Changelog.md @@ -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) diff --git a/lib/mutant.rb b/lib/mutant.rb index 37d4f7681..7b2133f71 100644 --- a/lib/mutant.rb +++ b/lib/mutant.rb @@ -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' diff --git a/lib/mutant/mutator/node/block_pass.rb b/lib/mutant/mutator/node/block_pass.rb new file mode 100644 index 000000000..f5005e474 --- /dev/null +++ b/lib/mutant/mutator/node/block_pass.rb @@ -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 diff --git a/lib/mutant/mutator/node/noop.rb b/lib/mutant/mutator/node/noop.rb index a56b8a0ce..6bc7fb1a0 100644 --- a/lib/mutant/mutator/node/noop.rb +++ b/lib/mutant/mutator/node/noop.rb @@ -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 diff --git a/meta/block.rb b/meta/block.rb index df05b8ea7..2db4d39c6 100644 --- a/meta/block.rb +++ b/meta/block.rb @@ -184,6 +184,9 @@ mutation 'foo.baz { }' mutation 'foo(&:bar).baz' mutation 'self.baz {}' + mutation 'foo(&nil).baz {}' + mutation 'foo(&self).baz {}' + mutation 'foo(&:bar__mutant__).baz {}' end Mutant::Meta::Example.add :block do @@ -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 diff --git a/meta/block_pass.rb b/meta/block_pass.rb index e23306852..6bbbb729e 100644 --- a/meta/block_pass.rb +++ b/meta/block_pass.rb @@ -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 diff --git a/meta/send.rb b/meta/send.rb index 6f0af5dcc..30668fbb4 100644 --- a/meta/send.rb +++ b/meta/send.rb @@ -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)' @@ -422,6 +424,8 @@ singleton_mutations mutation 'foo' + mutation 'foo(&nil)' + mutation 'foo(&self)' end Mutant::Meta::Example.add :send do