19 declare(strict_types=1);
38 $parse = $primitives->simpleEither([
43 $intermediate = $this->getMockBuilder(Intermediate::class)->disableOriginalConstructor()->getMock();
44 $intermediate->expects(self::exactly(2))->method(
'value')->willReturn(ord(
"\x0"));
45 $intermediate->method(
'accept')->willReturn(
new Ok($intermediate));
46 $intermediate->method(
'reject')->willReturn(
new Error(
'Failed'));
48 $result = $parse($intermediate,
static fn(
Result $x):
Result => $x);
50 $this->assertTrue($result->isOk());
51 $this->assertEquals($intermediate, $result->value());
58 $parse = $primitives->simpleSequence([
'a',
'd']);
60 $intermediate = $this->getMockBuilder(Intermediate::class)->disableOriginalConstructor()->getMock();
61 $intermediate->expects(self::exactly(2))->method(
'value')->willReturnOnConsecutiveCalls(ord(
'a'), ord(
'd'));
62 $intermediate->method(
'accept')->willReturn(
new Ok($intermediate));
64 $result = $parse($intermediate,
static fn(
Result $x):
Result => $x);
66 $this->assertTrue($result->isOk());
67 $this->assertEquals($intermediate, $result->value());
73 $parser = $primitives->until(0,
static function ():
void {
74 throw new Exception(
'Should not be called.');
77 $intermediate = $this->getMockBuilder(Intermediate::class)->disableOriginalConstructor()->getMock();
79 $result = $parser($intermediate,
static fn(
Result $x):
Result => $x);
80 $this->assertTrue($result->isOk());
81 $this->assertEquals($intermediate, $result->value());
87 foreach (array_fill(0, $n + 1,
null) as $i => $_) {
93 return $cc(
new Ok($x));
96 $intermediate = $this->getMockBuilder(Intermediate::class)->disableOriginalConstructor()->getMock();
98 $result = $parser($intermediate,
static function (
Result $x) use (&$end_called, $i):
Result {
100 return $end_called <= $i ?
new Error(
'Failed') : $x;
103 $this->assertEquals($i, $n);
104 $this->assertFalse($result->isOk());
106 $this->assertEquals($i, $called);
107 $this->assertEquals($i + 1, $end_called);
108 $this->assertTrue($result->isOk());
109 $this->assertEquals($intermediate, $result->value());
122 return $cc(
new Ok($x));
125 $intermediate = $this->getMockBuilder(Intermediate::class)->disableOriginalConstructor()->getMock();
127 $result = $parser($intermediate,
static function (
Result $x) use (&$end_called, $success_after):
Result {
129 return $end_called <= $success_after ?
new Error(
'Failed') : $x;
131 $this->assertEquals($success_after, $called);
132 $this->assertEquals($success_after + 1, $end_called);
133 $this->assertTrue($result->isOk());
134 $this->assertEquals($intermediate, $result->value());
145 return $called <= $fail_after ? $cc(
new Ok($x)) : $cc(
new Error(
'Failed.'));
148 $intermediate = $this->getMockBuilder(Intermediate::class)->disableOriginalConstructor()->getMock();
150 $result = $parser($intermediate,
static function (
Result $x) use (&$end_called):
Result {
152 return new Error(
'Failed.');
154 $this->assertEquals($fail_after + 1, $called);
155 $this->assertEquals(($fail_after + 1) * 2, $end_called);
156 $this->assertFalse($result->isOk());
163 $parser =
static function ():
void {
164 throw new Exception(
'Should not be called.');
167 $this->assertEquals($parser, $primitives->parserFrom($parser));
174 $intermediate = $this->getMockBuilder(Intermediate::class)->disableOriginalConstructor()->getMock();
175 $intermediate->method(
'value')->willReturnOnConsecutiveCalls(ord(
'h'), ord(
'e'), ord(
'l'), ord(
'l'), ord(
'o'));
176 $intermediate->method(
'accept')->willReturn(
new Ok($intermediate));
178 $parser = $primitives->parserFrom(
'hello');
180 $result = $parser($intermediate,
static fn(
Result $x):
Result => $x);
181 $this->assertTrue($result->isOk());
188 $intermediate = $this->getMockBuilder(Intermediate::class)->disableOriginalConstructor()->getMock();
189 $intermediate->expects(self::never())->method(
'value');
190 $intermediate->expects(self::never())->method(
'accept');
191 $intermediate->expects(self::never())->method(
'reject');
193 $parser = $primitives->parserFrom(
'');
195 $result = $parser($intermediate, fn($x) => $x);
196 $this->assertTrue($result->isOk());
197 $this->assertEquals($intermediate, $result->value());
-type Continuation Closure(Result<Intermediate>): Result<Intermediate> -type Parser Closure(Intermedi...
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
A result encapsulates a value or an error and simplifies the handling of those.
testParserFromEmptyString()