From f2a30c6ef193b1eeb758d63091958035d5bb132d Mon Sep 17 00:00:00 2001 From: fzerorubigd Date: Fri, 6 Dec 2013 19:17:27 +0330 Subject: [PATCH] More test for better coverage --- src/Handlebars/Helpers.php | 2 +- src/Handlebars/Parser.php | 6 +- tests/Xamin/HandlebarsTest.php | 111 +++++++++++++++++++++++++++++++++ 3 files changed, 114 insertions(+), 5 deletions(-) diff --git a/src/Handlebars/Helpers.php b/src/Handlebars/Helpers.php index af2026f..cf48c8d 100755 --- a/src/Handlebars/Helpers.php +++ b/src/Handlebars/Helpers.php @@ -274,7 +274,7 @@ class Helpers */ public function __unset($name) { - unset($this->helpers[$name]); + $this->remove($name); } /** diff --git a/src/Handlebars/Parser.php b/src/Handlebars/Parser.php index d096ebb..bdc8339 100755 --- a/src/Handlebars/Parser.php +++ b/src/Handlebars/Parser.php @@ -66,9 +66,7 @@ class Parser $token = $tokens->current(); $tokens->next(); - if ($token === null) { - continue; - } else { + if ($token !== null) { switch ($token[Tokenizer::TYPE]) { case Tokenizer::T_END_SECTION: $newNodes = array(); @@ -87,7 +85,7 @@ class Parser $result[Tokenizer::NODES] = $newNodes; $result[Tokenizer::END] = $token[Tokenizer::INDEX]; array_push($stack, $result); - break 2; + break; } else { array_unshift($newNodes, $result); } diff --git a/tests/Xamin/HandlebarsTest.php b/tests/Xamin/HandlebarsTest.php index d8390ce..c865139 100644 --- a/tests/Xamin/HandlebarsTest.php +++ b/tests/Xamin/HandlebarsTest.php @@ -111,6 +111,11 @@ class HandlebarsTest extends \PHPUnit_Framework_TestCase 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), @@ -120,6 +125,11 @@ class HandlebarsTest extends \PHPUnit_Framework_TestCase '{{#unless data}}ok{{/unless}}', array('data' => false), 'ok' + ), + array( + '{{#bindAttr data}}', + array(), + 'data' ) ); @@ -158,6 +168,107 @@ class HandlebarsTest extends \PHPUnit_Framework_TestCase $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()); } /**