From 9908744566240c247a9f3a77e68133e2ab2f04a3 Mon Sep 17 00:00:00 2001 From: Sandro Gehri Date: Fri, 17 Nov 2023 13:55:19 +0100 Subject: [PATCH] Add Removal mutators --- WIP.md | 1 - src/Mutators.php | 10 ++++ src/Mutators/Removal/RemoveFunctionCall.php | 41 ++++++++++++++ src/Mutators/Removal/RemoveMethodCall.php | 46 ++++++++++++++++ src/Mutators/Sets/DefaultSet.php | 1 + src/Mutators/Sets/RemovalSet.php | 26 +++++++++ src/Support/MutatorMap.php | 2 + src/Support/Printers/DefaultPrinter.php | 2 + .../Removal/RemoveFunctionCallTest.php | 17 ++++++ .../Mutators/Removal/RemoveMethodCallTest.php | 53 +++++++++++++++++++ 10 files changed, 198 insertions(+), 1 deletion(-) create mode 100644 src/Mutators/Removal/RemoveFunctionCall.php create mode 100644 src/Mutators/Removal/RemoveMethodCall.php create mode 100644 src/Mutators/Sets/RemovalSet.php create mode 100644 tests/Mutators/Removal/RemoveFunctionCallTest.php create mode 100644 tests/Mutators/Removal/RemoveMethodCallTest.php diff --git a/WIP.md b/WIP.md index 9f79bc3..059c894 100644 --- a/WIP.md +++ b/WIP.md @@ -27,7 +27,6 @@ - [ ] Awesome docs # Backlog Prio 1 -- [ ] Add return mutators - [ ] Add remove function call mutator - [ ] Add null safe mutators - [ ] Add array declaration mutators: https://stryker-mutator.io/docs/mutation-testing-elements/supported-mutators/#array-declaration diff --git a/src/Mutators.php b/src/Mutators.php index 1bff89a..d587560 100644 --- a/src/Mutators.php +++ b/src/Mutators.php @@ -126,6 +126,8 @@ use Pest\Mutate\Mutators\Number\DecrementInteger; use Pest\Mutate\Mutators\Number\IncrementFloat; use Pest\Mutate\Mutators\Number\IncrementInteger; +use Pest\Mutate\Mutators\Removal\RemoveFunctionCall; +use Pest\Mutate\Mutators\Removal\RemoveMethodCall; use Pest\Mutate\Mutators\Return\AlwaysReturnEmptyArray; use Pest\Mutate\Mutators\Return\AlwaysReturnNull; use Pest\Mutate\Mutators\Sets\ArithmeticSet; @@ -139,6 +141,7 @@ use Pest\Mutate\Mutators\Sets\LogicalSet; use Pest\Mutate\Mutators\Sets\MathSet; use Pest\Mutate\Mutators\Sets\NumberSet; +use Pest\Mutate\Mutators\Sets\RemovalSet; use Pest\Mutate\Mutators\Sets\ReturnSet; use Pest\Mutate\Mutators\Sets\StringSet; use Pest\Mutate\Mutators\Sets\VisibilitySet; @@ -204,6 +207,8 @@ class Mutators final public const SET_NUMBER = NumberSet::class; + final public const SET_REMOVAL = RemovalSet::class; + final public const SET_RETURN = ReturnSet::class; final public const SET_STRING = StringSet::class; @@ -471,6 +476,11 @@ class Mutators final public const NUMBER_INCREMENT_INTEGER = IncrementInteger::class; + /** Removal */ + final public const REMOVAL_REMOVE_FUNCTION_CALL = RemoveFunctionCall::class; + + final public const REMOVAL_REMOVE_METHOD_CALL = RemoveMethodCall::class; + /** Return */ final public const RETURN_ALWAYS_RETURN_EMPTY_ARRAY = AlwaysReturnEmptyArray::class; diff --git a/src/Mutators/Removal/RemoveFunctionCall.php b/src/Mutators/Removal/RemoveFunctionCall.php new file mode 100644 index 0000000..6d9f677 --- /dev/null +++ b/src/Mutators/Removal/RemoveFunctionCall.php @@ -0,0 +1,41 @@ +expr instanceof FuncCall; + } + + public static function mutate(Node $node): Node + { + return new Nop(); + } +} diff --git a/src/Mutators/Removal/RemoveMethodCall.php b/src/Mutators/Removal/RemoveMethodCall.php new file mode 100644 index 0000000..b67ebec --- /dev/null +++ b/src/Mutators/Removal/RemoveMethodCall.php @@ -0,0 +1,46 @@ +foo(); // [tl! remove] + DIFF; + + public static function nodesToHandle(): array + { + return [Expression::class]; + } + + public static function can(Node $node): bool + { + if (! $node instanceof Expression) { + return false; + } + + if ($node->expr instanceof MethodCall) { + return true; + } + + return $node->expr instanceof StaticCall; + } + + public static function mutate(Node $node): Node + { + return new Nop(); + } +} diff --git a/src/Mutators/Sets/DefaultSet.php b/src/Mutators/Sets/DefaultSet.php index 06a409f..81f083f 100644 --- a/src/Mutators/Sets/DefaultSet.php +++ b/src/Mutators/Sets/DefaultSet.php @@ -26,6 +26,7 @@ public static function mutators(): array ...LogicalSet::mutators(), ...MathSet::mutators(), ...NumberSet::mutators(), + ...RemovalSet::mutators(), ...ReturnSet::mutators(), ...StringSet::mutators(), ...VisibilitySet::defaultMutators(), diff --git a/src/Mutators/Sets/RemovalSet.php b/src/Mutators/Sets/RemovalSet.php new file mode 100644 index 0000000..9b36339 --- /dev/null +++ b/src/Mutators/Sets/RemovalSet.php @@ -0,0 +1,26 @@ +mutation->diff['modified'] as $line) { + $line = htmlentities($line); // TODO: this is not good, but currently required other printer breaks on `$this->foo()` because of the > $diff .= "
+ {$line}
"; } foreach ($test->mutation->diff['original'] as $line) { + $line = htmlentities($line); // TODO: this is not good, but currently required other printer breaks on `$this->foo()` because of the > $diff .= "
- {$line}
"; } diff --git a/tests/Mutators/Removal/RemoveFunctionCallTest.php b/tests/Mutators/Removal/RemoveFunctionCallTest.php new file mode 100644 index 0000000..25b20c8 --- /dev/null +++ b/tests/Mutators/Removal/RemoveFunctionCallTest.php @@ -0,0 +1,17 @@ +toBe(<<<'CODE' + bar(); + } + } + CODE))->toBe(<<<'CODE' + toBe(<<<'CODE' +