File: test_template.py

package info (click to toggle)
cubictemp 2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, squeeze, wheezy
  • size: 264 kB
  • ctags: 139
  • sloc: python: 682; makefile: 4
file content (153 lines) | stat: -rw-r--r-- 4,025 bytes parent folder | download
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/usr/local/bin/python
import glob, sys, os.path
import libpry
import cubictemp

class uBasic(libpry.AutoTree):
    def test_simple(self):
        test = "@!1!@"
        ct = cubictemp.Template(test)
        assert str(ct) == "1"

    def test_simple2(self):
        test = "@!a!@"
        ns = {"a": 1}
        ct = cubictemp.Template(test, **ns)
        assert str(ct) == "1"


class TemplateTester(libpry.AutoTree):
    def _runTemp(self, filename, nsDict):
        return str(cubictemp.File(filename + ".test", **nsDict))


class uTemplate(TemplateTester):
    PREFIX = "ptests"
    def _runTemp(self, filename, nsDict={}):
        filename = os.path.join(self.PREFIX, filename)
        mtemp = TemplateTester._runTemp(self, filename, nsDict)
        out = open(filename + ".out", "r").read().split()
        mout = mtemp.split()
        if not mout == out:
            print "Expected output:"
            print "-------"
            print out
            print "-------"
            print "Actual output:"
            print "-------"
            print mout
            print "-------"
            self.fail("Actual output does not match expected output")

    def test_if(self):
        self._runTemp("if")

    def test_block(self):
        ns = {
            "bar": "rab",
        }
        self._runTemp("block", ns)

    def test_commonuse(self):
        ns = {
             "foo": open("ptests/commonuse.data")
        }
        self._runTemp("commonuse", ns)

    def test_nested(self):
        self._runTemp("nested")

    def test_repeat(self):
        self._runTemp("repeat")

    def test_simple(self):
        ns = {
            "foo": "one",
            "bar": "two",
            "boo": "three",
            "mdict": {"entry": "foo"}
        }
        self._runTemp("simple", ns)


class uErrors(TemplateTester):
    PREFIX = "ntests"
    def _runTemp(self, filename, err):
        filename = os.path.join(self.PREFIX, filename)
        libpry.raises(err, TemplateTester._runTemp, self, filename, {})
        
    def test_blockLine(self):
        self._runTemp("blockLine", "'a' is not defined")

    def test_ifundefined(self):
        self._runTemp("ifundefined", "'a' is not defined")

    def test_loopsyntax(self):
        self._runTemp("loopsyntax", "invalid expression")

    def test_loopundefined(self):
        self._runTemp("loopundefined", "'a' is not defined")

    def test_noniterable(self):
        self._runTemp("noniterable", "can not iterate")

    def test_syntax(self):
        self._runTemp("syntax", "invalid expression")

    def test_undefined(self):
        self._runTemp("undefined", "'foo' is not defined")


class uQuoting(TemplateTester):
    PREFIX = "qtests"
    def _runTemp(self, filename, nsDict={}):
        filename = os.path.join(self.PREFIX, filename)
        output = TemplateTester._runTemp(self, filename, nsDict)
        if (output.find("<") >= 0): self.fail()
        if (output.find(">") >= 0): self.fail()
        
    def test_commonuse(self):
        class Dummy:
            def __repr__(self):
                return "<foo>"

        mdict = {
            "foo":      "<foinkle>",
            "object":   Dummy()
        }
        self._runTemp("commonuse", mdict)


class uNonQuoting(TemplateTester):
    PREFIX = "nqtests"
    def _runTemp(self, filename, nsDict):
        filename = os.path.join(self.PREFIX, filename)
        output = TemplateTester._runTemp(self, filename, nsDict)
        if (output.find("<") < 0): self.fail()
        if (output.find(">") < 0): self.fail()
        
    def test_commonuse(self):
        class Dummy:
            _cubictemp_unescaped = 1
            def __repr__(self):
                return "<foo>"

        mdict = {
            "object":   Dummy()
        }
        self._runTemp("object", mdict)

    def test_tag(self):
        mdict = {
            "tag":   "<foo>"
        }
        self._runTemp("tag", mdict)
 

tests = [
    uBasic(),
    uTemplate(),
    uErrors(),
    uQuoting(),
    uNonQuoting(),
]