# HG changeset patch # User Daniele Nicolodi # Date 1313943447 -7200 # Node ID e7e0258660aafce3f1b29dbde4a8c59b7322111c # Parent 601d49a7c35372fb302e9c794c8d3a3e4ae50e02 Test to check that all templates have a title block. diff -r 601d49a7c353 -r e7e0258660aa src/ltpdarepo/tests/test_templates.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/ltpdarepo/tests/test_templates.py Sun Aug 21 18:17:27 2011 +0200 @@ -0,0 +1,50 @@ +import os +import re +import unittest2 as unittest + + +def templates(): + path = os.path.join(os.path.dirname(__file__), '..', 'templates') + path = os.path.normpath(path) + plen = len(path) + 1 + for root, dirs, files in os.walk(path): + for f in files: + if f.endswith('.html'): + path = os.path.join(root, f) + name = path[plen:] + yield name, path + + +class TestCase(unittest.TestCase): + + def test_templates_have_title_block(self): + # templates to skip in this test + blacklist = frozenset(( + 'forms.html', # forms rendering macros + 'form.html', # generic form rendering + 'pagination.html', # pagination rendering macros + 'browse.html', # extends template with title + 'query.html', # ditto + 'timerange.html', # ditto + )) + + # regular expression to obtain the title block + title = re.compile(r'{% block title %}\s*(.*)\s*{% endblock %}', re.MULTILINE) + + # list of templates that miss a title block + missing = [] + + for name, path in templates(): + if name in blacklist: + continue + with open(path) as fd: + template = fd.read() + match = title.search(template) + if not match: + missing.append(name) + continue + # print '%-30s %s' % (name, match.group(1)) + + if missing: + self.fail('%d templates do not define a title block: %s' + % (len(missing), ', '.join(missing)))