2008/link-testsuite/harness/lib Documentation.py,NONE,1.1

Update of /sources/public/2008/link-testsuite/harness/lib
In directory hutz:/tmp/cvs-serv1987

Added Files:
	Documentation.py 
Log Message:
simple template engine class using jinja http://jinja.pocoo.org

--- NEW FILE: Documentation.py ---
#!/usr/bin/env python
# encoding: utf-8
"""
Documentation.py
Generate Link Test Suite index and documentation

Created by olivier Thereaux on 2008-01-30.
Copyright (c) 2008 W3C. Licensed under the W3C Software License
http://www.w3.org/Consortium/Legal/copyright-software
"""

import sys
import os
import unittest
import LinkTestCase
import datetime

class Documentation:
    def __init__(self, type=None):
        ok_types = ['test', 'index']
        if type in ok_types:
            self.type = type
        else:
            self.type = 'index'
        self.test_collections = list()
    def addCollection(self, test_collection):
        if isinstance(test_collection, LinkTestCase.LinkTestCollection) == 0:
            raise TypeError("not a proper test collection") 
        else:
            self.test_collections.append(test_collection)
    
    def generate(self, template_path=None):
        """pass through template engine"""
        import jinja,datetime
        if template_path == None:
            template_path= os.path.abspath('..')
            template_path=os.path.join(template_path, "templates")    
        template_engine = jinja.Environment(loader=jinja.FileSystemLoader(template_path))
        template = template_engine.get_template(os.path.join(self.type+'.html'))
        return template.render(collections=self.test_collections,
        year=str(datetime.date.today().year))

class DocumentationTests(unittest.TestCase):
    def test_has_jinja(self):
        try:
            import jinja
        except ImportError:
            self.fail("you need to install the jinja templating system -- http://jinja.pocoo.org/download")

    def test_init(self):
        """initialize a Documentation Generator"""
        generator = Documentation()
        self.assertEquals(generator.type, 'index')
    
    def test_init_default_fallback(self):
        """initialize a Documentation Generator with a bogus type"""
        generator = Documentation(type="grut")
        self.assertEquals(generator.type, 'index')

    def test_addCollection(self):
        """add test collection"""
        generator = Documentation()
        test_col = LinkTestCase.LinkTestCollection()
        generator.addCollection(test_col)
        self.assertEquals(len(generator.test_collections), 1)
    
    def test_boguscollection(self):
        """add bogus test collection and raise error"""
        generator = Documentation()
        test_col = 5 
        self.assertRaises(TypeError, Documentation.addCollection, generator, test_col)
    
    def test_generate_test(self):
        """generate collections index without any collection"""
        generator = Documentation('test')
        self.assertEqual(generator.generate(), str(datetime.date.today().year))


if __name__ == '__main__':
    unittest.main()

Received on Wednesday, 30 January 2008 07:05:55 UTC