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

Enhancement: Implement integer increment and decrement mutators #152

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Expand Up @@ -24,7 +24,7 @@ matrix:

env:
global:
- INFECTION_FLAGS='--threads=4 --min-msi=59 --min-covered-msi=86'
- INFECTION_FLAGS='--threads=4 --min-msi=57 --min-covered-msi=84'

cache:
directories:
Expand Down
4 changes: 4 additions & 0 deletions src/Config/InfectionConfig.php
Expand Up @@ -50,6 +50,8 @@
use Infection\Mutator\ConditionalNegotiation\NotIdentical;
use Infection\Mutator\FunctionSignature\ProtectedVisibility;
use Infection\Mutator\FunctionSignature\PublicVisibility;
use Infection\Mutator\Number\DecrementInteger;
use Infection\Mutator\Number\IncrementInteger;
use Infection\Mutator\Number\OneZeroFloat;
use Infection\Mutator\Number\OneZeroInteger;
use Infection\Mutator\Operator\Break_;
Expand Down Expand Up @@ -118,6 +120,8 @@ class InfectionConfig
NotIdentical::class,

// Number
DecrementInteger::class,
IncrementInteger::class,
OneZeroInteger::class,
OneZeroFloat::class,

Expand Down
32 changes: 32 additions & 0 deletions src/Mutator/Number/DecrementInteger.php
@@ -0,0 +1,32 @@
<?php
/**
* Copyright Β© 2017-2018 Maks Rafalko
*
* License: https://opensource.org/licenses/BSD-3-Clause New BSD License
*/
declare(strict_types=1);

namespace Infection\Mutator\Number;

use Infection\Mutator\Mutator;
use PhpParser\Node;

class DecrementInteger extends Mutator
{
/**
* Decrements an integer by 1.
*
* @param Node $node
*
* @return Node\Scalar\LNumber
*/
public function mutate(Node $node)
{
return new Node\Scalar\LNumber($node->value - 1);
}

public function shouldMutate(Node $node): bool
{
return $node instanceof Node\Scalar\LNumber && $node->value !== 1;
}
}
32 changes: 32 additions & 0 deletions src/Mutator/Number/IncrementInteger.php
@@ -0,0 +1,32 @@
<?php
/**
* Copyright Β© 2017-2018 Maks Rafalko
*
* License: https://opensource.org/licenses/BSD-3-Clause New BSD License
*/
declare(strict_types=1);

namespace Infection\Mutator\Number;

use Infection\Mutator\Mutator;
use PhpParser\Node;

class IncrementInteger extends Mutator
{
/**
* Increments an integer by 1.
*
* @param Node $node
*
* @return Node\Scalar\LNumber
*/
public function mutate(Node $node)
{
return new Node\Scalar\LNumber($node->value + 1);
}

public function shouldMutate(Node $node): bool
{
return $node instanceof Node\Scalar\LNumber && $node->value !== 0;
}
}
73 changes: 73 additions & 0 deletions tests/Mutator/Number/DecrementIntegerTest.php
@@ -0,0 +1,73 @@
<?php
/**
* Copyright Β© 2017-2018 Maks Rafalko
*
* License: https://opensource.org/licenses/BSD-3-Clause New BSD License
*/
declare(strict_types=1);

namespace Infection\Tests\Mutator\Number;

use Infection\Mutator\Mutator;
use Infection\Mutator\Number\DecrementInteger;
use Infection\Mutator\Number\OneZeroInteger;
use Infection\Tests\Mutator\AbstractMutator;

class DecrementIntegerTest extends AbstractMutator
{
public function test_it_decrements_an_integer()
{
$code = <<<'PHP'
<?php

if ($foo < 10) {
echo 'bar';
}
PHP;

$mutatedCode = $this->mutate($code);

$expectedMutatedCode = <<<'PHP'
<?php

if ($foo < 9) {
echo 'bar';
}
PHP;

$this->assertSame($expectedMutatedCode, $mutatedCode);
}

/**
* Mutator should skip 1 to reduce the number of mutations.
*
* @see OneZeroInteger::mutate()
*/
public function test_it_does_not_decrement_one()
{
$code = <<<'PHP'
<?php

if ($foo < 1) {
echo 'bar';
}
PHP;

$mutatedCode = $this->mutate($code);

$expectedMutatedCode = <<<'PHP'
<?php

if ($foo < 1) {
echo 'bar';
}
PHP;

$this->assertSame($expectedMutatedCode, $mutatedCode);
}

protected function getMutator(): Mutator
{
return new DecrementInteger();
}
}
73 changes: 73 additions & 0 deletions tests/Mutator/Number/IncrementIntegerTest.php
@@ -0,0 +1,73 @@
<?php
/**
* Copyright Β© 2017-2018 Maks Rafalko
*
* License: https://opensource.org/licenses/BSD-3-Clause New BSD License
*/
declare(strict_types=1);

namespace Infection\Tests\Mutator\Number;

use Infection\Mutator\Mutator;
use Infection\Mutator\Number\IncrementInteger;
use Infection\Mutator\Number\OneZeroInteger;
use Infection\Tests\Mutator\AbstractMutator;

class IncrementIntegerTest extends AbstractMutator
{
public function test_it_increments_an_integer()
{
$code = <<<'PHP'
<?php

if ($foo < 10) {
echo 'bar';
}
PHP;

$mutatedCode = $this->mutate($code);

$expectedMutatedCode = <<<'PHP'
<?php

if ($foo < 11) {
echo 'bar';
}
PHP;

$this->assertSame($expectedMutatedCode, $mutatedCode);
}

/**
* Mutator should skip 0 to reduce the number of mutations.
*
* @see OneZeroInteger::mutate()
*/
public function test_it_does_not_increment_zero()
{
$code = <<<'PHP'
<?php

if ($foo < 0) {
echo 'bar';
}
PHP;

$mutatedCode = $this->mutate($code);

$expectedMutatedCode = <<<'PHP'
<?php

if ($foo < 0) {
echo 'bar';
}
PHP;

$this->assertSame($expectedMutatedCode, $mutatedCode);
}

protected function getMutator(): Mutator
{
return new IncrementInteger();
}
}