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.

LazyTest::testInterface()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 3
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 5
rs 10
1
<?php
2
declare(strict_types = 1);
3
4
namespace Tests\Innmind\Immutable\Sequence;
5
6
use Innmind\Immutable\{
7
    Sequence\Lazy,
8
    Sequence\Implementation,
9
    Map,
10
    Sequence,
11
    Str,
12
    Set,
13
    SideEffect,
14
};
15
use PHPUnit\Framework\TestCase;
16
17
class LazyTest extends TestCase
18
{
19
    public function testInterface()
20
    {
21
        $this->assertInstanceOf(
22
            Implementation::class,
23
            new Lazy(static fn() => yield),
24
        );
25
    }
26
27
    public function testGeneratorNotLoadedAtInstanciation()
28
    {
29
        $loaded = false;
30
        $sequence = new Lazy(static function() use (&$loaded) {
0 ignored issues
show
Unused Code introduced by
The assignment to $sequence is dead and can be removed.
Loading history...
31
            yield 1;
32
            $loaded = true;
33
        });
34
35
        $this->assertFalse($loaded);
36
    }
37
38
    public function testSize()
39
    {
40
        $sequence = new Lazy(static function() {
41
            yield 1;
42
            yield 1;
43
        });
44
45
        $this->assertSame(2, $sequence->size());
46
        $this->assertSame(2, $sequence->count());
47
    }
48
49
    public function testIterator()
50
    {
51
        $sequence = new Lazy(static function() {
52
            yield 1;
53
            yield 2;
54
            yield 3;
55
        });
56
57
        $this->assertSame(
58
            [1, 2, 3],
59
            \iterator_to_array($sequence->iterator()),
60
        );
61
    }
62
63
    public function testGet()
64
    {
65
        $sequence = new Lazy(static function() {
66
            yield 1;
67
            yield 42;
68
            yield 3;
69
        });
70
71
        $this->assertSame(42, $this->get($sequence, 1));
72
    }
73
74
    public function testReturnNothingWhenIndexNotFound()
75
    {
76
        $sequence = new Lazy(static function() {
77
            if (false) {
78
                yield 1;
79
            }
80
        });
81
82
        $this->assertNull(
83
            $sequence->get(0)->match(
84
                static fn($value) => $value,
85
                static fn() => null,
86
            ),
87
        );
88
    }
89
90
    public function testDiff()
91
    {
92
        $aLoaded = false;
93
        $bLoaded = false;
94
        $a = new Lazy(static function() use (&$aLoaded) {
95
            yield 1;
96
            yield 2;
97
            $aLoaded = true;
98
        });
99
        $b = new Lazy(static function() use (&$bLoaded) {
100
            yield 2;
101
            yield 3;
102
            $bLoaded = true;
103
        });
104
        $c = $a->diff($b);
105
106
        $this->assertFalse($aLoaded);
107
        $this->assertFalse($bLoaded);
108
        $this->assertSame([1, 2], \iterator_to_array($a->iterator()));
109
        $this->assertSame([2, 3], \iterator_to_array($b->iterator()));
110
        $this->assertInstanceOf(Lazy::class, $c);
111
        $this->assertSame([1], \iterator_to_array($c->iterator()));
112
        $this->assertTrue($aLoaded);
113
        $this->assertTrue($bLoaded);
114
    }
115
116
    public function testDistinct()
117
    {
118
        $loaded = false;
119
        $a = new Lazy(static function() use (&$loaded) {
120
            yield 1;
121
            yield 2;
122
            yield 1;
123
            $loaded = true;
124
        });
125
        $b = $a->distinct();
126
127
        $this->assertFalse($loaded);
128
        $this->assertSame([1, 2, 1], \iterator_to_array($a->iterator()));
129
        $this->assertInstanceOf(Lazy::class, $b);
130
        $this->assertSame([1, 2], \iterator_to_array($b->iterator()));
131
        $this->assertTrue($loaded);
132
    }
133
134
    public function testDrop()
135
    {
136
        $loaded = false;
137
        $a = new Lazy(static function() use (&$loaded) {
138
            yield 1;
139
            yield 2;
140
            yield 3;
141
            yield 4;
142
            $loaded = true;
143
        });
144
        $b = $a->drop(2);
145
146
        $this->assertFalse($loaded);
147
        $this->assertSame([1, 2, 3, 4], \iterator_to_array($a->iterator()));
148
        $this->assertInstanceOf(Lazy::class, $b);
149
        $this->assertSame([3, 4], \iterator_to_array($b->iterator()));
150
        $this->assertTrue($loaded);
151
    }
152
153
    public function testDropEnd()
154
    {
155
        $a = new Lazy(static function() {
156
            yield 1;
157
            yield 2;
158
            yield 3;
159
            yield 4;
160
        });
161
        $b = $a->dropEnd(2);
162
163
        $this->assertSame([1, 2, 3, 4], \iterator_to_array($a->iterator()));
164
        $this->assertInstanceOf(Implementation::class, $b);
165
        $this->assertSame([1, 2], \iterator_to_array($b->iterator()));
166
    }
167
168
    public function testEquals()
169
    {
170
        $a = new Lazy(static function() {
171
            yield 1;
172
            yield 2;
173
        });
174
        $b = new Lazy(static function() {
175
            yield 1;
176
            yield 2;
177
            yield 3;
178
        });
179
180
        $this->assertTrue($a->equals($a));
181
        $this->assertFalse($a->equals($b));
182
    }
183
184
    public function testFilter()
185
    {
186
        $loaded = false;
187
        $a = new Lazy(static function() use (&$loaded) {
188
            yield 1;
189
            yield 2;
190
            yield 3;
191
            yield 4;
192
            $loaded = true;
193
        });
194
        $b = $a->filter(static fn($i) => $i % 2 === 0);
195
196
        $this->assertFalse($loaded);
197
        $this->assertSame([1, 2, 3, 4], \iterator_to_array($a->iterator()));
198
        $this->assertInstanceOf(Lazy::class, $b);
199
        $this->assertSame([2, 4], \iterator_to_array($b->iterator()));
200
        $this->assertTrue($loaded);
201
    }
202
203
    public function testForeach()
204
    {
205
        $sequence = new Lazy(static function() {
206
            yield 1;
207
            yield 2;
208
            yield 3;
209
            yield 4;
210
        });
211
        $calls = 0;
212
        $sum = 0;
213
214
        $this->assertInstanceOf(
215
            SideEffect::class,
216
            $sequence->foreach(static function($i) use (&$calls, &$sum) {
217
                ++$calls;
218
                $sum += $i;
219
            }),
220
        );
221
        $this->assertSame(4, $calls);
222
        $this->assertSame(10, $sum);
223
    }
224
225
    public function testGroupEmptySequence()
226
    {
227
        $sequence = new Lazy(static function() {
228
            if (false) {
229
                yield 1;
230
            }
231
        });
232
233
        $this->assertTrue(
234
            $sequence
235
                ->groupBy(static fn($i) => $i)
236
                ->equals(Map::of()),
237
        );
238
    }
239
240
    public function testGroupBy()
241
    {
242
        $sequence = new Lazy(static function() {
243
            yield 1;
244
            yield 2;
245
            yield 3;
246
            yield 4;
247
        });
248
        $groups = $sequence->groupBy(static fn($i) => $i % 2);
249
250
        $this->assertSame([1, 2, 3, 4], \iterator_to_array($sequence->iterator()));
251
        $this->assertInstanceOf(Map::class, $groups);
252
        $this->assertCount(2, $groups);
253
        $this->assertSame([2, 4], $this->get($groups, 0)->toList());
254
        $this->assertSame([1, 3], $this->get($groups, 1)->toList());
255
    }
256
257
    public function testReturnNothingWhenTryingToAccessFirstElementOnEmptySequence()
258
    {
259
        $sequence = new Lazy(static function() {
260
            if (false) {
261
                yield 1;
262
            }
263
        });
264
265
        $this->assertNull(
266
            $sequence->first()->match(
267
                static fn($value) => $value,
268
                static fn() => null,
269
            ),
270
        );
271
    }
272
273
    public function testReturnNothingWhenTryingToAccessLastElementOnEmptySequence()
274
    {
275
        $sequence = new Lazy(static function() {
276
            if (false) {
277
                yield 1;
278
            }
279
        });
280
281
        $this->assertNull(
282
            $sequence->last()->match(
283
                static fn($value) => $value,
284
                static fn() => null,
285
            ),
286
        );
287
    }
288
289
    public function testFirst()
290
    {
291
        $sequence = new Lazy(static function() {
292
            yield 2;
293
            yield 3;
294
            yield 4;
295
        });
296
297
        $this->assertSame(
298
            2,
299
            $sequence->first()->match(
300
                static fn($value) => $value,
301
                static fn() => null,
302
            ),
303
        );
304
    }
305
306
    public function testLast()
307
    {
308
        $sequence = new Lazy(static function() {
309
            yield 1;
310
            yield 2;
311
            yield 3;
312
        });
313
314
        $this->assertSame(
315
            3,
316
            $sequence->last()->match(
317
                static fn($value) => $value,
318
                static fn() => null,
319
            ),
320
        );
321
    }
322
323
    public function testContains()
324
    {
325
        $sequence = new Lazy(static function() {
326
            yield 1;
327
            yield 2;
328
            yield 3;
329
        });
330
331
        $this->assertTrue($sequence->contains(2));
0 ignored issues
show
Bug introduced by
2 of type integer is incompatible with the type Innmind\Immutable\Sequence\T expected by parameter $element of Innmind\Immutable\Sequence\Lazy::contains(). ( Ignorable by Annotation )

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

331
        $this->assertTrue($sequence->contains(/** @scrutinizer ignore-type */ 2));
Loading history...
332
        $this->assertFalse($sequence->contains(4));
333
    }
334
335
    public function testIndexOf()
336
    {
337
        $sequence = new Lazy(static function() {
338
            yield 1;
339
            yield 2;
340
            yield 4;
341
        });
342
343
        $this->assertSame(
344
            1,
345
            $sequence->indexOf(2)->match(
0 ignored issues
show
Bug introduced by
2 of type integer is incompatible with the type Innmind\Immutable\Sequence\T expected by parameter $element of Innmind\Immutable\Sequence\Lazy::indexOf(). ( Ignorable by Annotation )

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

345
            $sequence->indexOf(/** @scrutinizer ignore-type */ 2)->match(
Loading history...
346
                static fn($value) => $value,
347
                static fn() => null,
348
            ),
349
        );
350
        $this->assertSame(
351
            2,
352
            $sequence->indexOf(4)->match(
353
                static fn($value) => $value,
354
                static fn() => null,
355
            ),
356
        );
357
    }
358
359
    public function testReturnNothingWhenTryingToAccessIndexOfUnknownValue()
360
    {
361
        $sequence = new Lazy(static function() {
362
            if (false) {
363
                yield 1;
364
            }
365
        });
366
367
        $this->assertNull(
368
            $sequence->indexOf(1)->match(
0 ignored issues
show
Bug introduced by
1 of type integer is incompatible with the type Innmind\Immutable\Sequence\T expected by parameter $element of Innmind\Immutable\Sequence\Lazy::indexOf(). ( Ignorable by Annotation )

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

368
            $sequence->indexOf(/** @scrutinizer ignore-type */ 1)->match(
Loading history...
369
                static fn($value) => $value,
370
                static fn() => null,
371
            ),
372
        );
373
    }
374
375
    public function testIndices()
376
    {
377
        $loaded = false;
378
        $a = new Lazy(static function() use (&$loaded) {
379
            yield '1';
380
            yield '2';
381
            $loaded = true;
382
        });
383
        $b = $a->indices();
384
385
        $this->assertFalse($loaded);
386
        $this->assertSame(['1', '2'], \iterator_to_array($a->iterator()));
387
        $this->assertInstanceOf(Lazy::class, $b);
388
        $this->assertSame([0, 1], \iterator_to_array($b->iterator()));
389
        $this->assertTrue($loaded);
390
    }
391
392
    public function testIndicesOnEmptySequence()
393
    {
394
        $a = new Lazy(static function() {
395
            if (false) {
396
                yield 1;
397
            }
398
        });
399
        $b = $a->indices();
400
401
        $this->assertSame([], \iterator_to_array($a->iterator()));
402
        $this->assertInstanceOf(Lazy::class, $b);
403
        $this->assertSame([], \iterator_to_array($b->iterator()));
404
    }
405
406
    public function testMap()
407
    {
408
        $loaded = false;
409
        $a = new Lazy(static function() use (&$loaded) {
410
            yield 1;
411
            yield 2;
412
            yield 3;
413
            $loaded = true;
414
        });
415
        $b = $a->map(static fn($i) => $i * 2);
416
417
        $this->assertFalse($loaded);
418
        $this->assertSame([1, 2, 3], \iterator_to_array($a->iterator()));
419
        $this->assertInstanceOf(Lazy::class, $b);
420
        $this->assertSame([2, 4, 6], \iterator_to_array($b->iterator()));
421
        $this->assertTrue($loaded);
422
    }
423
424
    public function testPad()
425
    {
426
        $loaded = false;
427
        $a = new Lazy(static function() use (&$loaded) {
428
            yield 1;
429
            yield 2;
430
            $loaded = true;
431
        });
432
        $b = $a->pad(4, 0);
0 ignored issues
show
Bug introduced by
0 of type integer is incompatible with the type Innmind\Immutable\Sequence\T expected by parameter $element of Innmind\Immutable\Sequence\Lazy::pad(). ( Ignorable by Annotation )

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

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

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

621
        $this->assertSame(10, $sequence->reduce(/** @scrutinizer ignore-type */ 0, static fn($sum, $i) => $sum + $i));
Loading history...
622
    }
623
624
    public function testClear()
625
    {
626
        $loaded = false;
627
        $a = new Lazy(static function() use (&$loaded) {
628
            yield 1;
629
            yield 2;
630
            $loaded = true;
631
        });
632
        $b = $a->clear();
633
634
        $this->assertFalse($loaded);
635
        $this->assertInstanceOf(Implementation::class, $b);
636
        $this->assertSame([], \iterator_to_array($b->iterator()));
637
        $this->assertFalse($loaded);
638
        $this->assertSame([1, 2], \iterator_to_array($a->iterator()));
639
    }
640
641
    public function testReverse()
642
    {
643
        $loaded = false;
644
        $a = new Lazy(static function() use (&$loaded) {
645
            yield 1;
646
            yield 2;
647
            yield 3;
648
            yield 4;
649
            $loaded = true;
650
        });
651
        $b = $a->reverse();
652
653
        $this->assertFalse($loaded);
654
        $this->assertSame([1, 2, 3, 4], \iterator_to_array($a->iterator()));
655
        $this->assertInstanceOf(Lazy::class, $b);
656
        $this->assertSame([4, 3, 2, 1], \iterator_to_array($b->iterator()));
657
        $this->assertTrue($loaded);
658
    }
659
660
    public function testEmpty()
661
    {
662
        $aLoaded = false;
663
        $bLoaded = false;
664
        $a = new Lazy(static function() use (&$aLoaded) {
665
            yield 1;
666
            $aLoaded = true;
667
        });
668
        $b = new Lazy(static function() use (&$bLoaded) {
669
            if (false) {
670
                yield 1;
671
            }
672
            $bLoaded = true;
673
        });
674
675
        $this->assertFalse($aLoaded);
676
        $this->assertFalse($bLoaded);
677
        $this->assertTrue($b->empty());
678
        $this->assertFalse($a->empty());
679
        $this->assertFalse($aLoaded); // still false as we don't need to load the full iterator to know if it's empty
680
        $this->assertTrue($bLoaded);
681
    }
682
683
    public function testFind()
684
    {
685
        $count = 0;
686
        $sequence = new Lazy(static function() use (&$count) {
687
            ++$count;
688
            yield 1;
689
            ++$count;
690
            yield 2;
691
            ++$count;
692
            yield 3;
693
        });
694
695
        $this->assertSame(
696
            1,
697
            $sequence->find(static fn($i) => $i === 1)->match(
698
                static fn($i) => $i,
699
                static fn() => null,
700
            ),
701
        );
702
        $this->assertSame(1, $count);
703
        $this->assertSame(
704
            2,
705
            $sequence->find(static fn($i) => $i === 2)->match(
706
                static fn($i) => $i,
707
                static fn() => null,
708
            ),
709
        );
710
        $this->assertSame(3, $count);
711
        $this->assertSame(
712
            3,
713
            $sequence->find(static fn($i) => $i === 3)->match(
714
                static fn($i) => $i,
715
                static fn() => null,
716
            ),
717
        );
718
        $this->assertSame(6, $count);
719
720
        $this->assertNull(
721
            $sequence->find(static fn($i) => $i === 0)->match(
722
                static fn($i) => $i,
723
                static fn() => null,
724
            ),
725
        );
726
    }
727
728
    public function testCallCleanupWhenGettingIndexBeforeTheEndOfGenerator()
729
    {
730
        $cleanupCalled = false;
731
        $sequence = new Lazy(static function($registerCleanup) use (&$cleanupCalled) {
732
            $registerCleanup(static function() use (&$cleanupCalled) {
733
                $cleanupCalled = true;
734
            });
735
            yield 2;
736
            yield 3;
737
            yield 4;
738
            yield 5;
739
        });
740
        $this->assertFalse($cleanupCalled);
741
        $value = $sequence->get(1)->match(
742
            static fn($value) => $value,
743
            static fn() => null,
744
        );
745
746
        $this->assertSame(3, $value);
747
        $this->assertTrue($cleanupCalled);
748
    }
749
750
    public function testCallCleanupWhenElementBeingLookedForIsFoundBeforeTheEndOfGenerator()
751
    {
752
        $cleanupCalled = false;
753
        $sequence = new Lazy(static function($registerCleanup) use (&$cleanupCalled) {
754
            $registerCleanup(static function() use (&$cleanupCalled) {
755
                $cleanupCalled = true;
756
            });
757
            yield 2;
758
            yield 3;
759
            yield 4;
760
            yield 5;
761
        });
762
763
        $this->assertFalse($cleanupCalled);
764
        $this->assertTrue($sequence->contains(3));
0 ignored issues
show
Bug introduced by
3 of type integer is incompatible with the type Innmind\Immutable\Sequence\T expected by parameter $element of Innmind\Immutable\Sequence\Lazy::contains(). ( Ignorable by Annotation )

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

764
        $this->assertTrue($sequence->contains(/** @scrutinizer ignore-type */ 3));
Loading history...
765
        $this->assertTrue($cleanupCalled);
766
    }
767
768
    public function testCallCleanupWhenIndexOfElementBeingLookedForIsFoundBeforeTheEndOfGenerator()
769
    {
770
        $cleanupCalled = false;
771
        $sequence = new Lazy(static function($registerCleanup) use (&$cleanupCalled) {
772
            $registerCleanup(static function() use (&$cleanupCalled) {
773
                $cleanupCalled = true;
774
            });
775
            yield 2;
776
            yield 3;
777
            yield 4;
778
            yield 5;
779
        });
780
781
        $this->assertFalse($cleanupCalled);
782
        $index = $sequence->indexOf(3)->match(
0 ignored issues
show
Bug introduced by
3 of type integer is incompatible with the type Innmind\Immutable\Sequence\T expected by parameter $element of Innmind\Immutable\Sequence\Lazy::indexOf(). ( Ignorable by Annotation )

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

782
        $index = $sequence->indexOf(/** @scrutinizer ignore-type */ 3)->match(
Loading history...
783
            static fn($index) => $index,
784
            static fn() => null,
785
        );
786
        $this->assertSame(1, $index);
787
        $this->assertTrue($cleanupCalled);
788
    }
789
790
    public function testCallCleanupWhenTakingLessElementsThanContainedInTheGenerator()
791
    {
792
        $cleanupCalled = false;
793
        $sequence = new Lazy(static function($registerCleanup) use (&$cleanupCalled) {
794
            $registerCleanup(static function() use (&$cleanupCalled) {
795
                $cleanupCalled = true;
796
            });
797
            yield 2;
798
            yield 3;
799
            yield 4;
800
            yield 5;
801
        });
802
803
        $this->assertFalse($cleanupCalled);
804
        $this->assertSame([2, 3], \iterator_to_array($sequence->take(2)->iterator()));
805
        $this->assertTrue($cleanupCalled);
806
    }
807
808
    public function testCallCleanupWhenFindingElementBeforeTheEndOfGenerator()
809
    {
810
        $cleanupCalled = false;
811
        $sequence = new Lazy(static function($registerCleanup) use (&$cleanupCalled) {
812
            $registerCleanup(static function() use (&$cleanupCalled) {
813
                $cleanupCalled = true;
814
            });
815
            yield 2;
816
            yield 3;
817
            yield 4;
818
            yield 5;
819
        });
820
        $this->assertFalse($cleanupCalled);
821
        $value = $sequence->find(static fn($value) => $value === 3)->match(
822
            static fn($value) => $value,
823
            static fn() => null,
824
        );
825
826
        $this->assertSame(3, $value);
827
        $this->assertTrue($cleanupCalled);
828
    }
829
830
    public function get($map, $index)
831
    {
832
        return $map->get($index)->match(
833
            static fn($value) => $value,
834
            static fn() => null,
835
        );
836
    }
837
}
838