1
from mako.template import Template
2
from mako import lookup
3
from util import flatten_result, result_lines
6
class NamespaceTest(unittest.TestCase):
14
this is x b, and heres ${a()}
22
assert flatten_result(t.render()) == "this is x a this is x b, and heres this is x a"
24
def test_template(self):
25
collection = lookup.TemplateLookup()
27
collection.put_string('main.html', """
28
<%namespace name="comp" file="defs.html"/>
30
this is main. ${comp.def1("hi")}
34
collection.put_string('defs.html', """
44
assert flatten_result(collection.get_template('main.html').render()) == "this is main. def1: hi def2: there"
46
def test_module(self):
47
collection = lookup.TemplateLookup()
49
collection.put_string('main.html', """
50
<%namespace name="comp" module="test_namespace"/>
52
this is main. ${comp.foo1()}
56
assert flatten_result(collection.get_template('main.html').render()) == "this is main. this is foo1. this is foo2, x is hi"
58
def test_module_2(self):
59
collection = lookup.TemplateLookup()
61
collection.put_string('main.html', """
62
<%namespace name="comp" module="foo.test_ns"/>
64
this is main. ${comp.foo1()}
68
assert flatten_result(collection.get_template('main.html').render()) == "this is main. this is foo1. this is foo2, x is hi"
70
def test_module_imports(self):
71
collection = lookup.TemplateLookup()
73
collection.put_string('main.html', """
74
<%namespace import="*" module="foo.test_ns"/>
76
this is main. ${foo1()}
80
assert flatten_result(collection.get_template('main.html').render()) == "this is main. this is foo1. this is foo2, x is hi"
82
def test_module_imports_2(self):
83
collection = lookup.TemplateLookup()
85
collection.put_string('main.html', """
86
<%namespace import="foo1, foo2" module="foo.test_ns"/>
88
this is main. ${foo1()}
92
assert flatten_result(collection.get_template('main.html').render()) == "this is main. this is foo1. this is foo2, x is hi"
94
def test_context(self):
95
"""test that namespace callables get access to the current context"""
96
collection = lookup.TemplateLookup()
98
collection.put_string('main.html', """
99
<%namespace name="comp" file="defs.html"/>
101
this is main. ${comp.def1()}
102
${comp.def2("there")}
105
collection.put_string('defs.html', """
110
<%def name="def2(x)">
115
assert flatten_result(collection.get_template('main.html').render(x="context x")) == "this is main. def1: x is context x def2: x is there"
117
def test_overload(self):
118
collection = lookup.TemplateLookup()
120
collection.put_string('main.html', """
121
<%namespace name="comp" file="defs.html">
122
<%def name="def1(x, y)">
123
overridden def1 ${x}, ${y}
127
this is main. ${comp.def1("hi", "there")}
128
${comp.def2("there")}
131
collection.put_string('defs.html', """
132
<%def name="def1(s)">
136
<%def name="def2(x)">
141
assert flatten_result(collection.get_template('main.html').render()) == "this is main. overridden def1 hi, there def2: there"
143
def test_getattr(self):
144
collection = lookup.TemplateLookup()
145
collection.put_string("main.html", """
146
<%namespace name="foo" file="ns.html"/>
148
if hasattr(foo, 'lala'):
150
if not hasattr(foo, 'hoho'):
151
context.write('foo has no hoho.')
154
collection.put_string("ns.html", """
155
<%def name="lala()">this is lala.</%def>
157
assert flatten_result(collection.get_template("main.html").render()) == "this is lala.foo has no hoho."
159
def test_in_def(self):
160
collection = lookup.TemplateLookup()
161
collection.put_string("main.html", """
162
<%namespace name="foo" file="ns.html"/>
164
this is main. ${bar()}
166
this is bar, foo is ${foo.bar()}
170
collection.put_string("ns.html", """
176
assert result_lines(collection.get_template("main.html").render()) == [
178
"this is bar, foo is" ,
179
"this is ns.html->bar"
183
def test_in_remote_def(self):
184
collection = lookup.TemplateLookup()
185
collection.put_string("main.html", """
186
<%namespace name="foo" file="ns.html"/>
188
this is main. ${bar()}
190
this is bar, foo is ${foo.bar()}
194
collection.put_string("ns.html", """
200
collection.put_string("index.html", """
201
<%namespace name="main" file="main.html"/>
207
assert result_lines(collection.get_template("index.html").render()) == [
209
"this is bar, foo is" ,
210
"this is ns.html->bar"
213
def test_inheritance(self):
214
"""test namespace initialization in a base inherited template that doesnt otherwise access the namespace"""
215
collection = lookup.TemplateLookup()
216
collection.put_string("base.html", """
217
<%namespace name="foo" file="ns.html" inheritable="True"/>
221
collection.put_string("ns.html", """
227
collection.put_string("index.html", """
228
<%inherit file="base.html"/>
234
assert result_lines(collection.get_template("index.html").render()) == [
236
"this is ns.html->bar"
239
def test_ccall(self):
240
collection = lookup.TemplateLookup()
241
collection.put_string("base.html", """
242
<%namespace name="foo" file="ns.html" inheritable="True"/>
246
collection.put_string("ns.html", """
249
caller body: ${caller.body()}
253
collection.put_string("index.html", """
254
<%inherit file="base.html"/>
257
<%call expr="self.foo.bar()">
262
assert result_lines(collection.get_template("index.html").render()) == [
264
"this is ns.html->bar",
269
def test_ccall_2(self):
270
collection = lookup.TemplateLookup()
271
collection.put_string("base.html", """
272
<%namespace name="foo" file="ns1.html" inheritable="True"/>
276
collection.put_string("ns1.html", """
277
<%namespace name="foo2" file="ns2.html"/>
279
<%call expr="foo2.ns2_bar()">
280
this is ns1.html->bar
281
caller body: ${caller.body()}
286
collection.put_string("ns2.html", """
287
<%def name="ns2_bar()">
288
this is ns2.html->bar
289
caller body: ${caller.body()}
293
collection.put_string("index.html", """
294
<%inherit file="base.html"/>
297
<%call expr="self.foo.bar()">
302
assert result_lines(collection.get_template("index.html").render()) == [
304
"this is ns2.html->bar",
306
"this is ns1.html->bar",
311
def test_import(self):
312
collection = lookup.TemplateLookup()
313
collection.put_string("functions.html","""
327
collection.put_string("func2.html", """
335
collection.put_string("index.html", """
336
<%namespace file="functions.html" import="*"/>
337
<%namespace file="func2.html" import="a, b"/>
346
assert result_lines(collection.get_template("index.html").render(bar="this is bar", x="this is x")) == [
355
def test_import_calledfromdef(self):
356
l = lookup.TemplateLookup()
357
l.put_string("a", """
358
<%def name="table()">
364
<%namespace file="a" import="table"/>
375
t = l.get_template("b")
376
assert flatten_result(t.render()) == "im table"
378
def test_closure_import(self):
379
collection = lookup.TemplateLookup()
380
collection.put_string("functions.html","""
390
collection.put_string("index.html", """
391
<%namespace file="functions.html" import="*"/>
403
assert result_lines(collection.get_template("index.html").render(bar="this is bar", x="this is x")) == [
408
def test_import_local(self):
410
<%namespace import="*">
419
assert flatten_result(t.render()) == "this is foo"
421
def test_ccall_import(self):
422
collection = lookup.TemplateLookup()
423
collection.put_string("functions.html","""
435
collection.put_string("index.html", """
436
<%namespace name="func" file="functions.html" import="*"/>
438
this is index embedded
441
this is lala ${foo()}
445
#print collection.get_template("index.html").code
446
#print collection.get_template("functions.html").code
447
assert result_lines(collection.get_template("index.html").render()) == [
449
"this is index embedded",
456
if __name__ == '__main__':