Skip to content

Commit

Permalink
Add test for helpers inside sections
Browse files Browse the repository at this point in the history
  • Loading branch information
thekid committed Oct 22, 2023
1 parent 64ba24d commit 9da43b4
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions src/test/php/com/handlebarsjs/unittest/ExecutionTest.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,20 @@ class ExecutionTest {
protected function evaluate($template, $variables, $templates= ['test' => 'Partial']) {
return (new HandlebarsEngine())
->withTemplates(new InMemory($templates))
->withHelper('date', function($node, $context, $options) { return date('Y-m-d', $options[0] ?? time()); })
->withHelper('time', ['short' => ['24' => function($node, $context, $options) { return date('H:i', $options[0] ?? time()); }]])
->withHelper('code', function($node, $context, $options) { return '<code>'.$options[0].'</code>'; })
->withHelper('date', function($node, $context, $options) {
return date('Y-m-d', $options[0] ?? time());
})
->withHelper('time', ['short' => ['24' => function($node, $context, $options) {
return date('H:i', $options[0] ?? time());
}]])
->withHelper('code', function($node, $context, $options) {
return '<code>'.$options[0].'</code>';
})
->withHelper('match', function($node, $context, $options) {
if ($context->isTruthy($options[0])) {
return $context->engine->render($options['fn'], $context->newInstance($options[0]));
}
})
->render($template, $variables)
;
}
Expand Down Expand Up @@ -219,4 +230,9 @@ public function handlebars_in_replacement() {
public function handlebars_returned_from_helper() {
Assert::equals('<code>{{name}}</code>', $this->evaluate('{{& code in}}', ['in' => '{{name}}']));
}

#[Test]
public function helper_used_in_section() {
Assert::equals('Test', $this->evaluate('{{#match in}}{{name}}{{/match}}', ['in' => ['name' => 'Test']]));
}
}

0 comments on commit 9da43b4

Please sign in to comment.