Package logilab :: Package common :: Package test :: Module unittest_textutils
[frames] | no frames]

Source Code for Module logilab.common.test.unittest_textutils

  1  # -*- coding: utf-8 -*- 
  2  """ 
  3  unit tests for module textutils 
  4  squeleton generated by /home/syt/cvs_work/logilab/pyreverse/py2tests.py on Sep 08 at 09:1:31 
  5   
  6  """ 
  7  import re 
  8  from os import linesep 
  9   
 10  from logilab.common import textutils as tu # .textutils import * 
 11  from logilab.common.testlib import TestCase, DocTest, unittest_main 
 12   
 13   
 14  if linesep != '\n': 
 15      import re 
 16      LINE_RGX = re.compile(linesep) 
17 - def ulines(string):
18 return LINE_RGX.sub('\n', string)
19 else:
20 - def ulines(string):
21 return string
22
23 -class NormalizeTextTC(TestCase):
24
25 - def test_known_values(self):
26 self.assertEquals(ulines(tu.normalize_text('''some really malformated 27 text. 28 With some times some veeeeeeeeeeeeeeerrrrryyyyyyyyyyyyyyyyyyy loooooooooooooooooooooong linnnnnnnnnnnes 29 30 and empty lines! 31 ''')), 32 '''some really malformated text. With some times some 33 veeeeeeeeeeeeeeerrrrryyyyyyyyyyyyyyyyyyy loooooooooooooooooooooong 34 linnnnnnnnnnnes 35 36 and empty lines!''') 37 self.assertTextEquals(ulines(tu.normalize_text('''\ 38 some ReST formated text 39 ======================= 40 With some times some veeeeeeeeeeeeeeerrrrryyyyyyyyyyyyyyyyyyy loooooooooooooooooooooong linnnnnnnnnnnes 41 and normal lines! 42 43 another paragraph 44 ''', rest=True)), 45 '''\ 46 some ReST formated text 47 ======================= 48 With some times some veeeeeeeeeeeeeeerrrrryyyyyyyyyyyyyyyyyyy 49 loooooooooooooooooooooong linnnnnnnnnnnes 50 and normal lines! 51 52 another paragraph''')
53
55 self.assertEquals(ulines(tu.normalize_text('''petit complement : 56 57 http://www.plonefr.net/blog/archive/2005/10/30/tester-la-future-infrastructure-i18n 58 ''', 80)), 59 '''petit complement : 60 61 http://www.plonefr.net/blog/archive/2005/10/30/tester-la-future-infrastructure-i18n''')
62 63
65 self.assertEquals(ulines(tu.normalize_text("""... Il est donc evident que tout le monde doit lire le compte-rendu de RSH et aller discuter avec les autres si c'est utile ou necessaire. 66 """, rest=True)), """... Il est donc evident que tout le monde doit lire le compte-rendu de RSH et 67 aller discuter avec les autres si c'est utile ou necessaire.""")
68
70 self.assertEquals(ulines(tu.normalize_rest_paragraph("""**nico**: toto""")), 71 """**nico**: toto""")
72
74 self.assertEquals(ulines(tu.normalize_rest_paragraph(""".. _tdm: http://www.editions-eni.fr/Livres/Python-Les-fondamentaux-du-langage---La-programmation-pour-les-scientifiques-Table-des-matieres/.20_adaa41fb-c125-4919-aece-049601e81c8e_0_0.pdf 75 .. _extrait: http://www.editions-eni.fr/Livres/Python-Les-fondamentaux-du-langage---La-programmation-pour-les-scientifiques-Extrait-du-livre/.20_d6eed0be-0d36-4384-be59-2dd09e081012_0_0.pdf""", indent='> ')), 76 """> .. _tdm: 77 > http://www.editions-eni.fr/Livres/Python-Les-fondamentaux-du-langage---La-programmation-pour-les-scientifiques-Table-des-matieres/.20_adaa41fb-c125-4919-aece-049601e81c8e_0_0.pdf 78 > .. _extrait: 79 > http://www.editions-eni.fr/Livres/Python-Les-fondamentaux-du-langage---La-programmation-pour-les-scientifiques-Extrait-du-livre/.20_d6eed0be-0d36-4384-be59-2dd09e081012_0_0.pdf""")
80
82 self.assertEquals(ulines(tu.normalize_paragraph(""".. _tdm: http://www.editions-eni.fr/Livres/Python-Les-fondamentaux-du-langage---La-programmation-pour-les-scientifiques-Table-des-matieres/.20_adaa41fb-c125-4919-aece-049601e81c8e_0_0.pdf 83 .. _extrait: http://www.editions-eni.fr/Livres/Python-Les-fondamentaux-du-langage---La-programmation-pour-les-scientifiques-Extrait-du-livre/.20_d6eed0be-0d36-4384-be59-2dd09e081012_0_0.pdf""", indent='> ')), 84 """> .. _tdm: 85 > http://www.editions-eni.fr/Livres/Python-Les-fondamentaux-du-langage---La-programmation-pour-les-scientifiques-Table-des-matieres/.20_adaa41fb-c125-4919-aece-049601e81c8e_0_0.pdf 86 > .. _extrait: 87 > http://www.editions-eni.fr/Livres/Python-Les-fondamentaux-du-langage---La-programmation-pour-les-scientifiques-Extrait-du-livre/.20_d6eed0be-0d36-4384-be59-2dd09e081012_0_0.pdf""")
88
89 -class NormalizeParagraphTC(TestCase):
90
91 - def test_known_values(self):
92 self.assertEquals(ulines(tu.normalize_text("""This package contains test files shared by the logilab-common package. It isn't 93 necessary to install this package unless you want to execute or look at 94 the tests.""", indent=' ', line_len=70)), 95 """\ 96 This package contains test files shared by the logilab-common 97 package. It isn't necessary to install this package unless you want 98 to execute or look at the tests.""")
99 100
101 -class GetCsvTC(TestCase):
102
103 - def test_known(self):
104 self.assertEquals(tu.splitstrip('a, b,c '), ['a', 'b', 'c'])
105
106 -class UnitsTC(TestCase):
107
108 - def setUp(self):
109 self.units = { 110 'm':60, 111 'KB':1024, 112 'MB':1024*1024, 113 }
114
115 - def test_empty_base(self):
116 self.assertEquals(tu.apply_units('17', {}), 17)
117
118 - def test_empty_inter(self):
119 def inter(value): 120 return int(float(value)) * 2
121 result = tu.apply_units('12.4', {}, inter=inter) 122 self.assertEquals(result, 12 * 2) 123 self.assertIsInstance(result, float)
124
125 - def test_empty_final(self):
126 # int('12.4') raise value error 127 self.assertRaises(ValueError, tu.apply_units,'12.4', {}, final=int)
128
129 - def test_empty_inter_final(self):
130 result = tu.apply_units('12.4', {}, inter=float,final=int) 131 self.assertEquals(result, 12) 132 self.assertIsInstance(result, int)
133
134 - def test_blank_base(self):
135 result = tu.apply_units(' 42 ', {}, final=int) 136 self.assertEquals(result, 42)
137
138 - def test_blank_space(self):
139 result = tu.apply_units(' 1 337 ', {}, final=int) 140 self.assertEquals(result, 1337)
141
142 - def test_blank_coma(self):
143 result = tu.apply_units(' 4,298.42 ', {}) 144 self.assertEquals(result, 4298.42)
145
146 - def test_blank_mixed(self):
147 result = tu.apply_units('45, 317, 337', {},final=int) 148 self.assertEquals(result, 45317337)
149
150 - def test_unit_singleunit_singleletter(self):
151 result = tu.apply_units('15m', self.units) 152 self.assertEquals(result, 15 * self.units['m'] )
153
154 - def test_unit_singleunit_multipleletter(self):
155 result = tu.apply_units('47KB', self.units) 156 self.assertEquals(result, 47 * self.units['KB'] )
157
158 - def test_unit_multipleunit(self):
159 result = tu.apply_units('47KB 1.5MB', self.units) 160 self.assertEquals(result, 47 * self.units['KB'] + 1.5 * self.units['MB'])
161
162 - def test_unit_with_blank(self):
163 result = tu.apply_units('1 000 KB', self.units) 164 self.assertEquals(result, 1000 * self.units['KB'])
165 166 RGX = re.compile('abcd')
167 -class PrettyMatchTC(TestCase):
168
169 - def test_known(self):
170 string = 'hiuherabcdef' 171 self.assertEquals(ulines(tu.pretty_match(RGX.search(string), string)), 172 'hiuherabcdef\n ^^^^')
173 - def test_known_values_1(self):
174 rgx = re.compile('(to*)') 175 string = 'toto' 176 match = rgx.search(string) 177 self.assertEquals(ulines(tu.pretty_match(match, string)), '''toto 178 ^^''')
179
180 - def test_known_values_2(self):
181 rgx = re.compile('(to*)') 182 string = ''' ... ... to to 183 ... ... ''' 184 match = rgx.search(string) 185 self.assertEquals(ulines(tu.pretty_match(match, string)), ''' ... ... to to 186 ^^ 187 ... ...''')
188 189 190
191 -class UnquoteTC(TestCase):
192 - def test(self):
193 self.assertEquals(tu.unquote('"toto"'), 'toto') 194 self.assertEquals(tu.unquote("'l'inenarrable toto'"), "l'inenarrable toto") 195 self.assertEquals(tu.unquote("no quote"), "no quote")
196 197
198 -class ColorizeAnsiTC(TestCase):
199 - def test_known(self):
200 self.assertEquals(tu.colorize_ansi('hello', 'blue', 'strike'), '\x1b[9;34mhello\x1b[0m') 201 self.assertEquals(tu.colorize_ansi('hello', style='strike, inverse'), '\x1b[9;7mhello\x1b[0m') 202 self.assertEquals(tu.colorize_ansi('hello', None, None), 'hello') 203 self.assertEquals(tu.colorize_ansi('hello', '', ''), 'hello')
204 - def test_raise(self):
205 self.assertRaises(KeyError, tu.colorize_ansi, 'hello', 'bleu', None) 206 self.assertRaises(KeyError, tu.colorize_ansi, 'hello', None, 'italique')
207 208
209 -class UnormalizeTC(TestCase):
210 - def test_unormalize(self):
211 data = [(u'\u0153nologie', u'oenologie'), 212 (u'\u0152nologie', u'OEnologie'), 213 (u'l\xf8to', u'loto'), 214 (u'été', u'ete'), 215 ] 216 for input, output in data: 217 yield self.assertEquals, tu.unormalize(input), output
218
219 -class ModuleDocTest(DocTest):
220 """test doc test in this module""" 221 module = tu
222 # from logilab.common import textutils as module 223 del DocTest # necessary if we don't want it to be executed (we don't...) 224 225 if __name__ == '__main__': 226 unittest_main() 227