~jonas-drange/ubuntu-start-page/1252899-mobile-friendly

« back to all changes in this revision

Viewing changes to src/Mako-0.1.9/test/namespace.py

  • Committer: Matthew Nuzum
  • Date: 2008-04-18 01:58:53 UTC
  • Revision ID: matthew.nuzum@canonical.com-20080418015853-2b8rf979z2c2exxl
adding files

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from mako.template import Template
 
2
from mako import lookup
 
3
from util import flatten_result, result_lines
 
4
import unittest
 
5
 
 
6
class NamespaceTest(unittest.TestCase):
 
7
    def test_inline(self):
 
8
        t = Template("""
 
9
        <%namespace name="x">
 
10
            <%def name="a()">
 
11
                this is x a
 
12
            </%def>
 
13
            <%def name="b()">
 
14
                this is x b, and heres ${a()}
 
15
            </%def>
 
16
        </%namespace>
 
17
        
 
18
        ${x.a()}
 
19
        
 
20
        ${x.b()}
 
21
""")
 
22
        assert flatten_result(t.render()) == "this is x a this is x b, and heres this is x a"
 
23
 
 
24
    def test_template(self):
 
25
        collection = lookup.TemplateLookup()
 
26
 
 
27
        collection.put_string('main.html', """
 
28
        <%namespace name="comp" file="defs.html"/>
 
29
        
 
30
        this is main.  ${comp.def1("hi")}
 
31
        ${comp.def2("there")}
 
32
""")
 
33
 
 
34
        collection.put_string('defs.html', """
 
35
        <%def name="def1(s)">
 
36
            def1: ${s}
 
37
        </%def>
 
38
        
 
39
        <%def name="def2(x)">
 
40
            def2: ${x}
 
41
        </%def>
 
42
""")
 
43
 
 
44
        assert flatten_result(collection.get_template('main.html').render()) == "this is main. def1: hi def2: there"
 
45
    
 
46
    def test_module(self):
 
47
        collection = lookup.TemplateLookup()
 
48
 
 
49
        collection.put_string('main.html', """
 
50
        <%namespace name="comp" module="test_namespace"/>
 
51
 
 
52
        this is main.  ${comp.foo1()}
 
53
        ${comp.foo2("hi")}
 
54
""")
 
55
 
 
56
        assert flatten_result(collection.get_template('main.html').render()) == "this is main. this is foo1. this is foo2, x is hi"
 
57
 
 
58
    def test_module_2(self):
 
59
        collection = lookup.TemplateLookup()
 
60
 
 
61
        collection.put_string('main.html', """
 
62
        <%namespace name="comp" module="foo.test_ns"/>
 
63
 
 
64
        this is main.  ${comp.foo1()}
 
65
        ${comp.foo2("hi")}
 
66
""")
 
67
 
 
68
        assert flatten_result(collection.get_template('main.html').render()) == "this is main. this is foo1. this is foo2, x is hi"
 
69
 
 
70
    def test_module_imports(self):
 
71
        collection = lookup.TemplateLookup()
 
72
 
 
73
        collection.put_string('main.html', """
 
74
        <%namespace import="*" module="foo.test_ns"/>
 
75
 
 
76
        this is main.  ${foo1()}
 
77
        ${foo2("hi")}
 
78
""")
 
79
 
 
80
        assert flatten_result(collection.get_template('main.html').render()) == "this is main. this is foo1. this is foo2, x is hi"
 
81
 
 
82
    def test_module_imports_2(self):
 
83
        collection = lookup.TemplateLookup()
 
84
 
 
85
        collection.put_string('main.html', """
 
86
        <%namespace import="foo1, foo2" module="foo.test_ns"/>
 
87
 
 
88
        this is main.  ${foo1()}
 
89
        ${foo2("hi")}
 
90
""")
 
91
 
 
92
        assert flatten_result(collection.get_template('main.html').render()) == "this is main. this is foo1. this is foo2, x is hi"
 
