diff --git a/CHANGES b/CHANGES index 96fedad79527ee9de4771de9a0f1616830bbcbab..66f47edaf22d07857b5a757ade24cacd595a11ec 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 ff758e8110edadda5ef4545f448e4a92f11e607b..0000000000000000000000000000000000000000 --- 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 7820bb6d83ae770c86ff149f7f2f93fb09ff8194..0000000000000000000000000000000000000000 --- 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 0000000000000000000000000000000000000000..084fe97225049e66b79a052aa2d5aa549e43c63c --- /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 similarity index 99% rename from test/ast.py rename to test/test_ast.py index f60be653fe522c15448910fda8ede1dc7f513eec..30da4f175bbf1e1c0f3d795e67c6b9f4aa9fe445 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 similarity index 97% rename from test/babelplugin.py rename to test/test_babelplugin.py index af3cb6345db5701a85a248faafcbb4adca9fbdd0..1a7f9d0d36042b2050fcda4573f6199105876f85 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 similarity index 99% rename from test/cache.py rename to test/test_cache.py index 1049b300fae5ca4f2ae1f0293d50234c56baa147..ced515e55becd1a53affeb4f1f8e4b4bda4068a5 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 similarity index 99% rename from test/call.py rename to test/test_call.py index 0c89694b6fcfbd813970a354090958cc705b8c77..164c8990e35aa69e3af36bde10e87782fd7bc47a 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 similarity index 98% rename from test/decorators.py rename to test/test_decorators.py index 2bd9e0df88892cfa0818a7bbb42d6a24c59ba446..62ba360ac9098bafd51b3df3bc43ecbcd8ddc75b 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 similarity index 99% rename from test/def.py rename to test/test_def.py index 69ce70ebdd9123b78d427097f685a768b38eaa04..6cb1fdfd36a8b807739255e0033bacc247b3d37e 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 similarity index 98% rename from test/exceptions_.py rename to test/test_exceptions.py index 2cb5d1d74eac99d11c5ac627b34e7bcf7622e5ab..52c9544176ecc0629a747ef1b0c74a8af3368472 100644 --- a/test/exceptions_.py +++ b/test/test_exceptions.py @@ -103,5 +103,3 @@ ${u'привет' + foobar} assert '''<div class="highlight">2 ${u\'привет\' + 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 similarity index 99% rename from test/filters.py rename to test/test_filters.py index 4fdd3420fbc5f835709a538474b1b507be1d778e..e7a672514f5dd712a8f044c57bd8a912ab02db39 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 similarity index 99% rename from test/inheritance.py rename to test/test_inheritance.py index 8cd4165573a581e299139d11e1bfa8293485cb94..c9c6990e1b4cebcb880a05afa300beeb39a99521 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 similarity index 99% rename from test/lexer.py rename to test/test_lexer.py index 17a2ba20c14ea0e609e2b376214fb176abca2b9d..21c18a22584af7ed283cee39ac611ca6a008c24b 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 similarity index 97% rename from test/lookup.py rename to test/test_lookup.py index fddbf8467074d51c5df1aefa14f10bb80c1bcb00..81bb7ec4d8551e9595d38a6fcc5a105fb560f7b3 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 similarity index 98% rename from test/lru.py rename to test/test_lru.py index 24e92ad2c494ac24962e132ff8ba463d98d93c20..d75b47a89f257e3cbd90783d4918c8f83fefc931 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 084fe97225049e66b79a052aa2d5aa549e43c63c..6351caa5345a8175a77aee1cd4104bfc75e40065 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 similarity index 99% rename from test/pygen.py rename to test/test_pygen.py index 95444ddb343d40af7bafe7f88ee82624a84aa792..181140f3ce37c039c8b45ffb8e6631b7e4c2981e 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 similarity index 98% rename from test/template.py rename to test/test_template.py index 3ef0794fdb839e33dfabc27f0065fc432a107bac..a5755d0fca1859126c2bb0f4aae108bf8805a617 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 similarity index 95% rename from test/tgplugin.py rename to test/test_tgplugin.py index 0ff1318d679b06625a29c08e6e249810e4c8ed43..701eb94a5c01c2ac69377cbe6b9b055774ac16d6 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