fenom/tests/cases/Fenom/MacrosTest.php

76 lines
2.1 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
class MacrosTest extends TestCase {
2013-02-23 13:29:20 +04:00
public function setUp() {
parent::setUp();
$this->tpl("math.tpl", '
2013-02-23 02:03:05 +04:00
{macro plus(x, y)}
x + y = {$x + $y}
{/macro}
2013-02-23 13:29:20 +04:00
{macro minus(x, y, z=0)}
x - y - z = {$x - $y - $z}
{/macro}
2013-03-15 00:12:02 +04:00
{macro multi(x, y)}
x * y = {$x * $y}
{/macro}
2013-02-23 13:29:20 +04:00
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}
2013-02-23 02:03:05 +04:00
');
2013-03-15 00:12:02 +04:00
$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}.
');
2013-02-23 13:29:20 +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));
}
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
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
$this->assertSame('a: x + y = 3 , x - y - z = 3 , new minus macros .', Modifier::strip($tpl->fetch(array()), true));
}
/**
* @expectedExceptionMessage Undefined macro 'plus'
2013-06-28 11:53:53 +04:00
* @expectedException \Fenom\CompileException
2013-03-15 00:12:02 +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
$this->assertSame('a: x + y = 3 , x - y - z = 3 , new minus macros .', Modifier::strip($tpl->fetch(array()), true));
}
2013-02-23 02:03:05 +04:00
}