def test_optimization_kwargs(): src = compile_source( '#def foo(**kwargs)\n' '$kwargs\n' '#end def\n', ) assert ' _v = kwargs #' in src
def test_optimization_args(): src = compile_source( '#def foo(*args)\n' '$args\n' '#end def\n' ) assert ' _v = args #' in src
def test_optimization_for(): src = compile_source( '#for foo in bar:\n' ' $foo\n' '#end for\n', ) assert ' _v = foo #' in src
def test_optimization_parameters(): src = compile_source( '#def foo(bar)\n' '$bar\n' '#end def\n', ) assert ' _v = bar #' in src
def test_optimization_with(): src = compile_source( '#with foo() as bar:\n' ' $bar\n' '#end with\n', ) assert ' _v = bar #' in src
def test_compile_source_with_encoding_returns_text(): ret = compile_source('Hello, world! ☃') assert type(ret) is six.text_type if six.PY2: assert "write('''Hello, world! \\u2603''')" in ret else: assert "write('''Hello, world! ☃''')" in ret
def test_optimize_multi_comprehension_rename_ns_variable(): src = '#py [($x, $y) for x in (1,) if $x and $y for y in (2,)]' src = compile_source(src) expected = ' [(x, y) for x in (1,) if x and {} for y in (2,)] #'.format( lookup('y'), ) assert expected in src
def test_unicode_literals_off(): tmpl_source = compile_source( 'Hello World', settings={'future_unicode_literals': False}, ) assert 'from __future__ import unicode_literals\n' not in tmpl_source # u because we're not unicode literals assert "write(u'''Hello World''')" in tmpl_source
def test_compile_ngettext_as_multi_level_attribute_function_returns_scannable_output(): ret = compile_source("$foo.translator.ngettext('${n} puppy', '${n} puppies', 1)") assert type(ret) is six.text_type expected = """\ def __CHEETAH_gettext_scannables(): foo.translator.ngettext('${n} puppy', '${n} puppies', 1)\ """ assert expected in ret
def test_unicode_literals_on(): tmpl_source = compile_source( 'Hello World', settings={'future_unicode_literals': True}, ) assert 'from __future__ import unicode_literals\n' in tmpl_source # No u because we're unicode literals assert "write('''Hello World''')" in tmpl_source
def test_optimization_oneline_class(): cheetah_src = ( '#py class notlocal(object): pass\n' '$notlocal.__name__\n' ) assert compile_to_class(cheetah_src)().respond() == 'notlocal\n' src = compile_source(cheetah_src) assert ' _v = notlocal.__name__ #' in src
def test_optimization_oneline_function(): cheetah_src = ( '#py def foo(x): return x * x\n' '$foo(2)\n' ) assert compile_to_class(cheetah_src)().respond() == '4\n' src = compile_source(cheetah_src) assert ' _v = foo(2) #' in src
def test_compile_ngettext_function_returns_scannable_output(): ret = compile_source("$ngettext('${n} puppy', '${n} puppies', 1)") assert type(ret) is six.text_type expected = """\ def __CHEETAH_gettext_scannables(): ngettext('${n} puppy', '${n} puppies', 1)\ """ assert expected in ret
def test_compile_gettext_alias_function_returns_scannable_output(): ret = compile_source("$_('Hello, world!')") assert type(ret) is six.text_type expected = """\ def __CHEETAH_gettext_scannables(): _('Hello, world!')\ """ assert expected in ret
def test_compile_gettext_as_attribute_function_returns_scannable_output(): ret = compile_source("$translator.gettext('Hello, world!')") assert type(ret) is six.text_type expected = """\ def __CHEETAH_gettext_scannables(): translator.gettext('Hello, world!')\ """ assert expected in ret
def test_compile_translator_with_name_variable_arg_and_gettext_attribute_returns_scannable_output(): ret = compile_source("$translator($interface_locale).gettext('Hello, world!')") assert type(ret) is six.text_type expected = """\ def __CHEETAH_gettext_scannables(): translator(VFFSL("interface_locale", locals(), globals(), self, NS)).gettext('Hello, world!')\ """ assert expected in ret
def test_compile_translator_with_literal_arg_and_gettext_attribute_returns_scannable_output(): ret = compile_source("$translator('en_US').gettext('Hello, world!')") assert type(ret) is six.text_type expected = """\ def __CHEETAH_gettext_scannables(): translator('en_US').gettext('Hello, world!')\ """ assert expected in ret
def to_py(src): return compile_source( src, # This turns off the usual $var -> VFFSL(SL, 'var', True, True) # so now we get $var -> var settings={'useNameMapper': False}, compiler_cls=AllMacrosTrivialCompiler, )
def test_optimization_except(): src = compile_source( '#try\n' ' #pass\n' '#except Exception as e\n' ' $e\n' '#end try\n', ) assert ' _v = e #' in src
def test_compile_gettext_as_multi_level_callable_attribute_function_returns_scannable_output(): # Verify attribute access few more levels deep ret = compile_source("$foo.bar().baz().womp().gettext('Hello, world!')") assert type(ret) is six.text_type expected = """\ def __CHEETAH_gettext_scannables(): foo.bar().baz().womp().gettext('Hello, world!')\ """ assert expected in ret
def test_optimization_tuple_assign(): src = compile_source( '#py x, (y, z) = (1, (2, 3))\n' '$x\n' '$y\n' '$z\n', ) assert ' _v = x #' in src assert ' _v = y #' in src assert ' _v = z #' in src
def test_optimization_multiple_assign(): src = compile_source( '#py x = y = z = 0\n' '$x\n' '$y\n' '$z\n', ) assert ' _v = x #' in src assert ' _v = y #' in src assert ' _v = z #' in src
def test_compile_mutiple_gettext_functions_returns_scannable_output(): source_template = """\ $gettext('Hello, world!') $_('I am hungry') $ngettext('${n} puppy', '${n} puppies', 1)") """ ret = compile_source(source_template) assert type(ret) is six.text_type expected = """\ def __CHEETAH_gettext_scannables(): gettext('Hello, world!') # generated from line 1, col 1. _('I am hungry') # generated from line 2, col 1. ngettext('${n} puppy', '${n} puppies', 1) # generated from line 3, col 1. """ assert expected in ret
def test_comprehension_with_newlines(): src = compile_source( '#py [\n' '$x\n' 'for\n' 'x\n' 'in\n' '(1,)\n' ']', ) expected = ( ' [\n' 'x\n' 'for\n' 'x\n' 'in\n' '(1,)\n' '] #' ) assert expected in src
def assert_parse_error(error, source, cls=ParseError): with assert_raises_exactly(cls, error): compile_source(source)
def to_py(src): return compile_source(src, compiler_cls=NoCompilerSettingsCompiler)
def test_optimization_oneline_class(): cheetah_src = ('#py class notlocal(object): pass\n' '$notlocal.__name__\n') assert compile_to_class(cheetah_src)().respond() == 'notlocal\n' src = compile_source(cheetah_src) assert ' _v = notlocal.__name__ #' in src
def test_optimization_removes_VFN(): src = compile_source(VFN_opt_src) assert 'VFN(' not in src assert ' _v = {}.barvar[0].upper() #'.format(lookup('foo')) in src cls = compile_to_class(VFN_opt_src) assert cls({'foo': fooobj}).respond() == 'W'
def test_optimization_args(): src = compile_source('#def foo(*args)\n' '$args\n' '#end def\n') assert ' _v = args #' in src
def test_optimization_oneline_function(): cheetah_src = ('#py def foo(x): return x * x\n' '$foo(2)\n') assert compile_to_class(cheetah_src)().respond() == '4\n' src = compile_source(cheetah_src) assert ' _v = foo(2) #' in src
def test_compile_is_deterministic(): # This crazy template uses all of the currently supported directives MEGA_TEMPLATE = """ #compiler-settings useNameMapper = False #end compiler-settings #extends testing.templates.extends_test_template #implements respond #import contextlib #import sys #from tests.SyntaxAndOutput_test import dummydecorator #attr attribute = "bar" #@dummydecorator #def foo_call_func(arg) $arg #end def #def returning_function() #return 5 #end def #def try_raise_finally_func() #try #raise AssertionError("foo") #except AssertionError Caught AssertionError #except ValueError #pass #finally Finally #end try #end def #def spacer() #super() after #end def #def gen() #yield 1 #yield 2 #yield 3 #end def #@contextlib.contextmanager #def ctx() before #yield after #end def #call self.foo_call_func Hello world #end call #py foo = {"a": 1} #del foo['a'] $foo #assert True $self.returning_function() $self.spacer() #if 15 15! #elif 16 16! #else not 15 or 16 #end if #py arr = [1, 2, 3] #py arr.append(5) $arr #block infinite_loop_meybs #while True infinite loop? #break ## nope lol #end while #end block #for i in self.gen() #if $i == 2 #continue #end if $i#slurp #end for #with self.ctx() inside ctx #end with """ compiled_templates = [compile_source(MEGA_TEMPLATE) for _ in range(5)] assert len(set(compiled_templates)) == 1 # Make sure we got all of the directives for directive_name in directiveNamesAndParsers: assert '#{0}'.format(directive_name) in MEGA_TEMPLATE # also make sure MEGA_TEMPLATE renders assert compile_to_class(MEGA_TEMPLATE)().respond()
def test_optimization_for(): src = compile_source('#for foo in bar:\n' ' $foo\n' '#end for\n') assert ' _v = foo #' in src
def test_compile_source_requires_text(): with pytest.raises(TypeError): compile_source(b'not text')
def test_compile_source_returns_text(): ret = compile_source('Hello, world!') assert type(ret) is six.text_type assert "write('''Hello, world!''')" in ret
def test_compile_source_returns_text(): ret = compile_source('Hello, world!') assert type(ret) is five.text assert "write('''Hello, world!''')" in ret
def test_optimization_with(): src = compile_source('#with foo() as bar:\n' ' $bar\n' '#end with\n') assert ' _v = bar #' in src
def test_compile_is_deterministic(): # This crazy template uses all of the currently supported directives MEGA_TEMPLATE = """ #compiler-settings useDottedNotation = True #end compiler-settings #extends testing.templates.extends_test_template #implements respond #import contextlib #import sys #from tests.SyntaxAndOutput_test import dummydecorator #attr attribute = "bar" #@dummydecorator #def foo_call_func(arg) $arg #end def #def returning_function() #return 5 #end def #def try_raise_finally_func() #try #raise AssertionError("foo") #except AssertionError Caught AssertionError #except ValueError #pass #finally Finally #end try #end def #def spacer() #super() after #end def #def gen() #yield 1 #yield 2 #yield 3 #end def #@contextlib.contextmanager #def ctx() before #yield after #end def #call $foo_call_func Hello world #end call #set foo = {"a": 1} #del foo['a'] $foo #assert True $returning_function() $spacer() #if 15 15! #elif 16 16! #else not 15 or 16 #end if #set arr = [1, 2, 3] #silent arr.append(4) $arr #block infinite_loop_meybs #while True infinite loop? #break ## nope lol #end while #end block #filter None Default filter? #end filter #for i in $gen() #if $i == 2 #continue #end if $i#slurp #end for #with $ctx() inside ctx #end with """ compiled_templates = [compile_source(MEGA_TEMPLATE) for _ in range(5)] assert len(set(compiled_templates)) == 1 # Make sure we got all of the directives for directive_name in directiveNamesAndParsers: assert '#{0}'.format(directive_name) in MEGA_TEMPLATE # also make sure MEGA_TEMPLATE renders assert compile_to_class(MEGA_TEMPLATE)().respond()
def test_optimization_tuple_assign(): src = compile_source('#py x, (y, z) = (1, (2, 3))\n' '$x\n' '$y\n' '$z\n') assert ' _v = x #' in src assert ' _v = y #' in src assert ' _v = z #' in src
def test_compile_source_cls_name_requires_text(): with pytest.raises(TypeError): compile_source('text', cls_name=b'not text')
def test_template_exposes_global(): src = compile_source('Hello World') module = _create_module_from_source(src) assert module.__YELP_CHEETAH__ is True
def test_compile_cls_name_in_output(): ret = compile_source('Hello, world!', cls_name='my_cls_name') assert 'class my_cls_name(' in ret
def test_optimization_multiple_assign(): src = compile_source('#py x = y = z = 0\n' '$x\n' '$y\n' '$z\n') assert ' _v = x #' in src assert ' _v = y #' in src assert ' _v = z #' in src