packages/sql-semantics-sqlite/tests/Unit/Model/Rendering/OutputTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Model\Rendering;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Small;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\Sqlite\Model\Contract\NameUse;
11use SqlSemantics\Platform\Sqlite\Model\Rendering\Lexical;
12use SqlSemantics\Platform\Sqlite\Model\Rendering\Output;
13use SqlSemantics\Platform\Sqlite\Model\Rendering\Piece;
14use SqlSemantics\Platform\Sqlite\Model\Rendering\PieceKind;
15use SqlSemantics\Platform\Sqlite\Model\Statement\Identifier\Name;
16use SqlSemantics\Platform\Sqlite\Model\Statement\Spelling\Layout;
17use SqlSemantics\Platform\Sqlite\Model\Statement\Spelling\Spelled;
18use SqlSemantics\Platform\Sqlite\Rendering\Codec;
19use SqlSemantics\Platform\Sqlite\Statement\Expression\Literal\IntegerLiteral;
20use SqlSemantics\Platform\Sqlite\Statement\Expression\Literal\NullLiteral;
21
22#[CoversClass(Output::class)]
23#[Small]
24final class OutputTest extends TestCase
25{
26 public function testKeywordWritesEachUpperCaseWordAsAPiece(): void
27 {
28 $output = new Output(new Codec());
29
30 $output->keyword('ORDER', 'BY');
31
32 self::assertSame(['ORDER', 'BY'], array_map(static fn (Piece $piece): string => $piece->text, $output->pieces()));
33 self::assertSame(PieceKind::Keyword, $output->pieces()[0]->kind);
34 }
35
36 public function testKeywordRefusesAnythingButOneUpperCaseWord(): void
37 {
38 $output = new Output(new Codec());
39
40 $this->expectExceptionMessage('A keyword piece is one upper-case word.');
41
42 $output->keyword('order by');
43 }
44
45 public function testSymbolWritesPunctuationOnly(): void
46 {
47 $output = new Output(new Codec());
48
49 $output->symbol('(')->symbol('<>')->symbol('->>');
50
51 self::assertSame(['(', '<>', '->>'], array_map(static fn (Piece $piece): string => $piece->text, $output->pieces()));
52 }
53
54 public function testSymbolRefusesLetters(): void
55 {
56 $output = new Output(new Codec());
57
58 $this->expectExceptionMessage('A symbol piece contains punctuation only.');
59
60 $output->symbol('IS');
61 }
62
63 public function testNameIsSpelledByTheCodecForItsPosition(): void
64 {
65 $output = new Output(new Codec());
66
67 $output->name(new Name('order'), NameUse::Relation)->name(new Name('a'));
68
69 self::assertSame(['`order`', 'a'], array_map(static fn (Piece $piece): string => $piece->text, $output->pieces()));
70 self::assertSame(PieceKind::Name, $output->pieces()[0]->kind);
71 }
72
73 public function testSpelledWritesALiteralSpelling(): void
74 {
75 $output = new Output(new Codec());
76
77 $output->spelled("'it''s'");
78
79 self::assertSame("'it''s'", $output->pieces()[0]->text);
80 self::assertSame(PieceKind::Literal, $output->pieces()[0]->kind);
81 }
82
83 public function testSpelledRefusesAnEmptySpelling(): void
84 {
85 $output = new Output(new Codec());
86
87 $this->expectExceptionMessage('A spelled piece is not empty.');
88
89 $output->spelled('');
90 }
91
92 public function testGlueJoinsOnlyTheNextPiece(): void
93 {
94 $output = new Output(new Codec());
95
96 $output->symbol('-')->glue()->spelled('1')->spelled('2');
97
98 self::assertSame([false, true, false], array_map(static fn (Piece $piece): bool => $piece->glued, $output->pieces()));
99 }
100
101 public function testNodeWritesAChildAndNothingForNull(): void
102 {
103 $output = new Output(new Codec());
104
105 $output->node(new IntegerLiteral('42'))->node(null)->node(new NullLiteral());
106
107 self::assertSame(['42', 'NULL'], array_map(static fn (Piece $piece): string => $piece->text, $output->pieces()));
108 }
109
110 public function testListSeparatesChildrenWithTheSymbol(): void
111 {
112 $output = new Output(new Codec());
113
114 $output->list([new IntegerLiteral('1'), new IntegerLiteral('2'), new IntegerLiteral('3')])->list([], ',')->list([new IntegerLiteral('4')], ';');
115
116 self::assertSame(['1', ',', '2', ',', '3', '4'], array_map(static fn (Piece $piece): string => $piece->text, $output->pieces()));
117 }
118
119 public function testAddRecordsAPieceAndConsumesAPendingGlue(): void
120 {
121 $output = new Output(new Codec());
122
123 $output->glue();
124 $output->add(PieceKind::Literal, 'x');
125 $output->add(PieceKind::Literal, 'y');
126
127 self::assertTrue($output->pieces()[0]->glued);
128 self::assertFalse($output->pieces()[1]->glued);
129 }
130
131 public function testPiecesAreEmptyBeforeAnythingIsWritten(): void
132 {
133 self::assertSame([], (new Output(new Codec()))->pieces());
134 }
135
136 public function testLayoutRespellsEachRenderedPieceAndKeepsTheRenderedText(): void
137 {
138 $output = new Output(new Codec());
139 $layout = new Layout([new Spelled('', 'null')]);
140
141 $output->keyword('SELECT')->layout($layout, new NullLiteral());
142
143 self::assertSame(['SELECT', 'null'], array_map(static fn (Piece $piece): string => $piece->text, $output->pieces()));
144 self::assertSame(['SELECT', 'NULL'], array_map(static fn (Piece $piece): string => $piece->text, $output->canonical()));
145 self::assertSame('SELECT null', (new Lexical())->join($output->pieces()));
146 }
147
148 public function testLayoutWithoutALayoutRendersRegularly(): void
149 {
150 $output = new Output(new Codec());
151
152 $output->layout(null, new IntegerLiteral('7'));
153
154 self::assertSame(['7'], array_map(static fn (Piece $piece): string => $piece->text, $output->pieces()));
155 self::assertSame(['7'], array_map(static fn (Piece $piece): string => $piece->text, $output->canonical()));
156 }
157
158 public function testLayoutRefusesALayoutThatDoesNotAlignWithTheRenderedPieces(): void
159 {
160 $output = new Output(new Codec());
161
162 $this->expectExceptionMessage('The layout spells 2 tokens where the rendering writes 1.');
163
164 $output->layout(new Layout([new Spelled('', '7'), new Spelled(' ', '8')]), new IntegerLiteral('7'));
165 }
166
167 public function testCanonicalKeepsTheRenderedTextOfEveryPiece(): void
168 {
169 $output = (new Output(new Codec()))->keyword('SELECT')->layout(new Layout([new Spelled('', '7')]), new IntegerLiteral('7'));
170
171 self::assertSame(['SELECT', '7'], array_map(static fn (Piece $piece): string => $piece->text, $output->canonical()));
172 }
173
174 public function testLayoutWritesItsTrailBeforeTheNextPiece(): void
175 {
176 $output = new Output(new Codec());
177
178 $output->layout(new Layout([new Spelled('', '7')], ' /* c */'), new IntegerLiteral('7'))->symbol(',');
179
180 self::assertSame('7 /* c */,', (new Lexical())->join($output->pieces()));
181 self::assertSame('', $output->trailing());
182 }
183
184 public function testTrailingAnswersATrailNothingFollowed(): void
185 {
186 $output = new Output(new Codec());
187
188 $output->layout(new Layout([new Spelled('', '7')], ' -- c'), new IntegerLiteral('7'));
189
190 self::assertSame(' -- c', $output->trailing());
191 }
192}
193