/
test_template.py
135 lines (116 loc) · 4.8 KB
/
test_template.py
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
import tw2.core as twc
import testapi
import itertools
import os
import webob as wo
from nose.tools import raises, eq_
# TBD: only test engines that are installed
engines = ['genshi', 'mako']
class TestWD(twc.Widget):
test = twc.Param(default='bob')
class TestTemplate(object):
def setUp(self):
testapi.setup()
def _check_render(self, template, data, expected, engine=None):
if engine:
mw = twc.make_middleware(None, preferred_rendering_engines=[engine])
testapi.request(1, mw)
twc.util.flush_memoization()
out = twc.templating.render(template, 'string', data)
assert(isinstance(out, unicode))
eq_(out, expected)
def test_get_source_inline(self):
template_in = "awesome"
template_out = twc.templating.get_source("mako", template_in, True)
eq_(template_in, template_out)
def test_get_source_mako(self):
expected = "<p>TEST mako</p>"
t = twc.templating.get_source("mako", "tw2.core.test_templates.simple")
eq_(expected, t)
def test_auto_select_engine(self):
for engine in engines:
#set up the default renderers
yield self._check_render, 'tw2.core.test_templates.simple', {'test':engine}, '<p>TEST %s</p>'%engine, engine
@raises(ValueError)
def test_auto_select_unavailable_engine(self):
engine = 'mako'
self._check_render('tw2.core.test_templates.simple_genshi', {'test':engine}, '<p>TEST %s</p>'%engine, engine)
def test_auto_select_cache_works(self):
engine='genshi'
args = 'tw2.core.test_templates.simple_genshi', 'string', {'test':engine}
out = twc.templating.render(*args)
assert(isinstance(out, unicode))
assert out == '<p>TEST genshi</p>', out
out = twc.templating.render(*args)
assert(isinstance(out, unicode))
assert out == '<p>TEST genshi</p>', out
def test_auto_select_unavailable_engine_not_strict(self):
engine = 'mako'
mw = twc.make_middleware(
None,
preferred_rendering_engines=[engine],
strict_engine_selection=False,
)
testapi.request(501, mw)
self._check_render(
'tw2.core.test_templates.simple_genshi',
{'test':'blah!'},
'<p>TEST blah!</p>',
)
def test_engines(self):
for engine in engines:
print "Testing %s..." % engine
args = [
'%s:tw2.core.test_templates.simple_%s' % (engine, engine),
'string',
{'test':'test1'},
]
out = twc.templating.render(*args)
assert(isinstance(out, unicode))
assert(out == '<p>TEST test1</p>')
def test_engines_unicode(self):
for engine in engines:
print "Testing %s..." % engine
out = twc.templating.render(
'%s:tw2.core.test_templates.simple_%s' % (engine, engine),
'string', {'test':'test\u1234'}
)
assert(out == '<p>TEST test\u1234</p>')
def test_nesting(self):
"Check that templates can be correctly nested, in any combination"
for outer, inner in itertools.product(engines, engines):
print 'Testing %s on %s' % (inner, outer)
test = twc.templating.render(
'%s:tw2.core.test_templates.simple_%s' % (inner, inner),
outer,
{'test':'test1'},
)
out = twc.templating.render(
'%s:tw2.core.test_templates.simple_%s' % (outer, outer),
'string',
{'test':test},
)
eq_(out, '<p>TEST <p>TEST test1</p></p>')
def test_widget_display(self):
twc.core.request_local()['middleware'] = twc.make_middleware(None)
mtest = TestWD(id='x')
for eng in engines:
print eng
test = mtest.req()
test.template = '%s:tw2.core.test_templates.inner_%s' % (eng, eng)
out = test.display()
assert(out == '<p>TEST bob</p>')
def test_widget_nesting(self):
twc.core.request_local()['middleware'] = twc.make_middleware(None)
for outer, inner in itertools.product(engines, engines):
outer = '%s:tw2.core.test_templates.widget_%s' % (outer, outer)
inner = '%s:tw2.core.test_templates.inner_%s' % (inner, inner)
test = twc.CompoundWidget(
id='x', template=outer,
children=[TestWD(id='y', template=inner)]
)
eq_(test.display(), '<p>TEST <p>TEST bob</p></p>')
def test_genshi_abs(self):
test_dir = os.path.sep.join(__file__.split(os.path.sep)[:-1])
fname = os.path.sep.join([test_dir, 'test.html'])
twc.Widget(template='genshi_abs:%s' % fname).display()