93
        
 
94
    def test_context(self):
 
95
        """test that namespace callables get access to the current context"""
 
96
        collection = lookup.TemplateLookup()
 
97
 
 
98
        collection.put_string('main.html', """
 
99
        <%namespace name="comp" file="defs.html"/>
 
100
 
 
101
        this is main.  ${comp.def1()}
 
102
        ${comp.def2("there")}
 
103
""")
 
104
 
 
105
        collection.put_string('defs.html', """
 
106
        <%def name="def1()">
 
107
            def1: x is ${x}
 
108
        </%def>
 
109
 
 
110
        <%def name="def2(x)">
 
111
            def2: x is ${x}
 
112
        </%def>
 
113
""")
 
114
 
 
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"
 
116
        
 
117
    def test_overload(self):
 
118
        collection = lookup.TemplateLookup()
 
119
 
 
120
        collection.put_string('main.html', """
 
121
        <%namespace name="comp" file="defs.html">
 
122
            <%def name="def1(x, y)">
 
123
                overridden def1 ${x}, ${y}
 
124
            </%def>
 
125
        </%namespace>
 
126
 
 
127
        this is main.  ${comp.def1("hi", "there")}
 
128
        ${comp.def2("there")}
 
129
    """)
 
130
 
 
131
        collection.put_string('defs.html', """
 
132
        <%def name="def1(s)">
 
133
            def1: ${s}
 
134
        </%def>
 
135
 
 
136
        <%def name="def2(x)">
 
137
            def2: ${x}
 
138
        </%def>
 
139
    """)
 
140
 
 
141
        assert flatten_result(collection.get_template('main.html').render()) == "this is main. overridden def1 hi, there def2: there"
 
142
 
 
143
    def test_getattr(self):
 
144
        collection = lookup.TemplateLookup()
 
145
        collection.put_string("main.html", """
 
146
            <%namespace name="foo" file="ns.html"/>
 
147
            <%
 
148
                 if hasattr(foo, 'lala'):
 
149
                     foo.lala()
 
150
                 if not hasattr(foo, 'hoho'):
 
151
                     context.write('foo has no hoho.')
 
152
            %>
 
153
         """)
 
154
        collection.put_string("ns.html", """
 
155
          <%def name="lala()">this is lala.</%def>
 
156
        """)
 
157
        assert flatten_result(collection.get_template("main.html").render()) == "this is lala.foo has no hoho."
 
158
 
 
159
    def test_in_def(self):
 
160
        collection = lookup.TemplateLookup()
 
161
        collection.put_string("main.html", """
 
162
            <%namespace name="foo" file="ns.html"/>
 
163
            
 
164
            this is main.  ${bar()}
 
165
            <%def name="bar()">
 
166
                this is bar, foo is ${foo.bar()}
 
167
            </%def>
 
168
        """)
 
169
        
 
170
        collection.put_string("ns.html", """
 
171
            <%def name="bar()">
 
172
                this is ns.html->bar
 
173
            </%def>
 
174
        """)
 
175
 
 
176
        assert result_lines(collection.get_template("main.html").render()) == [
 
177
            "this is main.",
 
178
            "this is bar, foo is" ,
 
179
            "this is ns.html->bar"
 
180
        ]
 
181
 
 
182
 
 
183
    def test_in_remote_def(self):
 
184
        collection = lookup.TemplateLookup()
 
185
        collection.put_string("main.html", """
 
186
            <%namespace name="foo" file="ns.html"/>
 
187
 
 
188
            this is main.  ${bar()}
 
189
            <%def name="bar()">
 
190
                this is bar, foo is ${foo.bar()}
 
191
            </%def>
 
192
        """)
 
193
 
 
194
        collection.put_string("ns.html", """
 
195
            <%def name="bar()">
 
196
                this is ns.html->bar
 
197
            </%def>
 
198
        """)
 
