-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCancelTest.php
More file actions
72 lines (51 loc) · 1.76 KB
/
CancelTest.php
File metadata and controls
72 lines (51 loc) · 1.76 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
<?php declare(strict_types=1);
namespace Versary\EffectSystem;
use PHPUnit\Framework\TestCase;
use Versary\EffectSystem\Errors\UnhandledEffect;
use Versary\EffectSystem\Tests\CancelTest\{Cancel, CancelHandler};
class CancelTest extends TestCase
{
// shitty way to test this
public bool $flag = true;
function inner() {
yield new Cancel;
}
function program() {
$this->flag = true;
yield from $this->inner();
// this does not get executed
$this->flag = false;
}
public function test_cancel() {
$gen = $this->program();
$gen = Effect::handle($gen, new CancelHandler);
$result = Effect::run($gen);
$this->assertEquals('cancelled', $result);
$this->assertTrue($this->flag);
}
function division(int $a, int $b) {
if ($b === 0) yield new Cancel;
return $a / $b;
}
public function test_division() {
$result = Effect::run(Effect::handle($this->division(3, 0), new CancelHandler));
$this->assertEquals('cancelled', $result);
$result = Effect::run(Effect::handle($this->division(6, 2), new CancelHandler));
$this->assertEquals(3, $result);
}
public function test_unhandled_division() {
$this->expectException(\DivisionByZeroError::class);
Effect::run(Effect::handle($this->division(3, 0), new class extends Handler {
public static $effect = Cancel::class;
}));
}
}
namespace Versary\EffectSystem\Tests\CancelTest;
use Versary\EffectSystem\{Effect, Handler};
class Cancel extends Effect {}
class CancelHandler extends Handler {
public static $effect = Cancel::class;
public function handle(mixed $effect, \Closure $resume) {
return 'cancelled';
}
}