Skip to content

Commit

Permalink
fixing parenthesing logic - unary operators sebastianbergmann#1
Browse files Browse the repository at this point in the history
  • Loading branch information
ptomulik committed Jun 4, 2020
1 parent 2516d89 commit 0ae6990
Show file tree
Hide file tree
Showing 2 changed files with 162 additions and 24 deletions.
8 changes: 8 additions & 0 deletions src/Framework/Constraint/Unary.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,12 @@ final protected function constraint(): Constraint
{
return $this->constraint;
}

/**
* Returns true if the $constraint needs to be wrapped with braces.
*/
protected function constraintNeedsParentheses(Constraint $constraint): bool
{
return $constraint instanceof self || parent::constraintNeedsParentheses($constraint);
}
}
178 changes: 154 additions & 24 deletions tests/unit/Framework/Constraint/LogicalNotTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,10 @@ public function testLogicalNotOfLogicalAndFailureDescription(): void

$expected = 'not( \'apple\' ' . \implode(' and ', $names) . ' )';

$reflectionClass = new \ReflectionClass(LogicalNot::class);
$reflectionMethod = $reflectionClass->getMethod('failureDescription');
$reflectionMethod->setAccessible(true);
$method = new \ReflectionMethod(LogicalNot::class, 'failureDescription');
$method->setAccessible(true);

$this->assertSame($expected, $reflectionMethod->invokeArgs($constraint, ['apple']));
$this->assertSame($expected, $method->invokeArgs($constraint, ['apple']));
}

public function testLogicalNotOfLogicalAndFailureDescription2(): void
Expand All @@ -191,11 +190,10 @@ public function testLogicalNotOfLogicalAndFailureDescription2(): void

$expected = '\'apple\' is not healthy';

$reflectionClass = new \ReflectionClass(LogicalNot::class);
$reflectionMethod = $reflectionClass->getMethod('failureDescription');
$reflectionMethod->setAccessible(true);
$method = new \ReflectionMethod(LogicalNot::class, 'failureDescription');
$method->setAccessible(true);

$this->assertSame($expected, $reflectionMethod->invokeArgs($constraint, ['apple']));
$this->assertSame($expected, $method->invokeArgs($constraint, ['apple']));
}

public function testLogicalNotOfLogicalOrFailureDescription(): void
Expand All @@ -214,11 +212,10 @@ public function testLogicalNotOfLogicalOrFailureDescription(): void

$expected = 'not( \'apple\' ' . \implode(' or ', $names) . ' )';

$reflectionClass = new \ReflectionClass(LogicalNot::class);
$reflectionMethod = $reflectionClass->getMethod('failureDescription');
$reflectionMethod->setAccessible(true);
$method = new \ReflectionMethod(LogicalNot::class, 'failureDescription');
$method->setAccessible(true);

$this->assertSame($expected, $reflectionMethod->invokeArgs($constraint, ['apple']));
$this->assertSame($expected, $method->invokeArgs($constraint, ['apple']));
}

public function testLogicalNotOfLogicalOrFailureDescription2(): void
Expand All @@ -231,11 +228,10 @@ public function testLogicalNotOfLogicalOrFailureDescription2(): void

$expected = '\'apple\' is not healthy';

$reflectionClass = new \ReflectionClass(LogicalNot::class);
$reflectionMethod = $reflectionClass->getMethod('failureDescription');
$reflectionMethod->setAccessible(true);
$method = new \ReflectionMethod(LogicalNot::class, 'failureDescription');
$method->setAccessible(true);

$this->assertSame($expected, $reflectionMethod->invokeArgs($constraint, ['apple']));
$this->assertSame($expected, $method->invokeArgs($constraint, ['apple']));
}

public function testLogicalNotOfLogicalXorFailureDescription(): void
Expand All @@ -254,11 +250,10 @@ public function testLogicalNotOfLogicalXorFailureDescription(): void

$expected = 'not( \'apple\' ' . \implode(' xor ', $names) . ' )';

$reflectionClass = new \ReflectionClass(LogicalNot::class);
$reflectionMethod = $reflectionClass->getMethod('failureDescription');
$reflectionMethod->setAccessible(true);
$method = new \ReflectionMethod(LogicalNot::class, 'failureDescription');
$method->setAccessible(true);

