Skip to content

Commit 60a29f5

Browse files
committed
Add JSON example
1 parent 871a343 commit 60a29f5

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

src/test/php/lang/ast/syntax/php/unittest/TemplateLiteralsTest.class.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,28 @@ public function run($userId) {
145145
Assert::equals(['base' => '//example.com', 'path' => '/users/%40me'], $t->newInstance()->run('@me'));
146146
}
147147

148+
#[Test]
149+
public function json_example() {
150+
$t= $this->type('class %T {
151+
private function json($strings, ... $arguments) {
152+
$s= "";
153+
foreach ($arguments as $i => $argument) {
154+
$s.= $strings[$i].json_encode($argument);
155+
}
156+
return $s.($strings[$i + 1] ?? "");
157+
}
158+
159+
public function run($name, $args) {
160+
return $this->json`{
161+
"kind" : ${$name},
162+
"args" : ${[0, ...$args]}
163+
}`;
164+
}
165+
}');
166+
167+
Assert::equals(['kind' => 'test', 'args' => [0, 1, 2]], json_decode($t->newInstance()->run('test', [1, 2]), true));
168+
}
169+
148170
#[Test, Expect(class: Errors::class, message: '/Unexpected string "Test" \[line 1 of .+\]/')]
149171
public function cannot_suffix_other_literals() {
150172
$this->evaluate('$f"Test"');

0 commit comments

Comments
 (0)