Skip to content

Commit 15f1c26

Browse files
committed
Add JSX example
1 parent a1cc204 commit 15f1c26

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,35 @@ public function run($name, $args) {
167167
Assert::equals(['kind' => 'test', 'args' => [0, 1, 2]], json_decode($t->newInstance()->run('test', [1, 2]), true));
168168
}
169169

170+
#[Test]
171+
public function jsx_example() {
172+
$t= $this->type('class %T {
173+
private function jsx($strings, ... $arguments) {
174+
$s= "";
175+
$tag= false;
176+
foreach ($strings as $i => $string) {
177+
$s.= strtr($string, ["<>" => "<div>", "</>" => "</div>"]);
178+
$open= substr_count($string, "<");
179+
$close= substr_count($string, ">");
180+
if ($open > $close) {
181+
$s.= "\"".htmlspecialchars($arguments[$i] ?? "")."\"";
182+
} else {
183+
$s.= htmlspecialchars($arguments[$i] ?? "");
184+
}
185+
}
186+
return $s;
187+
}
188+
189+
public function run($title, $class= "test") {
190+
return $this->jsx`<>
191+
<h1 class=${$class}>${strtoupper($title)}</h1>
192+
</>`;
193+
}
194+
}');
195+
196+
Assert::matches('/<div>.+<h1 class="test">TEST &amp; RUN<\/h1>.+<\/div>/s', $t->newInstance()->run('Test & run'));
197+
}
198+
170199
#[Test, Expect(class: Errors::class, message: '/Unexpected string "Test" \[line 1 of .+\]/')]
171200
public function cannot_suffix_other_literals() {
172201
$this->evaluate('$f"Test"');

0 commit comments

Comments
 (0)