~ubuntu-branches/ubuntu/quantal/cubictemp/quantal

« back to all changes in this revision

Viewing changes to test/test_template.py

  • Committer: Bazaar Package Importer
  • Author(s): Mohammed Adnène Trojette
  • Date: 2009-03-21 19:56:53 UTC
  • mfrom: (3.1.1 squeeze)
  • Revision ID: james.westby@ubuntu.com-20090321195653-2tj0jz2itpfyxt8y
Tags: 2.0-1
* New upstream release.
* debian/control:
   + bump Standards-Version to 3.8.1.
   + bump debhelper compatibility to 7.
   + add ${misc:Depends} to Depends.
* Remove unneeded patches and patching system accordingly.
* Update debian/copyright: licence is now MIT.
* Update packaging to prevent FTBFS. Thanks to Josselin
  Mouette. (Closes: #516162)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#!/usr/local/bin/python
2
 
 
3
 
import unittest, glob, sys, os.path
 
2
import glob, sys, os.path
 
3
import libpry
4
4
import cubictemp
5
5
 
6
 
class uBasic(unittest.TestCase):
 
6
class uBasic(libpry.AutoTree):
7
7
    def test_simple(self):
8
8
        test = "@!1!@"
9
 
        ct = cubictemp.Temp(test)
10
 
        self.failUnless(repr(ct) == "1")
 
9
        ct = cubictemp.Template(test)
 
10
        assert str(ct) == "1"
11
11
 
12
12
    def test_simple2(self):
13
13
        test = "@!a!@"
14
14
        ns = {"a": 1}
15
 
        ct = cubictemp.Temp(test, **ns)
16
 
        self.failUnless(repr(ct) == "1")
17
 
 
18
 
 
19
 
class TemplateTester(unittest.TestCase):
20
 
    def _run(self, filename):
21
 
        nsDict = {}
22
 
        try:
23
 
            execfile(filename + ".py", {}, nsDict)
24
 
            nsDict = nsDict["mdict"]
25
 
        except IOError:
26
 
            pass
27
 
        return repr(cubictemp.File(filename + ".test", **nsDict))
 
15
        ct = cubictemp.Template(test, **ns)
 
16
        assert str(ct) == "1"
 
17
 
 
18
 
 
19
class TemplateTester(libpry.AutoTree):
 
20
    def _runTemp(self, filename, nsDict):
 
21
        return str(cubictemp.File(filename + ".test", **nsDict))
28
22
 
29
23
 
30
24
class uTemplate(TemplateTester):
31
25
    PREFIX = "ptests"
32
 
    def _run(self, filename):
 
26
    def _runTemp(self, filename, nsDict={}):
33
27
        filename = os.path.join(self.PREFIX, filename)
34
 
        mtemp = TemplateTester._run(self, filename)
 
28
        mtemp = TemplateTester._runTemp(self, filename, nsDict)
35
29
        out = open(filename + ".out", "r").read().split()
36
 
        
37
30
        mout = mtemp.split()
38
31
        if not mout == out:
39
32
            print "Expected output:"
47
40
            self.fail("Actual output does not match expected output")
48
41
 
49
42
    def test_if(self):
50
 
        self._run("if")
 
43
        self._runTemp("if")
51
44
 
52
45
    def test_block(self):
53
 
        self._run("block")
 
46
        ns = {
 
47
            "bar": "rab",
 
48
        }
 
49
        self._runTemp("block", ns)
54
50
 
55
51
    def test_commonuse(self):
56
 
        self._run("commonuse")
 
52
        ns = {
 
53
             "foo": open("ptests/commonuse.data")
 
54
        }
 
55
        self._runTemp("commonuse", ns)
57
56
 
58
57
    def test_nested(self):
59
 
        self._run("nested")
60
 
 
61
 
    def test_raw(self):
62
 
        self._run("raw")
 
58
        self._runTemp("nested")
63
59
 
64
60
    def test_repeat(self):
65
 
        self._run("repeat")
 
61
        self._runTemp("repeat")
66
62
 
67
63
    def test_simple(self):
68
 
        self._run("simple")
 
64
        ns = {
 
65
            "foo": "one",
 
66
            "bar": "two",
 
67
            "boo": "three",
 
68
            "mdict": {"entry": "foo"}
 
69
        }
 
70
        self._runTemp("simple", ns)
69
71
 
70
72
 
71
73
class uErrors(TemplateTester):
72
74
    PREFIX = "ntests"
73
 
    def _run(self, filename):
 
75
    def _runTemp(self, filename, err):
74
76
        filename = os.path.join(self.PREFIX, filename)
75
 
        try:
76
 
            TemplateTester._run(self, filename)
77
 
        except cubictemp.tempException, value:
78
 
            pass
79
 
        else:
80
 
            self.fail("No exception on test case %s.\n" % (filename))
 
77
        libpry.raises(err, TemplateTester._runTemp, self, filename, {})
81
78
        
82
79
    def test_blockLine(self):
83
 
        self._run("blockLine")
 
80
        self._runTemp("blockLine", "'a' is not defined")
84
81
 
85
82
    def test_ifundefined(self):
86
 
        self._run("ifundefined")
 
83
        self._runTemp("ifundefined", "'a' is not defined")
87
84
 
88
85
    def test_loopsyntax(self):
89
 
        self._run("loopsyntax")
 
86
        self._runTemp("loopsyntax", "invalid expression")
90
87
 
91
88
    def test_loopundefined(self):
92
 
        self._run("loopundefined")
 
89
        self._runTemp("loopundefined", "'a' is not defined")
93
90
 
94
91
    def test_noniterable(self):
95
 
        self._run("noniterable")
 
92
        self._runTemp("noniterable", "can not iterate")
96
93
 
97
94
    def test_syntax(self):
98
 
        self._run("syntax")
 
95
        self._runTemp("syntax", "invalid expression")
99
96
 
100
97
    def test_undefined(self):
101
 
        self._run("undefined")
 
98
        self._runTemp("undefined", "'foo' is not defined")
102
99
 
103
100
 
104
101
class uQuoting(TemplateTester):
105
102
    PREFIX = "qtests"
106
 
    def _run(self, filename):
 
103
    def _runTemp(self, filename, nsDict={}):
107
104
        filename = os.path.join(self.PREFIX, filename)
108
 
        output = TemplateTester._run(self, filename)
 
105
        output = TemplateTester._runTemp(self, filename, nsDict)
109
106
        if (output.find("<") >= 0): self.fail()
110
107
        if (output.find(">") >= 0): self.fail()
111
108
        
112
109
    def test_commonuse(self):
113
 
        self._run("commonuse")
 
110
        class Dummy:
 
111
            def __repr__(self):
 
112
                return "<foo>"
 
113
 
 
114
        mdict = {
 
115
            "foo":      "<foinkle>",
 
116
            "object":   Dummy()
 
117
        }
 
118
        self._runTemp("commonuse", mdict)
114
119
 
115
120
 
116
121
class uNonQuoting(TemplateTester):
117
122
    PREFIX = "nqtests"
118
 
    def _run(self, filename):
 
123
    def _runTemp(self, filename, nsDict):
119
124
        filename = os.path.join(self.PREFIX, filename)
120
 
        output = TemplateTester._run(self, filename)
 
125
        output = TemplateTester._runTemp(self, filename, nsDict)
121
126
        if (output.find("<") < 0): self.fail()
122
127
        if (output.find(">") < 0): self.fail()
123
128
        
124
129
    def test_commonuse(self):
125
 
        self._run("object")
 
130
        class Dummy:
 
131
            _cubictemp_unescaped = 1
 
132
            def __repr__(self):
 
133
                return "<foo>"
 
134
 
 
135
        mdict = {
 
136
            "object":   Dummy()
 
137
        }
 
138
        self._runTemp("object", mdict)
126
139
 
127
140
    def test_tag(self):
128
 
        self._run("tag")
 
141
        mdict = {
 
142
            "tag":   "<foo>"
 
143
        }
 
144
        self._runTemp("tag", mdict)
 
145
 
 
146
 
 
147
tests = [
 
148
    uBasic(),
 
149
    uTemplate(),
 
150
    uErrors(),
 
151
    uQuoting(),
 
152
    uNonQuoting(),
 
153
]