diff options
author | Kevin Chabowski <kevin@kch42.de> | 2013-10-25 21:44:02 +0200 |
---|---|---|
committer | Kevin Chabowski <kevin@kch42.de> | 2013-10-25 21:44:02 +0200 |
commit | dd03af69265ad686ae8daff6ecbd9df763e6d19f (patch) | |
tree | a86dae876afae2dcea9a3d70e3b513e305b38415 /tests | |
parent | 91b965181f907b169aeeff63a6c15f9b8df9d9a9 (diff) | |
parent | eebf5cb885f266104333ac145355ef6e2599e5f6 (diff) | |
download | ste-dd03af69265ad686ae8daff6ecbd9df763e6d19f.tar.gz ste-dd03af69265ad686ae8daff6ecbd9df763e6d19f.tar.bz2 ste-dd03af69265ad686ae8daff6ecbd9df763e6d19f.zip |
Merge branch 'parser2'
STE gets a new, more robust parser. The previous parser was very fragile
and relied on some ugly regular expressions.
Other advantages of the new parser:
* No precompile phase to implement short tags (?{}, ~{}), that means
short tags can now be nested in any order.
* The parses now uses the mb_* functions, so it should handle non-ascii
text correctly.
There were also some improvements to the compiler:
* Various small bugfixes.
* The compiler accepts and compiles tag parameters that have tags in
them. The parser still doesn't accept this (and probably never will),
this is mainly used to allow arbitrary code inside of short cmp tags
(~{..}).
There are now (finally!) tests for the language to maintain a certain
quality and consistency (which STE lacked in the past, to be honest).
And finally the code was reformatted. Now 1TBS is used instead of
Allman.
Diffstat (limited to 'tests')
37 files changed, 206 insertions, 0 deletions
diff --git a/tests/dump_ast.php b/tests/dump_ast.php new file mode 100644 index 0000000..418fbc8 --- /dev/null +++ b/tests/dump_ast.php @@ -0,0 +1,5 @@ +<?php + +require(dirname(__FILE__) . "/../stupid_template_engine.php"); + +var_dump(\ste\Parser::parse(file_get_contents("php://stdin"), "-")); diff --git a/tests/mktest.sh b/tests/mktest.sh new file mode 100755 index 0000000..f249a7f --- /dev/null +++ b/tests/mktest.sh @@ -0,0 +1,15 @@ +#!/bin/sh + +mkdir "$1" +touch "$1/test.tpl" +touch "$1/want" + +echo '<?php + +function test_func($ste) { + +}' > "$1/code.php" + +echo 'have +*.ast +*.transc.php' > "$1/.gitignore" diff --git a/tests/run_all.sh b/tests/run_all.sh new file mode 100755 index 0000000..5727afb --- /dev/null +++ b/tests/run_all.sh @@ -0,0 +1,21 @@ +#!/bin/sh + +r=0 +for t in test_*; do + cd $t + php ../test.php > have + echo -ne "\e[1m$t\e[0m: " + if sed 's/\s*//' < have | grep -v '^$' | cmp -s want; then + echo "OK" + rm *.transc.php + else + echo "FAILED" + for tpl in *.tpl; do + php ../dump_ast.php < $tpl > $tpl.ast + done + r=1 + fi + cd .. +done + +exit $r diff --git a/tests/test.php b/tests/test.php new file mode 100644 index 0000000..41713dc --- /dev/null +++ b/tests/test.php @@ -0,0 +1,23 @@ +<?php + +require(dirname(__FILE__) . "/../stupid_template_engine.php"); +require("code.php"); + +class TestStorage implements \ste\StorageAccess { + public function load($tpl, &$mode) { + $mode = \ste\MODE_SOURCE; + return file_get_contents($tpl); + } + + public function save($tpl, $data, $mode) { + if($mode != \ste\MODE_TRANSCOMPILED) { + return; + } + + file_put_contents("$tpl.transc.php", $data); + } +} + +$ste = new \ste\STECore(new TestStorage()); +test_func($ste); +echo $ste->exectemplate("test.tpl"); diff --git a/tests/test_array/.gitignore b/tests/test_array/.gitignore new file mode 100644 index 0000000..de2a41b --- /dev/null +++ b/tests/test_array/.gitignore @@ -0,0 +1,3 @@ +have +*.ast +*.transc.php diff --git a/tests/test_array/code.php b/tests/test_array/code.php new file mode 100644 index 0000000..58601e2 --- /dev/null +++ b/tests/test_array/code.php @@ -0,0 +1,13 @@ +<?php + +function test_func($ste) { + $ste->vars["foo"] = array( + "a" => array( + "blabla" => "OK" + ), + "b" => "bla" + ); + $ste->vars["bar"] = array( + "baz" => "a" + ); +} diff --git a/tests/test_array/test.tpl b/tests/test_array/test.tpl new file mode 100644 index 0000000..050045d --- /dev/null +++ b/tests/test_array/test.tpl @@ -0,0 +1 @@ +${foo[$bar[baz]][${foo[b]}bla]}
\ No newline at end of file diff --git a/tests/test_array/want b/tests/test_array/want new file mode 100644 index 0000000..d86bac9 --- /dev/null +++ b/tests/test_array/want @@ -0,0 +1 @@ +OK diff --git a/tests/test_blocks/.gitignore b/tests/test_blocks/.gitignore new file mode 100644 index 0000000..de2a41b --- /dev/null +++ b/tests/test_blocks/.gitignore @@ -0,0 +1,3 @@ +have +*.ast +*.transc.php diff --git a/tests/test_blocks/code.php b/tests/test_blocks/code.php new file mode 100644 index 0000000..de8c553 --- /dev/null +++ b/tests/test_blocks/code.php @@ -0,0 +1,5 @@ +<?php + +function test_func($ste) { + +} diff --git a/tests/test_blocks/master.tpl b/tests/test_blocks/master.tpl new file mode 100644 index 0000000..a9608bd --- /dev/null +++ b/tests/test_blocks/master.tpl @@ -0,0 +1,3 @@ +Foo +<ste:block name="blk1">Bar</ste:block> +<ste:block name="blk2">Baz</ste:block>
\ No newline at end of file diff --git a/tests/test_blocks/test.tpl b/tests/test_blocks/test.tpl new file mode 100644 index 0000000..44c6891 --- /dev/null +++ b/tests/test_blocks/test.tpl @@ -0,0 +1,2 @@ +<ste:load name="master.tpl" /> +<ste:block name="blk1">Replaced</ste:block>
\ No newline at end of file diff --git a/tests/test_blocks/want b/tests/test_blocks/want new file mode 100644 index 0000000..640cfef --- /dev/null +++ b/tests/test_blocks/want @@ -0,0 +1,3 @@ +Foo +Replaced +Baz diff --git a/tests/test_escapes/.gitignore b/tests/test_escapes/.gitignore new file mode 100644 index 0000000..de2a41b --- /dev/null +++ b/tests/test_escapes/.gitignore @@ -0,0 +1,3 @@ +have +*.ast +*.transc.php diff --git a/tests/test_escapes/code.php b/tests/test_escapes/code.php new file mode 100644 index 0000000..05984d1 --- /dev/null +++ b/tests/test_escapes/code.php @@ -0,0 +1,7 @@ +<?php + +function test_func($ste) { + $ste->register_tag("my_echo", function($ste, $params, $sub) { + return $params["text"]; + }); +} diff --git a/tests/test_escapes/test.tpl b/tests/test_escapes/test.tpl new file mode 100644 index 0000000..5458fde --- /dev/null +++ b/tests/test_escapes/test.tpl @@ -0,0 +1,7 @@ +\\\\\\\$foo +\${bar} +\?{foo|bar|baz} +?{|Foo|Bar\|Baz} +<ste:my_echo text="foo\\\"bar\$" /> +<ste:my_echo text='\'' /> +\'
\ No newline at end of file diff --git a/tests/test_escapes/want b/tests/test_escapes/want new file mode 100644 index 0000000..3278c4d --- /dev/null +++ b/tests/test_escapes/want @@ -0,0 +1,7 @@ +\\\$foo +${bar} +?{foo|bar|baz} +Bar|Baz +foo\"bar$ +' +\' diff --git a/tests/test_loop/.gitignore b/tests/test_loop/.gitignore new file mode 100644 index 0000000..de2a41b --- /dev/null +++ b/tests/test_loop/.gitignore @@ -0,0 +1,3 @@ +have +*.ast +*.transc.php diff --git a/tests/test_loop/code.php b/tests/test_loop/code.php new file mode 100644 index 0000000..de8c553 --- /dev/null +++ b/tests/test_loop/code.php @@ -0,0 +1,5 @@ +<?php + +function test_func($ste) { + +} diff --git a/tests/test_loop/test.tpl b/tests/test_loop/test.tpl new file mode 100644 index 0000000..399f05a --- /dev/null +++ b/tests/test_loop/test.tpl @@ -0,0 +1,3 @@ +<ste:for start="10" stop="0" step="-1" counter="i"> +$i +</ste:for>
\ No newline at end of file diff --git a/tests/test_loop/want b/tests/test_loop/want new file mode 100644 index 0000000..30b148d --- /dev/null +++ b/tests/test_loop/want @@ -0,0 +1,11 @@ +10 +9 +8 +7 +6 +5 +4 +3 +2 +1 +0 diff --git a/tests/test_mktag/.gitignore b/tests/test_mktag/.gitignore new file mode 100644 index 0000000..de2a41b --- /dev/null +++ b/tests/test_mktag/.gitignore @@ -0,0 +1,3 @@ +have +*.ast +*.transc.php diff --git a/tests/test_mktag/code.php b/tests/test_mktag/code.php new file mode 100644 index 0000000..de8c553 --- /dev/null +++ b/tests/test_mktag/code.php @@ -0,0 +1,5 @@ +<?php + +function test_func($ste) { + +} diff --git a/tests/test_mktag/test.tpl b/tests/test_mktag/test.tpl new file mode 100644 index 0000000..c381ef4 --- /dev/null +++ b/tests/test_mktag/test.tpl @@ -0,0 +1,12 @@ +<ste:mktag name="double"><ste:calc>2 * <ste:tagcontent /></ste:calc></ste:mktag> +<ste:mktag name="foo" mandatory="a|b"> + <ste:set var="b">$_tag_parameters[b]</ste:set><ste:comment>Ugly hack, since STE does no scoping...</ste:comment> + <ste:for counter="i" start="0" stop="$_tag_parameters[a]"> + <ste:if> + <ste:even>$i</ste:even> + <ste:then><ste:double>$i</ste:double></ste:then> + <ste:else>$b</ste:else> + </ste:if> + </ste:for> +</ste:mktag> +<ste:foo a="10" b="-" />
\ No newline at end of file diff --git a/tests/test_mktag/want b/tests/test_mktag/want new file mode 100644 index 0000000..3e2351c --- /dev/null +++ b/tests/test_mktag/want @@ -0,0 +1,11 @@ +0 +- +4 +- +8 +- +12 +- +16 +- +20 diff --git a/tests/test_pseudotags/.gitignore b/tests/test_pseudotags/.gitignore new file mode 100644 index 0000000..de2a41b --- /dev/null +++ b/tests/test_pseudotags/.gitignore @@ -0,0 +1,3 @@ +have +*.ast +*.transc.php diff --git a/tests/test_pseudotags/code.php b/tests/test_pseudotags/code.php new file mode 100644 index 0000000..de8c553 --- /dev/null +++ b/tests/test_pseudotags/code.php @@ -0,0 +1,5 @@ +<?php + +function test_func($ste) { + +} diff --git a/tests/test_pseudotags/test.tpl b/tests/test_pseudotags/test.tpl new file mode 100644 index 0000000..756be8a --- /dev/null +++ b/tests/test_pseudotags/test.tpl @@ -0,0 +1,2 @@ +<ste:comment><ste:comment><ste:foo>Ignore $me<ste:rawtext>Foo</ste:rawtext></ste:comment> +<ste:rawtext><ste:rawtext>$foo bar <ste:bla a="$b" /></ste:rawtext>
\ No newline at end of file diff --git a/tests/test_pseudotags/want b/tests/test_pseudotags/want new file mode 100644 index 0000000..3aaffdc --- /dev/null +++ b/tests/test_pseudotags/want @@ -0,0 +1 @@ +<ste:rawtext>$foo bar <ste:bla a="$b" /> diff --git a/tests/test_short/.gitignore b/tests/test_short/.gitignore new file mode 100644 index 0000000..de2a41b --- /dev/null +++ b/tests/test_short/.gitignore @@ -0,0 +1,3 @@ +have +*.ast +*.transc.php diff --git a/tests/test_short/code.php b/tests/test_short/code.php new file mode 100644 index 0000000..de8c553 --- /dev/null +++ b/tests/test_short/code.php @@ -0,0 +1,5 @@ +<?php + +function test_func($ste) { + +} diff --git a/tests/test_short/test.tpl b/tests/test_short/test.tpl new file mode 100644 index 0000000..42d498b --- /dev/null +++ b/tests/test_short/test.tpl @@ -0,0 +1 @@ +?{~{foo|eq|bar}|FAIL|?{~{~{1|gt|2}|?{y|eq|neq}|~{3|lt|2}}|OK|FAIL}}
\ No newline at end of file diff --git a/tests/test_short/want b/tests/test_short/want new file mode 100644 index 0000000..d86bac9 --- /dev/null +++ b/tests/test_short/want @@ -0,0 +1 @@ +OK diff --git a/tests/test_simple/.gitignore b/tests/test_simple/.gitignore new file mode 100644 index 0000000..de2a41b --- /dev/null +++ b/tests/test_simple/.gitignore @@ -0,0 +1,3 @@ +have +*.ast +*.transc.php diff --git a/tests/test_simple/code.php b/tests/test_simple/code.php new file mode 100644 index 0000000..4c0babc --- /dev/null +++ b/tests/test_simple/code.php @@ -0,0 +1,5 @@ +<?php + +function test_func($ste) { + $ste->vars["foo"] = "World"; +} diff --git a/tests/test_simple/test.tpl b/tests/test_simple/test.tpl new file mode 100644 index 0000000..30a4f22 --- /dev/null +++ b/tests/test_simple/test.tpl @@ -0,0 +1 @@ +Hello $foo!
\ No newline at end of file diff --git a/tests/test_simple/want b/tests/test_simple/want new file mode 100644 index 0000000..980a0d5 --- /dev/null +++ b/tests/test_simple/want @@ -0,0 +1 @@ +Hello World! |