packages/ztd-query-postgres/tests/Unit/Shadow/Mutation/Upsert/Expression/ExpressionLexemeTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Shadow\Mutation\Upsert\Expression;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\TestCase;
9
10#[CoversClass(\ZtdQuery\Platform\Postgres\Shadow\Mutation\Upsert\Expression\ExpressionLexeme::class)]
11#[\PHPUnit\Framework\Attributes\UsesClass(\ZtdQuery\Platform\Postgres\Sql\PgSqlLexerProfile::class)]
12final class ExpressionLexemeTest extends TestCase
13{
14    public function testIsSymbolRequiresTheSymbolTokenKind(): void
15    {
16        $sql = '+ word';
17        $tokens = \ZtdQuery\Sql\SqlTokenStream::tokenize($sql, \ZtdQuery\Platform\Postgres\Sql\PgSqlLexerProfile::create())->significantTokens();
18        self::assertTrue((new \ZtdQuery\Platform\Postgres\Shadow\Mutation\Upsert\Expression\ExpressionLexeme())->isSymbol($tokens[0], ['+']));
19        self::assertFalse((new \ZtdQuery\Platform\Postgres\Shadow\Mutation\Upsert\Expression\ExpressionLexeme())->isSymbol($tokens[1], ['word']));
20    }
21
22    public function testIsIdentifierRecognizesBareAndQuotedNames(): void
23    {
24        $sql = 'name "Quoted" 1';
25        $tokens = \ZtdQuery\Sql\SqlTokenStream::tokenize($sql, \ZtdQuery\Platform\Postgres\Sql\PgSqlLexerProfile::create())->significantTokens();
26        self::assertTrue((new \ZtdQuery\Platform\Postgres\Shadow\Mutation\Upsert\Expression\ExpressionLexeme())->isIdentifier($tokens[0]));
27        self::assertTrue((new \ZtdQuery\Platform\Postgres\Shadow\Mutation\Upsert\Expression\ExpressionLexeme())->isIdentifier($tokens[1]));
28        self::assertFalse((new \ZtdQuery\Platform\Postgres\Shadow\Mutation\Upsert\Expression\ExpressionLexeme())->isIdentifier($tokens[2]));
29    }
30
31    public function testIdentifierUnescapesDoubledQuotes(): void
32    {
33        $sql = '"odd""name"';
34        $tokens = \ZtdQuery\Sql\SqlTokenStream::tokenize($sql, \ZtdQuery\Platform\Postgres\Sql\PgSqlLexerProfile::create())->significantTokens();
35        self::assertSame('odd"name', (new \ZtdQuery\Platform\Postgres\Shadow\Mutation\Upsert\Expression\ExpressionLexeme())->identifier($tokens[0]));
36    }
37
38    public function testNumberDistinguishesIntegersAndExponentLiterals(): void
39    {
40        self::assertSame(1000, (new \ZtdQuery\Platform\Postgres\Shadow\Mutation\Upsert\Expression\ExpressionLexeme())->number('1_000'));
41        self::assertSame(150.0, (new \ZtdQuery\Platform\Postgres\Shadow\Mutation\Upsert\Expression\ExpressionLexeme())->number('1.5e2'));
42    }
43
44    public function testStringUnescapesDoubledApostrophes(): void
45    {
46        self::assertSame("it's", (new \ZtdQuery\Platform\Postgres\Shadow\Mutation\Upsert\Expression\ExpressionLexeme())->string("'it''s'"));
47    }
48}
49