aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--CHANGES5
-rw-r--r--test/alltests.py31
-rw-r--r--test/namespace.py717
-rw-r--r--test/sample_module_namespace.py7
-rw-r--r--test/test_ast.py (renamed from test/ast.py)2
-rw-r--r--test/test_babelplugin.py (renamed from test/babelplugin.py)2
-rw-r--r--test/test_cache.py (renamed from test/cache.py)3
-rw-r--r--test/test_call.py (renamed from test/call.py)2
-rw-r--r--test/test_decorators.py (renamed from test/decorators.py)3
-rw-r--r--test/test_def.py (renamed from test/def.py)3
-rw-r--r--test/test_exceptions.py (renamed from test/exceptions_.py)2
-rw-r--r--test/test_filters.py (renamed from test/filters.py)2
-rw-r--r--test/test_inheritance.py (renamed from test/inheritance.py)2
-rw-r--r--test/test_lexer.py (renamed from test/lexer.py)2
-rw-r--r--test/test_lookup.py (renamed from test/lookup.py)2
-rw-r--r--test/test_lru.py (renamed from test/lru.py)3
-rw-r--r--test/test_namespace.py719
-rw-r--r--test/test_pygen.py (renamed from test/pygen.py)3
-rw-r--r--test/test_template.py (renamed from test/template.py)14
-rw-r--r--test/test_tgplugin.py (renamed from test/tgplugin.py)3
20 files changed, 731 insertions, 796 deletions
diff --git a/CHANGES b/CHANGES
index 96fedad..66f47ed 100644
--- a/CHANGES
+++ b/CHANGES
@@ -1,6 +1,9 @@
0.3
- Python 2.3 support is dropped (who can resist
- @decorators)
+ @decorators) [ticket:123]
+
+- Unit tests now run with nose. [ticket:127]
+
0.2.6
diff --git a/test/alltests.py b/test/alltests.py
deleted file mode 100644
index ff758e8..0000000
--- a/test/alltests.py
+++ /dev/null
@@ -1,31 +0,0 @@
-import unittest
-
-def suite():
- modules_to_test = (
- 'lru',
- 'ast',
- 'pygen',
- 'lexer',
- 'template',
- 'lookup',
- 'def',
- 'decorators',
- 'namespace',
- 'filters',
- 'inheritance',
- 'call',
- 'cache',
- 'exceptions_',
- 'babelplugin',
- 'tgplugin'
- )
- alltests = unittest.TestSuite()
- for name in modules_to_test:
- mod = __import__(name)
- for token in name.split('.')[1:]:
- mod = getattr(mod, token)
- alltests.addTest(unittest.findTestCases(mod, suiteClass=None))
- return alltests
-
-if __name__ == '__main__':
- unittest.main(defaultTest='suite')
diff --git a/test/namespace.py b/test/namespace.py
deleted file mode 100644
index 7820bb6..0000000
--- a/test/namespace.py
+++ /dev/null
@@ -1,717 +0,0 @@
-from mako.template import Template
-from mako import lookup
-from util import flatten_result, result_lines
-import unittest
-
-class NamespaceTest(unittest.TestCase):
- def test_inline(self):
- t = Template("""
- <%namespace name="x">
- <%def name="a()">
- this is x a
- </%def>
- <%def name="b()">
- this is x b, and heres ${a()}
- </%def>
- </%namespace>
-
- ${x.a()}
-
- ${x.b()}
-""")
- assert flatten_result(t.render()) == "this is x a this is x b, and heres this is x a"
-
- def test_template(self):
- collection = lookup.TemplateLookup()
-
- collection.put_string('main.html', """
- <%namespace name="comp" file="defs.html"/>
-
- this is main. ${comp.def1("hi")}
- ${comp.def2("there")}
-""")
-
- collection.put_string('defs.html', """
- <%def name="def1(s)">
- def1: ${s}
- </%def>
-
- <%def name="def2(x)">
- def2: ${x}
- </%def>
-""")
-
- assert flatten_result(collection.get_template('main.html').render()) == "this is main. def1: hi def2: there"
-
- def test_module(self):
- collection = lookup.TemplateLookup()
-
- collection.put_string('main.html', """
- <%namespace name="comp" module="test_namespace"/>
-
- this is main. ${comp.foo1()}
- ${comp.foo2("hi")}
-""")
-
- assert flatten_result(collection.get_template('main.html').render()) == "this is main. this is foo1. this is foo2, x is hi"
-
- def test_module_2(self):
- collection = lookup.TemplateLookup()
-
- collection.put_string('main.html', """
- <%namespace name="comp" module="foo.test_ns"/>
-
- this is main. ${comp.foo1()}
- ${comp.foo2("hi")}
-""")
-
- assert flatten_result(collection.get_template('main.html').render()) == "this is main. this is foo1. this is foo2, x is hi"
-
- def test_module_imports(self):
- collection = lookup.TemplateLookup()
-
- collection.put_string('main.html', """
- <%namespace import="*" module="foo.test_ns"/>
-
- this is main. ${foo1()}
- ${foo2("hi")}
-""")
-
- assert flatten_result(collection.get_template('main.html').render()) == "this is main. this is foo1. this is foo2, x is hi"
-
- def test_module_imports_2(self):
- collection = lookup.TemplateLookup()
-
- collection.put_string('main.html', """
- <%namespace import="foo1, foo2" module="foo.test_ns"/>
-
- this is main. ${foo1()}
- ${foo2("hi")}
-""")
-
- assert flatten_result(collection.get_template('main.html').render()) == "this is main. this is foo1. this is foo2, x is hi"
-
- def test_context(self):
- """test that namespace callables get access to the current context"""
- collection = lookup.TemplateLookup()
-
- collection.put_string('main.html', """
- <%namespace name="comp" file="defs.html"/>
-
- this is main. ${comp.def1()}
- ${comp.def2("there")}
-""")
-
- collection.put_string('defs.html', """
- <%def name="def1()">
- def1: x is ${x}
- </%def>
-
- <%def name="def2(x)">
- def2: x is ${x}
- </%def>
-""")
-
- assert flatten_result(collection.get_template('main.html').render(x="context x")) == "this is main. def1: x is context x def2: x is there"
-
- def test_overload(self):
- collection = lookup.TemplateLookup()
-
- collection.put_string('main.html', """
- <%namespace name="comp" file="defs.html">
- <%def name="def1(x, y)">
- overridden def1 ${x}, ${y}
- </%def>
- </%namespace>
-
- this is main. ${comp.def1("hi", "there")}
- ${comp.def2("there")}
- """)
-
- collection.put_string('defs.html', """
- <%def name="def1(s)">
- def1: ${s}
- </%def>
-
- <%def name="def2(x)">
- def2: ${x}
- </%def>
- """)
-
- assert flatten_result(collection.get_template('main.html').render()) == "this is main. overridden def1 hi, there def2: there"
-
- def test_getattr(self):
- collection = lookup.TemplateLookup()
- collection.put_string("main.html", """
- <%namespace name="foo" file="ns.html"/>
- <%
- if hasattr(foo, 'lala'):
- foo.lala()
- if not hasattr(foo, 'hoho'):
- context.write('foo has no hoho.')
- %>
- """)
- collection.put_string("ns.html", """
- <%def name="lala()">this is lala.</%def>
- """)
- assert flatten_result(collection.get_template("main.html").render()) == "this is lala.foo has no hoho."
-
- def test_in_def(self):
- collection = lookup.TemplateLookup()
- collection.put_string("main.html", """
- <%namespace name="foo" file="ns.html"/>
-
- this is main. ${bar()}
- <%def name="bar()">
- this is bar, foo is ${foo.bar()}
- </%def>
- """)
-
- collection.put_string("ns.html", """
- <%def name="bar()">
- this is ns.html->bar
- </%def>
- """)
-
- assert result_lines(collection.get_template("main.html").render()) == [
- "this is main.",
- "this is bar, foo is" ,
- "this is ns.html->bar"
- ]
-
-
- def test_in_remote_def(self):
- collection = lookup.TemplateLookup()
- collection.put_string("main.html", """
- <%namespace name="foo" file="ns.html"/>
-
- this is main. ${bar()}
- <%def name="bar()">
- this is bar, foo is ${foo.bar()}
- </%def>
- """)
-
- collection.put_string("ns.html", """
- <%def name="bar()">
- this is ns.html->bar
- </%def>
- """)
-
- collection.put_string("index.html", """
- <%namespace name="main" file="main.html"/>
-
- this is index
- ${main.bar()}
- """)
-
- assert result_lines(collection.get_template("index.html").render()) == [
- "this is index",
- "this is bar, foo is" ,
- "this is ns.html->bar"
- ]
-
- def test_dont_pollute_self(self):
- # test that get_namespace() doesn't modify the original context
- # incompatibly
-
- collection = lookup.TemplateLookup()
- collection.put_string("base.html", """
-
- <%def name="foo()">
- <%
- foo = local.get_namespace("foo.html")
- %>
- </%def>
-
- name: ${self.name}
- name via bar: ${bar()}
-
- ${next.body()}
-
- name: ${self.name}
- name via bar: ${bar()}
- <%def name="bar()">
- ${self.name}
- </%def>
-
-
- """)
-
- collection.put_string("page.html", """
- <%inherit file="base.html"/>
-
- ${self.foo()}
-
- hello world
-
- """)
-
- collection.put_string("foo.html", """<%inherit file="base.html"/>""")
- assert result_lines(collection.get_template("page.html").render()) == [
- "name: self:page.html",
- "name via bar:",
- "self:page.html",
- "hello world",
- "name: self:page.html",
- "name via bar:",
- "self:page.html"
- ]
-
- def test_inheritance(self):
- """test namespace initialization in a base inherited template that doesnt otherwise access the namespace"""
- collection = lookup.TemplateLookup()
- collection.put_string("base.html", """
- <%namespace name="foo" file="ns.html" inheritable="True"/>
-
- ${next.body()}
-""")
- collection.put_string("ns.html", """
- <%def name="bar()">
- this is ns.html->bar
- </%def>
- """)
-
- collection.put_string("index.html", """
- <%inherit file="base.html"/>
-
- this is index
- ${self.foo.bar()}
- """)
-
- assert result_lines(collection.get_template("index.html").render()) == [
- "this is index",
- "this is ns.html->bar"
- ]
-
- def test_inheritance_two(self):
- collection = lookup.TemplateLookup()
- collection.put_string("base.html", """
- <%def name="foo()">
- base.foo
- </%def>
-
- <%def name="bat()">
- base.bat
- </%def>
-""")
- collection.put_string("lib.html", """
- <%inherit file="base.html"/>
- <%def name="bar()">
- lib.bar
- ${parent.foo()}
- ${self.foo()}
- ${parent.bat()}
- ${self.bat()}
- </%def>
-
- <%def name="foo()">
- lib.foo
- </%def>
-
- """)
-
- collection.put_string("front.html", """
- <%namespace name="lib" file="lib.html"/>
- ${lib.bar()}
- """)
-
- assert result_lines(collection.get_template("front.html").render()) == ['lib.bar', 'base.foo', 'lib.foo', 'base.bat', 'base.bat']
-
- def test_attr(self):
- l = lookup.TemplateLookup()
-
- l.put_string("foo.html", """
- <%!
- foofoo = "foo foo"
- onlyfoo = "only foo"
- %>
- <%inherit file="base.html"/>
- <%def name="setup()">
- <%
- self.attr.foolala = "foo lala"
- %>
- </%def>
- ${self.attr.basefoo}
- ${self.attr.foofoo}
- ${self.attr.onlyfoo}
- ${self.attr.lala}
- ${self.attr.foolala}
- """)
-
- l.put_string("base.html", """
- <%!
- basefoo = "base foo 1"
- foofoo = "base foo 2"
- %>
- <%
- self.attr.lala = "base lala"
- %>
-
- ${self.attr.basefoo}
- ${self.attr.foofoo}
- ${self.attr.onlyfoo}
- ${self.attr.lala}
- ${self.setup()}
- ${self.attr.foolala}
- body
- ${self.body()}
- """)
-
- assert result_lines(l.get_template("foo.html").render()) == [
- "base foo 1",
- "foo foo",
- "only foo",
- "base lala",
- "foo lala",
- "body",
- "base foo 1",
- "foo foo",
- "only foo",
- "base lala",
- "foo lala",
- ]
-
- def test_attr_raise(self):
- l = lookup.TemplateLookup()
-
- l.put_string("foo.html", """
- <%def name="foo()">
- </%def>
- """)
-
- l.put_string("bar.html", """
- <%namespace name="foo" file="foo.html"/>
-
- ${foo.notfoo()}
- """)
-
- self.assertRaises(AttributeError, l.get_template("bar.html").render)
-
- def test_custom_tag_1(self):
- template = Template("""
-
- <%def name="foo(x, y)">
- foo: ${x} ${y}
- </%def>
-
- <%self:foo x="5" y="${7+8}"/>
- """)
- assert result_lines(template.render()) == ['foo: 5 15']
-
- def test_custom_tag_2(self):
- collection = lookup.TemplateLookup()
- collection.put_string("base.html", """
- <%def name="foo(x, y)">
- foo: ${x} ${y}
- </%def>
-
- <%def name="bat(g)"><%
- return "the bat! %s" % g
- %></%def>
-
- <%def name="bar(x)">
- ${caller.body(z=x)}
- </%def>
- """)
-
- collection.put_string("index.html", """
- <%namespace name="myns" file="base.html"/>
-
- <%myns:foo x="${'some x'}" y="some y"/>
-
- <%myns:bar x="${myns.bat(10)}" args="z">
- record: ${z}
- </%myns:bar>
-
- """)
-
- assert result_lines(collection.get_template("index.html").render()) == [
- 'foo: some x some y',
- 'record: the bat! 10'
- ]
-
- def test_custom_tag_3(self):
- collection = lookup.TemplateLookup()
- collection.put_string("base.html", """
- <%namespace name="foo" file="ns.html" inheritable="True"/>
-
- ${next.body()}
- """)
- collection.put_string("ns.html", """
- <%def name="bar()">
- this is ns.html->bar
- caller body: ${caller.body()}
- </%def>
- """)
-
- collection.put_string("index.html", """
- <%inherit file="base.html"/>
-
- this is index
- <%self.foo:bar>
- call body
- </%self.foo:bar>
- """)
-
- assert result_lines(collection.get_template("index.html").render()) == [
- "this is index",
- "this is ns.html->bar",
- "caller body:",
- "call body"
- ]
-
- def test_custom_tag_case_sensitive(self):
- t = Template("""
- <%def name="renderPanel()">
- panel ${caller.body()}
- </%def>
-
- <%def name="renderTablePanel()">
- <%self:renderPanel>
- hi
- </%self:renderPanel>
- </%def>
-
- <%self:renderTablePanel/>
- """)
- assert result_lines(t.render()) == ['panel', 'hi']
-
-
- def test_expr_grouping(self):
- """test that parenthesis are placed around string-embedded expressions."""
-
- template = Template("""
- <%def name="bar(x, y)">
- ${x}
- ${y}
- </%def>
-
- <%self:bar x=" ${foo} " y="x${g and '1' or '2'}y"/>
- """, input_encoding='utf-8')
-
- # the concat has to come out as "x + (g and '1' or '2') + y"
- assert result_lines(template.render(foo='this is foo', g=False)) == [
- "this is foo",
- "x2y"
- ]
-
-
- def test_ccall(self):
- collection = lookup.TemplateLookup()
- collection.put_string("base.html", """
- <%namespace name="foo" file="ns.html" inheritable="True"/>
-
- ${next.body()}
- """)
- collection.put_string("ns.html", """
- <%def name="bar()">
- this is ns.html->bar
- caller body: ${caller.body()}
- </%def>
- """)
-
- collection.put_string("index.html", """
- <%inherit file="base.html"/>
-
- this is index
- <%call expr="self.foo.bar()">
- call body
- </%call>
- """)
-
- assert result_lines(collection.get_template("index.html").render()) == [
- "this is index",
- "this is ns.html->bar",
- "caller body:",
- "call body"
- ]
-
- def test_ccall_2(self):
- collection = lookup.TemplateLookup()
- collection.put_string("base.html", """
- <%namespace name="foo" file="ns1.html" inheritable="True"/>
-
- ${next.body()}
- """)
- collection.put_string("ns1.html", """
- <%namespace name="foo2" file="ns2.html"/>
- <%def name="bar()">
- <%call expr="foo2.ns2_bar()">
- this is ns1.html->bar
- caller body: ${caller.body()}
- </%call>
- </%def>
- """)
-
- collection.put_string("ns2.html", """
- <%def name="ns2_bar()">
- this is ns2.html->bar
- caller body: ${caller.body()}
- </%def>
- """)
-
- collection.put_string("index.html", """
- <%inherit file="base.html"/>
-
- this is index
- <%call expr="self.foo.bar()">
- call body
- </%call>
- """)
-
- assert result_lines(collection.get_template("index.html").render()) == [
- "this is index",
- "this is ns2.html->bar",
- "caller body:",
- "this is ns1.html->bar",
- "caller body:",
- "call body"
- ]
-
- def test_import(self):
- collection = lookup.TemplateLookup()
- collection.put_string("functions.html","""
- <%def name="foo()">
- this is foo
- </%def>
-
- <%def name="bar()">
- this is bar
- </%def>
-
- <%def name="lala()">
- this is lala
- </%def>
- """)
-
- collection.put_string("func2.html", """
- <%def name="a()">
- this is a
- </%def>
- <%def name="b()">
- this is b
- </%def>
- """)
- collection.put_string("index.html", """
- <%namespace file="functions.html" import="*"/>
- <%namespace file="func2.html" import="a, b"/>
- ${foo()}
- ${bar()}
- ${lala()}
- ${a()}
- ${b()}
- ${x}
- """)
-
- assert result_lines(collection.get_template("index.html").render(bar="this is bar", x="this is x")) == [
- "this is foo",
- "this is bar",
- "this is lala",
- "this is a",
- "this is b",
- "this is x"
- ]
-
- def test_import_calledfromdef(self):
- l = lookup.TemplateLookup()
- l.put_string("a", """
- <%def name="table()">
- im table
- </%def>
- """)
-
- l.put_string("b","""
- <%namespace file="a" import="table"/>
-
- <%
- def table2():
- table()
- return ""
- %>
-
- ${table2()}
- """)
-
- t = l.get_template("b")
- assert flatten_result(t.render()) == "im table"
-
- def test_closure_import(self):
- collection = lookup.TemplateLookup()
- collection.put_string("functions.html","""
- <%def name="foo()">
- this is foo
- </%def>
-
- <%def name="bar()">
- this is bar
- </%def>
- """)
-
- collection.put_string("index.html", """
- <%namespace file="functions.html" import="*"/>
- <%def name="cl1()">
- ${foo()}
- </%def>
-
- <%def name="cl2()">
- ${bar()}
- </%def>
-
- ${cl1()}
- ${cl2()}
- """)
- assert result_lines(collection.get_template("index.html").render(bar="this is bar", x="this is x")) == [
- "this is foo",
- "this is bar",
- ]
-
- def test_import_local(self):
- t = Template("""
- <%namespace import="*">
- <%def name="foo()">
- this is foo
- </%def>
- </%namespace>
-
- ${foo()}
-
- """)
- assert flatten_result(t.render()) == "this is foo"
-
- def test_ccall_import(self):
- collection = lookup.TemplateLookup()
- collection.put_string("functions.html","""
- <%def name="foo()">
- this is foo
- </%def>
-
- <%def name="bar()">
- this is bar.
- ${caller.body()}
- ${caller.lala()}
- </%def>
- """)
-
- collection.put_string("index.html", """
- <%namespace name="func" file="functions.html" import="*"/>
- <%call expr="bar()">
- this is index embedded
- foo is ${foo()}
- <%def name="lala()">
- this is lala ${foo()}
- </%def>
- </%call>
- """)
- #print collection.get_template("index.html").code
- #print collection.get_template("functions.html").code
- assert result_lines(collection.get_template("index.html").render()) == [
- "this is bar.",
- "this is index embedded",
- "foo is",
- "this is foo",
- "this is lala",
- "this is foo"
- ]
-
-if __name__ == '__main__':
- unittest.main()
diff --git a/test/sample_module_namespace.py b/test/sample_module_namespace.py
new file mode 100644
index 0000000..084fe97
--- /dev/null
+++ b/test/sample_module_namespace.py
@@ -0,0 +1,7 @@
+def foo1(context):
+ context.write("this is foo1.")
+ return ''
+
+def foo2(context, x):
+ context.write("this is foo2, x is " + x)
+ return '' \ No newline at end of file
diff --git a/test/ast.py b/test/test_ast.py
index f60be65..30da4f1 100644
--- a/test/ast.py
+++ b/test/test_ast.py
@@ -212,7 +212,5 @@ import x as bar
#print code, newcode
assert(eval(code, local_dict)) == eval(newcode, local_dict), "%s != %s" % (code, newcode)
-if __name__ == '__main__':
- unittest.main()
diff --git a/test/babelplugin.py b/test/test_babelplugin.py
index af3cb63..1a7f9d0 100644
--- a/test/babelplugin.py
+++ b/test/test_babelplugin.py
@@ -39,5 +39,3 @@ except ImportError:
warnings.warn('babel not installed: skipping babelplugin test',
RuntimeWarning, 1)
-if __name__ == '__main__':
- unittest.main()
diff --git a/test/cache.py b/test/test_cache.py
index 1049b30..ced515e 100644
--- a/test/cache.py
+++ b/test/test_cache.py
@@ -403,6 +403,3 @@ class CacheTest(unittest.TestCase):
m = MockCache(template.module._template_cache)
template.module._template_cache = m
return m
-
-if __name__ == '__main__':
- unittest.main()
diff --git a/test/call.py b/test/test_call.py
index 0c89694..164c899 100644
--- a/test/call.py
+++ b/test/test_call.py
@@ -427,5 +427,3 @@ class SelfCacheTest(unittest.TestCase):
"this is foo"
]
-if __name__ == '__main__':
- unittest.main()
diff --git a/test/decorators.py b/test/test_decorators.py
index 2bd9e0d..62ba360 100644
--- a/test/decorators.py
+++ b/test/test_decorators.py
@@ -108,6 +108,3 @@ class DecoratorTest(unittest.TestCase):
assert flatten_result(template.render()) == "function bar this is bar"
-
-if __name__ == '__main__':
- unittest.main()
diff --git a/test/def.py b/test/test_def.py
index 69ce70e..6cb1fdf 100644
--- a/test/def.py
+++ b/test/test_def.py
@@ -536,6 +536,3 @@ class ExceptionTest(unittest.TestCase):
""", error_handler=handle)
assert template.render().strip() == """error message is this is a test"""
-
-if __name__ == '__main__':
- unittest.main()
diff --git a/test/exceptions_.py b/test/test_exceptions.py
index 2cb5d1d..52c9544 100644
--- a/test/exceptions_.py
+++ b/test/test_exceptions.py
@@ -103,5 +103,3 @@ ${u'привет' + foobar}
assert '''<div class="highlight">2 ${u\'&#x43F;&#x440;&#x438;&#x432;&#x435;&#x442;\' + foobar}</div>''' in result_lines(l.get_template("foo.html").render())
-if __name__ == '__main__':
- unittest.main()
diff --git a/test/filters.py b/test/test_filters.py
index 4fdd342..e7a6725 100644
--- a/test/filters.py
+++ b/test/test_filters.py
@@ -264,5 +264,3 @@ class BufferTest(unittest.TestCase):
#print t.render()
assert flatten_result(t.render()) == "this is foo. body: ccall body"
-if __name__ == '__main__':
- unittest.main() \ No newline at end of file
diff --git a/test/inheritance.py b/test/test_inheritance.py
index 8cd4165..c9c6990 100644
--- a/test/inheritance.py
+++ b/test/test_inheritance.py
@@ -339,5 +339,3 @@ ${next.body()}
"Oh yea!"
]
-if __name__ == '__main__':
- unittest.main()
diff --git a/test/lexer.py b/test/test_lexer.py
index 17a2ba2..21c18a2 100644
--- a/test/lexer.py
+++ b/test/test_lexer.py
@@ -472,5 +472,3 @@ hi
nodes = Lexer(template, preprocessor=preproc).parse()
assert repr(nodes) == r"""TemplateNode({}, [Text(u'\n hi\n', (1, 1)), Comment(u'old style comment', (3, 1)), Comment(u'another comment', (4, 1))])"""
-if __name__ == '__main__':
- unittest.main()
diff --git a/test/lookup.py b/test/test_lookup.py
index fddbf84..81bb7ec 100644
--- a/test/lookup.py
+++ b/test/test_lookup.py
@@ -64,5 +64,3 @@ class LookupTest(unittest.TestCase):
tl._uri_cache[('foo', 'bar')] = '/some/path'
assert tl._uri_cache[('foo', 'bar')] == '/some/path'
-if __name__ == '__main__':
- unittest.main() \ No newline at end of file
diff --git a/test/lru.py b/test/test_lru.py
index 24e92ad..d75b47a 100644
--- a/test/lru.py
+++ b/test/test_lru.py
@@ -105,5 +105,4 @@ class LRUTest(unittest.TestCase):
assert hotzone_avg > total_avg * 5 > control_avg * 5
-if __name__ == "__main__":
- unittest.main()
+
diff --git a/test/test_namespace.py b/test/test_namespace.py
index 084fe97..6351caa 100644
--- a/test/test_namespace.py
+++ b/test/test_namespace.py
@@ -1,7 +1,714 @@
-def foo1(context):
- context.write("this is foo1.")
- return ''
+from mako.template import Template
+from mako import lookup
+from util import flatten_result, result_lines
+import unittest
+
+class NamespaceTest(unittest.TestCase):
+ def test_inline(self):
+ t = Template("""
+ <%namespace name="x">
+ <%def name="a()">
+ this is x a
+ </%def>
+ <%def name="b()">
+ this is x b, and heres ${a()}
+ </%def>
+ </%namespace>
+
+ ${x.a()}
+
+ ${x.b()}
+""")
+ assert flatten_result(t.render()) == "this is x a this is x b, and heres this is x a"
+
+ def test_template(self):
+ collection = lookup.TemplateLookup()
+
+ collection.put_string('main.html', """
+ <%namespace name="comp" file="defs.html"/>
+
+ this is main. ${comp.def1("hi")}
+ ${comp.def2("there")}
+""")
+
+ collection.put_string('defs.html', """
+ <%def name="def1(s)">
+ def1: ${s}
+ </%def>
+
+ <%def name="def2(x)">
+ def2: ${x}
+ </%def>
+""")
+
+ assert flatten_result(collection.get_template('main.html').render()) == "this is main. def1: hi def2: there"
-def foo2(context, x):
- context.write("this is foo2, x is " + x)
- return '' \ No newline at end of file
+ def test_module(self):
+ collection = lookup.TemplateLookup()
+
+ collection.put_string('main.html', """
+ <%namespace name="comp" module="test.sample_module_namespace"/>
+
+ this is main. ${comp.foo1()}
+ ${comp.foo2("hi")}
+""")
+
+ assert flatten_result(collection.get_template('main.html').render()) == "this is main. this is foo1. this is foo2, x is hi"
+
+ def test_module_2(self):
+ collection = lookup.TemplateLookup()
+
+ collection.put_string('main.html', """
+ <%namespace name="comp" module="test.foo.test_ns"/>
+
+ this is main. ${comp.foo1()}
+ ${comp.foo2("hi")}
+""")
+
+ assert flatten_result(collection.get_template('main.html').render()) == "this is main. this is foo1. this is foo2, x is hi"
+
+ def test_module_imports(self):
+ collection = lookup.TemplateLookup()
+
+ collection.put_string('main.html', """
+ <%namespace import="*" module="test.foo.test_ns"/>
+
+ this is main. ${foo1()}
+ ${foo2("hi")}
+""")
+
+ assert flatten_result(collection.get_template('main.html').render()) == "this is main. this is foo1. this is foo2, x is hi"
+
+ def test_module_imports_2(self):
+ collection = lookup.TemplateLookup()
+
+ collection.put_string('main.html', """
+ <%namespace import="foo1, foo2" module="test.foo.test_ns"/>
+
+ this is main. ${foo1()}
+ ${foo2("hi")}
+""")
+
+ assert flatten_result(collection.get_template('main.html').render()) == "this is main. this is foo1. this is foo2, x is hi"
+
+ def test_context(self):
+ """test that namespace callables get access to the current context"""
+ collection = lookup.TemplateLookup()
+
+ collection.put_string('main.html', """
+ <%namespace name="comp" file="defs.html"/>
+
+ this is main. ${comp.def1()}
+ ${comp.def2("there")}
+""")
+
+ collection.put_string('defs.html', """
+ <%def name="def1()">
+ def1: x is ${x}
+ </%def>
+
+ <%def name="def2(x)">
+ def2: x is ${x}
+ </%def>
+""")
+
+ assert flatten_result(collection.get_template('main.html').render(x="context x")) == "this is main. def1: x is context x def2: x is there"
+
+ def test_overload(self):
+ collection = lookup.TemplateLookup()
+
+ collection.put_string('main.html', """
+ <%namespace name="comp" file="defs.html">
+ <%def name="def1(x, y)">
+ overridden def1 ${x}, ${y}
+ </%def>
+ </%namespace>
+
+ this is main. ${comp.def1("hi", "there")}
+ ${comp.def2("there")}
+ """)
+
+ collection.put_string('defs.html', """
+ <%def name="def1(s)">
+ def1: ${s}
+ </%def>
+
+ <%def name="def2(x)">
+ def2: ${x}
+ </%def>
+ """)
+
+ assert flatten_result(collection.get_template('main.html').render()) == "this is main. overridden def1 hi, there def2: there"
+
+ def test_getattr(self):
+ collection = lookup.TemplateLookup()
+ collection.put_string("main.html", """
+ <%namespace name="foo" file="ns.html"/>
+ <%
+ if hasattr(foo, 'lala'):
+ foo.lala()
+ if not hasattr(foo, 'hoho'):
+ context.write('foo has no hoho.')
+ %>
+ """)
+ collection.put_string("ns.html", """
+ <%def name="lala()">this is lala.</%def>
+ """)
+ assert flatten_result(collection.get_template("main.html").render()) == "this is lala.foo has no hoho."
+
+ def test_in_def(self):
+ collection = lookup.TemplateLookup()
+ collection.put_string("main.html", """
+ <%namespace name="foo" file="ns.html"/>
+
+ this is main. ${bar()}
+ <%def name="bar()">
+ this is bar, foo is ${foo.bar()}
+ </%def>
+ """)
+
+ collection.put_string("ns.html", """
+ <%def name="bar()">
+ this is ns.html->bar
+ </%def>
+ """)
+
+ assert result_lines(collection.get_template("main.html").render()) == [
+ "this is main.",
+ "this is bar, foo is" ,
+ "this is ns.html->bar"
+ ]
+
+
+ def test_in_remote_def(self):
+ collection = lookup.TemplateLookup()
+ collection.put_string("main.html", """
+ <%namespace name="foo" file="ns.html"/>
+
+ this is main. ${bar()}
+ <%def name="bar()">
+ this is bar, foo is ${foo.bar()}
+ </%def>
+ """)
+
+ collection.put_string("ns.html", """
+ <%def name="bar()">
+ this is ns.html->bar
+ </%def>
+ """)
+
+ collection.put_string("index.html", """
+ <%namespace name="main" file="main.html"/>
+
+ this is index
+ ${main.bar()}
+ """)
+
+ assert result_lines(collection.get_template("index.html").render()) == [
+ "this is index",
+ "this is bar, foo is" ,
+ "this is ns.html->bar"
+ ]
+
+ def test_dont_pollute_self(self):
+ # test that get_namespace() doesn't modify the original context
+ # incompatibly
+
+ collection = lookup.TemplateLookup()
+ collection.put_string("base.html", """
+
+ <%def name="foo()">
+ <%
+ foo = local.get_namespace("foo.html")
+ %>
+ </%def>
+
+ name: ${self.name}
+ name via bar: ${bar()}
+
+ ${next.body()}
+
+ name: ${self.name}
+ name via bar: ${bar()}
+ <%def name="bar()">
+ ${self.name}
+ </%def>
+
+
+ """)
+
+ collection.put_string("page.html", """
+ <%inherit file="base.html"/>
+
+ ${self.foo()}
+
+ hello world
+
+ """)
+
+ collection.put_string("foo.html", """<%inherit file="base.html"/>""")
+ assert result_lines(collection.get_template("page.html").render()) == [
+ "name: self:page.html",
+ "name via bar:",
+ "self:page.html",
+ "hello world",
+ "name: self:page.html",
+ "name via bar:",
+ "self:page.html"
+ ]
+
+ def test_inheritance(self):
+ """test namespace initialization in a base inherited template that doesnt otherwise access the namespace"""
+ collection = lookup.TemplateLookup()
+ collection.put_string("base.html", """
+ <%namespace name="foo" file="ns.html" inheritable="True"/>
+
+ ${next.body()}
+""")
+ collection.put_string("ns.html", """
+ <%def name="bar()">
+ this is ns.html->bar
+ </%def>
+ """)
+
+ collection.put_string("index.html", """
+ <%inherit file="base.html"/>
+
+ this is index
+ ${self.foo.bar()}
+ """)
+
+ assert result_lines(collection.get_template("index.html").render()) == [
+ "this is index",
+ "this is ns.html->bar"
+ ]
+
+ def test_inheritance_two(self):
+ collection = lookup.TemplateLookup()
+ collection.put_string("base.html", """
+ <%def name="foo()">
+ base.foo
+ </%def>
+
+ <%def name="bat()">
+ base.bat
+ </%def>
+""")
+ collection.put_string("lib.html", """
+ <%inherit file="base.html"/>
+ <%def name="bar()">
+ lib.bar
+ ${parent.foo()}
+ ${self.foo()}
+ ${parent.bat()}
+ ${self.bat()}
+ </%def>
+
+ <%def name="foo()">
+ lib.foo
+ </%def>
+
+ """)
+
+ collection.put_string("front.html", """
+ <%namespace name="lib" file="lib.html"/>
+ ${lib.bar()}
+ """)
+
+ assert result_lines(collection.get_template("front.html").render()) == ['lib.bar', 'base.foo', 'lib.foo', 'base.bat', 'base.bat']
+
+ def test_attr(self):
+ l = lookup.TemplateLookup()
+
+ l.put_string("foo.html", """
+ <%!
+ foofoo = "foo foo"
+ onlyfoo = "only foo"
+ %>
+ <%inherit file="base.html"/>
+ <%def name="setup()">
+ <%
+ self.attr.foolala = "foo lala"
+ %>
+ </%def>
+ ${self.attr.basefoo}
+ ${self.attr.foofoo}
+ ${self.attr.onlyfoo}
+ ${self.attr.lala}
+ ${self.attr.foolala}
+ """)
+
+ l.put_string("base.html", """
+ <%!
+ basefoo = "base foo 1"
+ foofoo = "base foo 2"
+ %>
+ <%
+ self.attr.lala = "base lala"
+ %>
+
+ ${self.attr.basefoo}
+ ${self.attr.foofoo}
+ ${self.attr.onlyfoo}
+ ${self.attr.lala}
+ ${self.setup()}
+ ${self.attr.foolala}
+ body
+ ${self.body()}
+ """)
+
+ assert result_lines(l.get_template("foo.html").render()) == [
+ "base foo 1",
+ "foo foo",
+ "only foo",
+ "base lala",
+ "foo lala",
+ "body",
+ "base foo 1",
+ "foo foo",
+ "only foo",
+ "base lala",
+ "foo lala",
+ ]
+
+ def test_attr_raise(self):
+ l = lookup.TemplateLookup()
+
+ l.put_string("foo.html", """
+ <%def name="foo()">
+ </%def>
+ """)
+
+ l.put_string("bar.html", """
+ <%namespace name="foo" file="foo.html"/>
+
+ ${foo.notfoo()}
+ """)
+
+ self.assertRaises(AttributeError, l.get_template("bar.html").render)
+
+ def test_custom_tag_1(self):
+ template = Template("""
+
+ <%def name="foo(x, y)">
+ foo: ${x} ${y}
+ </%def>
+
+ <%self:foo x="5" y="${7+8}"/>
+ """)
+ assert result_lines(template.render()) == ['foo: 5 15']
+
+ def test_custom_tag_2(self):
+ collection = lookup.TemplateLookup()
+ collection.put_string("base.html", """
+ <%def name="foo(x, y)">
+ foo: ${x} ${y}
+ </%def>
+
+ <%def name="bat(g)"><%
+ return "the bat! %s" % g
+ %></%def>
+
+ <%def name="bar(x)">
+ ${caller.body(z=x)}
+ </%def>
+ """)
+
+ collection.put_string("index.html", """
+ <%namespace name="myns" file="base.html"/>
+
+ <%myns:foo x="${'some x'}" y="some y"/>
+
+ <%myns:bar x="${myns.bat(10)}" args="z">
+ record: ${z}
+ </%myns:bar>
+
+ """)
+
+ assert result_lines(collection.get_template("index.html").render()) == [
+ 'foo: some x some y',
+ 'record: the bat! 10'
+ ]
+
+ def test_custom_tag_3(self):
+ collection = lookup.TemplateLookup()
+ collection.put_string("base.html", """
+ <%namespace name="foo" file="ns.html" inheritable="True"/>
+
+ ${next.body()}
+ """)
+ collection.put_string("ns.html", """
+ <%def name="bar()">
+ this is ns.html->bar
+ caller body: ${caller.body()}
+ </%def>
+ """)
+
+ collection.put_string("index.html", """
+ <%inherit file="base.html"/>
+
+ this is index
+ <%self.foo:bar>
+ call body
+ </%self.foo:bar>
+ """)
+
+ assert result_lines(collection.get_template("index.html").render()) == [
+ "this is index",
+ "this is ns.html->bar",
+ "caller body:",
+ "call body"
+ ]
+
+ def test_custom_tag_case_sensitive(self):
+ t = Template("""
+ <%def name="renderPanel()">
+ panel ${caller.body()}
+ </%def>
+
+ <%def name="renderTablePanel()">
+ <%self:renderPanel>
+ hi
+ </%self:renderPanel>
+ </%def>
+
+ <%self:renderTablePanel/>
+ """)
+ assert result_lines(t.render()) == ['panel', 'hi']
+
+
+ def test_expr_grouping(self):
+ """test that parenthesis are placed around string-embedded expressions."""
+
+ template = Template("""
+ <%def name="bar(x, y)">
+ ${x}
+ ${y}
+ </%def>
+
+ <%self:bar x=" ${foo} " y="x${g and '1' or '2'}y"/>
+ """, input_encoding='utf-8')
+
+ # the concat has to come out as "x + (g and '1' or '2') + y"
+ assert result_lines(template.render(foo='this is foo', g=False)) == [
+ "this is foo",
+ "x2y"
+ ]
+
+
+ def test_ccall(self):
+ collection = lookup.TemplateLookup()
+ collection.put_string("base.html", """
+ <%namespace name="foo" file="ns.html" inheritable="True"/>
+
+ ${next.body()}
+ """)
+ collection.put_string("ns.html", """
+ <%def name="bar()">
+ this is ns.html->bar
+ caller body: ${caller.body()}
+ </%def>
+ """)
+
+ collection.put_string("index.html", """
+ <%inherit file="base.html"/>
+
+ this is index
+ <%call expr="self.foo.bar()">
+ call body
+ </%call>
+ """)
+
+ assert result_lines(collection.get_template("index.html").render()) == [
+ "this is index",
+ "this is ns.html->bar",
+ "caller body:",
+ "call body"
+ ]
+
+ def test_ccall_2(self):
+ collection = lookup.TemplateLookup()
+ collection.put_string("base.html", """
+ <%namespace name="foo" file="ns1.html" inheritable="True"/>
+
+ ${next.body()}
+ """)
+ collection.put_string("ns1.html", """
+ <%namespace name="foo2" file="ns2.html"/>
+ <%def name="bar()">
+ <%call expr="foo2.ns2_bar()">
+ this is ns1.html->bar
+ caller body: ${caller.body()}
+ </%call>
+ </%def>
+ """)
+
+ collection.put_string("ns2.html", """
+ <%def name="ns2_bar()">
+ this is ns2.html->bar
+ caller body: ${caller.body()}
+ </%def>
+ """)
+
+ collection.put_string("index.html", """
+ <%inherit file="base.html"/>
+
+ this is index
+ <%call expr="self.foo.bar()">
+ call body
+ </%call>
+ """)
+
+ assert result_lines(collection.get_template("index.html").render()) == [
+ "this is index",
+ "this is ns2.html->bar",
+ "caller body:",
+ "this is ns1.html->bar",
+ "caller body:",
+ "call body"
+ ]
+
+ def test_import(self):
+ collection = lookup.TemplateLookup()
+ collection.put_string("functions.html","""
+ <%def name="foo()">
+ this is foo
+ </%def>
+
+ <%def name="bar()">
+ this is bar
+ </%def>
+
+ <%def name="lala()">
+ this is lala
+ </%def>
+ """)
+
+ collection.put_string("func2.html", """
+ <%def name="a()">
+ this is a
+ </%def>
+ <%def name="b()">
+ this is b
+ </%def>
+ """)
+ collection.put_string("index.html", """
+ <%namespace file="functions.html" import="*"/>
+ <%namespace file="func2.html" import="a, b"/>
+ ${foo()}
+ ${bar()}
+ ${lala()}
+ ${a()}
+ ${b()}
+ ${x}
+ """)
+
+ assert result_lines(collection.get_template("index.html").render(bar="this is bar", x="this is x")) == [
+ "this is foo",
+ "this is bar",
+ "this is lala",
+ "this is a",
+ "this is b",
+ "this is x"
+ ]
+
+ def test_import_calledfromdef(self):
+ l = lookup.TemplateLookup()
+ l.put_string("a", """
+ <%def name="table()">
+ im table
+ </%def>
+ """)
+
+ l.put_string("b","""
+ <%namespace file="a" import="table"/>
+
+ <%
+ def table2():
+ table()
+ return ""
+ %>
+
+ ${table2()}
+ """)
+
+ t = l.get_template("b")
+ assert flatten_result(t.render()) == "im table"
+
+ def test_closure_import(self):
+ collection = lookup.TemplateLookup()
+ collection.put_string("functions.html","""
+ <%def name="foo()">
+ this is foo
+ </%def>
+
+ <%def name="bar()">
+ this is bar
+ </%def>
+ """)
+
+ collection.put_string("index.html", """
+ <%namespace file="functions.html" import="*"/>
+ <%def name="cl1()">
+ ${foo()}
+ </%def>
+
+ <%def name="cl2()">
+ ${bar()}
+ </%def>
+
+ ${cl1()}
+ ${cl2()}
+ """)
+ assert result_lines(collection.get_template("index.html").render(bar="this is bar", x="this is x")) == [
+ "this is foo",
+ "this is bar",
+ ]
+
+ def test_import_local(self):
+ t = Template("""
+ <%namespace import="*">
+ <%def name="foo()">
+ this is foo
+ </%def>
+ </%namespace>
+
+ ${foo()}
+
+ """)
+ assert flatten_result(t.render()) == "this is foo"
+
+ def test_ccall_import(self):
+ collection = lookup.TemplateLookup()
+ collection.put_string("functions.html","""
+ <%def name="foo()">
+ this is foo
+ </%def>
+
+ <%def name="bar()">
+ this is bar.
+ ${caller.body()}
+ ${caller.lala()}
+ </%def>
+ """)
+
+ collection.put_string("index.html", """
+ <%namespace name="func" file="functions.html" import="*"/>
+ <%call expr="bar()">
+ this is index embedded
+ foo is ${foo()}
+ <%def name="lala()">
+ this is lala ${foo()}
+ </%def>
+ </%call>
+ """)
+ #print collection.get_template("index.html").code
+ #print collection.get_template("functions.html").code
+ assert result_lines(collection.get_template("index.html").render()) == [
+ "this is bar.",
+ "this is index embedded",
+ "foo is",
+ "this is foo",
+ "this is lala",
+ "this is foo"
+ ]
diff --git a/test/pygen.py b/test/test_pygen.py
index 95444dd..181140f 100644
--- a/test/pygen.py
+++ b/test/test_pygen.py
@@ -250,6 +250,3 @@ asdkfjnads kfajns '''
if x:
print y
"""
-
-if __name__ == '__main__':
- unittest.main()
diff --git a/test/template.py b/test/test_template.py
index 3ef0794..a5755d0 100644
--- a/test/template.py
+++ b/test/test_template.py
@@ -363,7 +363,7 @@ class GlobalsTest(unittest.TestCase):
assert t.render().strip() == "y is hi"
class RichTracebackTest(unittest.TestCase):
- def do_test_traceback(self, utf8, memory, syntax):
+ def _do_test_traceback(self, utf8, memory, syntax):
if memory:
if syntax:
source = u'## coding: utf-8\n<% print "m’a réveillé. Elle disait: « S’il vous plaît… dessine-moi un mouton! » %>'
@@ -398,14 +398,15 @@ class RichTracebackTest(unittest.TestCase):
for utf8 in (True, False):
for memory in (True, False):
for syntax in (True, False):
- def do_test(self):
- self.do_test_traceback(utf8, memory, syntax)
+ def _do_test(self):
+ self._do_test_traceback(utf8, memory, syntax)
name = 'test_%s_%s_%s' % (utf8 and 'utf8' or 'unicode', memory and 'memory' or 'file', syntax and 'syntax' or 'runtime')
try:
- do_test.__name__ = name
+ _do_test.__name__ = name
except:
pass
- setattr(RichTracebackTest, name, do_test)
+ setattr(RichTracebackTest, name, _do_test)
+ del _do_test
class ModuleDirTest(unittest.TestCase):
@@ -476,6 +477,3 @@ class PreprocessTest(unittest.TestCase):
""", preprocessor=convert_comments)
assert flatten_result(t.render()) == "im a template - # not a comment - ## not a comment"
-
-if __name__ == '__main__':
- unittest.main()
diff --git a/test/tgplugin.py b/test/test_tgplugin.py
index 0ff1318..701eb94 100644
--- a/test/tgplugin.py
+++ b/test/test_tgplugin.py
@@ -39,6 +39,3 @@ class TestTGPlugun(unittest.TestCase):
def test_string(self):
t = tl.load_template('foo', "hello world")
assert t.render() == "hello world"
-
-if __name__ == '__main__':
- unittest.main() \ No newline at end of file