testPythonEmbedded.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 » testPythonEmbedded.py
#!/usr/bin/env python
# $Id: testPythonEmbedded.py,v 1.3 2004/04/12 10:00:16 ods Exp $

import unittest, sys, os

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

from PPA.Template.Engines.PythonEmbedded import Parser,ParseError,Engine


class PyEmParserTestCase(unittest.TestCase):

    def _parse(self, s):
        return Parser(s).process()
    
    def testRawHTML(self):
        '''Parsing raw HTML'''
        raw_html = '''\
<html>
<body background="#ffffff">
<h1 align="center">Header</h1>
<p style="color: blue">Some Text</p>
</body>
</html>'''
        self.assertEqual(self._parse(raw_html), [('html', raw_html)])

    def testSimpleSuite(self):
        '''Parsing simple python suite'''
        suite = '''
import operator
def fact(n):
    assert n>=0
    if n<=1:
        return 1
    return reduce(operator.mul, range(1, n+1))
'''
        self.assertEqual(self._parse('<%' + suite + '%>'), [('suite', suite)])

    def testSimpleExpr(self):
        '''Parsing simple Python expression'''
        expr = '(a+b)*c'
        self.assertEqual(self._parse('<%=' + expr + '%>'), [('expr', expr)])

    def testQuotes(self):
        '''Parsing quotes in suites and expressions'''
        for quotes in [
                '"string"',
                "'string'",
                '"""multiline\nstring"""',
                "'''multiline\nstring'''",
                '"\\"escapes\\""',
                "'\\'escapes\\''",
                '"con""cat"\'ena\'\'tion\'',
                '"""multiline\n\\\'with\\\'\\"esqapes\\""""']:
            suite = '\n%s\n' % quotes
            self.assertEqual(self._parse('<%' + suite + '%>'),
                             [('suite', suite)])
            expr = quotes
            self.assertEqual(self._parse('<%=' + expr + '%>'),
                             [('expr', expr)])

    def testImproperExpr(self):
        '''Improperly embedded expressions'''
        for expr in ['\n1', '1\n', '1\n2', '', ' ']:
            self.assertRaises(ParseError, self._parse, '<%=' + expr + '%>')

    def testImproperSuite(self):
        '''Improperly embedded suites'''
        for suite in ['\n1', '1\n', '12', '1\n2']:
            self.assertRaises(ParseError, self._parse, '<%' + suite + '%>')


from TemplateEnginesCommon import InterpretTestSuite

list_of_interpret_tests = [
    ('<html></html>', {}, {}, '<html></html>'),
    ('<%= 1 %>', {}, {}, '1'),
    ('<%= title %>', {'title': 'Just a title'}, {}, 'Just a title'),
    ('<%= title.upper() %>', {'title': 'Just a title'}, {}, 'JUST A TITLE'),
    ('''\
<%
for i in range(n):
    %><%= i+1 %><br><%
%>''', {'n': 5}, {}, '1<br>2<br>3<br>4<br>5<br>'),
    ('<%\nimport string\n%><%= string.__name__ %>', {}, {}, 'string'),
]


def testSuite():
    return unittest.TestSuite([
        unittest.makeSuite(PyEmParserTestCase),
        InterpretTestSuite(Engine, list_of_interpret_tests)
    ])


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.