fenom/tests/cases/Fenom/MacrosTest.php

163 lines
4.5 KiB
PHP
Raw Normal View History

2013-02-23 02:03:05 +04:00
<?php
2013-06-28 11:53:53 +04:00
namespace Fenom;
2013-02-23 02:03:05 +04:00
2013-07-29 14:58:14 +04:00
class MacrosTest extends TestCase
{
2013-02-23 02:03:05 +04:00
2013-07-29 14:58:14 +04:00
public function setUp()
{
2013-02-23 13:29:20 +04:00
parent::setUp();
2014-05-06 14:22:58 +04:00
$this->tpl(
"math.tpl",
'
{macro plus(x, y)}
x + y = {$x + $y}
{/macro}
{macro minus(x, y, z=0)}
x - y - z = {$x - $y - $z}
{/macro}
{macro multi(x, y)}
x * y = {$x * $y}
{/macro}
Math: {macro.plus x=2 y=3}, {macro.minus x=10 y=4}
'
);
$this->tpl(
"import.tpl",
'
{import "math.tpl"}
{import "math.tpl" as math}
Imp: {macro.plus x=1 y=2}, {math.minus x=6 y=2 z=1}
'
);
$this->tpl(
"import_custom.tpl",
'
{macro minus($x, $y)}
new minus macros
{/macro}
{import [plus, minus] from "math.tpl" as math}
a: {math.plus x=1 y=2}, {math.minus x=6 y=2 z=1}, {macro.minus x=5 y=3}.
'
);
$this->tpl(
"import_miss.tpl",
'
{import [minus] from "math.tpl" as math}
a: {macro.plus x=5 y=3}.
'
);
$this->tpl(
"macro_recursive.tpl",
'{macro factorial(num)}
{if $num}
{$num} {macro.factorial num=$num-1} {$num}
{/if}
{/macro}
{macro.factorial num=10}'
);
$this->tpl(
"macro_recursive_import.tpl",
'
{import "macro_recursive.tpl" as math}
{math.factorial num=10}'
);
2013-07-29 14:53:21 +04:00
}
2013-08-22 10:35:43 +04:00
public function _testSandbox()
2013-08-05 13:07:16 +04:00
{
try {
2013-08-22 00:03:20 +04:00
// $this->fenom->compile("macro_recursive.tpl")->display([]);
2013-07-29 16:32:54 +04:00
// $this->fenom->flush();
// var_dump($this->fenom->fetch("macro_recursive.tpl", []));
2014-05-06 14:22:58 +04:00
var_dump(
$this->fenom->compileCode(
'{macro factorial(num)}
{if $num}
{$num} {macro.factorial num=$num-1} {$num}
{/if}
{/macro}'
)->getBody()
);
// var_dump($this->fenom->display("macro_recursive_import.tpl", array()));
2013-08-05 13:07:16 +04:00
} catch (\Exception $e) {
var_dump($e->getMessage() . ": " . $e->getTraceAsString());
}
exit;
}
2013-02-23 13:29:20 +04:00
2013-07-29 14:58:14 +04:00
public function testMacros()
{
2013-06-28 11:53:53 +04:00
$tpl = $this->fenom->compile('math.tpl');
2013-02-23 02:03:05 +04:00
$this->assertStringStartsWith('x + y = ', trim($tpl->macros["plus"]["body"]));
2013-02-23 13:29:20 +04:00
$this->assertSame('Math: x + y = 5 , x - y - z = 6', Modifier::strip($tpl->fetch(array()), true));
}
2013-07-29 14:58:14 +04:00
public function testImport()
{
2013-06-28 11:53:53 +04:00
$tpl = $this->fenom->compile('import.tpl');
2013-02-23 13:29:20 +04:00
$this->assertSame('Imp: x + y = 3 , x - y - z = 3', Modifier::strip($tpl->fetch(array()), true));
2013-02-23 02:03:05 +04:00
}
2013-03-15 00:12:02 +04:00
/**
* @group importCustom
*/
2013-07-29 14:58:14 +04:00
public function testImportCustom()
{
2013-06-28 11:53:53 +04:00
$tpl = $this->fenom->compile('import_custom.tpl');
2013-03-15 00:12:02 +04:00
2014-05-06 14:22:58 +04:00
$this->assertSame(
'a: x + y = 3 , x - y - z = 3 , new minus macros .',
Modifier::strip($tpl->fetch(array()), true)
);
2013-03-15 00:12:02 +04:00
}
/**
* @expectedExceptionMessage Undefined macro 'plus'
2013-07-29 16:15:52 +04:00
* @expectedException \Fenom\Error\CompileException
2013-03-15 00:12:02 +04:00
*/
2013-07-29 14:58:14 +04:00
public function testImportMiss()
{
2013-06-28 11:53:53 +04:00
$tpl = $this->fenom->compile('import_miss.tpl');
2013-03-15 00:12:02 +04:00
2014-05-06 14:22:58 +04:00
$this->assertSame(
'a: x + y = 3 , x - y - z = 3 , new minus macros .',
Modifier::strip($tpl->fetch(array()), true)
);
2013-03-15 00:12:02 +04:00
}
2013-07-29 14:53:21 +04:00
/**
* @group macro-recursive
*/
2013-07-29 14:58:14 +04:00
public function testRecursive()
{
2013-07-29 14:53:21 +04:00
$this->fenom->compile('macro_recursive.tpl');
$this->fenom->flush();
$tpl = $this->fenom->getTemplate('macro_recursive.tpl');
2013-08-01 01:05:19 +04:00
$this->assertSame("10 9 8 7 6 5 4 3 2 1 1 2 3 4 5 6 7 8 9 10", Modifier::strip($tpl->fetch(array()), true));
2013-07-29 14:53:21 +04:00
}
2013-08-22 00:03:20 +04:00
public function testImportRecursive()
{
$this->fenom->compile('macro_recursive_import.tpl');
$this->fenom->flush();
$tpl = $this->fenom->getTemplate('macro_recursive.tpl');
$this->assertSame("10 9 8 7 6 5 4 3 2 1 1 2 3 4 5 6 7 8 9 10", Modifier::strip($tpl->fetch(array()), true));
}
2013-02-23 02:03:05 +04:00
}