diff options
| author | ianb <devnull@localhost> | 2007-01-31 17:43:01 +0000 |
|---|---|---|
| committer | ianb <devnull@localhost> | 2007-01-31 17:43:01 +0000 |
| commit | 7dd6025cafb48902ffe2cdb0b7493bd0bf27f07a (patch) | |
| tree | 882b19075a75bc28d49bdd8f631d1e28e3ae6906 /tests | |
| parent | 40e7e0a2fd3cb9a0bf4914b0dd6aa3340b02ba3d (diff) | |
| download | paste-7dd6025cafb48902ffe2cdb0b7493bd0bf27f07a.tar.gz | |
Added a templating language
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/conftest.py | 3 | ||||
| -rw-r--r-- | tests/test_doctests.py | 36 | ||||
| -rw-r--r-- | tests/test_template.txt | 66 |
3 files changed, 104 insertions, 1 deletions
diff --git a/tests/conftest.py b/tests/conftest.py index d9113df..e639ec1 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -7,7 +7,7 @@ import pkg_resources pkg_resources.require('Paste') import py - +""" Option = py.test.Config.Option option = py.test.Config.addoptions( "Paste options", @@ -25,3 +25,4 @@ class SetupDirectory(py.test.collect.Directory): warnings.filterwarnings('error') Directory = SetupDirectory +""" diff --git a/tests/test_doctests.py b/tests/test_doctests.py new file mode 100644 index 0000000..46470d2 --- /dev/null +++ b/tests/test_doctests.py @@ -0,0 +1,36 @@ +import doctest +from paste.util.import_string import simple_import +import os + +filenames = [ + 'tests/test_template.txt', + ] + +modules = [ + 'paste.util.template', + ] + +options = doctest.ELLIPSIS|doctest.REPORT_ONLY_FIRST_FAILURE + +def test_doctests(): + for filename in filenames: + filename = os.path.join( + os.path.dirname(os.path.dirname(__file__)), + filename) + yield do_doctest, filename + +def do_doctest(filename): + failure, total = doctest.testfile( + filename, module_relative=False, + optionflags=options) + assert not failure, "Failure in %r" % filename + +def test_doctest_mods(): + for module in modules: + yield do_doctest_mod, module + +def do_doctest_mod(module): + module = simple_import(module) + failure, total = doctest.testmod( + module, optionflags=options) + assert not failure, "Failure in %r" % module diff --git a/tests/test_template.txt b/tests/test_template.txt new file mode 100644 index 0000000..a5dad58 --- /dev/null +++ b/tests/test_template.txt @@ -0,0 +1,66 @@ +The templating language is fairly simple, just {{stuff}}. For +example:: + + >>> from paste.util.template import Template, sub + >>> sub('Hi {{name}}', name='Ian') + 'Hi Ian' + >>> Template('Hi {{repr(name)}}').substitute(name='Ian') + "Hi 'Ian'" + >>> Template('Hi {{name+1}}').substitute(name='Ian') + Traceback (most recent call last): + ... + TypeError: cannot concatenate 'str' and 'int' objects at line 1 column 6 + +It also has Django-style piping:: + + >>> sub('Hi {{name|repr}}', name='Ian') + "Hi 'Ian'" + +Note that None shows up as an empty string:: + + >>> sub('Hi {{name}}', name=None) + 'Hi ' + +And if/elif/else:: + + >>> t = Template('{{if x}}{{y}}{{else}}{{z}}{{endif}}') + >>> t.substitute(x=1, y=2, z=3) + '2' + >>> t.substitute(x=0, y=2, z=3) + '3' + >>> t = Template('{{if x > 0}}positive{{elif x < 0}}negative{{else}}zero{{endif}}') + >>> t.substitute(x=1), t.substitute(x=-10), t.substitute(x=0) + ('positive', 'negative', 'zero') + +Plus a for loop:: + + >>> t = Template('{{for i in x}}i={{i}}\n{{endfor}}') + >>> t.substitute(x=range(3)) + 'i=0\ni=1\ni=2\n' + >>> t = Template('{{for a, b in sorted(z.items()):}}{{a}}={{b}},{{endfor}}') + >>> t.substitute(z={1: 2, 3: 4}) + '1=2,3=4,' + >>> t = Template('{{for i in x}}{{if not i}}{{break}}' + ... '{{endif}}{{i}} {{endfor}}') + >>> t.substitute(x=[1, 2, 0, 3, 4]) + '1 2 ' + >>> t = Template('{{for i in x}}{{if not i}}{{continue}}' + ... '{{endif}}{{i}} {{endfor}}') + >>> t.substitute(x=[1, 2, 0, 3, 0, 4]) + '1 2 3 4 ' + +Also Python blocks:: + + >>> sub('{{py:\nx=1\n}}{{x}}') + '1' + +And some syntax errors:: + + >>> t = Template('{{if x}}', name='foo.html') + Traceback (most recent call last): + ... + TemplateError: No {{endif}} at line 1 column 3 in foo.html + >>> t = Template('{{for x}}', name='foo2.html') + Traceback (most recent call last): + ... + TemplateError: Bad for (no "in") in 'x' at line 1 column 3 in foo2.html |
