Skip to content

Commit

Permalink
More test for better coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
fzerorubigd committed Dec 6, 2013
1 parent 82ea9f9 commit f2a30c6
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/Handlebars/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ public function __set($name, $helper)
*/
public function __unset($name)
{
unset($this->helpers[$name]);
$this->remove($name);
}

/**
Expand Down
6 changes: 2 additions & 4 deletions src/Handlebars/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,7 @@ private function _buildTree(\ArrayIterator $tokens)
$token = $tokens->current();
$tokens->next();

if ($token === null) {
continue;
} else {
if ($token !== null) {
switch ($token[Tokenizer::TYPE]) {
case Tokenizer::T_END_SECTION:
$newNodes = array();
Expand All @@ -87,7 +85,7 @@ private function _buildTree(\ArrayIterator $tokens)
$result[Tokenizer::NODES] = $newNodes;
$result[Tokenizer::END] = $token[Tokenizer::INDEX];
array_push($stack, $result);
break 2;
break;
} else {
array_unshift($newNodes, $result);
}
Expand Down
111 changes: 111 additions & 0 deletions tests/Xamin/HandlebarsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ public function internalHelpersdataProvider()
array('data' => array(1, 2, 3, 4)),
'1234'
),
array(
'{{#each data}}{{@key}}=>{{this}}{{/each}}',
array('data' => array('key1'=>1, 'key2'=>2,)),
'key1=>1key2=>2'
),
array(
'{{#unless data}}ok{{/unless}}',
array('data' => true),
Expand All @@ -120,6 +125,11 @@ public function internalHelpersdataProvider()
'{{#unless data}}ok{{/unless}}',
array('data' => false),
'ok'
),
array(
'{{#bindAttr data}}',
array(),
'data'
)

);
Expand Down Expand Up @@ -158,6 +168,107 @@ public function testCustomHelper()
$this->assertEquals('Test helper is called with a b c', $engine->render('{{#test2 a b c}}', array()));
$this->assertEquals('Test helper is called with a b c', $engine->render('{{test2 a b c}}', array()));

$engine->addHelper('renderme', function() {return new \Handlebars\String("{{test}}");});
$this->assertEquals('Test helper is called', $engine->render('{{#renderme}}', array()));

$engine->addHelper('dontrenderme', function() {return "{{test}}";});
$this->assertEquals('{{test}}', $engine->render('{{#dontrenderme}}', array()));
}

/**
* Test mustache style loop and if
*/
public function testMustacheStyle()
{
$loader = new \Handlebars\Loader\StringLoader();
$engine = new \Handlebars\Handlebars(array('loader' => $loader));
$this->assertEquals('yes', $engine->render('{{#x}}yes{{/x}}', array ('x' => true)));
$this->assertEquals('', $engine->render('{{#x}}yes{{/x}}', array ('x' => false)));
$this->assertEquals('yes', $engine->render('{{^x}}yes{{/x}}', array ('x' => false)));
$this->assertEquals('1234', $engine->render('{{#x}}{{this}}{{/x}}', array ('x' => array (1,2,3,4))));
$std = new stdClass();
$std->value = 1;
$this->assertEquals('1', $engine->render('{{#x}}{{value}}{{/x}}', array ('x' => $std)));
$this->assertEquals('1', $engine->render('{{{x}}}', array ('x' => 1)));
}

/**
* @expectedException \LogicException
*/
public function testParserException()
{
$loader = new \Handlebars\Loader\StringLoader();
$engine = new \Handlebars\Handlebars(array('loader' => $loader));
$engine->render('{{#test}}{{#test2}}{{/test}}{{/test2}}', array());
}

/**
* Test add/get/has/clear functions on helper class
*/
public function testHelpersClass()
{
$helpers = new \Handlebars\Helpers();
$helpers->add('test', function(){});
$this->assertTrue($helpers->has('test'));
$this->assertTrue(isset($helpers->test));
$this->assertFalse($helpers->isEmpty());
$helpers->test2 = function(){};
$this->assertTrue($helpers->has('test2'));
$this->assertTrue(isset($helpers->test2));
$this->assertFalse($helpers->isEmpty());
unset($helpers->test2);
$this->assertFalse($helpers->has('test2'));
$this->assertFalse(isset($helpers->test2));
$helpers->clear();
$this->assertFalse($helpers->has('test'));
$this->assertFalse(isset($helpers->test));
$this->assertTrue($helpers->isEmpty());
}

/**
* @expectedException \InvalidArgumentException
*/
public function testHelperWrongConstructor()
{
$helper = new \Handlebars\Helpers("helper");
}

/**
* @expectedException \InvalidArgumentException
*/
public function testHelperWrongCallable()
{
$helper = new \Handlebars\Helpers();
$helper->add('test', 1);
}

/**
* @expectedException \InvalidArgumentException
*/
public function testHelperWrongGet()
{
$helper = new \Handlebars\Helpers();
$x = $helper->test;
}

/**
* @expectedException \InvalidArgumentException
*/
public function testHelperWrongUnset()
{
$helper = new \Handlebars\Helpers();
unset($helper->test);
}

/**
* test String class
*/
public function testStringClass()
{
$string = new \Handlebars\String('test');
$this->assertEquals('test', $string->getString());
$string->setString('new');
$this->assertEquals('new', $string->getString());
}

/**
Expand Down

0 comments on commit f2a30c6

Please sign in to comment.