packages/sql-parser/tests/Unit/PostgreSql/Lexer/TriviaScannerTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\PostgreSql\Lexer;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Small;
9use PHPUnit\Framework\Attributes\UsesClass;
10use PHPUnit\Framework\TestCase;
11use SqlParser\Lexer\Cursor;
12use SqlParser\Lexer\LexicalException;
13use SqlParser\PostgreSql\Lexer\KeywordTable;
14use SqlParser\PostgreSql\Lexer\Scan;
15use SqlParser\PostgreSql\Lexer\TriviaScanner;
16
17#[CoversClass(TriviaScanner::class)]
18#[UsesClass(Cursor::class)]
19#[UsesClass(LexicalException::class)]
20#[UsesClass(KeywordTable::class)]
21#[UsesClass(Scan::class)]
22#[UsesClass(\SqlParser\Lexer\SourcePosition::class)]
23#[UsesClass(\SqlParser\Lexer\SourceException::class)]
24#[Small]
25final class TriviaScannerTest extends TestCase
26{
27    public function testSkip(): void
28    {
29        $scan = new Scan(new Cursor(" \t-- note\n/* outer /* inner */ still */x"), new KeywordTable([]));
30        (new TriviaScanner())->skip($scan);
31
32        self::assertSame('x', $scan->cursor->take(1));
33    }
34
35    public function testBlockComment(): void
36    {
37        $scan = new Scan(new Cursor('/* a */b'), new KeywordTable([]));
38        (new TriviaScanner())->blockComment($scan);
39
40        self::assertSame('b', $scan->cursor->take(1));
41    }
42
43    public function testBlockCommentRejectsAnUnterminatedComment(): void
44    {
45        $this->expectException(LexicalException::class);
46
47        (new TriviaScanner())->blockComment(new Scan(new Cursor('/* /* */'), new KeywordTable([])));
48    }
49}
50