packages/bison-parser/tests/Unit/Printer/SymbolPrinterTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Printer;
6
7use BisonParser\Ast\Location;
8use BisonParser\Ast\Symbol;
9use BisonParser\Ast\SymbolKind;
10use BisonParser\Ast\Tag;
11use BisonParser\Printer\SymbolPrinter;
12use BisonParser\Scanner\Escapes;
13use PHPUnit\Framework\Attributes\CoversClass;
14use PHPUnit\Framework\Attributes\Small;
15use PHPUnit\Framework\Attributes\UsesClass;
16use PHPUnit\Framework\TestCase;
17
18#[CoversClass(SymbolPrinter::class)]
19#[UsesClass(Escapes::class)]
20#[UsesClass(Location::class)]
21#[UsesClass(Symbol::class)]
22#[UsesClass(SymbolKind::class)]
23#[UsesClass(Tag::class)]
24#[Small]
25final class SymbolPrinterTest extends TestCase
26{
27 public function testSymbol(): void
28 {
29 $printer = new SymbolPrinter();
30
31 self::assertSame('expr', $printer->symbol(new Symbol(SymbolKind::Identifier, 'expr', new Location(1, 1))));
32 self::assertSame("'\\n'", $printer->symbol(new Symbol(SymbolKind::CharLiteral, "\n", new Location(1, 1))));
33 self::assertSame("'\\''", $printer->symbol(new Symbol(SymbolKind::CharLiteral, "'", new Location(1, 1))));
34 self::assertSame('"a\\"b"', $printer->symbol(new Symbol(SymbolKind::String, 'a"b', new Location(1, 1))));
35 self::assertSame("'\\101'", $printer->symbol(new Symbol(SymbolKind::CharLiteral, 'A', new Location(1, 1), "'\\101'")));
36 self::assertSame('"\\x61"', $printer->symbol(new Symbol(SymbolKind::String, 'a', new Location(1, 1), '"\\x61"')));
37 }
38
39 public function testString(): void
40 {
41 self::assertSame('"tab\\there"', (new SymbolPrinter())->string("tab\there"));
42 }
43
44 public function testTag(): void
45 {
46 $printer = new SymbolPrinter();
47
48 self::assertSame('<int>', $printer->tag(new Tag('int', new Location(1, 1))));
49 self::assertSame('<*>', $printer->tag(new Tag(Tag::ANY, new Location(1, 1))));
50 self::assertSame('<>', $printer->tag(new Tag(Tag::NONE, new Location(1, 1))));
51 }
52
53 public function testCode(): void
54 {
55 self::assertSame('{ $$ = $1; }', (new SymbolPrinter())->code(' $$ = $1; '));
56 }
57}
58