author | Adrien Di Mascio <Adrien.DiMascio@logilab.fr> |
Thu, 23 Jul 2009 15:28:41 +0200 | |
changeset 2456 | aa25d6b244c8 |
parent 1977 | 606923dff11b |
child 2933 | d511ddcd2689 |
child 2996 | 866a2c135c33 |
permissions | -rw-r--r-- |
0 | 1 |
# -*- coding: utf-8 -*- |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1646
diff
changeset
|
2 |
"""unittests for cubicweb.common.uilib |
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1646
diff
changeset
|
3 |
|
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1646
diff
changeset
|
4 |
:organization: Logilab |
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1646
diff
changeset
|
5 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1646
diff
changeset
|
6 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1646
diff
changeset
|
7 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1646
diff
changeset
|
8 |
""" |
0 | 9 |
|
10 |
__docformat__ = "restructuredtext en" |
|
11 |
||
12 |
from logilab.common.testlib import TestCase, unittest_main |
|
13 |
from logilab.common.tree import Node |
|
14 |
||
15 |
from cubicweb.common import uilib |
|
16 |
||
17 |
class UILIBTC(TestCase): |
|
18 |
||
19 |
def test_remove_tags(self): |
|
20 |
"""make sure remove_tags remove all tags""" |
|
21 |
data = [ |
|
22 |
('<h1>Hello</h1>', 'Hello'), |
|
23 |
('<h1>Hello <a href="foo/bar"><b>s</b>pam</a></h1>', 'Hello spam'), |
|
24 |
('<br>Hello<img src="doh.png"/>', 'Hello'), |
|
25 |
('<p></p>', ''), |
|
26 |
] |
|
27 |
for text, expected in data: |
|
28 |
got = uilib.remove_html_tags(text) |
|
29 |
self.assertEquals(got, expected) |
|
1646 | 30 |
|
350
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
343
diff
changeset
|
31 |
def test_fallback_safe_cut(self): |
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
343
diff
changeset
|
32 |
self.assertEquals(uilib.fallback_safe_cut(u'ab <a href="hello">cd</a>', 4), u'ab c...') |
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
343
diff
changeset
|
33 |
self.assertEquals(uilib.fallback_safe_cut(u'ab <a href="hello">cd</a>', 5), u'ab <a href="hello">cd</a>') |
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
343
diff
changeset
|
34 |
self.assertEquals(uilib.fallback_safe_cut(u'ab <a href="hello">&d</a>', 4), u'ab &...') |
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
343
diff
changeset
|
35 |
self.assertEquals(uilib.fallback_safe_cut(u'ab <a href="hello">&d</a> ef', 5), u'ab &d...') |
362
a6a319f000c3
use mtconverter's html_unescape rather than saxutils' escape to deal with any html entity
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
350
diff
changeset
|
36 |
self.assertEquals(uilib.fallback_safe_cut(u'ab <a href="hello">ìd</a>', 4), u'ab ì...') |
350
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
343
diff
changeset
|
37 |
self.assertEquals(uilib.fallback_safe_cut(u'& <a href="hello">&d</a> ef', 4), u'& &d...') |
1646 | 38 |
|
350
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
343
diff
changeset
|
39 |
def test_lxml_safe_cut(self): |
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
343
diff
changeset
|
40 |
self.assertEquals(uilib.safe_cut(u'aaa<div>aaad</div> ef', 4), u'<p>aaa</p><div>a...</div>') |
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
343
diff
changeset
|
41 |
self.assertEquals(uilib.safe_cut(u'aaa<div>aaad</div> ef', 7), u'<p>aaa</p><div>aaad</div>...') |
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
343
diff
changeset
|
42 |
self.assertEquals(uilib.safe_cut(u'aaa<div>aaad</div>', 7), u'<p>aaa</p><div>aaad</div>') |
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
343
diff
changeset
|
43 |
# Missing ellipsis due to space management but we don't care |
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
343
diff
changeset
|
44 |
self.assertEquals(uilib.safe_cut(u'ab <a href="hello">&d</a>', 4), u'<p>ab <a href="hello">&...</a></p>') |
0 | 45 |
|
46 |
def test_cut(self): |
|
160
cf9ab83b1cf3
adding tests for safe_cut (and text_cut_letters)
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
0
diff
changeset
|
47 |
"""tests uilib.cut() behaviour""" |
0 | 48 |
data = [ |
49 |
('hello', 'hello'), |
|
350
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
343
diff
changeset
|
50 |
('hello world', 'hello wo...'), |
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
343
diff
changeset
|
51 |
("hell<b>O'</b> world", "hell<b>O..."), |
0 | 52 |
] |
53 |
for text, expected in data: |
|
54 |
got = uilib.cut(text, 8) |
|
55 |
self.assertEquals(got, expected) |
|
56 |
||
350
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
343
diff
changeset
|
57 |
def test_text_cut(self): |
0 | 58 |
"""tests uilib.text_cut() behaviour with no text""" |
350
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
343
diff
changeset
|
59 |
data = [('',''), |
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
343
diff
changeset
|
60 |
("""Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod |
0 | 61 |
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, |
62 |
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo |
|
63 |
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse |
|
350
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
343
diff
changeset
|
64 |
cillum dolore eu fugiat nulla pariatur.""", |
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
343
diff
changeset
|
65 |
"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod \ |
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
343
diff
changeset
|
66 |
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, \ |
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
343
diff
changeset
|
67 |
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo \ |
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
343
diff
changeset
|
68 |
consequat."), |
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
343
diff
changeset
|
69 |
("""Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod |
0 | 70 |
tempor incididunt ut labore et dolore magna aliqua Ut enim ad minim veniam, |
71 |
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo |
|
72 |
consequat Duis aute irure dolor in reprehenderit in voluptate velit esse |
|
73 |
cillum dolore eu fugiat nulla pariatur Excepteur sint occaecat cupidatat non |
|
74 |
proident, sunt in culpa qui officia deserunt mollit anim id est laborum |
|
350
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
343
diff
changeset
|
75 |
""", |
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
343
diff
changeset
|
76 |
"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod \ |
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
343
diff
changeset
|
77 |
tempor incididunt ut labore et dolore magna aliqua Ut enim ad minim veniam, \ |
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
343
diff
changeset
|
78 |
quis nostrud exercitation ullamco laboris nisi"), |
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
343
diff
changeset
|
79 |
] |
0 | 80 |
for text, expected in data: |
81 |
got = uilib.text_cut(text, 30) |
|
82 |
self.assertEquals(got, expected) |
|
83 |
||
84 |
tree = ('root', ( |
|
85 |
('child_1_1', ( |
|
86 |
('child_2_1', ()), ('child_2_2', ( |
|
87 |
('child_3_1', ()), |
|
88 |
('child_3_2', ()), |
|
89 |
('child_3_3', ()), |
|
90 |
)))), |
|
91 |
('child_1_2', (('child_2_3', ()),)))) |
|
92 |
||
93 |
generated_html = """\ |
|
94 |
<table class="tree"> |
|
95 |
<tr><td class="tree_cell" rowspan="2"><div class="tree_cell">root</div></td><td class="tree_cell_1_1"> </td><td class="tree_cell_1_2"> </td><td class="tree_cell" rowspan="2"><div class="tree_cell">child_1_1</div></td><td class="tree_cell_1_1"> </td><td class="tree_cell_1_2"> </td><td class="tree_cell" rowspan="2"><div class="tree_cell">child_2_1</div></td><td class="tree_cell_0_1"> </td><td class="tree_cell_0_2"> </td><td rowspan="2"> </td></tr> |
|
96 |
<tr><td class="tree_cell_1_3"> </td><td class="tree_cell_1_4"> </td><td class="tree_cell_1_3"> </td><td class="tree_cell_1_4"> </td><td class="tree_cell_0_3"> </td><td class="tree_cell_0_4"> </td></tr> |
|
97 |
<tr><td rowspan="2"> </td><td class="tree_cell_2_1"> </td><td class="tree_cell_2_2"> </td><td rowspan="2"> </td><td class="tree_cell_4_1"> </td><td class="tree_cell_4_2"> </td><td class="tree_cell" rowspan="2"><div id="selected" class="tree_cell">child_2_2</div></td><td class="tree_cell_1_1"> </td><td class="tree_cell_1_2"> </td><td class="tree_cell" rowspan="2"><div class="tree_cell">child_3_1</div></td></tr> |
|
98 |
<tr><td class="tree_cell_2_3"> </td><td class="tree_cell_2_4"> </td><td class="tree_cell_4_3"> </td><td class="tree_cell_4_4"> </td><td class="tree_cell_1_3"> </td><td class="tree_cell_1_4"> </td></tr> |
|
99 |
<tr><td rowspan="2"> </td><td class="tree_cell_2_1"> </td><td class="tree_cell_2_2"> </td><td rowspan="2"> </td><td class="tree_cell_0_1"> </td><td class="tree_cell_0_2"> </td><td rowspan="2"> </td><td class="tree_cell_3_1"> </td><td class="tree_cell_3_2"> </td><td class="tree_cell" rowspan="2"><div class="tree_cell">child_3_2</div></td></tr> |
|
100 |
<tr><td class="tree_cell_2_3"> </td><td class="tree_cell_2_4"> </td><td class="tree_cell_0_3"> </td><td class="tree_cell_0_4"> </td><td class="tree_cell_3_3"> </td><td class="tree_cell_3_4"> </td></tr> |
|
101 |
<tr><td rowspan="2"> </td><td class="tree_cell_2_1"> </td><td class="tree_cell_2_2"> </td><td rowspan="2"> </td><td class="tree_cell_0_1"> </td><td class="tree_cell_0_2"> </td><td rowspan="2"> </td><td class="tree_cell_4_1"> </td><td class="tree_cell_4_2"> </td><td class="tree_cell" rowspan="2"><div class="tree_cell">child_3_3</div></td></tr> |
|
102 |
<tr><td class="tree_cell_2_3"> </td><td class="tree_cell_2_4"> </td><td class="tree_cell_0_3"> </td><td class="tree_cell_0_4"> </td><td class="tree_cell_4_3"> </td><td class="tree_cell_4_4"> </td></tr> |
|
103 |
<tr><td rowspan="2"> </td><td class="tree_cell_4_1"> </td><td class="tree_cell_4_2"> </td><td class="tree_cell" rowspan="2"><div class="tree_cell">child_1_2</div></td><td class="tree_cell_5_1"> </td><td class="tree_cell_5_2"> </td><td class="tree_cell" rowspan="2"><div class="tree_cell">child_2_3</div></td><td class="tree_cell_0_1"> </td><td class="tree_cell_0_2"> </td><td rowspan="2"> </td></tr> |
|
104 |
<tr><td class="tree_cell_4_3"> </td><td class="tree_cell_4_4"> </td><td class="tree_cell_5_3"> </td><td class="tree_cell_5_4"> </td><td class="tree_cell_0_3"> </td><td class="tree_cell_0_4"> </td></tr> |
|
105 |
</table>\ |
|
106 |
""" |
|
107 |
||
108 |
def make_tree(tuple): |
|
109 |
n = Node(tuple[0]) |
|
110 |
for child in tuple[1]: |
|
111 |
n.append(make_tree(child)) |
|
112 |
return n |
|
1646 | 113 |
|
0 | 114 |
class UIlibHTMLGenerationTC(TestCase): |
115 |
""" a basic tree node, caracterised by an id""" |
|
116 |
def setUp(self): |
|
1646 | 117 |
""" called before each test from this class """ |
0 | 118 |
self.o = make_tree(tree) |
119 |
||
120 |
def test_generated_html(self): |
|
121 |
s = uilib.render_HTML_tree(self.o, selected_node="child_2_2") |
|
122 |
self.assertTextEqual(s, generated_html) |
|
1646 | 123 |
|
124 |
||
0 | 125 |
if __name__ == '__main__': |
126 |
unittest_main() |
|
127 |