MustachePragmaImplicitIteratorTest.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. require_once '../Mustache.php';
  3. class MustachePragmaImplicitIteratorTest extends PHPUnit_Framework_TestCase {
  4. public function testEnablePragma() {
  5. $m = $this->getMock('Mustache', array('_renderPragma'), array('{{%IMPLICIT-ITERATOR}}'));
  6. $m->expects($this->exactly(1))
  7. ->method('_renderPragma')
  8. ->with(array('{{%IMPLICIT-ITERATOR}}', 'IMPLICIT-ITERATOR', null));
  9. $m->render();
  10. }
  11. public function testImplicitIterator() {
  12. $m1 = new Mustache('{{%IMPLICIT-ITERATOR}}{{#items}}{{.}}{{/items}}', array('items' => array('a', 'b', 'c')));
  13. $this->assertEquals('abc', $m1->render());
  14. $m2 = new Mustache('{{%IMPLICIT-ITERATOR}}{{#items}}{{.}}{{/items}}', array('items' => array(1, 2, 3)));
  15. $this->assertEquals('123', $m2->render());
  16. }
  17. public function testDotNotationCollision() {
  18. $m = new Mustache(null, array('items' => array('foo', 'bar', 'baz')));
  19. $this->assertEquals('foobarbaz', $m->render('{{%IMPLICIT-ITERATOR}}{{%DOT-NOTATION}}{{#items}}{{.}}{{/items}}'));
  20. $this->assertEquals('foobarbaz', $m->render('{{%DOT-NOTATION}}{{%IMPLICIT-ITERATOR}}{{#items}}{{.}}{{/items}}'));
  21. }
  22. public function testCustomIterator() {
  23. $m = new Mustache(null, array('items' => array('foo', 'bar', 'baz')));
  24. $this->assertEquals('foobarbaz', $m->render('{{%IMPLICIT-ITERATOR}}{{#items}}{{.}}{{/items}}'));
  25. $this->assertEquals('foobarbaz', $m->render('{{%IMPLICIT-ITERATOR iterator=i}}{{#items}}{{i}}{{/items}}'));
  26. $this->assertEquals('foobarbaz', $m->render('{{%IMPLICIT-ITERATOR iterator=items}}{{#items}}{{items}}{{/items}}'));
  27. }
  28. public function testDotNotationContext() {
  29. $m = new Mustache(null, array('items' => array(
  30. array('index' => 1, 'name' => 'foo'),
  31. array('index' => 2, 'name' => 'bar'),
  32. array('index' => 3, 'name' => 'baz'),
  33. )));
  34. $this->assertEquals('foobarbaz', $m->render('{{%IMPLICIT-ITERATOR}}{{#items}}{{#.}}{{name}}{{/.}}{{/items}}'));
  35. $this->assertEquals('123', $m->render('{{%IMPLICIT-ITERATOR iterator=i}}{{%DOT-NOTATION}}{{#items}}{{i.index}}{{/items}}'));
  36. $this->assertEquals('foobarbaz', $m->render('{{%IMPLICIT-ITERATOR iterator=i}}{{%DOT-NOTATION}}{{#items}}{{i.name}}{{/items}}'));
  37. }
  38. }