GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Issues (201)

tests/SequenceTest.php (10 issues)

1
<?php
2
declare(strict_types = 1);
3
4
namespace Tests\Innmind\Immutable;
5
6
use Innmind\Immutable\{
7
    Sequence,
8
    Str,
9
    Set,
10
    Map,
11
    Monoid\Concat,
12
};
13
use PHPUnit\Framework\TestCase;
14
15
class SequenceTest extends TestCase
16
{
17
    public function testInterface()
18
    {
19
        $sequence = Sequence::of();
20
21
        $this->assertInstanceOf(\Countable::class, $sequence);
22
        $this->assertSame([], $sequence->toList());
23
    }
24
25
    public function testOf()
26
    {
27
        $this->assertTrue(
28
            Sequence::of(1, 2, 3)->equals(
0 ignored issues
show
1 of type integer is incompatible with the type Innmind\Immutable\V expected by parameter $values of Innmind\Immutable\Sequence::of(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

28
            Sequence::of(/** @scrutinizer ignore-type */ 1, 2, 3)->equals(
Loading history...
29
                Sequence::of()
30
                    ->add(1)
31
                    ->add(2)
32
                    ->add(3),
33
            ),
34
        );
35
    }
36
37
    public function testDefer()
38
    {
39
        $loaded = false;
40
        $sequence = Sequence::defer((static function() use (&$loaded) {
41
            yield 1;
42
            yield 2;
43
            yield 3;
44
            $loaded = true;
45
        })());
46
47
        $this->assertInstanceOf(Sequence::class, $sequence);
48
        $this->assertFalse($loaded);
49
        $this->assertSame([1, 2, 3], $sequence->toList());
50
        $this->assertTrue($loaded);
51
    }
52
53
    public function testLazy()
54
    {
55
        $loaded = false;
56
        $sequence = Sequence::lazy(static function() use (&$loaded) {
57
            yield 1;
58
            yield 2;
59
            yield 3;
60
            $loaded = true;
61
        });
62
63
        $this->assertInstanceOf(Sequence::class, $sequence);
64
        $this->assertFalse($loaded);
65
        $this->assertSame([1, 2, 3], $sequence->toList());
66
        $this->assertTrue($loaded);
67
    }
68
69
    public function testMixed()
70
    {
71
        $sequence = Sequence::mixed(1, '2', 3);
72
73
        $this->assertInstanceOf(Sequence::class, $sequence);
74
        $this->assertSame([1, '2', 3], $sequence->toList());
75
    }
76
77
    public function testInts()
78
    {
79
        $sequence = Sequence::ints(1, 2, 3);
80
81
        $this->assertInstanceOf(Sequence::class, $sequence);
82
        $this->assertSame([1, 2, 3], $sequence->toList());
83
    }
84
85
    public function testFloats()
86
    {
87
        $sequence = Sequence::floats(1, 2, 3.2);
88
89
        $this->assertInstanceOf(Sequence::class, $sequence);
90
        $this->assertSame([1.0, 2.0, 3.2], $sequence->toList());
91
    }
92
93
    public function testStrings()
94
    {
95
        $sequence = Sequence::strings('1', '2', '3');
96
97
        $this->assertInstanceOf(Sequence::class, $sequence);
98
        $this->assertSame(['1', '2', '3'], $sequence->toList());
99
    }
100
101
    public function testObjects()
102
    {
103
        $a = new \stdClass;
104
        $b = new \stdClass;
105
        $c = new \stdClass;
106
        $sequence = Sequence::objects($a, $b, $c);
107
108
        $this->assertInstanceOf(Sequence::class, $sequence);
109
        $this->assertSame([$a, $b, $c], $sequence->toList());
110
    }
111
112
    public function testSize()
113
    {
114
        $this->assertSame(
115
            2,
116
            Sequence::of()
117
                ->add(1)
118
                ->add(2)
119
                ->size(),
120
        );
121
    }
122
123
    public function testCount()
124
    {
125
        $this->assertCount(
126
            2,
127
            Sequence::of()
128
                ->add(1)
129
                ->add(2),
130
        );
131
    }
132
133
    public function testGet()
134
    {
135
        $this->assertSame(
136
            1,
137
            $this->get(Sequence::of()->add(1), 0),
138
        );
139
    }
140
141
    public function testReturnNothingWhenGettingUnknownIndex()
142
    {
143
        $this->assertNull($this->get(Sequence::of(), 0));
144
    }
145
146
    public function testDiff()
147
    {
148
        $a = Sequence::of()
149
            ->add(1)
150
            ->add(2)
151
            ->add(3);
152
        $b = Sequence::of()
153
            ->add(3)
154
            ->add(4)
155
            ->add(5);
156
        $c = $a->diff($b);
157
158
        $this->assertInstanceOf(Sequence::class, $c);
159
        $this->assertNotSame($c, $a);
160
        $this->assertNotSame($c, $b);
161
        $this->assertSame([1, 2, 3], $a->toList());
162
        $this->assertSame([3, 4, 5], $b->toList());
163
        $this->assertSame([1, 2], $c->toList());
164
    }
165
166
    public function testDistinct()
167
    {
168
        $a = Sequence::of()
169
            ->add(1)
170
            ->add(1)
171
            ->add(1);
172
        $b = $a->distinct();
173
174
        $this->assertInstanceOf(Sequence::class, $b);
175
        $this->assertNotSame($a, $b);
176
        $this->assertSame([1, 1, 1], $a->toList());
177
        $this->assertSame([1], $b->toList());
178
    }
179
180
    public function testDrop()
181
    {
182
        $a = Sequence::of()
183
            ->add(1)
184
            ->add(3)
185
            ->add(5);
186
        $b = $a->drop(2);
187
188
        $this->assertInstanceOf(Sequence::class, $b);
189
        $this->assertNotSame($a, $b);
190
        $this->assertSame([1, 3, 5], $a->toList());
191
        $this->assertSame([5], $b->toList());
192
    }
193
194
    public function testDropEnd()
195
    {
196
        $a = Sequence::of()
197
            ->add(1)
198
            ->add(3)
199
            ->add(5);
200
        $b = $a->dropEnd(2);
201
202
        $this->assertInstanceOf(Sequence::class, $b);
203
        $this->assertNotSame($a, $b);
204
        $this->assertSame([1, 3, 5], $a->toList());
205
        $this->assertSame([1], $b->toList());
206
    }
207
208
    public function testEquals()
209
    {
210
        $a = Sequence::of()
211
            ->add(1)
212
            ->add(3)
213
            ->add(5);
214
        $b = Sequence::of()
215
            ->add(1)
216
            ->add(5);
217
        $c = Sequence::of()
218
            ->add(1)
219
            ->add(3)
220
            ->add(5);
221
222
        $this->assertTrue($a->equals($c));
223
        $this->assertTrue($a->equals($a));
224
        $this->assertFalse($a->equals($b));
225
    }
226
227
    public function testFilter()
228
    {
229
        $a = Sequence::of()
230
            ->add(1)
231
            ->add(2)
232
            ->add(3)
233
            ->add(4);
234
        $b = $a->filter(static function(int $value): bool {
235
            return $value % 2 === 0;
236
        });
237
238
        $this->assertInstanceOf(Sequence::class, $b);
239
        $this->assertNotSame($a, $b);
240
        $this->assertSame([1, 2, 3, 4], $a->toList());
241
        $this->assertSame([2, 4], $b->toList());
242
    }
243
244
    public function testForeach()
245
    {
246
        $sum = 0;
247
        $sequence = Sequence::of()
0 ignored issues
show
The assignment to $sequence is dead and can be removed.
Loading history...
248
            ->add(1)
249
            ->add(2)
250
            ->add(3)
251
            ->add(4)
252
            ->foreach(static function(int $value) use (&$sum) {
253
                $sum += $value;
254
            });
255
256
        $this->assertSame(10, $sum);
257
    }
258
259
    public function testGroupBy()
260
    {
261
        $sequence = Sequence::of()
262
            ->add(1)
263
            ->add(2)
264
            ->add(3)
265
            ->add(4);
266
        $map = $sequence->groupBy(static function(int $value): int {
267
            return $value % 3;
268
        });
269
270
        $this->assertInstanceOf(Map::class, $map);
271
        $this->assertCount(3, $map);
272
        $this->assertSame([3], $this->get($map, 0)->toList());
273
        $this->assertSame([1, 4], $this->get($map, 1)->toList());
274
        $this->assertSame([2], $this->get($map, 2)->toList());
275
    }
276
277
    public function testGroupEmptySequence()
278
    {
279
        $this->assertTrue(
280
            Sequence::of()
281
                ->groupBy(static function() {})
282
                ->equals(Map::of()),
283
        );
284
    }
285
286
    public function testFirst()
287
    {
288
        $sequence = Sequence::of()
289
            ->add(1)
290
            ->add(2)
291
            ->add(3)
292
            ->add(4);
293
294
        $this->assertSame(
295
            1,
296
            $sequence->first()->match(
297
                static fn($value) => $value,
298
                static fn() => null,
299
            ),
300
        );
301
    }
302
303
    public function testLast()
304
    {
305
        $sequence = Sequence::of()
306
            ->add(1)
307
            ->add(2)
308
            ->add(3)
309
            ->add(4);
310
311
        $this->assertSame(
312
            4,
313
            $sequence->last()->match(
314
                static fn($value) => $value,
315
                static fn() => null,
316
            ),
317
        );
318
    }
319
320
    public function testContains()
321
    {
322
        $sequence = Sequence::of()
323
            ->add(1)
324
            ->add(2)
325
            ->add(3)
326
            ->add(4);
327
328
        $this->assertTrue($sequence->contains(2));
329
        $this->assertFalse($sequence->contains(5));
330
    }
331
332
    public function testIndexOf()
333
    {
334
        $sequence = Sequence::of()
335
            ->add(1)
336
            ->add(2)
337
            ->add(3)
338
            ->add(4);
339
340
        $this->assertSame(
341
            0,
342
            $sequence->indexOf(1)->match(
343
                static fn($value) => $value,
344
                static fn() => null,
345
            ),
346
        );
347
        $this->assertSame(
348
            3,
349
            $sequence->indexOf(4)->match(
350
                static fn($value) => $value,
351
                static fn() => null,
352
            ),
353
        );
354
    }
355
356
    public function testIndices()
357
    {
358
        $sequence = Sequence::of()
359
            ->add(1)
360
            ->add(2)
361
            ->add(3)
362
            ->add(4);
363
        $indices = $sequence->indices();
364
365
        $this->assertInstanceOf(Sequence::class, $indices);
366
        $this->assertSame([0, 1, 2, 3], $indices->toList());
367
    }
368
369
    public function testEmptyIndices()
370
    {
371
        $sequence = Sequence::of();
372
        $indices = $sequence->indices();
373
374
        $this->assertInstanceOf(Sequence::class, $indices);
375
        $this->assertSame([], $indices->toList());
376
    }
377
378
    public function testMap()
379
    {
380
        $a = Sequence::of()
381
            ->add(1)
382
            ->add(2)
383
            ->add(3)
384
            ->add(4);
385
        $b = $a->map(static function(int $value): int {
386
            return $value**2;
387
        });
388
389
        $this->assertInstanceOf(Sequence::class, $b);
390
        $this->assertNotSame($a, $b);
391
        $this->assertSame([1, 2, 3, 4], $a->toList());
392
        $this->assertSame([1, 4, 9, 16], $b->toList());
393
    }
394
395
    public function testFlatMap()
396
    {
397
        $sequence = Sequence::of(1, 2, 3, 4);
0 ignored issues
show
1 of type integer is incompatible with the type Innmind\Immutable\V expected by parameter $values of Innmind\Immutable\Sequence::of(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

397
        $sequence = Sequence::of(/** @scrutinizer ignore-type */ 1, 2, 3, 4);
Loading history...
398
        $sequence2 = $sequence->flatMap(static fn($i) => Sequence::of($i, $i));
399
400
        $this->assertNotSame($sequence, $sequence2);
401
        $this->assertSame([1, 2, 3, 4], $sequence->toList());
402
        $this->assertSame([1, 1, 2, 2, 3, 3, 4, 4], $sequence2->toList());
403
    }
404
405
    public function testLazyFlatMap()
406
    {
407
        $loaded = false;
408
        $a = Sequence::lazy(static function() use (&$loaded) {
409
            yield 1;
410
            yield 2;
411
            yield 3;
412
            yield 4;
413
            $loaded = true;
414
        });
415
        $b = $a->flatMap(static fn($i) => Sequence::of($i, $i*2));
0 ignored issues
show
$i * 2 of type integer is incompatible with the type Innmind\Immutable\V expected by parameter $values of Innmind\Immutable\Sequence::of(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

415
        $b = $a->flatMap(static fn($i) => Sequence::of($i, /** @scrutinizer ignore-type */ $i*2));
Loading history...
416
417
        $this->assertFalse($loaded);
418
        $this->assertSame([1, 2, 2, 4, 3, 6, 4, 8], $b->toList());
419
        $this->assertTrue($loaded);
420
        $this->assertSame([1, 2, 3, 4], $a->toList());
421
    }
422
423
    public function testDeferFlatMap()
424
    {
425
        $loaded = false;
426
        $a = Sequence::defer((static function() use (&$loaded) {
427
            yield 1;
428
            yield 2;
429
            yield 3;
430
            yield 4;
431
            $loaded = true;
432
        })());
433
        $b = $a->flatMap(static fn($i) => Sequence::of($i, $i*2));
0 ignored issues
show
$i * 2 of type integer is incompatible with the type Innmind\Immutable\V expected by parameter $values of Innmind\Immutable\Sequence::of(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

433
        $b = $a->flatMap(static fn($i) => Sequence::of($i, /** @scrutinizer ignore-type */ $i*2));
Loading history...
434
435
        $this->assertFalse($loaded);
436
        $this->assertSame([1, 2, 2, 4, 3, 6, 4, 8], $b->toList());
437
        $this->assertTrue($loaded);
438
        $this->assertSame([1, 2, 3, 4], $a->toList());
439
    }
440
441
    public function testPad()
442
    {
443
        $a = Sequence::of()
444
            ->add(1)
445
            ->add(2);
446
        $b = $a->pad(4, 0);
447
448
        $this->assertInstanceOf(Sequence::class, $b);
449
        $this->assertNotSame($a, $b);
450
        $this->assertSame([1, 2], $a->toList());
451
        $this->assertSame([1, 2, 0, 0], $b->toList());
452
    }
453
454
    public function testPartition()
455
    {
456
        $map = Sequence::of()
457
            ->add(1)
458
            ->add(2)
459
            ->add(3)
460
            ->add(4)
461
            ->partition(static function(int $value): bool {
462
                return $value % 2 === 0;
463
            });
464
465
        $this->assertInstanceOf(Map::class, $map);
466
        $this->assertSame([2, 4], $this->get($map, true)->toList());
467
        $this->assertSame([1, 3], $this->get($map, false)->toList());
468
    }
469
470
    public function testSlice()
471
    {
472
        $a = Sequence::of()
473
            ->add(1)
474
            ->add(2)
475
            ->add(3)
476
            ->add(4);
477
        $b = $a->slice(1, 3);
478
479
        $this->assertInstanceOf(Sequence::class, $b);
480
        $this->assertNotSame($a, $b);
481
        $this->assertSame([1, 2, 3, 4], $a->toList());
482
        $this->assertSame([2, 3], $b->toList());
483
    }
484
485
    public function testTake()
486
    {
487
        $a = Sequence::of()
488
            ->add(1)
489
            ->add(2)
490
            ->add(3)
491
            ->add(4);
492
        $b = $a->take(2);
493
494
        $this->assertInstanceOf(Sequence::class, $b);
495
        $this->assertNotSame($a, $b);
496
        $this->assertSame([1, 2, 3, 4], $a->toList());
497
        $this->assertSame([1, 2], $b->toList());
498
    }
499
500
    public function testTakeEnd()
501
    {
502
        $a = Sequence::of()
503
            ->add(1)
504
            ->add(2)
505
            ->add(3)
506
            ->add(4);
507
        $b = $a->takeEnd(2);
508
509
        $this->assertInstanceOf(Sequence::class, $b);
510
        $this->assertNotSame($a, $b);
511
        $this->assertSame([1, 2, 3, 4], $a->toList());
512
        $this->assertSame([3, 4], $b->toList());
513
    }
514
515
    public function testAppend()
516
    {
517
        $a = Sequence::of()
518
            ->add(1)
519
            ->add(2);
520
        $b = Sequence::of()
521
            ->add(3)
522
            ->add(4);
523
        $c = $b->append($a);
524
525
        $this->assertInstanceOf(Sequence::class, $c);
526
        $this->assertNotSame($c, $a);
527
        $this->assertNotSame($c, $b);
528
        $this->assertSame([1, 2], $a->toList());
529
        $this->assertSame([3, 4], $b->toList());
530
        $this->assertSame([3, 4, 1, 2], $c->toList());
531
    }
532
533
    public function testIntersect()
534
    {
535
        $a = Sequence::of()
536
            ->add(1)
537
            ->add(2);
538
        $b = Sequence::of()
539
            ->add(2)
540
            ->add(3);
541
        $c = $b->intersect($a);
542
543
        $this->assertInstanceOf(Sequence::class, $c);
544
        $this->assertNotSame($c, $a);
545
        $this->assertNotSame($c, $b);
546
        $this->assertSame([1, 2], $a->toList());
547
        $this->assertSame([2, 3], $b->toList());
548
        $this->assertSame([2], $c->toList());
549
    }
550
551
    public function testAdd()
552
    {
553
        $a = Sequence::of();
554
        $b = $a->add(1);
555
556
        $this->assertInstanceOf(Sequence::class, $b);
557
        $this->assertNotSame($a, $b);
558
        $this->assertSame([], $a->toList());
559
        $this->assertSame([1], $b->toList());
560
561
        $this->assertSame(
562
            [1, 2, 3],
563
            Sequence::ints(1)(2)(3)->toList(),
564
        );
565
    }
566
567
    public function testSort()
568
    {
569
        $a = Sequence::of()
570
            ->add(1)
571
            ->add(2)
572
            ->add(3)
573
            ->add(3)
574
            ->add(4);
575
        $b = $a->sort(static function(int $a, int $b): int {
576
            return ($b > $a) ? 1 : -1;
577
        });
578
579
        $this->assertInstanceOf(Sequence::class, $b);
580
        $this->assertNotSame($a, $b);
581
        $this->assertSame([1, 2, 3, 3, 4], $a->toList());
582
        $this->assertSame([4, 3, 3, 2, 1], $b->toList());
583
    }
584
585
    public function testReduce()
586
    {
587
        $value = Sequence::of()
588
            ->add(1)
589
            ->add(2)
590
            ->add(3)
591
            ->add(4)
592
            ->reduce(
593
                0,
0 ignored issues
show
0 of type integer is incompatible with the type Innmind\Immutable\R expected by parameter $carry of Innmind\Immutable\Sequence::reduce(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

593
                /** @scrutinizer ignore-type */ 0,
Loading history...
594
                static function(int $carry, int $value): int {
595
                    return $carry + $value;
596
                },
597
            );
598
599
        $this->assertSame(10, $value);
600
    }
601
602
    public function testClear()
603
    {
604
        $sequence = Sequence::of()
605
            ->add(1)
606
            ->add(2)
607
            ->add(3);
608
        $sequence2 = $sequence->clear();
609
610
        $this->assertNotSame($sequence, $sequence2);
611
        $this->assertSame([1, 2, 3], $sequence->toList());
612
        $this->assertSame([], $sequence2->toList());
613
    }
614
615
    public function testReverse()
616
    {
617
        $sequence = Sequence::of()
618
            ->add(1)
619
            ->add(3)
620
            ->add(4)
621
            ->add(2);
622
        $reverse = $sequence->reverse();
623
624
        $this->assertInstanceOf(Sequence::class, $reverse);
625
        $this->assertNotSame($sequence, $reverse);
626
        $this->assertSame([1, 3, 4, 2], $sequence->toList());
627
        $this->assertSame([2, 4, 3, 1], $reverse->toList());
628
    }
629
630
    public function testEmpty()
631
    {
632
        $this->assertTrue(Sequence::of()->empty());
633
        $this->assertFalse(Sequence::of(1)->empty());
0 ignored issues
show
1 of type integer is incompatible with the type Innmind\Immutable\V expected by parameter $values of Innmind\Immutable\Sequence::of(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

633
        $this->assertFalse(Sequence::of(/** @scrutinizer ignore-type */ 1)->empty());
Loading history...
634
    }
635
636
    public function testToList()
637
    {
638
        $this->assertSame(
639
            [1, 2, 3],
640
            Sequence::ints(1, 2, 3)->toList(),
641
        );
642
    }
643
644
    public function testFind()
645
    {
646
        $sequence = Sequence::ints(1, 2, 3);
647
648
        $this->assertSame(
649
            1,
650
            $sequence->find(static fn($i) => $i === 1)->match(
651
                static fn($i) => $i,
652
                static fn() => null,
653
            ),
654
        );
655
        $this->assertSame(
656
            2,
657
            $sequence->find(static fn($i) => $i === 2)->match(
658
                static fn($i) => $i,
659
                static fn() => null,
660
            ),
661
        );
662
        $this->assertSame(
663
            3,
664
            $sequence->find(static fn($i) => $i === 3)->match(
665
                static fn($i) => $i,
666
                static fn() => null,
667
            ),
668
        );
669
670
        $this->assertNull(
671
            $sequence->find(static fn($i) => $i === 0)->match(
672
                static fn($i) => $i,
673
                static fn() => null,
674
            ),
675
        );
676
    }
677
678
    public function testMatches()
679
    {
680
        $sequence = Sequence::ints(1, 2, 3);
681
682
        $this->assertTrue($sequence->matches(static fn($i) => $i % 1 === 0));
683
        $this->assertFalse($sequence->matches(static fn($i) => $i % 2 === 0));
684
    }
685
686
    public function testAny()
687
    {
688
        $sequence = Sequence::ints(1, 2, 3);
689
690
        $this->assertTrue($sequence->any(static fn($i) => $i === 2));
691
        $this->assertFalse($sequence->any(static fn($i) => $i === 0));
692
    }
693
694
    public function testPossibilityToCleanupResourcesWhenGeneratorStoppedBeforeEnd()
695
    {
696
        $cleanupCalled = false;
697
        $endReached = false;
698
        $started = 0;
699
        $sequence = Sequence::lazy(static function($registerCleanup) use (&$cleanupCalled, &$endReached, &$started) {
700
            ++$started;
701
            $file = \fopen(__FILE__, 'r');
702
            $registerCleanup(static function() use ($file, &$cleanupCalled) {
703
                \fclose($file);
704
                $cleanupCalled = true;
705
            });
706
707
            while (!\feof($file)) {
708
                $line = \fgets($file);
709
710
                yield $line;
711
            }
712
713
            $endReached = true;
714
            \fclose($file);
715
        });
716
717
        $line = $sequence
718
            ->map(static fn($line) => \trim($line))
719
            ->filter(static fn($line) => $line !== '')
720
            ->find(static fn($line) => \substr($line, -2) === '()')
721
            ->match(
722
                static fn($line) => $line,
723
                static fn() => null,
724
            );
725
726
        $this->assertSame('public function testInterface()', $line);
727
        $this->assertSame(1, $started);
728
        $this->assertTrue($cleanupCalled);
729
        $this->assertFalse($endReached);
730
    }
731
732
    public function testCleanupIsNotCalledWhenReachingTheEndOfTheGenerator()
733
    {
734
        $cleanupCalled = false;
735
        $endReached = false;
736
        $started = 0;
737
        $sequence = Sequence::lazy(static function($registerCleanup) use (&$cleanupCalled, &$endReached, &$started) {
738
            ++$started;
739
            $file = \fopen(__FILE__, 'r');
740
            $registerCleanup(static function() use ($file, &$cleanupCalled) {
741
                \fclose($file);
742
                $cleanupCalled = true;
743
            });
744
745
            while (!\feof($file)) {
746
                $line = \fgets($file);
747
748
                yield $line;
749
            }
750
751
            $endReached = true;
752
            \fclose($file);
753
        });
754
755
        $line = $sequence
756
            ->filter(static fn($line) => \is_string($line))
757
            ->map(static fn($line) => \trim($line))
758
            ->filter(static fn($line) => $line !== '')
759
            ->find(static fn($line) => $line === 'unknown')
760
            ->match(
761
                static fn($line) => $line,
762
                static fn() => null,
763
            );
764
765
        $this->assertNull($line);
766
        $this->assertSame(1, $started);
767
        $this->assertFalse($cleanupCalled);
768
        $this->assertTrue($endReached);
769
    }
770
771
    public function testMatch()
772
    {
773
        $sequence = Sequence::of(1, 2, 3, 4);
0 ignored issues
show
1 of type integer is incompatible with the type Innmind\Immutable\V expected by parameter $values of Innmind\Immutable\Sequence::of(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

773
        $sequence = Sequence::of(/** @scrutinizer ignore-type */ 1, 2, 3, 4);
Loading history...
774
        [$head, $tail] = $sequence->match(
775
            static fn($head, $tail) => [$head, $tail],
776
            static fn() => [null, null],
777
        );
778
779
        $this->assertSame(1, $head);
780
        $this->assertTrue($tail->equals(Sequence::of(2, 3, 4)));
781
        $this->assertSame([1, 2, 3, 4], $sequence->toList());
782
    }
783
784
    public function testDeferredMatch()
785
    {
786
        $sequence = Sequence::defer((static function() {
787
            yield 1;
788
            yield 2;
789
            yield 3;
790
            yield 4;
791
        })());
792
        [$head, $tail] = $sequence->match(
793
            static fn($head, $tail) => [$head, $tail],
794
            static fn() => [null, null],
795
        );
796
797
        $this->assertSame(1, $head);
798
        $this->assertTrue($tail->equals(Sequence::of(2, 3, 4)));
0 ignored issues
show
2 of type integer is incompatible with the type Innmind\Immutable\V expected by parameter $values of Innmind\Immutable\Sequence::of(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

798
        $this->assertTrue($tail->equals(Sequence::of(/** @scrutinizer ignore-type */ 2, 3, 4)));
Loading history...
799
        $this->assertSame([1, 2, 3, 4], $sequence->toList());
800
    }
801
802
    public function testLazyMatch()
803
    {
804
        $started = 0;
805
        $sequence = Sequence::lazy(static function() use (&$started) {
806
            ++$started;
807
            yield 1;
808
            yield 2;
809
            yield 3;
810
            yield 4;
811
        });
812
        [$head, $tail] = $sequence->match(
813
            static fn($head, $tail) => [$head, $tail],
814
            static fn() => [null, null],
815
        );
816
817
        $this->assertSame(1, $head);
818
        $this->assertTrue($tail->equals(Sequence::of(2, 3, 4)));
0 ignored issues
show
2 of type integer is incompatible with the type Innmind\Immutable\V expected by parameter $values of Innmind\Immutable\Sequence::of(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

818
        $this->assertTrue($tail->equals(Sequence::of(/** @scrutinizer ignore-type */ 2, 3, 4)));
Loading history...
819
        $this->assertSame(1, $started);
820
        $this->assertSame([1, 2, 3, 4], $sequence->toList());
821
    }
822
823
    public function testFold()
824
    {
825
        $str = Sequence::of(Str::of('foo'), Str::of('bar'), Str::of('baz'))->fold(new Concat);
826
827
        $this->assertInstanceOf(Str::class, $str);
828
        $this->assertSame('foobarbaz', $str->toString());
829
830
        $str = Sequence::of(Str::of('baz'), Str::of('foo'), Str::of('bar'))->fold(new Concat);
831
832
        $this->assertInstanceOf(Str::class, $str);
833
        $this->assertSame('bazfoobar', $str->toString());
834
    }
835
836
    public function get($map, $index)
837
    {
838
        return $map->get($index)->match(
839
            static fn($value) => $value,
840
            static fn() => null,
841
        );
842
    }
843
}
844