packages/sql-semantics-sqlite/tests/Unit/Statement/Expression/Literal/HexLiteralTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Expression\Literal;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\Sqlite\Model\Statement\Operation;
11use SqlSemantics\Platform\Sqlite\Model\Statement\Type\Invalid;
12use SqlSemantics\Platform\Sqlite\Model\Statement\Type\Known;
13use SqlSemantics\Platform\Sqlite\Model\Statement\Type\Nullability;
14use SqlSemantics\Platform\Sqlite\Semantics;
15use SqlSemantics\Platform\Sqlite\Statement\Expression\Literal\HexLiteral;
16use SqlSemantics\Platform\Sqlite\Statement\Query\Problem\Misuse;
17use SqlSemantics\Platform\Sqlite\Statement\Query\Problem\MisuseRule;
18use SqlSemantics\Platform\Sqlite\Statement\Query\ResultColumn;
19use SqlSemantics\Platform\Sqlite\Statement\Query\Select;
20use SqlSemantics\Platform\Sqlite\Statement\Type\Storage;
21
22#[CoversClass(HexLiteral::class)]
23#[Medium]
24final class HexLiteralTest extends TestCase
25{
26    public function testDeriveScalarGivesInteger(): void
27    {
28        $operation = (new Semantics())->inspect('SELECT 0x1f, 0xFFFFFFFFFFFFFFFF, 0x0000000000000000001', []);
29        $statement = $operation->statement;
30
31        self::assertInstanceOf(Select::class, $statement);
32        self::assertInstanceOf(ResultColumn::class, $statement->columns[0]);
33        self::assertInstanceOf(HexLiteral::class, $statement->columns[0]->expression);
34        self::assertSame('1F', $statement->columns[0]->expression->digits);
35        $fact = $operation->facts->scalar($statement->columns[0]->expression);
36        self::assertEquals(new Known(Storage::Integer), $fact->type);
37        self::assertSame(Nullability::NotNull, $fact->nullability);
38        self::assertEquals(new Known(Storage::Integer), $operation->field(1)->type);
39        self::assertEquals(new Known(Storage::Integer), $operation->field(2)->type);
40        self::assertSame([], $operation->facts->diagnostics);
41    }
42
43    public function testDeriveScalarReportsALiteralWiderThanSixtyFourBits(): void
44    {
45        $operation = (new Semantics())->inspect('SELECT 0x10000000000000000', []);
46        $statement = $operation->statement;
47
48        self::assertInstanceOf(Select::class, $statement);
49        self::assertInstanceOf(ResultColumn::class, $statement->columns[0]);
50        $fact = $operation->facts->scalar($statement->columns[0]->expression);
51        self::assertInstanceOf(Invalid::class, $fact->type);
52        self::assertEquals(new Misuse(MisuseRule::HexLiteralTooBig), $fact->type->cause);
53        self::assertSame(Nullability::NotNull, $fact->nullability);
54        self::assertSame([$fact->type->cause], $operation->facts->diagnostics);
55        self::assertSame('hex literal too big', $operation->facts->diagnostics[0]->message());
56    }
57
58    public function testRenderWritesThePrefixAndUpperCaseDigits(): void
59    {
60        self::assertSame('SELECT 0x1F AS c1, 0xABC AS c2', (new Semantics())->inspect('select 0X1f AS c1, 0xabc AS c2')->toString());
61    }
62
63    public function testRenderWritesANewlyBuiltLiteral(): void
64    {
65        $semantics = new Semantics();
66        $operation = new Operation($semantics->context(), new Select([new ResultColumn(new HexLiteral('00FF'))]));
67
68        self::assertSame('SELECT 0x00FF', $operation->toString());
69    }
70}
71