packages/sql-semantics-postgres/tests/Unit/Lowering/Routine/SignatureRuleTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Lowering\Routine;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Small;
9use PHPUnit\Framework\TestCase;
10use SqlParser\PostgreSql\PostgreSqlParser;
11use SqlSemantics\Platform\PostgreSql\Lowering\Lowering;
12use SqlSemantics\Platform\PostgreSql\Lowering\Routine\SignatureRule;
13use SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease;
14use SqlSemantics\Platform\PostgreSql\Model\Contract\LanguageProfile;
15use SqlSemantics\Platform\PostgreSql\Model\Lowering\Leaves;
16use SqlSemantics\Platform\PostgreSql\Platform;
17use SqlSemantics\Platform\PostgreSql\Statement\Routine\ParameterMode;
18use SqlSemantics\Platform\PostgreSql\Statement\Routine\Signature\OperatorArity;
19
20#[CoversClass(SignatureRule::class)]
21#[Small]
22final class SignatureRuleTest extends TestCase
23{
24    public function testFunctionLowersEachForm(): void
25    {
26        $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
27        $tree = (new PostgreSqlParser('pg-17.2'))->parse('DROP FUNCTION f(int), left, s.g');
28        $rule = new SignatureRule($lowering);
29        self::assertSame(['f', 'left', 'g'], [$rule->function($tree->find('function_with_argtypes')[0])->name->last()->value, $rule->function($tree->find('function_with_argtypes')[1])->name->last()->value, $rule->function($tree->find('function_with_argtypes')[2])->name->last()->value]);
30    }
31
32    public function testFunctionRejectsAnotherNonterminal(): void
33    {
34        $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
35        $tree = (new PostgreSqlParser('pg-17.2'))->parse('DROP TABLE t');
36        $this->expectExceptionMessage('No semantic rule is implemented for: any_name: ColId');
37        (new SignatureRule($lowering))->function($tree->find('any_name')[0]);
38    }
39
40    public function testFunctionsLowersTheList(): void
41    {
42        $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
43        $tree = (new PostgreSqlParser('pg-17.2'))->parse('DROP FUNCTION f, g, h');
44        self::assertCount(3, (new SignatureRule($lowering))->functions($tree->find('function_with_argtypes_list')[0]));
45    }
46
47    public function testArgumentsLowersAnEmptyList(): void
48    {
49        $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
50        $tree = (new PostgreSqlParser('pg-17.2'))->parse('DROP FUNCTION f()');
51        self::assertSame([], (new SignatureRule($lowering))->arguments($tree->find('func_args')[0]));
52    }
53
54    public function testParameterKeepsTheOrderOfNameAndMode(): void
55    {
56        $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
57        $tree = (new PostgreSqlParser('pg-17.2'))->parse('DROP FUNCTION f(a IN int, OUT b int, VARIADIC int[], c int)');
58        $rule = new SignatureRule($lowering);
59        $first = $rule->parameter($tree->find('func_arg')[0]);
60        $second = $rule->parameter($tree->find('func_arg')[1]);
61        self::assertSame([true, ParameterMode::In, false, ParameterMode::Out], [$first->nameFirst, $first->mode, $second->nameFirst, $second->mode]);
62        self::assertSame([ParameterMode::Variadic, 'c'], [$rule->parameter($tree->find('func_arg')[2])->mode, $rule->parameter($tree->find('func_arg')[3])->name?->value]);
63    }
64
65    public function testModeKeepsTheSpellingOfInOut(): void
66    {
67        $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
68        $tree = (new PostgreSqlParser('pg-17.2'))->parse('DROP FUNCTION f(IN OUT int, INOUT int)');
69        self::assertSame([ParameterMode::InAndOut, ParameterMode::InOut], [(new SignatureRule($lowering))->mode($tree->find('arg_class')[0]), (new SignatureRule($lowering))->mode($tree->find('arg_class')[1])]);
70    }
71
72    public function testAggregateLowersNameAndArguments(): void
73    {
74        $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
75        $tree = (new PostgreSqlParser('pg-17.2'))->parse('DROP AGGREGATE s.a(*)');
76        self::assertTrue((new SignatureRule($lowering))->aggregate($tree->find('aggregate_with_argtypes')[0])->arguments->star());
77    }
78
79    public function testAggregatesLowersTheList(): void
80    {
81        $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
82        $tree = (new PostgreSqlParser('pg-17.2'))->parse('DROP AGGREGATE a(*), b(int)');
83        self::assertCount(2, (new SignatureRule($lowering))->aggregates($tree->find('aggregate_with_argtypes_list')[0]));
84    }
85
86    public function testAggregateArgumentsLowersOrderedSets(): void
87    {
88        $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
89        $tree = (new PostgreSqlParser('pg-17.2'))->parse('DROP AGGREGATE a(ORDER BY int), b(text ORDER BY int, int)');
90        $rule = new SignatureRule($lowering);
91        $first = $rule->aggregateArguments($tree->find('aggr_args')[0]);
92        $second = $rule->aggregateArguments($tree->find('aggr_args')[1]);
93        self::assertSame([0, 1, 1, 2], [count($first->direct), count($first->ordered ?? []), count($second->direct), count($second->ordered ?? [])]);
94    }
95
96    public function testAggregateListLowersTheArguments(): void
97    {
98        $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
99        $tree = (new PostgreSqlParser('pg-17.2'))->parse('DROP AGGREGATE a(int, text)');
100        self::assertCount(2, (new SignatureRule($lowering))->aggregateList($tree->find('aggr_args_list')[0]));
101    }
102
103    public function testOperatorKeepsHowTheTypesAreWritten(): void
104    {
105        $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
106        $tree = (new PostgreSqlParser('pg-17.2'))->parse('DROP OPERATOR + (int, int), - (NONE, int), ! (int, NONE)');
107        $rule = new SignatureRule($lowering);
108        self::assertSame([OperatorArity::Binary, OperatorArity::Prefix, OperatorArity::Postfix], [$rule->operator($tree->find('operator_with_argtypes')[0])->arity, $rule->operator($tree->find('operator_with_argtypes')[1])->arity, $rule->operator($tree->find('operator_with_argtypes')[2])->arity]);
109    }
110
111    public function testOperatorsLowersTheList(): void
112    {
113        $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
114        $tree = (new PostgreSqlParser('pg-17.2'))->parse('DROP OPERATOR + (int, int), - (NONE, int)');
115        self::assertCount(2, (new SignatureRule($lowering))->operators($tree->find('operator_with_argtypes_list')[0]));
116    }
117}
118