-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathChapter_2_Optional_Maybe.php
95 lines (75 loc) · 3.47 KB
/
Chapter_2_Optional_Maybe.php
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php declare(strict_types=1);
namespace Stratadox\Parser\Test\Guide;
use PHPUnit\Framework\TestCase;
use Stratadox\Parser\Parsers\Ignore;
use function Stratadox\Parser\any;
use function Stratadox\Parser\text;
final class Chapter_2_Optional_Maybe extends TestCase
{
/** @test */
function Chapter_2_1_example_1()
{
$greeting = text("Hello, ")->or("Hi, ")->or("Hey there, ");
$verb = text("my name is ")->or("I'm called ")->or("I'm ")->ignore();
$suffix = text(". :)")->or("!")->or("")->end();
$name = any()->except($suffix)->repeatableString();
$introduction = $greeting->andThen($verb, $name, $suffix);
$result = $introduction->parse("Hey there, my name is Tom Bombadil. :)");
self::assertEquals(["Hey there, ", "Tom Bombadil", ". :)"], $result->data());
}
/** @test */
function Chapter_2_1_example_2()
{
$greeting = text("Hello")->or("Hi")->or("Hey there")->andThen(Ignore::the(', '));
$verb = text("my name is ")->or("I'm called ")->or("I'm ");
$suffix = text(". :)")->or("!")->or("")->end();
$name = any()->except($suffix)->repeatableString();
$introduction = $greeting->andThen($verb, $name, $suffix);
$result = $introduction->parse("Hey there, my name is Tom Bombadil. :)");
self::assertEquals(["Hey there", "my name is ", "Tom Bombadil", ". :)"], $result->data());
}
/** @test */
function Chapter_2_2_example_1()
{
$greeting = text("Hello, ")->or("Hi, ")->or("Hey there, ");
$verb = text("my name is ")->or("I'm called ")->or("I'm ")->ignore();
$suffix = text(". :)")->or("!")->maybe()->end();
$name = any()->except($suffix)->repeatableString();
$introduction = $greeting->andThen($verb, $name, $suffix);
$result = $introduction->parse("Hello, my name is Alice");
self::assertEquals(["Hello, ", "Alice"], $result->data());
}
/** @test */
function Chapter_2_2_example_2()
{
$greeting = text("Hello, ")->or("Hi, ")->or("Hey there, ");
$verb = text("my name is ")->or("I'm called ")->or("I'm ")->ignore();
$suffix = text(". :)")->or("!")->maybe()->end();
$name = any()->except($suffix)->repeatableString();
$introduction = $greeting->andThen($verb, $name, $suffix);
$result = $introduction->parse("Hi, I'm Bob. :)");
self::assertEquals(["Hi, ", "Bob", ". :)"], $result->data());
}
/** @test */
function Chapter_2_3_example_1()
{
$greeting = text("Hello, ")->or("Hi, ")->or("Hey there, ")->optional();
$verb = text("my name is ")->or("I'm called ")->or("I'm ")->ignore();
$suffix = text(". :)")->or("!")->maybe()->end();
$name = any()->except($suffix)->repeatableString();
$introduction = $greeting->andThen($verb, $name, $suffix);
$result = $introduction->parse("Hello, my name is Alice");
self::assertEquals(["Alice"], $result->data());
}
/** @test */
function Chapter_2_3_example_2()
{
$greeting = text("Hello, ")->or("Hi, ")->or("Hey there, ")->optional();
$verb = text("my name is ")->or("I'm called ")->or("I'm ")->ignore();
$suffix = text(". :)")->or("!")->maybe()->end();
$name = any()->except($suffix)->repeatableString();
$introduction = $greeting->andThen($verb, $name, $suffix);
$result = $introduction->parse("Hi, I'm Bob. :)");
self::assertEquals(["Bob", ". :)"], $result->data());
}
}