Skip to content

Commit

Permalink
added & fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Feb 18, 2022
1 parent 2677be8 commit 8a046d1
Show file tree
Hide file tree
Showing 6 changed files with 190 additions and 7 deletions.
12 changes: 6 additions & 6 deletions tests/common/contentType.compatibility.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ $latte->setLoader(new Latte\Loaders\StringLoader([
'context1a' => '<p>{block html|noescape}<hr> " &lt;{/block}</p>',
'context1b' => '<p>{block html|upper}<hr> " &lt;{/block}</p>',
'context1c' => '<p>{block html|stripHtml|upper}<hr> " &lt;{/block}</p>',
'context2' => '<p title="{block html}<hr> &quot;{/block}"</p>',
'context2' => '<p title="{block html}<hr> &quot;{/block}"></p>',
'context2a' => '<p title="{block html|stripHtml|upper}<hr> &quot;{/block}"></p>',
'context6' => '<!--{block html}<hr> &lt;{/block}-->',
'context6a' => '<!--{block html|stripHtml|upper}<hr> &lt;{/block}-->',
Expand All @@ -310,7 +310,7 @@ Assert::exception(function () use ($latte) {
}, Latte\RuntimeException::class, 'Filter |upper is called with incompatible content type HTML, try to prepend |stripHtml.');

Assert::same('<p> " &lt;</p>', $latte->renderToString('context1c'));
Assert::same('<p title="&lt;hr&gt; &quot;"</p>', $latte->renderToString('context2'));
Assert::same('<p title="&lt;hr&gt; &quot;"></p>', $latte->renderToString('context2'));
Assert::same('<p title=" &quot;"></p>', $latte->renderToString('context2a'));
Assert::same('<!--<hr> &lt;-->', $latte->renderToString('context6'));

Expand All @@ -326,7 +326,7 @@ $latte->setLoader(new Latte\Loaders\StringLoader([
'context1a' => '<p>{var $n=html}{block $n|noescape}<hr> " &lt;{/block}</p>',
'context1b' => '<p>{var $n=html}{block $n|upper}<hr> " &lt;{/block}</p>',
'context1c' => '<p>{var $n=html}{block $n|stripHtml|upper}<hr> " &lt;{/block}</p>',
'context2' => '<p title="{var $n=html}{block $n}<hr> &quot;{/block}"</p>',
'context2' => '<p title="{var $n=html}{block $n}<hr> &quot;{/block}"></p>',
'context2a' => '<p title="{var $n=html}{block $n|stripHtml|upper}<hr> &quot;{/block}"></p>',
'context6' => '<!--{var $n=html}{block $n}<hr> &lt;{/block}-->',
'context6a' => '<!--{var $n=html}{block $n|stripHtml|upper}<hr> &lt;{/block}-->',
Expand All @@ -343,7 +343,7 @@ Assert::exception(function () use ($latte) {
}, Latte\RuntimeException::class, 'Filter |upper is called with incompatible content type HTML, try to prepend |stripHtml.');

Assert::same('<p> " &lt;</p>', $latte->renderToString('context1c'));
Assert::same('<p title="&lt;hr&gt; &quot;"</p>', $latte->renderToString('context2'));
Assert::same('<p title="&lt;hr&gt; &quot;"></p>', $latte->renderToString('context2'));
Assert::same('<p title=" &quot;"></p>', $latte->renderToString('context2a'));
Assert::same('<!--<hr> &lt;-->', $latte->renderToString('context6'));

Expand All @@ -359,7 +359,7 @@ $latte->setLoader(new Latte\Loaders\StringLoader([
'context1a' => '<p>{block|noescape}<hr> " &lt;{/block}</p>',
'context1b' => '<p>{block|upper}<hr> " &lt;{/block}</p>',
'context1c' => '<p>{block|stripHtml|upper}<hr> " &lt;{/block}</p>',
'context2' => '<p title="{block}<hr> &quot;{/block}"</p>',
'context2' => '<p title="{block}<hr> &quot;{/block}"></p>',
'context2a' => '<p title="{block|stripHtml|upper}<hr> &quot;{/block}"></p>',
'context6' => '<!--{block}<hr> &lt;{/block}-->',
'context6a' => '<!--{block|stripHtml|upper}<hr> &lt;{/block}-->',
Expand All @@ -376,7 +376,7 @@ Assert::exception(function () use ($latte) {
}, Latte\RuntimeException::class, 'Filter |upper is called with incompatible content type HTML, try to prepend |stripHtml.');

Assert::same('<p> " &lt;</p>', $latte->renderToString('context1c'));
Assert::same('<p title="<hr> &quot;"</p>', $latte->renderToString('context2'));
Assert::same('<p title="<hr> &quot;"></p>', $latte->renderToString('context2'));
Assert::same('<p title=" &quot;"></p>', $latte->renderToString('context2a'));
Assert::same('<!--<hr> &lt;-->', $latte->renderToString('context6'));

Expand Down
15 changes: 15 additions & 0 deletions tests/tags/contentType.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,21 @@ Assert::exception(function () use ($latte) {
$latte->createTemplate('<div>{contentType xml}</div>');
}, Latte\CompileException::class, '{contentType} is allowed only in template header.');

Assert::same(
'<script> <p n:if=0 /> </script>',
$latte->renderToString('{contentType html}<script> <p n:if=0 /> </script>')
);

Assert::same(
'<script> </script>',
$latte->renderToString('{contentType xml}<script> <p n:if=0 /> </script>')
);

Assert::same(
'<p n:if=0 />',
$latte->renderToString('{contentType text}<p n:if=0 />')
);

// defined on $latte
$latte = new Latte\Engine;
$latte->setLoader(new Latte\Loaders\StringLoader);
Expand Down
35 changes: 35 additions & 0 deletions tests/tags/expected/n-class.phtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
%A%
foreach ($iterator = $ʟ_it = new LR\CachingIterator([1,2,3], $ʟ_it ?? null) as $foo) /* line %d% */ {
echo ' <b';
echo ($ʟ_tmp = array_filter([$iterator->even ? 'even' : null])) ? ' class="' . LR\Filters::escapeHtmlAttr(implode(" ", array_unique($ʟ_tmp))) . '"' : "" /* line %d% */;
echo '>item</b>
';
$iterations++;
}
$iterator = $ʟ_it = $ʟ_it->getParent();
echo "\n";
$iterations = 0;
foreach ($iterator = $ʟ_it = new LR\CachingIterator([1, 2, 3], $ʟ_it ?? null) as $foo) /* line %d% */ {
echo '<p';
echo ($ʟ_tmp = array_filter([$iterator->even ? 'even' : null])) ? ' class="' . LR\Filters::escapeHtmlAttr(implode(" ", array_unique($ʟ_tmp))) . '"' : "" /* line %d% */;
echo '>';
echo LR\Filters::escapeHtmlText($foo) /* line %d% */;
echo '</p>
';
$iterations++;
}
$iterator = $ʟ_it = $ʟ_it->getParent();
echo '
<p';
echo ($ʟ_tmp = array_filter(['foo', (false ? 'first' : null), 'odd', (true ? 'foo' : 'bar')])) ? ' class="' . LR\Filters::escapeHtmlAttr(implode(" ", array_unique($ʟ_tmp))) . '"' : "" /* line %d% */;
echo '>n:class</p>

<p';
echo ($ʟ_tmp = array_filter([false ? 'first' : null])) ? ' class="' . LR\Filters::escapeHtmlAttr(implode(" ", array_unique($ʟ_tmp))) . '"' : "" /* line %d% */;
echo '>n:class empty</p>

<p';
echo ($ʟ_tmp = array_filter([true ? 'bem--modifier' : null])) ? ' class="' . LR\Filters::escapeHtmlAttr(implode(" ", array_unique($ʟ_tmp))) . '"' : "" /* line %d% */;
echo '>n:class with BEM</p>
';
%A%
65 changes: 65 additions & 0 deletions tests/tags/n-attr.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

/**
* n:attr
*/

declare(strict_types=1);

use Tester\Assert;


require __DIR__ . '/../bootstrap.php';


$latte = new Latte\Engine;
$latte->setLoader(new Latte\Loaders\StringLoader);

$template = <<<'EOD'
<p n:attr="title => hello, lang => isset($lang) ? $lang"> </p>
<p n:attr="[title => hello]"> </p>

EOD;

Assert::match(
<<<'XX'
%A%
echo '
<p';
$ʟ_tmp = ['title' => 'hello', 'lang' => isset($lang) ? $lang : null];
echo LR\Filters::htmlAttributes(isset($ʟ_tmp[0]) && is_array($ʟ_tmp[0]) ? $ʟ_tmp[0] : $ʟ_tmp) /* line 2 */;
echo '> </p>
<p';
$ʟ_tmp = [['title' => 'hello']];
echo LR\Filters::htmlAttributes(isset($ʟ_tmp[0]) && is_array($ʟ_tmp[0]) ? $ʟ_tmp[0] : $ʟ_tmp) /* line 4 */;
echo '> </p>
';
%A%
XX
,
$latte->compile($template)
);

Assert::match(
<<<'XX'
<p title="hello"> </p>
<p title="hello"> </p>
XX
,
$latte->renderToString($template)
);


Assert::exception(function () use ($latte) {
$latte->compile('<div n:attr/>');
}, Latte\CompileException::class, 'Missing arguments in n:attr');


Assert::exception(function () use ($latte) {
$latte->compile('<div n:inner-attr/>');
}, Latte\CompileException::class, 'Unknown attribute n:inner-attr');
68 changes: 68 additions & 0 deletions tests/tags/n-class.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

/**
* n:class
*/

declare(strict_types=1);

use Tester\Assert;


require __DIR__ . '/../bootstrap.php';


$latte = new Latte\Engine;
$latte->setLoader(new Latte\Loaders\StringLoader);

$template = <<<'EOD'
{foreach [1,2,3] as $foo}
<b n:class="$iterator->even ? even">item</b>
{/foreach}
<p n:foreach="[1, 2, 3] as $foo" n:class="$iterator->even ? even">{$foo}</p>
<p n:class="foo, (false ? first), odd, (true ? foo : bar)">n:class</p>
<p n:class="false ? first">n:class empty</p>
<p n:class="true ? bem--modifier">n:class with BEM</p>

EOD;

Assert::matchFile(
__DIR__ . '/expected/n-class.phtml',
$latte->compile($template)
);

Assert::match(
<<<'XX'
<b>item</b>
<b class="even">item</b>
<b>item</b>
<p>1</p>
<p class="even">2</p>
<p>3</p>
<p class="foo odd">n:class</p>
<p>n:class empty</p>
<p class="bem--modifier">n:class with BEM</p>
XX
,
$latte->renderToString($template)
);


Assert::exception(function () use ($latte) {
$latte->compile('<div n:class/>');
}, Latte\CompileException::class, 'Missing arguments in n:class');


Assert::exception(function () use ($latte) {
$latte->compile('<div n:inner-class/>');
}, Latte\CompileException::class, 'Unknown attribute n:inner-class');
2 changes: 1 addition & 1 deletion tests/tags/n-tag.phpt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

/**
* Test: Latte\Engine and n:tag.
* n:tag
*/

declare(strict_types=1);
Expand Down

0 comments on commit 8a046d1

Please sign in to comment.