-
-
Notifications
You must be signed in to change notification settings - Fork 259
/
Copy pathFilterTest.php
105 lines (87 loc) · 2.87 KB
/
FilterTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
namespace Amp\Test;
use Amp\Emitter;
use Amp\Iterator;
use Amp\Loop;
use Amp\PHPUnit\TestException;
use Amp\Producer;
class FilterTest extends BaseTest
{
public function testNoValuesEmitted()
{
$invoked = false;
Loop::run(function () use (&$invoked) {
$emitter = new Emitter;
$iterator = Iterator\filter($emitter->iterate(), function ($value) use (&$invoked) {
$invoked = true;
});
$this->assertInstanceOf(Iterator::class, $iterator);
$emitter->complete();
});
$this->assertFalse($invoked);
}
public function testValuesEmitted()
{
Loop::run(function () {
$count = 0;
$values = [1, 2, 3];
$expected = [1, 3];
$producer = new Producer(function (callable $emit) use ($values) {
foreach ($values as $value) {
yield $emit($value);
}
});
$iterator = Iterator\filter($producer, function ($value) use (&$count) {
++$count;
return $value & 1;
});
while (yield $iterator->advance()) {
$this->assertSame(\array_shift($expected), $iterator->getCurrent());
}
$this->assertSame(3, $count);
});
}
/**
* @depends testValuesEmitted
*/
public function testCallbackThrows()
{
Loop::run(function () {
$values = [1, 2, 3];
$exception = new TestException;
$producer = new Producer(function (callable $emit) use ($values) {
foreach ($values as $value) {
yield $emit($value);
}
});
$iterator = Iterator\filter($producer, function () use ($exception) {
throw $exception;
});
try {
yield $iterator->advance();
$this->fail("The exception thrown from the filter callback should be thrown from advance()");
} catch (TestException $reason) {
$this->assertSame($reason, $exception);
}
});
}
public function testIteratorFails()
{
Loop::run(function () {
$invoked = false;
$exception = new TestException;
$emitter = new Emitter;
$iterator = Iterator\filter($emitter->iterate(), function ($value) use (&$invoked) {
$invoked = true;
});
$emitter->fail($exception);
try {
yield $iterator->advance();
$this->fail("The exception used to fail the iterator should be thrown from advance()");
} catch (TestException $reason) {
$this->assertSame($reason, $exception);
}
$this->assertFalse($invoked);
});
}
}