199
        
 
200
        collection.put_string("index.html", """
 
201
            <%namespace name="main" file="main.html"/>
 
202
            
 
203
            this is index
 
204
            ${main.bar()}
 
205
        """)
 
206
 
 
207
        assert result_lines(collection.get_template("index.html").render()) == [  
 
208
            "this is index",
 
209
            "this is bar, foo is" ,
 
210
            "this is ns.html->bar"
 
211
        ]
 
212
    
 
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"/>
 
218
            
 
219
            ${next.body()}
 
220
""")
 
221
        collection.put_string("ns.html", """
 
222
            <%def name="bar()">
 
223
                this is ns.html->bar
 
224
            </%def>
 
225
        """)
 
226
 
 
227
        collection.put_string("index.html", """
 
228
            <%inherit file="base.html"/>
 
229
    
 
230
            this is index
 
231
            ${self.foo.bar()}
 
232
        """)
 
233
        
 
234
        assert result_lines(collection.get_template("index.html").render()) == [
 
235
            "this is index",
 
236
            "this is ns.html->bar"
 
237
        ]
 
238
        
 
239
    def test_ccall(self):
 
240
        collection = lookup.TemplateLookup()
 
241
        collection.put_string("base.html", """
 
242
            <%namespace name="foo" file="ns.html" inheritable="True"/>
 
243
 
 
244
            ${next.body()}
 
245
    """)
 
246
        collection.put_string("ns.html", """
 
247
            <%def name="bar()">
 
248
                this is ns.html->bar
 
249
                caller body: ${caller.body()}
 
250
            </%def>
 
251
        """)
 
252
 
 
253
        collection.put_string("index.html", """
 
254
            <%inherit file="base.html"/>
 
255
 
 
256
            this is index
 
257
            <%call expr="self.foo.bar()">
 
258
                call body
 
259
            </%call>
 
260
        """)
 
261
 
 
262
        assert result_lines(collection.get_template("index.html").render()) == [
 
263
            "this is index",
 
264
            "this is ns.html->bar",
 
265
            "caller body:",
 
266
            "call body"
 
267
        ]
 
268
 
 
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"/>
 
273
 
 
274
            ${next.body()}
 
275
    """)
 
276
        collection.put_string("ns1.html", """
 
277
            <%namespace name="foo2" file="ns2.html"/>
 
278
            <%def name="bar()">
 
279
                <%call expr="foo2.ns2_bar()">
 
280
                this is ns1.html->bar
 
281
                caller body: ${caller.body()}
 
282
                </%call>
 
283
            </%def>
 
284
        """)
 
285
 
 
286
        collection.put_string("ns2.html", """
 
287
            <%def name="ns2_bar()">
 
288
                this is ns2.html->bar
 
289
                caller body: ${caller.body()}
 
290
            </%def>
 
291
        """)
 
292
 
 
293
        collection.put_string("index.html", """
 
294
            <%inherit file="base.html"/>
 
295
 
 
296
            this is index
 
297
            <%call expr="self.foo.bar()">
 
298
                call body
 
299
            </%call>
 
300
        """)
 
301
 
 
302
        assert result_lines(collection.get_template("index.html").render()) == [
 
303
            "this is index",
 
304
            "this is ns2.html->bar",
 
305
            "caller body:",
 
306
            "this is ns1.html->bar",
 
307
            "caller body:",
 
308
            "call body"
 
309
        ]
 
310
 
 
311
    def test_import(self):
 
312
        collection = lookup.TemplateLookup()
 
313
        collection.put_string("functions.html","""
 
314
            <%def name="foo()">
 
315
                this is foo
 
316
            </%def>
 
317
            
 
318
            <%def name="bar()">
 
319
                this is bar
 
320
            </%def>
 
321
            
 
322
            <%def name="lala()">
 
323
                this is lala
 
324
            </%def>
 
325
        """)
 
326
 
 
327
        collection.put_string("func2.html", """
 
328
            <%def name="a()">
 
329
                this is a
 
330
            </%def>
 
331
            <%def name="b()">
 
332
                this is b
 
333
            </%def>
 
334
        """)
 
