-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathChoiceTest.php
More file actions
75 lines (54 loc) · 1.71 KB
/
ChoiceTest.php
File metadata and controls
75 lines (54 loc) · 1.71 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
<?php
namespace Versary\EffectSystem;
use PHPUnit\Framework\TestCase;
use Versary\EffectSystem\Errors\ResumedTwice;
use Versary\EffectSystem\Tests\ChoiceTest\{Choice, ChoiceRandomHandler, ChoiceAllHandler};
class ChoiceTest extends TestCase
{
function xor() {
$p = yield new Choice();
$q = yield new Choice();
return (bool)($p ^ $q);
}
public function test_random() {
$gen = Effect::handle($this->xor(), new ChoiceRandomHandler);
$result = Effect::run($gen);
$this->assertIsBool($result);
}
public function test_all() {
// we are not allowed to resume twice cause php doesn't let me clone generators :((
$this->expectException(ResumedTwice::class);
$gen = Effect::handle($this->xor(), new ChoiceAllHandler);
$result = Effect::run($gen);
$this->assertEquals($result, [
true, // true ^ false
false,
false,
true
]);
}
}
namespace Versary\EffectSystem\Tests\ChoiceTest;
use Versary\EffectSystem\{Effect, Handler};
class Choice extends Effect {
public function __construct() {}
}
class ChoiceRandomHandler extends Handler {
public static $effect = Choice::class;
public function resume(mixed $effect) {
return (bool)random_int(0, 1);
}
}
class ChoiceAllHandler extends Handler {
public static $effect = Choice::class;
public function __construct(public array $options = []) {}
public function handle(mixed $effect, \Closure $resume) {
return [
...yield from $resume(true),
...yield from $resume(false),
];
}
public function return(mixed $value) {
return [$value];
}
}