Skip to content

Commit 3513e43

Browse files
committed
Add float tests
1 parent 755a829 commit 3513e43

File tree

4 files changed

+146
-57
lines changed

4 files changed

+146
-57
lines changed

tests/Type/FloatTypeTest.php

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TypeLang\Mapper\Tests\Type;
6+
7+
use PHPUnit\Framework\Attributes\CoversClass;
8+
use PHPUnit\Framework\Attributes\Group;
9+
use TypeLang\Mapper\Tests\Type\Stub\IntBackedEnumStub;
10+
use TypeLang\Mapper\Type\Coercer\FloatTypeCoercer;
11+
use TypeLang\Mapper\Type\FloatType;
12+
use TypeLang\Mapper\Type\TypeInterface;
13+
14+
#[Group('types')]
15+
#[CoversClass(FloatType::class)]
16+
#[CoversClass(FloatTypeCoercer::class)]
17+
final class FloatTypeTest extends SymmetricTypeTestCase
18+
{
19+
protected static function createType(): TypeInterface
20+
{
21+
return new FloatType();
22+
}
23+
24+
protected static function matchValues(bool $strict): iterable
25+
{
26+
foreach (self::defaultMatchDataProviderSamples() as $value => $default) {
27+
yield $value => match (true) {
28+
$value === \INF,
29+
$value === -\INF,
30+
\is_float($value) && \is_nan($value),
31+
$value === \PHP_INT_MAX + 1,
32+
$value === 42.5,
33+
$value === 42.0,
34+
$value === 1.0,
35+
$value === 0.0,
36+
$value === -1.0,
37+
$value === -42.0,
38+
$value === -42.5,
39+
$value === \PHP_INT_MIN - 1 => true,
40+
default => $default,
41+
};
42+
}
43+
}
44+
45+
protected static function castValues(bool $strict): iterable
46+
{
47+
foreach (self::defaultCastDataProviderSamples() as $value => $default) {
48+
yield $value => match (true) {
49+
$value === \INF => \INF,
50+
$value === -\INF => -\INF,
51+
\is_float($value) && \is_nan($value) => \NAN,
52+
$value === \PHP_INT_MAX + 1 => \PHP_INT_MAX + 1,
53+
$value === 42.5 => 42.5,
54+
$value === 42.0 => 42.0,
55+
$value === 1.0 => 1.0,
56+
$value === 0.0 => 0.0,
57+
$value === -1.0 => -1.0,
58+
$value === -42.0 => -42.0,
59+
$value === -42.5 => -42.5,
60+
$value === \PHP_INT_MIN - 1 => \PHP_INT_MIN - 1,
61+
// Type casts
62+
$strict === false => match (true) {
63+
$value === \PHP_INT_MAX => (float) \PHP_INT_MAX,
64+
$value === 42 => 42.0,
65+
$value === 1 => 1.0,
66+
$value === 0 => 0.0,
67+
$value === -1 => -1.0,
68+
$value === -42 => -42.0,
69+
$value === \PHP_INT_MIN => (float) \PHP_INT_MIN,
70+
$value === "9223372036854775808" => 9223372036854775808.0,
71+
$value === "9223372036854775807" => 9223372036854775807.0,
72+
$value === "42" => 42.0,
73+
$value === "1" => 1.0,
74+
$value === "0" => 0.0,
75+
$value === "-1" => -1.0,
76+
$value === "-42" => -42.0,
77+
$value === "-9223372036854775808" => -9223372036854775808.0,
78+
$value === "-9223372036854775809" => -9223372036854775809.0,
79+
$value === "9223372036854775808.0" => 9223372036854775808.0,
80+
$value === "9223372036854775807.0" => 9223372036854775807.0,
81+
$value === "42.5" => 42.5,
82+
$value === "42.0" => 42.0,
83+
$value === "1.0" => 1.0,
84+
$value === "0.0" => 0.0,
85+
$value === "-1.0" => -1.0,
86+
$value === "-42.0" => -42.0,
87+
$value === "-42.5" => -42.5,
88+
$value === "-9223372036854775808.0" => -9223372036854775808.0,
89+
$value === "-9223372036854775809.0" => -9223372036854775809.0,
90+
$value === 42.0 => 42.0,
91+
$value === 1.0 => 1.0,
92+
$value === 0.0 => 0.0,
93+
$value === -1.0 => -1.0,
94+
$value === -42.0 => -42.0,
95+
$value === null => 0.0,
96+
$value === true => 1.0,
97+
$value === false => 0.0,
98+
$value === IntBackedEnumStub::ExampleCase => (float) IntBackedEnumStub::ExampleCase->value,
99+
default => $default,
100+
},
101+
default => $default,
102+
};
103+
}
104+
}
105+
}

