Skip to content

Commit 871a343

Browse files
committed
Verify use statement is honored
1 parent 82952aa commit 871a343

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
use lang\ast\Errors;
44
use lang\ast\unittest\emit\EmittingTest;
55
use test\{Assert, Before, Expect, Test};
6+
use util\Date;
67

78
class TemplateLiteralsTest extends EmittingTest {
89
private $format;
910

1011
/** Evaluates a strign template */
1112
private function evaluate(string $template, array $arguments= []) {
12-
return $this->type('class %T { public function run($f, $arguments) { return '.$template.'; } }')
13+
return $this->type('use util\Date; class %T { public function run($f, $arguments) { return '.$template.'; } }')
1314
->newInstance()
1415
->run($this->format, $arguments)
1516
;
@@ -20,7 +21,12 @@ public function format() {
2021
$this->format= function($strings, ... $arguments) {
2122
$r= '';
2223
foreach ($strings as $i => $string) {
23-
$r.= $string.htmlspecialchars($arguments[$i] ?? '');
24+
$argument= $arguments[$i] ?? '';
25+
if ($argument instanceof Date) {
26+
$r.= $string.$argument->format('%Y-%m-%d');
27+
} else {
28+
$r.= $string.htmlspecialchars($argument);
29+
}
2430
}
2531
return $r;
2632
};
@@ -71,6 +77,14 @@ public function evaluates_global_constant() {
7177
Assert::equals('PHP_OS = '.PHP_OS, $this->evaluate('$f`PHP_OS = ${PHP_OS}`'));
7278
}
7379

80+
#[Test]
81+
public function use_statement_honored() {
82+
Assert::equals(
83+
'It is 1970-01-01',
84+
$this->evaluate('$f`It is ${new Date(0)}`')
85+
);
86+
}
87+
7488
#[Test]
7589
public function argument_passed() {
7690
Assert::equals(

0 commit comments

Comments
 (0)