MustachePragmaDotNotationTest.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. require_once '../Mustache.php';
  3. require_once 'PHPUnit/Framework.php';
  4. class MustachePragmaDotNotationTest extends PHPUnit_Framework_TestCase {
  5. public function testDotTraversal() {
  6. $m = new Mustache('', array('foo' => array('bar' => 'this worked')));
  7. $this->assertEquals($m->render('{{foo.bar}}'), '',
  8. 'Dot notation not enabled, variable should have been replaced with nothing');
  9. $this->assertEquals($m->render('{{%DOT-NOTATION}}{{foo.bar}}'), 'this worked',
  10. 'Dot notation enabled, variable should have been replaced by "this worked"');
  11. }
  12. public function testDeepTraversal() {
  13. $data = array(
  14. 'foo' => array('bar' => array('baz' => array('qux' => array('quux' => 'WIN!')))),
  15. 'a' => array('b' => array('c' => array('d' => array('e' => 'abcs')))),
  16. 'one' => array(
  17. 'one' => 'one-one',
  18. 'two' => 'one-two',
  19. 'three' => 'one-three',
  20. ),
  21. );
  22. $m = new Mustache('', $data);
  23. $this->assertEquals($m->render('{{%DOT-NOTATION}}{{foo.bar.baz.qux.quux}}'), 'WIN!');
  24. $this->assertEquals($m->render('{{%DOT-NOTATION}}{{a.b.c.d.e}}'), 'abcs');
  25. $this->assertEquals($m->render('{{%DOT-NOTATION}}{{one.one}}|{{one.two}}|{{one.three}}'), 'one-one|one-two|one-three');
  26. }
  27. public function testDotNotationContext() {
  28. $data = array('parent' => array('items' => array(
  29. array('item' => array('index' => 1)),
  30. array('item' => array('index' => 2)),
  31. array('item' => array('index' => 3)),
  32. array('item' => array('index' => 4)),
  33. array('item' => array('index' => 5)),
  34. )));
  35. $m = new Mustache('', $data);
  36. $this->assertEquals('12345', $m->render('{{%DOT-NOTATION}}{{#parent}}{{#items}}{{item.index}}{{/items}}{{/parent}}'));
  37. }
  38. public function testDotNotationSectionNames() {
  39. $data = array('parent' => array('items' => array(
  40. array('item' => array('index' => 1)),
  41. array('item' => array('index' => 2)),
  42. array('item' => array('index' => 3)),
  43. array('item' => array('index' => 4)),
  44. array('item' => array('index' => 5)),
  45. )));
  46. $m = new Mustache('', $data);
  47. $this->assertEquals('.....', $m->render('{{%DOT-NOTATION}}{{#parent.items}}.{{/parent.items}}'));
  48. $this->assertEquals('12345', $m->render('{{%DOT-NOTATION}}{{#parent.items}}{{item.index}}{{/parent.items}}'));
  49. $this->assertEquals('12345', $m->render('{{%DOT-NOTATION}}{{#parent.items}}{{#item}}{{index}}{{/item}}{{/parent.items}}'));
  50. }
  51. }