packages/sql-semantics-mysql/tests/Unit/Rules/RadixSpellingTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Rules;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Small;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\MySql\Rules\RadixSpelling;
11
12#[CoversClass(RadixSpelling::class)]
13#[Small]
14final class RadixSpellingTest extends TestCase
15{
16 public function testDigitsReadsBothSpellingsOfAHexadecimalLiteral(): void
17 {
18 self::assertSame('1F', (new RadixSpelling())->digits("X'1F'"));
19 self::assertSame('1f', (new RadixSpelling())->digits("x'1f'"));
20 self::assertSame('1F', (new RadixSpelling())->digits('0x1F'));
21 self::assertSame('01f', (new RadixSpelling())->digits('0x01f'));
22 self::assertSame('', (new RadixSpelling())->digits("X''"));
23 }
24
25 public function testDigitsReadsBothSpellingsOfABitLiteral(): void
26 {
27 self::assertSame('101', (new RadixSpelling())->digits("b'101'"));
28 self::assertSame('0101', (new RadixSpelling())->digits("B'0101'"));
29 self::assertSame('101', (new RadixSpelling())->digits('0b101'));
30 self::assertSame('', (new RadixSpelling())->digits("b''"));
31 }
32
33 public function testHexadecimalQuotesAnEvenCountAndPrefixesAnOddCount(): void
34 {
35 self::assertSame("x'1F'", (new RadixSpelling())->hexadecimal('1F'));
36 self::assertSame("x''", (new RadixSpelling())->hexadecimal(''));
37 self::assertSame("x'00ab'", (new RadixSpelling())->hexadecimal('00ab'));
38 self::assertSame('0x1', (new RadixSpelling())->hexadecimal('1'));
39 self::assertSame('0xabc', (new RadixSpelling())->hexadecimal('abc'));
40 }
41
42 public function testHexadecimalRoundTripsThroughDigits(): void
43 {
44 self::assertSame('abc', (new RadixSpelling())->digits((new RadixSpelling())->hexadecimal('abc')));
45 self::assertSame('00AB', (new RadixSpelling())->digits((new RadixSpelling())->hexadecimal('00AB')));
46 self::assertSame('', (new RadixSpelling())->digits((new RadixSpelling())->hexadecimal('')));
47 }
48
49 public function testBitsQuotesAnyCountOfDigits(): void
50 {
51 self::assertSame("b'101'", (new RadixSpelling())->bits('101'));
52 self::assertSame("b'1'", (new RadixSpelling())->bits('1'));
53 self::assertSame("b''", (new RadixSpelling())->bits(''));
54 self::assertSame('0011', (new RadixSpelling())->digits((new RadixSpelling())->bits('0011')));
55 }
56}
57