DOT-NOTATION and IMPLICIT-ITERATOR are now part of core spec.
These pragmas have proved useful enough that they're no longer considered pragmas. Enable them by default. Update tests accordingly. This is a (very) backwards incompatible change, so it won't be released until v0.6.0.
This commit is contained in:
@@ -1,63 +0,0 @@
|
||||
<?php
|
||||
|
||||
require_once '../Mustache.php';
|
||||
|
||||
/**
|
||||
* @group pragmas
|
||||
*/
|
||||
class MustachePragmaDotNotationTest extends PHPUnit_Framework_TestCase {
|
||||
|
||||
public function testDotTraversal() {
|
||||
$m = new Mustache('', array('foo' => array('bar' => 'this worked')));
|
||||
|
||||
$this->assertEquals($m->render('{{foo.bar}}'), '',
|
||||
'Dot notation not enabled, variable should have been replaced with nothing');
|
||||
$this->assertEquals($m->render('{{%DOT-NOTATION}}{{foo.bar}}'), 'this worked',
|
||||
'Dot notation enabled, variable should have been replaced by "this worked"');
|
||||
}
|
||||
|
||||
public function testDeepTraversal() {
|
||||
$data = array(
|
||||
'foo' => array('bar' => array('baz' => array('qux' => array('quux' => 'WIN!')))),
|
||||
'a' => array('b' => array('c' => array('d' => array('e' => 'abcs')))),
|
||||
'one' => array(
|
||||
'one' => 'one-one',
|
||||
'two' => 'one-two',
|
||||
'three' => 'one-three',
|
||||
),
|
||||
);
|
||||
|
||||
$m = new Mustache('', $data);
|
||||
$this->assertEquals($m->render('{{%DOT-NOTATION}}{{foo.bar.baz.qux.quux}}'), 'WIN!');
|
||||
$this->assertEquals($m->render('{{%DOT-NOTATION}}{{a.b.c.d.e}}'), 'abcs');
|
||||
$this->assertEquals($m->render('{{%DOT-NOTATION}}{{one.one}}|{{one.two}}|{{one.three}}'), 'one-one|one-two|one-three');
|
||||
}
|
||||
|
||||
public function testDotNotationContext() {
|
||||
$data = array('parent' => array('items' => array(
|
||||
array('item' => array('index' => 1)),
|
||||
array('item' => array('index' => 2)),
|
||||
array('item' => array('index' => 3)),
|
||||
array('item' => array('index' => 4)),
|
||||
array('item' => array('index' => 5)),
|
||||
)));
|
||||
|
||||
$m = new Mustache('', $data);
|
||||
$this->assertEquals('12345', $m->render('{{%DOT-NOTATION}}{{#parent}}{{#items}}{{item.index}}{{/items}}{{/parent}}'));
|
||||
}
|
||||
|
||||
public function testDotNotationSectionNames() {
|
||||
$data = array('parent' => array('items' => array(
|
||||
array('item' => array('index' => 1)),
|
||||
array('item' => array('index' => 2)),
|
||||
array('item' => array('index' => 3)),
|
||||
array('item' => array('index' => 4)),
|
||||
array('item' => array('index' => 5)),
|
||||
)));
|
||||
|
||||
$m = new Mustache('', $data);
|
||||
$this->assertEquals('.....', $m->render('{{%DOT-NOTATION}}{{#parent.items}}.{{/parent.items}}'));
|
||||
$this->assertEquals('12345', $m->render('{{%DOT-NOTATION}}{{#parent.items}}{{item.index}}{{/parent.items}}'));
|
||||
$this->assertEquals('12345', $m->render('{{%DOT-NOTATION}}{{#parent.items}}{{#item}}{{index}}{{/item}}{{/parent.items}}'));
|
||||
}
|
||||
}
|
||||
@@ -1,87 +0,0 @@
|
||||
<?php
|
||||
|
||||
require_once '../Mustache.php';
|
||||
|
||||
/**
|
||||
* @group pragmas
|
||||
*/
|
||||
class MustachePragmaImplicitIteratorTest extends PHPUnit_Framework_TestCase {
|
||||
|
||||
public function testEnablePragma() {
|
||||
$m = $this->getMock('Mustache', array('_renderPragma'), array('{{%IMPLICIT-ITERATOR}}'));
|
||||
$m->expects($this->exactly(1))
|
||||
->method('_renderPragma')
|
||||
->with(array(
|
||||
0 => '{{%IMPLICIT-ITERATOR}}',
|
||||
1 => 'IMPLICIT-ITERATOR', 'pragma_name' => 'IMPLICIT-ITERATOR',
|
||||
2 => null, 'options_string' => null
|
||||
));
|
||||
$m->render();
|
||||
}
|
||||
|
||||
public function testImplicitIterator() {
|
||||
$m1 = new Mustache('{{%IMPLICIT-ITERATOR}}{{#items}}{{.}}{{/items}}', array('items' => array('a', 'b', 'c')));
|
||||
$this->assertEquals('abc', $m1->render());
|
||||
|
||||
$m2 = new Mustache('{{%IMPLICIT-ITERATOR}}{{#items}}{{.}}{{/items}}', array('items' => array(1, 2, 3)));
|
||||
$this->assertEquals('123', $m2->render());
|
||||
}
|
||||
|
||||
public function testDotNotationCollision() {
|
||||
$m = new Mustache(null, array('items' => array('foo', 'bar', 'baz')));
|
||||
|
||||
$this->assertEquals('foobarbaz', $m->render('{{%IMPLICIT-ITERATOR}}{{%DOT-NOTATION}}{{#items}}{{.}}{{/items}}'));
|
||||
$this->assertEquals('foobarbaz', $m->render('{{%DOT-NOTATION}}{{%IMPLICIT-ITERATOR}}{{#items}}{{.}}{{/items}}'));
|
||||
}
|
||||
|
||||
public function testCustomIterator() {
|
||||
$m = new Mustache(null, array('items' => array('foo', 'bar', 'baz')));
|
||||
|
||||
$this->assertEquals('foobarbaz', $m->render('{{%IMPLICIT-ITERATOR}}{{#items}}{{.}}{{/items}}'));
|
||||
$this->assertEquals('foobarbaz', $m->render('{{%IMPLICIT-ITERATOR iterator=i}}{{#items}}{{i}}{{/items}}'));
|
||||
$this->assertEquals('foobarbaz', $m->render('{{%IMPLICIT-ITERATOR iterator=items}}{{#items}}{{items}}{{/items}}'));
|
||||
}
|
||||
|
||||
public function testDotNotationContext() {
|
||||
$m = new Mustache(null, array('items' => array(
|
||||
array('index' => 1, 'name' => 'foo'),
|
||||
array('index' => 2, 'name' => 'bar'),
|
||||
array('index' => 3, 'name' => 'baz'),
|
||||
)));
|
||||
|
||||
$this->assertEquals('foobarbaz', $m->render('{{%IMPLICIT-ITERATOR}}{{#items}}{{#.}}{{name}}{{/.}}{{/items}}'));
|
||||
$this->assertEquals('123', $m->render('{{%IMPLICIT-ITERATOR iterator=i}}{{%DOT-NOTATION}}{{#items}}{{i.index}}{{/items}}'));
|
||||
$this->assertEquals('foobarbaz', $m->render('{{%IMPLICIT-ITERATOR iterator=i}}{{%DOT-NOTATION}}{{#items}}{{i.name}}{{/items}}'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider recursiveSectionData
|
||||
*/
|
||||
public function testRecursiveSections($template, $view, $result) {
|
||||
$m = new Mustache();
|
||||
$this->assertEquals($result, $m->render($template, $view));
|
||||
}
|
||||
|
||||
public function recursiveSectionData() {
|
||||
return array(
|
||||
array(
|
||||
'{{%IMPLICIT-ITERATOR}}{{#items}}{{#.}}{{.}}{{/.}}{{/items}}',
|
||||
array('items' => array(array('a', 'b', 'c'), array('d', 'e', 'f'))),
|
||||
'abcdef'
|
||||
),
|
||||
array(
|
||||
'{{%IMPLICIT-ITERATOR}}{{#items}}{{#.}}{{#.}}{{.}}{{/.}}{{/.}}{{/items}}',
|
||||
array('items' => array(array(array('a', 'b'), array('c')), array(array('d'), array('e', 'f')))),
|
||||
'abcdef'
|
||||
),
|
||||
array(
|
||||
'{{%IMPLICIT-ITERATOR}}{{#items}}{{#.}}{{#items}}{{.}}{{/items}}{{/.}}{{/items}}',
|
||||
array('items' => array(
|
||||
array('items' => array('a', 'b', 'c')),
|
||||
array('items' => array('d', 'e', 'f')),
|
||||
)),
|
||||
'abcdef'
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -22,23 +22,23 @@ class MustachePragmaTest extends PHPUnit_Framework_TestCase {
|
||||
|
||||
public function testPragmaReplace() {
|
||||
$m = new Mustache();
|
||||
$this->assertEquals('', $m->render('{{%DOT-NOTATION}}'), 'Pragma tag not removed');
|
||||
$this->assertEquals('', $m->render('{{%UNESCAPED}}'), 'Pragma tag not removed');
|
||||
}
|
||||
|
||||
public function testPragmaReplaceMultiple() {
|
||||
$m = new Mustache();
|
||||
|
||||
$this->assertEquals('', $m->render('{{% DOT-NOTATION }}'), 'Pragmas should allow whitespace');
|
||||
$this->assertEquals('', $m->render('{{% DOT-NOTATION foo=bar }}'), 'Pragmas should allow whitespace');
|
||||
$this->assertEquals('', $m->render("{{%DOT-NOTATION}}\n{{%DOT-NOTATION}}"), 'Multiple pragma tags not removed');
|
||||
$this->assertEquals(' ', $m->render('{{%DOT-NOTATION}} {{%DOT-NOTATION}}'), 'Multiple pragma tags not removed');
|
||||
$this->assertEquals('', $m->render('{{% UNESCAPED }}'), 'Pragmas should allow whitespace');
|
||||
$this->assertEquals('', $m->render('{{% UNESCAPED foo=bar }}'), 'Pragmas should allow whitespace');
|
||||
$this->assertEquals('', $m->render("{{%UNESCAPED}}\n{{%UNESCAPED}}"), 'Multiple pragma tags not removed');
|
||||
$this->assertEquals(' ', $m->render('{{%UNESCAPED}} {{%UNESCAPED}}'), 'Multiple pragma tags not removed');
|
||||
}
|
||||
|
||||
public function testPragmaReplaceNewline() {
|
||||
$m = new Mustache();
|
||||
$this->assertEquals('', $m->render("{{%DOT-NOTATION}}\n"), 'Trailing newline after pragma tag not removed');
|
||||
$this->assertEquals("\n", $m->render("\n{{%DOT-NOTATION}}\n"), 'Too many newlines removed with pragma tag');
|
||||
$this->assertEquals("1\n23", $m->render("1\n2{{%DOT-NOTATION}}\n3"), 'Wrong newline removed with pragma tag');
|
||||
$this->assertEquals('', $m->render("{{%UNESCAPED}}\n"), 'Trailing newline after pragma tag not removed');
|
||||
$this->assertEquals("\n", $m->render("\n{{%UNESCAPED}}\n"), 'Too many newlines removed with pragma tag');
|
||||
$this->assertEquals("1\n23", $m->render("1\n2{{%UNESCAPED}}\n3"), 'Wrong newline removed with pragma tag');
|
||||
}
|
||||
|
||||
public function testPragmaReset() {
|
||||
|
||||
@@ -104,11 +104,11 @@ class MustacheTest extends PHPUnit_Framework_TestCase {
|
||||
array(
|
||||
'charset' => 'cp866',
|
||||
'delimiters' => array('[[[[', ']]]]'),
|
||||
'pragmas' => array(Mustache::PRAGMA_DOT_NOTATION, Mustache::PRAGMA_IMPLICIT_ITERATOR)
|
||||
'pragmas' => array(Mustache::PRAGMA_UNESCAPED)
|
||||
),
|
||||
'cp866',
|
||||
array('[[[[', ']]]]'),
|
||||
array(Mustache::PRAGMA_DOT_NOTATION, Mustache::PRAGMA_IMPLICIT_ITERATOR),
|
||||
array(Mustache::PRAGMA_UNESCAPED),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user