k_encoding_tests.py :  » Web-Frameworks » Karrigell » Karrigell-3.1 » karrigell » core » k_encodings » 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 » Karrigell 
Karrigell » Karrigell 3.1 » karrigell » core » k_encodings » k_encoding_tests.py
#!/bin/env python
#  -*- coding: iso-8859-15 -*-
# File: k_encoding_tests.py
"""Tests for k_encoding module
"""

# k_encoding_charsets can be rebuilt dynamically (see k_encoding_makecharsets.py)
try :
    import k_encoding_charsets
except :
    import k_encoding_makecharsets
    k_encoding_makecharsets.make()
    import k_encoding_charsets

import k_encoding

# Base dictionnaries with encoding mappings.
# Note that dict indexs use normenc() to be normalized.
encoding2python_map = k_encoding_charsets.encoding2python_map
encoding2mime_map = k_encoding_charsets.encoding2mime_map


python_file1 = (u"""#!/bin/env python
# -*- coding: %(the_encoding)s -*-
# Hello you.
def donothing (x) : pass

""","PY")

python_file2 = (u"""<%%
#!/bin/env python
# -*- coding:  %(the_encoding)s -*-
# Hello you.
def donothing (x) : pass

%%><?xml version="1" encoding="UTF8" ?>
<!DOCTYPE html PUBLIC "//truc/machin" >
<html>
  <head>
  </head>
  <body>
  </body>
</html>
""","PIH")

xml_file1 = (u"""<?xml   version="1.0"  encoding="%(the_encoding)s"  standalone="yes" ?>
<!DOCTYPE shadock  />
<shadock mode='on'>
    <pompe>up</pompe>
    <pompe>down</pompe>
    <pompe>up</pompe>
    <pompe>down</pompe>
    <pompe>up</pompe>
    <pompe>down</pompe>
    <pompe>up</pompe>
    <pompe>down</pompe>
    <pompe>up</pompe>
    <pompe>down</pompe>
    <pompe>up</pompe>
    <pompe>down</pompe>
</shadock>
""",'XML')

html_file1 = (u"""<html>
<head>
    <title>A document title</title>
    <meta http-equiv="Content-Type" content="text/html; charset=%(the_encoding)s">
</head>
<body>pompe pompe pompe pompe pompe</body>
""","HTML")

css_file1 = (u"""


@charset  "%(the_encoding)s"

h1 { text-align: center;  font-size: 200%%; }
h2 { text-align: left; font-size:  150%%;padding-left: 2em; }
""","CSS")


tests_sets = [
    python_file1,
    python_file2,
    xml_file1,
    html_file1,
    css_file1
    ]

print "%-5s %-19s RESULT[%-6s]:%s"%("FORM","ENCODING","WHERE","RETURNED")

encoding_tests = [
    "ASCII",
    "US-ASCII",
    "ISO-8859-1",
    "UTF8"
    ]
    
for e in encoding_tests :
    p = encoding2python_map.get(k_encoding.normenc(e),None)
    assert p
    m = encoding2mime_map.get(k_encoding.normenc(e),None)

    # Test with each exemple.
    for text,comment in tests_sets :
        #  Make text containing encoding directive.
        t = text % { 'the_encoding' : e }

        # Encode it into the encoding to test.
        try :
            s = t.encode(p,"replace")
        except LookupError: # Unsupported encoding.
            continue

        # Tin tin tin tin....
        result = k_encoding.guess_buffer_encoding(s)
        if  result :
            foundin = result.directive
            print "%-5s %-20s    OK[%-6s]:"%(comment,e,result.directive),result
        else :
            foundin = "!found"
            print "%-5s %-20s ERROR[%-6s]:"%(comment,e,"!found"),(e,p)

        # By using command lines like:
        #        python k_encoding_tests.py | grep ERROR | grep XML
        # We can easily found problem cases.
        
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.