author | Adrien Di Mascio <Adrien.DiMascio@logilab.fr> |
Wed, 10 Dec 2008 10:36:38 +0100 | |
changeset 202 | 74f319aff847 |
parent 160 | cf9ab83b1cf3 |
child 343 | 50073bc6df5e |
permissions | -rw-r--r-- |
0 | 1 |
# -*- coding: utf-8 -*- |
2 |
"""unittests for cubicweb.common.uilib""" |
|
3 |
||
4 |
__docformat__ = "restructuredtext en" |
|
5 |
||
6 |
from logilab.common.testlib import TestCase, unittest_main |
|
7 |
from logilab.common.tree import Node |
|
8 |
||
9 |
from cubicweb.common import uilib |
|
10 |
||
11 |
class UILIBTC(TestCase): |
|
12 |
||
13 |
||
14 |
def test_remove_tags(self): |
|
15 |
"""make sure remove_tags remove all tags""" |
|
16 |
data = [ |
|
17 |
('<h1>Hello</h1>', 'Hello'), |
|
18 |
('<h1>Hello <a href="foo/bar"><b>s</b>pam</a></h1>', 'Hello spam'), |
|
19 |
('<br>Hello<img src="doh.png"/>', 'Hello'), |
|
20 |
('<p></p>', ''), |
|
21 |
] |
|
22 |
for text, expected in data: |
|
23 |
got = uilib.remove_html_tags(text) |
|
24 |
self.assertEquals(got, expected) |
|
160
cf9ab83b1cf3
adding tests for safe_cut (and text_cut_letters)
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
0
diff
changeset
|
25 |
|
0 | 26 |
def test_safe_cut(self): |
160
cf9ab83b1cf3
adding tests for safe_cut (and text_cut_letters)
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
0
diff
changeset
|
27 |
""" tests uilib.safe_cut() behaviour with very long text""" |
cf9ab83b1cf3
adding tests for safe_cut (and text_cut_letters)
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
0
diff
changeset
|
28 |
|
0 | 29 |
data = [ |
160
cf9ab83b1cf3
adding tests for safe_cut (and text_cut_letters)
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
0
diff
changeset
|
30 |
('opkolk', '<div><p>opkolk</p></div>'), |
cf9ab83b1cf3
adding tests for safe_cut (and text_cut_letters)
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
0
diff
changeset
|
31 |
("""<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod |
cf9ab83b1cf3
adding tests for safe_cut (and text_cut_letters)
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
0
diff
changeset
|
32 |
tempor incididunt <strong>ut</strong> labore et dolore magna aliqua. Ut enim ad minim veniam, |
cf9ab83b1cf3
adding tests for safe_cut (and text_cut_letters)
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
0
diff
changeset
|
33 |
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo |
cf9ab83b1cf3
adding tests for safe_cut (and text_cut_letters)
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
0
diff
changeset
|
34 |
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse |
cf9ab83b1cf3
adding tests for safe_cut (and text_cut_letters)
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
0
diff
changeset
|
35 |
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non |
cf9ab83b1cf3
adding tests for safe_cut (and text_cut_letters)
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
0
diff
changeset
|
36 |
proident, sunt in culpa qui officia deserunt mollit anim id est laborum. |
cf9ab83b1cf3
adding tests for safe_cut (and text_cut_letters)
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
0
diff
changeset
|
37 |
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod |
cf9ab83b1cf3
adding tests for safe_cut (and text_cut_letters)
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
0
diff
changeset
|
38 |
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, |
cf9ab83b1cf3
adding tests for safe_cut (and text_cut_letters)
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
0
diff
changeset
|
39 |
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo |
cf9ab83b1cf3
adding tests for safe_cut (and text_cut_letters)
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
0
diff
changeset
|
40 |
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse |
cf9ab83b1cf3
adding tests for safe_cut (and text_cut_letters)
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
0
diff
changeset
|
41 |
cillum dolore eu fugiat nulla pariatur.</p> ""","""<div><p>Lorem ipsum dolor sit amet, consectetur</p></div>"""), |
cf9ab83b1cf3
adding tests for safe_cut (and text_cut_letters)
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
0
diff
changeset
|
42 |
("""<p>empor incididunt utlabore et dolore magna aliqua. Ut enim ad minim veniam, |
cf9ab83b1cf3
adding tests for safe_cut (and text_cut_letters)
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
0
diff
changeset
|
43 |
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo |
cf9ab83b1cf3
adding tests for safe_cut (and text_cut_letters)
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
0
diff
changeset
|
44 |
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse |
cf9ab83b1cf3
adding tests for safe_cut (and text_cut_letters)
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
0
diff
changeset
|
45 |
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non |
cf9ab83b1cf3
adding tests for safe_cut (and text_cut_letters)
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
0
diff
changeset
|
46 |
proident, sunt in culpa qui officia d</p>""","""<div><p>empor incididunt utlabore et dolore magna aliqua.</p></div>"""), |
cf9ab83b1cf3
adding tests for safe_cut (and text_cut_letters)
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
0
diff
changeset
|
47 |
("""empor <strong>incididunt</strong> utlabore et dolore magna aliqua. Ut enim ad minim veniam, |
cf9ab83b1cf3
adding tests for safe_cut (and text_cut_letters)
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
0
diff
changeset
|
48 |
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo |
cf9ab83b1cf3
adding tests for safe_cut (and text_cut_letters)
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
0
diff
changeset
|
49 |
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse |
cf9ab83b1cf3
adding tests for safe_cut (and text_cut_letters)
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
0
diff
changeset
|
50 |
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non |
cf9ab83b1cf3
adding tests for safe_cut (and text_cut_letters)
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
0
diff
changeset
|
51 |
proident, sunt in culpa qui officia""","""<div><p>empor <strong>incididunt</strong> utlabore et dolore magna aliqua.</p></div>"""), |
cf9ab83b1cf3
adding tests for safe_cut (and text_cut_letters)
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
0
diff
changeset
|
52 |
("""<p>Lorem <strong>ipsum</strong> dolor <it>sit</it> amet, <strong>consectetur</strong> adipisicing elit, sed do eiusmod |
cf9ab83b1cf3
adding tests for safe_cut (and text_cut_letters)
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
0
diff
changeset
|
53 |
tempor incididunt <strong>ut</strong> labore et dolore magna aliqua. Ut enim ad minim veniam, |
cf9ab83b1cf3
adding tests for safe_cut (and text_cut_letters)
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
0
diff
changeset
|
54 |
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo |
cf9ab83b1cf3
adding tests for safe_cut (and text_cut_letters)
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
0
diff
changeset
|
55 |
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse |
cf9ab83b1cf3
adding tests for safe_cut (and text_cut_letters)
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
0
diff
changeset
|
56 |
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non |
cf9ab83b1cf3
adding tests for safe_cut (and text_cut_letters)
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
0
diff
changeset
|
57 |
proident, sunt in culpa qui officia deserunt mollit anim id est laborum. |
cf9ab83b1cf3
adding tests for safe_cut (and text_cut_letters)
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
0
diff
changeset
|
58 |
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod |
cf9ab83b1cf3
adding tests for safe_cut (and text_cut_letters)
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
0
diff
changeset
|
59 |
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, |
cf9ab83b1cf3
adding tests for safe_cut (and text_cut_letters)
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
0
diff
changeset
|
60 |
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo |
cf9ab83b1cf3
adding tests for safe_cut (and text_cut_letters)
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
0
diff
changeset
|
61 |
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse |
cf9ab83b1cf3
adding tests for safe_cut (and text_cut_letters)
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
0
diff
changeset
|
62 |
cillum dolore eu fugiat nulla pariatur.</p>""","""<div><p>Lorem <strong>ipsum</strong> dolor <it>sit</it> amet, <strong>consectetur</strong></p></div>"""), |
0 | 63 |
] |
64 |
for text, expected in data: |
|
160
cf9ab83b1cf3
adding tests for safe_cut (and text_cut_letters)
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
0
diff
changeset
|
65 |
got = uilib.safe_cut(text, 30) |
0 | 66 |
self.assertEquals(got, expected) |
67 |
||
68 |
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
|
69 |
"""tests uilib.cut() behaviour""" |
0 | 70 |
data = [ |
71 |
('hello', 'hello'), |
|
72 |
('hello world', 'hello...'), |
|
73 |
("hell<b>O'</b> world", "hell<..."), |
|
74 |
] |
|
75 |
for text, expected in data: |
|
76 |
got = uilib.cut(text, 8) |
|
77 |
self.assertEquals(got, expected) |
|
78 |
||
79 |
def test_text_cut_no_text(self): |
|
80 |
"""tests uilib.text_cut() behaviour with no text""" |
|
81 |
data = [('','')] |
|
82 |
for text, expected in data: |
|
83 |
got = uilib.text_cut(text, 8) |
|
84 |
self.assertEquals(got, expected) |
|
85 |
||
86 |
def test_text_cut_long_text(self): |
|
87 |
"""tests uilib.text_cut() behaviour with long text""" |
|
88 |
data = [("""Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod |
|
89 |
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, |
|
90 |
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo |
|
91 |
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse |
|
92 |
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non |
|
93 |
proident, sunt in culpa qui officia deserunt mollit anim id est laborum. |
|
94 |
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod |
|
95 |
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, |
|
96 |
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo |
|
97 |
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse |
|
98 |
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non |
|
99 |
proident, sunt in culpa qui officia deserunt mollit anim id est laborum. |
|
100 |
""","""Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod |
|
101 |
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, |
|
102 |
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo |
|
103 |
consequat.""")] |
|
104 |
for text, expected in data: |
|
105 |
got = uilib.text_cut(text, 30) |
|
106 |
self.assertEquals(got, expected) |
|
107 |
||
108 |
def test_text_cut_no_point(self): |
|
109 |
"""tests uilib.text_cut() behaviour with no point""" |
|
110 |
data = [("""Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod |
|
111 |
tempor incididunt ut labore et dolore magna aliqua Ut enim ad minim veniam, |
|
112 |
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo |
|
113 |
consequat Duis aute irure dolor in reprehenderit in voluptate velit esse |
|
114 |
cillum dolore eu fugiat nulla pariatur Excepteur sint occaecat cupidatat non |
|
115 |
proident, sunt in culpa qui officia deserunt mollit anim id est laborum |
|
116 |
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod |
|
117 |
tempor incididunt ut labore et dolore magna aliqua Ut enim ad minim veniam, |
|
118 |
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo |
|
119 |
consequat Duis aute irure dolor in reprehenderit in voluptate velit esse |
|
120 |
cillum dolore eu fugiat nulla pariatur Excepteur sint occaecat cupidatat non |
|
121 |
proident, sunt in culpa qui officia deserunt mollit anim id est laborum |
|
122 |
""","""Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod |
|
123 |
tempor incididunt ut labore et dolore magna aliqua Ut enim ad minim veniam, |
|
124 |
quis nostrud exercitation ullamco laboris nisi""")] |
|
125 |
for text, expected in data: |
|
126 |
got = uilib.text_cut(text, 30) |
|
127 |
self.assertEquals(got, expected) |
|
128 |
||
129 |
def test_ajax_replace_url(self): |
|
130 |
# NOTE: for the simplest use cases, we could use doctest |
|
131 |
arurl = uilib.ajax_replace_url |
|
132 |
self.assertEquals(arurl('foo', 'Person P'), |
|
133 |
"javascript: replacePageChunk('foo', 'Person%20P');") |
|
134 |
self.assertEquals(arurl('foo', 'Person P', 'oneline'), |
|
135 |
"javascript: replacePageChunk('foo', 'Person%20P', 'oneline');") |
|
136 |
self.assertEquals(arurl('foo', 'Person P', 'oneline', name='bar', age=12), |
|
137 |
'javascript: replacePageChunk(\'foo\', \'Person%20P\', \'oneline\', {"age": 12, "name": "bar"});') |
|
138 |
self.assertEquals(arurl('foo', 'Person P', name='bar', age=12), |
|
139 |
'javascript: replacePageChunk(\'foo\', \'Person%20P\', \'null\', {"age": 12, "name": "bar"});') |
|
140 |
||
141 |
tree = ('root', ( |
|
142 |
('child_1_1', ( |
|
143 |
('child_2_1', ()), ('child_2_2', ( |
|
144 |
('child_3_1', ()), |
|
145 |
('child_3_2', ()), |
|
146 |
('child_3_3', ()), |
|
147 |
)))), |
|
148 |
('child_1_2', (('child_2_3', ()),)))) |
|
149 |
||
150 |
generated_html = """\ |
|
151 |
<table class="tree"> |
|
152 |
<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> |
|
153 |
<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> |
|
154 |
<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> |
|
155 |
<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> |
|
156 |
<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> |
|
157 |
<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> |
|
158 |
<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> |
|
159 |
<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> |
|
160 |
<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> |
|
161 |
<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> |
|
162 |
</table>\ |
|
163 |
""" |
|
164 |
||
165 |
def make_tree(tuple): |
|
166 |
n = Node(tuple[0]) |
|
167 |
for child in tuple[1]: |
|
168 |
n.append(make_tree(child)) |
|
169 |
return n |
|
170 |
||
171 |
class UIlibHTMLGenerationTC(TestCase): |
|
172 |
""" a basic tree node, caracterised by an id""" |
|
173 |
def setUp(self): |
|
174 |
""" called before each test from this class """ |
|
175 |
self.o = make_tree(tree) |
|
176 |
||
177 |
def test_generated_html(self): |
|
178 |
s = uilib.render_HTML_tree(self.o, selected_node="child_2_2") |
|
179 |
self.assertTextEqual(s, generated_html) |
|
180 |
||
181 |
||
182 |
if __name__ == '__main__': |
|
183 |
unittest_main() |
|
184 |