Package python-module-logilab-mtconverter-0 :: Package 8 :: Package 4 :: Package test :: Module unittest_utils
[frames] | no frames]

Source Code for Module python-module-logilab-mtconverter-0.8.4.test.unittest_utils

  1  # -*- coding: utf-8 -*- 
  2  # copyright 2006-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved. 
  3  # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr 
  4  # 
  5  # This file is part of logilab-mtconverter. 
  6  # 
  7  # logilab-mtconverter is free software: you can redistribute it and/or modify it 
  8  # under the terms of the GNU Lesser General Public License as published by the 
  9  # Free Software Foundation, either version 2.1 of the License, or (at your 
 10  # option) any later version. 
 11  # 
 12  # logilab-mtconverter is distributed in the hope that it will be useful, but 
 13  # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
 14  # FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License 
 15  # for more details. 
 16  # 
 17  # You should have received a copy of the GNU Lesser General Public License along 
 18  # with logilab-mtconverter. If not, see <http://www.gnu.org/licenses/>. 
 19  from logilab.common.testlib import TestCase, unittest_main 
 20   
 21  import locale 
 22  from StringIO import StringIO 
 23  from logilab.mtconverter import * 
 24   
 25  SPECIAL_CHARS = { 
 26      '\f' : '\n', 
 27      '\b' : ' ', 
 28      '\n' : '\n', 
 29      '\r' : '\r', 
 30      '\r\n' : '\r\n', 
 31      '\t' : '\t', 
 32      '\v' : '\n', 
 33      } 
 34   
35 -class HtmlEscapeTC(TestCase):
36
37 - def test_escape(self):
38 for data, expected in [('toto', 'toto'), 39 ('r&d', 'r&amp;d'), 40 ('23<12 && 3>2', '23&lt;12 &amp;&amp; 3&gt;2'), 41 ('d"h"', 'd&quot;h&quot;'), 42 ("h'", 'h&#39;'), 43 ]: 44 yield self.assertEqual, xml_escape(data), expected
45
47 for car, trcar in SPECIAL_CHARS.items(): 48 yield self.assertEqual, xml_escape(car), trcar 49 for carnum in xrange(32): 50 car = chr(carnum) 51 if car in SPECIAL_CHARS: 52 continue 53 yield self.assertEqual, xml_escape(car), ' ' 54 yield self.assertEqual, xml_escape(u'é'), u'é'
55
57 for car, trcar in SPECIAL_CHARS.items(): 58 yield self.assertEqual, xml_escape(unicode(car)), trcar 59 for carnum in xrange(32): 60 car = chr(carnum) 61 if car in SPECIAL_CHARS: 62 continue 63 yield self.assertEqual, xml_escape(unicode(car)), ' '
64
65 - def test_html_unescape(self):
66 for data, expected in [('toto', 'toto'), 67 ('r&amp;d', 'r&d' ), 68 ('23&lt;12 &amp;&amp; 3&gt;2', '23<12 && 3>2'), 69 ('d&quot;h&quot;', 'd"h"'), 70 ('h&#39;', "h'"), 71 ('x &equiv; y', u"x \u2261 y"), 72 ]: 73 yield self.assertEqual, html_unescape(data), expected
74 75
76 -class GuessEncodingTC(TestCase):
77
79 data = '''# -*- coding: latin1 -*-''' 80 self.assertEqual(guess_encoding(data), 'latin1')
81
83 data = '''# -*- coding: latin1 -*-''' 84 self.assertEqual(guess_encoding(StringIO(data)), 'latin1')
85
87 data = '''<?xml version="1.0" encoding="latin1"?> 88 <root/>''' 89 self.assertEqual(guess_encoding(data), 'latin1')
90
92 data = '''<html xmlns="http://www.w3.org/1999/xhtml" xmlns:erudi="http://www.logilab.fr/" xml:lang="fr" lang="fr"> 93 <head> 94 <base href="http://intranet.logilab.fr/jpl/" /><meta http-equiv="content-type" content="text/html; charset=latin1"/> 95 </head> 96 <body><p>hello world</p> 97 </body> 98 </html>''' 99 self.assertEqual(guess_encoding(data), 'latin1')
100
101 - def test_bad_detection(self):
102 data = '''class SchemaViewer(object): 103 """return an ureport layout for some part of a schema""" 104 def __init__(self, req=None, encoding=None): 105 ''' 106 # ascii detected by chardet 107 try: 108 import chardet 109 self.assertEqual(guess_encoding(data), 'ascii') 110 except ImportError: 111 self.assertEqual(guess_encoding(data), DEFAULT_ENCODING)
112
113 -class GuessMimetymeAndEncodingTC(TestCase):
114 - def test_base(self):
115 format, encoding = guess_mimetype_and_encoding(filename=u"foo.txt", data="xxx") 116 self.assertEqual(format, u'text/plain') 117 self.assertEqual(encoding, locale.getpreferredencoding())
118
120 format, encoding = guess_mimetype_and_encoding(filename=u"foo.txt.gz", data="xxx") 121 self.assertEqual(format, u'text/plain') 122 self.assertEqual(encoding, u'gzip') 123 format, encoding = guess_mimetype_and_encoding(filename=u"foo.txt.gz", data="xxx", 124 format='application/gzip') 125 self.assertEqual(format, u'text/plain') 126 self.assertEqual(encoding, u'gzip') 127 format, encoding = guess_mimetype_and_encoding(filename=u"foo.gz", data="xxx") 128 self.assertEqual(format, u'application/gzip') 129 self.assertEqual(encoding, None)
130
132 format, encoding = guess_mimetype_and_encoding(filename=u"foo.txt.bz2", data="xxx") 133 self.assertEqual(format, u'text/plain') 134 self.assertEqual(encoding, u'bzip2') 135 format, encoding = guess_mimetype_and_encoding(filename=u"foo.txt.bz2", data="xxx", 136 format='application/bzip2') 137 self.assertEqual(format, u'text/plain') 138 self.assertEqual(encoding, u'bzip2') 139 format, encoding = guess_mimetype_and_encoding(filename=u"foo.bz2", data="xxx") 140 self.assertEqual(format, u'application/bzip2') 141 self.assertEqual(encoding, None)
142
144 format, encoding = guess_mimetype_and_encoding(filename=u"foo.123", data="xxx") 145 self.assertEqual(format, u'application/octet-stream') 146 self.assertEqual(encoding, None)
147 148
149 -class TransformDataTC(TestCase):
151 data = TransformData('''<?xml version="1.0" encoding="latin1"?> 152 <root/>''', 'text/xml') 153 self.assertEqual(data.encoding, 'latin1')
154 155 156 if __name__ == '__main__': 157 unittest_main() 158