Skip to content

Commit

Permalink
[Type] add vec and dict type tests
Browse files Browse the repository at this point in the history
  • Loading branch information
azjezz committed Feb 17, 2021
1 parent fcd03fb commit f5a47ac
Show file tree
Hide file tree
Showing 4 changed files with 194 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/Psl/Type/Internal/DictType.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Psl\Type\Exception\CoercionException;

use function is_iterable;
use function is_array;

/**
* @template Tk of array-key
Expand Down Expand Up @@ -95,7 +96,7 @@ public function coerce($value): array
*/
public function assert($value): array
{
if (is_iterable($value)) {
if (is_array($value)) {
$key_trace = $this->getTrace()
->withFrame(Str\format('array<%s, _>', $this->key_type->toString()));
$value_trace = $this->getTrace()
Expand Down
102 changes: 102 additions & 0 deletions tests/Psl/Type/DictTypeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php

declare(strict_types=1);

namespace Psl\Tests\Type;

use Psl\Collection;
use Psl\Dict;
use Psl\Iter;
use Psl\Str;
use Psl\Type;
use Psl\Vec;

/**
* @extends TypeTest<array<array-key, mixed>>
*/
final class DictTypeTest extends TypeTest
{
public function getType(): Type\TypeInterface
{
return Type\dict(Type\int(), Type\int());
}

public function getValidCoercions(): iterable
{
yield [
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
];

yield [
['1', '2', '3', '4', '5', '6', '7', '8', '9', '10'],
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
];

yield [
new Collection\Vector([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]),
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
];

yield [
new Collection\Map([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]),
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
];

yield [
new Collection\Vector(['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']),
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
];

yield [
new Collection\Map(['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']),
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
];

yield [
Dict\map_keys(Vec\range(1, 10), static fn(int $key): string => (string)$key),
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
];

yield [
Dict\map(
Vec\range(1, 10),
static fn(int $value): string => Str\format('00%d', $value)
),
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
];

yield [
Dict\map_keys(
Dict\map(
Vec\range(1, 10),
static fn(int $value): string => Str\format('00%d', $value)
),
static fn(int $key): string => Str\format('00%d', $key)
),
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
];
}

public function getInvalidCoercions(): iterable
{
yield [1.0];
yield [1.23];
yield [Type\bool()];
yield [null];
yield [false];
yield [true];
yield [STDIN];
}

public function getToStringExamples(): iterable
{
yield [$this->getType(), 'array<int, int>'];
yield [Type\dict(Type\array_key(), Type\int()), 'array<array-key, int>'];
yield [Type\dict(Type\array_key(), Type\string()), 'array<array-key, string>'];
yield [
Type\dict(Type\array_key(), Type\object(Iter\Iterator::class)),
'array<array-key, Psl\Iter\Iterator>'
];
}
}
5 changes: 3 additions & 2 deletions tests/Psl/Type/TypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Psl\Type\Exception\AssertException;
use Psl\Type\Exception\CoercionException;
use Psl\Type\Type;
use Psl\Type\TypeInterface;
use Psl\Vec;

/**
Expand All @@ -17,9 +18,9 @@
abstract class TypeTest extends TestCase
{
/**
* @psalm-return Type<T>
* @psalm-return TypeInterface<T>
*/
abstract public function getType(): Type;
abstract public function getType(): TypeInterface;

/**
* @psalm-return iterable<array{0: mixed, 1: T}>
Expand Down
87 changes: 87 additions & 0 deletions tests/Psl/Type/VecTypeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

declare(strict_types=1);

namespace Psl\Tests\Type;

use Psl\Collection;
use Psl\Dict;
use Psl\Iter;
use Psl\Str;
use Psl\Type;
use Psl\Vec;

/**
* @extends TypeTest<list<mixed>>
*/
final class VecTypeTest extends TypeTest
{
public function getType(): Type\TypeInterface
{
return Type\vec(Type\int());
}

public function getValidCoercions(): iterable
{
yield [
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
];

yield [
['1', '2', '3', '4', '5', '6', '7', '8', '9', '10'],
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
];

yield [
new Collection\Vector([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]),
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
];

yield [
new Collection\Map([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]),
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
];

yield [
new Collection\Vector(['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']),
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
];

yield [
new Collection\Map(['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']),
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
];

yield [
Dict\map_keys(Vec\range(1, 10), static fn(int $key): string => (string)$key),
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
];

yield [
Dict\map(Vec\range(1, 10), static fn(int $value): string => Str\format('00%d', $value)),
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
];
}

public function getInvalidCoercions(): iterable
{
yield [1.0];
yield [1.23];
yield [Type\bool()];
yield [null];
yield [false];
yield [true];
yield [STDIN];
}

public function getToStringExamples(): iterable
{
yield [$this->getType(), 'list<int>'];
yield [Type\vec(Type\string()), 'list<string>'];
yield [
Type\vec(Type\object(Iter\Iterator::class)),
'list<Psl\Iter\Iterator>'
];
}
}

0 comments on commit f5a47ac

Please sign in to comment.