From c51d1e61b72dab929b5e556d5b815b2908cedfbb Mon Sep 17 00:00:00 2001 From: Jeremy Bush Date: Thu, 22 Jul 2010 18:42:56 -0500 Subject: [PATCH 01/13] adding tests for space failure --- examples/sections_spaces/Sections_Spaces.php | 14 ++++++++++++++ examples/sections_spaces/sections_spaces.mustache | 9 +++++++++ examples/sections_spaces/sections_spaces.txt | 8 ++++++++ 3 files changed, 31 insertions(+) create mode 100644 examples/sections_spaces/Sections_Spaces.php create mode 100644 examples/sections_spaces/sections_spaces.mustache create mode 100644 examples/sections_spaces/sections_spaces.txt diff --git a/examples/sections_spaces/Sections_Spaces.php b/examples/sections_spaces/Sections_Spaces.php new file mode 100644 index 0000000..79db095 --- /dev/null +++ b/examples/sections_spaces/Sections_Spaces.php @@ -0,0 +1,14 @@ + "And it worked the second time."), + array('item' => "As well as the third."), + ); + } + + public $final = "Then, surprisingly, it worked the final time."; +} \ No newline at end of file diff --git a/examples/sections_spaces/sections_spaces.mustache b/examples/sections_spaces/sections_spaces.mustache new file mode 100644 index 0000000..cea9f55 --- /dev/null +++ b/examples/sections_spaces/sections_spaces.mustache @@ -0,0 +1,9 @@ + * {{ start }} +{{# middle }} + * {{ item }} +{{/ middle }} +* {{ final }} + + * {{ start }} +{{# middle }} * {{ item }}{{/ middle }} +* {{ final }} \ No newline at end of file diff --git a/examples/sections_spaces/sections_spaces.txt b/examples/sections_spaces/sections_spaces.txt new file mode 100644 index 0000000..fd5d48b --- /dev/null +++ b/examples/sections_spaces/sections_spaces.txt @@ -0,0 +1,8 @@ + * It worked the first time. + * And it worked the second time. + * As well as the third. +* Then, surprisingly, it worked the final time. + + * It worked the first time. + * And it worked the second time. * As well as the third. +* Then, surprisingly, it worked the final time. \ No newline at end of file From ad6e15e5b5629e80860a3d26db8118feef4bc3e4 Mon Sep 17 00:00:00 2001 From: Justin Hileman Date: Thu, 22 Jul 2010 20:36:29 -0400 Subject: [PATCH 02/13] Update SectionSpaces class name to work with template autoloading, changing .txt to desired (not actual) output so the test will fail. --- .../{Sections_Spaces.php => SectionsSpaces.php} | 2 +- examples/sections_spaces/sections_spaces.mustache | 4 ++-- examples/sections_spaces/sections_spaces.txt | 7 ++++--- 3 files changed, 7 insertions(+), 6 deletions(-) rename examples/sections_spaces/{Sections_Spaces.php => SectionsSpaces.php} (86%) diff --git a/examples/sections_spaces/Sections_Spaces.php b/examples/sections_spaces/SectionsSpaces.php similarity index 86% rename from examples/sections_spaces/Sections_Spaces.php rename to examples/sections_spaces/SectionsSpaces.php index 79db095..9b5adb8 100644 --- a/examples/sections_spaces/Sections_Spaces.php +++ b/examples/sections_spaces/SectionsSpaces.php @@ -1,6 +1,6 @@ Date: Fri, 13 Aug 2010 06:54:29 -0700 Subject: [PATCH 03/13] Re-ordering mustache variable precedence --- Mustache.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Mustache.php b/Mustache.php index 5c7c319..5c4b230 100644 --- a/Mustache.php +++ b/Mustache.php @@ -605,10 +605,10 @@ class Mustache { protected function _findVariableInContext($tag_name, $context) { foreach ($context as $view) { if (is_object($view)) { - if (isset($view->$tag_name)) { - return $view->$tag_name; - } else if (method_exists($view, $tag_name)) { + if (method_exists($view, $tag_name)) { return $view->$tag_name(); + } else if (isset($view->$tag_name)) { + return $view->$tag_name; } } else if (isset($view[$tag_name])) { return $view[$tag_name]; From e40a57cba19a3fe744e216ce96c8a646fc041dea Mon Sep 17 00:00:00 2001 From: Justin Hileman Date: Sun, 15 Aug 2010 06:27:54 -0400 Subject: [PATCH 04/13] Refactor section_iterator_objects example. Defining an IteratorObject class is unnecessary: the ArrayIterator will do just fine. --- .../SectionIteratorObjects.php | 30 ++----------------- 1 file changed, 3 insertions(+), 27 deletions(-) diff --git a/examples/section_iterator_objects/SectionIteratorObjects.php b/examples/section_iterator_objects/SectionIteratorObjects.php index fa5213c..7b65597 100644 --- a/examples/section_iterator_objects/SectionIteratorObjects.php +++ b/examples/section_iterator_objects/SectionIteratorObjects.php @@ -3,38 +3,14 @@ class SectionIteratorObjects extends Mustache { public $start = "It worked the first time."; - public function middle() { - return new IteratorObject(); - } - - public $final = "Then, surprisingly, it worked the final time."; -} - -class IteratorObject implements Iterator { - protected $_position = 0; - protected $_data = array( array('item' => 'And it worked the second time.'), array('item' => 'As well as the third.'), ); - public function rewind() { - $this->_position = 0; + public function middle() { + return new ArrayIterator($this->_data); } - public function current() { - return $this->_data[$this->_position]; - } - - public function key() { - return $this->_position; - } - - public function next() { - ++$this->_position; - } - - public function valid() { - return isset($this->_data[$this->_position]); - } + public $final = "Then, surprisingly, it worked the final time."; } \ No newline at end of file From 582c3278fa5b7ee0e452c13cd3084e13f76328b1 Mon Sep 17 00:00:00 2001 From: Justin Hileman Date: Sun, 15 Aug 2010 06:28:56 -0400 Subject: [PATCH 05/13] Add known issue for whitespace, mark whitespace test as skipped. --- README.markdown | 1 + test/MustacheTest.php | 15 +++++++++------ 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/README.markdown b/README.markdown index ab27846..9512104 100644 --- a/README.markdown +++ b/README.markdown @@ -83,6 +83,7 @@ Known Issues * Sections don't respect delimiter changes -- `delimiters` example currently fails with an "unclosed section" exception. + * Mustache isn't always very good at whitespace. * Test coverage is incomplete. diff --git a/test/MustacheTest.php b/test/MustacheTest.php index a877c03..a2807cf 100644 --- a/test/MustacheTest.php +++ b/test/MustacheTest.php @@ -35,6 +35,11 @@ class MustacheTest extends PHPUnit_Framework_TestCase { const TEST_CLASS = 'Mustache'; + protected $knownIssues = array( + 'Delimiters' => "Known issue: sections don't respect delimiter changes", + 'SectionsSpaces' => "Known issue: Mustache fails miserably at whitespace", + ); + /** * Test Mustache constructor. * @@ -191,9 +196,8 @@ class MustacheTest extends PHPUnit_Framework_TestCase { * @return void */ public function test__clone($class, $template, $output) { - if ($class == 'Delimiters') { - $this->markTestSkipped("Known issue: sections don't respect delimiter changes"); - return; + if (isset($this->knownIssues[$class])) { + return $this->markTestSkipped($this->knownIssues[$class]); } $m = new $class; @@ -222,9 +226,8 @@ class MustacheTest extends PHPUnit_Framework_TestCase { * @return void */ public function testExamples($class, $template, $output) { - if ($class == 'Delimiters') { - $this->markTestSkipped("Known issue: sections don't respect delimiter changes"); - return; + if (isset($this->knownIssues[$class])) { + return $this->markTestSkipped($this->knownIssues[$class]); } $m = new $class; From de968a42b4a329b0cf4957d0cde614ade3a2f1d3 Mon Sep 17 00:00:00 2001 From: Justin Hileman Date: Sun, 15 Aug 2010 06:30:15 -0400 Subject: [PATCH 06/13] Initialize local pragmas with empty array. --- Mustache.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Mustache.php b/Mustache.php index 5c7c319..1ba62e3 100644 --- a/Mustache.php +++ b/Mustache.php @@ -98,7 +98,7 @@ class Mustache { self::PRAGMA_UNESCAPED ); - protected $_localPragmas; + protected $_localPragmas = array(); /** * Mustache class constructor. @@ -130,7 +130,7 @@ class Mustache { public function __clone() { $this->_otag = '{{'; $this->_ctag = '}}'; - $this->_localPragmas = null; + $this->_localPragmas = array(); if ($keys = array_keys($this->_context)) { $last = array_pop($keys); From 5de845cb2e227810ab03f41a397c106d8c434960 Mon Sep 17 00:00:00 2001 From: Justin Hileman Date: Sun, 15 Aug 2010 06:32:36 -0400 Subject: [PATCH 07/13] Add test coverage for edge cases and exceptions. --- test/MustacheExceptionTest.php | 97 ++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 test/MustacheExceptionTest.php diff --git a/test/MustacheExceptionTest.php b/test/MustacheExceptionTest.php new file mode 100644 index 0000000..39a0f5b --- /dev/null +++ b/test/MustacheExceptionTest.php @@ -0,0 +1,97 @@ +pickyMustache = new PickyMustache(); + $this->slackerMustache = new SlackerMustache(); + } + + /** + * @expectedException MustacheException + */ + public function testThrowsUnknownVariableException() { + $this->pickyMustache->render('{{not_a_variable}}'); + } + + /** + * @expectedException MustacheException + */ + public function testThrowsUnclosedSectionException() { + $this->pickyMustache->render('{{#unclosed}}'); + } + + /** + * @expectedException MustacheException + */ + public function testThrowsUnexpectedCloseSectionException() { + $this->pickyMustache->render('{{/unopened}}'); + } + + /** + * @expectedException MustacheException + */ + public function testThrowsUnknownPartialException() { + $this->pickyMustache->render('{{>impartial}}'); + } + + /** + * @expectedException MustacheException + */ + public function testThrowsUnknownPragmaException() { + $this->pickyMustache->render('{{%SWEET-MUSTACHE-BRO}}'); + } + + public function testDoesntThrowUnclosedSectionException() { + $this->assertEquals('', $this->slackerMustache->render('{{#unclosed}}')); + } + + public function testDoesntThrowUnexpectedCloseSectionException() { + $this->assertEquals('', $this->slackerMustache->render('{{/unopened}}')); + } + + public function testDoesntThrowUnknownPartialException() { + $this->assertEquals('', $this->slackerMustache->render('{{>impartial}}')); + } + + /** + * @expectedException MustacheException + */ + public function testGetPragmaOptionsThrowsExceptionsIfItThinksYouHaveAPragmaButItTurnsOutYouDont() { + $mustache = new TestableMustache(); + $mustache->testableGetPragmaOptions('PRAGMATIC'); + } +} + +class PickyMustache extends Mustache { + protected $_throwsExceptions = array( + MustacheException::UNKNOWN_VARIABLE => true, + MustacheException::UNCLOSED_SECTION => true, + MustacheException::UNEXPECTED_CLOSE_SECTION => true, + MustacheException::UNKNOWN_PARTIAL => true, + MustacheException::UNKNOWN_PRAGMA => true, + ); +} + +class SlackerMustache extends Mustache { + protected $_throwsExceptions = array( + MustacheException::UNKNOWN_VARIABLE => false, + MustacheException::UNCLOSED_SECTION => false, + MustacheException::UNEXPECTED_CLOSE_SECTION => false, + MustacheException::UNKNOWN_PARTIAL => false, + MustacheException::UNKNOWN_PRAGMA => false, + ); +} + +class TestableMustache extends Mustache { + public function testableGetPragmaOptions($pragma_name) { + return $this->_getPragmaOptions($pragma_name); + } +} \ No newline at end of file From 54d91b667be92b92c5ef0944d806e894959b9fdd Mon Sep 17 00:00:00 2001 From: Justin Hileman Date: Sun, 15 Aug 2010 06:33:55 -0400 Subject: [PATCH 08/13] Adjust _renderTag so xdebug relizes that it actually has full test coverage. This commit marks 100% test coverage of Mustache.php. --- Mustache.php | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/Mustache.php b/Mustache.php index 1ba62e3..97200c7 100644 --- a/Mustache.php +++ b/Mustache.php @@ -452,14 +452,12 @@ class Mustache { return $this->_renderUnescaped($tag_name); } break; - case '': - default: - if ($this->_hasPragma(self::PRAGMA_UNESCAPED)) { - return $this->_renderUnescaped($tag_name); - } else { - return $this->_renderEscaped($tag_name); - } - break; + } + + if ($this->_hasPragma(self::PRAGMA_UNESCAPED)) { + return $this->_renderUnescaped($modifier . $tag_name); + } else { + return $this->_renderEscaped($modifier . $tag_name); } } From 39b24c1933bde02b1535a5ba10b322bfb696a4aa Mon Sep 17 00:00:00 2001 From: Justin Hileman Date: Sun, 15 Aug 2010 06:53:12 -0400 Subject: [PATCH 09/13] Update mustache object section test to cover class method access for embedded objects. --- test/MustacheObjectSectionTest.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/test/MustacheObjectSectionTest.php b/test/MustacheObjectSectionTest.php index f9ae45b..17ad2e0 100644 --- a/test/MustacheObjectSectionTest.php +++ b/test/MustacheObjectSectionTest.php @@ -17,6 +17,12 @@ class MustacheObjectSectionTest extends PHPUnit_Framework_TestCase { $gamma = new Gamma(); $this->assertEquals('Foo', $gamma->render('{{#bar}}{{#foo}}{{name}}{{/foo}}{{/bar}}')); } + + public function testSectionObjectWithFunction() { + $alpha = new Alpha(); + $alpha->foo = new Delta(); + $this->assertEquals('Foo', $alpha->render('{{#foo}}{{name}}{{/foo}}')); + } } class Alpha extends Mustache { @@ -53,4 +59,12 @@ class Gamma extends Mustache { public function __construct() { $this->bar = new Beta(); } +} + +class Delta extends Mustache { + protected $_name = 'Foo'; + + public function name() { + return $this->_name; + } } \ No newline at end of file From e2546d40c1bc00d75b3d10f12abf4234ac6f2b8b Mon Sep 17 00:00:00 2001 From: Justin Hileman Date: Sun, 15 Aug 2010 06:54:54 -0400 Subject: [PATCH 10/13] Minor test coverage improvements. Add regression test for newlines in comments. Add rudimentary partials test. --- test/MustacheTest.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/test/MustacheTest.php b/test/MustacheTest.php index a2807cf..7e647cc 100644 --- a/test/MustacheTest.php +++ b/test/MustacheTest.php @@ -141,6 +141,23 @@ class MustacheTest extends PHPUnit_Framework_TestCase { $this->assertEquals('Zappa, Frank', $m->render('{{last_name}}, {{first_name}}', array('first_name' => 'Frank', 'last_name' => 'Zappa'))); } + public function testRenderWithPartials() { + $m = new Mustache('{{>stache}}', null, array('stache' => '{{first_name}} {{last_name}}')); + $this->assertEquals('Charlie Chaplin', $m->render(null, array('first_name' => 'Charlie', 'last_name' => 'Chaplin'))); + $this->assertEquals('Zappa, Frank', $m->render('{{last_name}}, {{first_name}}', array('first_name' => 'Frank', 'last_name' => 'Zappa'))); + } + + /** + * Mustache should allow newlines (and other whitespace) in comments and all other tags. + * + * @access public + * @return void + */ + public function testNewlinesInComments() { + $m = new Mustache("{{! comment \n \t still a comment... }}"); + $this->assertEquals('', $m->render()); + } + /** * Mustache should return the same thing when invoked multiple times. * From f91f74ef1ba274d3b8a9456e57a10047100c8396 Mon Sep 17 00:00:00 2001 From: Justin Hileman Date: Sun, 15 Aug 2010 06:55:12 -0400 Subject: [PATCH 11/13] Clean up comments and whitespace. --- test/MustacheTest.php | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/test/MustacheTest.php b/test/MustacheTest.php index 7e647cc..7269b36 100644 --- a/test/MustacheTest.php +++ b/test/MustacheTest.php @@ -188,10 +188,9 @@ class MustacheTest extends PHPUnit_Framework_TestCase { $this->assertEquals($first, $second); } - /** * Mustache should not use templates passed to the render() method for subsequent invocations. - * + * * @access public * @return void */ @@ -199,7 +198,7 @@ class MustacheTest extends PHPUnit_Framework_TestCase { $m = new Mustache('Sirve.'); $this->assertEquals('No sirve.', $m->render('No sirve.')); $this->assertEquals('Sirve.', $m->render()); - + $m2 = new Mustache(); $this->assertEquals('No sirve.', $m2->render('No sirve.')); $this->assertEquals('', $m2->render()); @@ -208,8 +207,12 @@ class MustacheTest extends PHPUnit_Framework_TestCase { /** * testClone function. * + * @group examples * @dataProvider getExamples * @access public + * @param string $class + * @param string $template + * @param string $output * @return void */ public function test__clone($class, $template, $output) { @@ -235,11 +238,12 @@ class MustacheTest extends PHPUnit_Framework_TestCase { /** * Test everything in the `examples` directory. * + * @group examples * @dataProvider getExamples * @access public - * @param mixed $class - * @param mixed $template - * @param mixed $output + * @param string $class + * @param string $template + * @param string $output * @return void */ public function testExamples($class, $template, $output) { From 31b38b71a2fecb1642a136f3971a8c1bffef8f3a Mon Sep 17 00:00:00 2001 From: Justin Hileman Date: Sun, 15 Aug 2010 06:55:47 -0400 Subject: [PATCH 12/13] Test coverage is no longer incomplete. --- README.markdown | 1 - 1 file changed, 1 deletion(-) diff --git a/README.markdown b/README.markdown index 9512104..18e72d4 100644 --- a/README.markdown +++ b/README.markdown @@ -84,7 +84,6 @@ Known Issues * Sections don't respect delimiter changes -- `delimiters` example currently fails with an "unclosed section" exception. * Mustache isn't always very good at whitespace. - * Test coverage is incomplete. See Also From 568b6c851255f74d9f1fe740363a8d5be118394d Mon Sep 17 00:00:00 2001 From: Justin Hileman Date: Sun, 15 Aug 2010 07:12:26 -0400 Subject: [PATCH 13/13] don't include phpunit in test cases. --- test/MustachePragmaImplicitIteratorTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/test/MustachePragmaImplicitIteratorTest.php b/test/MustachePragmaImplicitIteratorTest.php index bf39091..207a8c6 100644 --- a/test/MustachePragmaImplicitIteratorTest.php +++ b/test/MustachePragmaImplicitIteratorTest.php @@ -1,7 +1,6 @@