"""Unit Tests for Template Reuse.""" __revision__ = "$Rev: 455 $" __author__ = "Christoph Zwerschke " __copyright__ = "Copyright 2006, Christoph Zwerschke" from os.path import join as joinpath from tempfile import mkdtemp from shutil import rmtree import kid def setup_module(module): global tmpdir tmpdir = mkdtemp(prefix='kid_test_extends_') kid.path.insert(tmpdir) open(joinpath(tmpdir, 'layout.kid'), 'w').write("""\

my header

my footer

""") def teardown_module(module): kid.path.remove(tmpdir) rmtree(tmpdir) def test_extends(): """Test the basic template reuse functionality.""" page = """\

my content

""" for extends in ("'layout.kid'", "layout.kid", "'layout'", "layout"): source = page % extends rslt = kid.Template(source=source).serialize() assert 'my header' in rslt assert 'my content' in rslt assert 'my footer' in rslt source = page % "layout_module" from kid.template_util import TemplateExtendsError try: rslt = kid.Template(source=source).serialize() except TemplateExtendsError, e: e = str(e) except Exception: e = 'wrong error' else: e = 'silent' assert "'layout_module'" in e assert 'not defined' in e assert 'while processing extends=' in e source = """ """ + source for extends in ("layout_module", "layout_module.Template"): rslt = kid.Template(source=source).serialize() assert 'my header' in rslt assert 'my content' in rslt assert 'my footer' in rslt def test_comments_in_extends(): """Test for the bug that was reported in ticket #66.""" open(joinpath(tmpdir, 'layout2.kid'), 'w').write("""\ layout
header

... content will be inserted here ...

footer
""") open(joinpath(tmpdir, 'page2.kid'), 'w').write("""\ page

my content

""") t = kid.Template(file="page2.kid") rslt = t.serialize(output='xhtml') expected = """\ page
header

my content

footer
""" i = 0 for line in expected.splitlines(): line = line.strip() i = rslt.find(line, i) assert i >= 0, 'Missing or misplaced: ' + line def test_layout_and_extends(): """Test for the bug that was reported in ticket #194.""" open(joinpath(tmpdir, 'page3.kid'), 'w').write("""\ Welcome to the test
Welcome
""") open(joinpath(tmpdir, 'page3e.kid'), 'w').write("""\ Extend Page page """) open(joinpath(tmpdir, 'layout3.kid'), 'w').write("""\ Layout Title

""") open(joinpath(tmpdir, 'layout3e.kid'), 'w').write("""\ Extend Layout layout """) t = kid.Template(file="page3.kid") rslt = t.serialize(output='xhtml') expected = """\ Layout Title

layout

Welcome page
""" i = 0 for line in expected.splitlines(): line = line.strip() i = rslt.find(line, i) assert i >= 0, 'Missing or misplaced: ' + line def test_pudge_layout(): """This is how Pudge implements layouts. This will cause a generator to be sent to template_util.generate_content. For each of the (ev, item) pairs yielded from the generator will be fed through generate content. Before the fix the tuples were treated as text for the output. """ open(joinpath(tmpdir, 'pudge_layout.kid'), 'w').write("""
Interesting text here
""".strip()) open(joinpath(tmpdir, 'testlayout.kid'), 'w').write("""\
""".strip()) t = kid.Template(file="pudge_layout.kid") rslt = t.serialize(output='xml') print rslt expected = """\
Interesting text here
""" rslt = [x.strip() for x in rslt.splitlines() if x.strip()] expected = [x.strip() for x in expected.splitlines() if x.strip()] assert expected == rslt