File: test_call.py

package info (click to toggle)
mako 1.0.6%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,556 kB
  • ctags: 1,495
  • sloc: python: 11,807; makefile: 167
file content (514 lines) | stat: -rw-r--r-- 13,281 bytes parent folder | download | duplicates (3)
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
from mako.template import Template
from mako import util
from test.util import result_lines, flatten_result
from test import TemplateTest, eq_

class CallTest(TemplateTest):
    def test_call(self):
        t = Template("""
        <%def name="foo()">
            hi im foo ${caller.body(y=5)}
        </%def>

        <%call expr="foo()" args="y, **kwargs">
            this is the body, y is ${y}
        </%call>
""")
        assert result_lines(t.render()) == ['hi im foo', 'this is the body, y is 5']


    def test_compound_call(self):
        t = Template("""

        <%def name="bar()">
            this is bar
        </%def>

        <%def name="comp1()">
            this comp1 should not be called
        </%def>

        <%def name="foo()">
            foo calling comp1: ${caller.comp1(x=5)}
            foo calling body: ${caller.body()}
        </%def>

        <%call expr="foo()">
            <%def name="comp1(x)">
                this is comp1, ${x}
            </%def>
            this is the body, ${comp1(6)}
        </%call>
        ${bar()}

""")
        assert result_lines(t.render()) == ['foo calling comp1:', 'this is comp1, 5', 'foo calling body:', 'this is the body,', 'this is comp1, 6', 'this is bar']

    def test_new_syntax(self):
        """test foo:bar syntax, including multiline args and expression eval."""

        # note the trailing whitespace in the bottom ${} expr, need to strip
        # that off < python 2.7

        t = Template("""
            <%def name="foo(x, y, q, z)">
                ${x}
                ${y}
                ${q}
                ${",".join("%s->%s" % (a, b) for a, b in z)}
            </%def>

            <%self:foo x="this is x" y="${'some ' + 'y'}" q="
                this
                is
                q"

                z="${[
                (1, 2),
                (3, 4),
                (5, 6)
            ]

            }"/>
        """)

        eq_(
            result_lines(t.render()),
             ['this is x', 'some y', 'this', 'is', 'q', '1->2,3->4,5->6']
        )

    def test_ccall_caller(self):
        t = Template("""
        <%def name="outer_func()">
        OUTER BEGIN
            <%call expr="caller.inner_func()">
                INNER CALL
            </%call>
        OUTER END
        </%def>

        <%call expr="outer_func()">
            <%def name="inner_func()">
                INNER BEGIN
                ${caller.body()}
                INNER END
            </%def>
        </%call>

        """)
        #print t.code
        assert result_lines(t.render()) == [
            "OUTER BEGIN",
            "INNER BEGIN",
            "INNER CALL",
            "INNER END",
            "OUTER END",
        ]

    def test_stack_pop(self):
        t = Template("""
        <%def name="links()" buffered="True">
           Some links
        </%def>

        <%def name="wrapper(links)">
           <h1>${caller.body()}</h1>
           ${links}
        </%def>

        ## links() pushes a stack frame on.  when complete,
        ## 'nextcaller' must be restored
        <%call expr="wrapper(links())">
           Some title
        </%call>

        """)

        assert result_lines(t.render()) == [
        "<h1>",
        "Some title",
        "</h1>",
        "Some links"
        ]

    def test_conditional_call(self):
        """test that 'caller' is non-None only if the immediate <%def> was called via <%call>"""

        t = Template("""
        <%def name="a()">
        % if caller:
        ${ caller.body() } \\
        % endif
        AAA
        ${ b() }
        </%def>

        <%def name="b()">
        % if caller:
        ${ caller.body() } \\
        % endif
        BBB
        ${ c() }
        </%def>

        <%def name="c()">
        % if caller:
        ${ caller.body() } \\
        % endif
        CCC
        </%def>

        <%call expr="a()">
        CALL
        </%call>

        """)
        assert result_lines(t.render()) == [
            "CALL",
            "AAA",
            "BBB",
            "CCC"
        ]

    def test_chained_call(self):
        """test %calls that are chained through their targets"""
        t = Template("""
            <%def name="a()">
                this is a.
                <%call expr="b()">
                    this is a's ccall.  heres my body: ${caller.body()}
                </%call>
            </%def>
            <%def name="b()">
                this is b.  heres  my body: ${caller.body()}
                whats in the body's caller's body ?
                ${context.caller_stack[-2].body()}
            </%def>

            <%call expr="a()">
                heres the main templ call
            </%call>

""")
        assert result_lines(t.render()) == [
            'this is a.',
            'this is b. heres my body:',
            "this is a's ccall. heres my body:",
            'heres the main templ call',
            "whats in the body's caller's body ?",
            'heres the main templ call'
        ]

    def test_nested_call(self):
        """test %calls that are nested inside each other"""
        t = Template("""
            <%def name="foo()">
                ${caller.body(x=10)}
            </%def>

            x is ${x}
            <%def name="bar()">
                bar: ${caller.body()}
            </%def>

            <%call expr="foo()" args="x">
                this is foo body: ${x}

                <%call expr="bar()">
                    this is bar body: ${x}
                </%call>
            </%call>
""")
        assert result_lines(t.render(x=5)) == [
            "x is 5",
            "this is foo body: 10",
            "bar:",
            "this is bar body: 10"
        ]

    def test_nested_call_2(self):
        t = Template("""
            x is ${x}
            <%def name="foo()">
                ${caller.foosub(x=10)}
            </%def>

            <%def name="bar()">
                bar: ${caller.barsub()}
            </%def>

            <%call expr="foo()">
                <%def name="foosub(x)">
                this is foo body: ${x}

                <%call expr="bar()">
                    <%def name="barsub()">
                    this is bar body: ${x}
                    </%def>
                </%call>

                </%def>

            </%call>
""")
        assert result_lines(t.render(x=5)) == [
            "x is 5",
            "this is foo body: 10",
            "bar:",
            "this is bar body: 10"
        ]

    def test_nested_call_3(self):
        template = Template('''\
        <%def name="A()">
          ${caller.body()}
        </%def>

        <%def name="B()">
          ${caller.foo()}
        </%def>

        <%call expr="A()">
          <%call expr="B()">
            <%def name="foo()">
              foo
            </%def>
          </%call>
        </%call>

        ''')
        assert flatten_result(template.render()) == "foo"

    def test_nested_call_4(self):
        base = """
        <%def name="A()">
        A_def
        ${caller.body()}
        </%def>

        <%def name="B()">
        B_def
        ${caller.body()}
        </%def>
        """

        template = Template(base + """
        <%def name="C()">
         C_def
         <%self:B>
           <%self:A>
              A_body
           </%self:A>
            B_body
           ${caller.body()}
         </%self:B>
        </%def>

        <%self:C>
        C_body
        </%self:C>
        """)

        eq_(
            flatten_result(template.render()),
            "C_def B_def A_def A_body B_body C_body"
        )

        template = Template(base + """
        <%def name="C()">
         C_def
         <%self:B>
            B_body
           ${caller.body()}
           <%self:A>
              A_body
           </%self:A>
         </%self:B>
        </%def>

        <%self:C>
        C_body
        </%self:C>
        """)

        eq_(
            flatten_result(template.render()),
            "C_def B_def B_body C_body A_def A_body"
        )

    def test_chained_call_in_nested(self):
        t = Template("""
            <%def name="embedded()">
            <%def name="a()">
                this is a.
                <%call expr="b()">
                    this is a's ccall.  heres my body: ${caller.body()}
                </%call>
            </%def>
            <%def name="b()">
                this is b.  heres  my body: ${caller.body()}
                whats in the body's caller's body ? ${context.caller_stack[-2].body()}
            </%def>

            <%call expr="a()">
                heres the main templ call
            </%call>
            </%def>
            ${embedded()}
""")
        #print t.code
        #print result_lines(t.render())
        assert result_lines(t.render()) == [
            'this is a.',
            'this is b. heres my body:',
            "this is a's ccall. heres my body:",
            'heres the main templ call',
            "whats in the body's caller's body ?",
            'heres the main templ call'
        ]

    def test_call_in_nested(self):
        t = Template("""
            <%def name="a()">
                this is a ${b()}
                <%def name="b()">
                    this is b
                    <%call expr="c()">
                        this is the body in b's call
                    </%call>
                </%def>
                <%def name="c()">
                    this is c: ${caller.body()}
                </%def>
            </%def>
        ${a()}
""")
        assert result_lines(t.render()) == ['this is a', 'this is b', 'this is c:', "this is the body in b's call"]

    def test_composed_def(self):
        t = Template("""
            <%def name="f()"><f>${caller.body()}</f></%def>
            <%def name="g()"><g>${caller.body()}</g></%def>
            <%def name="fg()">
                <%self:f><%self:g>${caller.body()}</%self:g></%self:f>
            </%def>
            <%self:fg>fgbody</%self:fg>
            """)
        assert result_lines(t.render()) == ['<f><g>fgbody</g></f>']

    def test_regular_defs(self):
        t = Template("""
        <%!
            @runtime.supports_caller
            def a(context):
                context.write("this is a")
                if context['caller']:
                    context['caller'].body()
                context.write("a is done")
                return ''
        %>

        <%def name="b()">
            this is b
            our body: ${caller.body()}
            ${a(context)}
        </%def>
        test 1
        <%call expr="a(context)">
            this is the body
        </%call>
        test 2
        <%call expr="b()">
            this is the body
        </%call>
        test 3
        <%call expr="b()">
            this is the body
            <%call expr="b()">
                this is the nested body
            </%call>
        </%call>


        """)
        assert result_lines(t.render()) == [
            "test 1",
            "this is a",
            "this is the body",
            "a is done",
            "test 2",
            "this is b",
            "our body:",
            "this is the body",
            "this is aa is done",
            "test 3",
            "this is b",
            "our body:",
            "this is the body",
            "this is b",
            "our body:",
            "this is the nested body",
            "this is aa is done",
            "this is aa is done"
        ]

    def test_call_in_nested_2(self):
        t = Template("""
            <%def name="a()">
                <%def name="d()">
                    not this d
                </%def>
                this is a ${b()}
                <%def name="b()">
                    <%def name="d()">
                        not this d either
                    </%def>
                    this is b
                    <%call expr="c()">
                        <%def name="d()">
                            this is d
                        </%def>
                        this is the body in b's call
                    </%call>
                </%def>
                <%def name="c()">
                    this is c: ${caller.body()}
                    the embedded "d" is: ${caller.d()}
                </%def>
            </%def>
        ${a()}
""")
        assert result_lines(t.render()) == ['this is a', 'this is b', 'this is c:', "this is the body in b's call", 'the embedded "d" is:', 'this is d']

class SelfCacheTest(TemplateTest):
    """this test uses a now non-public API."""

    def test_basic(self):
        t = Template("""
        <%!
            cached = None
        %>
        <%def name="foo()">
            <%
                global cached
                if cached:
                    return "cached: " + cached
                __M_writer = context._push_writer()
            %>
            this is foo
            <%
                buf, __M_writer = context._pop_buffer_and_writer()
                cached = buf.getvalue()
                return cached
            %>
        </%def>

        ${foo()}
        ${foo()}
""")
        assert result_lines(t.render()) == [
            "this is foo",
            "cached:",
            "this is foo"
        ]