Skip to content
Myr

moved example tests into a folder

Before adoption: this commit came in with the history the project adopted, so no landing records it.

--HG-- branch : trunk rename : bench.py => examples/bench.py rename : test.py => examples/test.py rename : test_filter_and_linestatements.py => examples/test_filter_and_linestatements.py rename : test_loop_filter.py => examples/test_loop_filter.py

authored by Armin Ronacher · committed by Armin Ronacher · parent 3d8b784 · Browse files · 8 files+191 added−191 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

8 files+191 added−191 removed

  • other:+191 added−191 removed

8 files+191 added−191 removed

added:examples/bench.py+127 added
1from django.conf import settings2settings.configure()3from django.template import Template as DjangoTemplate, Context as DjangoContext4from jinja2 import Environment as JinjaEnvironment5from mako.template import Template as MakoTemplate6from timeit import Timer789jinja_template = JinjaEnvironment(10 line_statement_prefix='%',11 variable_start_string="${",12 variable_end_string="}"13).from_string("""\14<!doctype html>15<html>16 <head>17 <title>${page_title|e}18 </head>19 <body>20 <div class="header">21 <h1>${page_title|e}</h1>22 </div>23 <ul class="navigation">24 % for href, caption in [25 ('index.html', 'Index'),26 ('downloads.html', 'Downloads'),27 ('products.html', 'Products')28 ]29 <li><a href="${href|e}">${caption|e}</a></li>30 % endfor31 </ul>32 <div class="table">33 <table>34 % for row in table35 <tr>36 % for cell in row37 <td>${cell}</td>38 % endfor39 </tr>40 % endfor41 </table>42 </div>43 </body>44</html>\45""")4647django_template = DjangoTemplate("""\48<!doctype html>49<html>50 <head>51 <title>{{ page_title }}52 </head>53 <body>54 <div class="header">55 <h1>{{ page_title }}</h1>56 </div>57 <ul class="navigation">58 {% for href, caption in navigation %}59 <li><a href="{{ href }}">{{ caption }}</a></li>60 {% endfor %}61 </ul>62 <div class="table">63 <table>64 {% for row in table %}65 <tr>66 {% for cell in row %}67 <td>{{ cell }}</td>68 {% endfor %}69 </tr>70 {% endfor %}71 </table>72 </div>73 </body>74</html>\75""")7677mako_template = MakoTemplate("""\78<!doctype html>79<html>80 <head>81 <title>${page_title|h}82 </head>83 <body>84 <div class="header">85 <h1>${page_title|h}</h1>86 </div>87 <ul class="navigation">88 % for href, caption in [('index.html', 'Index'), ('downloads.html', 'Downloads'), ('products.html', 'Products')]:89 <li><a href="${href|h}">${caption|h}</a></li>90 % endfor91 </ul>92 <div class="table">93 <table>94 % for row in table:95 <tr>96 % for cell in row:97 <td>${cell}</td>98 % endfor99 </tr>100 % endfor101 </table>102 </div>103 </body>104</html>\105""")106107context = {108 'page_title': 'mitsuhiko\'s benchmark',109 'table': [dict(a=1,b=2,c=3,d=4,e=5,f=6,g=7,h=8,i=9,j=10) for x in range(1000)]110}111112def test_jinja():113 jinja_template.render(context)114115def test_django():116 c = DjangoContext(context)117 c['navigation'] = [('index.html', 'Index'), ('downloads.html', 'Downloads'), ('products.html', 'Products')]118 django_template.render(c)119120def test_mako():121 mako_template.render(**context)122123124for test in 'jinja', 'mako', 'django':125 t = Timer(setup='from __main__ import test_%s as bench' % test,126 stmt='bench()')127 print '%-20s%.4fms' % (test, t.timeit(number=20) / 20)
added:examples/test_filter_and_linestatements.py+25 added
1from jinja2 import Environment234env = Environment(line_statement_prefix='%', variable_start_string="${", variable_end_string="}")5tmpl = env.from_string("""\6% macro foo()7 ${caller(42)}8% endmacro9<ul>10% for item in seq11 <li>${item}</li>12% endfor13</ul>14% call(var) foo()15 [${var}]16% endcall17% filter escape18 <hello world>19 % for item in [1, 2, 3]20 - ${item}21 % endfor22% endfilter23""")2425print tmpl.render(seq=range(10))
added:examples/test_loop_filter.py+12 added
1from jinja2 import Environment23tmpl = Environment().from_string("""\4<ul>5{% for item in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] if item % 2 == 0 %}6 <li>{{ loop.index }} / {{ loop.length }}: {{ item }}</li>7{% endfor %}8</ul>9{{ 1 if foo else 0 }}10""")1112print tmpl.render(foo=True)
added:examples/test.py+27 added
1from jinja2 import Environment2from jinja2.loaders import DictLoader34env = Environment(loader=DictLoader({5'child.html': u'''\6{% extends master_layout or 'master.html' %}7{% include helpers = 'helpers.html' %}8{% macro get_the_answer() %}42{% endmacro %}9{% title = 'Hello World' %}10{% block body %}11 {{ get_the_answer() }}12 {{ helpers.conspirate() }}13{% endblock %}14''',15'master.html': u'''\16<!doctype html>17<title>{{ title }}</title>18{% block body %}{% endblock %}19''',20'helpers.html': u'''\21{% macro conspirate() %}23{% endmacro %}22'''23}))242526tmpl = env.get_template("child.html")27print tmpl.render()
deleted:bench.py-127 removed
1from django.conf import settings2settings.configure()3from django.template import Template as DjangoTemplate, Context as DjangoContext4from jinja2 import Environment as JinjaEnvironment5from mako.template import Template as MakoTemplate6from timeit import Timer789jinja_template = JinjaEnvironment(10 line_statement_prefix='%',11 variable_start_string="${",12 variable_end_string="}"13).from_string("""\14<!doctype html>15<html>16 <head>17 <title>${page_title|e}18 </head>19 <body>20 <div class="header">21 <h1>${page_title|e}</h1>22 </div>23 <ul class="navigation">24 % for href, caption in [25 ('index.html', 'Index'),26 ('downloads.html', 'Downloads'),27 ('products.html', 'Products')28 ]29 <li><a href="${href|e}">${caption|e}</a></li>30 % endfor31 </ul>32 <div class="table">33 <table>34 % for row in table35 <tr>36 % for cell in row37 <td>${cell}</td>38 % endfor39 </tr>40 % endfor41 </table>42 </div>43 </body>44</html>\45""")4647django_template = DjangoTemplate("""\48<!doctype html>49<html>50 <head>51 <title>{{ page_title }}52 </head>53 <body>54 <div class="header">55 <h1>{{ page_title }}</h1>56 </div>57 <ul class="navigation">58 {% for href, caption in navigation %}59 <li><a href="{{ href }}">{{ caption }}</a></li>60 {% endfor %}61 </ul>62 <div class="table">63 <table>64 {% for row in table %}65 <tr>66 {% for cell in row %}67 <td>{{ cell }}</td>68 {% endfor %}69 </tr>70 {% endfor %}71 </table>72 </div>73 </body>74</html>\75""")7677mako_template = MakoTemplate("""\78<!doctype html>79<html>80 <head>81 <title>${page_title|h}82 </head>83 <body>84 <div class="header">85 <h1>${page_title|h}</h1>86 </div>87 <ul class="navigation">88 % for href, caption in [('index.html', 'Index'), ('downloads.html', 'Downloads'), ('products.html', 'Products')]:89 <li><a href="${href|h}">${caption|h}</a></li>90 % endfor91 </ul>92 <div class="table">93 <table>94 % for row in table:95 <tr>96 % for cell in row:97 <td>${cell}</td>98 % endfor99 </tr>100 % endfor101 </table>102 </div>103 </body>104</html>\105""")106107context = {108 'page_title': 'mitsuhiko\'s benchmark',109 'table': [dict(a=1,b=2,c=3,d=4,e=5,f=6,g=7,h=8,i=9,j=10) for x in range(1000)]110}111112def test_jinja():113 jinja_template.render(context)114115def test_django():116 c = DjangoContext(context)117 c['navigation'] = [('index.html', 'Index'), ('downloads.html', 'Downloads'), ('products.html', 'Products')]118 django_template.render(c)119120def test_mako():121 mako_template.render(**context)122123124for test in 'jinja', 'mako', 'django':125 t = Timer(setup='from __main__ import test_%s as bench' % test,126 stmt='bench()')127 print '%-20s%.4fms' % (test, t.timeit(number=20) / 20)
deleted:test_filter_and_linestatements.py-25 removed
1from jinja2 import Environment234env = Environment(line_statement_prefix='%', variable_start_string="${", variable_end_string="}")5tmpl = env.from_string("""\6% macro foo()7 ${caller(42)}8% endmacro9<ul>10% for item in seq11 <li>${item}</li>12% endfor13</ul>14% call(var) foo()15 [${var}]16% endcall17% filter escape18 <hello world>19 % for item in [1, 2, 3]20 - ${item}21 % endfor22% endfilter23""")2425print tmpl.render(seq=range(10))
deleted:test_loop_filter.py-12 removed
1from jinja2 import Environment23tmpl = Environment().from_string("""\4<ul>5{% for item in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] if item % 2 == 0 %}6 <li>{{ loop.index }} / {{ loop.length }}: {{ item }}</li>7{% endfor %}8</ul>9{{ 1 if foo else 0 }}10""")1112print tmpl.render(foo=True)
deleted:test.py-27 removed
1from jinja2 import Environment2from jinja2.loaders import DictLoader34env = Environment(loader=DictLoader({5'child.html': u'''\6{% extends master_layout or 'master.html' %}7{% include helpers = 'helpers.html' %}8{% macro get_the_answer() %}42{% endmacro %}9{% title = 'Hello World' %}10{% block body %}11 {{ get_the_answer() }}12 {{ helpers.conspirate() }}13{% endblock %}14''',15'master.html': u'''\16<!doctype html>17<title>{{ title }}</title>18{% block body %}{% endblock %}19''',20'helpers.html': u'''\21{% macro conspirate() %}23{% endmacro %}22'''23}))242526tmpl = env.get_template("child.html")27print tmpl.render()