packages/sql-semantics/tests/Unit/Statement/CommentsTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Small;
9use PHPUnit\Framework\Attributes\UsesClass;
10use PHPUnit\Framework\TestCase;
11use SqlSemantics\Statement\Assertion;
12use SqlSemantics\Statement\Comments;
13
14#[CoversClass(Comments::class)]
15#[UsesClass(Assertion::class)]
16#[Small]
17final class CommentsTest extends TestCase
18{
19    public function testBeforeAnswersTheCommentsOfASymbolPositionInWritingOrder(): void
20    {
21        $comments = new Comments([1 => ['-- first', '/* second */'], 3 => ['#third']]);
22        self::assertSame(['-- first', '/* second */'], $comments->before(1));
23        self::assertSame(['#third'], $comments->before(3));
24        self::assertSame([], $comments->before(0));
25        self::assertSame([], (new Comments())->before(0));
26    }
27
28    public function testPositionsListsTheCommentedSymbolsInAscendingOrder(): void
29    {
30        self::assertSame([], (new Comments())->positions());
31        self::assertSame([0, 2, 5], (new Comments([5 => ['-- a'], 0 => ['-- b'], 2 => ['-- c']]))->positions());
32    }
33
34    public function testWithAddsCommentsAfterThoseAlreadyAtThePositionAndPreservesTheOriginal(): void
35    {
36        $original = new Comments([0 => ['-- a']]);
37        $updated = $original->with(0, '-- b', '/* c */')->with(2, '*/');
38        self::assertSame(['-- a'], $original->before(0));
39        self::assertSame([0], $original->positions());
40        self::assertSame(['-- a', '-- b', '/* c */'], $updated->before(0));
41        self::assertSame(['*/'], $updated->before(2));
42        self::assertSame([0, 2], $updated->positions());
43    }
44}
45