-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfunctional.execute.spec.php
More file actions
130 lines (106 loc) · 3.2 KB
/
Copy pathfunctional.execute.spec.php
File metadata and controls
130 lines (106 loc) · 3.2 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
declare(strict_types=1); // @codeCoverageIgnore
namespace Recoil;
use Generator;
use InvalidArgumentException;
use Recoil\Kernel\Api;
context('kernel/execute', function () {
it('accepts a generator object', function () {
$this->kernel()->execute((function () {
echo '<ok>';
return;
yield;
})());
ob_start();
$this->kernel()->run();
expect(ob_get_clean())->to->equal('<ok>');
});
it('accepts a generator function', function () {
$this->kernel()->execute(function () {
echo '<ok>';
return;
yield;
});
ob_start();
$this->kernel()->run();
expect(ob_get_clean())->to->equal('<ok>');
});
it('does not accept regular functions', function () {
try {
$this->kernel()->execute(function () {
});
} catch (InvalidArgumentException $e) {
expect($e->getMessage())->to->equal('Callable must return a generator.');
}
});
it('accepts a coroutine provider', function () {
$this->kernel()->execute(new class() implements CoroutineProvider {
public function coroutine(): Generator
{
echo '<ok>';
return;
yield;
}
});
ob_start();
$this->kernel()->run();
expect(ob_get_clean())->to->equal('<ok>');
});
it('accepts an awaitable provider', function () {
$this->kernel()->execute(new class() implements AwaitableProvider {
public function awaitable(): Awaitable
{
return new class() implements Awaitable {
public function await(Listener $listener)
{
echo '<ok>';
}
};
}
});
ob_start();
$this->kernel()->run();
expect(ob_get_clean())->to->equal('<ok>');
});
it('accepts an awaitable', function () {
$this->kernel()->execute(new class() implements Awaitable {
public function await(Listener $listener)
{
echo '<ok>';
}
});
ob_start();
$this->kernel()->run();
expect(ob_get_clean())->to->equal('<ok>');
});
it('dispatches other types via the kernel api', function () {
$this->kernel()->execute([
function () {
echo '<ok>';
return;
yield;
},
]);
ob_start();
$this->kernel()->run();
expect(ob_get_clean())->to->equal('<ok>');
});
it('returns the strand', function () {
$strand = $this->kernel()->execute('<coroutine>');
expect($strand)->to->be->an->instanceof(Strand::class);
});
it('defers execution', function () {
ob_start();
$this->kernel()->execute([
function () {
echo '<ok>';
return;
yield;
},
]);
expect(ob_get_clean())->to->equal('');
ob_start();
$this->kernel()->run();
expect(ob_get_clean())->to->equal('<ok>');
});
});