-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathChapter_4_Between.php
67 lines (50 loc) · 1.89 KB
/
Chapter_4_Between.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
<?php declare(strict_types=1);
namespace Stratadox\Parser\Test\Guide;
use PHPUnit\Framework\TestCase;
use Stratadox\Parser\Helpers\Between;
use Stratadox\Parser\Parsers\Ignore;
use function Stratadox\Parser\any;
use function Stratadox\Parser\pattern;
use function Stratadox\Parser\text;
final class Chapter_4_Between extends TestCase
{
/** @test */
function Chapter_4_1_example_1()
{
$greeting = text("Hello")->or("Hi", "Hey there")->andThen(Ignore::the(','));
$verb = text("my name is")->or("I'm called", "I'm")->between(' ');
$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", "my name is", "Alice"], $result->data());
}
/** @test */
function Chapter_4_1_example_2()
{
$parser = text('content')->between(pattern('[({]'), text(')')->or('}'));
$result = $parser->parse('{content}');
self::assertEquals('content', $result->data());
}
/** @test */
function Chapter_4_1_example_3()
{
$parser = text('content')->between(pattern('[({]'), text(')')->or('}'));
$result = $parser->parse('(content)');
self::assertEquals('content', $result->data());
}
/** @test */
function Chapter_4_2_example_1()
{
$parser = Between::escaped('"', '"', '\\');
$result = $parser->parse('"Regular text between quotes"');
self::assertEquals('Regular text between quotes', $result->data());
}
/** @test */
function Chapter_4_2_example_2()
{
$parser = Between::escaped('"', '"', '\\');
$result = $parser->parse('"Text with \\"escaped\\" quotes"');
self::assertEquals('Text with "escaped" quotes', $result->data());
}
}