packages/sql-semantics-sqlite/tests/Unit/Statement/Expression/Literal/IntegerLiteralTest.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\Known;
12use SqlSemantics\Platform\Sqlite\Model\Statement\Type\Nullability;
13use SqlSemantics\Platform\Sqlite\Semantics;
14use SqlSemantics\Platform\Sqlite\Statement\Expression\Literal\IntegerLiteral;
15use SqlSemantics\Platform\Sqlite\Statement\Query\ResultColumn;
16use SqlSemantics\Platform\Sqlite\Statement\Query\Select;
17use SqlSemantics\Platform\Sqlite\Statement\Type\Storage;
18
19#[CoversClass(IntegerLiteral::class)]
20#[Medium]
21final class IntegerLiteralTest extends TestCase
22{
23 public function testFitsAcceptsTheLargestSignedSixtyFourBitValue(): void
24 {
25 self::assertTrue((new IntegerLiteral('9223372036854775807'))->fits());
26 self::assertTrue((new IntegerLiteral('0'))->fits());
27 self::assertTrue((new IntegerLiteral('00009223372036854775807'))->fits());
28 }
29
30 public function testFitsRefusesOneMore(): void
31 {
32 self::assertFalse((new IntegerLiteral('9223372036854775808'))->fits());
33 self::assertFalse((new IntegerLiteral('09223372036854775808'))->fits());
34 self::assertFalse((new IntegerLiteral('10000000000000000000'))->fits());
35 }
36
37 public function testDeriveScalarGivesIntegerWhenTheValueFits(): void
38 {
39 $operation = (new Semantics())->analyze('SELECT 1_000, 9223372036854775807', []);
40 $statement = $operation->statement;
41
42 self::assertInstanceOf(Select::class, $statement);
43 self::assertInstanceOf(ResultColumn::class, $statement->columns[0]);
44 self::assertInstanceOf(IntegerLiteral::class, $statement->columns[0]->expression);
45 self::assertSame('1000', $statement->columns[0]->expression->digits);
46 $fact = $operation->facts->scalar($statement->columns[0]->expression);
47 self::assertEquals(new Known(Storage::Integer), $fact->type);
48 self::assertSame(Nullability::NotNull, $fact->nullability);
49 self::assertNull($fact->resolution);
50 self::assertEquals(new Known(Storage::Integer), $operation->field(1)->type);
51 self::assertSame([], $operation->facts->diagnostics);
52 }
53
54 public function testDeriveScalarGivesRealForALargerValue(): void
55 {
56 $operation = (new Semantics())->analyze('SELECT 9223372036854775808', []);
57 $statement = $operation->statement;
58
59 self::assertInstanceOf(Select::class, $statement);
60 self::assertInstanceOf(ResultColumn::class, $statement->columns[0]);
61 self::assertInstanceOf(IntegerLiteral::class, $statement->columns[0]->expression);
62 self::assertSame('9223372036854775808', $statement->columns[0]->expression->digits);
63 self::assertEquals(new Known(Storage::Real), $operation->field(0)->type);
64 self::assertSame(Nullability::NotNull, $operation->field(0)->nullability);
65 }
66
67 public function testRenderWritesTheDigitsWithoutSeparators(): void
68 {
69 self::assertSame('SELECT 1000 AS c1, 007 AS c2, 9223372036854775808 AS c3', (new Semantics())->analyze('select 1_000 AS c1, 007 AS c2, 9223372036854775808 AS c3')->toString());
70 }
71
72 public function testRenderWritesANewlyBuiltLiteral(): void
73 {
74 $semantics = new Semantics();
75 $operation = new Operation($semantics->context(), new Select([new ResultColumn(new IntegerLiteral('42')), new ResultColumn(new IntegerLiteral('0'))]));
76
77 self::assertSame('SELECT 42, 0', $operation->toString());
78 }
79}
80