Skip to content
Myr

test_macros.py

This file

Path

tests/test_macros.py

106 lines · 2.7 KiB · Python

Last changed by

Copyright switch to "The Jinja Team".Armin Ronacherbefore adoption62ccd1b

History of this file

Take it

The walk stopped after 200 commits: lines older than b217886 point at it.

added author information and fixed small off-by-one bugArmin Ronacherbefore adoptionb217886

lines 1–5why, one step back
1# -*- coding: utf-8 -*-
2"""3 unit test for the macros4 ~~~~~~~~~~~~~~~~~~~~~~~~5

Copyright switch to "The Jinja Team".Armin Ronacherbefore adoption62ccd1b

line 6why, one step back
6 :copyright: (c) 2009 by the Jinja Team.

added author information and fixed small off-by-one bugArmin Ronacherbefore adoptionb217886

lines 7–42why, one step back
7 :license: BSD, see LICENSE for more details.
8"""910SIMPLE = '''\11{% macro say_hello(name) %}Hello {{ name }}!{% endmacro %}12{{ say_hello('Peter') }}\13'''1415SCOPING = '''\16{% macro level1(data1) %}17{% macro level2(data2) %}{{ data1 }}|{{ data2 }}{% endmacro %}18{{ level2('bar') }}{% endmacro %}19{{ level1('foo') }}\20'''2122ARGUMENTS = '''\23{% macro m(a, b, c='c', d='d') %}{{ a }}|{{ b }}|{{ c }}|{{ d }}{% endmacro %}24{{ m() }}|{{ m('a') }}|{{ m('a', 'b') }}|{{ m(1, 2, 3) }}\25'''2627VARARGS = '''\28{% macro test() %}{{ varargs|join('|') }}{% endmacro %}\29{{ test(1, 2, 3) }}\30'''3132SIMPLECALL = '''\33{% macro test() %}[[{{ caller() }}]]{% endmacro %}\34{% call test() %}data{% endcall %}\35'''3637COMPLEXCALL = '''\38{% macro test() %}[[{{ caller('data') }}]]{% endmacro %}\39{% call(data) test() %}{{ data }}{% endcall %}\40'''4142CALLERUNDEFINED = '''\

switched back to explicit set for assignments. {% foo = 42 %} becomes {% set foo = 42 %} and {% foo.something() }} becomes {{ void(foo.something() }} with this commit.Armin Ronacherbefore adoption0a2ac69

line 43why, one step back
43{% set caller = 42 %}\

added author information and fixed small off-by-one bugArmin Ronacherbefore adoptionb217886

lines 44–88why, one step back
44{% macro test() %}{{ caller is not defined }}{% endmacro %}\
45{{ test() }}\46'''4748INCLUDETEMPLATE = '''{% macro test(foo) %}[{{ foo }}]{% endmacro %}'''495051def test_simple(env):52 tmpl = env.from_string(SIMPLE)53 assert tmpl.render() == 'Hello Peter!'545556def test_scoping(env):57 tmpl = env.from_string(SCOPING)58 assert tmpl.render() == 'foo|bar'596061def test_arguments(env):62 tmpl = env.from_string(ARGUMENTS)63 assert tmpl.render() == '||c|d|a||c|d|a|b|c|d|1|2|3|d'646566def test_varargs(env):67 tmpl = env.from_string(VARARGS)68 assert tmpl.render() == '1|2|3'697071def test_simple_call(env):72 tmpl = env.from_string(SIMPLECALL)73 assert tmpl.render() == '[[data]]'747576def test_complex_call(env):77 tmpl = env.from_string(COMPLEXCALL)78 assert tmpl.render() == '[[data]]'798081def test_caller_undefined(env):82 tmpl = env.from_string(CALLERUNDEFINED)83 assert tmpl.render() == 'True'848586def test_include(env):87 tmpl = env.from_string('{% from "include" import test %}{{ test("foo") }}')88 assert tmpl.render() == '[foo]'

more unittestsArmin Ronacherbefore adoptione62b7ef

lines 89–106why, one step back
89
9091def test_macro_api(env):92 tmpl = env.from_string('{% macro foo(a, b) %}{% endmacro %}'93 '{% macro bar() %}{{ varargs }}{{ kwargs }}{% endmacro %}'94 '{% macro baz() %}{{ caller() }}{% endmacro %}')95 assert tmpl.module.foo.arguments == ('a', 'b')96 assert tmpl.module.foo.defaults == ()97 assert tmpl.module.foo.name == 'foo'98 assert not tmpl.module.foo.caller99 assert not tmpl.module.foo.catch_kwargs100 assert not tmpl.module.foo.catch_varargs101 assert tmpl.module.bar.arguments == ()102 assert tmpl.module.bar.defaults == ()103 assert not tmpl.module.bar.caller104 assert tmpl.module.bar.catch_kwargs105 assert tmpl.module.bar.catch_varargs106 assert tmpl.module.baz.caller