-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathConditionalTypesWithGenericsTest.php
More file actions
115 lines (81 loc) · 3.75 KB
/
Copy pathConditionalTypesWithGenericsTest.php
File metadata and controls
115 lines (81 loc) · 3.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
declare(strict_types=1);
use TypePHP\Tests\Fixtures\Domain\Cat;
use TypePHP\Tests\Fixtures\Domain\Dog;
use TypePHP\Tests\Fixtures\Generics\DogConditionalBox;
use TypePHP\Tests\Fixtures\Generics\GenericConditionalFactory;
/**
* Standalone function with generic conditional return type
*
* @template T
*
* @param T $typeSample
* @param mixed $value
*
* @return (T is Dog ? positive-int : non-empty-string)
*/
function testStandaloneGenericConditional(mixed $typeSample, mixed $value): mixed
{
return $value;
}
describe('Conditional Types with Generics (T is Target ? If : Else)', function () {
describe('Standalone Generic Functions', function () {
test('evaluates positive-int branch when template T is inferred as Dog', function () {
$dog = new Dog();
expect(testStandaloneGenericConditional($dog, 100))->toBe(100);
expect(fn () => testStandaloneGenericConditional($dog, -50))
->toThrow(TypeError::class, 'Return value must be of type positive-int')
;
});
test('evaluates non-empty-string branch when template T is inferred as Cat', function () {
$cat = new Cat();
expect(testStandaloneGenericConditional($cat, 'valid_string'))->toBe('valid_string');
expect(fn () => testStandaloneGenericConditional($cat, ''))
->toThrow(TypeError::class, 'Return value must be of type non-empty-string')
;
});
});
describe('Negated Conditional Types (T is not Target ? If : Else)', function () {
test('evaluates non-empty-string branch when T is not Dog', function () {
$factory = new GenericConditionalFactory();
$cat = new Cat();
expect($factory->processNegated($cat, 'hello'))->toBe('hello');
expect(fn () => $factory->processNegated($cat, ''))
->toThrow(TypeError::class, 'Return value must be of type non-empty-string')
;
});
test('evaluates positive-int branch when T is Dog in negated conditional', function () {
$factory = new GenericConditionalFactory();
$dog = new Dog();
expect($factory->processNegated($dog, 42))->toBe(42);
expect(fn () => $factory->processNegated($dog, -10))
->toThrow(TypeError::class, 'Return value must be of type positive-int')
;
});
});
describe('class-string<T> Factories with Conditional Return Types', function () {
test('evaluates list<positive-int> when class-string is Dog::class', function () {
$factory = new GenericConditionalFactory();
expect($factory->createPayload(Dog::class, [10, 20, 30]))->toBe([10, 20, 30]);
expect(fn () => $factory->createPayload(Dog::class, [10, -5]))
->toThrow(TypeError::class, 'Return value')
;
});
test('evaluates list<non-empty-string> when class-string is Cat::class', function () {
$factory = new GenericConditionalFactory();
expect($factory->createPayload(Cat::class, ['a', 'b']))->toBe(['a', 'b']);
expect(fn () => $factory->createPayload(Cat::class, ['a', '']))
->toThrow(TypeError::class, 'Return value')
;
});
});
describe('Inherited Generic Classes with Conditionals', function () {
test('evaluates conditional return types inherited from abstract parent generic class', function () {
$dogBox = new DogConditionalBox();
expect($dogBox->processInput(100))->toBe(100);
expect(fn () => $dogBox->processInput(-99))
->toThrow(TypeError::class, 'Return value must be of type positive-int')
;
});
});
});