From 2b3d45084986df9eb6d3b1d81d698f091e79748e Mon Sep 17 00:00:00 2001 From: Thomas Seven Date: Thu, 29 Jan 2015 01:32:41 +0100 Subject: [PATCH 1/3] Fixes and Tests for #105 --- src/Handlebars/Helper/IfHelper.php | 6 +++- src/Handlebars/Helper/UnlessHelper.php | 10 +++++- tests/Xamin/HandlebarsTest.php | 50 ++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 2 deletions(-) diff --git a/src/Handlebars/Helper/IfHelper.php b/src/Handlebars/Helper/IfHelper.php index e4dd945..b3cb543 100644 --- a/src/Handlebars/Helper/IfHelper.php +++ b/src/Handlebars/Helper/IfHelper.php @@ -52,7 +52,11 @@ class IfHelper implements Helper { if (is_numeric($args)) { $tmp = $args; - } else { + } elseif(preg_match('/^\'.*\'$/', trim($args))) { + $tmp = preg_replace('/^\'(.*)\'$/', '$1', trim($args)); + } elseif(preg_match('/^".*"$/', trim($args))) { + $tmp = preg_replace('/^"(.*)"$/', '$1', trim($args)); + } else { $tmp = $context->get($args); } diff --git a/src/Handlebars/Helper/UnlessHelper.php b/src/Handlebars/Helper/UnlessHelper.php index f851bf7..c8029b8 100644 --- a/src/Handlebars/Helper/UnlessHelper.php +++ b/src/Handlebars/Helper/UnlessHelper.php @@ -50,7 +50,15 @@ class UnlessHelper implements Helper */ public function execute(Template $template, Context $context, $args, $source) { - $tmp = $context->get($args); + if (is_numeric($args)) { + $tmp = $args; + } elseif(preg_match('/^\'.*\'$/', trim($args))) { + $tmp = preg_replace('/^\'(.*)\'$/', '$1', trim($args)); + } elseif(preg_match('/^".*"$/', trim($args))) { + $tmp = preg_replace('/^"(.*)"$/', '$1', trim($args)); + } else { + $tmp = $context->get($args); + } $context->push($context->last()); diff --git a/tests/Xamin/HandlebarsTest.php b/tests/Xamin/HandlebarsTest.php index 9fa5d5b..2b30d76 100644 --- a/tests/Xamin/HandlebarsTest.php +++ b/tests/Xamin/HandlebarsTest.php @@ -1155,4 +1155,54 @@ class HandlebarsTest extends \PHPUnit_Framework_TestCase $args = new \Handlebars\Arguments($argsString); $this->assertEquals($argsString, (string)$args); } + + + public function stringLiteralsInIfAndUnlessHelpersProvider() { + return array( + // IfHelper + array('{{#if "truthyString"}}true{{else}}false{{/if}}', array(), "true"), + array("{{#if 'truthyStringSingleQuotes'}}true{{else}}false{{/if}}", array(), "true"), + array("{{#if ''}}true{{else}}false{{/if}}", array(), "false"), + array("{{#if '0'}}true{{else}}false{{/if}}", array(), "false"), + array("{{#if (add 0 1)}}true{{else}}false{{/if}}", array(), "true"), + array("{{#if (add 1 -1)}}true{{else}}false{{/if}}", array(), "false"), + // UnlessHelper + array('{{#unless "truthyString"}}true{{else}}false{{/unless}}', array(), "false"), + array("{{#unless 'truthyStringSingleQuotes'}}true{{else}}false{{/unless}}", array(), "false"), + array("{{#unless ''}}true{{else}}false{{/unless}}", array(), "true"), + array("{{#unless '0'}}true{{else}}false{{/unless}}", array(), "true"), + array("{{#unless (add 0 1)}}true{{else}}false{{/unless}}", array(), "false"), + array("{{#unless (add 1 -1)}}true{{else}}false{{/unless}}", array(), "true"), + ); + } + + /** + * Test integer literals in the context of if and unless helpers + * + * @param string $template template text + * @param array $data context data + * @param string $results The Expected Results + * + * @dataProvider stringLiteralsInIfAndUnlessHelpersProvider + * + * @return void + */ + public function testStringLiteralsInIfAndUnlessHelpers($template, $data, $results) + { + $engine = new \Handlebars\Handlebars(); + + $engine->addHelper('add', function ($template, $context, $args) { + $sum = 0; + + foreach ($template->parseArguments($args) as $value) { + $sum += intval($context->get($value)); + } + + return $sum; + }); + + $res = $engine->render($template, $data); + $this->assertEquals($res, $results); + } + } From 84b3031a449fd6a5b5050e03a5550226c2aec84b Mon Sep 17 00:00:00 2001 From: Thomas Seven Date: Thu, 29 Jan 2015 11:59:14 +0100 Subject: [PATCH 2/3] Make the fix for #105 more elegant, using $template->parseArguments() --- src/Handlebars/Helper/IfHelper.php | 11 ++--------- src/Handlebars/Helper/UnlessHelper.php | 11 ++--------- 2 files changed, 4 insertions(+), 18 deletions(-) diff --git a/src/Handlebars/Helper/IfHelper.php b/src/Handlebars/Helper/IfHelper.php index b3cb543..8b90924 100644 --- a/src/Handlebars/Helper/IfHelper.php +++ b/src/Handlebars/Helper/IfHelper.php @@ -50,15 +50,8 @@ class IfHelper implements Helper */ public function execute(Template $template, Context $context, $args, $source) { - if (is_numeric($args)) { - $tmp = $args; - } elseif(preg_match('/^\'.*\'$/', trim($args))) { - $tmp = preg_replace('/^\'(.*)\'$/', '$1', trim($args)); - } elseif(preg_match('/^".*"$/', trim($args))) { - $tmp = preg_replace('/^"(.*)"$/', '$1', trim($args)); - } else { - $tmp = $context->get($args); - } + $parsedArgs = $template->parseArguments($args); +$tmp = $context->get($parsedArgs[0]); $context->push($context->last()); if ($tmp) { diff --git a/src/Handlebars/Helper/UnlessHelper.php b/src/Handlebars/Helper/UnlessHelper.php index c8029b8..e099532 100644 --- a/src/Handlebars/Helper/UnlessHelper.php +++ b/src/Handlebars/Helper/UnlessHelper.php @@ -50,15 +50,8 @@ class UnlessHelper implements Helper */ public function execute(Template $template, Context $context, $args, $source) { - if (is_numeric($args)) { - $tmp = $args; - } elseif(preg_match('/^\'.*\'$/', trim($args))) { - $tmp = preg_replace('/^\'(.*)\'$/', '$1', trim($args)); - } elseif(preg_match('/^".*"$/', trim($args))) { - $tmp = preg_replace('/^"(.*)"$/', '$1', trim($args)); - } else { - $tmp = $context->get($args); - } + $parsedArgs = $template->parseArguments($args); + $tmp = $context->get($parsedArgs[0]); $context->push($context->last()); From 8eb24bd9e42acaacb13c159144a09aa85eaa0004 Mon Sep 17 00:00:00 2001 From: Thomas Seven Date: Thu, 29 Jan 2015 12:06:21 +0100 Subject: [PATCH 3/3] Fixed indentation and comments in the fix for #105 --- src/Handlebars/Helper/IfHelper.php | 2 +- tests/Xamin/HandlebarsTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Handlebars/Helper/IfHelper.php b/src/Handlebars/Helper/IfHelper.php index 8b90924..9c0cb4a 100644 --- a/src/Handlebars/Helper/IfHelper.php +++ b/src/Handlebars/Helper/IfHelper.php @@ -51,7 +51,7 @@ class IfHelper implements Helper public function execute(Template $template, Context $context, $args, $source) { $parsedArgs = $template->parseArguments($args); -$tmp = $context->get($parsedArgs[0]); + $tmp = $context->get($parsedArgs[0]); $context->push($context->last()); if ($tmp) { diff --git a/tests/Xamin/HandlebarsTest.php b/tests/Xamin/HandlebarsTest.php index 2b30d76..5be6d48 100644 --- a/tests/Xamin/HandlebarsTest.php +++ b/tests/Xamin/HandlebarsTest.php @@ -1177,7 +1177,7 @@ class HandlebarsTest extends \PHPUnit_Framework_TestCase } /** - * Test integer literals in the context of if and unless helpers + * Test string literals in the context of if and unless helpers * * @param string $template template text * @param array $data context data