|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types = 1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Tests\Innmind\Immutable\Set; |
|
5
|
|
|
|
|
6
|
|
|
use Innmind\Immutable\{ |
|
7
|
|
|
Set\Primitive, |
|
|
|
|
|
|
8
|
|
|
Set\Implementation, |
|
9
|
|
|
Set, |
|
10
|
|
|
Map, |
|
11
|
|
|
Str, |
|
12
|
|
|
Sequence, |
|
13
|
|
|
}; |
|
14
|
|
|
use function Innmind\Immutable\unwrap; |
|
15
|
|
|
use PHPUnit\Framework\TestCase; |
|
16
|
|
|
|
|
17
|
|
|
class PrimitiveTest extends TestCase |
|
18
|
|
|
{ |
|
19
|
|
|
public function testInterface() |
|
20
|
|
|
{ |
|
21
|
|
|
$this->assertInstanceOf( |
|
22
|
|
|
Implementation::class, |
|
23
|
|
|
new Primitive('int'), |
|
24
|
|
|
); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
public function testType() |
|
28
|
|
|
{ |
|
29
|
|
|
$this->assertSame('int', (new Primitive('int'))->type()); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function testSize() |
|
33
|
|
|
{ |
|
34
|
|
|
$this->assertSame(2, (new Primitive('int', 1, 2))->size()); |
|
35
|
|
|
$this->assertSame(2, (new Primitive('int', 1, 2))->count()); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function testIterator() |
|
39
|
|
|
{ |
|
40
|
|
|
$this->assertSame([1, 2], \iterator_to_array((new Primitive('int', 1, 2))->iterator())); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function testIntersect() |
|
44
|
|
|
{ |
|
45
|
|
|
$a = new Primitive('int', 1, 2); |
|
46
|
|
|
$b = new Primitive('int', 2, 3); |
|
47
|
|
|
$c = $a->intersect($b); |
|
48
|
|
|
|
|
49
|
|
|
$this->assertSame([1, 2], \iterator_to_array($a->iterator())); |
|
50
|
|
|
$this->assertSame([2, 3], \iterator_to_array($b->iterator())); |
|
51
|
|
|
$this->assertInstanceOf(Primitive::class, $c); |
|
52
|
|
|
$this->assertSame([2], \iterator_to_array($c->iterator())); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function testAdd() |
|
56
|
|
|
{ |
|
57
|
|
|
$a = new Primitive('int', 1); |
|
58
|
|
|
$b = ($a)(2); |
|
59
|
|
|
|
|
60
|
|
|
$this->assertSame([1], \iterator_to_array($a->iterator())); |
|
61
|
|
|
$this->assertInstanceOf(Primitive::class, $b); |
|
62
|
|
|
$this->assertSame([1, 2], \iterator_to_array($b->iterator())); |
|
63
|
|
|
$this->assertSame($b, ($b)(2)); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function testContains() |
|
67
|
|
|
{ |
|
68
|
|
|
$set = new Primitive('int', 1); |
|
69
|
|
|
|
|
70
|
|
|
$this->assertTrue($set->contains(1)); |
|
71
|
|
|
$this->assertFalse($set->contains(2)); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function testRemove() |
|
75
|
|
|
{ |
|
76
|
|
|
$a = new Primitive('int', 1, 2, 3, 4); |
|
77
|
|
|
$b = $a->remove(3); |
|
78
|
|
|
|
|
79
|
|
|
$this->assertSame([1, 2, 3, 4], \iterator_to_array($a->iterator())); |
|
80
|
|
|
$this->assertInstanceOf(Primitive::class, $b); |
|
81
|
|
|
$this->assertSame([1, 2, 4], \iterator_to_array($b->iterator())); |
|
82
|
|
|
$this->assertSame($a, $a->remove(5)); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function testDiff() |
|
86
|
|
|
{ |
|
87
|
|
|
$a = new Primitive('int', 1, 2, 3); |
|
88
|
|
|
$b = new Primitive('int', 2, 4); |
|
89
|
|
|
$c = $a->diff($b); |
|
90
|
|
|
|
|
91
|
|
|
$this->assertSame([1, 2, 3], \iterator_to_array($a->iterator())); |
|
92
|
|
|
$this->assertSame([2, 4], \iterator_to_array($b->iterator())); |
|
93
|
|
|
$this->assertInstanceOf(Primitive::class, $c); |
|
94
|
|
|
$this->assertSame([1, 3], \iterator_to_array($c->iterator())); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
public function testEquals() |
|
98
|
|
|
{ |
|
99
|
|
|
$this->assertTrue((new Primitive('int', 1, 2))->equals(new Primitive('int', 1, 2))); |
|
100
|
|
|
$this->assertFalse((new Primitive('int', 1, 2))->equals(new Primitive('int', 1))); |
|
101
|
|
|
$this->assertFalse((new Primitive('int', 1, 2))->equals(new Primitive('int', 1, 2, 3))); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
public function testFilter() |
|
105
|
|
|
{ |
|
106
|
|
|
$a = new Primitive('int', 1, 2, 3, 4); |
|
107
|
|
|
$b = $a->filter(fn($i) => $i % 2 === 0); |
|
108
|
|
|
|
|
109
|
|
|
$this->assertSame([1, 2, 3, 4], \iterator_to_array($a->iterator())); |
|
110
|
|
|
$this->assertInstanceOf(Primitive::class, $b); |
|
111
|
|
|
$this->assertSame([2, 4], \iterator_to_array($b->iterator())); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
public function testForeach() |
|
115
|
|
|
{ |
|
116
|
|
|
$set = new Primitive('int', 1, 2, 3, 4); |
|
117
|
|
|
$calls = 0; |
|
118
|
|
|
$sum = 0; |
|
119
|
|
|
|
|
120
|
|
|
$this->assertNull($set->foreach(function($i) use (&$calls, &$sum) { |
|
121
|
|
|
++$calls; |
|
122
|
|
|
$sum += $i; |
|
123
|
|
|
})); |
|
124
|
|
|
|
|
125
|
|
|
$this->assertSame(4, $calls); |
|
126
|
|
|
$this->assertSame(10, $sum); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
public function testGroupBy() |
|
130
|
|
|
{ |
|
131
|
|
|
$set = new Primitive('int', 1, 2, 3, 4); |
|
132
|
|
|
$groups = $set->groupBy(fn($i) => $i % 2); |
|
133
|
|
|
|
|
134
|
|
|
$this->assertSame([1, 2, 3, 4], \iterator_to_array($set->iterator())); |
|
135
|
|
|
$this->assertInstanceOf(Map::class, $groups); |
|
136
|
|
|
$this->assertTrue($groups->isOfType('int', Set::class)); |
|
137
|
|
|
$this->assertCount(2, $groups); |
|
138
|
|
|
$this->assertTrue($groups->get(0)->isOfType('int')); |
|
139
|
|
|
$this->assertTrue($groups->get(1)->isOfType('int')); |
|
140
|
|
|
$this->assertSame([2, 4], unwrap($groups->get(0))); |
|
141
|
|
|
$this->assertSame([1, 3], unwrap($groups->get(1))); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
public function testMap() |
|
145
|
|
|
{ |
|
146
|
|
|
$a = new Primitive('int', 1, 2, 3); |
|
147
|
|
|
$b = $a->map(fn($i) => $i * 2); |
|
148
|
|
|
|
|
149
|
|
|
$this->assertSame([1, 2, 3], \iterator_to_array($a->iterator())); |
|
150
|
|
|
$this->assertInstanceOf(Primitive::class, $b); |
|
151
|
|
|
$this->assertSame([2, 4, 6], \iterator_to_array($b->iterator())); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
public function testThrowWhenTryingToModifyTheTypeWhenMapping() |
|
155
|
|
|
{ |
|
156
|
|
|
$this->expectException(\TypeError::class); |
|
157
|
|
|
$this->expectExceptionMessage('Argument 1 must be of type int, string given'); |
|
158
|
|
|
|
|
159
|
|
|
(new Primitive('int', 1))->map(fn($i) => (string) $i); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
public function testPartition() |
|
163
|
|
|
{ |
|
164
|
|
|
$set = new Primitive('int', 1, 2, 3, 4); |
|
165
|
|
|
$groups = $set->partition(fn($i) => $i % 2 === 0); |
|
166
|
|
|
|
|
167
|
|
|
$this->assertSame([1, 2, 3, 4], \iterator_to_array($set->iterator())); |
|
168
|
|
|
$this->assertInstanceOf(Map::class, $groups); |
|
169
|
|
|
$this->assertTrue($groups->isOfType('bool', Set::class)); |
|
170
|
|
|
$this->assertCount(2, $groups); |
|
171
|
|
|
$this->assertTrue($groups->get(true)->isOfType('int')); |
|
172
|
|
|
$this->assertTrue($groups->get(false)->isOfType('int')); |
|
173
|
|
|
$this->assertSame([2, 4], unwrap($groups->get(true))); |
|
174
|
|
|
$this->assertSame([1, 3], unwrap($groups->get(false))); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
public function testSort() |
|
178
|
|
|
{ |
|
179
|
|
|
$set = new Primitive('int', 1, 4, 3, 2); |
|
180
|
|
|
$sorted = $set->sort(fn($a, $b) => $a > $b); |
|
181
|
|
|
|
|
182
|
|
|
$this->assertSame([1, 4, 3, 2], \iterator_to_array($set->iterator())); |
|
183
|
|
|
$this->assertInstanceOf(Sequence::class, $sorted); |
|
184
|
|
|
$this->assertSame([1, 2, 3, 4], unwrap($sorted)); |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
public function testMerge() |
|
188
|
|
|
{ |
|
189
|
|
|
$a = new Primitive('int', 1, 2); |
|
190
|
|
|
$b = new Primitive('int', 2, 3); |
|
191
|
|
|
$c = $a->merge($b); |
|
192
|
|
|
|
|
193
|
|
|
$this->assertSame([1, 2], \iterator_to_array($a->iterator())); |
|
194
|
|
|
$this->assertSame([2, 3], \iterator_to_array($b->iterator())); |
|
195
|
|
|
$this->assertInstanceOf(Primitive::class, $c); |
|
196
|
|
|
$this->assertSame([1, 2, 3], \iterator_to_array($c->iterator())); |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
public function testReduce() |
|
200
|
|
|
{ |
|
201
|
|
|
$set = new Primitive('int', 1, 2, 3, 4); |
|
202
|
|
|
|
|
203
|
|
|
$this->assertSame(10, $set->reduce(0, fn($sum, $i) => $sum + $i)); |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
public function testClear() |
|
207
|
|
|
{ |
|
208
|
|
|
$a = new Primitive('int', 1); |
|
209
|
|
|
$b = $a->clear(); |
|
210
|
|
|
|
|
211
|
|
|
$this->assertSame([1], \iterator_to_array($a->iterator())); |
|
212
|
|
|
$this->assertInstanceOf(Primitive::class, $b); |
|
213
|
|
|
$this->assertSame([], \iterator_to_array($b->iterator())); |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
public function testEmpty() |
|
217
|
|
|
{ |
|
218
|
|
|
$this->assertTrue((new Primitive('int'))->empty()); |
|
219
|
|
|
$this->assertFalse((new Primitive('int', 1))->empty()); |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
public function testToSequenceOf() |
|
223
|
|
|
{ |
|
224
|
|
|
$set = new Primitive('int', 1, 2, 3); |
|
225
|
|
|
$sequence = $set->toSequenceOf('string|int', function($i) { |
|
226
|
|
|
yield (string) $i; |
|
227
|
|
|
yield $i; |
|
228
|
|
|
}); |
|
229
|
|
|
|
|
230
|
|
|
$this->assertInstanceOf(Sequence::class, $sequence); |
|
231
|
|
|
$this->assertSame( |
|
232
|
|
|
['1', 1, '2', 2, '3', 3], |
|
233
|
|
|
unwrap($sequence), |
|
234
|
|
|
); |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
public function testToSetOf() |
|
238
|
|
|
{ |
|
239
|
|
|
$set = new Primitive('int', 1, 2, 3); |
|
240
|
|
|
$set = $set->toSetOf('string|int', function($i) { |
|
241
|
|
|
yield (string) $i; |
|
242
|
|
|
yield $i; |
|
243
|
|
|
}); |
|
244
|
|
|
|
|
245
|
|
|
$this->assertInstanceOf(Set::class, $set); |
|
246
|
|
|
$this->assertSame( |
|
247
|
|
|
['1', 1, '2', 2, '3', 3], |
|
248
|
|
|
unwrap($set), |
|
249
|
|
|
); |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
public function testToMapOf() |
|
253
|
|
|
{ |
|
254
|
|
|
$set = new Primitive('int', 1, 2, 3); |
|
255
|
|
|
$map = $set->toMapOf('string', 'int', fn($i) => yield (string) $i => $i); |
|
256
|
|
|
|
|
257
|
|
|
$this->assertInstanceOf(Map::class, $map); |
|
258
|
|
|
$this->assertCount(3, $map); |
|
259
|
|
|
$this->assertSame(1, $map->get('1')); |
|
260
|
|
|
$this->assertSame(2, $map->get('2')); |
|
261
|
|
|
$this->assertSame(3, $map->get('3')); |
|
262
|
|
|
} |
|
263
|
|
|
} |
|
264
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths