Skip to content

Commit

Permalink
added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Feb 10, 2022
1 parent 2677be8 commit 8be7593
Show file tree
Hide file tree
Showing 5 changed files with 184 additions and 1 deletion.
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 8be7593

Please sign in to comment.