summaryrefslogtreecommitdiff
path: root/pypers/europython05/Quixote-2.0/ptl/test/utest_ptl.py
blob: 91b96ba4f17761bef6c2d1e7d67205f90094d589 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env python
from sancho.utest import UTest
from quixote.ptl.ptl_compile import compile_template
from cStringIO import StringIO
from quixote.html import TemplateIO, htmltext

def run_ptl(*source):
    """
    Compile the given lines of source code using the ptl compiler
    and run the resulting compiled code.
    """
    # When the ptl compiler compiles a module, it places _q_TemplateIO
    # and _q_htmltext into the globals of the module.  Here, we don't
    # have a module, but we provide these same globals for eval.
    eval(compile_template(StringIO('\n'.join(source)), 'test'),
         dict(_q_TemplateIO=TemplateIO, _q_htmltext=htmltext))

class Test (UTest):

    def check_html(self):
        run_ptl(
            'from quixote.html import htmltext',
            'def f [html] (a):',
            '    "&"',
            '    a',
            'assert type(f(1)) == htmltext',
            'assert f("") == "&"',
            'assert f("&") == "&&"',
            'assert f(htmltext("&")) == "&&"')

    def check_plain(self):
        run_ptl(
            'from quixote.html import htmltext',
            'def f [plain] (a):',
            '    "&"',
            '    a',
            'assert type(f(1)) == str',
            'assert f("") == "&"',
            'assert f("&") == "&&"',
            'assert f(htmltext("&")) == "&&"',
            'assert type(f(htmltext("&"))) == str')

    def check_syntax(self):
        run_ptl('def f(a):\n    a')
        try:
            run_ptl('def f [] (a):\n    a')
            assert 0
        except SyntaxError, e:
            assert e.lineno == 1
        try:
            run_ptl('def f [HTML] (a):\n    a')
            assert 0
        except SyntaxError, e:
            assert e.lineno == 1

if __name__ == "__main__":
    Test()