def test_locate_identifiers_7(self): code = """ import foo.bar """ parsed = ast.PythonCode(code, **exception_kwargs) eq_(parsed.declared_identifiers, {"foo"}) eq_(parsed.undeclared_identifiers, set())
def test_locate_identifiers_10(self): code = """ lambda q: q + 5 """ parsed = ast.PythonCode(code, **exception_kwargs) eq_(parsed.declared_identifiers, set()) eq_(parsed.undeclared_identifiers, set())
def test_fileargs_pagetag(self): t = Template(""" <%%page cache_dir='%s' cache_type='dbm'/> <%%! callcount = [0] %%> <%%def name="foo()" cached="True"> this is foo <%% callcount[0] += 1 %%> </%%def> ${foo()} ${foo()} ${foo()} callcount: ${callcount} """ % module_base) m = self._install_mock_cache(t) assert result_lines(t.render()) == [ "this is foo", "this is foo", "this is foo", "callcount: [1]", ] eq_(m.kwargs, {"dir": module_base, "type": "dbm"})
def test_fileargs_lookup(self): l = lookup.TemplateLookup(cache_dir=module_base, cache_type="file") l.put_string( "test", """ <%! callcount = [0] %> <%def name="foo()" cached="True"> this is foo <% callcount[0] += 1 %> </%def> ${foo()} ${foo()} ${foo()} callcount: ${callcount} """, ) t = l.get_template("test") m = self._install_mock_cache(t) assert result_lines(l.get_template("test").render()) == [ "this is foo", "this is foo", "this is foo", "callcount: [1]", ] eq_(m.kwargs, {"dir": module_base, "type": "file"})
def test_new_syntax(self): """test foo:bar syntax, including multiline args and expression eval.""" # note the trailing whitespace in the bottom ${} expr, need to strip # that off < python 2.7 t = Template( """ <%def name="foo(x, y, q, z)"> ${x} ${y} ${q} ${",".join("%s->%s" % (a, b) for a, b in z)} </%def> <%self:foo x="this is x" y="${'some ' + 'y'}" q=" this is q" z="${[ (1, 2), (3, 4), (5, 6) ] }"/> """ ) eq_( result_lines(t.render()), ["this is x", "some y", "this", "is", "q", "1->2,3->4,5->6"], )
def test_fileargs_implicit(self): l = lookup.TemplateLookup(module_directory=module_base) l.put_string( "test", """ <%! callcount = [0] %> <%def name="foo()" cached="True" cache_type='dbm'> this is foo <% callcount[0] += 1 %> </%def> ${foo()} ${foo()} ${foo()} callcount: ${callcount} """, ) m = self._install_mock_cache(l.get_template("test")) assert result_lines(l.get_template("test").render()) == [ "this is foo", "this is foo", "this is foo", "callcount: [1]", ] eq_(m.kwargs, {"type": "dbm"})
def test_locate_identifiers_6(self): code = """ def foo(): return bar() """ parsed = ast.PythonCode(code, **exception_kwargs) eq_(parsed.undeclared_identifiers, {"bar"}) code = """ def lala(x, y): return x, y, z print(x) """ parsed = ast.PythonCode(code, **exception_kwargs) eq_(parsed.undeclared_identifiers, {"z", "x"}) eq_(parsed.declared_identifiers, {"lala"}) code = """ def lala(x, y): def hoho(): def bar(): z = 7 print(z) """ parsed = ast.PythonCode(code, **exception_kwargs) eq_(parsed.undeclared_identifiers, {"z"}) eq_(parsed.declared_identifiers, {"lala"})
def test_scope_ten(self): t = Template(""" <%def name="a()"> <%def name="b()"> <% y = 19 %> b/c: ${c()} b/y: ${y} </%def> <%def name="c()"> c/y: ${y} </%def> <% # we assign to "y". but the 'enclosing # scope' of "b" and "c" is from # the "y" on the outside y = 10 %> a/y: ${y} a/b: ${b()} </%def> <% y = 7 %> main/a: ${a()} main/y: ${y} """) eq_( flatten_result(t.render()), "main/a: a/y: 10 a/b: b/c: c/y: 10 b/y: 19 main/y: 7", )
def test_scope_nine(self): """test that 'enclosing scope' doesnt get exported to other templates""" l = lookup.TemplateLookup() l.put_string( "main", """ <% x = 5 %> this is main. <%include file="secondary"/> """, ) l.put_string( "secondary", """ this is secondary. x is ${x} """, ) eq_( flatten_result(l.get_template("main").render(x=2)), "this is main. this is secondary. x is 2", )
def test_scope_five(self): """test that variables are pulled from 'enclosing' scope before context.""" # same as test four, but adds a scope around it. t = Template(""" <%def name="enclosing()"> <% x = 5 %> <%def name="a()"> this is a. x is ${x}. </%def> <%def name="b()"> <% x = 9 %> this is b. x is ${x}. calling a. ${a()} </%def> ${b()} </%def> ${enclosing()} """) eq_( flatten_result(t.render()), "this is b. x is 9. calling a. this is a. x is 5.", )
def test_locate_identifiers(self): """test the location of identifiers in a python code string""" code = """ a = 10 b = 5 c = x * 5 + a + b + q (g,h,i) = (1,2,3) [u,k,j] = [4,5,6] foo.hoho.lala.bar = 7 + gah.blah + u + blah for lar in (1,2,3): gh = 5 x = 12 ("hello world, ", a, b) ("Another expr", c) """ parsed = ast.PythonCode(code, **exception_kwargs) eq_( parsed.declared_identifiers, {"a", "b", "c", "g", "h", "i", "u", "k", "j", "gh", "lar", "x"}, ) eq_( parsed.undeclared_identifiers, {"x", "q", "foo", "gah", "blah"}, ) parsed = ast.PythonCode("x + 5 * (y-z)", **exception_kwargs) assert parsed.undeclared_identifiers == {"x", "y", "z"} assert parsed.declared_identifiers == set()
def test_custom_args_page(self): t = Template(""" <%page cached="True" cache_region="myregion" cache_timeout="50" cache_foo="foob"/> """) m = self._install_mock_cache(t, "simple") t.render() eq_(m.kwargs, {"region": "myregion", "timeout": 50, "foo": "foob"})
def test_fast_buffer_truncate(self): buf = util.FastEncodingBuffer() buf.write("string a ") buf.write("string b") buf.truncate() buf.write("string c ") buf.write("string d") eq_(buf.getvalue(), "string c string d")
def test_encode_filter_non_str(self): t = Template( """# coding: utf-8 some stuff.... ${x} """, default_filters=["decode.utf8"], ) eq_(t.render_unicode(x=3).strip(), "some stuff.... 3")
def test_def_blankargs(self): template = Template(""" <%def name="mycomp()"> hello mycomp ${variable} </%def> ${mycomp()}""") eq_(template.render(variable="hi").strip(), "hello mycomp hi")
def test_locate_identifiers_11(self): code = """ def x(q): return q + 5 """ parsed = ast.PythonCode(code, **exception_kwargs) eq_(parsed.declared_identifiers, {"x"}) eq_(parsed.undeclared_identifiers, set())
def test_quoting(self): t = Template(""" foo ${bar | h} """) eq_( flatten_result(t.render(bar="<'some bar'>")), "foo <'some bar'>", )
def test_url_escaping(self): t = Template(""" http://example.com/?bar=${bar | u}&v=1 """) eq_( flatten_result(t.render(bar="酒吧bar")), "http://example.com/?bar=%E9%85%92%E5%90%A7bar&v=1", )
def test_locate_identifiers_5(self): code = """ try: print(x) except: print(y) """ parsed = ast.PythonCode(code, **exception_kwargs) eq_(parsed.undeclared_identifiers, {"x", "y"})
def test_locate_identifiers_17(self): code = """ try: print(x) except (Foo, Bar) as e: print(y) """ parsed = ast.PythonCode(code, **exception_kwargs) eq_(parsed.undeclared_identifiers, {"x", "y", "Foo", "Bar"})
def test_stdin_success(self): with self._capture_output_fixture() as stdout: with mock.patch( "sys.stdin", mock.Mock(read=mock.Mock(return_value="hello world ${x}")), ): cmdline(["--var", "x=5", "-"]) eq_(stdout.write.mock_calls[0][1][0], "hello world 5")
def test_list_comprehensions_plus_undeclared_nonstrict(self): # traditional behavior. variable inside a list comprehension # is treated as an "undefined", so is pulled from the context. t = Template(""" t is: ${t} ${",".join([t for t in ("a", "b", "c")])} """) eq_(result_lines(t.render(t="T")), ["t is: T", "a,b,c"])
def test_locate_identifiers_4(self): code = """ x = 5 (y, ) def mydef(mydefarg): print("mda is", mydefarg) """ parsed = ast.PythonCode(code, **exception_kwargs) eq_(parsed.undeclared_identifiers, {"y"}) eq_(parsed.declared_identifiers, {"mydef", "x"})
def test_locate_identifiers_13(self): code = """ def foo(): class Bat: pass Bat """ parsed = ast.PythonCode(code, **exception_kwargs) eq_(parsed.declared_identifiers, {"foo"}) eq_(parsed.undeclared_identifiers, set())
def test_locate_identifiers_12(self): code = """ def foo(): s = 1 def bar(): t = s """ parsed = ast.PythonCode(code, **exception_kwargs) eq_(parsed.declared_identifiers, {"foo"}) eq_(parsed.undeclared_identifiers, set())
def test_custom_args_def(self): t = Template(""" <%def name="foo()" cached="True" cache_region="myregion" cache_timeout="50" cache_foo="foob"> </%def> ${foo()} """) m = self._install_mock_cache(t, "simple") t.render() eq_(m.kwargs, {"region": "myregion", "timeout": 50, "foo": "foob"})
def test_locate_identifiers_8(self): code = """ class Hi: foo = 7 def hoho(self): x = 5 """ parsed = ast.PythonCode(code, **exception_kwargs) eq_(parsed.declared_identifiers, {"Hi"}) eq_(parsed.undeclared_identifiers, set())
def test_return_in_template(self): t = Template( """ Line one <% return STOP_RENDERING %> Line Three """, strict_undefined=True, ) eq_(result_lines(t.render()), ["Line one"])
def test_unicode_file_lookup(self): lookup = TemplateLookup( directories=[config.template_base], output_encoding="utf-8", default_filters=["decode.utf8"], ) template = lookup.get_template("/chs_unicode_py3k.html") eq_( flatten_result(template.render_unicode(name="毛泽东")), ("毛泽东 是 新中国的主席<br/> Welcome 你 to 北京."), )
def test_file_success(self): with self._capture_output_fixture() as stdout: cmdline( [ "--var", "x=5", os.path.join(config.template_base, "cmd_good.mako"), ] ) eq_(stdout.write.mock_calls[0][1][0], "hello world 5")