1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace Tests\Innmind\Immutable; |
5
|
|
|
|
6
|
|
|
use Innmind\Immutable\Accumulate; |
7
|
|
|
use PHPUnit\Framework\TestCase; |
8
|
|
|
|
9
|
|
|
class AccumulateTest extends TestCase |
10
|
|
|
{ |
11
|
|
|
public function testInterface() |
12
|
|
|
{ |
13
|
|
|
$this->assertInstanceOf( |
14
|
|
|
\Iterator::class, |
15
|
|
|
new Accumulate((static function() { |
16
|
|
|
yield 1; |
17
|
|
|
})()), |
18
|
|
|
); |
19
|
|
|
|
20
|
|
|
$loaded = false; |
21
|
|
|
$iterator = new Accumulate((static function() use (&$loaded) { |
22
|
|
|
yield 1; |
23
|
|
|
yield 2; |
24
|
|
|
yield 3; |
25
|
|
|
$loaded = true; |
26
|
|
|
})()); |
27
|
|
|
|
28
|
|
|
$this->assertFalse($loaded); |
29
|
|
|
$this->assertSame([1, 2, 3], \iterator_to_array($iterator)); |
30
|
|
|
$this->assertTrue($loaded); |
31
|
|
|
$this->assertSame([1, 2, 3], \iterator_to_array($iterator)); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function testSupportsPartialIterations() |
35
|
|
|
{ |
36
|
|
|
$loaded = false; |
37
|
|
|
$iterator = new Accumulate((static function() use (&$loaded) { |
38
|
|
|
yield 1; |
39
|
|
|
yield 2; |
40
|
|
|
yield 3; |
41
|
|
|
yield 4; |
42
|
|
|
$loaded = true; |
43
|
|
|
})()); |
44
|
|
|
|
45
|
|
|
$this->assertSame(0, $iterator->key()); |
46
|
|
|
$this->assertSame(1, $iterator->current()); |
47
|
|
|
$this->assertNull($iterator->next()); |
|
|
|
|
48
|
|
|
$this->assertSame(1, $iterator->key()); |
49
|
|
|
$this->assertSame(2, $iterator->current()); |
50
|
|
|
$this->assertNull($iterator->rewind()); |
|
|
|
|
51
|
|
|
$this->assertFalse($loaded); |
52
|
|
|
$this->assertSame([1, 2, 3, 4], \iterator_to_array($iterator)); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function testMixingPartialIterationsInGeneratorsCompositionDoesntTamperIteration() |
56
|
|
|
{ |
57
|
|
|
$initial = new Accumulate((static function() { |
58
|
|
|
yield 1; |
59
|
|
|
yield 2; |
60
|
|
|
})()); |
61
|
|
|
$decorate = (static function($initial) { |
62
|
|
|
foreach ($initial as $i) { |
63
|
|
|
yield $i; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
yield 3; |
67
|
|
|
})($initial); |
68
|
|
|
$iterator = new Accumulate($decorate); |
69
|
|
|
$iterator->rewind(); |
70
|
|
|
|
71
|
|
|
$this->assertTrue($iterator->valid()); |
72
|
|
|
$this->assertSame([1, 2], \iterator_to_array($initial)); |
73
|
|
|
$this->assertSame([1, 2, 3], \iterator_to_array($iterator)); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
This check looks for function or method calls that always return null and whose return value is used.
The method
getObject()
can return nothing but null, so it makes no sense to use the return value.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.