test_html_errors.py :  » Template-Engines » Myghty » Myghty-1.1 » test » 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 » Template Engines » Myghty 
Myghty » Myghty 1.1 » test » test_html_errors.py
# encoding: latin1
# test_html_errors.py
"""Test HTML formatting of errors.
"""
import re, unittest
import testbase
from myghty.exception import Error,Syntax

class HTMLDecodingErrorBase(object):

    expected_error = Error
    wrapped_error = None
    expected_html = "euro:€"
    
    def _testComponent(self, component_maker):
        try:
            self.runComponent(component_maker(self.csource))
        except Error, ex:
            if self.expected_error:
                self.failUnless(isinstance(ex, self.expected_error))
            if self.wrapped_error:
                self.failUnless(isinstance(ex.wrapped, self.wrapped_error))
        else:
            self.fail()

        if self.expected_html not in ex.htmlformat():
            print ex.htmlformat()
        self.failUnless(self.expected_html in ex.htmlformat())
        
    def testFileComponent(self):
        self._testComponent(self.makeFileBasedComponent)

    def testMemoryComponent(self):
        self._testComponent(self.makeMemoryComponent)

class PythonErrorInComponentTests(HTMLDecodingErrorBase,
                                  testbase.ComponentTester):
    csource = """# encoding: iso-8859-15
                 % m.setTYPO_output_encoding('UTF-8')
                 euro:\xa4
                 <br />encoding is: <% m.output_encoding %>
                 """

    wrapped_error = AttributeError

class PythonErrorInComponentCodeline(HTMLDecodingErrorBase,
                                  testbase.ComponentTester):
    csource = """# encoding: iso-8859-15
                 % u'euro:\xa4' + 1
                 """

    wrapped_error = TypeError
   
class SyntaxErrorInComponentTests(HTMLDecodingErrorBase,
                                  testbase.ComponentTester):
    
    csource = """# encoding: iso-8859-15
                 <%python scope='bogus'>
                 </%python>
                 euro:\xa4
                 """

    expected_error = Syntax

class ErrorInModuleComponent(HTMLDecodingErrorBase,
                             testbase.ComponentTester):
    csource = """# encoding: iso-8859-15
                 def f(m):
                     return u'euro:\xa4' + 1
                 """

    def testFileComponent(self):
        def comp_maker(csource):
            module = self.makeModule(csource)
            return module.f
        self._testComponent(comp_maker)

    def testMemoryComponent(self):
        pass


class AnonymousPythonErrorInComponentTests(HTMLDecodingErrorBase,
                                           testbase.ComponentTester):
    csource = """# encoding: iso-8859-15
                 <%python>
                 g = dict()
                 eval(compile('# encoding: iso-8859-15\\n'
                              'def f(): return u\"euro:\xa4\" + 1\\n',
                              '<memory>', 'exec', 0, True), g, g)
                 f = g['f']
                 </%python>
                 % f()
                 """

    wrapped_error = TypeError

    expected_html = "2: None"


################################################################        

import codecs, StringIO
from myghty.exception import _parse_encoding

class parse_encoding_Tests(testbase.MyghtyTest):
    def testParseEncoding(self, src="", coding=None):
        f = StringIO.StringIO(src)
        try:
            self.failUnlessEqual(parse_encoding(f), coding)
        finally:
            self.failUnlessEqual(f.read(), src) # test that file is rewound
        
    def testSimple(self):
        self.testParseEncoding("# -*- coding: foo -*-\n", 'foo')
        self.testParseEncoding("# First line \n"
                               "# vim:fileencoding=bar\n",
                               'bar')
        self.testParseEncoding("# First line\n"
                               "# Second line\n"
                               "# -*- coding: foo -*-\n",
                               None)

    def testBom(self):
        self.testParseEncoding(codecs.BOM_UTF8 + "# Howdy\n",
                               'utf_8')

    def testBadBom(self):
        self.failUnlessRaises(SyntaxError,
                              self.testParseEncoding,
                              codecs.BOM_UTF8 + "# coding: ascii\n",
                              'utf_8')

        self.failUnlessRaises(SyntaxError,
                              self.testParseEncoding,
                              codecs.BOM_UTF8 + "'line1'\n# coding: ascii\n",
                              'utf_8')

    def testEsoteric(self):
        self.testParseEncoding('print """\n'
                               '# coding: foo\n'
                               '"""\n',
                               None)

        self.testParseEncoding('(\n'
                               '# coding: foo\n'
                               ')\n',
                               None)


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