Arbit - project tracking

Dwoo

Browse source code

File: / tests/ BlockTests.php

Type
text/plain text/plain
Last Author
seldaek
Version
9315dcc815b5063f6212c06dd2c20b8b75004434
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 BlockTests extends PHPUnit_Framework_TestCase
6 Seldaek {
7 Seldaek protected $compiler;
8 Seldaek protected $dwoo;
9 Seldaek
10 Seldaek public function __construct()
11 Seldaek {
12 Seldaek // extend this class and override this in your constructor to test a modded compiler
13 9cf6ad Seldaek $this->compiler = new Dwoo_Compiler();
14 9315dc seldaek $this->dwoo = new Dwoo_Core(DWOO_COMPILE_DIR, DWOO_CACHE_DIR);
15 dc33f4 Seldaek }
16 Seldaek
17 5fc482 Seldaek public function testA()
18 Seldaek {
19 Seldaek $tpl = new Dwoo_Template_String('{a "http://foo/" test="foo" bar="bar"; "Foo!" /}');
20 Seldaek $tpl->forceCompilation();
21 Seldaek $this->assertEquals('<a href="http://foo/" test="foo" bar="bar">Foo!</a>', $this->dwoo->get($tpl, array(), $this->compiler));
22 Seldaek
23 05ec46 seldaek $tpl = new Dwoo_Template_String('{a "http://foo/" /}');
24 seldaek $tpl->forceCompilation();
25 seldaek $this->assertEquals('<a href="http://foo/">http://foo/</a>', $this->dwoo->get($tpl, array(), $this->compiler));
26 seldaek
27 seldaek $tpl = new Dwoo_Template_String('{a "http://foo/"; $link /}');
28 5fc482 Seldaek $tpl->forceCompilation();
29 05ec46 seldaek $this->assertEquals('<a href="http://foo/">moo</a>', $this->dwoo->get($tpl, array('link'=>'moo'), $this->compiler));
30 5fc482 Seldaek
31 05ec46 seldaek $tpl = new Dwoo_Template_String('{a $url test="foo" bar="bar"}');
32 seldaek $tpl->forceCompilation();
33 5fc482 Seldaek $this->assertEquals('<a href="http://foo/" test="foo" bar="bar">http://foo/</a>', $this->dwoo->get($tpl, array('url'=>'http://foo/'), $this->compiler));
34 Seldaek
35 Seldaek $tpl = new Dwoo_Template_String('{a $url foo="bar"; "text" /}
36 Seldaek {a $url; "" /}
37 Seldaek {a $url; /}
38 Seldaek {a $url}{/}');
39 Seldaek $tpl->forceCompilation();
40 Seldaek $this->assertEquals('<a href="http://foo/" foo="bar">text</a>
41 Seldaek <a href="http://foo/"></a>
42 Seldaek <a href="http://foo/">http://foo/</a>
43 Seldaek <a href="http://foo/">http://foo/</a>', $this->dwoo->get($tpl, array('url'=>'http://foo/'), $this->compiler));
44 05ec46 seldaek
45 seldaek // fixes the init call not being called (which is normal)
46 seldaek $fixCall = new Dwoo_Plugin_a($this->dwoo);
47 seldaek $fixCall->init('');
48 5fc482 Seldaek }
49 Seldaek
50 630089 Seldaek public function testAutoEscape()
51 Seldaek {
52 Seldaek $cmp = new Dwoo_Compiler();
53 Seldaek $cmp->setAutoEscape(true);
54 77501e Seldaek
55 630089 Seldaek $tpl = new Dwoo_Template_String('{$foo}{auto_escape off}{$foo}{/}');
56 Seldaek $tpl->forceCompilation();
57 Seldaek
58 Seldaek $this->assertEquals("a&lt;b&gt;ca<b>c", $this->dwoo->get($tpl, array('foo'=>'a<b>c'), $cmp));
59 9315dc seldaek
60 9b4b83 Seldaek $cmp->setAutoEscape(false);
61 630089 Seldaek $tpl = new Dwoo_Template_String('{$foo}{auto_escape true}{$foo}{/}');
62 Seldaek $tpl->forceCompilation();
63 Seldaek
64 Seldaek $this->assertEquals("a<b>ca&lt;b&gt;c", $this->dwoo->get($tpl, array('foo'=>'a<b>c')));
65 77501e Seldaek
66 630089 Seldaek // fixes the init call not being called (which is normal)
67 Seldaek $fixCall = new Dwoo_Plugin_auto_escape($this->dwoo);
68 Seldaek $fixCall->init('');
69 Seldaek }
70 Seldaek
71 Seldaek /**
72 Seldaek * @expectedException Dwoo_Compilation_Exception
73 Seldaek */
74 Seldaek public function testAutoEscapeWrongParam()
75 Seldaek {
76 Seldaek $tpl = new Dwoo_Template_String('{$foo}{auto_escape slkfjsl}{$foo}{/}');
77 Seldaek $tpl->forceCompilation();
78 Seldaek
79 Seldaek $this->dwoo->get($tpl, array('foo'=>'a<b>c'));
80 Seldaek }
81 Seldaek
82 dc33f4 Seldaek public function testCapture()
83 Seldaek {
84 9cf6ad Seldaek $tpl = new Dwoo_Template_String('{capture name="foo" assign="foo"}BAR{/capture}{$dwoo.capture.foo}-{$foo}');
85 dc33f4 Seldaek $tpl->forceCompilation();
86 Seldaek $this->assertEquals('BAR-BAR', $this->dwoo->get($tpl, array(), $this->compiler));
87 Seldaek
88 703f30 seldaek $tpl = new Dwoo_Template_String('{capture "foo" "foo"}BAR{/capture}{capture "foo" "foo" true}BAR{/capture}{$foo}');
89 dc33f4 Seldaek $tpl->forceCompilation();
90 Seldaek $this->assertEquals('BARBAR', $this->dwoo->get($tpl, array(), $this->compiler));
91 Seldaek
92 05ec46 seldaek $tpl = new Dwoo_Template_String('{capture "foo" "foo" false true}
93 seldaek
94 seldaek BAZZ {/capture}{$foo}');
95 seldaek $tpl->forceCompilation();
96 seldaek $this->assertEquals('BAZZ', $this->dwoo->get($tpl, array(), $this->compiler));
97 seldaek
98 dc33f4 Seldaek // fixes the init call not being called (which is normal)
99 9cf6ad Seldaek $fixCall = new Dwoo_Plugin_capture($this->dwoo);
100 dc33f4 Seldaek $fixCall->init('');
101 Seldaek }
102 77501e Seldaek
103 c6133a Seldaek public function testDynamic()
104 Seldaek {
105 Seldaek $preTime = time();
106 Seldaek $tpl = new Dwoo_Template_String('{$pre}{dynamic}{$pre}{/}', 10, 'testDynamic');
107 Seldaek $tpl->forceCompilation();
108 Seldaek
109 Seldaek $this->assertEquals($preTime . $preTime, $this->dwoo->get($tpl, array('pre'=>$preTime), $this->compiler));
110 77501e Seldaek
111 c6133a Seldaek sleep(1);
112 Seldaek $postTime = time();
113 Seldaek $this->assertEquals($preTime . $postTime, $this->dwoo->get($tpl, array('pre'=>$postTime), $this->compiler));
114 Seldaek
115 Seldaek // fixes the init call not being called (which is normal)
116 Seldaek $fixCall = new Dwoo_Plugin_dynamic($this->dwoo);
117 Seldaek $fixCall->init('');
118 Seldaek }
119 dc33f4 Seldaek
120 Seldaek public function testExtends()
121 Seldaek {
122 9cf6ad Seldaek $tpl = new Dwoo_Template_File(dirname(__FILE__).DIRECTORY_SEPARATOR.'resources'.DIRECTORY_SEPARATOR.'extend1.html');
123 dc33f4 Seldaek $tpl->forceCompilation();
124 Seldaek
125 Seldaek $this->assertThat($this->dwoo->get($tpl, array(), $this->compiler), new DwooConstraintStringEquals("foo
126 Seldaek child1
127 Seldaek toplevelContent1
128 Seldaek bar
129 Seldaek toplevelContent2
130 Seldaek baz"));
131 Seldaek }
132 Seldaek
133 Seldaek public function testNonExtendedBlocksFromParent()
134 Seldaek {
135 9cf6ad Seldaek $tpl = new Dwoo_Template_File(dirname(__FILE__).DIRECTORY_SEPARATOR.'resources'.DIRECTORY_SEPARATOR.'toplevel.html');
136 dc33f4 Seldaek $tpl->forceCompilation();
137 Seldaek
138 Seldaek $this->assertThat($this->dwoo->get($tpl, array(), $this->compiler), new DwooConstraintStringEquals("foo
139 Seldaek
140 Seldaek toplevelContent1
141 Seldaek
142 Seldaek bar
143 Seldaek
144 Seldaek toplevelContent2
145 Seldaek
146 Seldaek baz"));
147 Seldaek // fixes the init call not being called (which is normal)
148 9cf6ad Seldaek $fixCall = new Dwoo_Plugin_block($this->dwoo);
149 dc33f4 Seldaek $fixCall->init('');
150 Seldaek }
151 Seldaek
152 Seldaek public function testExtendsMultiple()
153 Seldaek {
154 9cf6ad Seldaek $tpl = new Dwoo_Template_File(dirname(__FILE__).DIRECTORY_SEPARATOR.'resources'.DIRECTORY_SEPARATOR.'extend2.html');
155 dc33f4 Seldaek $tpl->forceCompilation();
156 Seldaek
157 Seldaek $this->assertThat($this->dwoo->get($tpl, array('foo'=>'bar'), $this->compiler), new DwooConstraintStringEquals("foo
158 Seldaek child1
159 Seldaek toplevelContent1child2
160 Seldaek bar
161 Seldaek FOObartoplevelContent2
162 Seldaek baz"));
163 Seldaek }
164 Seldaek
165 1e2469 Seldaek public function testIf ()
166 dc33f4 Seldaek {
167 9cf6ad Seldaek $tpl = new Dwoo_Template_String('{if "BAR"==reverse($foo|reverse|upper)}true{/if}');
168 dc33f4 Seldaek $tpl->forceCompilation();
169 Seldaek
170 Seldaek $this->assertEquals('true', $this->dwoo->get($tpl, array('foo'=>'bar'), $this->compiler));
171 Seldaek
172 6c4451 Seldaek // fixes the init call not being called (which is normal)
173 Seldaek $fixCall = new Dwoo_Plugin_if ($this->dwoo);
174 Seldaek $fixCall->init(array());
175 Seldaek }
176 5fc482 Seldaek
177 6c4451 Seldaek public function testIfVariation2 ()
178 Seldaek {
179 9cf6ad Seldaek $tpl = new Dwoo_Template_String('{if 4/2==2 && 2!=1 && 3>0 && 4<5 && 5<=5 && 6>=3 && 3===3 && "3"!==3}true{/if}');
180 dc33f4 Seldaek $tpl->forceCompilation();
181 Seldaek
182 Seldaek $this->assertEquals('true', $this->dwoo->get($tpl, array(), $this->compiler));
183 6c4451 Seldaek }
184 5fc482 Seldaek
185 6c4451 Seldaek public function testIfVariation3 ()
186 Seldaek {
187 9cf6ad Seldaek $tpl = new Dwoo_Template_String('{if 5%2==1 && !isset($foo)}true{/if}');
188 dc33f4 Seldaek $tpl->forceCompilation();
189 Seldaek
190 Seldaek $this->assertEquals('true', $this->dwoo->get($tpl, array(), $this->compiler));
191 6c4451 Seldaek }
192 5fc482 Seldaek
193 6c4451 Seldaek public function testIfVariation4 ()
194 Seldaek {
195 9cf6ad Seldaek $tpl = new Dwoo_Template_String('{if 5 is not div by 2 && 4 is div by 2 && 6 is even && 6 is not even by 5 && (3 is odd && 9 is odd by 3)}true{/if}');
196 dc33f4 Seldaek $tpl->forceCompilation();
197 Seldaek
198 Seldaek $this->assertEquals('true', $this->dwoo->get($tpl, array(), $this->compiler));
199 6c4451 Seldaek }
200 5fc482 Seldaek
201 6c4451 Seldaek public function testIfVariation5 ()
202 Seldaek {
203 9cf6ad Seldaek $tpl = new Dwoo_Template_String('{if (3==4 && 5==5) || 3==3}true{/if}');
204 dc33f4 Seldaek $tpl->forceCompilation();
205 Seldaek
206 Seldaek $this->assertEquals('true', $this->dwoo->get($tpl, array(), $this->compiler));
207 Seldaek }
208 Seldaek
209 1e2469 Seldaek public function testIfElseif ()
210 dc33f4 Seldaek {
211 9cf6ad Seldaek $tpl = new Dwoo_Template_String('{if "BAR" == "bar"}true{elseif "BAR"=="BAR"}false{/if}');
212 dc33f4 Seldaek $tpl->forceCompilation();
213 Seldaek
214 Seldaek $this->assertEquals('false', $this->dwoo->get($tpl, array(), $this->compiler));
215 Seldaek
216 Seldaek // fixes the init call not being called (which is normal)
217 1e2469 Seldaek $fixCall = new Dwoo_Plugin_elseif ($this->dwoo);
218 dc33f4 Seldaek $fixCall->init(array());
219 Seldaek }
220 Seldaek
221 Seldaek public function testIfElse()
222 Seldaek {
223 9cf6ad Seldaek $tpl = new Dwoo_Template_String('{if "BAR" == "bar"}true{else}false{/if}');
224 dc33f4 Seldaek $tpl->forceCompilation();
225 Seldaek
226 Seldaek $this->assertEquals('false', $this->dwoo->get($tpl, array(), $this->compiler));
227 Seldaek
228 Seldaek // fixes the init call not being called (which is normal)
229 9cf6ad Seldaek $fixCall = new Dwoo_Plugin_else($this->dwoo);
230 dc33f4 Seldaek $fixCall->init();
231 Seldaek }
232 Seldaek
233 5fc482 Seldaek public function testIfElseifElse()
234 Seldaek {
235 Seldaek $tpl = new Dwoo_Template_String('{if "BAR" == "bar"}true{elseif 3==5}true{else}false{/if}');
236 Seldaek $tpl->forceCompilation();
237 Seldaek
238 Seldaek $this->assertEquals('false', $this->dwoo->get($tpl, array(), $this->compiler));
239 Seldaek }
240 Seldaek
241 Seldaek public function testIfElseifElseifElse()
242 Seldaek {
243 Seldaek $tpl = new Dwoo_Template_String('{if "BAR" == "bar"}true{elseif 3==5}true{elseif 5==3}true{else}false{/if}');
244 Seldaek $tpl->forceCompilation();
245 Seldaek
246 Seldaek $this->assertEquals('false', $this->dwoo->get($tpl, array(), $this->compiler));
247 Seldaek }
248 Seldaek
249 Seldaek public function testIfElseifElseif()
250 Seldaek {
251 Seldaek $tpl = new Dwoo_Template_String('{if "BAR" == "bar"}true{elseif 3==5}true{elseif 5==5}moo{/if}');
252 Seldaek $tpl->forceCompilation();
253 Seldaek
254 Seldaek $this->assertEquals('moo', $this->dwoo->get($tpl, array(), $this->compiler));
255 Seldaek }
256 Seldaek
257 Seldaek public function testIfElseifElseifVariation2()
258 Seldaek {
259 Seldaek $tpl = new Dwoo_Template_String('{if "BAR" == "bar"}true{elseif 5==5}moo{elseif 3==5}true{/if}');
260 Seldaek $tpl->forceCompilation();
261 Seldaek
262 Seldaek $this->assertEquals('moo', $this->dwoo->get($tpl, array(), $this->compiler));
263 Seldaek }
264 Seldaek
265 dc33f4 Seldaek public function testIfElseImplicitUnset()
266 Seldaek {
267 9cf6ad Seldaek $tpl = new Dwoo_Template_String('{if $moo}true{else}false{/if}');
268 dc33f4 Seldaek $tpl->forceCompilation();
269 Seldaek
270 Seldaek $this->assertEquals('false', $this->dwoo->get($tpl, array(), $this->compiler));
271 Seldaek }
272 Seldaek
273 Seldaek public function testIfElseImplicitTrue()
274 Seldaek {
275 9cf6ad Seldaek $tpl = new Dwoo_Template_String('{if $moo}true{else}false{/if}');
276 dc33f4 Seldaek $tpl->forceCompilation();
277 Seldaek
278 Seldaek $this->assertEquals('true', $this->dwoo->get($tpl, array('moo'=>'i'), $this->compiler));
279 Seldaek }
280 Seldaek
281 6dd36b seldaek public function testFor()
282 dc33f4 Seldaek {
283 9cf6ad Seldaek $tpl = new Dwoo_Template_String('{for name=i from=$sub}{$i}.{$sub[$i]}{/for}');
284 dc33f4 Seldaek $tpl->forceCompilation();
285 Seldaek
286 Seldaek $this->assertEquals('0.foo1.bar2.baz3.qux', $this->dwoo->get($tpl, array('sub'=>array('foo','bar','baz','qux')), $this->compiler));
287 Seldaek
288 6dd36b seldaek $tpl = new Dwoo_Template_String('{for name=i from=$sub to=2}{$i}.{$sub[$i]}{/for}');
289 seldaek $tpl->forceCompilation();
290 seldaek
291 seldaek $this->assertEquals('0.foo1.bar', $this->dwoo->get($tpl, array('sub'=>array('foo','bar','baz','qux')), $this->compiler));
292 seldaek
293 dc33f4 Seldaek // fixes the init call not being called (which is normal)
294 1e2469 Seldaek $fixCall = new Dwoo_Plugin_for ($this->dwoo);
295 dc33f4 Seldaek $fixCall->init(null,null);
296 Seldaek }
297 Seldaek
298 05ec46 seldaek public function testForVars()
299 seldaek {
300 seldaek $tpl = new Dwoo_Template_String('{for name=i from=3 to=6}{$.for.i.index}|{$.for.i.iteration}|{$.for.i.first}|{$.for.i.last}|{$.for.i.show}|{$.for.i.total}||{/for}');
301 seldaek $tpl->forceCompilation();
302 seldaek $this->assertEquals('3|1|1||1|4||'.'4|2|||1|4||'.'5|3|||1|4||'.'6|4||1|1|4||', $this->dwoo->get($tpl, array(), $this->compiler));
303 seldaek }
304 seldaek
305 6dd36b seldaek public function testForVariations()
306 seldaek {
307 seldaek $tpl = new Dwoo_Template_String('{for i 1 1}-{$i}{/for}|{for i 1 2}-{$i}{/for}|{for i 1 3}-{$i}{/for}');
308 seldaek $tpl->forceCompilation();
309 seldaek
310 seldaek $this->assertEquals('-1|-1-2|-1-2-3', $this->dwoo->get($tpl, array('sub'=>array('foo','bar','baz','qux')), $this->compiler));
311 f01967 Seldaek
312 Seldaek $tpl = new Dwoo_Template_String('{for i 10 7}-{$i}{/for}');
313 Seldaek $tpl->forceCompilation();
314 Seldaek
315 Seldaek $this->assertEquals('-10-9-8-7', $this->dwoo->get($tpl, array('sub'=>array('foo','bar','baz','qux')), $this->compiler));
316 6dd36b seldaek }
317 seldaek
318 dc33f4 Seldaek public function testForElse()
319 Seldaek {
320 6dd36b seldaek $tpl = new Dwoo_Template_String('{for name=i from=array()}{$i}{else}Narp!{/for}');
321 dc33f4 Seldaek $tpl->forceCompilation();
322 Seldaek
323 Seldaek $this->assertEquals('Narp!', $this->dwoo->get($tpl, array(), $this->compiler));
324 Seldaek
325 9cf6ad Seldaek $tpl = new Dwoo_Template_String('{for name=i from=0 to=0}{$i}{forelse}Narp!{/for}');
326 dc33f4 Seldaek $tpl->forceCompilation();
327 Seldaek
328 6dd36b seldaek $this->assertEquals('0', $this->dwoo->get($tpl, array(), $this->compiler));
329 dc33f4 Seldaek
330 9cf6ad Seldaek $tpl = new Dwoo_Template_String('{for name=i from=0 to=10 step=3}{$i}{else}Narp!{/for}');
331 dc33f4 Seldaek $tpl->forceCompilation();
332 Seldaek
333 Seldaek $this->assertEquals('0369', $this->dwoo->get($tpl, array(), $this->compiler));
334 Seldaek
335 Seldaek // fixes the init call not being called (which is normal)
336 9cf6ad Seldaek $fixCall = new Dwoo_Plugin_forelse($this->dwoo);
337 dc33f4 Seldaek $fixCall->init(null,null);
338 Seldaek }
339 Seldaek
340 Seldaek public function testForeachSmarty()
341 Seldaek {
342 9cf6ad Seldaek $tpl = new Dwoo_Template_String('{foreach from=$sub key=key item=item}{$key}.{$item}{/foreach}');
343 dc33f4 Seldaek $tpl->forceCompilation();
344 Seldaek
345 Seldaek $this->assertEquals('0.foo1.bar', $this->dwoo->get($tpl, array('sub'=>array('foo','bar')), $this->compiler));
346 Seldaek
347 Seldaek // fixes the init call not being called (which is normal)
348 1e2469 Seldaek $fixCall = new Dwoo_Plugin_foreach ($this->dwoo);
349 dc33f4 Seldaek $fixCall->init('');
350 Seldaek }
351 Seldaek
352 Seldaek public function testForeachSmartyAlt()
353 Seldaek {
354 9cf6ad Seldaek $tpl = new Dwoo_Template_String('{foreach from=$sub key=key item=item}{$key}.{$item}{/foreach}');
355 dc33f4 Seldaek $tpl->forceCompilation();
356 Seldaek
357 Seldaek $this->assertEquals('0.foo1.bar', $this->dwoo->get($tpl, array('sub'=>array('foo','bar')), $this->compiler));
358 Seldaek }
359 Seldaek
360 Seldaek public function testForeachDwoo()
361 Seldaek {
362 1e2469 Seldaek // Item only, key arg is mapped to it just as foreach ($foo as $item)
363 9cf6ad Seldaek $tpl = new Dwoo_Template_String('{foreach $sub item}{$item}{/foreach}');
364 dc33f4 Seldaek $tpl->forceCompilation();
365 Seldaek
366 Seldaek $this->assertEquals('foobar', $this->dwoo->get($tpl, array('sub'=>array('foo','bar')), $this->compiler));
367 Seldaek
368 1e2469 Seldaek // Item and key used, key is second just as foreach ($foo as $key=>$item)
369 9cf6ad Seldaek $tpl = new Dwoo_Template_String('{foreach $sub key item}{$key}.{$item}{/foreach}');
370 dc33f4 Seldaek $tpl->forceCompilation();
371 Seldaek
372 Seldaek $this->assertEquals('0.foo1.bar', $this->dwoo->get($tpl, array('sub'=>array('foo','bar')), $this->compiler));
373 Seldaek }
374 Seldaek
375 05ec46 seldaek public function testForeachImplode()
376 seldaek {
377 seldaek $tpl = new Dwoo_Template_String('{foreach $sub item implode=", "}{$item}{/foreach}');
378 seldaek $tpl->forceCompilation();
379 seldaek $this->assertEquals('foo, bar', $this->dwoo->get($tpl, array('sub'=>array('foo','bar')), $this->compiler));
380 seldaek }
381 seldaek
382 dc33f4 Seldaek public function testForeachWithGlobalVars()
383 Seldaek {
384 9cf6ad Seldaek $tpl = new Dwoo_Template_String('{foreach $sub key item foo}{if $dwoo.foreach.foo.first}F{elseif $dwoo.foreach.foo.last}L{/if}{/foreach}');
385 dc33f4 Seldaek $tpl->forceCompilation();
386 Seldaek
387 Seldaek $this->assertEquals('FL', $this->dwoo->get($tpl, array('sub'=>array('foo','bar')), $this->compiler));
388 Seldaek }
389 Seldaek
390 Seldaek public function testForeachWithGlobalVarsPreceding()
391 Seldaek {
392 9cf6ad Seldaek $tpl = new Dwoo_Template_String('{if isset($dwoo.foreach.foo.total)}fail{/if}{foreach $sub key item foo}{/foreach}');
393 dc33f4 Seldaek $tpl->forceCompilation();
394 Seldaek
395 Seldaek $this->assertEquals('', $this->dwoo->get($tpl, array('sub'=>array('foo','bar')), $this->compiler));
396 Seldaek }
397 Seldaek
398 Seldaek public function testForeachWithGlobalVarsFollowing()
399 Seldaek {
400 9cf6ad Seldaek $tpl = new Dwoo_Template_String('{foreach $sub key item foo}{/foreach}{$dwoo.foreach.foo.total}');
401 dc33f4 Seldaek $tpl->forceCompilation();
402 Seldaek
403 Seldaek $this->assertEquals('2', $this->dwoo->get($tpl, array('sub'=>array('foo','bar')), $this->compiler));
404 Seldaek }
405 Seldaek
406 9cf6ad Seldaek public function testForeachDwoo_Alt()
407 dc33f4 Seldaek {
408 9cf6ad Seldaek $tpl = new Dwoo_Template_String('{foreach $sub key item}{$key}.{$item}{/foreach}');
409 dc33f4 Seldaek $tpl->forceCompilation();
410 Seldaek
411 Seldaek $this->assertEquals('0.foo1.bar', $this->dwoo->get($tpl, array('sub'=>array('foo','bar')), $this->compiler));
412 Seldaek }
413 Seldaek
414 Seldaek public function testForeachElseEmpty()
415 Seldaek {
416 9cf6ad Seldaek $tpl = new Dwoo_Template_String('{foreach from=$sub key=key item=item}{$key}.{$item}{foreachelse}bar{/foreach}');
417 dc33f4 Seldaek $tpl->forceCompilation();
418 Seldaek
419 Seldaek $this->assertEquals('bar', $this->dwoo->get($tpl, array('sub'=>array()), $this->compiler));
420 Seldaek
421 Seldaek // fixes the init call not being called (which is normal)
422 9cf6ad Seldaek $fixCall = new Dwoo_Plugin_foreachelse($this->dwoo);
423 dc33f4 Seldaek $fixCall->init('');
424 Seldaek }
425 Seldaek
426 Seldaek public function testForeachElseUnset()
427 Seldaek {
428 9cf6ad Seldaek $tpl = new Dwoo_Template_String('{foreach from=$sub key=key item=item}{$key}.{$item}{foreachelse}bar{/foreach}');
429 dc33f4 Seldaek $tpl->forceCompilation();
430 Seldaek
431 Seldaek $this->assertEquals('bar', $this->dwoo->get($tpl, array(), $this->compiler));
432 Seldaek }
433 Seldaek
434 Seldaek public function testLoop()
435 Seldaek {
436 9cf6ad Seldaek $tpl = new Dwoo_Template_String('{loop $foo}{$.loop.default.index}>{$0}/{$1}{/loop}');
437 dc33f4 Seldaek $tpl->forceCompilation();
438 Seldaek
439 Seldaek $this->assertEquals('0>a/b1>c/d', $this->dwoo->get($tpl, array('foo'=>array(array('a','b'), array('c','d'))) , $this->compiler));
440 Seldaek
441 Seldaek // fixes the init call not being called (which is normal)
442 9cf6ad Seldaek $fixCall = new Dwoo_Plugin_loop($this->dwoo);
443 dc33f4 Seldaek $fixCall->init('');
444 Seldaek }
445 Seldaek
446 Seldaek public function testLoopElse()
447 Seldaek {
448 9cf6ad Seldaek $tpl = new Dwoo_Template_String('{loop $foo}{$.loop.default.index}>{$0}/{$1}{else}MOO{/loop}');
449 dc33f4 Seldaek $tpl->forceCompilation();
450 Seldaek
451 Seldaek $this->assertEquals('MOO', $this->dwoo->get($tpl, array() , $this->compiler));
452 Seldaek
453 Seldaek // fixes the init call not being called (which is normal)
454 9cf6ad Seldaek $fixCall = new Dwoo_Plugin_loop($this->dwoo);
455 dc33f4 Seldaek $fixCall->init('');
456 Seldaek }
457 Seldaek
458 05ec46 seldaek public function testLoopVars()
459 seldaek {
460 seldaek $tpl = new Dwoo_Template_String('{loop $foo name=i}{$.loop.i.index}|{$.loop.i.iteration}|{$.loop.i.first}|{$.loop.i.last}|{$.loop.i.show}|{$.loop.i.total}||{/}');
461 seldaek $tpl->forceCompilation();
462 seldaek $this->assertEquals('0|1|1||1|4||'.'1|2|||1|4||'.'2|3|||1|4||'.'3|4||1|1|4||', $this->dwoo->get($tpl, array('foo'=>array('a','b','c','d')), $this->compiler));
463 seldaek }
464 seldaek
465 c6133a Seldaek public function testStrip()
466 Seldaek {
467 Seldaek $tpl = new Dwoo_Template_String("{strip}a\nb\nc{/strip}a\nb\nc");
468 Seldaek $tpl->forceCompilation();
469 Seldaek $this->assertEquals("abca\nb\nc", $this->dwoo->get($tpl, array(), $this->compiler));
470 05ec46 seldaek
471 seldaek // fixes the init call not being called (which is normal)
472 seldaek $fixCall = new Dwoo_Plugin_strip($this->dwoo);
473 seldaek $fixCall->init('');
474 seldaek }
475 seldaek
476 seldaek public function testStripJavascript()
477 seldaek {
478 seldaek $tpl = new Dwoo_Template_String("{strip js}function() { // does bleh
479 seldaek bleh();
480 seldaek /* block comment
481 seldaek
482 seldaek */
483 seldaek }
484 seldaek {/strip}");
485 seldaek $tpl->forceCompilation();
486 seldaek $this->assertEquals("function() {bleh();}", $this->dwoo->get($tpl, array(), $this->compiler));
487 c6133a Seldaek }
488 Seldaek
489 77501e Seldaek public function testStripWithPhp()
490 c6133a Seldaek {
491 77501e Seldaek $tpl = new Dwoo_Template_String("{strip}a\nb{\$foo=\"\\n\"}{if \$foo}>{\$foo}<{/if}\nc{/strip}a\nb\nc");
492 c6133a Seldaek $tpl->forceCompilation();
493 77501e Seldaek $this->assertEquals("ab>\n<ca\nb\nc", $this->dwoo->get($tpl, array(), $this->compiler));
494 c6133a Seldaek }
495 Seldaek
496 05ec46 seldaek public function testSubTemplates()
497 seldaek {
498 ef0858 Seldaek $tpl = new Dwoo_Template_String('{load_templates "file:'.TEST_DIRECTORY.'/resources/templates.html"}{menu $menu}{noparam}{load_templates ""}');
499 05ec46 seldaek $tpl->forceCompilation();
500 ef0858 Seldaek $this->assertEquals('<ul class="level0"><li>foo</li><li>bar</li><ul class="level1"><li>baz</li><li>qux</li></ul><li>boo</li><ul class="level1"><li>far</li><ul class="level2"><li>faz</li><li>mux</li></ul></ul><li>duck</li></ul>noparamoutput'."\n",
501 05ec46 seldaek $this->dwoo->get($tpl, array('menu'=>array('foo', 'bar'=>array('baz','qux'), 'boo'=>array('far'=>array('faz','mux')), 'duck')), $this->compiler));
502 seldaek
503 seldaek // fixes the init call not being called (which is normal)
504 seldaek $fixCall = new Dwoo_Plugin_template($this->dwoo);
505 seldaek $fixCall->init('');
506 seldaek }
507 seldaek
508 e756cf seldaek public function testSubTemplatesWithAutoEscape()
509 seldaek {
510 seldaek $tpl = new Dwoo_Template_String('{load_templates "file:'.TEST_DIRECTORY.'/resources/templates.html"}{menu $menu}{noparam}{load_templates ""}');
511 seldaek $tpl->forceCompilation();
512 seldaek $this->compiler->setAutoEscape(true);
513 seldaek $this->assertEquals('<ul class="level0"><li>foo</li><li>bar</li><ul class="level1"><li>baz</li><li>qux</li></ul><li>boo</li><ul class="level1"><li>far</li><ul class="level2"><li>faz</li><li>mux</li></ul></ul><li>duck</li></ul>noparamoutput'."\n",
514 seldaek $this->dwoo->get($tpl, array('menu'=>array('foo', 'bar'=>array('baz','qux'), 'boo'=>array('far'=>array('faz','mux')), 'duck')), $this->compiler));
515 seldaek $this->compiler->setAutoEscape(false);
516 seldaek
517 seldaek // fixes the init call not being called (which is normal)
518 seldaek $fixCall = new Dwoo_Plugin_template($this->dwoo);
519 seldaek $fixCall->init('');
520 seldaek }
521 seldaek
522 ef0858 Seldaek public function testSubTemplatesMultiInc()
523 Seldaek {
524 Seldaek $tpl = new Dwoo_Template_File(TEST_DIRECTORY.'/resources/templateUsage.html');
525 Seldaek $tpl->forceCompilation();
526 Seldaek $this->assertEquals("\n".'noparamoutput'."\n", $this->dwoo->get($tpl, array(), $this->compiler));
527 Seldaek $this->assertEquals("\n".'noparamoutput'."\n", $this->dwoo->get($tpl, array(), $this->compiler));
528 Seldaek }
529 Seldaek
530 dc33f4 Seldaek public function testTextFormat()
531 Seldaek {
532 9cf6ad Seldaek $tpl = new Dwoo_Template_String('aa{textformat wrap=10 wrap_char="\n"}hello world is so unoriginal but hey.. {textformat wrap=4 wrap_char="\n"}a a a a a a{/textformat}it works.{/textformat}bb');
533 dc33f4 Seldaek $tpl->forceCompilation();
534 Seldaek
535 Seldaek $this->assertEquals('aahello
536 Seldaek world is
537 Seldaek so
538 Seldaek unoriginal
539 Seldaek but hey..
540 Seldaek a a
541 Seldaek
542 Seldaek a a
543 Seldaek
544 Seldaek a ait
545 Seldaek works.bb', $this->dwoo->get($tpl, array(), $this->compiler));
546 Seldaek
547 9cf6ad Seldaek $tpl = new Dwoo_Template_String('{textformat style=email indent=50 wrap_char="\n"}hello world is so unoriginal but hey.. it works.{/textformat}');
548 dc33f4 Seldaek $tpl->forceCompilation();
549 Seldaek
550 Seldaek $this->assertEquals(' hello world is so
551 937188 Seldaek unoriginal but hey..
552 Seldaek it works.', $this->dwoo->get($tpl, array(), $this->compiler));
553 dc33f4 Seldaek
554 9cf6ad Seldaek $tpl = new Dwoo_Template_String('{textformat style=email indent=50 assign=foo wrap_char="\n"}hello world is so unoriginal but hey.. it works.{/textformat}-{$foo}');
555 dc33f4 Seldaek $tpl->forceCompilation();
556 Seldaek
557 Seldaek $this->assertEquals('- hello world is so
558 937188 Seldaek unoriginal but hey..
559 Seldaek it works.', $this->dwoo->get($tpl, array(), $this->compiler));
560 dc33f4 Seldaek
561 9cf6ad Seldaek $tpl = new Dwoo_Template_String('{textformat style=html wrap=10 wrap_char="\n"}hello world{/textformat}');
562 dc33f4 Seldaek $tpl->forceCompilation();
563 Seldaek
564 Seldaek $this->assertEquals('hello<br />world', $this->dwoo->get($tpl, array(), $this->compiler));
565 Seldaek }
566 Seldaek
567 Seldaek public function testWith()
568 Seldaek {
569 9cf6ad Seldaek $tpl = new Dwoo_Template_String('{with $foo}{$a}{/with}-{if $a}FAIL{/if}-{with $foo.b}mlsk{/with}');
570 dc33f4 Seldaek $tpl->forceCompilation();
571 Seldaek
572 Seldaek $this->assertEquals('bar--', $this->dwoo->get($tpl, array('foo'=>array('a'=>'bar')), $this->compiler));
573 Seldaek
574 9cf6ad Seldaek $tpl = new Dwoo_Template_String('{with $foo}{$a.0}{with $a}{$0}{/with}{with $b}B{else}NOB{/with}{/with}-{if $a}FAIL{/if}-{with $foo.b}mlsk{/with}{with $fooo}a{withelse}b{/with}');
575 dc33f4 Seldaek $tpl->forceCompilation();
576 Seldaek
577 Seldaek $this->assertEquals('barbarNOB--b', $this->dwoo->get($tpl, array('foo'=>array('a'=>array('bar'))), $this->compiler));
578 Seldaek
579 Seldaek // fixes the init call not being called (which is normal)
580 9cf6ad Seldaek $fixCall = new Dwoo_Plugin_with($this->dwoo);
581 dc33f4 Seldaek $fixCall->init('');
582 9cf6ad Seldaek $fixCall = new Dwoo_Plugin_withelse($this->dwoo);
583 dc33f4 Seldaek $fixCall->init('');
584 Seldaek }
585 Seldaek }