packages/sql-semantics-postgres/tests/Unit/Statement/Literal/SignedNumberTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Literal;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Small;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\PostgreSql\Model\Construction\Derivation;
11use SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease;
12use SqlSemantics\Platform\PostgreSql\Model\Contract\LanguageProfile;
13use SqlSemantics\Platform\PostgreSql\Model\Rendering\Lexical;
14use SqlSemantics\Platform\PostgreSql\Model\Rendering\Output;
15use SqlSemantics\Platform\PostgreSql\Platform;
16use SqlSemantics\Platform\PostgreSql\Rendering\Codec;
17use SqlSemantics\Platform\PostgreSql\Statement\Literal\IntegerConstant;
18use SqlSemantics\Platform\PostgreSql\Statement\Literal\NumericConstant;
19use SqlSemantics\Platform\PostgreSql\Statement\Literal\SignedNumber;
20
21#[CoversClass(SignedNumber::class)]
22#[Small]
23final class SignedNumberTest extends TestCase
24{
25    public function testDeriveClauseRecordsNothing(): void
26    {
27        $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
28        (new SignedNumber(false, new IntegerConstant('1')))->deriveClause($derivation, $derivation->environment());
29        self::assertSame([], $derivation->facts()->diagnostics);
30    }
31
32    public function testRenderWritesTheMinusSign(): void
33    {
34        $out = new Output(new Codec(GrammarRelease::PostgreSql172));
35        (new SignedNumber(true, new NumericConstant('1.5')))->render($out);
36        self::assertSame('- 1.5', (new Lexical())->join($out->pieces()));
37        $second = new Output(new Codec(GrammarRelease::PostgreSql172));
38        (new SignedNumber(false, new IntegerConstant('7')))->render($second);
39        self::assertSame('7', (new Lexical())->join($second->pieces()));
40    }
41
42    public function testTextIsTheIntegerValueOrTheWrittenTextAfterTheSign(): void
43    {
44        self::assertSame(['-.5', '1e2', '-7', '0', '-0x1FFFFFFFFF'], [
45            (new SignedNumber(true, new NumericConstant('.5')))->text(),
46            (new SignedNumber(false, new NumericConstant('1e2')))->text(),
47            (new SignedNumber(true, new IntegerConstant('7')))->text(),
48            (new SignedNumber(true, new IntegerConstant('0')))->text(),
49            (new SignedNumber(true, new NumericConstant('0x1FFFFFFFFF')))->text(),
50        ]);
51    }
52}
53