|
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) |
|
25 |
|
26 def test_safe_cut(self): |
|
27 """tests uilib.safe_cut() behaviour""" |
|
28 data = [ |
|
29 ('hello', 'hello'), |
|
30 ('hello world', 'hello...'), |
|
31 ("hell<b>O'</b> world", "hellO..."), |
|
32 ('<h1>hello</h1>', '<h1>hello</h1>'), |
|
33 ] |
|
34 for text, expected in data: |
|
35 got = uilib.safe_cut(text, 8) |
|
36 self.assertEquals(got, expected) |
|
37 |
|
38 def test_cut(self): |
|
39 """tests uilib.safe_cut() behaviour""" |
|
40 data = [ |
|
41 ('hello', 'hello'), |
|
42 ('hello world', 'hello...'), |
|
43 ("hell<b>O'</b> world", "hell<..."), |
|
44 ] |
|
45 for text, expected in data: |
|
46 got = uilib.cut(text, 8) |
|
47 self.assertEquals(got, expected) |
|
48 |
|
49 def test_text_cut_no_text(self): |
|
50 """tests uilib.text_cut() behaviour with no text""" |
|
51 data = [('','')] |
|
52 for text, expected in data: |
|
53 got = uilib.text_cut(text, 8) |
|
54 self.assertEquals(got, expected) |
|
55 |
|
56 def test_text_cut_long_text(self): |
|
57 """tests uilib.text_cut() behaviour with long text""" |
|
58 data = [("""Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod |
|
59 tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, |
|
60 quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo |
|
61 consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse |
|
62 cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non |
|
63 proident, sunt in culpa qui officia deserunt mollit anim id est laborum. |
|
64 Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod |
|
65 tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, |
|
66 quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo |
|
67 consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse |
|
68 cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non |
|
69 proident, sunt in culpa qui officia deserunt mollit anim id est laborum. |
|
70 ""","""Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod |
|
71 tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, |
|
72 quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo |
|
73 consequat.""")] |
|
74 for text, expected in data: |
|
75 got = uilib.text_cut(text, 30) |
|
76 self.assertEquals(got, expected) |
|
77 |
|
78 def test_text_cut_no_point(self): |
|
79 """tests uilib.text_cut() behaviour with no point""" |
|
80 data = [("""Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod |
|
81 tempor incididunt ut labore et dolore magna aliqua Ut enim ad minim veniam, |
|
82 quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo |
|
83 consequat Duis aute irure dolor in reprehenderit in voluptate velit esse |
|
84 cillum dolore eu fugiat nulla pariatur Excepteur sint occaecat cupidatat non |
|
85 proident, sunt in culpa qui officia deserunt mollit anim id est laborum |
|
86 Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod |
|
87 tempor incididunt ut labore et dolore magna aliqua Ut enim ad minim veniam, |
|
88 quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo |
|
89 consequat Duis aute irure dolor in reprehenderit in voluptate velit esse |
|
90 cillum dolore eu fugiat nulla pariatur Excepteur sint occaecat cupidatat non |
|
91 proident, sunt in culpa qui officia deserunt mollit anim id est laborum |
|
92 ""","""Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod |
|
93 tempor incididunt ut labore et dolore magna aliqua Ut enim ad minim veniam, |
|
94 quis nostrud exercitation ullamco laboris nisi""")] |
|
95 for text, expected in data: |
|
96 got = uilib.text_cut(text, 30) |
|
97 self.assertEquals(got, expected) |
|
98 |
|
99 def test_ajax_replace_url(self): |
|
100 # NOTE: for the simplest use cases, we could use doctest |
|
101 arurl = uilib.ajax_replace_url |
|
102 self.assertEquals(arurl('foo', 'Person P'), |
|
103 "javascript: replacePageChunk('foo', 'Person%20P');") |
|
104 self.assertEquals(arurl('foo', 'Person P', 'oneline'), |
|
105 "javascript: replacePageChunk('foo', 'Person%20P', 'oneline');") |
|
106 self.assertEquals(arurl('foo', 'Person P', 'oneline', name='bar', age=12), |
|
107 'javascript: replacePageChunk(\'foo\', \'Person%20P\', \'oneline\', {"age": 12, "name": "bar"});') |
|
108 self.assertEquals(arurl('foo', 'Person P', name='bar', age=12), |
|
109 'javascript: replacePageChunk(\'foo\', \'Person%20P\', \'null\', {"age": 12, "name": "bar"});') |
|
110 |
|
111 tree = ('root', ( |
|
112 ('child_1_1', ( |
|
113 ('child_2_1', ()), ('child_2_2', ( |
|
114 ('child_3_1', ()), |
|
115 ('child_3_2', ()), |
|
116 ('child_3_3', ()), |
|
117 )))), |
|
118 ('child_1_2', (('child_2_3', ()),)))) |
|
119 |
|
120 generated_html = """\ |
|
121 <table class="tree"> |
|
122 <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> |
|
123 <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> |
|
124 <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> |
|
125 <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> |
|
126 <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> |
|
127 <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> |
|
128 <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> |
|
129 <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> |
|
130 <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> |
|
131 <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> |
|
132 </table>\ |
|
133 """ |
|
134 |
|
135 def make_tree(tuple): |
|
136 n = Node(tuple[0]) |
|
137 for child in tuple[1]: |
|
138 n.append(make_tree(child)) |
|
139 return n |
|
140 |
|
141 class UIlibHTMLGenerationTC(TestCase): |
|
142 """ a basic tree node, caracterised by an id""" |
|
143 def setUp(self): |
|
144 """ called before each test from this class """ |
|
145 self.o = make_tree(tree) |
|
146 |
|
147 def test_generated_html(self): |
|
148 s = uilib.render_HTML_tree(self.o, selected_node="child_2_2") |
|
149 self.assertTextEqual(s, generated_html) |
|
150 |
|
151 |
|
152 if __name__ == '__main__': |
|
153 unittest_main() |
|
154 |