packages/sql-semantics-postgres/tests/Unit/Statement/Literal/NumericConstantTest.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\Contract\GrammarRelease;
11use SqlSemantics\Platform\PostgreSql\Model\Rendering\Lexical;
12use SqlSemantics\Platform\PostgreSql\Model\Rendering\Output;
13use SqlSemantics\Platform\PostgreSql\Rendering\Codec;
14use SqlSemantics\Platform\PostgreSql\Statement\Literal\NumericConstant;
15
16#[CoversClass(NumericConstant::class)]
17#[Small]
18final class NumericConstantTest extends TestCase
19{
20    public function testRenderWritesTheTextAsWritten(): void
21    {
22        $out = new Output(new Codec(GrammarRelease::PostgreSql172));
23        (new NumericConstant('0001.50'))->render($out);
24        self::assertSame('0001.50', (new Lexical())->join($out->pieces()));
25        $second = new Output(new Codec(GrammarRelease::PostgreSql172));
26        (new NumericConstant('0x1FFFFFFFFF'))->render($second);
27        self::assertSame('0x1FFFFFFFFF', (new Lexical())->join($second->pieces()));
28    }
29
30    public function testKeepsTheSpellingsTheServerKeepsApart(): void
31    {
32        self::assertSame(['1e2', '100.', '.5', '1_000_000_000_000'], [(new NumericConstant('1e2'))->text, (new NumericConstant('100.'))->text, (new NumericConstant('.5'))->text, (new NumericConstant('1_000_000_000_000'))->text]);
33    }
34
35    public function testRejectsAnIntegerThatFits32Bits(): void
36    {
37        $this->expectExceptionMessage('A numeric constant is a number with a point or an exponent, or an integer beyond 32 bits, as the scanner reads it.');
38        new NumericConstant('0x7FFFFFFF');
39    }
40
41    public function testRejectsASign(): void
42    {
43        $this->expectExceptionMessage('A numeric constant is a number with a point or an exponent, or an integer beyond 32 bits, as the scanner reads it.');
44        new NumericConstant('-1.5');
45    }
46
47    public function testRejectsAnExponentWithoutDigits(): void
48    {
49        $this->expectExceptionMessage('A numeric constant is a number with a point or an exponent, or an integer beyond 32 bits, as the scanner reads it.');
50        new NumericConstant('1e');
51    }
52}
53