diff options
| author | David Siegfried <david.siegfried@uni-vechta.de> | 2024-05-08 07:38:22 +0000 |
|---|---|---|
| committer | Jan-Hendrik Willms <tleilax+studip@gmail.com> | 2024-05-08 07:38:22 +0000 |
| commit | 15c29abc3dc12e40c40a9331f6b9478386d2bf7d (patch) | |
| tree | 66f3723eb6ca406119e1112f2fefade6bf338a53 /tests/unit | |
| parent | 2c4c41a7733923cbd0f9f93eb7c90ad412efda1f (diff) | |
move exTpl to lib, re #4119
Merge request studip/studip!2963
Diffstat (limited to 'tests/unit')
| -rw-r--r-- | tests/unit/_bootstrap.php | 1 | ||||
| -rw-r--r-- | tests/unit/lib/classes/extTPLTemplateTest.php | 213 |
2 files changed, 214 insertions, 0 deletions
diff --git a/tests/unit/_bootstrap.php b/tests/unit/_bootstrap.php index e5299a6..3aa1144 100644 --- a/tests/unit/_bootstrap.php +++ b/tests/unit/_bootstrap.php @@ -53,6 +53,7 @@ StudipAutoloader::addAutoloadPath('lib/activities', 'Studip\\Activity'); StudipAutoloader::addAutoloadPath('lib/models'); StudipAutoloader::addAutoloadPath('lib/classes'); StudipAutoloader::addAutoloadPath('lib/classes', 'Studip'); +StudipAutoloader::addAutoloadPath('lib/exTpl', 'exTpl'); StudipAutoloader::addAutoloadPath('lib/exceptions'); StudipAutoloader::addAutoloadPath('lib/classes/sidebar'); StudipAutoloader::addAutoloadPath('lib/classes/helpbar'); diff --git a/tests/unit/lib/classes/extTPLTemplateTest.php b/tests/unit/lib/classes/extTPLTemplateTest.php new file mode 100644 index 0000000..43a9629 --- /dev/null +++ b/tests/unit/lib/classes/extTPLTemplateTest.php @@ -0,0 +1,213 @@ +<?php +/* + * template_test.php - expression template unit tests + * + * Copyright (c) 2013 Elmar Ludwig + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of + * the License, or (at your option) any later version. + */ + +use exTpl\Template; + +class extTplTemplateTest extends \Codeception\Test\Unit +{ + public function testSimpleString() + { + $bindings = []; + $template = 'The quick brown fox jumps over the layz dog.'; + $expected = $template; + $tmpl_obj = new Template($template); + + $this->assertEquals($expected, $tmpl_obj->render($bindings)); + } + + public function testConstantExpression() + { + $bindings = array(); + $template = '17 + 4 = {"foo" != "bar" ? 17 + 4 : 42.0}'; + $expected = '17 + 4 = 21'; + $tmpl_obj = new Template($template); + + $this->assertEquals($expected, $tmpl_obj->render($bindings)); + } + + public function testConditionExpression() + { + $bindings = array('a' => 0, 'b' => 42); + $template = 'answer is {"" ?: a ?: b}'; + $expected = 'answer is 42'; + $tmpl_obj = new Template($template); + + $this->assertEquals($expected, $tmpl_obj->render($bindings)); + } + + public function testStringEscapes() + { + $bindings = array(); + $template = '"{"\\tfoo\'\\"\\n"}{\'{"bar"}\'}"'; + $expected = "\"\tfoo'\"\n{\"bar\"}\""; + $tmpl_obj = new Template($template); + + $this->assertEquals($expected, $tmpl_obj->render($bindings)); + } + + public function testOperatorPrecedence() + { + $bindings = array('val' => array(array(42))); + $template = '{-val[0][0] / (17+4) + 8 > 6 && "foo" == "f"~"o"~"o" ? 1 : 2}'; + $expected = '2'; + $tmpl_obj = new Template($template); + + $this->assertEquals($expected, $tmpl_obj->render($bindings)); + } + + public function testSimpleBindings() + { + $bindings = array('foo' => 'bar', 'val' => array(17, 4), 'pi' => 3.14159); + $template = 'foo = "{foo}", sum = {val[0] + val[1]}, pi^2 = {pi * pi}, x = {x}'; + $expected = 'foo = "bar", sum = 21, pi^2 = 9.8695877281, x = '; + $tmpl_obj = new Template($template); + + $this->assertEquals($expected, $tmpl_obj->render($bindings)); + } + + public function testConditional() + { + $bindings = array('foo' => 'bar', 'pi' => 3.14159); + $template = '{if foo == "foo"}NO{elseif foo == "bar"}pi = {pi}{else}NO{endif}'; + $expected = 'pi = 3.14159'; + $tmpl_obj = new Template($template); + + $this->assertEquals($expected, $tmpl_obj->render($bindings)); + } + + public function testConditionalIteration() + { + $bindings = array('foo' => 'bar', 'pi' => 3.14159); + $template = '{foreach foo}{if foo}{foo}{endif}{endforeach}'; + $expected = 'bar'; + $tmpl_obj = new Template($template); + + $this->assertEquals($expected, $tmpl_obj->render($bindings)); + } + + public function testIteration() + { + $bindings = array('persons' => array( + 1 => array('user' => 'jane', 'phone' => '555-81281'), + 2 => array('user' => 'mike', 'phone' => '230-28382'), + 3 => array('user' => 'john', 'phone' => '911-19212') + )); + $template = '<ul>{foreach persons as person}<li>{index~":"~person.user~":"~phone}</li>{endforeach}</ul>'; + $expected = '<ul>' . + '<li>1:jane:555-81281</li>' . + '<li>2:mike:230-28382</li>' . + '<li>3:john:911-19212</li>' . + '</ul>'; + $tmpl_obj = new Template($template); + + $this->assertEquals($expected, $tmpl_obj->render($bindings)); + } + + public function testEmptyIteration() + { + $bindings = array('foo' => array(), 'bar' => false); + $template = '{foreach foo}foo{endforeach}:{foreach bar}bar{endforeach}'; + $expected = ':'; + $tmpl_obj = new Template($template); + + $this->assertEquals($expected, $tmpl_obj->render($bindings)); + } + + public function testVariableScope() + { + $bindings = array('value' => 42, 'test' => array( + array(), + array('value' => 17), + array('test' => array( + array(), + array('value' => 4) + )) + )); + $template = '{foreach test}{value}:{foreach test}{value}~{endforeach}{endforeach}'; + $expected = '42:42~17~42~17:17~17~17~42:42~4~'; + $tmpl_obj = new Template($template); + + $this->assertEquals($expected, $tmpl_obj->render($bindings)); + } + + public function testNestedStatements() + { + foreach (range(0, 9) as $i) { + $bindings['loop'][$i]['i'] = "$i"; + } + $template = '{foreach loop}' . + '{if i+1>4 && i<(1+10/2)}{i==4*1 ? \'foo\'~i : "bar"}' . + '{elseif !(i<=+4)}+{elseif i==""}..{else}{"-"}{endif}' . + '{endforeach}'; + $expected = '----foo4bar++++'; + $tmpl_obj = new Template($template); + + $this->assertEquals($expected, $tmpl_obj->render($bindings)); + } + + public function testFunctionCall() + { + $bindings = array('val' => array(0, 1, 2, 3)); + $template = '{strlen("foobar") + count(val)}'; + $expected = '10'; + $tmpl_obj = new Template($template); + + $this->assertEquals($expected, $tmpl_obj->render($bindings)); + } + + public function testFilters() + { + $bindings = array( + 'pi' => 3.14159, + 'format' => function($a, $b) { return number_format($a, $b); }, + 'upper' => function($a) { return strtoupper($a); } + ); + $template = '{pi|format(3) ~ ":" ~ "foobar"|upper}'; + $expected = '3.142:FOOBAR'; + $tmpl_obj = new Template($template); + + $this->assertEquals($expected, $tmpl_obj->render($bindings)); + } + + public function testRawFilter() + { + $bindings = array('foo' => '<img>', 'upper' => function($a) { return strtoupper($a); }); + $template = '{foo}:{foo|upper|raw}'; + $expected = '<img>:<IMG>'; + $tmpl_obj = new Template($template); + $tmpl_obj->autoescape('html'); + + $this->assertEquals($expected, $tmpl_obj->render($bindings)); + } + + public function testHtmlAutoEscape() + { + $bindings = array('foo' => '<img>', 'pi' => 3.14159); + $template = '{foo}:{pi}'; + $expected = '<img>:3.14159'; + $tmpl_obj = new Template($template); + $tmpl_obj->autoescape('html'); + + $this->assertEquals($expected, $tmpl_obj->render($bindings)); + } + + public function testJsonAutoEscape() + { + $bindings = array('foo' => '<img>', 'pi' => 3.14159); + $template = '{foo}:{pi}'; + $expected = '"<img>":3.14159'; + $tmpl_obj = new Template($template); + $tmpl_obj->autoescape('json'); + + $this->assertEquals($expected, $tmpl_obj->render($bindings)); + } +} |
