33use lang \ast \Errors ;
44use lang \ast \unittest \emit \EmittingTest ;
55use test \{Assert , Before , Expect , Test };
6+ use util \Date ;
67
78class 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