Skip to content

Commit

Permalink
tests: added new tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Jun 27, 2016
1 parent f8fa6c5 commit cfefb74
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 36 deletions.
2 changes: 2 additions & 0 deletions tests/Latte/Compiler.htmlNode.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class MockMacro implements IMacro
1 => ['a' => '', 'b' => '123', 'c' => 'abc', 'd' => 'text', 'e' => 'xxx', 'f' => TRUE],
2 => ['a' => '', 'b' => '456', 'c' => 'abc', 'd' => 'text', 'e' => 'xxx', 'f' => TRUE, 'g' => TRUE],
3 => ['a' => '', 'b' => '456', 'c' => 'abc', 'd' => 'text', 'e' => 'xxx', 'f' => TRUE, 'g' => TRUE],
4 => ['href' => TRUE],
];
Assert::same($res[$node->args], $node->htmlNode->attrs);
}
Expand All @@ -38,3 +39,4 @@ $parser = new Parser;
$compiler = new Compiler;
$compiler->addMacro('foo', new MockMacro);
$compiler->compile($parser->parse('<div a b=123 c = abc d="text" e=\'xxx\' f={foo 1/} b="456" g="a{foo 2/}b"> {foo 3/}'), 'Template');
$compiler->compile($parser->parse('<a href={foo 4/}>'), 'Template');
8 changes: 7 additions & 1 deletion tests/Latte/Compiler.unquoted.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,16 @@ $latte = new Latte\Engine;
$latte->setLoader(new Latte\Loaders\StringLoader);

$template = <<<'EOD'
<span title={$x}></span>
<span title={$x} class={$x}></span>
<span title={$x} {$x}></span>
<span title={if true}{$x}{else}{$y}{/if}></span>
<span title={if true}{$x}{else}"item"{/if}></span>
<span title={if true}{$x} {$x}{else}"item"{/if}></span>
<span {='title'}={$x}></span>

EOD;
Expand Down
69 changes: 59 additions & 10 deletions tests/Latte/Parser.parse.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@ function parse($s)
{
$parser = new Latte\Parser;
return array_map(function (Token $token) {
return [$token->type, $token->text];
return array_filter([$token->type, $token->text, $token->name, $token->value]);
}, $parser->parse($s));
}

Assert::same([
['text', '<0>']
['text', '<0>'],
], parse('<0>'));

