packages/ztd-query-postgres/tests/Unit/Sql/Lexing/QuotedSpanTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Sql\Lexing;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\TestCase;
9
10#[CoversClass(\ZtdQuery\Platform\Postgres\Sql\Lexing\QuotedSpan::class)]
11final class QuotedSpanTest extends TestCase
12{
13    public function testQuotedLengthPreservesEscapedAndDoubledQuotes(): void
14    {
15        self::assertSame(6, \ZtdQuery\Platform\Postgres\Sql\Lexing\QuotedSpan::quotedLength('\'a\'\'b\' tail', '\'', false));
16        self::assertSame(6, \ZtdQuery\Platform\Postgres\Sql\Lexing\QuotedSpan::quotedLength('\'a\\\'b\' tail', '\'', true));
17        self::assertSame(6, \ZtdQuery\Platform\Postgres\Sql\Lexing\QuotedSpan::quotedLength('"name" rest', '"', false));
18        self::assertSame(5, \ZtdQuery\Platform\Postgres\Sql\Lexing\QuotedSpan::quotedLength('\'open', '\'', false));
19    }
20
21    public function testDollarQuotedLengthIncludesBothDelimiters(): void
22    {
23        self::assertSame(7, \ZtdQuery\Platform\Postgres\Sql\Lexing\QuotedSpan::dollarQuotedLength('$x$a$x$'));
24        self::assertNull(\ZtdQuery\Platform\Postgres\Sql\Lexing\QuotedSpan::dollarQuotedLength('$1$'));
25        self::assertSame(7, \ZtdQuery\Platform\Postgres\Sql\Lexing\QuotedSpan::dollarQuotedLength('$x$open'));
26    }
27
28    public function testDollarQuoteDelimiterValidatesTag(): void
29    {
30        self::assertSame('$$', \ZtdQuery\Platform\Postgres\Sql\Lexing\QuotedSpan::dollarQuoteDelimiter('$$body$$'));
31        self::assertSame('$tag_1$', \ZtdQuery\Platform\Postgres\Sql\Lexing\QuotedSpan::dollarQuoteDelimiter('$tag_1$body'));
32        self::assertNull(\ZtdQuery\Platform\Postgres\Sql\Lexing\QuotedSpan::dollarQuoteDelimiter('$1bad$'));
33    }
34
35    public function testIsEscapeStringStartRequiresASeparatePrefix(): void
36    {
37        self::assertTrue(\ZtdQuery\Platform\Postgres\Sql\Lexing\QuotedSpan::isEscapeStringStart("E'abc'", 1));
38        self::assertFalse(\ZtdQuery\Platform\Postgres\Sql\Lexing\QuotedSpan::isEscapeStringStart("nameE'abc'", 5));
39    }
40}
41