| 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 | |
|---|
| 54 | def test_nonregr_unsplitable_word(self): |
|---|
| 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 | |
|---|
| 64 | def test_nonregr_rest_normalize(self): |
|---|
| 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 | |
|---|
| 69 | def test_normalize_rest_paragraph(self): |
|---|
| 70 | self.assertEquals(ulines(tu.normalize_rest_paragraph("""**nico**: toto""")), |
|---|
| 71 | """**nico**: toto""") |
|---|
| 72 | |
|---|
| 73 | def test_normalize_rest_paragraph2(self): |
|---|
| 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 | |
|---|
| 81 | def test_normalize_paragraph2(self): |
|---|
| 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.get_csv('a, b,c '), ['a', 'b', 'c']) |
|---|
| 105 | |
|---|
| 106 | |
|---|
| 107 | RGX = re.compile('abcd') |
|---|
| 108 | class PrettyMatchTC(TestCase): |
|---|
| 109 | |
|---|
| 110 | def test_known(self): |
|---|
| 111 | string = 'hiuherabcdef' |
|---|
| 112 | self.assertEquals(ulines(tu.pretty_match(RGX.search(string), string)), |
|---|
| 113 | 'hiuherabcdef\n ^^^^') |
|---|
| 114 | def test_known_values_1(self): |
|---|
| 115 | rgx = re.compile('(to*)') |
|---|
| 116 | string = 'toto' |
|---|
| 117 | match = rgx.search(string) |
|---|
| 118 | self.assertEquals(ulines(tu.pretty_match(match, string)), '''toto |
|---|
| 119 | ^^''') |
|---|
| 120 | |
|---|
| 121 | def test_known_values_2(self): |
|---|
| 122 | rgx = re.compile('(to*)') |
|---|
| 123 | string = ''' ... ... to to |
|---|
| 124 | ... ... ''' |
|---|
| 125 | match = rgx.search(string) |
|---|
| 126 | self.assertEquals(ulines(tu.pretty_match(match, string)), ''' ... ... to to |
|---|
| 127 | ^^ |
|---|
| 128 | ... ...''') |
|---|
| 129 | |
|---|
| 130 | |
|---|
| 131 | |
|---|
| 132 | class UnquoteTC(TestCase): |
|---|
| 133 | def test(self): |
|---|
| 134 | self.assertEquals(tu.unquote('"toto"'), 'toto') |
|---|
| 135 | self.assertEquals(tu.unquote("'l'inenarrable toto'"), "l'inenarrable toto") |
|---|
| 136 | self.assertEquals(tu.unquote("no quote"), "no quote") |
|---|
| 137 | |
|---|
| 138 | |
|---|
| 139 | class ColorizeAnsiTC(TestCase): |
|---|
| 140 | def test_known(self): |
|---|
| 141 | self.assertEquals(tu.colorize_ansi('hello', 'blue', 'strike'), '\x1b[9;34mhello\x1b[0m') |
|---|
| 142 | self.assertEquals(tu.colorize_ansi('hello', style='strike, inverse'), '\x1b[9;7mhello\x1b[0m') |
|---|
| 143 | self.assertEquals(tu.colorize_ansi('hello', None, None), 'hello') |
|---|
| 144 | self.assertEquals(tu.colorize_ansi('hello', '', ''), 'hello') |
|---|
| 145 | def test_raise(self): |
|---|
| 146 | self.assertRaises(KeyError, tu.colorize_ansi, 'hello', 'bleu', None) |
|---|
| 147 | self.assertRaises(KeyError, tu.colorize_ansi, 'hello', None, 'italique') |
|---|
| 148 | |
|---|
| 149 | |
|---|
| 150 | class UnormalizeTC(TestCase): |
|---|
| 151 | def test_unormalize(self): |
|---|
| 152 | data = [(u'\u0153nologie', u'oenologie'), |
|---|
| 153 | (u'\u0152nologie', u'OEnologie'), |
|---|
| 154 | (u'l\xf8to', u'loto'), |
|---|
| 155 | (u'été', u'ete'), |
|---|
| 156 | ] |
|---|
| 157 | for input, output in data: |
|---|
| 158 | yield self.assertEquals, tu.unormalize(input), output |
|---|
| 159 | |
|---|
| 160 | class ModuleDocTest(DocTest): |
|---|
| 161 | """test doc test in this module""" |
|---|
| 162 | module = tu |
|---|
| 163 | # from logilab.common import textutils as module |
|---|
| 164 | del DocTest # necessary if we don't want it to be executed (we don't...) |
|---|
| 165 | |
|---|
| 166 | if __name__ == '__main__': |
|---|
| 167 | unittest_main() |
|---|