packages/sql-semantics-mysql/tests/Unit/Statement/Literal/NumeralTest.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\MySql\Lowering\Lowering;
11use SqlSemantics\Platform\MySql\Model\Contract\GrammarRelease;
12use SqlSemantics\Platform\MySql\Model\Contract\ParameterStyle;
13use SqlSemantics\Platform\MySql\Model\Lowering\Leaves;
14use SqlSemantics\Platform\MySql\Model\Rendering\Lexical;
15use SqlSemantics\Platform\MySql\Model\Rendering\Output;
16use SqlSemantics\Platform\MySql\Platform;
17use SqlSemantics\Platform\MySql\Rendering\Codec;
18use SqlSemantics\Platform\MySql\Statement\Literal\Numeral;
19
20#[CoversClass(Numeral::class)]
21#[Small]
22final class NumeralTest extends TestCase
23{
24    public function testRenderWritesTheExactDecimalText(): void
25    {
26        $out = new Output(new Codec(GrammarRelease::MySql847));
27        (new Numeral('007'))->render($out);
28        (new Numeral('1.5'))->render($out);
29        (new Numeral('1e2'))->render($out);
30
31        self::assertSame('007 1.5 1e2', (new Lexical())->join($out->pieces()));
32    }
33
34    public function testRenderQuotesAnEvenCountOfHexadecimalDigits(): void
35    {
36        $out = new Output(new Codec(GrammarRelease::MySql847));
37        (new Numeral('10', true))->render($out);
38        (new Numeral('', true))->render($out);
39
40        self::assertSame("x'10' x''", (new Lexical())->join($out->pieces()));
41    }
42
43    public function testRenderPrefixesAnOddCountOfHexadecimalDigits(): void
44    {
45        $out = new Output(new Codec(GrammarRelease::MySql847));
46        (new Numeral('1', true))->render($out);
47
48        self::assertSame('0x1', (new Lexical())->join($out->pieces()));
49    }
50
51    public function testLoweredNumeralKeepsLeadingZeros(): void
52    {
53        $platform = new Platform();
54        $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
55        $node = $platform->parser($profile)->parse('ALTER TABLE t COALESCE PARTITION 007')->find('real_ulong_num')[0];
56        $numeral = (new Lowering($platform->productions($profile), new Leaves(), $profile))->numbers->numeral($node);
57
58        self::assertSame('007', $numeral->text);
59        self::assertFalse($numeral->hexadecimal);
60    }
61
62    public function testLoweredHexadecimalNumeralKeepsTheDigitsOfEitherSpelling(): void
63    {
64        $platform = new Platform();
65        $profile = $platform->profile('mysql-5.7.44', null, ParameterStyle::Native);
66        $parser = $platform->parser($profile);
67        $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
68        $prefixed = $lowering->numbers->numeral($parser->parse('CHANGE MASTER TO MASTER_PORT = 0xCE6')->find('ulong_num')[0]);
69        $quoted = $lowering->numbers->numeral($parser->parse("ALTER TABLE t COALESCE PARTITION x'0a'")->find('real_ulong_num')[0]);
70        $out = new Output($platform->codec($profile));
71        $prefixed->render($out);
72        $quoted->render($out);
73
74        self::assertSame('CE6', $prefixed->text);
75        self::assertTrue($prefixed->hexadecimal);
76        self::assertSame('0a', $quoted->text);
77        self::assertTrue($quoted->hexadecimal);
78        self::assertSame("0xCE6 x'0a'", (new Lexical())->join($out->pieces()));
79    }
80
81    public function testLoweredDecimalAndFloatNumeralsAreKeptAsWritten(): void
82    {
83        $platform = new Platform();
84        $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
85        $parser = $platform->parser($profile);
86        $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
87        $decimal = $lowering->numbers->numeral($parser->parse('ALTER TABLE t COALESCE PARTITION 1.5')->find('real_ulong_num')[0]);
88        $float = $lowering->numbers->numeral($parser->parse('ALTER TABLE t COALESCE PARTITION 1e2')->find('real_ulong_num')[0]);
89
90        self::assertSame('1.5', $decimal->text);
91        self::assertSame('1e2', $float->text);
92    }
93
94    public function testRejectsASignedNumber(): void
95    {
96        $this->expectExceptionMessage('A numeral is an unsigned number or the digits of a hexadecimal literal.');
97
98        new Numeral('-1');
99    }
100
101    public function testRejectsAWordAsADecimalNumber(): void
102    {
103        $this->expectExceptionMessage('A numeral is an unsigned number or the digits of a hexadecimal literal.');
104
105        new Numeral('16M');
106    }
107
108    public function testRejectsNonHexadecimalDigits(): void
109    {
110        $this->expectExceptionMessage('A numeral is an unsigned number or the digits of a hexadecimal literal.');
111
112        new Numeral('zz', true);
113    }
114}
115