summaryrefslogtreecommitdiff
path: root/src/zope/pagetemplate/tests/test_htmltests.py
blob: 15506348eea90712ceee0a2b9d6ca499a4c1c452 (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
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
154
155
156
157
##############################################################################
#
# Copyright (c) 2001, 2002 Zope Foundation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Page Template HTML Tests
"""
import unittest

from zope.pagetemplate.tests import util
from zope.pagetemplate.pagetemplate import PageTemplate


class Folder(object):
    context = property(lambda self: self)

class HTMLTests(unittest.TestCase):

    def setUp(self):
        self.folder = f = Folder()
        f.laf = PageTemplate()
        f.t = PageTemplate()

    def getProducts(self):
        return [
            {
                'description': ('This is the tee for those who LOVE Zope. '
                                'Show your heart on your tee.'),
                'price': 12.99, 'image': 'smlatee.jpg'
            },
            {
                'description': ('This is the tee for Jim Fulton. '
                                'He\'s the Zope Pope!'),
                'price': 11.99, 'image': 'smpztee.jpg'
            },
        ]

    def test_1(self):
        laf = self.folder.laf
        laf.write(util.read_input('teeshoplaf.html'))
        expect = util.read_output('teeshoplaf.html')
        util.check_html(expect, laf())

    def test_2(self):
        self.folder.laf.write(util.read_input('teeshoplaf.html'))

        t = self.folder.t
        t.write(util.read_input('teeshop2.html'))
        expect = util.read_output('teeshop2.html')
        out = t(laf=self.folder.laf, getProducts=self.getProducts)
        util.check_html(expect, out)


    def test_3(self):
        self.folder.laf.write(util.read_input('teeshoplaf.html'))

        t = self.folder.t
        t.write(util.read_input('teeshop1.html'))
        expect = util.read_output('teeshop1.html')
        out = t(laf=self.folder.laf, getProducts=self.getProducts)
        util.check_html(expect, out)

    def test_SimpleLoop(self):
        t = self.folder.t
        t.write(util.read_input('loop1.html'))
        expect = util.read_output('loop1.html')
        out = t()
        util.check_html(expect, out)

    def test_GlobalsShadowLocals(self):
        t = self.folder.t
        t.write(util.read_input('globalsshadowlocals.html'))
        expect = util.read_output('globalsshadowlocals.html')
        out = t()
        util.check_html(expect, out)

    def test_StringExpressions(self):
        t = self.folder.t
        t.write(util.read_input('stringexpression.html'))
        expect = util.read_output('stringexpression.html')
        out = t()
        util.check_html(expect, out)

    def test_ReplaceWithNothing(self):
        t = self.folder.t
        t.write(util.read_input('checknothing.html'))
        expect = util.read_output('checknothing.html')
        out = t()
        util.check_html(expect, out)

    def test_WithXMLHeader(self):
        t = self.folder.t
        t.write(util.read_input('checkwithxmlheader.html'))
        expect = util.read_output('checkwithxmlheader.html')
        out = t()
        util.check_html(expect, out)

    def test_NotExpression(self):
        t = self.folder.t
        t.write(util.read_input('checknotexpression.html'))
        expect = util.read_output('checknotexpression.html')
        out = t()
        util.check_html(expect, out)

    def test_PathNothing(self):
        t = self.folder.t
        t.write(util.read_input('checkpathnothing.html'))
        expect = util.read_output('checkpathnothing.html')
        out = t()
        util.check_html(expect, out)

    def test_PathAlt(self):
        t = self.folder.t
        t.write(util.read_input('checkpathalt.html'))
        expect = util.read_output('checkpathalt.html')
        out = t()
        util.check_html(expect, out)

    def test_translation(self):
        from zope.i18nmessageid import MessageFactory
        _ = MessageFactory('pttest')
        msg = _("Translate this!")

        t = self.folder.t
        t.write(util.read_input('translation.html'))
        expect = util.read_output('translation.html')
        out = t(msg=msg)
        util.check_html(expect, out)

    def test_recursion(self):
        t = self.folder.t
        t.write(util.read_input('recursive.html'))
        expect = util.read_output('recursive.html')
        context = dict(name='root',
                       children=[dict(name='first', children=[]),
                                 dict(name='second', children=[])])
        namespace = dict(template=t, options={}, args=(),
                         nothing=None, context=context)
        out = t.pt_render(namespace)
        # crude way of normalizing whitespace
        expect = expect.replace(' ', '').replace('\n\n', '\n')
        out = out.replace(' ', '').replace('\n\n', '\n')
        util.check_html(expect, out)
        # https://bugs.launchpad.net/zope.pagetemplate/+bug/732972
        errors = t.pt_errors(namespace, check_macro_expansion=False)
        self.assertFalse(errors)

def test_suite():
    return unittest.defaultTestLoader.loadTestsFromName(__name__)