TalesTest.php
4.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php
/**
* PHPTAL templating engine
*
* PHP Version 5
*
* @category HTML
* @package PHPTAL
* @author Laurent Bedubourg <lbedubourg@motion-twin.com>
* @author Kornel Lesiński <kornel@aardvarkmedia.co.uk>
* @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
* @version SVN: $Id: TalesTest.php 657 2009-06-30 16:48:20Z kornel $
* @link http://phptal.org/
*/
require_once dirname(__FILE__)."/config.php";
PHPTAL::setIncludePath();
require_once 'PHPTAL/Tales.php';
PHPTAL::restoreIncludePath();
function phptal_tales_custom($src,$nothrow)
{
return 'sprintf("%01.2f", '.PHPTAL_Php_TalesInternal::path($src, $nothrow).')';
}
class MyTalesClass implements PHPTAL_Tales
{
public static function reverse($exp,$nothrow){
return 'strrev('.phptal_tales($exp,$nothrow).')';
}
}
class TalesTest extends PHPTAL_TestCase
{
function testString()
{
$src = 'string:foo bar baz';
$res = PHPTAL_Php_TalesInternal::compileToPHPStatements($src);
$this->assertEquals("'foo bar baz'", $res);
$src = "'foo bar baz'";
$res = phptal_tales($src);
$this->assertEquals("'foo bar baz'", $res);
}
function testPhp()
{
$src = 'php: foo.x[10].doBar()';
$res = PHPTAL_Php_TalesInternal::compileToPHPStatements($src);
$this->assertEquals('$ctx->foo->x[10]->doBar()', $res);
}
function testPath()
{
$src = 'foo/x/y';
$res = phptal_tales($src);
$this->assertEquals("\$ctx->path(\$ctx->foo, 'x/y')", $res);
}
function testNot()
{
$src = "not: php: foo()";
$res = PHPTAL_Php_TalesInternal::compileToPHPStatements($src);
$this->assertEquals("!(foo())", $res);
}
function testTrue()
{
$tpl = $this->newPHPTAL('input/tales-true.html');
$tpl->isNotTrue = false;
$tpl->isTrue = true;
$res = $tpl->execute();
$this->assertEquals(trim_file('output/tales-true.html'), trim_string($res));
}
function testCustom()
{
$src = 'custom: some/path';
$this->assertEquals('sprintf("%01.2f", $ctx->path($ctx->some, \'path\'))',
phptal_tales($src));
}
function testCustomClass()
{
$src = 'MyTalesClass.reverse: some';
$this->assertEquals('strrev($ctx->some)', phptal_tales($src));
}
function testInterpolate1()
{
$this->assertEquals('$ctx->{$ctx->path($ctx->some, \'path\')}',PHPTAL_Php_TalesInternal::compileToPHPStatements('${some/path}'));
}
function testInterpolate2()
{
$this->assertEquals('$ctx->path($ctx->{$ctx->path($ctx->some, \'path\')}, \'meh\')',phptal_tales('${some/path}/meh'));
}
function testInterpolate3()
{
$this->assertEquals('$ctx->path($ctx->meh, $ctx->path($ctx->some, \'path\'))',PHPTAL_Php_TalesInternal::compileToPHPStatements('meh/${some/path}'));
}
function testInterpolate4()
{
$this->assertEquals('$ctx->path($ctx->{$ctx->meh}, $ctx->blah)',phptal_tales('${meh}/${blah}'));
}
function testSuperglobals()
{
$this->assertEquals('$ctx->path($ctx->{\'_GET\'}, \'a\')',PHPTAL_Php_TalesInternal::compileToPHPStatements('_GET/a'));
}
function testInterpolatedPHP1()
{
$tpl = $this->newPHPTAL();
$tpl->setSource('<div tal:content="string:foo${php:true?'bar':0}${php:false?0:\'b$$a$z\'}"/>');
$this->assertEquals('<div>foobarb$$a$z</div>',$tpl->execute());
}
function testInterpolatedTALES()
{
$tpl = $this->newPHPTAL();
$tpl->var = 'ba';
$tpl->setSource('<div tal:content="string:foo${nonexistant | string:bar$var}z"/>');
$this->assertEquals('<div>foobarbaz</div>',$tpl->execute());
}
function testInterpolatedPHP2()
{
$tpl = $this->newPHPTAL();
$tpl->somearray = array(1=>9,9,9);
$tpl->setSource('<div tal:repeat="x php:somearray"><x tal:replace=\'repeat/${php:
"x"}/key\'/></div>');
$this->assertEquals('<div>1</div><div>2</div><div>3</div>',$tpl->execute());
}
function testStringWithLongVarName()
{
$tpl = $this->newPHPTAL();
$tpl->aaaaaaaaaaaaaaaaaaaaa = 'ok';
$tpl->bbb = 'ok';
$tpl->setSource('<x tal:attributes="y string:$bbb/y/y; x string:$aaaaaaaaaaaaaaaaaaaaa/x/x" />');
$tpl->execute();
}
}