add test cases for constant predicate
This commit is contained in:
parent
804dd0e0e4
commit
1a0c6a5bc8
@ -20,6 +20,11 @@ abstract class AbstractPredicate implements Predicate
|
||||
return new NegatedPredicate($this);
|
||||
}
|
||||
|
||||
public function negate(): Predicate
|
||||
{
|
||||
return $this->not();
|
||||
}
|
||||
|
||||
public function and(...$predicate): Predicate
|
||||
{
|
||||
array_unshift($predicate, $this);
|
||||
|
@ -30,13 +30,15 @@ class ConstantPredicate extends AbstractPredicate
|
||||
|
||||
public function not(): Predicate
|
||||
{
|
||||
return new self(!$this->value);
|
||||
return $this->value
|
||||
? self::never()
|
||||
: self::always();
|
||||
}
|
||||
|
||||
public function and (...$predicate): Predicate
|
||||
{
|
||||
if (!$this->value) {
|
||||
return new self(false);
|
||||
return self::never();
|
||||
}
|
||||
|
||||
return all($predicate);
|
||||
@ -45,7 +47,7 @@ class ConstantPredicate extends AbstractPredicate
|
||||
public function or (...$predicate): Predicate
|
||||
{
|
||||
if ($this->value) {
|
||||
return new self(true);
|
||||
return self::always();
|
||||
}
|
||||
|
||||
return any($predicate);
|
||||
@ -58,11 +60,13 @@ class ConstantPredicate extends AbstractPredicate
|
||||
|
||||
public static function always()
|
||||
{
|
||||
return new static(true);
|
||||
static $always;
|
||||
return $always ? $always : $always = new static(true);
|
||||
}
|
||||
|
||||
public static function never()
|
||||
{
|
||||
return new static(false);
|
||||
static $never;
|
||||
return $never ? $never : $never = new static(false);
|
||||
}
|
||||
}
|
123
tests/ConstantPredicateTest.php
Normal file
123
tests/ConstantPredicateTest.php
Normal file
@ -0,0 +1,123 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright 2018 Kacper Donat
|
||||
*
|
||||
* @author Kacper "Kadet" Donat <kacper@kadet.net>
|
||||
*
|
||||
* Full license available in separate LICENSE file
|
||||
*/
|
||||
|
||||
use function Kadet\Functional\predicate;
|
||||
use Kadet\Functional\Predicate\ConstantPredicate;
|
||||
|
||||
class ConstantPredicateTest extends PHPUnit\Framework\TestCase
|
||||
{
|
||||
private function getTestPredicate()
|
||||
{
|
||||
return function ($arg = null) {
|
||||
return is_bool($arg);
|
||||
};
|
||||
}
|
||||
|
||||
public function randomDataProvider()
|
||||
{
|
||||
return [
|
||||
['x'],
|
||||
[false],
|
||||
[true],
|
||||
[],
|
||||
[(object)[]],
|
||||
[false, true],
|
||||
[null, true],
|
||||
[2321321]
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider randomDataProvider
|
||||
*/
|
||||
public function testNeverAndPredicate(...$args)
|
||||
{
|
||||
$predicate = ConstantPredicate::never()->and($this->getTestPredicate());
|
||||
|
||||
$this->assertFalse($predicate(...$args));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider randomDataProvider
|
||||
*/
|
||||
public function testNeverOrPredicate(...$args)
|
||||
{
|
||||
$predicate = predicate($this->getTestPredicate());
|
||||
$tested = ConstantPredicate::never()->or($predicate);
|
||||
|
||||
$this->assertSame($predicate(...$args), $tested(...$args));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider randomDataProvider
|
||||
*/
|
||||
public function testAlwaysOrPredicate(...$args)
|
||||
{
|
||||
$predicate = ConstantPredicate::always()->or($this->getTestPredicate());
|
||||
|
||||
$this->assertTrue($predicate(...$args));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider randomDataProvider
|
||||
*/
|
||||
public function testAlwaysAndPredicate(...$args)
|
||||
{
|
||||
$predicate = predicate($this->getTestPredicate());
|
||||
$tested = ConstantPredicate::always()->and($predicate);
|
||||
|
||||
$this->assertSame($predicate(...$args), $tested(...$args));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider randomDataProvider
|
||||
*/
|
||||
public function testNever(...$args)
|
||||
{
|
||||
$tested = ConstantPredicate::never();
|
||||
|
||||
$this->assertFalse($tested(...$args));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider randomDataProvider
|
||||
*/
|
||||
public function testNotAlways(...$args)
|
||||
{
|
||||
$tested = ConstantPredicate::always()->not();
|
||||
|
||||
$this->assertFalse($tested(...$args));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider randomDataProvider
|
||||
*/
|
||||
public function testNotNever(...$args)
|
||||
{
|
||||
$tested = ConstantPredicate::never()->not();
|
||||
|
||||
$this->assertTrue($tested(...$args));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider randomDataProvider
|
||||
*/
|
||||
public function testAlways(...$args)
|
||||
{
|
||||
$tested = ConstantPredicate::always();
|
||||
|
||||
$this->assertTrue($tested(...$args));
|
||||
}
|
||||
|
||||
public function testAliases()
|
||||
{
|
||||
$this->assertSame(\Kadet\Functional\always(), ConstantPredicate::always());
|
||||
$this->assertSame(\Kadet\Functional\never(), ConstantPredicate::never());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user