Skip to content

Commit 9026df2

Browse files
authored
Merge pull request #16 from recoilphp/empty-composite-ops
Update functional tests for composite "wait" operations with no coroutines.
2 parents c5c27a8 + bcf394b commit 9026df2

4 files changed

Lines changed: 153 additions & 18 deletions

File tree

test-kernel/api/functional.all.spec.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,4 +173,31 @@ function () {
173173
}
174174
});
175175
});
176+
177+
context('when no coroutines are provided', function () {
178+
it('yields control to another strand', function () {
179+
ob_start();
180+
181+
yield Recoil::execute(function () {
182+
echo 'b';
183+
184+
return;
185+
yield;
186+
});
187+
188+
echo 'a';
189+
yield Recoil::all();
190+
echo 'c';
191+
192+
expect(ob_get_clean())->to->equal('abc');
193+
});
194+
195+
it('returns an empty array when invoked directly', function () {
196+
expect(yield Recoil::all())->to->equal([]);
197+
});
198+
199+
it('returns an empty array when invoked by yielding an empty array', function () {
200+
expect(yield [])->to->equal([]);
201+
});
202+
});
176203
});

test-kernel/api/functional.any.spec.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,4 +115,27 @@ function () {
115115
}
116116
});
117117
});
118+
119+
context('when no coroutines are provided', function () {
120+
it('yields control to another strand', function () {
121+
ob_start();
122+
123+
yield Recoil::execute(function () {
124+
echo 'b';
125+
126+
return;
127+
yield;
128+
});
129+
130+
echo 'a';
131+
yield Recoil::any();
132+
echo 'c';
133+
134+
expect(ob_get_clean())->to->equal('abc');
135+
});
136+
137+
it('returns null', function () {
138+
expect(yield Recoil::any())->to->be->null();
139+
});
140+
});
118141
});

test-kernel/api/functional.first.spec.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,4 +147,27 @@ function () {
147147
}
148148
});
149149
});
150+
151+
context('when no coroutines are provided', function () {
152+
it('yields control to another strand', function () {
153+
ob_start();
154+
155+
yield Recoil::execute(function () {
156+
echo 'b';
157+
158+
return;
159+
yield;
160+
});
161+
162+
echo 'a';
163+
yield Recoil::first();
164+
echo 'c';
165+
166+
expect(ob_get_clean())->to->equal('abc');
167+
});
168+
169+
it('returns null', function () {
170+
expect(yield Recoil::first())->to->be->null();
171+
});
172+
});
150173
});

test-kernel/api/functional.some.spec.php

Lines changed: 80 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -52,22 +52,6 @@ function () {
5252
$strand->terminate();
5353
});
5454

55-
it('throws when the count is zero', function () {
56-
try {
57-
yield Recoil::some(
58-
0,
59-
function () {
60-
},
61-
function () {
62-
}
63-
);
64-
} catch (InvalidArgumentException $e) {
65-
expect($e->getMessage())->to->equal(
66-
'Can not wait for 0 coroutines, count must be between 1 and 2, inclusive.'
67-
);
68-
}
69-
});
70-
7155
it('throws when the count is negative', function () {
7256
try {
7357
yield Recoil::some(
@@ -79,7 +63,7 @@ function () {
7963
);
8064
} catch (InvalidArgumentException $e) {
8165
expect($e->getMessage())->to->equal(
82-
'Can not wait for -1 coroutines, count must be between 1 and 2, inclusive.'
66+
'Can not wait for -1 coroutines, count must be between 0 and 2, inclusive.'
8367
);
8468
}
8569
});
@@ -95,7 +79,7 @@ function () {
9579
);
9680
} catch (InvalidArgumentException $e) {
9781
expect($e->getMessage())->to->equal(
98-
'Can not wait for 3 coroutines, count must be between 1 and 2, inclusive.'
82+
'Can not wait for 3 coroutines, count must be between 0 and 2, inclusive.'
9983
);
10084
}
10185
});
@@ -185,4 +169,82 @@ function () {
185169
}
186170
});
187171
});
172+
173+
context('when the count is zero', function () {
174+
it('returns an empty array', function () {
175+
expect(yield Recoil::some(
176+
0,
177+
function () {
178+
yield;
179+
180+
return 'a';
181+
},
182+
function () {
183+
return 'b';
184+
yield;
185+
},
186+
function () {
187+
return 'c';
188+
yield;
189+
}
190+
))->to->equal([]);
191+
});
192+
193+
it('terminates all strands', function () {
194+
yield Recoil::some(
195+
0,
196+
function () {
197+
yield;
198+
expect(false)->to->be->ok('strand was not terminated');
199+
},
200+
function () {
201+
yield;
202+
expect(false)->to->be->ok('strand was not terminated');
203+
}
204+
);
205+
});
206+
207+
it('yields control to another strand', function () {
208+
ob_start();
209+
210+
yield Recoil::execute(function () {
211+
echo 'b';
212+
213+
return;
214+
yield;
215+
});
216+
217+
echo 'a';
218+
yield Recoil::some(0, function () {
219+
yield;
220+
expect(false)->to->be->ok('strand was not terminated');
221+
});
222+
echo 'c';
223+
224+
expect(ob_get_clean())->to->equal('abc');
225+
});
226+
});
227+
228+
context('when no coroutines are provided', function () {
229+
it('yields control to another strand', function () {
230+
ob_start();
231+
232+
yield Recoil::execute(function () {
233+
echo 'b';
234+
235+
return;
236+
yield;
237+
});
238+
239+
echo 'a';
240+
yield Recoil::some(0);
241+
echo 'c';
242+
243+
expect(ob_get_clean())->to->equal('abc');
244+
});
245+
246+
it('returns an empty array', function () {
247+
expect(yield Recoil::some(0))->to->be->equal([]);
248+
});
249+
});
188250
});

0 commit comments

Comments
 (0)