Added Tests and changed internal representation of quoted string literals from strangely quoted string to an instance of \Handlebars\String

This commit is contained in:
Chris Gray 2014-01-20 09:57:16 -06:00
parent 49dcbb2573
commit a44777f52f
3 changed files with 48 additions and 5 deletions

View File

@ -189,7 +189,9 @@ class Context
*/
public function get($variableName, $strict = false)
{
//Need to clean up
if ($variableName instanceof \Handlebars\String){
return (string)$variableName;
}
$variableName = trim($variableName);
$level = 0;
while (substr($variableName, 0, 3) == '../') {
@ -217,7 +219,6 @@ class Context
'can not find variable in context'
);
}
return '';
} elseif ($variableName == '.' || $variableName == 'this') {
return $current;
@ -225,8 +226,6 @@ class Context
$current = $this->lastIndex();
} elseif ($variableName == '@key') {
$current = $this->lastKey();
} elseif ($variableName instanceof \Handlebars\String){
$current = (string)$variableName;
} else {
$chunks = explode('.', $variableName);
foreach ($chunks as $chunk) {

View File

@ -446,7 +446,6 @@ class Template
}
}
return $args;
}
}

View File

@ -94,6 +94,16 @@ class HandlebarsTest extends \PHPUnit_Framework_TestCase
'{{data.length}}',
array("data"=>array("length"=>"15 inches", "test","test","test")),
"15 inches"
),
array(
'{{data.0}}',
array("data" => array(1,2,3,4)),
'1'
),
array(
'{{data.property.3}}',
array("data"=>array("property"=>array(1,2,3,4))),
'4'
)
);
}
@ -527,6 +537,41 @@ class HandlebarsTest extends \PHPUnit_Framework_TestCase
}
public function stringLiteralInCustomHelperProvider(){
return array(
array('{{#test2 arg1 "Argument 2"}}', array("arg1"=>"Argument 1"), "Argument 1:Argument 2"),
array('{{#test2 "Argument 1" "Argument 2"}}', array("arg1"=>"Argument 1"), "Argument 1:Argument 2"),
array('{{#test2 "Argument 1" arg2}}', array("arg2"=>"Argument 2"), "Argument 1:Argument 2")
);
}
/**
* Test String literals in the context of a helper
*
* @param string $template template text
* @param array $data context data
* @param string $results The Expected Results
*
* @dataProvider stringLiteralInCustomHelperProvider
*
* @return void
*/
public function testStringLiteralInCustomHelper($template, $data, $results){
$engine = new \Handlebars\Handlebars();
$engine->addHelper('test2', function ($template, $context, $args) {
$args = $template->parseArguments($args);
$args = array_map(function ($a) use ($context) {return $context->get($a);}, $args);
return implode(':', $args);
});
$res = $engine->render($template, $data);
$this->assertEquals($res, $results);
}
public function testString(){
$string = new \Handlebars\String("Hello World");
$this->assertEquals((string)$string, "Hello World");
}
}