packages/sql-semantics-sqlite/tests/Unit/Statement/Expression/Literal/RealLiteralTest.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\RealLiteral;
15use SqlSemantics\Platform\Sqlite\Statement\Query\ResultColumn;
16use SqlSemantics\Platform\Sqlite\Statement\Query\Select;
17use SqlSemantics\Platform\Sqlite\Statement\Type\Storage;
18
19#[CoversClass(RealLiteral::class)]
20#[Medium]
21final class RealLiteralTest extends TestCase
22{
23    public function testKeepsTheExactParts(): void
24    {
25        $literal = new RealLiteral('', '5', '+2');
26
27        self::assertSame('', $literal->whole);
28        self::assertSame('5', $literal->fraction);
29        self::assertSame('+2', $literal->exponent);
30        self::assertNull((new RealLiteral('7', null, '-1'))->fraction);
31        self::assertNull((new RealLiteral('7', ''))->exponent);
32    }
33
34    public function testDeriveScalarGivesReal(): void
35    {
36        $operation = (new Semantics())->analyze('SELECT 1.5, .5, 5., 1e2, 1.50E-3, 1E+2', []);
37        $statement = $operation->statement;
38
39        self::assertInstanceOf(Select::class, $statement);
40        $literals = array_map(static function (object $column): RealLiteral {
41            self::assertInstanceOf(ResultColumn::class, $column);
42            self::assertInstanceOf(RealLiteral::class, $column->expression);
43
44            return $column->expression;
45        }, $statement->columns);
46        self::assertSame(['1', '', '5', '1', '1', '1'], array_map(static fn (RealLiteral $literal): string => $literal->whole, $literals));
47        self::assertSame(['5', '5', '', null, '50', null], array_map(static fn (RealLiteral $literal): ?string => $literal->fraction, $literals));
48        self::assertSame([null, null, null, '2', '-3', '+2'], array_map(static fn (RealLiteral $literal): ?string => $literal->exponent, $literals));
49        $fact = $operation->facts->scalar($literals[4]);
50        self::assertEquals(new Known(Storage::Real), $fact->type);
51        self::assertSame(Nullability::NotNull, $fact->nullability);
52        self::assertNull($fact->resolution);
53        self::assertEquals(new Known(Storage::Real), $operation->field(0)->type);
54        self::assertSame([], $operation->facts->diagnostics);
55    }
56
57    public function testRenderWritesThePartsWithALowerCaseExponentMarker(): void
58    {
59        self::assertSame('SELECT 1.5 AS c1, .5 AS c2, 5. AS c3, 1e2 AS c4, 1.50e-3 AS c5, 1e+2 AS c6', (new Semantics())->analyze('select 1.5 AS c1, .5 AS c2, 5. AS c3, 1e2 AS c4, 1.50E-3 AS c5, 1E+2 AS c6')->toString());
60    }
61
62    public function testRenderWritesANewlyBuiltLiteral(): void
63    {
64        $semantics = new Semantics();
65        $operation = new Operation($semantics->context(), new Select([new ResultColumn(new RealLiteral('', '5', '+2')), new ResultColumn(new RealLiteral('7', null, '-1')), new ResultColumn(new RealLiteral('3', '14'))]));
66
67        self::assertSame('SELECT .5e+2, 7e-1, 3.14', $operation->toString());
68    }
69}
70