diff --git a/src/Type/Types/IntegerValueType.php b/src/Type/Types/IntegerValueType.php index bf0d656c..c511bd1a 100644 --- a/src/Type/Types/IntegerValueType.php +++ b/src/Type/Types/IntegerValueType.php @@ -42,8 +42,19 @@ public function matches(Type $other): bool 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 diff --git a/tests/Unit/Type/Types/IntegerValueTypeTest.php b/tests/Unit/Type/Types/IntegerValueTypeTest.php index 76cb547b..1ecc6c0c 100644 --- a/tests/Unit/Type/Types/IntegerValueTypeTest.php +++ b/tests/Unit/Type/Types/IntegerValueTypeTest.php @@ -9,6 +9,9 @@ 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 @@ public function test_does_not_match_same_type_with_different_value(): void 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 @@ public function test_matches_mixed_type(): void 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());