packages/sql-semantics-sqlite/tests/Unit/Rendering/CanonicalTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Rendering;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\Sqlite\Model\Statement\Identifier\Name;
11use SqlSemantics\Platform\Sqlite\Model\Statement\Spelling\Layout;
12use SqlSemantics\Platform\Sqlite\Model\Statement\Spelling\Spelled;
13use SqlSemantics\Platform\Sqlite\Rendering\Canonical;
14use SqlSemantics\Platform\Sqlite\Semantics;
15use SqlSemantics\Platform\Sqlite\Statement\Expression\FunctionCall;
16use SqlSemantics\Platform\Sqlite\Statement\Expression\Literal\IntegerLiteral;
17use SqlSemantics\Platform\Sqlite\Statement\Query\ResultColumn;
18use SqlSemantics\Platform\Sqlite\Statement\Query\Select;
19
20#[CoversClass(Canonical::class)]
21#[Medium]
22final class CanonicalTest extends TestCase
23{
24    public function testLayoutSpellsTheRenderedPiecesWithTheirGaps(): void
25    {
26        $layout = (new Canonical())->layout(new FunctionCall(new Name('max'), [new IntegerLiteral('1'), new IntegerLiteral('2')]));
27
28        self::assertSame(['', '', '', '', ' ', ''], array_map(static fn (Spelled $token): string => $token->gap, $layout->tokens));
29        self::assertSame('max(1, 2)', $layout->text());
30    }
31
32    public function testLayoutKeepsTheLayoutsNestedInTheNode(): void
33    {
34        $query = (new Semantics())->analyze('SELECT (SELECT 1+1)');
35        self::assertInstanceOf(Select::class, $query->statement);
36        $column = $query->statement->columns[0];
37        self::assertInstanceOf(ResultColumn::class, $column);
38
39        self::assertNull($column->layout);
40        self::assertSame('(SELECT 1+1)', (new Canonical())->layout($column->expression)->text());
41    }
42
43    public function testSameComparesTheTokensAndTheGapsAfterTheFirst(): void
44    {
45        $canonical = new Canonical();
46        $one = new Layout([new Spelled('', '1'), new Spelled(' ', '+'), new Spelled(' ', '1')]);
47
48        self::assertTrue($canonical->same($one, new Layout([new Spelled('  ', '1'), new Spelled(' ', '+'), new Spelled(' ', '1')])));
49        self::assertFalse($canonical->same($one, new Layout([new Spelled('', '1'), new Spelled('', '+'), new Spelled(' ', '1')])));
50        self::assertFalse($canonical->same($one, new Layout([new Spelled('', '1'), new Spelled(' ', '-'), new Spelled(' ', '1')])));
51        self::assertFalse($canonical->same($one, new Layout([new Spelled('', '1')])));
52        self::assertFalse($canonical->same($one, new Layout([new Spelled('', '1'), new Spelled(' ', '+'), new Spelled(' ', '1')], ' /* c */')));
53    }
54
55    public function testTrailKeepsTheTriviaOnlyWhenItHoldsAComment(): void
56    {
57        $canonical = new Canonical();
58
59        self::assertSame('', $canonical->trail(" \n\t\f\r"));
60        self::assertSame(" /* c */\n ", $canonical->trail(" /* c */\n "));
61        self::assertSame(' -- c', $canonical->trail(' -- c'));
62    }
63
64    public function testSpanEndsBeforeTheWhitespaceAfterTheLastComment(): void
65    {
66        $canonical = new Canonical();
67
68        self::assertSame('1 + /* a */ 1 -- b', $canonical->span(new Layout([new Spelled('', '1'), new Spelled(' ', '+'), new Spelled(' /* a */ ', '1')], " -- b\n")));
69        self::assertSame('1', $canonical->span(new Layout([new Spelled('', '1')])));
70    }
71}
72