$this->assertSame($expected, $reflectionMethod->invokeArgs($constraint, ['apple']));
$this->assertSame($expected, $method->invokeArgs($constraint, ['apple']));
}

public function testLogicalNotOfLogicalXorFailureDescription2(): void
Expand All @@ -271,10 +266,145 @@ public function testLogicalNotOfLogicalXorFailureDescription2(): void

$expected = '\'apple\' is not healthy';

$reflectionClass = new \ReflectionClass(LogicalNot::class);
$reflectionMethod = $reflectionClass->getMethod('failureDescription');
$reflectionMethod->setAccessible(true);
$method = new \ReflectionMethod(LogicalNot::class, 'failureDescription');
$method->setAccessible(true);

$this->assertSame($expected, $reflectionMethod->invokeArgs($constraint, ['apple']));
$this->assertSame($expected, $method->invokeArgs($constraint, ['apple']));
}

public function testNestedLogicalNotOfIsEqualToString(): void
{
$constraint = new LogicalNot(
new LogicalNot(
new IsEqual(5)
)
);

$expected = 'not( is not equal to 5 )';

$this->assertSame($expected, $constraint->toString());
}

public function testNestedLogicalNotOfIsEqualFailureDescription(): void
{
$constraint = new LogicalNot(
new LogicalNot(
new IsEqual(5)
)
);

$method = new \ReflectionMethod(LogicalNot::class, 'failureDescription');
$method->setAccessible(true);

$expected = 'not( 3 is not equal to 5 )';

$this->assertSame($expected, $method->invokeArgs($constraint, [3]));
}

public function testNestedLogicalNotOfLogicalOrWithSingleOperandToString(): void
{
$constraint = new LogicalNot(
new LogicalNot(
LogicalOr::fromConstraints(
new IsEqual(5)
)
)
);

$expected = 'not( is not equal to 5 )';

$this->assertSame($expected, $constraint->toString());
}

public function testNestedLogicalNotOfLogicalOrWithSingleOperandFailureDescription(): void
{
$constraint = new LogicalNot(
new LogicalNot(
LogicalOr::fromConstraints(
new IsEqual(5)
)
)
);

$method = new \ReflectionMethod(LogicalNot::class, 'failureDescription');
$method->setAccessible(true);

$expected = 'not( 3 is not equal to 5 )';

$this->assertSame($expected, $method->invokeArgs($constraint, [3]));
}

public function testNestedLogicalNotOfNestedLogicalOrWithSingleOperandToString(): void
{
$constraint = new LogicalNot(
new LogicalNot(
LogicalOr::fromConstraints(
LogicalOr::fromConstraints(
new IsEqual(5)
)
)
)
);

$expected = 'not( is not equal to 5 )';

$this->assertSame($expected, $constraint->toString());
}

public function testNestedLogicalNotOfNestedLogicalOrWithSingleOperandFailureDescription(): void
{
$constraint = new LogicalNot(
new LogicalNot(
LogicalOr::fromConstraints(
LogicalOr::fromConstraints(
new IsEqual(5)
)
)
)
);

$method = new \ReflectionMethod(LogicalNot::class, 'failureDescription');
$method->setAccessible(true);

$expected = 'not( 3 is not equal to 5 )';

$this->assertSame($expected, $method->invokeArgs($constraint, [3]));
}

public function testNestedLogicalNotOfLogicalOrWithMultipleOperandsToString(): void
{
$constraint = new LogicalNot(
new LogicalNot(
LogicalOr::fromConstraints(
new LessThan(5),
new IsEqual(10),
new GreaterThan(15)
)
)
);

$expected = 'not( not( is less than 5 or is equal to 10 or is greater than 15 ) )';

$this->assertSame($expected, $constraint->toString());
}

public function testNestedLogicalNotOfLogicalOrWithMultipleOperandsFailureDescription(): void
{
$constraint = new LogicalNot(
new LogicalNot(
LogicalOr::fromConstraints(
new LessThan(5),
new IsEqual(10),
new GreaterThan(15)
)
)
);

$method = new \ReflectionMethod(LogicalNot::class, 'failureDescription');
$method->setAccessible(true);

$expected = 'not( not( 7 is less than 5 or is equal to 10 or is greater than 15 ) )';

$this->assertSame($expected, $method->invokeArgs($constraint, [7]));
}
}

0 comments on commit 0ae6990

Please sign in to comment.