|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Psl\Tests\Unit\Type; |
| 6 | + |
| 7 | +use Psl\Collection; |
| 8 | +use Psl\Dict; |
| 9 | +use Psl\Iter; |
| 10 | +use Psl\Str; |
| 11 | +use Psl\Type; |
| 12 | +use Psl\Vec; |
| 13 | + |
| 14 | +/** |
| 15 | + * @extends TypeTest<non-empty-array<array-key, mixed>> |
| 16 | + */ |
| 17 | +final class NonEmptyDictTypeTest extends TypeTest |
| 18 | +{ |
| 19 | + public function getType(): Type\TypeInterface |
| 20 | + { |
| 21 | + return Type\non_empty_dict(Type\int(), Type\int()); |
| 22 | + } |
| 23 | + |
| 24 | + public function getValidCoercions(): iterable |
| 25 | + { |
| 26 | + yield [ |
| 27 | + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], |
| 28 | + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] |
| 29 | + ]; |
| 30 | + |
| 31 | + yield [ |
| 32 | + ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10'], |
| 33 | + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] |
| 34 | + ]; |
| 35 | + |
| 36 | + yield [ |
| 37 | + new Collection\Vector([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]), |
| 38 | + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], |
| 39 | + ]; |
| 40 | + |
| 41 | + yield [ |
| 42 | + new Collection\Map([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]), |
| 43 | + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], |
| 44 | + ]; |
| 45 | + |
| 46 | + yield [ |
| 47 | + new Collection\Vector(['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']), |
| 48 | + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], |
| 49 | + ]; |
| 50 | + |
| 51 | + yield [ |
| 52 | + new Collection\Map(['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']), |
| 53 | + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], |
| 54 | + ]; |
| 55 | + |
| 56 | + yield [ |
| 57 | + Dict\map_keys(Vec\range(1, 10), static fn(int $key): string => (string)$key), |
| 58 | + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] |
| 59 | + ]; |
| 60 | + |
| 61 | + yield [ |
| 62 | + Dict\map( |
| 63 | + Vec\range(1, 10), |
| 64 | + static fn(int $value): string => Str\format('00%d', $value) |
| 65 | + ), |
| 66 | + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] |
| 67 | + ]; |
| 68 | + |
| 69 | + yield [ |
| 70 | + Dict\map_keys( |
| 71 | + Dict\map( |
| 72 | + Vec\range(1, 10), |
| 73 | + static fn(int $value): string => Str\format('00%d', $value) |
| 74 | + ), |
| 75 | + static fn(int $key): string => Str\format('00%d', $key) |
| 76 | + ), |
| 77 | + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] |
| 78 | + ]; |
| 79 | + } |
| 80 | + |
| 81 | + public function getInvalidCoercions(): iterable |
| 82 | + { |
| 83 | + yield [[]]; |
| 84 | + yield [1.0]; |
| 85 | + yield [1.23]; |
| 86 | + yield [Type\bool()]; |
| 87 | + yield [null]; |
| 88 | + yield [false]; |
| 89 | + yield [true]; |
| 90 | + yield [STDIN]; |
| 91 | + } |
| 92 | + |
| 93 | + public function getToStringExamples(): iterable |
| 94 | + { |
| 95 | + yield [$this->getType(), 'non-empty-dict<int, int>']; |
| 96 | + yield [Type\non_empty_dict(Type\array_key(), Type\int()), 'non-empty-dict<array-key, int>']; |
| 97 | + yield [Type\non_empty_dict(Type\array_key(), Type\string()), 'non-empty-dict<array-key, string>']; |
| 98 | + yield [ |
| 99 | + Type\non_empty_dict(Type\array_key(), Type\object(Iter\Iterator::class)), |
| 100 | + 'non-empty-dict<array-key, Psl\Iter\Iterator>' |
| 101 | + ]; |
| 102 | + } |
| 103 | +} |
0 commit comments