Assert::same([
['htmlTagBegin', '<?'],
['text', 'xml encoding="'],
['macroTag', '{$enc}'],
['macroTag', '{$enc}', '=', '$enc'],
['text', '" ?'],
['htmlTagEnd', '>'],
['text', 'text'],
Expand All @@ -49,7 +49,7 @@ Assert::same([
], parse('<?bogus>text'));

Assert::same([
['macroTag', '{contentType xml}'],
['macroTag', '{contentType xml}', 'contentType', 'xml'],
['htmlTagBegin', '<?'],
['text', 'bogus>text'],
], parse('{contentType xml}<?bogus>text'));
Expand Down Expand Up @@ -83,16 +83,65 @@ Assert::same([
], parse('<!bogus>text'));

Assert::same([
['htmlTagBegin', '<div'],
['comment', ' n:syntax="off"'],
['htmlTagBegin', '<div', 'div'],
['comment', ' n:syntax="off"', 'n:syntax', 'off'],
['htmlTagEnd', '>'],
['htmlTagBegin', '<div'],
['htmlTagBegin', '<div', 'div'],
['htmlTagEnd', '>'],
['text', '{foo}'],
['htmlTagBegin', '</div'],
['htmlTagBegin', '</div', 'div'],
['htmlTagEnd', '>'],
['text', '{bar}'],
['htmlTagBegin', '</div'],
['htmlTagBegin', '</div', 'div'],
['htmlTagEnd', '>'],
['macroTag', '{lorem}'],
['macroTag', '{lorem}', 'lorem'],
], parse('<div n:syntax="off"><div>{foo}</div>{bar}</div>{lorem}'));

// html attributes
Assert::same([
['htmlTagBegin', '<div', 'div'],
['htmlAttributeBegin', ' a', 'a'],
['htmlAttributeBegin', ' b', 'b'],
['htmlAttributeBegin', ' c = d', 'c', 'd'],
['htmlAttributeBegin', ' e = "', 'e', '"'],
['text', 'f'],
['htmlAttributeEnd', '"'],
['htmlAttributeBegin', ' g', 'g'],
['htmlTagEnd', '>'],
['htmlTagBegin', '</div', 'div'],
['htmlTagEnd', '>'],
], parse('<div a b c = d e = "f" g></div>'));

Assert::same([
['htmlTagBegin', '<div', 'div'],
['htmlAttributeBegin', ' a', 'a'],
['text', ' '],
['macroTag', '{b}', 'b'],
['htmlAttributeBegin', ' c', 'c'],
['text', ' = '],
['macroTag', '{d}', 'd'],
['htmlAttributeBegin', ' e = a', 'e', 'a'],
['macroTag', '{b}', 'b'],
['htmlAttributeBegin', 'c', 'c'],
['htmlAttributeBegin', ' f = "', 'f', '"'],
['text', 'a'],
['macroTag', '{b}', 'b'],
['text', 'c'],
['htmlAttributeEnd', '"'],
['htmlTagEnd', '>'],
['htmlTagBegin', '</div', 'div'],
['htmlTagEnd', '>'],
], parse('<div a {b} c = {d} e = a{b}c f = "a{b}c"></div>'));

// macro attributes
Assert::same([
['htmlTagBegin', '<div', 'div'],
['htmlAttributeBegin', ' n:a', 'n:a'],
['htmlAttributeBegin', ' n:b', 'n:b'],
['htmlAttributeBegin', ' n:c = d', 'n:c', 'd'],
['htmlAttributeBegin', ' n:e = "f"', 'n:e', 'f'],
['htmlAttributeBegin', ' n:g', 'n:g'],
['htmlTagEnd', '>'],
['htmlTagBegin', '</div', 'div'],
['htmlTagEnd', '>'],
], parse('<div n:a n:b n:c = d n:e = "f" n:g></div>'));
6 changes: 6 additions & 0 deletions tests/Latte/expected/Compiler.unquoted.attrs.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
<span title="&#039; &amp; &quot;" class="&#039; &amp; &quot;"></span>

<span title="&#039; &amp; &quot;" "&#039; &amp; &quot;"></span>

<span title="&#039; &amp; &quot;"></span>

<span title="&#039; &amp; &quot;"></span>

<span title="&#039; &amp; &quot;" "&#039; &amp; &quot;"></span>
Expand Down
32 changes: 30 additions & 2 deletions tests/Latte/expected/Compiler.unquoted.attrs.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,39 @@ class Template%a% extends Latte\Runtime\Template
function main()
{
%A%
?><span title=<?php echo LR\Filters::escapeHtmlAttrUnquoted($x) /* line 1 */ ?>></span>
?><span title=<?php echo LR\Filters::escapeHtmlAttrUnquoted($x) /* line 1 */ ?> class=<?php echo LR\Filters::escapeHtmlAttrUnquoted($x) /* line 1 */ ?>></span>

<span title=<?php echo LR\Filters::escapeHtmlAttrUnquoted($x) /* line 3 */ ?> <?php echo LR\Filters::escapeHtmlAttrUnquoted($x) /* line 3 */ ?>></span>

<span <?php echo LR\Filters::escapeHtmlAttrUnquoted('title') /* line 5 */ ?>=<?php echo LR\Filters::escapeHtmlAttrUnquoted($x) /* line 5 */ ?>></span>
<span title=<?php
if (true) {
echo LR\Filters::escapeHtmlAttrUnquoted($x) /* line 5 */;
}
else {
echo LR\Filters::escapeHtmlAttrUnquoted($y) /* line 5 */;
}
?>></span>

<span title=<?php
if (true) {
echo LR\Filters::escapeHtmlAttrUnquoted($x) /* line 7 */;
}
else {
?>"item"<?php
}
?>></span>

<span title=<?php
if (true) {
echo LR\Filters::escapeHtmlAttrUnquoted($x) /* line 9 */ ?> <?php
echo LR\Filters::escapeHtmlAttrUnquoted($x) /* line 9 */;
}
else {
?>"item"<?php
}
?>></span>

<span <?php echo LR\Filters::escapeHtmlAttrUnquoted('title') /* line 11 */ ?>=<?php echo LR\Filters::escapeHtmlAttrUnquoted($x) /* line 11 */ ?>></span>
<?php
%A%
}
Expand Down
3 changes: 2 additions & 1 deletion tests/Latte/expected/contentType.xml.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/css" href=":/item"?>

<script><meta /></script>

Expand Down
42 changes: 21 additions & 21 deletions tests/Latte/expected/contentType.xml.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ class Template%a% extends Latte\Runtime\Template
{
extract($this->params);
if (empty($this->global->coreCaptured) && in_array($this->getReferenceType(), ["extends", NULL], TRUE)) header('Content-Type: application/xml; charset=utf-8');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
?><<?php ?>?xml version="1.0" encoding="utf-8"?>
<<?php ?>?xml-stylesheet type="text/css" href="<?php echo LR\Filters::escapeXml($id) /* line 3 */ ?>"?>

<script><?php
if (1) {
Expand All @@ -22,17 +22,17 @@ class Template%a% extends Latte\Runtime\Template


<ul>
<li>Escaped: <?php echo LR\Filters::escapeXml($hello) /* line 8 */ ?></li>
<li>Non-escaped: <?php echo $hello /* line 9 */ ?></li>
<li>Escaped expression: <?php echo LR\Filters::escapeXml('<' . 'b' . '>hello' . '</b>') /* line 10 */ ?></li>
<li>Non-escaped expression: <?php echo '<' . 'b' . '>hello' . '</b>' /* line 11 */ ?></li>
<li>Array access: <?php echo LR\Filters::escapeXml($people[1]) /* line 12 */ ?></li>
<li>Html: <?php echo LR\Filters::escapeXml($el) /* line 13 */ ?></li>
<li>Escaped: <?php echo LR\Filters::escapeXml($hello) /* line 9 */ ?></li>
<li>Non-escaped: <?php echo $hello /* line 10 */ ?></li>
<li>Escaped expression: <?php echo LR\Filters::escapeXml('<' . 'b' . '>hello' . '</b>') /* line 11 */ ?></li>
<li>Non-escaped expression: <?php echo '<' . 'b' . '>hello' . '</b>' /* line 12 */ ?></li>
<li>Array access: <?php echo LR\Filters::escapeXml($people[1]) /* line 13 */ ?></li>
<li>Html: <?php echo LR\Filters::escapeXml($el) /* line 14 */ ?></li>
</ul>

<style type="text/css">
<!--
#<?php echo LR\Filters::escapeHtmlComment($id) /* line 18 */ ?> {
#<?php echo LR\Filters::escapeHtmlComment($id) /* line 19 */ ?> {
background: blue;
}
-->
Expand All @@ -41,22 +41,22 @@ class Template%a% extends Latte\Runtime\Template

<script>
<!--
var html = <?php echo LR\Filters::escapeHtmlComment($el) /* line 27 */ ?>;
var html = <?php echo LR\Filters::escapeHtmlComment($el) /* line 28 */ ?>;
-->
</script>


<p onclick =
'alert(<?php echo LR\Filters::escapeXml($id) /* line 33 */ ?>);alert("hello");'
title='<?php echo LR\Filters::escapeXml($id) /* line 34 */ ?>"'
'alert(<?php echo LR\Filters::escapeXml($id) /* line 34 */ ?>);alert("hello");'
title='<?php echo LR\Filters::escapeXml($id) /* line 35 */ ?>"'
style =
"color:<?php echo LR\Filters::escapeXml($id) /* line 36 */ ?>;'"
alt='<?php echo LR\Filters::escapeXml($el) /* line 37 */ ?>'
onfocus="alert(<?php echo LR\Filters::escapeXml($el) /* line 38 */ ?>)"
"color:<?php echo LR\Filters::escapeXml($id) /* line 37 */ ?>;'"
alt='<?php echo LR\Filters::escapeXml($el) /* line 38 */ ?>'
onfocus="alert(<?php echo LR\Filters::escapeXml($el) /* line 39 */ ?>)"
>click on me</p>


<!-- <?php echo LR\Filters::escapeHtmlComment($comment) /* line 42 */ ?> -->
<!-- <?php echo LR\Filters::escapeHtmlComment($comment) /* line 43 */ ?> -->


</ul>
Expand All @@ -66,7 +66,7 @@ var html = <?php echo LR\Filters::escapeHtmlComment($el) /* line 27 */ ?>;
<?php
$iterations = 0;
foreach ($people as $person) {
?> <li><?php echo LR\Filters::escapeXml($person) /* line 49 */ ?></li>
?> <li><?php echo LR\Filters::escapeXml($person) /* line 50 */ ?></li>
<?php
$iterations++;
}
Expand All @@ -93,11 +93,11 @@ var html = <?php echo LR\Filters::escapeHtmlComment($el) /* line 27 */ ?>;
}
?>> </p>

<p val = <?php echo LR\Filters::escapeXmlAttrUnquoted($xss) /* line 60 */ ?> val2=<?php echo LR\Filters::escapeXmlAttrUnquoted($mxss) /* line 60 */ ?>> </p>
<p val = <?php echo LR\Filters::escapeXmlAttrUnquoted($xss) /* line 61 */ ?> val2=<?php echo LR\Filters::escapeXmlAttrUnquoted($mxss) /* line 61 */ ?>> </p>

<p onclick = <?php echo LR\Filters::escapeXmlAttrUnquoted($xss) /* line 62 */ ?>> </p>
<p onclick = <?php echo LR\Filters::escapeXmlAttrUnquoted($xss) /* line 63 */ ?>> </p>

<p val = /><?php echo LR\Filters::escapeXml($xss) /* line 64 */ ?></p>
<p val = /><?php echo LR\Filters::escapeXml($xss) /* line 65 */ ?></p>
<?php
return get_defined_vars();
}
Expand All @@ -106,7 +106,7 @@ var html = <?php echo LR\Filters::escapeHtmlComment($el) /* line 27 */ ?>;
function prepare()
{
extract($this->params);
if (isset($this->params['person'])) trigger_error('Variable $person overwritten in foreach on line 49');
if (isset($this->params['person'])) trigger_error('Variable $person overwritten in foreach on line 50');

}

Expand Down
3 changes: 2 additions & 1 deletion tests/Latte/templates/contentType.xml.latte
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{contentType application/xml; charset=utf-8}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/css" href="{$id}"?>

<script><meta n:if=1 /></script>

Expand Down

0 comments on commit cfefb74

Please sign in to comment.