335
        collection.put_string("index.html", """
 
336
            <%namespace file="functions.html" import="*"/>
 
337
            <%namespace file="func2.html" import="a, b"/>
 
338
            ${foo()}
 
339
            ${bar()}
 
340
            ${lala()}
 
341
            ${a()}
 
342
            ${b()}
 
343
            ${x}
 
344
        """)
 
345
 
 
346
        assert result_lines(collection.get_template("index.html").render(bar="this is bar", x="this is x")) == [
 
347
            "this is foo",
 
348
            "this is bar",
 
349
            "this is lala",
 
350
            "this is a",
 
351
            "this is b",
 
352
            "this is x"
 
353
        ]
 
354
    
 
355
    def test_import_calledfromdef(self):
 
356
        l = lookup.TemplateLookup()
 
357
        l.put_string("a", """
 
358
        <%def name="table()">
 
359
            im table
 
360
        </%def>
 
361
        """)
 
362
 
 
363
        l.put_string("b","""
 
364
        <%namespace file="a" import="table"/>
 
365
 
 
366
        <%
 
367
            def table2():
 
368
                table()
 
369
                return ""
 
370
        %>
 
371
 
 
372
        ${table2()}
 
373
        """)
 
374
 
 
375
        t = l.get_template("b")
 
376
        assert flatten_result(t.render()) == "im table"
 
377
            
 
378
    def test_closure_import(self):
 
379
        collection = lookup.TemplateLookup()
 
380
        collection.put_string("functions.html","""
 
381
            <%def name="foo()">
 
382
                this is foo
 
383
            </%def>
 
384
            
 
385
            <%def name="bar()">
 
386
                this is bar
 
387
            </%def>
 
388
        """)
 
389
        
 
390
        collection.put_string("index.html", """
 
391
            <%namespace file="functions.html" import="*"/>
 
392
            <%def name="cl1()">
 
393
                ${foo()}
 
394
            </%def>
 
395
            
 
396
            <%def name="cl2()">
 
397
                ${bar()}
 
398
            </%def>
 
399
            
 
400
            ${cl1()}
 
401
            ${cl2()}
 
402
        """)
 
403
        assert result_lines(collection.get_template("index.html").render(bar="this is bar", x="this is x")) == [
 
404
            "this is foo",
 
405
            "this is bar",
 
406
        ]
 
407
 
 
408
    def test_import_local(self):
 
409
        t = Template("""
 
410
            <%namespace import="*">
 
411
                <%def name="foo()">
 
412
                    this is foo
 
413
                </%def>
 
414
            </%namespace>
 
415
            
 
416
            ${foo()}
 
417
        
 
418
        """)
 
419
        assert flatten_result(t.render()) == "this is foo"
 
420
        
 
421
    def test_ccall_import(self):
 
422
        collection = lookup.TemplateLookup()
 
423
        collection.put_string("functions.html","""
 
424
            <%def name="foo()">
 
425
                this is foo
 
426
            </%def>
 
427
            
 
428
            <%def name="bar()">
 
429
                this is bar.
 
430
                ${caller.body()}
 
431
                ${caller.lala()}
 
432
            </%def>
 
433
        """)
 
434
        
 
435
        collection.put_string("index.html", """
 
436
            <%namespace name="func" file="functions.html" import="*"/>
 
437
            <%call expr="bar()">
 
438
                this is index embedded
 
439
                foo is ${foo()}
 
440
                <%def name="lala()">
 
441
                     this is lala ${foo()}
 
442
                </%def>
 
443
            </%call>
 
444
        """)
 
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()) == [
 
448
            "this is bar.",
 
449
            "this is index embedded",
 
450
            "foo is",
 
451
            "this is foo",
 
452
            "this is lala",
 
453
            "this is foo"
 
454
        ]
 
455
 
 
456
if __name__ == '__main__':
 
457
    unittest.main()