author | Laure Bourgois <Laure.Bourgois@logilab.fr> |
Fri, 23 Jan 2009 16:19:36 +0100 | |
changeset 460 | 5cd2b27d5435 |
parent 362 | a6a319f000c3 |
child 1646 | 88182252fd11 |
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 |
def test_remove_tags(self): |
|
14 |
"""make sure remove_tags remove all tags""" |
|
15 |
data = [ |
|
16 |
('<h1>Hello</h1>', 'Hello'), |
|
17 |
('<h1>Hello <a href="foo/bar"><b>s</b>pam</a></h1>', 'Hello spam'), |
|
18 |
('<br>Hello<img src="doh.png"/>', 'Hello'), |
|
19 |
('<p></p>', ''), |
|
20 |
] |
|
21 |
for text, expected in data: |
|
22 |
got = uilib.remove_html_tags(text) |
|
23 |
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
|
24 |
|
350
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
343
diff
changeset
|
25 |
def test_fallback_safe_cut(self): |
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
343
diff
changeset
|
26 |
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
|
27 |
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
|
28 |
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
|
29 |
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
|
30 |
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
|
31 |
self.assertEquals(uilib.fallback_safe_cut(u'& <a href="hello">&d</a> ef', 4), u'& &d...') |
160
cf9ab83b1cf3
adding tests for safe_cut (and text_cut_letters)
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents:
0
diff
changeset
|
32 |
|
350
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
343
diff
changeset
|
33 |
def test_lxml_safe_cut(self): |
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
343
diff
changeset
|
34 |
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
|
35 |
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
|
36 |
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
|
37 |
# 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
|
38 |
self.assertEquals(uilib.safe_cut(u'ab <a href="hello">&d</a>', 4), u'<p>ab <a href="hello">&...</a></p>') |
0 | 39 |
|
40 |
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
|
41 |
"""tests uilib.cut() behaviour""" |
0 | 42 |
data = [ |
43 |
('hello', 'hello'), |
|
350
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
343
diff
changeset
|
44 |
('hello world', 'hello wo...'), |
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
343
diff
changeset
|
45 |
("hell<b>O'</b> world", "hell<b>O..."), |
0 | 46 |
] |
47 |
for text, expected in data: |
|
48 |
got = uilib.cut(text, 8) |
|
49 |
self.assertEquals(got, expected) |
|
50 |
||
350
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
343
diff
changeset
|
51 |
def test_text_cut(self): |
0 | 52 |
"""tests uilib.text_cut() behaviour with no text""" |
350
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
343
diff
changeset
|
53 |
data = [('',''), |
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
343
diff
changeset
|
54 |
("""Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod |
0 | 55 |
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, |
56 |
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo |
|
57 |
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
|
58 |
cillum dolore eu fugiat nulla pariatur.""", |
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
343
diff
changeset
|
59 |
"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
|
60 |
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
|
61 |
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
|
62 |
consequat."), |
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
343
diff
changeset
|
63 |
("""Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod |
0 | 64 |
tempor incididunt ut labore et dolore magna aliqua Ut enim ad minim veniam, |
65 |
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo |
|
66 |
consequat Duis aute irure dolor in reprehenderit in voluptate velit esse |
|
67 |
cillum dolore eu fugiat nulla pariatur Excepteur sint occaecat cupidatat non |
|
68 |
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
|
69 |
""", |
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
343
diff
changeset
|
70 |
"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
|
71 |
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
|
72 |
quis nostrud exercitation ullamco laboris nisi"), |
f34ef2c64605
cleanup/fix cut variants
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
343
diff
changeset
|
73 |
] |
0 | 74 |
for text, expected in data: |
75 |
got = uilib.text_cut(text, 30) |
|
76 |
self.assertEquals(got, expected) |
|
77 |
||
78 |
def test_ajax_replace_url(self): |
|
79 |
# NOTE: for the simplest use cases, we could use doctest |
|
80 |
arurl = uilib.ajax_replace_url |
|
81 |
self.assertEquals(arurl('foo', 'Person P'), |
|
82 |
"javascript: replacePageChunk('foo', 'Person%20P');") |
|
83 |
self.assertEquals(arurl('foo', 'Person P', 'oneline'), |
|
84 |
"javascript: replacePageChunk('foo', 'Person%20P', 'oneline');") |
|
85 |
self.assertEquals(arurl('foo', 'Person P', 'oneline', name='bar', age=12), |
|
86 |
'javascript: replacePageChunk(\'foo\', \'Person%20P\', \'oneline\', {"age": 12, "name": "bar"});') |
|
87 |
self.assertEquals(arurl('foo', 'Person P', name='bar', age=12), |
|
88 |
'javascript: replacePageChunk(\'foo\', \'Person%20P\', \'null\', {"age": 12, "name": "bar"});') |
|
89 |
||
90 |
tree = ('root', ( |
|
91 |
('child_1_1', ( |
|
92 |
('child_2_1', ()), ('child_2_2', ( |
|
93 |
('child_3_1', ()), |
|
94 |
('child_3_2', ()), |
|
95 |
('child_3_3', ()), |
|
96 |
)))), |
|
97 |
('child_1_2', (('child_2_3', ()),)))) |
|
98 |
||
99 |
generated_html = """\ |
|
100 |
<table class="tree"> |
|
101 |
<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> |
|
102 |
<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> |
|
103 |
<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> |
|
104 |
<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> |
|
105 |
<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> |
|
106 |
<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> |
|
107 |
<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> |
|
108 |
<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> |
|
109 |
<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> |
|
110 |
<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> |
|
111 |
</table>\ |
|
112 |
""" |
|
113 |
||
114 |
def make_tree(tuple): |
|
115 |
n = Node(tuple[0]) |
|
116 |
for child in tuple[1]: |
|
117 |
n.append(make_tree(child)) |
|
118 |
return n |
|
119 |
||
120 |
class UIlibHTMLGenerationTC(TestCase): |
|
121 |
""" a basic tree node, caracterised by an id""" |
|
122 |
def setUp(self): |
|
123 |
""" called before each test from this class """ |
|
124 |
self.o = make_tree(tree) |
|
125 |
||
126 |
def test_generated_html(self): |
|
127 |
s = uilib.render_HTML_tree(self.o, selected_node="child_2_2") |
|
128 |
self.assertTextEqual(s, generated_html) |
|
129 |
||
130 |
||
131 |
if __name__ == '__main__': |
|
132 |
unittest_main() |
|
133 |