tests/Type/IntTypeTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ protected static function castValues(bool $strict): iterable
6868
$value === null => 0,
6969
$value === true => 1,
7070
$value === false => 0,
71-
\is_resource($value) => \get_resource_id($value),
71+
// Resource to int type coercion is not obvious:
72+
// \is_resource($value) => \get_resource_id($value),
7273
$value === IntBackedEnumStub::ExampleCase => IntBackedEnumStub::ExampleCase->value,
7374
default => $default,
7475
},

tests/Type/StringTypeTest.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,13 @@
99
use TypeLang\Mapper\Tests\Type\Stub\IntBackedEnumStub;
1010
use TypeLang\Mapper\Tests\Type\Stub\StringBackedEnumStub;
1111
use TypeLang\Mapper\Tests\Type\Stub\UnitEnumStub;
12-
use TypeLang\Mapper\Type\ArrayKeyType;
13-
use TypeLang\Mapper\Type\Coercer\ArrayKeyTypeCoercer;
12+
use TypeLang\Mapper\Type\Coercer\StringTypeCoercer;
1413
use TypeLang\Mapper\Type\StringType;
1514
use TypeLang\Mapper\Type\TypeInterface;
1615

1716
#[Group('types')]
18-
#[CoversClass(ArrayKeyType::class)]
19-
#[CoversClass(ArrayKeyTypeCoercer::class)]
17+
#[CoversClass(StringType::class)]
18+
#[CoversClass(StringTypeCoercer::class)]
2019
final class StringTypeTest extends SymmetricTypeTestCase
2120
{
2221
protected static function createType(): TypeInterface

tests/Type/TypeTestCase.php

Lines changed: 36 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use PHPUnit\Framework\Attributes\DataProvider;
88
use TypeLang\Mapper\Context\RootContext;
9+
use TypeLang\Mapper\Exception\Mapping\InvalidValueException;
910
use TypeLang\Mapper\Runtime\Configuration;
1011
use TypeLang\Mapper\Runtime\Extractor\TypeExtractorInterface;
1112
use TypeLang\Mapper\Runtime\Parser\TypeParserInterface;
@@ -145,6 +146,33 @@ private static function defaultDataProviderSamples(): iterable
145146
yield StringBackedEnumStub::ExampleCase;
146147
}
147148

149+
protected static function assertIfNotException(mixed $expected, mixed $actual): void
150+
{
151+
switch (true) {
152+
case $expected instanceof \Throwable:
153+
break;
154+
case \is_array($expected):
155+
case \is_object($expected):
156+
self::assertEquals($expected, $actual);
157+
break;
158+
case \is_float($expected) && \is_nan($expected):
159+
self::assertNan($actual);
160+
break;
161+
default:
162+
self::assertSame($expected, $actual);
163+
}
164+
}
165+
166+
protected function expectTypeErrorIfException(mixed $expected): void
167+
{
168+
if (!$expected instanceof \Throwable) {
169+
return;
170+
}
171+
172+
$this->expectExceptionMessage($expected->getMessage());
173+
$this->expectException(InvalidValueException::class);
174+
}
175+
148176
/**
149177
* @return non-empty-string
150178
*/
@@ -261,9 +289,7 @@ public static function castStrictNormalizationDataProvider(): iterable
261289
#[DataProvider('castStrictNormalizationDataProvider')]
262290
public function testCastStrictNormalization(mixed $value, mixed $expected): void
263291
{
264-
if ($expected instanceof \Throwable) {
265-
self::expectExceptionMessage($expected->getMessage());
266-
}
292+
$this->expectTypeErrorIfException($expected);
267293

268294
$type = static::createType();
269295

@@ -272,16 +298,7 @@ public function testCastStrictNormalization(mixed $value, mixed $expected): void
272298
strictTypes: true,
273299
));
274300

