Skip to content

Commit f36051d

Browse files
committed
add tests
1 parent 657c08a commit f36051d

File tree

4 files changed

+105
-25
lines changed

4 files changed

+105
-25
lines changed

bin/generate.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,10 @@ private function returnType(Type $type): string
235235
'prolog' => Expect::string()->default(null),
236236
'epilog' => Expect::string()->default(null),
237237
])),
238-
'generate' => Expect::arrayOf(Expect::string()),
238+
'generate' => Expect::structure([
239+
'builtIn' => Expect::arrayOf(Expect::string()),
240+
'special' => Expect::arrayOf(Expect::string()),
241+
]),
239242
]), Neon::decode(FileSystem::read(__DIR__ . '/methods.neon')));
240243

241244
$generator = new TypeAssertionGenerator(
@@ -246,5 +249,6 @@ private function returnType(Type $type): string
246249
$data->types,
247250
);
248251

249-
FileSystem::write(__DIR__ . '/../src/Mixins/TypeAssertTrait.php', $generator->run($data->generate));
250-
FileSystem::write(__DIR__ . '/../src/Mixins/ArrayTypeAssertTrait.php', $generator->runArray($data->generate));
252+
$generate = array_merge($data->generate->builtIn, $data->generate->special);
253+
FileSystem::write(__DIR__ . '/../src/Mixins/TypeAssertTrait.php', $generator->run($generate));
254+
FileSystem::write(__DIR__ . '/../src/Mixins/ArrayTypeAssertTrait.php', $generator->runArray($generate));

bin/methods.neon

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ types:
1414
scalar:
1515
returns: [int, float, string, bool]
1616
assertions: '!is_scalar($value)'
17+
callable:
18+
assertions: '!is_callable($value)'
1719
null:
1820
assertions: '$value !== null'
1921

@@ -43,25 +45,28 @@ types:
4345
"""
4446

4547
generate:
46-
- array
47-
- array|null
48-
- object
49-
- object|null
50-
- string
51-
- string|null
52-
- int
53-
- int|null
54-
- float
55-
- float|null
56-
- int|float
57-
- int|float|null
58-
- bool
59-
- bool|null
60-
- scalar
61-
- scalar|null
62-
## special
63-
- numeric
64-
- numericInt
65-
- numericInt|null
66-
- numericFloat
67-
- numericFloat|null
48+
builtIn:
49+
- array
50+
- array|null
51+
- object
52+
- object|null
53+
- string
54+
- string|null
55+
- int
56+
- int|null
57+
- float
58+
- float|null
59+
- int|float
60+
- int|float|null
61+
- bool
62+
- bool|null
63+
- callable
64+
- callable|null
65+
special:
66+
- scalar
67+
- scalar|null
68+
- numeric
69+
- numericInt
70+
- numericInt|null
71+
- numericFloat
72+
- numericFloat|null

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,8 @@
99
"php": ">= 8.0",
1010
"nette/tester": "^2.4",
1111
"nette/utils": "^3.2"
12+
},
13+
"require-dev": {
14+
"nette/neon": "^3.3"
1215
}
1316
}

tests/TypeAssert.test.phpt

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php declare(strict_types = 1);
2+
3+
use Nette\Neon\Neon;
4+
use Nette\Utils\Arrays;
5+
use Nette\Utils\FileSystem;
6+
use Nette\Utils\Type;
7+
use Tester\Assert;
8+
use Utilitte\Asserts\Exceptions\AssertionFailedException;
9+
use Utilitte\Asserts\TypeAssert;
10+
11+
require __DIR__ . '/_bootstrap.php';
12+
13+
function hasType(array $array, string $type): bool
14+
{
15+
return Arrays::some($array, fn (string $name) => $name === $type);
16+
}
17+
18+
function type(mixed $value, bool $callable) {
19+
static $converts = [
20+
'boolean' => 'bool',
21+
'integer' => 'int',
22+
'double' => 'float',
23+
'string' => 'string',
24+
'object' => 'object',
25+
'array' => 'array',
26+
'NULL' => 'null',
27+
'resource' => 'resource',
28+
'resource (closed)' => 'resource',
29+
];
30+
31+
if ($callable && is_callable($value)) {
32+
return 'callable';
33+
}
34+
35+
return $converts[gettype($value)];
36+
}
37+
38+
function testVariants(callable $callback, string $type) {
39+
$values = [
40+
'string',
41+
1,
42+
1.2,
43+
true,
44+
null,
45+
[],
46+
function (): void { },
47+
new stdClass(),
48+
];
49+
50+
$type = Type::fromString($type);
51+
52+
foreach ($values as $value) {
53+
$valueType = type($value, hasType($type->getNames(), 'callable'));
54+
if (!$type->allows($valueType)) {
55+
Assert::exception(fn () => $callback($value), AssertionFailedException::class);
56+
} else {
57+
Assert::same($value, $callback($value));
58+
}
59+
}
60+
}
61+
62+
$data = Neon::decode(FileSystem::read(__DIR__ . '/../bin/methods.neon'));
63+
64+
foreach ($data['generate']['builtIn'] as $type) {
65+
$methodName = lcfirst(implode('Or', array_map('ucfirst', explode('|', $type))));
66+
67+
testVariants([TypeAssert::class, $methodName], $type);
68+
}

0 commit comments

Comments
 (0)