packages/bison-parser/tests/Unit/SyntaxExceptionTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit;
6
7use BisonParser\Ast\Location;
8use BisonParser\SyntaxException;
9use PHPUnit\Framework\Attributes\CoversClass;
10use PHPUnit\Framework\Attributes\Small;
11use PHPUnit\Framework\Attributes\UsesClass;
12use PHPUnit\Framework\TestCase;
13
14#[CoversClass(SyntaxException::class)]
15#[UsesClass(Location::class)]
16#[Small]
17final class SyntaxExceptionTest extends TestCase
18{
19 public function testGetMessage(): void
20 {
21 $exception = new SyntaxException('Something is off', new Location(3, 7));
22
23 self::assertSame('Something is off at 3:7', $exception->getMessage());
24 self::assertSame('3:7', (string) $exception->location);
25 }
26
27 public function testUnexpected(): void
28 {
29 self::assertSame("Expected a rule but found ';' at 1:2", SyntaxException::unexpected('a rule', "';'", new Location(1, 2))->getMessage());
30 }
31
32 public function testUnterminated(): void
33 {
34 self::assertSame("Missing '\"' closing the string opened at 4:5", SyntaxException::unterminated('string', '"', new Location(4, 5))->getMessage());
35 }
36
37 public function testInvalid(): void
38 {
39 self::assertSame('Invalid directive: %foo at 2:1', SyntaxException::invalid('invalid directive: %foo', new Location(2, 1))->getMessage());
40 }
41}
42