MustachePragmaDotNotationTest.php 2.3 KB

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