packages/sql-semantics-postgres/tests/Unit/Rules/Expression/Typing/PrefixTypingTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Rules\Expression\Typing;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Small;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease;
11use SqlSemantics\Platform\PostgreSql\Model\Contract\LanguageProfile;
12use SqlSemantics\Platform\PostgreSql\Model\Statement\Fact\ScalarFact;
13use SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name;
14use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Known;
15use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Nullability;
16use SqlSemantics\Platform\PostgreSql\Platform;
17use SqlSemantics\Platform\PostgreSql\Rules\Expression\Typing\PrefixTyping;
18use SqlSemantics\Platform\PostgreSql\Statement\Literal\Constant;
19use SqlSemantics\Platform\PostgreSql\Statement\Literal\IntegerConstant;
20use SqlSemantics\Platform\PostgreSql\Statement\Literal\NumericConstant;
21use SqlSemantics\Platform\PostgreSql\Statement\Name\OperatorName;
22use SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin;
23
24#[CoversClass(PrefixTyping::class)]
25#[Small]
26final class PrefixTypingTest extends TestCase
27{
28 public function testPrefixFoldsANegativeConstant(): void
29 {
30 $context = (new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true);
31 $constant = new Constant(new NumericConstant('9223372036854775808'));
32 $fact = (new PrefixTyping())->prefix($context, new OperatorName(new Name('-')), new ScalarFact(new Known(Builtin::Numeric), Nullability::NotNull), $constant);
33 self::assertEquals(new Known(Builtin::Int8), $fact->type);
34 }
35
36 public function testNegativeTypesByValue(): void
37 {
38 self::assertSame(
39 [Builtin::Int4, Builtin::Int4, Builtin::Int8, Builtin::Int8, Builtin::Numeric, Builtin::Numeric],
40 [
41 (new PrefixTyping())->negative(new IntegerConstant('2147483647')), (new PrefixTyping())->negative(new NumericConstant('0x8000_0000')),
42 (new PrefixTyping())->negative(new NumericConstant('2147483649')), (new PrefixTyping())->negative(new NumericConstant('9223372036854775808')),
43 (new PrefixTyping())->negative(new NumericConstant('9223372036854775809')), (new PrefixTyping())->negative(new NumericConstant('1.5')),
44 ],
45 );
46 }
47
48 public function testUnaryKeepsANumberType(): void
49 {
50 $context = (new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true);
51 self::assertEquals(new Known(Builtin::Float8), (new PrefixTyping())->unary($context, new OperatorName(new Name('|/')), new Known(Builtin::Int4)));
52 }
53
54}
55