Skip to content

Commit

Permalink
Merge pull request #16 from recoilphp/empty-composite-ops
Browse files Browse the repository at this point in the history
Update functional tests for composite "wait" operations with no coroutines.
  • Loading branch information
jmalloc authored Dec 13, 2017
2 parents c5c27a8 + bcf394b commit 9026df2
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 18 deletions.
27 changes: 27 additions & 0 deletions test-kernel/api/functional.all.spec.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,4 +173,31 @@ function () {
}
});
});

context('when no coroutines are provided', function () {
it('yields control to another strand', function () {
ob_start();

yield Recoil::execute(function () {
echo 'b';

return;
yield;
});

echo 'a';
yield Recoil::all();
echo 'c';

expect(ob_get_clean())->to->equal('abc');
});

it('returns an empty array when invoked directly', function () {
expect(yield Recoil::all())->to->equal([]);
});

it('returns an empty array when invoked by yielding an empty array', function () {
expect(yield [])->to->equal([]);
});
});
});
23 changes: 23 additions & 0 deletions test-kernel/api/functional.any.spec.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,27 @@ function () {
}
});
});

context('when no coroutines are provided', function () {
it('yields control to another strand', function () {
ob_start();

yield Recoil::execute(function () {
echo 'b';

return;
yield;
});

echo 'a';
yield Recoil::any();
echo 'c';

expect(ob_get_clean())->to->equal('abc');
});

it('returns null', function () {
expect(yield Recoil::any())->to->be->null();
});
});
});
23 changes: 23 additions & 0 deletions test-kernel/api/functional.first.spec.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,27 @@ function () {
}
});
});

context('when no coroutines are provided', function () {
it('yields control to another strand', function () {
ob_start();

yield Recoil::execute(function () {
echo 'b';

return;
yield;
});

echo 'a';
yield Recoil::first();
echo 'c';

expect(ob_get_clean())->to->equal('abc');
});

it('returns null', function () {
expect(yield Recoil::first())->to->be->null();
});
});
});
98 changes: 80 additions & 18 deletions test-kernel/api/functional.some.spec.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,22 +52,6 @@ function () {
$strand->terminate();
});

it('throws when the count is zero', function () {
try {
yield Recoil::some(
0,
function () {
},
function () {
}
);
} catch (InvalidArgumentException $e) {
expect($e->getMessage())->to->equal(
'Can not wait for 0 coroutines, count must be between 1 and 2, inclusive.'
);
}
});

it('throws when the count is negative', function () {
try {
yield Recoil::some(
Expand All @@ -79,7 +63,7 @@ function () {
);
} catch (InvalidArgumentException $e) {
expect($e->getMessage())->to->equal(
'Can not wait for -1 coroutines, count must be between 1 and 2, inclusive.'
'Can not wait for -1 coroutines, count must be between 0 and 2, inclusive.'
);
}
});
Expand All @@ -95,7 +79,7 @@ function () {
);
} catch (InvalidArgumentException $e) {
expect($e->getMessage())->to->equal(
'Can not wait for 3 coroutines, count must be between 1 and 2, inclusive.'
'Can not wait for 3 coroutines, count must be between 0 and 2, inclusive.'
);
}
});
Expand Down Expand Up @@ -185,4 +169,82 @@ function () {
}
});
});

context('when the count is zero', function () {
it('returns an empty array', function () {
expect(yield Recoil::some(
0,
function () {
yield;

return 'a';
},
function () {
return 'b';
yield;
},
function () {
return 'c';
yield;
}
))->to->equal([]);
});

it('terminates all strands', function () {
yield Recoil::some(
0,
function () {
yield;
expect(false)->to->be->ok('strand was not terminated');
},
function () {
yield;
expect(false)->to->be->ok('strand was not terminated');
}
);
});

it('yields control to another strand', function () {
ob_start();

yield Recoil::execute(function () {
echo 'b';

return;
yield;
});

echo 'a';
yield Recoil::some(0, function () {
yield;
expect(false)->to->be->ok('strand was not terminated');
});
echo 'c';

expect(ob_get_clean())->to->equal('abc');
});
});

context('when no coroutines are provided', function () {
it('yields control to another strand', function () {
ob_start();

yield Recoil::execute(function () {
echo 'b';

return;
yield;
});

echo 'a';
yield Recoil::some(0);
echo 'c';

expect(ob_get_clean())->to->equal('abc');
});

it('returns an empty array', function () {
expect(yield Recoil::some(0))->to->be->equal([]);
});
});
});

0 comments on commit 9026df2

Please sign in to comment.