18 # with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
18 # with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
19 """unittests for cubicweb.uilib""" |
19 """unittests for cubicweb.uilib""" |
20 |
20 |
21 __docformat__ = "restructuredtext en" |
21 __docformat__ = "restructuredtext en" |
22 |
22 |
|
23 |
|
24 import pkg_resources |
23 from logilab.common.testlib import TestCase, unittest_main |
25 from logilab.common.testlib import TestCase, unittest_main |
|
26 from unittest2 import skipIf |
24 |
27 |
25 from cubicweb import uilib |
28 from cubicweb import uilib |
|
29 |
|
30 lxml_version = pkg_resources.get_distribution('lxml').version.split('.') |
|
31 |
26 |
32 |
27 class UILIBTC(TestCase): |
33 class UILIBTC(TestCase): |
28 |
34 |
29 def test_remove_tags(self): |
35 def test_remove_tags(self): |
30 """make sure remove_tags remove all tags""" |
36 """make sure remove_tags remove all tags""" |
89 ] |
95 ] |
90 for text, expected in data: |
96 for text, expected in data: |
91 got = uilib.text_cut(text, 30) |
97 got = uilib.text_cut(text, 30) |
92 self.assertEqual(got, expected) |
98 self.assertEqual(got, expected) |
93 |
99 |
|
100 def test_soup2xhtml_0(self): |
|
101 self.assertEqual(uilib.soup2xhtml('hop\r\nhop', 'ascii'), |
|
102 'hop\nhop') |
|
103 |
94 def test_soup2xhtml_1_1(self): |
104 def test_soup2xhtml_1_1(self): |
|
105 self.assertEqual(uilib.soup2xhtml('hop', 'ascii'), |
|
106 'hop') |
|
107 self.assertEqual(uilib.soup2xhtml('hop<div>', 'ascii'), |
|
108 'hop<div/>') |
95 self.assertEqual(uilib.soup2xhtml('hop <div>', 'ascii'), |
109 self.assertEqual(uilib.soup2xhtml('hop <div>', 'ascii'), |
96 'hop <div/>') |
110 'hop <div/>') |
97 self.assertEqual(uilib.soup2xhtml('<div> hop', 'ascii'), |
111 self.assertEqual(uilib.soup2xhtml('<div> hop', 'ascii'), |
98 '<div> hop</div>') |
112 '<div> hop</div>') |
99 self.assertEqual(uilib.soup2xhtml('hop <div> hop', 'ascii'), |
113 self.assertEqual(uilib.soup2xhtml('hop <div> hop', 'ascii'), |
113 self.assertEqual(uilib.soup2xhtml('<body> hop', 'ascii'), |
127 self.assertEqual(uilib.soup2xhtml('<body> hop', 'ascii'), |
114 ' hop') |
128 ' hop') |
115 self.assertEqual(uilib.soup2xhtml('hop <body> hop', 'ascii'), |
129 self.assertEqual(uilib.soup2xhtml('hop <body> hop', 'ascii'), |
116 'hop hop') |
130 'hop hop') |
117 |
131 |
118 def test_soup2xhtml_2_2(self): |
132 def test_soup2xhtml_2_2a(self): |
119 self.assertEqual(uilib.soup2xhtml('hop </body>', 'ascii'), |
133 self.assertEqual(uilib.soup2xhtml('hop </body>', 'ascii'), |
120 'hop ') |
134 'hop ') |
121 self.assertEqual(uilib.soup2xhtml('</body> hop', 'ascii'), |
135 self.assertEqual(uilib.soup2xhtml('</body> hop', 'ascii'), |
122 ' hop') |
136 ' hop') |
|
137 |
|
138 @skipIf(lxml_version < ['2', '2'], 'expected behaviour on recent version of lxml only') |
|
139 def test_soup2xhtml_2_2b(self): |
123 self.assertEqual(uilib.soup2xhtml('hop </body> hop', 'ascii'), |
140 self.assertEqual(uilib.soup2xhtml('hop </body> hop', 'ascii'), |
124 'hop hop') |
141 'hop hop') |
125 |
142 |
126 def test_soup2xhtml_3_1(self): |
143 def test_soup2xhtml_3_1(self): |
127 self.assertEqual(uilib.soup2xhtml('hop <html>', 'ascii'), |
144 self.assertEqual(uilib.soup2xhtml('hop <html>', 'ascii'), |
137 self.assertEqual(uilib.soup2xhtml('</html> hop', 'ascii'), |
154 self.assertEqual(uilib.soup2xhtml('</html> hop', 'ascii'), |
138 ' hop') |
155 ' hop') |
139 self.assertEqual(uilib.soup2xhtml('hop </html> hop', 'ascii'), |
156 self.assertEqual(uilib.soup2xhtml('hop </html> hop', 'ascii'), |
140 'hop hop') |
157 'hop hop') |
141 |
158 |
|
159 def test_soup2xhtml_3_3(self): |
|
160 self.assertEqual(uilib.soup2xhtml('<script>test</script> hop ', 'ascii'), |
|
161 ' hop ') |
|
162 |
142 def test_js(self): |
163 def test_js(self): |
143 self.assertEqual(str(uilib.js.pouet(1, "2")), |
164 self.assertEqual(str(uilib.js.pouet(1, "2")), |
144 'pouet(1,"2")') |
165 'pouet(1,"2")') |
145 self.assertEqual(str(uilib.js.cw.pouet(1, "2")), |
166 self.assertEqual(str(uilib.js.cw.pouet(1, "2")), |
146 'cw.pouet(1,"2")') |
167 'cw.pouet(1,"2")') |
147 self.assertEqual(str(uilib.js.cw.pouet(1, "2").pouet(None)), |
168 self.assertEqual(str(uilib.js.cw.pouet(1, "2").pouet(None)), |
148 'cw.pouet(1,"2").pouet(null)') |
169 'cw.pouet(1,"2").pouet(null)') |
149 |
170 |
|
171 def test_embedded_css(self): |
|
172 incoming = u"""voir le ticket <style type="text/css">@font-face { font-family: "Cambria"; }p.MsoNormal, li.MsoNormal, div.MsoNormal { margin: 0cm 0cm 10pt; font-size: 12pt; font-family: "Times New Roman"; }a:link, span.MsoHyperlink { color: blue; text-decoration: underline; }a:visited, span.MsoHyperlinkFollowed { color: purple; text-decoration: underline; }div.Section1 { page: Section1; }</style></p><p class="MsoNormal">text</p>""" |
|
173 expected = 'voir le ticket <p class="MsoNormal">text</p>' |
|
174 self.assertMultiLineEqual(uilib.soup2xhtml(incoming, 'ascii'), expected) |
|
175 |
|
176 def test_unknown_namespace(self): |
|
177 incoming = '''<table cellspacing="0" cellpadding="0" width="81" border="0" x:str="" style="width: 61pt; border-collapse: collapse"> |
|
178 <colgroup><col width="81" style="width: 61pt; mso-width-source: userset; mso-width-alt: 2962"/></colgroup> |
|
179 <tbody><tr height="17" style="height: 12.75pt"><td width="81" height="17" style="border-right: #e0dfe3; border-top: #e0dfe3; border-left: #e0dfe3; width: 61pt; border-bottom: #e0dfe3; height: 12.75pt; background-color: transparent"><font size="2">XXXXXXX</font></td></tr></tbody> |
|
180 </table>''' |
|
181 expected = '''<table cellspacing="0" cellpadding="0" width="81" border="0">\ |
|
182 <colgroup><col width="81"/></colgroup>\ |
|
183 <tbody><tr height="17"><td width="81" height="17">XXXXXXX</td></tr></tbody>\ |
|
184 </table>''' |
|
185 self.assertMultiLineEqual(uilib.soup2xhtml(incoming, 'ascii'), expected) |
|
186 |
|
187 |
150 if __name__ == '__main__': |
188 if __name__ == '__main__': |
151 unittest_main() |
189 unittest_main() |
152 |
190 |