Skip to content

Commit

Permalink
fix: handle integer value match properly
Browse files Browse the repository at this point in the history
  • Loading branch information
romm committed Dec 7, 2021
1 parent fa3ce50 commit 9ee2cc4
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/Type/Types/IntegerValueType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 28 additions & 0 deletions tests/Unit/Type/Types/IntegerValueTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()));
Expand All @@ -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());
Expand Down

0 comments on commit 9ee2cc4

Please sign in to comment.