1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace Tests\Innmind\Immutable\Sequence; |
5
|
|
|
|
6
|
|
|
use Innmind\Immutable\{ |
7
|
|
|
Sequence\Primitive, |
|
|
|
|
8
|
|
|
Sequence\Implementation, |
9
|
|
|
Map, |
10
|
|
|
Sequence, |
11
|
|
|
Str, |
12
|
|
|
Set, |
13
|
|
|
Exception\OutOfBoundException, |
14
|
|
|
Exception\CannotGroupEmptyStructure, |
15
|
|
|
Exception\ElementNotFound, |
16
|
|
|
}; |
17
|
|
|
use function Innmind\Immutable\unwrap; |
18
|
|
|
use PHPUnit\Framework\TestCase; |
19
|
|
|
|
20
|
|
|
class PrimitiveTest extends TestCase |
21
|
|
|
{ |
22
|
|
|
public function testInterface() |
23
|
|
|
{ |
24
|
|
|
$this->assertInstanceOf( |
25
|
|
|
Implementation::class, |
26
|
|
|
new Primitive('mixed'), |
27
|
|
|
); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function testType() |
31
|
|
|
{ |
32
|
|
|
$this->assertSame('int', (new Primitive('int'))->type()); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function testSize() |
36
|
|
|
{ |
37
|
|
|
$this->assertSame(2, (new Primitive('int', 1, 1))->size()); |
38
|
|
|
$this->assertSame(2, (new Primitive('int', 1, 1))->count()); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function testIterator() |
42
|
|
|
{ |
43
|
|
|
$this->assertSame( |
44
|
|
|
[1, 2, 3], |
45
|
|
|
\iterator_to_array((new Primitive('int', 1, 2, 3))->iterator()), |
46
|
|
|
); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function testGet() |
50
|
|
|
{ |
51
|
|
|
$this->assertSame(42, (new Primitive('int', 1, 42, 3))->get(1)); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function testThrowWhenIndexNotFound() |
55
|
|
|
{ |
56
|
|
|
$this->expectException(OutOfBoundException::class); |
57
|
|
|
|
58
|
|
|
(new Primitive('int'))->get(0); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function testDiff() |
62
|
|
|
{ |
63
|
|
|
$a = new Primitive('int', 1, 2); |
64
|
|
|
$b = new Primitive('int', 2, 3); |
65
|
|
|
$c = $a->diff($b); |
66
|
|
|
|
67
|
|
|
$this->assertSame([1, 2], \iterator_to_array($a->iterator())); |
68
|
|
|
$this->assertSame([2, 3], \iterator_to_array($b->iterator())); |
69
|
|
|
$this->assertInstanceOf(Primitive::class, $c); |
70
|
|
|
$this->assertSame([1], \iterator_to_array($c->iterator())); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function testDistinct() |
74
|
|
|
{ |
75
|
|
|
$a = new Primitive('int', 1, 2, 1); |
76
|
|
|
$b = $a->distinct(); |
77
|
|
|
|
78
|
|
|
$this->assertSame([1, 2, 1], \iterator_to_array($a->iterator())); |
79
|
|
|
$this->assertInstanceOf(Primitive::class, $b); |
80
|
|
|
$this->assertSame([1, 2], \iterator_to_array($b->iterator())); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function testDrop() |
84
|
|
|
{ |
85
|
|
|
$a = new Primitive('int', 1, 2, 3, 4); |
86
|
|
|
$b = $a->drop(2); |
87
|
|
|
|
88
|
|
|
$this->assertSame([1, 2, 3, 4], \iterator_to_array($a->iterator())); |
89
|
|
|
$this->assertInstanceOf(Primitive::class, $b); |
90
|
|
|
$this->assertSame([3, 4], \iterator_to_array($b->iterator())); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function testDropEnd() |
94
|
|
|
{ |
95
|
|
|
$a = new Primitive('int', 1, 2, 3, 4); |
96
|
|
|
$b = $a->dropEnd(2); |
97
|
|
|
|
98
|
|
|
$this->assertSame([1, 2, 3, 4], \iterator_to_array($a->iterator())); |
99
|
|
|
$this->assertInstanceOf(Primitive::class, $b); |
100
|
|
|
$this->assertSame([1, 2], \iterator_to_array($b->iterator())); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function testEquals() |
104
|
|
|
{ |
105
|
|
|
$this->assertTrue((new Primitive('int', 1, 2))->equals(new Primitive('int', 1, 2))); |
106
|
|
|
$this->assertFalse((new Primitive('int', 1, 2))->equals(new Primitive('int', 2))); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function testFilter() |
110
|
|
|
{ |
111
|
|
|
$a = new Primitive('int', 1, 2, 3, 4); |
112
|
|
|
$b = $a->filter(fn($i) => $i % 2 === 0); |
113
|
|
|
|
114
|
|
|
$this->assertSame([1, 2, 3, 4], \iterator_to_array($a->iterator())); |
115
|
|
|
$this->assertInstanceOf(Primitive::class, $b); |
116
|
|
|
$this->assertSame([2, 4], \iterator_to_array($b->iterator())); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function testForeach() |
120
|
|
|
{ |
121
|
|
|
$sequence = new Primitive('int', 1, 2, 3, 4); |
122
|
|
|
$calls = 0; |
123
|
|
|
$sum = 0; |
124
|
|
|
|
125
|
|
|
$this->assertNull($sequence->foreach(function($i) use (&$calls, &$sum) { |
126
|
|
|
++$calls; |
127
|
|
|
$sum += $i; |
128
|
|
|
})); |
129
|
|
|
$this->assertSame(4, $calls); |
130
|
|
|
$this->assertSame(10, $sum); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public function testThrowWhenTryingToGroupEmptySequence() |
134
|
|
|
{ |
135
|
|
|
$this->expectException(CannotGroupEmptyStructure::class); |
136
|
|
|
|
137
|
|
|
(new Primitive('int'))->groupBy(fn($i) => $i); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
public function testGroupBy() |
141
|
|
|
{ |
142
|
|
|
$sequence = new Primitive('int', 1, 2, 3, 4); |
143
|
|
|
$groups = $sequence->groupBy(fn($i) => $i % 2); |
144
|
|
|
|
145
|
|
|
$this->assertSame([1, 2, 3, 4], \iterator_to_array($sequence->iterator())); |
146
|
|
|
$this->assertInstanceOf(Map::class, $groups); |
147
|
|
|
$this->assertTrue($groups->isOfType('int', Sequence::class)); |
148
|
|
|
$this->assertCount(2, $groups); |
149
|
|
|
$this->assertTrue($groups->get(0)->isOfType('int')); |
150
|
|
|
$this->assertTrue($groups->get(1)->isOfType('int')); |
151
|
|
|
$this->assertSame([2, 4], unwrap($groups->get(0))); |
152
|
|
|
$this->assertSame([1, 3], unwrap($groups->get(1))); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
public function testThrowWhenTryingToAccessFirstElementOnEmptySequence() |
156
|
|
|
{ |
157
|
|
|
$this->expectException(OutOfBoundException::class); |
158
|
|
|
|
159
|
|
|
(new Primitive('int'))->first(); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
public function testThrowWhenTryingToAccessLastElementOnEmptySequence() |
163
|
|
|
{ |
164
|
|
|
$this->expectException(OutOfBoundException::class); |
165
|
|
|
|
166
|
|
|
(new Primitive('int'))->last(); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
public function testFirst() |
170
|
|
|
{ |
171
|
|
|
$this->assertSame(2, (new Primitive('int', 2, 3, 4))->first()); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
public function testLast() |
175
|
|
|
{ |
176
|
|
|
$this->assertSame(4, (new Primitive('int', 2, 3, 4))->last()); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
public function testContains() |
180
|
|
|
{ |
181
|
|
|
$sequence = new Primitive('int', 1, 2, 3); |
182
|
|
|
|
183
|
|
|
$this->assertTrue($sequence->contains(2)); |
184
|
|
|
$this->assertFalse($sequence->contains(4)); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
public function testIndexOf() |
188
|
|
|
{ |
189
|
|
|
$sequence = new Primitive('int', 1, 2, 4); |
190
|
|
|
|
191
|
|
|
$this->assertSame(1, $sequence->indexOf(2)); |
192
|
|
|
$this->assertSame(2, $sequence->indexOf(4)); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
public function testThrowWhenTryingToAccessIndexOfUnknownValue() |
196
|
|
|
{ |
197
|
|
|
$this->expectException(ElementNotFound::class); |
198
|
|
|
|
199
|
|
|
(new Primitive('int'))->indexOf(1); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
public function testIndices() |
203
|
|
|
{ |
204
|
|
|
$a = new Primitive('string', '1', '2'); |
205
|
|
|
$b = $a->indices(); |
206
|
|
|
|
207
|
|
|
$this->assertSame(['1', '2'], \iterator_to_array($a->iterator())); |
208
|
|
|
$this->assertInstanceOf(Primitive::class, $b); |
209
|
|
|
$this->assertSame('int', $b->type()); |
210
|
|
|
$this->assertSame([0, 1], \iterator_to_array($b->iterator())); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
public function testIndicesOnEmptySequence() |
214
|
|
|
{ |
215
|
|
|
$a = new Primitive('string'); |
216
|
|
|
$b = $a->indices(); |
217
|
|
|
|
218
|
|
|
$this->assertSame([], \iterator_to_array($a->iterator())); |
219
|
|
|
$this->assertInstanceOf(Primitive::class, $b); |
220
|
|
|
$this->assertSame('int', $b->type()); |
221
|
|
|
$this->assertSame([], \iterator_to_array($b->iterator())); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
public function testMap() |
225
|
|
|
{ |
226
|
|
|
$a = new Primitive('int', 1, 2, 3); |
227
|
|
|
$b = $a->map(fn($i) => $i * 2); |
228
|
|
|
|
229
|
|
|
$this->assertSame([1, 2, 3], \iterator_to_array($a->iterator())); |
230
|
|
|
$this->assertInstanceOf(Primitive::class, $b); |
231
|
|
|
$this->assertSame([2, 4, 6], \iterator_to_array($b->iterator())); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
public function testThrowWhenTryingToModifyTheTypeWhenMapping() |
235
|
|
|
{ |
236
|
|
|
$this->expectException(\TypeError::class); |
237
|
|
|
$this->expectExceptionMessage('Argument 1 must be of type int, string given'); |
238
|
|
|
|
239
|
|
|
(new Primitive('int', 1))->map(fn($i) => (string) $i); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
public function testPad() |
243
|
|
|
{ |
244
|
|
|
$a = new Primitive('int', 1, 2); |
245
|
|
|
$b = $a->pad(4, 0); |
246
|
|
|
$c = $a->pad(1, 0); |
247
|
|
|
|
248
|
|
|
$this->assertSame([1, 2], \iterator_to_array($a->iterator())); |
249
|
|
|
$this->assertInstanceOf(Primitive::class, $b); |
250
|
|
|
$this->assertInstanceOf(Primitive::class, $c); |
251
|
|
|
$this->assertSame([1, 2, 0, 0], \iterator_to_array($b->iterator())); |
252
|
|
|
$this->assertSame([1, 2], \iterator_to_array($c->iterator())); |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
public function testPartition() |
256
|
|
|
{ |
257
|
|
|
$sequence = new Primitive('int', 1, 2, 3, 4); |
258
|
|
|
$partition = $sequence->partition(fn($i) => $i % 2 === 0); |
259
|
|
|
|
260
|
|
|
$this->assertSame([1, 2, 3, 4], \iterator_to_array($sequence->iterator())); |
261
|
|
|
$this->assertInstanceOf(Map::class, $partition); |
262
|
|
|
$this->assertTrue($partition->isOfType('bool', Sequence::class)); |
263
|
|
|
$this->assertCount(2, $partition); |
264
|
|
|
$this->assertTrue($partition->get(true)->isOfType('int')); |
265
|
|
|
$this->assertTrue($partition->get(false)->isOfType('int')); |
266
|
|
|
$this->assertSame([2, 4], unwrap($partition->get(true))); |
267
|
|
|
$this->assertSame([1, 3], unwrap($partition->get(false))); |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
public function testSlice() |
271
|
|
|
{ |
272
|
|
|
$a = new Primitive('int', 2, 3, 4, 5); |
273
|
|
|
$b = $a->slice(1, 3); |
274
|
|
|
|
275
|
|
|
$this->assertSame([2, 3, 4, 5], \iterator_to_array($a->iterator())); |
276
|
|
|
$this->assertInstanceOf(Primitive::class, $b); |
277
|
|
|
$this->assertSame([3, 4], \iterator_to_array($b->iterator())); |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
public function testSplitAt() |
281
|
|
|
{ |
282
|
|
|
$sequence = new Primitive('int', 2, 3, 4, 5); |
283
|
|
|
$parts = $sequence->splitAt(2); |
284
|
|
|
|
285
|
|
|
$this->assertSame([2, 3, 4, 5], \iterator_to_array($sequence->iterator())); |
286
|
|
|
$this->assertInstanceOf(Sequence::class, $parts); |
287
|
|
|
$this->assertTrue($parts->isOfType(Sequence::class)); |
288
|
|
|
$this->assertCount(2, $parts); |
289
|
|
|
$this->assertTrue($parts->first()->isOfType('int')); |
290
|
|
|
$this->assertTrue($parts->last()->isOfType('int')); |
291
|
|
|
$this->assertSame([2, 3], unwrap($parts->first())); |
292
|
|
|
$this->assertSame([4, 5], unwrap($parts->last())); |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
public function testTake() |
296
|
|
|
{ |
297
|
|
|
$a = new Primitive('int', 2, 3, 4); |
298
|
|
|
$b = $a->take(2); |
299
|
|
|
|
300
|
|
|
$this->assertSame([2, 3, 4], \iterator_to_array($a->iterator())); |
301
|
|
|
$this->assertInstanceOf(Primitive::class, $b); |
302
|
|
|
$this->assertSame([2, 3], \iterator_to_array($b->iterator())); |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
public function testTakeEnd() |
306
|
|
|
{ |
307
|
|
|
$a = new Primitive('int', 2, 3, 4); |
308
|
|
|
$b = $a->takeEnd(2); |
309
|
|
|
|
310
|
|
|
$this->assertSame([2, 3, 4], \iterator_to_array($a->iterator())); |
311
|
|
|
$this->assertInstanceOf(Primitive::class, $b); |
312
|
|
|
$this->assertSame([3, 4], \iterator_to_array($b->iterator())); |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
public function testAppend() |
316
|
|
|
{ |
317
|
|
|
$a = new Primitive('int', 1, 2); |
318
|
|
|
$b = new Primitive('int', 3, 4); |
319
|
|
|
$c = $a->append($b); |
320
|
|
|
|
321
|
|
|
$this->assertSame([1, 2], \iterator_to_array($a->iterator())); |
322
|
|
|
$this->assertSame([3, 4], \iterator_to_array($b->iterator())); |
323
|
|
|
$this->assertInstanceOf(Primitive::class, $c); |
324
|
|
|
$this->assertSame([1, 2, 3, 4], \iterator_to_array($c->iterator())); |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
public function testIntersect() |
328
|
|
|
{ |
329
|
|
|
$a = new Primitive('int', 1, 2); |
330
|
|
|
$b = new Primitive('int', 2, 3); |
331
|
|
|
$c = $a->intersect($b); |
332
|
|
|
|
333
|
|
|
$this->assertSame([1, 2], \iterator_to_array($a->iterator())); |
334
|
|
|
$this->assertSame([2, 3], \iterator_to_array($b->iterator())); |
335
|
|
|
$this->assertInstanceOf(Primitive::class, $c); |
336
|
|
|
$this->assertSame([2], \iterator_to_array($c->iterator())); |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
public function testAdd() |
340
|
|
|
{ |
341
|
|
|
$a = new Primitive('int', 1); |
342
|
|
|
$b = ($a)(2); |
343
|
|
|
|
344
|
|
|
$this->assertSame([1], \iterator_to_array($a->iterator())); |
345
|
|
|
$this->assertInstanceOf(Primitive::class, $b); |
346
|
|
|
$this->assertSame([1, 2], \iterator_to_array($b->iterator())); |
347
|
|
|
} |
348
|
|
|
|
349
|
|
|
public function testSort() |
350
|
|
|
{ |
351
|
|
|
$a = new Primitive('int', 1, 4, 3, 2); |
352
|
|
|
$b = $a->sort(fn($a, $b) => $a > $b); |
353
|
|
|
|
354
|
|
|
$this->assertSame([1, 4, 3, 2], \iterator_to_array($a->iterator())); |
355
|
|
|
$this->assertInstanceOf(Primitive::class, $b); |
356
|
|
|
$this->assertSame([1, 2, 3, 4], \iterator_to_array($b->iterator())); |
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
public function testReduce() |
360
|
|
|
{ |
361
|
|
|
$sequence = new Primitive('int', 1, 2, 3, 4); |
362
|
|
|
|
363
|
|
|
$this->assertSame(10, $sequence->reduce(0, fn($sum, $i) => $sum + $i)); |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
public function testClear() |
367
|
|
|
{ |
368
|
|
|
$a = new Primitive('int', 1, 2); |
369
|
|
|
$b = $a->clear(); |
370
|
|
|
|
371
|
|
|
$this->assertSame([1, 2], \iterator_to_array($a->iterator())); |
372
|
|
|
$this->assertInstanceOf(Primitive::class, $b); |
373
|
|
|
$this->assertSame([], \iterator_to_array($b->iterator())); |
374
|
|
|
} |
375
|
|
|
|
376
|
|
|
public function testReverse() |
377
|
|
|
{ |
378
|
|
|
$a = new Primitive('int', 1, 2, 3, 4); |
379
|
|
|
$b = $a->reverse(); |
380
|
|
|
|
381
|
|
|
$this->assertSame([1, 2, 3, 4], \iterator_to_array($a->iterator())); |
382
|
|
|
$this->assertInstanceOf(Primitive::class, $b); |
383
|
|
|
$this->assertSame([4, 3, 2, 1], \iterator_to_array($b->iterator())); |
384
|
|
|
} |
385
|
|
|
|
386
|
|
|
public function testEmpty() |
387
|
|
|
{ |
388
|
|
|
$this->assertTrue((new Primitive('int'))->empty()); |
389
|
|
|
$this->assertFalse((new Primitive('int', 1))->empty()); |
390
|
|
|
} |
391
|
|
|
|
392
|
|
|
public function testToSequenceOf() |
393
|
|
|
{ |
394
|
|
|
$sequence = new Primitive('int', 1, 2, 3); |
395
|
|
|
$sequence = $sequence->toSequenceOf('string|int', function($i) { |
396
|
|
|
yield (string) $i; |
397
|
|
|
yield $i; |
398
|
|
|
}); |
399
|
|
|
|
400
|
|
|
$this->assertInstanceOf(Sequence::class, $sequence); |
401
|
|
|
$this->assertSame( |
402
|
|
|
['1', 1, '2', 2, '3', 3], |
403
|
|
|
unwrap($sequence), |
404
|
|
|
); |
405
|
|
|
} |
406
|
|
|
|
407
|
|
|
public function testToSetOf() |
408
|
|
|
{ |
409
|
|
|
$sequence = new Primitive('int', 1, 2, 3); |
410
|
|
|
$set = $sequence->toSetOf('string|int', function($i) { |
411
|
|
|
yield (string) $i; |
412
|
|
|
yield $i; |
413
|
|
|
}); |
414
|
|
|
|
415
|
|
|
$this->assertInstanceOf(Set::class, $set); |
416
|
|
|
$this->assertSame( |
417
|
|
|
['1', 1, '2', 2, '3', 3], |
418
|
|
|
unwrap($set), |
419
|
|
|
); |
420
|
|
|
} |
421
|
|
|
|
422
|
|
|
public function testToMapOf() |
423
|
|
|
{ |
424
|
|
|
$sequence = new Primitive('int', 1, 2, 3); |
425
|
|
|
$map = $sequence->toMapOf('string', 'int', fn($i) => yield (string) $i => $i); |
426
|
|
|
|
427
|
|
|
$this->assertInstanceOf(Map::class, $map); |
428
|
|
|
$this->assertCount(3, $map); |
429
|
|
|
$this->assertSame(1, $map->get('1')); |
430
|
|
|
$this->assertSame(2, $map->get('2')); |
431
|
|
|
$this->assertSame(3, $map->get('3')); |
432
|
|
|
} |
433
|
|
|
} |
434
|
|
|
|
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