mirror of
https://github.com/danog/Valinor.git
synced 2024-11-26 20:24:40 +01:00
fix: handle integer value match properly
This commit is contained in:
parent
fa3ce50dfb
commit
9ee2cc471e
@ -42,8 +42,19 @@ final class IntegerValueType implements IntegerType, FixedType
|
||||
return $other->isMatchedBy($this);
|
||||
}
|
||||
|
||||
return $other instanceof IntegerType
|
||||
|| $other instanceof MixedType;
|
||||
if ($other instanceof NativeIntegerType || $other instanceof MixedType) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if ($other instanceof NegativeIntegerType && $this->value < 0) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if ($other instanceof PositiveIntegerType && $this->value > 0) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function canCast($value): bool
|
||||
|
@ -9,6 +9,9 @@ use CuyZ\Valinor\Type\Types\Exception\InvalidIntegerValue;
|
||||
use CuyZ\Valinor\Type\Types\Exception\InvalidIntegerValueType;
|
||||
use CuyZ\Valinor\Type\Types\IntegerValueType;
|
||||
use CuyZ\Valinor\Type\Types\MixedType;
|
||||
use CuyZ\Valinor\Type\Types\NativeIntegerType;
|
||||
use CuyZ\Valinor\Type\Types\NegativeIntegerType;
|
||||
use CuyZ\Valinor\Type\Types\PositiveIntegerType;
|
||||
use CuyZ\Valinor\Type\Types\UnionType;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use stdClass;
|
||||
@ -119,6 +122,26 @@ final class IntegerValueTypeTest extends TestCase
|
||||
self::assertFalse((new IntegerValueType(1337))->matches(new IntegerValueType(42)));
|
||||
}
|
||||
|
||||
public function test_matches_positive_integer_when_value_is_positive(): void
|
||||
{
|
||||
self::assertTrue((new IntegerValueType(1337))->matches(new PositiveIntegerType()));
|
||||
}
|
||||
|
||||
public function test_does_not_match_positive_integer_when_value_is_negative(): void
|
||||
{
|
||||
self::assertFalse((new IntegerValueType(-1337))->matches(new PositiveIntegerType()));
|
||||
}
|
||||
|
||||
public function test_matches_negative_integer_when_value_is_negative(): void
|
||||
{
|
||||
self::assertTrue((new IntegerValueType(-1337))->matches(new NegativeIntegerType()));
|
||||
}
|
||||
|
||||
public function test_does_not_match_negative_integer_when_value_is_positive(): void
|
||||
{
|
||||
self::assertFalse((new IntegerValueType(1337))->matches(new NegativeIntegerType()));
|
||||
}
|
||||
|
||||
public function test_does_not_match_other_type(): void
|
||||
{
|
||||
self::assertFalse($this->type->matches(new FakeType()));
|
||||
@ -129,6 +152,11 @@ final class IntegerValueTypeTest extends TestCase
|
||||
self::assertTrue($this->type->matches(new MixedType()));
|
||||
}
|
||||
|
||||
public function test_matches_native_integer_type(): void
|
||||
{
|
||||
self::assertTrue($this->type->matches(new NativeIntegerType()));
|
||||
}
|
||||
|
||||
public function test_matches_union_type_containing_integer_type(): void
|
||||
{
|
||||
$union = new UnionType(new FakeType(), new IntegerValueType(1337), new FakeType());
|
||||
|
Loading…
Reference in New Issue
Block a user