packages/sql-semantics-postgres/tests/Unit/Rules/Lexical/StringsTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Rules\Lexical;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Small;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\PostgreSql\Model\Diagnostic\AnalysisException;
11use SqlSemantics\Platform\PostgreSql\Rules\Lexical\Strings;
12
13#[CoversClass(Strings::class)]
14#[Small]
15final class StringsTest extends TestCase
16{
17 public function testDecodeReadsADoubledQuoteOfAStandardString(): void
18 {
19 self::assertSame("a'b\\n", (new Strings())->decode("'a''b\\n'"));
20 }
21
22 public function testDecodeReadsTheEscapesOfAnEscapeString(): void
23 {
24 self::assertSame("a'b\n\tAAé😀\\q", (new Strings())->decode("E'a\\'b\\n\\t\\x41\\101\\u00e9\\U0001F600\\\\\\q'"));
25 }
26
27 public function testDecodeJoinsASurrogatePairOfAnEscapeString(): void
28 {
29 self::assertSame('😀', (new Strings())->decode("e'\\uD83D\\uDE00'"));
30 }
31
32 public function testDecodeTakesADollarQuotedStringLiterally(): void
33 {
34 self::assertSame("a'\\\$\$b", (new Strings())->decode("\$t\$a'\\\$\$b\$t\$"));
35 }
36
37 public function testDecodeReplacesTheEscapesOfAUnicodeString(): void
38 {
39 self::assertSame("data'", (new Strings())->decode("U&'d\\0061t\\+000061'''"));
40 self::assertSame('data', (new Strings())->decode("u&'d!0061t!+000061' UESCAPE '!'"));
41 }
42
43 public function testDecodeJoinsSegmentsContinuedOnAnotherLine(): void
44 {
45 self::assertSame("ab\tc", (new Strings())->decode("E'a' -- note\n 'b\\tc'"));
46 }
47
48 public function testDecodeRejectsAZeroByteFromAnEscape(): void
49 {
50 $this->expectException(AnalysisException::class);
51 (new Strings())->decode("E'a\\000b'");
52 }
53
54 public function testDecodeRejectsBytesThatAreNotUtf8(): void
55 {
56 $this->expectException(AnalysisException::class);
57 (new Strings())->decode("E'\\xff'");
58 }
59
60 public function testDigitsAnswersTheTextBetweenTheQuotes(): void
61 {
62 self::assertSame('1F', (new Strings())->digits("X'1F'"));
63 self::assertSame('1001', (new Strings())->digits("b'10'\n'01'"));
64 }
65
66 public function testQuotedAnswersTheValueAndTheTextAfterIt(): void
67 {
68 self::assertSame(["a'b", " UESCAPE '!'"], (new Strings())->quoted("U&'a''b' UESCAPE '!'", 2, false));
69 }
70
71 public function testContinuationFindsTheNextQuoteAfterANewline(): void
72 {
73 self::assertSame(5, (new Strings())->continuation("'a'\n 'b'", 3));
74 self::assertNull((new Strings())->continuation("'a' UESCAPE '!'", 3));
75 }
76
77 public function testEscapeDecodesOneBackslashEscape(): void
78 {
79 self::assertSame(["\n", 2], (new Strings())->escape('\\n', 0));
80 self::assertSame(['A', 4], (new Strings())->escape('\\x41', 0));
81 self::assertSame(['S', 4], (new Strings())->escape('\\123', 0));
82 self::assertSame(['q', 2], (new Strings())->escape('\\q', 0));
83 }
84
85 public function testEscapeRejectsAnIncompleteUnicodeEscape(): void
86 {
87 $this->expectException(AnalysisException::class);
88 (new Strings())->escape('\\u12', 0);
89 }
90}
91