Arbit - project tracking

Dwoo

Browse source code

File: / tests/ CompilerTests.php

Type
text/plain text/plain
Last Author
Jordi Boggiano
Version
f9e789948346197f6543892b94efb3e60cec62da
Line Rev. Author Source
1 dc33f4 Seldaek <?php
2 Seldaek
3 dd288f Seldaek require_once DWOO_DIRECTORY . 'Dwoo/Compiler.php';
4 dc33f4 Seldaek
5 Seldaek class CompilerTests extends PHPUnit_Framework_TestCase
6 Seldaek {
7 c6133a Seldaek const FOO = 3;
8 dc33f4 Seldaek protected $compiler;
9 Seldaek protected $dwoo;
10 Seldaek
11 Seldaek public function __construct()
12 Seldaek {
13 Seldaek // extend this class and override this in your constructor to test a modded compiler
14 9cf6ad Seldaek $this->compiler = new Dwoo_Compiler();
15 9315dc seldaek $this->dwoo = new Dwoo_Core(DWOO_COMPILE_DIR, DWOO_CACHE_DIR);
16 dc33f4 Seldaek }
17 Seldaek
18 Seldaek public function testVarReplacement()
19 Seldaek {
20 9cf6ad Seldaek $tpl = new Dwoo_Template_String('{$foo}');
21 dc33f4 Seldaek $tpl->forceCompilation();
22 Seldaek
23 Seldaek $this->assertEquals('bar', $this->dwoo->get($tpl, array('foo'=>'bar'), $this->compiler));
24 Seldaek }
25 Seldaek
26 914a96 Seldaek public function testComplexVarReplacement()
27 Seldaek {
28 Seldaek $tpl = new Dwoo_Template_String('{$_root[$a].0}{$_[$a][0]}{$_[$c.d].0}{$_.$a.0}{$_[$c[$x.0]].0}{$_[$c.$y.0].0}');
29 Seldaek $tpl->forceCompilation();
30 Seldaek
31 Seldaek $this->assertEquals('cccccc', $this->dwoo->get($tpl, array('a'=>'b', 'x'=>array('d'), 'y'=>'e', 'b'=>array('c','d'),'c'=>array('d'=>'b','e'=>array('b'))), $this->compiler));
32 Seldaek }
33 Seldaek
34 dc33f4 Seldaek public function testModifier()
35 Seldaek {
36 9cf6ad Seldaek $tpl = new Dwoo_Template_String('{$foo|upper}');
37 dc33f4 Seldaek $tpl->forceCompilation();
38 Seldaek
39 Seldaek $this->assertEquals('BAR', $this->dwoo->get($tpl, array('foo'=>'bar'), $this->compiler));
40 Seldaek }
41 Seldaek
42 Seldaek public function testModifierArgs()
43 Seldaek {
44 9cf6ad Seldaek $tpl = new Dwoo_Template_String('{$foo|spacify:"-"|upper}');
45 dc33f4 Seldaek $tpl->forceCompilation();
46 Seldaek
47 Seldaek $this->assertEquals('B-A-R', $this->dwoo->get($tpl, array('foo'=>'bar'), $this->compiler));
48 Seldaek }
49 Seldaek
50 860ce2 Seldaek public function testModifierArgsVars()
51 Seldaek {
52 Seldaek $tpl = new Dwoo_Template_String('{$foo|spacify:$bar|upper}');
53 Seldaek $tpl->forceCompilation();
54 Seldaek
55 Seldaek $this->assertEquals('B-A-R', $this->dwoo->get($tpl, array('foo'=>'bar', 'bar'=>'-'), $this->compiler));
56 Seldaek }
57 Seldaek
58 Seldaek public function testModifierOnString()
59 Seldaek {
60 Seldaek $tpl = new Dwoo_Template_String('{"bar"|spacify:"-"|upper}');
61 Seldaek $tpl->forceCompilation();
62 Seldaek
63 Seldaek $this->assertEquals('B-A-R', $this->dwoo->get($tpl, array(), $this->compiler));
64 Seldaek }
65 Seldaek
66 Seldaek public function testModifierOnStringWithVar()
67 Seldaek {
68 Seldaek $tpl = new Dwoo_Template_String('{"bar"|spacify:$bar|upper}');
69 Seldaek $tpl->forceCompilation();
70 Seldaek
71 Seldaek $this->assertEquals('B-A-R', $this->dwoo->get($tpl, array('bar'=>'-'), $this->compiler));
72 Seldaek }
73 Seldaek
74 dc33f4 Seldaek public function testModifierWithModifier()
75 Seldaek {
76 9cf6ad Seldaek $tpl = new Dwoo_Template_String('{$foo.0}{assign $foo|reverse foo}{$foo.0}{assign $foo|@reverse foo}{$foo.0}');
77 dc33f4 Seldaek $tpl->forceCompilation();
78 Seldaek
79 Seldaek $this->assertEquals('barbazzab', $this->dwoo->get($tpl, array('foo'=>array('bar','baz')), $this->compiler));
80 Seldaek }
81 Seldaek
82 f9676b Seldaek public function testDwooFunc()
83 dc33f4 Seldaek {
84 9cf6ad Seldaek $tpl = new Dwoo_Template_String('{upper($foo)}');
85 dc33f4 Seldaek $tpl->forceCompilation();
86 Seldaek
87 Seldaek $this->assertEquals('BAR', $this->dwoo->get($tpl, array('foo'=>'bar'), $this->compiler));
88 Seldaek }
89 Seldaek
90 f9676b Seldaek public function testDwooLoose()
91 dc33f4 Seldaek {
92 9cf6ad Seldaek $tpl = new Dwoo_Template_String('{upper $foo}');
93 dc33f4 Seldaek $tpl->forceCompilation();
94 Seldaek
95 Seldaek $this->assertEquals('BAR', $this->dwoo->get($tpl, array('foo'=>'bar'), $this->compiler));
96 Seldaek }
97 Seldaek
98 Seldaek public function testNamedParameter()
99 Seldaek {
100 9cf6ad Seldaek $tpl = new Dwoo_Template_String('{upper value=$foo}');
101 dc33f4 Seldaek $tpl->forceCompilation();
102 Seldaek
103 Seldaek $this->assertEquals('BAR', $this->dwoo->get($tpl, array('foo'=>'bar'), $this->compiler));
104 Seldaek }
105 Seldaek
106 Seldaek public function testNamedParameter2()
107 Seldaek {
108 9cf6ad Seldaek $tpl = new Dwoo_Template_String('{replace value=$foo search="BAR"|lower replace="BAR"}');
109 dc33f4 Seldaek $tpl->forceCompilation();
110 Seldaek
111 Seldaek $this->assertEquals('BAR', $this->dwoo->get($tpl, array('foo'=>'bar'), $this->compiler));
112 Seldaek }
113 Seldaek
114 Seldaek public function testNamedParameter3()
115 Seldaek {
116 9cf6ad Seldaek $tpl = new Dwoo_Template_String('{assign value=reverse(array(foo=3,boo=5, 3=4), true) var=arr}{foreach $arr k v}{$k}{$v}{/foreach}');
117 dc33f4 Seldaek $tpl->forceCompilation();
118 Seldaek
119 Seldaek $this->assertEquals('34boo5foo3', $this->dwoo->get($tpl, array(), $this->compiler));
120 Seldaek }
121 Seldaek
122 aa5c96 seldaek public function testQuotedNamedParameters()
123 seldaek {
124 seldaek $tpl = new Dwoo_Template_String('{assign \'value\'=reverse(array("foo"=3,boo=5, 3=4), true) "var"=arr}{foreach $arr k v}{$k}{$v}{/foreach}');
125 seldaek $tpl->forceCompilation();
126 seldaek
127 seldaek $this->assertEquals('34boo5foo3', $this->dwoo->get($tpl, array(), $this->compiler));
128 seldaek }
129 seldaek
130 157ad4 Seldaek public function testMixedParameters()
131 Seldaek {
132 Seldaek $tpl = new Dwoo_Template_String('{assign value=array(3, boo=5, 3=4) var=arr}{foreach $arr k v}{$k}{$v}{/foreach}');
133 Seldaek $tpl->forceCompilation();
134 Seldaek
135 Seldaek $this->assertEquals('03boo534', $this->dwoo->get($tpl, array(), $this->compiler));
136 Seldaek }
137 Seldaek
138 Seldaek /**
139 Seldaek * @expectedException Dwoo_Compilation_Exception
140 Seldaek */
141 Seldaek public function testMixedParametersWrongOrder()
142 Seldaek {
143 aa5c96 seldaek $tpl = new Dwoo_Template_String("{assign value=5, 3)}");
144 157ad4 Seldaek $tpl->forceCompilation();
145 Seldaek $this->dwoo->get($tpl, array(), $this->compiler);
146 Seldaek }
147 Seldaek
148 1d3732 Seldaek public function testMixedParamsMultiline()
149 Seldaek {
150 Seldaek $tpl = new Dwoo_Template_String('{replace(
151 Seldaek $foo search="BAR"|lower
152 Seldaek replace="BAR"
153 Seldaek )}');
154 Seldaek $tpl->forceCompilation();
155 Seldaek
156 Seldaek $this->assertEquals('BAR', $this->dwoo->get($tpl, array('foo'=>'bar'), $this->compiler));
157 Seldaek
158 Seldaek $tpl = new Dwoo_Template_String('{replace(
159 Seldaek $foo search=$bar|lower
160 Seldaek replace="BAR"
161 Seldaek )}');
162 Seldaek $tpl->forceCompilation();
163 Seldaek
164 Seldaek $this->assertEquals('BAR', $this->dwoo->get($tpl, array('foo'=>'bar','bar'=>'BAR'), $this->compiler));
165 Seldaek }
166 Seldaek
167 dc33f4 Seldaek public function testRecursiveCall()
168 Seldaek {
169 9cf6ad Seldaek $tpl = new Dwoo_Template_String('{lower(reverse(upper($foo)))}');
170 dc33f4 Seldaek $tpl->forceCompilation();
171 Seldaek
172 Seldaek $this->assertEquals('rab', $this->dwoo->get($tpl, array('foo'=>'bar'), $this->compiler));
173 Seldaek }
174 Seldaek
175 Seldaek public function testComplexRecursiveCall()
176 Seldaek {
177 9cf6ad Seldaek $tpl = new Dwoo_Template_String('{lower reverse($foo|reverse|upper)}');
178 dc33f4 Seldaek $tpl->forceCompilation();
179 Seldaek
180 Seldaek $this->assertEquals('bar', $this->dwoo->get($tpl, array('foo'=>'BaR'), $this->compiler));
181 Seldaek }
182 Seldaek
183 Seldaek public function testComplexRecursiveCall2()
184 Seldaek {
185 9cf6ad Seldaek $tpl = new Dwoo_Template_String('{str_repeat "AB`$foo|reverse|spacify:o`CD" 3}');
186 dc33f4 Seldaek $tpl->forceCompilation();
187 Seldaek $this->assertEquals('AB3o2o1CDAB3o2o1CDAB3o2o1CD', $this->dwoo->get($tpl, array('foo'=>'123'), $this->compiler));
188 Seldaek }
189 Seldaek
190 Seldaek public function testWhitespace()
191 Seldaek {
192 9cf6ad Seldaek $tpl = new Dwoo_Template_String("{\$foo}{\$foo}\n{\$foo}\n\n{\$foo}\n\n\n{\$foo}");
193 dc33f4 Seldaek $tpl->forceCompilation();
194 Seldaek $this->assertEquals("aa\na\n\na\n\n\na", $this->dwoo->get($tpl, array('foo'=>'a'), $this->compiler));
195 Seldaek }
196 Seldaek
197 Seldaek public function testLiteral()
198 Seldaek {
199 9cf6ad Seldaek $tpl = new Dwoo_Template_String('{literal}{$foo}{hurray}{/literal}');
200 dc33f4 Seldaek $tpl->forceCompilation();
201 Seldaek $this->assertEquals('{$foo}{hurray}', $this->dwoo->get($tpl, array(), $this->compiler));
202 Seldaek }
203 Seldaek
204 f377fd Seldaek /**
205 Seldaek * @expectedException Dwoo_Compilation_Exception
206 Seldaek */
207 Seldaek public function testUnclosedLiteral()
208 Seldaek {
209 Seldaek $tpl = new Dwoo_Template_String('{literal}{$foo}{hurray}');
210 Seldaek $tpl->forceCompilation();
211 Seldaek $this->dwoo->get($tpl, array(), $this->compiler);
212 Seldaek }
213 Seldaek
214 dc33f4 Seldaek public function testEscaping()
215 Seldaek {
216 6c4451 Seldaek $tpl = new Dwoo_Template_String('\{foo\{bar\\\\{$var}}{"tes}t"}{"foo\"lol\"bar"}');
217 dc33f4 Seldaek $tpl->forceCompilation();
218 Seldaek $this->assertEquals('{foo{bar\\1}tes}tfoo"lol"bar', $this->dwoo->get($tpl, array('var'=>1), $this->compiler));
219 Seldaek }
220 Seldaek
221 Seldaek public function testFunctions()
222 Seldaek {
223 9cf6ad Seldaek $tpl = new Dwoo_Template_String('{dump()}{dump( )}{dump}');
224 dc33f4 Seldaek $tpl->forceCompilation();
225 9cf6ad Seldaek $this->assertEquals('<div style="background:#aaa; padding:5px; margin:5px; color:#000;">data (current scope): <div style="background:#ccc;"></div></div><div style="background:#aaa; padding:5px; margin:5px; color:#000;">data (current scope): <div style="background:#ccc;"></div></div><div style="background:#aaa; padding:5px; margin:5px; color:#000;">data (current scope): <div style="background:#ccc;"></div></div>', $this->dwoo->get($tpl, array(), $this->compiler));
226 dc33f4 Seldaek }
227 Seldaek
228 Seldaek public function testExpressions()
229 Seldaek {
230 9cf6ad Seldaek $tpl = new Dwoo_Template_String('{$foo+5} {$foo+$foo} {$foo+3*$foo} {$foo*$foo+4*$foo} {$foo*2/2|number_format} {$foo*2/3|number_format:1} {number_format $foo*2/3 1} {if $foo+5>9 && $foo < 7 && $foo+$foo==$foo*2}win{/if} {$arr[$foo+3]}');
231 dc33f4 Seldaek $tpl->forceCompilation();
232 Seldaek
233 Seldaek $this->assertEquals("10 10 40 145 5 3.3 3.3 win win", $this->dwoo->get($tpl, array('foo'=>5, 'arr'=>array(8=>'win')), $this->compiler));
234 5fc482 Seldaek }
235 dc33f4 Seldaek
236 5fc482 Seldaek public function testDelimitedExpressionsInString()
237 Seldaek {
238 Seldaek $tpl = new Dwoo_Template_String('{"`$foo/$foo`"}');
239 dc33f4 Seldaek $tpl->forceCompilation();
240 Seldaek
241 Seldaek $this->assertEquals("1", $this->dwoo->get($tpl, array('foo'=>5), $this->compiler));
242 Seldaek }
243 Seldaek
244 5fc482 Seldaek public function testNonDelimitedExpressionsInString()
245 dc33f4 Seldaek {
246 5fc482 Seldaek $tpl = new Dwoo_Template_String('{"$foo/$foo"}');
247 dc33f4 Seldaek $tpl->forceCompilation();
248 Seldaek
249 Seldaek $this->assertEquals("5/5", $this->dwoo->get($tpl, array('foo'=>5), $this->compiler));
250 Seldaek }
251 Seldaek
252 Seldaek public function testConstants()
253 Seldaek {
254 1e2469 Seldaek if (!defined('TEST')) {
255 dc33f4 Seldaek define('TEST', 'Test');
256 1e2469 Seldaek }
257 9315dc seldaek $tpl = new Dwoo_Template_String('{$dwoo.const.TEST} {$dwoo.const.Dwoo_Core::FUNC_PLUGIN*$dwoo.const.Dwoo_Core::BLOCK_PLUGIN}');
258 dc33f4 Seldaek $tpl->forceCompilation();
259 Seldaek
260 9315dc seldaek $this->assertEquals(TEST.' '.(Dwoo_Core::FUNC_PLUGIN*Dwoo_Core::BLOCK_PLUGIN), $this->dwoo->get($tpl, array(), $this->compiler));
261 dc33f4 Seldaek }
262 Seldaek
263 Seldaek public function testShortConstants()
264 Seldaek {
265 1e2469 Seldaek if (!defined('TEST')) {
266 dc33f4 Seldaek define('TEST', 'Test');
267 1e2469 Seldaek }
268 9315dc seldaek $tpl = new Dwoo_Template_String('{%TEST} {$dwoo.const.Dwoo_Core::FUNC_PLUGIN*%Dwoo_Core::BLOCK_PLUGIN}');
269 dc33f4 Seldaek $tpl->forceCompilation();
270 Seldaek
271 9315dc seldaek $this->assertEquals(TEST.' '.(Dwoo_Core::FUNC_PLUGIN*Dwoo_Core::BLOCK_PLUGIN), $this->dwoo->get($tpl, array(), $this->compiler));
272 dc33f4 Seldaek }
273 6c4451 Seldaek
274 c6133a Seldaek public function testShortClassConstants()
275 Seldaek {
276 Seldaek $tpl = new Dwoo_Template_String('{if %CompilerTests::FOO == 3}{%CompilerTests::FOO}{/}');
277 Seldaek $tpl->forceCompilation();
278 Seldaek
279 Seldaek $this->assertEquals(self::FOO, $this->dwoo->get($tpl, array(), $this->compiler));
280 Seldaek }
281 dc33f4 Seldaek
282 Seldaek public function testAltDelimiters()
283 Seldaek {
284 6c4451 Seldaek $tpl = new Dwoo_Template_String('{"test"} <%"test"%> <%"foo{lol}%>"%>');
285 dc33f4 Seldaek $tpl->forceCompilation();
286 Seldaek $this->compiler->setDelimiters('<%', '%>');
287 Seldaek $this->assertEquals('{"test"} test foo{lol}%>', $this->dwoo->get($tpl, array(), $this->compiler));
288 Seldaek
289 9cf6ad Seldaek $tpl = new Dwoo_Template_String('d"O"b');
290 dc33f4 Seldaek $tpl->forceCompilation();
291 Seldaek $this->compiler->setDelimiters('d', 'b');
292 Seldaek $this->assertEquals('O', $this->dwoo->get($tpl, array(), $this->compiler));
293 Seldaek
294 9cf6ad Seldaek $tpl = new Dwoo_Template_String('<!-- "O" --> \<!-- ');
295 dc33f4 Seldaek $tpl->forceCompilation();
296 Seldaek $this->compiler->setDelimiters('<!-- ', ' -->');
297 Seldaek $this->assertEquals('O <!-- ', $this->dwoo->get($tpl, array(), $this->compiler));
298 Seldaek
299 Seldaek $this->compiler->setDelimiters('{', '}');
300 Seldaek }
301 Seldaek
302 Seldaek public function testNumberedIndexes()
303 Seldaek {
304 9cf6ad Seldaek $tpl = new Dwoo_Template_String('{$100}-{$150}-{if $0}FAIL{/if}');
305 dc33f4 Seldaek $tpl->forceCompilation();
306 Seldaek
307 Seldaek $this->assertEquals('bar-foo-', $this->dwoo->get($tpl, array('100'=>'bar', 150=>'foo'), $this->compiler));
308 Seldaek }
309 Seldaek
310 Seldaek public function testParseBool()
311 Seldaek {
312 9cf6ad Seldaek $tpl = new Dwoo_Template_String('{if (true === yes && true === on) && (false===off && false===no)}okay{/if}');
313 dc33f4 Seldaek $tpl->forceCompilation();
314 Seldaek
315 Seldaek $this->assertEquals('okay', $this->dwoo->get($tpl, array(), $this->compiler));
316 Seldaek }
317 Seldaek
318 Seldaek public function testMethodCalls()
319 Seldaek {
320 f9e789 Jordi $tpl = new Dwoo_Template_String('{$a} {$a->foo()} {$b[$c]->foo()} {$a->bar()+$a->bar()} {$a->baz(5, $foo)} {$a->make(5)->getInt()} {$a->make(5)->getInt()/2} {$a->_foo($foo, 5)} {$a->_fooChain()->_foo(5, $foo)}');
321 dc33f4 Seldaek $tpl->forceCompilation();
322 Seldaek
323 Seldaek $a = new MethodCallsHelper();
324 f9e789 Jordi $this->assertEquals('obj 0 1 7 10bar 5 2.5 -5bar- -bar5-', $this->dwoo->get($tpl, array('a'=>$a, 'b'=>array('test'=>$a), 'c'=>'test', 'foo'=>'bar'), $this->compiler));
325 dc33f4 Seldaek }
326 Seldaek
327 Seldaek public function testLooseTagHandling()
328 Seldaek {
329 Seldaek $this->compiler->setLooseOpeningHandling(true);
330 Seldaek $this->assertEquals($this->compiler->getLooseOpeningHandling(), true);
331 Seldaek
332 9cf6ad Seldaek $tpl = new Dwoo_Template_String('{ $a }{$a }{ $a}{$a}');
333 dc33f4 Seldaek $tpl->forceCompilation();
334 Seldaek
335 Seldaek $this->assertEquals('moomoomoomoo', $this->dwoo->get($tpl, array('a'=>'moo'), $this->compiler));
336 Seldaek
337 Seldaek $this->compiler->setLooseOpeningHandling(false);
338 9cf6ad Seldaek $tpl = new Dwoo_Template_String('{ $a }{$a }{ $a}{$a}');
339 dc33f4 Seldaek $tpl->forceCompilation();
340 Seldaek
341 Seldaek $this->assertEquals('{ $a }moo{ $a}moo', $this->dwoo->get($tpl, array('a'=>'moo'), $this->compiler));
342 Seldaek }
343 Seldaek
344 f9676b Seldaek public function testDwooDotShortcut()
345 dc33f4 Seldaek {
346 9cf6ad Seldaek $tpl = new Dwoo_Template_String('{$.server.SCRIPT_NAME}{foreach $a item}{$.foreach.default.iteration}{$item}{$.foreach.$b.$c}{/foreach}');
347 dc33f4 Seldaek $tpl->forceCompilation();
348 Seldaek
349 Seldaek $this->assertEquals($_SERVER['SCRIPT_NAME'].'1a12b2', $this->dwoo->get($tpl, array('a'=>array('a','b'), 'b'=>'default', 'c'=>'iteration'), $this->compiler));
350 Seldaek }
351 Seldaek
352 f9676b Seldaek public function testRootAndParentShortcut()
353 Seldaek {
354 Seldaek $tpl = new Dwoo_Template_String('{with $a}{$__.b}{$_.b}{$0}{/with}{$__.b}');
355 Seldaek $tpl->forceCompilation();
356 Seldaek
357 Seldaek $this->assertEquals('defaultdefaultadefault', $this->dwoo->get($tpl, array('a'=>array('a','b'), 'b'=>'default'), $this->compiler));
358 Seldaek }
359 Seldaek
360 Seldaek public function testCurrentScopeShortcut()
361 Seldaek {
362 Seldaek $tpl = new Dwoo_Template_String('{loop $}{$_key} > {loop $}{$}{/loop}{/loop}');
363 Seldaek $tpl->forceCompilation();
364 Seldaek
365 Seldaek $this->assertEquals('1 > ab2 > cd', $this->dwoo->get($tpl, array('1'=>array('a','b'), '2'=>array('c','d')), $this->compiler));
366 Seldaek
367 Seldaek $tpl = new Dwoo_Template_String('{with $a}{$1}{/with}{loop $a}{$}{/loop}');
368 Seldaek $tpl->forceCompilation();
369 Seldaek
370 Seldaek $this->assertEquals('bab', $this->dwoo->get($tpl, array('a'=>array('a','b'), 'b'=>'default', 'c'=>'iteration'), $this->compiler));
371 Seldaek }
372 Seldaek
373 187a71 Seldaek public function testCloseBlockShortcut()
374 Seldaek {
375 Seldaek $tpl = new Dwoo_Template_String('{loop $}{$_key} > {loop $}{$}{/}{/}');
376 Seldaek $tpl->forceCompilation();
377 Seldaek
378 Seldaek $this->assertEquals('1 > ab2 > cd', $this->dwoo->get($tpl, array('1'=>array('a','b'), '2'=>array('c','d')), $this->compiler));
379 Seldaek }
380 Seldaek
381 Seldaek public function testImplicitCloseBlock()
382 Seldaek {
383 Seldaek $tpl = new Dwoo_Template_String('{loop $}{$_key} > {loop $}{$}');
384 Seldaek $tpl->forceCompilation();
385 Seldaek
386 Seldaek $this->assertEquals('1 > ab2 > cd', $this->dwoo->get($tpl, array('1'=>array('a','b'), '2'=>array('c','d')), $this->compiler));
387 Seldaek }
388 Seldaek
389 dc33f4 Seldaek public function testAssignAndIncrement()
390 Seldaek {
391 9cf6ad Seldaek $tpl = new Dwoo_Template_String('{$foo}{$foo+=3}{$foo}
392 dc33f4 Seldaek {$foo}{$foo-=3}{$foo}
393 Seldaek {$foo++}{$foo++}{$foo}{$foo*=$foo}{$foo}
394 Seldaek {$foo--}{$foo=5}{$foo}');
395 Seldaek $tpl->forceCompilation();
396 Seldaek
397 Seldaek $this->assertEquals("03\n30\n0124\n45", $this->dwoo->get($tpl, array('foo'=>0), $this->compiler));
398 Seldaek
399 9cf6ad Seldaek $tpl = new Dwoo_Template_String('{$foo="moo"}{$foo}{$foo=math("3+5")}{$foo}');
400 dc33f4 Seldaek $tpl->forceCompilation();
401 Seldaek
402 Seldaek $this->assertEquals("moo8", $this->dwoo->get($tpl, array('foo'=>0), $this->compiler));
403 Seldaek }
404 Seldaek
405 19287d Seldaek public function testAssignAndConcatenate()
406 Seldaek {
407 Seldaek $tpl = new Dwoo_Template_String('{$foo="test"}{$foo.="test"}{$foo}');
408 Seldaek $tpl->forceCompilation();
409 Seldaek
410 Seldaek $this->assertEquals("testtest", $this->dwoo->get($tpl, array('foo'=>0), $this->compiler));
411 Seldaek
412 Seldaek $tpl = new Dwoo_Template_String('{$foo.="baz"}{$foo|upper}');
413 Seldaek $tpl->forceCompilation();
414 Seldaek
415 Seldaek $this->assertEquals("BARBAZ", $this->dwoo->get($tpl, array('foo'=>'bar'), $this->compiler));
416 Seldaek }
417 Seldaek
418 dc33f4 Seldaek public function testSetStringValToTrueWhenUsingNamedParams()
419 Seldaek {
420 9315dc seldaek $this->dwoo->addPlugin('test', create_function('Dwoo_Core $dwoo, $name, $bool=false', 'return $bool ? $name."!" : $name."?";'));
421 9cf6ad Seldaek $tpl = new Dwoo_Template_String('{test name="Name"}{test name="Name" bool}');
422 dc33f4 Seldaek $tpl->forceCompilation();
423 Seldaek
424 Seldaek $this->assertEquals("Name?Name!", $this->dwoo->get($tpl, array(), $this->compiler));
425 Seldaek $this->dwoo->removePlugin('test');
426 Seldaek }
427 914a96 Seldaek
428 Seldaek /**
429 Seldaek * @expectedException Dwoo_Exception
430 Seldaek */
431 Seldaek public function testAddPreProcessorWithBadName()
432 Seldaek {
433 Seldaek $cmp = new Dwoo_Compiler();
434 Seldaek $cmp->addPreProcessor('__BAAAAD__', true);
435 1c727d Seldaek
436 Seldaek $tpl = new Dwoo_Template_String('');
437 Seldaek $tpl->forceCompilation();
438 Seldaek $this->dwoo->get($tpl, array(), $cmp);
439 914a96 Seldaek }
440 Seldaek
441 Seldaek /**
442 Seldaek * @expectedException Dwoo_Exception
443 Seldaek */
444 Seldaek public function testAddPostProcessorWithBadName()
445 Seldaek {
446 Seldaek $cmp = new Dwoo_Compiler();
447 Seldaek $cmp->addPostProcessor('__BAAAAD__', true);
448 1c727d Seldaek
449 Seldaek $tpl = new Dwoo_Template_String('');
450 Seldaek $tpl->forceCompilation();
451 Seldaek $this->dwoo->get($tpl, array(), $cmp);
452 914a96 Seldaek }
453 Seldaek
454 Seldaek /**
455 Seldaek * @expectedException Dwoo_Compilation_Exception
456 Seldaek */
457 Seldaek public function testCloseUnopenedBlock()
458 Seldaek {
459 Seldaek $tpl = new Dwoo_Template_String('{/foreach}');
460 Seldaek $tpl->forceCompilation();
461 Seldaek
462 Seldaek $this->dwoo->get($tpl, array(), $this->compiler);
463 Seldaek }
464 Seldaek
465 Seldaek /**
466 Seldaek * @expectedException Dwoo_Compilation_Exception
467 Seldaek */
468 Seldaek public function testParseError()
469 Seldaek {
470 Seldaek $tpl = new Dwoo_Template_String('{++}');
471 Seldaek $tpl->forceCompilation();
472 Seldaek
473 Seldaek $this->dwoo->get($tpl, array(), $this->compiler);
474 Seldaek }
475 Seldaek
476 Seldaek /**
477 Seldaek * @expectedException Dwoo_Compilation_Exception
478 Seldaek */
479 Seldaek public function testUnfinishedStringException()
480 Seldaek {
481 Seldaek $tpl = new Dwoo_Template_String('{"fooo}');
482 Seldaek $tpl->forceCompilation();
483 Seldaek
484 Seldaek $this->dwoo->get($tpl, array(), $this->compiler);
485 Seldaek }
486 Seldaek
487 Seldaek /**
488 Seldaek * @expectedException Dwoo_Compilation_Exception
489 Seldaek */
490 Seldaek public function testMissingArgumentException()
491 Seldaek {
492 Seldaek $tpl = new Dwoo_Template_String('{upper()}');
493 Seldaek $tpl->forceCompilation();
494 Seldaek
495 Seldaek $this->dwoo->get($tpl, array('foo'=>0), $this->compiler);
496 Seldaek }
497 Seldaek
498 Seldaek /**
499 Seldaek * @expectedException Dwoo_Compilation_Exception
500 Seldaek */
501 Seldaek public function testMissingArgumentExceptionVariation2()
502 Seldaek {
503 Seldaek $tpl = new Dwoo_Template_String('{upper foo=bar}');
504 Seldaek $tpl->forceCompilation();
505 Seldaek
506 6c4451 Seldaek $this->dwoo->get($tpl, array(), $this->compiler);
507 Seldaek }
508 Seldaek
509 Seldaek /**
510 Seldaek * @expectedException Dwoo_Compilation_Exception
511 Seldaek */
512 Seldaek public function testUnclosedTemplateTag()
513 Seldaek {
514 Seldaek $tpl = new Dwoo_Template_String('aa{upper foo=bar');
515 Seldaek $tpl->forceCompilation();
516 Seldaek
517 Seldaek $this->dwoo->get($tpl, array(), $this->compiler);
518 914a96 Seldaek }
519 1d3732 Seldaek
520 630089 Seldaek public function testAutoEscape()
521 Seldaek {
522 Seldaek $cmp = new Dwoo_Compiler();
523 Seldaek $cmp->setAutoEscape(true);
524 Seldaek $this->assertEquals(true, $cmp->getAutoEscape());
525 1d3732 Seldaek
526 630089 Seldaek $tpl = new Dwoo_Template_String('{$foo}{$foo|safe}');
527 Seldaek $tpl->forceCompilation();
528 Seldaek
529 Seldaek $this->assertEquals("a&lt;b&gt;ca<b>c", $this->dwoo->get($tpl, array('foo'=>'a<b>c'), $cmp));
530 Seldaek }
531 6c4451 Seldaek
532 7869a7 Seldaek public function testAutoEscapeWithFunctionCall()
533 Seldaek {
534 Seldaek $cmp = new Dwoo_Compiler();
535 Seldaek $cmp->setAutoEscape(true);
536 Seldaek $this->assertEquals(true, $cmp->getAutoEscape());
537 Seldaek
538 Seldaek $tpl = new Dwoo_Template_String('{upper $foo}{upper $foo|safe}');
539 Seldaek $tpl->forceCompilation();
540 Seldaek
541 Seldaek $this->assertEquals("A&LT;B&GT;CA<B>C", $this->dwoo->get($tpl, array('foo'=>'a<b>c'), $cmp));
542 Seldaek }
543 Seldaek
544 c6133a Seldaek public function testPhpInjection()
545 Seldaek {
546 Seldaek $tpl = new Dwoo_Template_String('{$foo}');
547 Seldaek $tpl->forceCompilation();
548 Seldaek
549 Seldaek $this->assertEquals('a <?php echo "foo"; ?>', $this->dwoo->get($tpl, array('foo'=>'a <?php echo "foo"; ?>'), $this->compiler));
550 Seldaek }
551 8c5c98 Seldaek
552 e3eb1b Seldaek public function testStaticMethodCall()
553 Seldaek {
554 Seldaek $tpl = new Dwoo_Template_String('{upper MethodCallsHelper::staticFoo(bar "baz")}');
555 Seldaek $tpl->forceCompilation();
556 8c5c98 Seldaek
557 Seldaek $this->assertEquals('-BAZBAR-', $this->dwoo->get($tpl, array(), $this->compiler));
558 Seldaek }
559 Seldaek
560 170592 Seldaek public function testFunctionCallsChaining()
561 Seldaek {
562 cacec9 Seldaek $tpl = new Dwoo_Template_String('{getobj()->foo()->Bar("hoy") getobj()->moo}');
563 170592 Seldaek $tpl->forceCompilation();
564 9315dc seldaek $dwoo = new Dwoo_Core(DWOO_COMPILE_DIR, DWOO_CACHE_DIR);
565 170592 Seldaek $dwoo->addPlugin('getobj', array(new PluginHelper(), 'call'));
566 Seldaek
567 cacec9 Seldaek $this->assertEquals('HOYyay', $dwoo->get($tpl, array(), $this->compiler));
568 170592 Seldaek }
569 Seldaek
570 8c5c98 Seldaek public function testPluginProxy()
571 Seldaek {
572 Seldaek $proxy = new ProxyHelper('baz',true,3);
573 9315dc seldaek $dwoo = new Dwoo_Core(DWOO_COMPILE_DIR, DWOO_CACHE_DIR);
574 8c5c98 Seldaek $dwoo->setPluginProxy($proxy);
575 Seldaek $tpl = new Dwoo_Template_String('{TestProxy("baz", true, 3)}');
576 Seldaek $tpl->forceCompilation();
577 Seldaek
578 Seldaek $this->assertEquals('valid', $dwoo->get($tpl, array(), $this->compiler));
579 Seldaek }
580 e1d4ca Seldaek
581 aa5c96 seldaek public function testCallingMethodOnProperty()
582 e1d4ca Seldaek {
583 Seldaek $tpl = new Dwoo_Template_String('{getobj()->instance->Bar("hoy")}');
584 Seldaek $tpl->forceCompilation();
585 9315dc seldaek $dwoo = new Dwoo_Core(DWOO_COMPILE_DIR, DWOO_CACHE_DIR);
586 e1d4ca Seldaek $dwoo->addPlugin('getobj', array(new PluginHelper(), 'call'));
587 Seldaek
588 Seldaek $this->assertEquals('HOY', $dwoo->get($tpl, array(), $this->compiler));
589 Seldaek }
590 05ec46 seldaek
591 seldaek public function testNestedCommentHandlingGetSet()
592 seldaek {
593 seldaek $cmp = new Dwoo_Compiler();
594 seldaek $this->assertEquals(false, $cmp->getNestedCommentsHandling());
595 seldaek $cmp->setNestedCommentsHandling(true);
596 seldaek $this->assertEquals(true, $cmp->getNestedCommentsHandling());
597 seldaek }
598 seldaek
599 seldaek public function testNestedCommentHandling()
600 seldaek {
601 seldaek $tpl = new Dwoo_Template_String('{* foo {* bar *} baz *}');
602 seldaek $tpl->forceCompilation();
603 seldaek $cmp = new Dwoo_Compiler();
604 seldaek $cmp->setNestedCommentsHandling(true);
605 seldaek $this->assertEquals('', $this->dwoo->get($tpl, array(), $cmp));
606 seldaek }
607 seldaek
608 seldaek public function testSecurityPolicyGetSet()
609 seldaek {
610 seldaek $cmp = new Dwoo_Compiler();
611 seldaek $policy = new Dwoo_Security_Policy();
612 seldaek $this->assertEquals(null, $cmp->getSecurityPolicy());
613 seldaek $cmp->setSecurityPolicy($policy);
614 seldaek $this->assertEquals($policy, $cmp->getSecurityPolicy());
615 seldaek }
616 seldaek
617 seldaek public function testPointerGetSet()
618 seldaek {
619 seldaek $cmp = new Dwoo_Compiler();
620 seldaek $this->assertEquals(null, $cmp->getPointer());
621 seldaek $cmp->setPointer(5);
622 seldaek $this->assertEquals(5, $cmp->getPointer());
623 seldaek $cmp->setPointer(5, true);
624 seldaek $this->assertEquals(10, $cmp->getPointer());
625 seldaek }
626 seldaek
627 seldaek public function testLineGetSet()
628 seldaek {
629 seldaek $cmp = new Dwoo_Compiler();
630 seldaek $this->assertEquals(null, $cmp->getLine());
631 seldaek $cmp->setLine(5);
632 seldaek $this->assertEquals(5, $cmp->getLine());
633 seldaek $cmp->setLine(5, true);
634 seldaek $this->assertEquals(10, $cmp->getLine());
635 seldaek }
636 seldaek
637 seldaek public function testTemplateSourceGetSet()
638 seldaek {
639 seldaek $cmp = new Dwoo_Compiler();
640 seldaek $this->assertEquals(null, $cmp->getTemplateSource());
641 seldaek $cmp->setTemplateSource("foobar");
642 seldaek $cmp->setPointer(3);
643 seldaek $this->assertEquals('foobar', $cmp->getTemplateSource());
644 seldaek $this->assertEquals('bar', $cmp->getTemplateSource(true));
645 seldaek $this->assertEquals('r', $cmp->getTemplateSource(5));
646 seldaek $cmp->setTemplateSource("baz", true);
647 seldaek $this->assertEquals('foobaz', $cmp->getTemplateSource());
648 seldaek $this->assertEquals('baz', $cmp->getTemplateSource(true));
649 seldaek $this->assertEquals('z', $cmp->getTemplateSource(5));
650 seldaek $cmp->setTemplateSource("baz");
651 seldaek $this->assertEquals('baz', $cmp->getTemplateSource());
652 seldaek $this->assertEquals('', $cmp->getTemplateSource(true));
653 seldaek $this->assertEquals('az', $cmp->getTemplateSource(1));
654 seldaek }
655 seldaek
656 seldaek public function testEndInstruction()
657 seldaek {
658 seldaek $tpl = new Dwoo_Template_String('{$foo = 4; $bar = 5; $foo; $bar}');
659 seldaek $tpl->forceCompilation();
660 seldaek $this->assertEquals('45', $this->dwoo->get($tpl, array(), $this->compiler));
661 seldaek }
662 seldaek
663 seldaek public function testExpressionAsParameter()
664 seldaek {
665 seldaek $tpl = new Dwoo_Template_String('{$foo = 4; $bar = 8; lower value=$foo + $bar}');
666 seldaek $tpl->forceCompilation();
667 seldaek $this->assertEquals('12', $this->dwoo->get($tpl, array(), $this->compiler));
668 seldaek }
669 seldaek
670 seldaek public function testExpressionAsAssignment()
671 seldaek {
672 seldaek $tpl = new Dwoo_Template_String('{$foo = 4; $bar = $foo + 5; $bar}');
673 seldaek $tpl->forceCompilation();
674 seldaek $this->assertEquals('9', $this->dwoo->get($tpl, array(), $this->compiler));
675 seldaek }
676 seldaek
677 seldaek public function testModifierOnFunc()
678 seldaek {
679 seldaek $tpl = new Dwoo_Template_String('{upper("fOo")|lower}');
680 seldaek $tpl->forceCompilation();
681 seldaek $this->assertEquals('foo', $this->dwoo->get($tpl, array(), $this->compiler));
682 seldaek }
683 936427 seldaek
684 703f30 seldaek public function testStaticPropertyAccess()
685 seldaek {
686 seldaek $tpl = new Dwoo_Template_String('{StaticHelper::$foo}/{StaticHelper::$foo * StaticHelper::$foo + 5}/{upper StaticHelper::$foo}/{StaticHelper::$foo++}/{StaticHelper::$foo}');
687 seldaek $tpl->forceCompilation();
688 seldaek $this->assertEquals('33/1094/33/33/34', $this->dwoo->get($tpl, array(), $this->compiler));
689 seldaek }
690 53a2dd seldaek
691 seldaek public function testExcessiveArguments()
692 seldaek {
693 seldaek $tpl = new Dwoo_Template_String('{excessArgsHelper a b c d e f}');
694 seldaek $tpl->forceCompilation();
695 seldaek $this->assertEquals('a:b:c:d:e:f', $this->dwoo->get($tpl, array(), $this->compiler));
696 seldaek }
697 936427 seldaek
698 seldaek public function testParsingOfMultilineIf()
699 seldaek {
700 seldaek $tpl = new Dwoo_Template_String('{if 0
701 seldaek || $null == "aa"}
702 seldaek fail
703 seldaek {/if}');
704 seldaek $tpl->forceCompilation();
705 seldaek $this->assertEquals('', trim($this->dwoo->get($tpl, array(), $this->compiler)));
706 seldaek }
707 8eb1d2 seldaek
708 seldaek public function testParsingOfMethodWithFollowingArgs()
709 seldaek {
710 seldaek $tpl = new Dwoo_Template_String('{lower cat($obj->Bar("test"), "TEST")}');
711 seldaek $tpl->forceCompilation();
712 seldaek $this->assertEquals('testtest', $this->dwoo->get($tpl, array('obj'=>new PluginHelper()), $this->compiler));
713 seldaek }
714 f9e789 Jordi
715 Jordi public function testFunctionCanStartWithUnderscore()
716 Jordi {
717 Jordi $tpl = new Dwoo_Template_String('{_underscoreHelper("test", _underscoreHelper("bar", 10))|_underscoreModifierHelper}');
718 Jordi $tpl->forceCompilation();
719 Jordi $this->assertEquals('_--10bar-test-_', $this->dwoo->get($tpl, array(), $this->compiler));
720 Jordi }
721 Jordi
722 53a2dd seldaek }
723 seldaek
724 seldaek function excessArgsHelper($a) {
725 seldaek $args = func_get_args();
726 seldaek return implode(':', $args);
727 703f30 seldaek }
728 seldaek
729 f9e789 Jordi function _underscoreHelper($foo, $bar) {
730 Jordi return "-$bar$foo-";
731 Jordi }
732 Jordi
733 Jordi function _underscoreModifierHelper($value) {
734 Jordi return "_${value}_";
735 Jordi }
736 Jordi
737 703f30 seldaek class StaticHelper {
738 seldaek static $foo = 33;
739 8c5c98 Seldaek }
740 Seldaek
741 Seldaek class ProxyHelper implements Dwoo_IPluginProxy
742 Seldaek {
743 Seldaek public function __construct()
744 Seldaek {
745 Seldaek $this->params = func_get_args();
746 Seldaek }
747 Seldaek
748 9bc397 Seldaek public function handles($name)
749 8c5c98 Seldaek {
750 Seldaek return $name === 'TestProxy';
751 Seldaek }
752 Seldaek
753 Seldaek public function checkTestProxy()
754 Seldaek {
755 Seldaek return func_get_args() === $this->params ? 'valid' : 'fubar';
756 Seldaek }
757 Seldaek
758 9bc397 Seldaek public function getCode($m, $p)
759 8c5c98 Seldaek {
760 9bc397 Seldaek if (isset($p['*'])) {
761 Seldaek return '$this->getPluginProxy()->check'.$m.'('.implode(',', $p['*']).')';
762 Seldaek } else {
763 Seldaek return '$this->getPluginProxy()->check'.$m.'()';
764 Seldaek }
765 Seldaek }
766 Seldaek
767 Seldaek public function getCallback($name)
768 Seldaek {
769 Seldaek return array($this, 'callbackHelper');
770 Seldaek }
771 Seldaek
772 Seldaek public function getLoader($name)
773 Seldaek {
774 Seldaek return '';
775 Seldaek }
776 Seldaek
777 Seldaek private function callbackHelper(array $rest = array()) {
778 Seldaek
779 e3eb1b Seldaek }
780 dc33f4 Seldaek }
781 Seldaek
782 170592 Seldaek class PluginHelper
783 Seldaek {
784 cacec9 Seldaek public $moo = "yay";
785 e1d4ca Seldaek public $instance;
786 Seldaek
787 Seldaek public function __construct()
788 Seldaek {
789 Seldaek $this->instance = $this;
790 Seldaek }
791 cacec9 Seldaek
792 9315dc seldaek public function callWithDwoo(Dwoo_Core $dwoo)
793 170592 Seldaek {
794 Seldaek return $this;
795 Seldaek }
796 Seldaek
797 Seldaek public function call()
798 Seldaek {
799 Seldaek return $this;
800 Seldaek }
801 Seldaek
802 Seldaek public function foo()
803 Seldaek {
804 Seldaek return $this;
805 Seldaek }
806 Seldaek
807 cacec9 Seldaek public function Bar($a)
808 170592 Seldaek {
809 Seldaek return strtoupper($a);
810 Seldaek }
811 Seldaek }
812 Seldaek
813 Seldaek class MethodCallsHelper
814 Seldaek {
815 dc33f4 Seldaek public function __construct($int=0) {
816 Seldaek $this->int = $int;
817 Seldaek }
818 Seldaek public function getInt() {
819 Seldaek return $this->int;
820 Seldaek }
821 Seldaek public function make($a=0) {
822 Seldaek return new self($a);
823 Seldaek }
824 Seldaek public function foo() {
825 Seldaek static $a=0;
826 Seldaek return $a++;
827 Seldaek }
828 Seldaek public function bar() {
829 Seldaek static $a=3;
830 Seldaek return $a++;
831 Seldaek }
832 Seldaek public function baz($int, $str) {
833 Seldaek return ($int+5).$str;
834 Seldaek }
835 Seldaek public function __toString() { return 'obj'; }
836 8c5c98 Seldaek
837 e3eb1b Seldaek public static function staticFoo($bar, $baz) {
838 Seldaek return "-$baz$bar-";
839 Seldaek }
840 f9e789 Jordi
841 Jordi public function _foo($bar, $baz) {
842 Jordi return "-$baz$bar-";
843 Jordi }
844 Jordi
845 Jordi public function _fooChain() {
846 Jordi return $this;
847 Jordi }
848 dc33f4 Seldaek }