<s> """<STR_LIT>""" <EOL> import os <EOL> import sys <EOL> import tempfile <EOL> import shutil <EOL> import unittest <EOL> from jinja2 . testsuite import JinjaTestCase , dict_loader , package_loader , filesystem_loader , function_loader , choice_loader , prefix_loader <EOL> from jinja2 import Environment , loaders <EOL> from jinja2 . loaders import split_template_path <EOL> from jinja2 . exceptions import TemplateNotFound <EOL> class LoaderTestCase ( JinjaTestCase ) : <EOL> def test_dict_loader ( self ) : <EOL> env = Environment ( loader = dict_loader ) <EOL> tmpl = env . get_template ( '<STR_LIT>' ) <EOL> assert tmpl . render ( ) . strip ( ) == '<STR_LIT>' <EOL> self . assert_raises ( TemplateNotFound , env . get_template , '<STR_LIT>' ) <EOL> def test_package_loader ( self ) : <EOL> env = Environment ( loader = package_loader ) <EOL> tmpl = env . get_template ( '<STR_LIT>' ) <EOL> assert tmpl . render ( ) . strip ( ) == '<STR_LIT>' <EOL> self . assert_raises ( TemplateNotFound , env . get_template , '<STR_LIT>' ) <EOL> def test_filesystem_loader ( self ) : <EOL> env = Environment ( loader = filesystem_loader ) <EOL> tmpl = env . get_template ( '<STR_LIT>' ) <EOL> assert tmpl . render ( ) . strip ( ) == '<STR_LIT>' <EOL> tmpl = env . get_template ( '<STR_LIT>' ) <EOL> assert tmpl . render ( ) . strip ( ) == '<STR_LIT>' <EOL> self . assert_raises ( TemplateNotFound , env . get_template , '<STR_LIT>' ) <EOL> def test_choice_loader ( self ) : <EOL> env = Environment ( loader = choice_loader ) <EOL> tmpl = env . get_template ( '<STR_LIT>' ) <EOL> assert tmpl . render ( ) . strip ( ) == '<STR_LIT>' <EOL> tmpl = env . get_template ( '<STR_LIT>' ) <EOL> assert tmpl . render ( ) . strip ( ) == '<STR_LIT>' <EOL> self . assert_raises ( TemplateNotFound , env . get_template , '<STR_LIT>' ) <EOL> def test_function_loader ( self ) : <EOL> env = Environment ( loader = function_loader ) <EOL> tmpl = env . get_template ( '<STR_LIT>' ) <EOL> assert tmpl . render ( ) . strip ( ) == '<STR_LIT>' <EOL> self . assert_raises ( TemplateNotFound , env . get_template , '<STR_LIT>' ) <EOL> def test_prefix_loader ( self ) : <EOL> env = Environment ( loader = prefix_loader ) <EOL> tmpl = env . get_template ( '<STR_LIT>' ) <EOL> assert tmpl . render ( ) . strip ( ) == '<STR_LIT>' <EOL> tmpl = env . get_template ( '<STR_LIT>' ) <EOL> assert tmpl . render ( ) . strip ( ) == '<STR_LIT>' <EOL> self . assert_raises ( TemplateNotFound , env . get_template , '<STR_LIT>' ) <EOL> def test_caching ( self ) : <EOL> changed = False <EOL> class TestLoader ( loaders . BaseLoader ) : <EOL> def get_source ( self , environment , template ) : <EOL> return u'<STR_LIT:foo>' , None , lambda : not changed <EOL> env = Environment ( loader = TestLoader ( ) , cache_size = - <NUM_LIT:1> ) <EOL> tmpl = env . get_template ( '<STR_LIT>' ) <EOL> assert tmpl is env . get_template ( '<STR_LIT>' ) <EOL> changed = True <EOL> assert tmpl is not env . get_template ( '<STR_LIT>' ) <EOL> changed = False <EOL> env = Environment ( loader = TestLoader ( ) , cache_size = <NUM_LIT:0> ) <EOL> assert env . get_template ( '<STR_LIT>' ) is not env . get_template ( '<STR_LIT>' ) <EOL> env = Environment ( loader = TestLoader ( ) , cache_size = <NUM_LIT:2> ) <EOL> t1 = env . get_template ( '<STR_LIT>' ) <EOL> t2 = env . get_template ( '<STR_LIT>' ) <EOL> assert t2 is env . get_template ( '<STR_LIT>' ) <EOL> assert t1 is env . get_template ( '<STR_LIT>' ) <EOL> t3 = env . get_template ( '<STR_LIT>' ) <EOL> assert '<STR_LIT>' in env . cache <EOL> assert '<STR_LIT>' not in env . cache <EOL> assert '<STR_LIT>' in env . cache <EOL> def test_split_template_path ( self ) : <EOL> assert split_template_path ( '<STR_LIT>' ) == [ '<STR_LIT:foo>' , '<STR_LIT:bar>' ] <EOL> assert split_template_path ( '<STR_LIT>' ) == [ '<STR_LIT:foo>' , '<STR_LIT:bar>' ] <EOL> self . assert_raises ( TemplateNotFound , split_template_path , '<STR_LIT>' ) <EOL> class ModuleLoaderTestCase ( JinjaTestCase ) : <EOL> archive = None <EOL> def compile_down ( self , zip = '<STR_LIT>' , py_compile = False ) : <EOL> super ( ModuleLoaderTestCase , self ) . setup ( ) <EOL> log = [ ] <EOL> self . reg_env = Environment ( loader = prefix_loader ) <EOL> if zip is not None : <EOL> self . archive = tempfile . mkstemp ( suffix = '<STR_LIT>' ) [ <NUM_LIT:1> ] <EOL> else : <EOL> self . archive = tempfile . mkdtemp ( ) <EOL> self . reg_env . compile_templates ( self . archive , zip = zip , <EOL> log_function = log . append , <EOL> py_compile = py_compile ) <EOL> self . mod_env = Environment ( loader = loaders . ModuleLoader ( self . archive ) ) <EOL> return '<STR_LIT>' . join ( log ) <EOL> def teardown ( self ) : <EOL> super ( ModuleLoaderTestCase , self ) . teardown ( ) <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> if os . path . isfile ( self . archive ) : <EOL> os . remove ( self . archive ) <EOL> else : <EOL> shutil . rmtree ( self . archive ) <EOL> self . archive = None <EOL> def test_log ( self ) : <EOL> log = self . compile_down ( ) <EOL> assert '<STR_LIT>' '<STR_LIT>' in log <EOL> assert '<STR_LIT>' in log <EOL> assert '<STR_LIT>' '<STR_LIT>' in log <EOL> def _test_common ( self ) : <EOL> tmpl1 = self . reg_env . get_template ( '<STR_LIT>' ) <EOL> tmpl2 = self . mod_env . get_template ( '<STR_LIT>' ) <EOL> assert tmpl1 . render ( ) == tmpl2 . render ( ) <EOL> tmpl1 = self . reg_env . get_template ( '<STR_LIT>' ) <EOL> tmpl2 = self . mod_env . get_template ( '<STR_LIT>' ) <EOL> assert tmpl1 . render ( ) == tmpl2 . render ( ) <EOL> def test_deflated_zip_compile ( self ) : <EOL> self . compile_down ( zip = '<STR_LIT>' ) <EOL> self . _test_common ( ) <EOL> def test_stored_zip_compile ( self ) : <EOL> self . compile_down ( zip = '<STR_LIT>' ) <EOL> self . _test_common ( ) <EOL> def test_filesystem_compile ( self ) : <EOL> self . compile_down ( zip = None ) <EOL> self . _test_common ( ) <EOL> def test_weak_references ( self ) : <EOL> self . compile_down ( ) <EOL> tmpl = self . mod_env . get_template ( '<STR_LIT>' ) <EOL> key = loaders . ModuleLoader . get_template_key ( '<STR_LIT>' ) <EOL> name = self . mod_env . loader . module . __name__ <EOL> assert hasattr ( self . mod_env . loader . module , key ) <EOL> assert name in sys . modules <EOL> self . mod_env = tmpl = None <EOL> try : <EOL> import gc <EOL> gc . collect ( ) <EOL> except : <EOL> pass <EOL> assert name not in sys . modules <EOL> def test_byte_compilation ( self ) : <EOL> log = self . compile_down ( py_compile = True ) <EOL> assert '<STR_LIT>' in log <EOL> tmpl1 = self . mod_env . get_template ( '<STR_LIT>' ) <EOL> mod = self . mod_env . loader . module . tmpl_3c4ddf650c1a73df961a6d3d2ce2752f1b8fd490 <EOL> assert mod . __file__ . endswith ( '<STR_LIT>' ) <EOL> def suite ( ) : <EOL> suite = unittest . TestSuite ( ) <EOL> suite . addTest ( unittest . makeSuite ( LoaderTestCase ) ) <EOL> suite . addTest ( unittest . makeSuite ( ModuleLoaderTestCase ) ) <EOL> return suite </s>
