testTemplate.py :  » Web-Frameworks » Python-Publishing-Accessories » PPA-0.2.7 » tests » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Web Frameworks » Python Publishing Accessories 
Python Publishing Accessories » PPA 0.2.7 » tests » testTemplate.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# $Id: testTemplate.py,v 1.7 2006/12/15 11:13:17 ods Exp $

import unittest, sys, os, codecs
from glob import glob
from cStringIO import StringIO

dir = os.path.dirname(os.path.abspath(globals().get('__file__', sys.argv[0])))
sys.path.insert(0, os.path.dirname(dir))

from PPA.Template.Controller import TemplateController
from PPA.Template.SourceFinders import FileSourceFinder,TemplateDirectory


class TemplateTestCase(unittest.TestCase):

    def __init__(self, template_name, template_type, charset, globals, locals,
                 templates_path, results_path):
        unittest.TestCase.__init__(self)
        self.template_name = template_name
        self.template_type = template_type
        writer = StringIO
        if charset is not None:
            templates_path = TemplateDirectory(templates_path, charset)
            writer = lambda: codecs.getwriter(charset)(StringIO())
        self.writer = writer
        self.globals = globals
        self.locals = locals
        self.templates_path = templates_path
        self.results_path = results_path
    
    def runTest(self):
        source_finder = FileSourceFinder([self.templates_path])
        controller = TemplateController(source_finder=source_finder)
        template = controller.getTemplate(self.template_name,
                                          self.template_type)
        fp = self.writer()
        template.interpret(fp, self.globals, self.locals)
        got_result = fp.getvalue()
        # We use real (possibly auto-determined) type of template when looking
        # for result.
        result_file_name = os.path.join(self.results_path,
                                '.'.join((self.template_name, template.type)))
        result_fp = file(result_file_name)
        result = result_fp.read()
        result_fp.close()
        self.assertEqual(got_result, result)


class TemplateTestSuite(unittest.TestSuite):

    def __init__(self, templates_path, results_path, list_of_tests):
        unittest.TestSuite.__init__(self)
        for template_name, template_type, charset, globals, locals \
                in list_of_tests:
            test = TemplateTestCase(template_name, template_type, charset,
                                    globals, locals,
                                    templates_path, results_path)
            self.addTest(test)


_list_of_tests = [
    ('test',   'pyem',  None,    {'var': 'global'},
                                 {'var': 'local'}),
    ('test',   None,    None,    {'var': 'global'},
                                 {'var': 'local'}),
    ('test2',  'htal',  None,    {'var': 'global'},
                                 {'title': 'Just a title',
                                  'var': 'local'}),
    ('test2',  None,    None,    {'var': 'global'},
                                 {'title': 'Just a title',
                                  'var': 'local'}),
    ('test3',  'xtal',  None,    {'var': 'global'},
                                 {'title': 'Just a title',
                                  'var': 'local'}),
    ('test3',  None,    None,    {'var': 'global'},
                                 {'title': 'Just a title',
                                  'var': 'local'}),
    ('test4',  'htal',  None,    {},
                                 {'title': 'Just a title',
                                  'body': 'Just a body'}),
    ('test4',  None,    None,    {},
                                 {'title': 'Just a title',
                                  'body': 'Just a body'}),
    ('test5',  'dtml',  None,    {'var': 'global'},
                                 {'title': 'Just a title',
                                  'items': ['foo', 'bar'],
                                  'var': 'local'}),
    ('test5',  None,    None,    {'var': 'global'},
                                 {'title': 'Just a title',
                                  'items': ['foo', 'bar'],
                                  'var': 'local'}),
    ('test6',  'raw',   None,    {}, {}),
    ('test6',  None,    None,    {}, {}),
    ('test7',  'pysi',  None,    {'var': 'global'}, 
                                 {'title': 'Just a title',
                                  'body': ['para1', 'para2', 'para3'],
                                  'max_paras': 2, 'var': 'local'}),
    ('test7',  None,    None,    {'var': 'global'},
                                 {'title': 'Just a title',
                                  'body': ['para1', 'para2', 'para3'],
                                  'max_paras': 2, 'var': 'local'}),
    ('test8',  None,    None,    {},
                                 {'sections': [
                                     {'title': 'Title1',
                                      'children': []},
                                     {'title': 'Title2',
                                      'children': [{'title': 'Title2.1',
                                                    'children': []},
                                                   {'title': 'Title2.2',
                                                    'children': []}]}
                                 ]}),
    ('test9',  'xhtal', None,    {'var': 'global'},
                                 {'title': 'Just a title',
                                  'var': 'local'}),
    ('test9',  None,    None,    {'var': 'global'},
                                 {'title': 'Just a title',
                                  'var': 'local'}),
    ('test9',  None,    None,    {'var': 'global'},
                                 {'title': 'Just a title',
                                  'var': 'local'}),
    # Tests with charset='utf-8'
    ('test10', 'raw',   'utf-8', {}, {}),
    ('test11', 'pysi',  'utf-8', {'cyrillic': ''.decode('utf-8')},
                                 {}),
    ('test12', 'pyem',  'utf-8', {'cyrillic': ''.decode('utf-8')},
                                 {}),
    ('test13', 'htal',  'utf-8', {'cyrillic': ''.decode('utf-8')},
                                 {}),
    ('test14', 'xtal',  'utf-8', {'cyrillic': ''.decode('utf-8')},
                                 {}),
    ('test15', 'xhtal', 'utf-8', {'cyrillic': ''.decode('utf-8')},
                                 {}),
    # Test for changes in co_consts of compiled object
    ('test16', 'pyem',  'utf-8', {}, {}),
    # Test for keyword arguments in unicode mode
    ('test17', 'pyem',  'utf-8', {}, {}),
]


def testSuite():
    templates_path = os.path.join(dir, 'templates')
    results_path = os.path.join(dir, 'results')
    suite = TemplateTestSuite(templates_path, results_path, _list_of_tests)
    return suite


if __name__=='__main__':
    unittest.TextTestRunner().run(testSuite())
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.