275-
switch (true) {
276-
case $expected instanceof \Throwable:
277-
break;
278-
case \is_array($expected):
279-
case \is_object($expected):
280-
self::assertEquals($expected, $actual);
281-
break;
282-
default:
283-
self::assertSame($expected, $actual);
284-
}
301+
self::assertIfNotException($expected, $actual);
285302
}
286303

287304
/**
@@ -295,9 +312,7 @@ public static function castNonStrictNormalizationDataProvider(): iterable
295312
#[DataProvider('castNonStrictNormalizationDataProvider')]
296313
public function testCastNonStrictNormalization(mixed $value, mixed $expected): void
297314
{
298-
if ($expected instanceof \Throwable) {
299-
self::expectExceptionMessage($expected->getMessage());
300-
}
315+
$this->expectTypeErrorIfException($expected);
301316

302317
$type = static::createType();
303318

@@ -306,16 +321,7 @@ public function testCastNonStrictNormalization(mixed $value, mixed $expected): v
306321
strictTypes: false,
307322
));
308323

309-
switch (true) {
310-
case $expected instanceof \Throwable:
311-
break;
312-
case \is_array($expected):
313-
case \is_object($expected):
314-
self::assertEquals($expected, $actual);
315-
break;
316-
default:
317-
self::assertSame($expected, $actual);
318-
}
324+
self::assertIfNotException($expected, $actual);
319325
}
320326

321327
/**
@@ -329,9 +335,7 @@ public static function castStrictDenormalizationDataProvider(): iterable
329335
#[DataProvider('castStrictDenormalizationDataProvider')]
330336
public function testCastStrictDenormalization(mixed $value, mixed $expected): void
331337
{
332-
if ($expected instanceof \Throwable) {
333-
self::expectExceptionMessage($expected->getMessage());
334-
}
338+
$this->expectTypeErrorIfException($expected);
335339

336340
$type = static::createType();
337341

@@ -340,16 +344,7 @@ public function testCastStrictDenormalization(mixed $value, mixed $expected): vo
340344
strictTypes: true,
341345
));
342346

343-
switch (true) {
344-
case $expected instanceof \Throwable:
345-
break;
346-
case \is_array($expected):
347-
case \is_object($expected):
348-
self::assertEquals($expected, $actual);
349-
break;
350-
default:
351-
self::assertSame($expected, $actual);
352-
}
347+
self::assertIfNotException($expected, $actual);
353348
}
354349

355350
/**
@@ -363,9 +358,7 @@ public static function castNonStrictDenormalizationDataProvider(): iterable
363358
#[DataProvider('castNonStrictDenormalizationDataProvider')]
364359
public function testCastNonStrictDenormalization(mixed $value, mixed $expected): void
365360
{
366-
if ($expected instanceof \Throwable) {
367-
self::expectExceptionMessage($expected->getMessage());
368-
}
361+
$this->expectTypeErrorIfException($expected);
369362

370363
$type = static::createType();
371364

@@ -374,16 +367,7 @@ public function testCastNonStrictDenormalization(mixed $value, mixed $expected):
374367
strictTypes: false,
375368
));
376369

377-
switch (true) {
378-
case $expected instanceof \Throwable:
379-
break;
380-
case \is_array($expected):
381-
case \is_object($expected):
382-
self::assertEquals($expected, $actual);
383-
break;
384-
default:
385-
self::assertSame($expected, $actual);
386-
}
370+
self::assertIfNotException($expected, $actual);
387371
}
388372

389373
/**

0 commit comments

Comments
 (0)