130 | | print "Testing Java template:" |
131 | | java = javacodetemplate.format(variables=misc.javaexpr(variables), template=java) |
132 | | print java.encode("utf-8") |
133 | | with open(os.path.join(tempdir, "UL4Test.java"), "wb") as f: |
134 | | f.write(java.encode("utf-8")) |
135 | | os.system("cd {}; javac -encoding utf-8 UL4Test.java".format(tempdir)) |
136 | | result = os.popen("cd {}; java UL4Test 2>&1".format(tempdir), "rb").read() |
137 | | finally: |
138 | | shutil.rmtree(tempdir) |
139 | | result = result.decode("utf-8") |
140 | | # Check if we have an exception |
141 | | resultlines = result.splitlines() |
142 | | for line in resultlines: |
143 | | prefix = 'Exception in thread "main"' |
144 | | if line.startswith(prefix): |
145 | | raise RuntimeError(line[len(prefix):].strip()) |
146 | | return result |
147 | | |
148 | | |
149 | | def with_all_renderers(func): |
150 | | # Decorator that turns a test into a generative test testing the function ``func`` with all ``render*`` functions. |
151 | | def decorated(): |
152 | | yield func, render |
153 | | yield func, renderdumps |
154 | | yield func, renderdump |
155 | | yield func, renderjs |
156 | | yield func, renderjava |
157 | | return decorated |
158 | | |
159 | | |
160 | | @with_all_renderers |
161 | | def test_text(r): |
162 | | assert u'gurk' == r(u'gurk') |
163 | | assert u'g\xfcrk' == r(u'g\xfcrk') |
164 | | |
165 | | |
166 | | @with_all_renderers |
167 | | def test_none(r): |
168 | | assert '' == r(u'<?print None?>') |
169 | | assert 'no' == r(u'<?if None?>yes<?else?>no<?end if?>') |
170 | | |
171 | | |
172 | | @with_all_renderers |
173 | | def test_false(r): |
174 | | assert 'False' == r(u'<?print False?>') |
175 | | assert 'no' == r(u'<?if False?>yes<?else?>no<?end if?>') |
176 | | |
177 | | |
178 | | @with_all_renderers |
179 | | def test_true(r): |
180 | | assert 'True' == r(u'<?print True?>') |
181 | | assert 'yes' == r(u'<?if True?>yes<?else?>no<?end if?>') |
182 | | |
183 | | |
184 | | @with_all_renderers |
185 | | def test_int(r): |
186 | | assert '0' == r(u'<?print 0?>') |
187 | | assert '42' == r(u'<?print 42?>') |
188 | | assert '-42' == r(u'<?print -42?>') |
189 | | assert '255' == r(u'<?print 0xff?>') |
190 | | assert '255' == r(u'<?print 0Xff?>') |
191 | | assert '-255' == r(u'<?print -0xff?>') |
192 | | assert '-255' == r(u'<?print -0Xff?>') |
193 | | assert '63' == r(u'<?print 0o77?>') |
194 | | assert '63' == r(u'<?print 0O77?>') |
195 | | assert '-63' == r(u'<?print -0o77?>') |
196 | | assert '-63' == r(u'<?print -0O77?>') |
197 | | assert '7' == r(u'<?print 0b111?>') |
198 | | assert '7' == r(u'<?print 0B111?>') |
199 | | assert '-7' == r(u'<?print -0b111?>') |
200 | | assert '-7' == r(u'<?print -0B111?>') |
201 | | |
202 | | assert 'no' == r(u'<?if 0?>yes<?else?>no<?end if?>') |
203 | | assert 'yes' == r(u'<?if 1?>yes<?else?>no<?end if?>') |
204 | | assert 'yes' == r(u'<?if -1?>yes<?else?>no<?end if?>') |
205 | | |
206 | | |
207 | | @with_all_renderers |
208 | | def test_float(r): |
209 | | # str() output might differ slightly between Python and JS, so eval the output again for tests |
210 | | assert 0.0 == eval(r(u'<?print 0.?>')) |
211 | | assert 42.0 == eval(r(u'<?print 42.?>')) |
212 | | assert -42.0 == eval(r(u'<?print -42.?>')) |
213 | | assert -42.5 == eval(r(u'<?print -42.5?>')) |
214 | | assert 1e42 == eval(r(u'<?print 1E42?>')) |
215 | | assert 1e42 == eval(r(u'<?print 1e42?>')) |
216 | | assert -1e42 == eval(r(u'<?print -1E42?>')) |
217 | | assert -1e42 == eval(r(u'<?print -1e42?>')) |
218 | | |
219 | | assert 'no' == r(u'<?if 0.?>yes<?else?>no<?end if?>') |
220 | | assert 'yes' == r(u'<?if 1.?>yes<?else?>no<?end if?>') |
221 | | assert 'yes' == r(u'<?if -1.?>yes<?else?>no<?end if?>') |
222 | | |
223 | | |
224 | | @with_all_renderers |
225 | | def test_string(r): |
226 | | with raises("Unterminated string"): |
227 | | r(u'<?print "?>') |
228 | | assert 'foo' == r(u'<?print "foo"?>') |
229 | | assert '\n' == r(u'<?print "\\n"?>') |
230 | | assert '\r' == r(u'<?print "\\r"?>') |
231 | | assert '\t' == r(u'<?print "\\t"?>') |
232 | | assert '\f' == r(u'<?print "\\f"?>') |
233 | | assert '\b' == r(u'<?print "\\b"?>') |
234 | | assert '\a' == r(u'<?print "\\a"?>') |
235 | | assert '\x1b' == r(u'<?print "\\e"?>') |
236 | | assert '"' == r(u'<?print "\\""?>') |
237 | | assert "'" == r(u'<?print "\\\'"?>') |
238 | | assert u'\u20ac' == r(u'<?print "\u20ac"?>') |
239 | | assert u'\xff' == r(u'<?print "\\xff"?>') |
240 | | assert u'\u20ac' == r(u'''<?print "\\u20ac"?>''') |
241 | | assert "a\nb" == r(u'<?print "a\nb"?>') |
242 | | |
243 | | assert 'no' == r(u'<?if ""?>yes<?else?>no<?end if?>') |
244 | | assert 'yes' == r(u'<?if "foo"?>yes<?else?>no<?end if?>') |
245 | | |
246 | | |
247 | | @with_all_renderers |
248 | | def test_date(r): |
249 | | assert '2000-02-29' == r(u'<?print @2000-02-29T.isoformat()?>') |
250 | | assert '2000-02-29T12:34:00' == r(u'<?print @2000-02-29T12:34.isoformat()?>') |
251 | | assert '2000-02-29T12:34:56' == r(u'<?print @2000-02-29T12:34:56.isoformat()?>') |
252 | | assert '2000-02-29T12:34:56.987000' == r(u'<?print @2000-02-29T12:34:56.987000.isoformat()?>') # JS and Java only supports milliseconds |
253 | | assert 'yes' == r(u'<?if @2000-02-29T12:34:56.987654?>yes<?else?>no<?end if?>') |
254 | | |
255 | | |
256 | | @with_all_renderers |
257 | | def test_color(r): |
258 | | assert '255,255,255,255' == r(u'<?code c = #fff?><?print c[0]?>,<?print c[1]?>,<?print c[2]?>,<?print c[3]?>') |
259 | | assert '255,255,255,255' == r(u'<?code c = #ffffff?><?print c[0]?>,<?print c[1]?>,<?print c[2]?>,<?print c[3]?>') |
260 | | assert '18,52,86,255' == r(u'<?code c = #123456?><?print c[0]?>,<?print c[1]?>,<?print c[2]?>,<?print c[3]?>') |
261 | | assert '17,34,51,68' == r(u'<?code c = #1234?><?print c[0]?>,<?print c[1]?>,<?print c[2]?>,<?print c[3]?>') |
262 | | assert '18,52,86,120' == r(u'<?code c = #12345678?><?print c[0]?>,<?print c[1]?>,<?print c[2]?>,<?print c[3]?>') |
263 | | assert 'yes' == r(u'<?if #fff?>yes<?else?>no<?end if?>') |
264 | | |
265 | | |
266 | | @with_all_renderers |
267 | | def test_list(r): |
268 | | assert '' == r(u'<?for item in []?><?print item?>;<?end for?>') |
269 | | assert '1;' == r(u'<?for item in [1]?><?print item?>;<?end for?>') |
270 | | assert '1;' == r(u'<?for item in [1,]?><?print item?>;<?end for?>') |
271 | | assert '1;2;' == r(u'<?for item in [1, 2]?><?print item?>;<?end for?>') |
272 | | assert '1;2;' == r(u'<?for item in [1, 2,]?><?print item?>;<?end for?>') |
273 | | assert 'no' == r(u'<?if []?>yes<?else?>no<?end if?>') |
274 | | assert 'yes' == r(u'<?if [1]?>yes<?else?>no<?end if?>') |
275 | | |
276 | | |
277 | | @with_all_renderers |
278 | | def test_dict(r): |
279 | | assert '' == r(u'<?for (key, value) in {}.items()?><?print key?>:<?print value?>\n<?end for?>') |
280 | | assert '1:2\n' == r(u'<?for (key, value) in {1:2}.items()?><?print key?>:<?print value?>\n<?end for?>') |
281 | | assert '1:2\n' == r(u'<?for (key, value) in {1:2,}.items()?><?print key?>:<?print value?>\n<?end for?>') |
282 | | # With duplicate keys, later ones simply overwrite earlier ones |
283 | | assert '1:3\n' == r(u'<?for (key, value) in {1:2, 1: 3}.items()?><?print key?>:<?print value?>\n<?end for?>') |
284 | | # Test ** |
285 | | assert '1:2\n' == r(u'<?for (key, value) in {**{1:2}}.items()?><?print key?>:<?print value?>\n<?end for?>') |
286 | | assert '1:4\n' == r(u'<?for (key, value) in {1:1, **{1:2}, 1:3, **{1:4}}.items()?><?print key?>:<?print value?>\n<?end for?>') |
287 | | assert 'no' == r(u'<?if {}?>yes<?else?>no<?end if?>') |
288 | | assert 'yes' == r(u'<?if {1:2}?>yes<?else?>no<?end if?>') |
289 | | |
290 | | |
291 | | @with_all_renderers |
292 | | def test_code_storevar(r): |
293 | | assert '42' == r(u'<?code x = 42?><?print x?>') |
294 | | assert 'xyzzy' == r(u'<?code x = "xyzzy"?><?print x?>') |
295 | | |
296 | | |
297 | | @with_all_renderers |
298 | | def test_code_addvar(r): |
299 | | assert '40' == r(u'<?code x = 17?><?code x += 23?><?print x?>') |
300 | | assert 'xyzzy' == r(u'<?code x = "xyz"?><?code x += "zy"?><?print x?>') |
301 | | |
302 | | |
303 | | @with_all_renderers |
304 | | def test_code_subvar(r): |
305 | | assert '-6' == r(u'<?code x = 17?><?code x -= 23?><?print x?>') |
306 | | |
307 | | |
308 | | @with_all_renderers |
309 | | def test_code_mulvar(r): |
310 | | assert '391' == r(u'<?code x = 17?><?code x *= 23?><?print x?>') |
311 | | assert 17*'xyzzy' == r(u'<?code x = 17?><?code x *= "xyzzy"?><?print x?>') |
312 | | assert 17*'xyzzy' == r(u'<?code x = "xyzzy"?><?code x *= 17?><?print x?>') |
313 | | |
314 | | |
315 | | @with_all_renderers |
316 | | def test_code_floordivvar(r): |
317 | | assert '2' == r(u'<?code x = 5?><?code x //= 2?><?print x?>') |
318 | | assert '-3' == r(u'<?code x = -5?><?code x //= 2?><?print x?>') |
319 | | |
320 | | |
321 | | @with_all_renderers |
322 | | def test_code_truedivvar(r): |
323 | | assert '2.5' == r(u'<?code x = 5?><?code x /= 2?><?print x?>') |
324 | | assert '-2.5' == r(u'<?code x = -5?><?code x /= 2?><?print x?>') |
325 | | |
326 | | |
327 | | @with_all_renderers |
328 | | def test_code_modvar(r): |
329 | | assert '4' == r(u'<?code x = 1729?><?code x %= 23?><?print x?>') |
330 | | |
331 | | |
332 | | @with_all_renderers |
333 | | def test_code_delvar(r): |
334 | | with raises("(KeyError|not found)"): |
335 | | r(u'<?code x = 1729?><?code del x?><?print x?>') |
336 | | |
337 | | |
338 | | @with_all_renderers |
339 | | def test_for_string(r): |
340 | | assert '' == r(u'<?for c in data?>(<?print c?>)<?end for?>', data="") |
341 | | assert '(g)(u)(r)(k)' == r(u'<?for c in data?>(<?print c?>)<?end for?>', data="gurk") |
342 | | |
343 | | |
344 | | @with_all_renderers |
345 | | def test_for_list(r): |
346 | | assert '' == r(u'<?for c in data?>(<?print c?>)<?end for?>', data="") |
347 | | assert '(g)(u)(r)(k)' == r(u'<?for c in data?>(<?print c?>)<?end for?>', data=["g", "u", "r", "k"]) |
348 | | |
349 | | |
350 | | @with_all_renderers |
351 | | def test_for_dict(r): |
352 | | assert '' == r(u'<?for c in data?>(<?print c?>)<?end for?>', data={}) |
353 | | assert '(a)(b)(c)' == r(u'<?for c in sorted(data)?>(<?print c?>)<?end for?>', data=dict(a=1, b=2, c=3)) |
354 | | |
355 | | |
356 | | @with_all_renderers |
357 | | def test_for_nested(r): |
358 | | assert '[(1)(2)][(3)(4)]' == r(u'<?for list in data?>[<?for n in list?>(<?print n?>)<?end for?>]<?end for?>', data=[[1, 2], [3, 4]]) |
359 | | |
360 | | |
361 | | @with_all_renderers |
362 | | def test_for_unpacking(r): |
| 180 | render.renders() |
| 181 | except Exception, exc: |
| 182 | assert re.search(msg, "{0.__class__.__module__}.{0.__class__.__name__}: {0}".format(exc)) is not None |
| 183 | else: |
| 184 | py.test.fail("failed to raise exception") |
| 185 | |
| 186 | |
| 187 | def test_text(): |
| 188 | for r in all_renderers: |
| 189 | yield eq, u'gurk', r(u'gurk') |
| 190 | yield eq, u'g\xfcrk', r(u'g\xfcrk') |
| 191 | |
| 192 | |
| 193 | def test_none(): |
| 194 | for r in all_renderers: |
| 195 | yield eq, '', r(u'<?print None?>') |
| 196 | yield eq, 'no', r(u'<?if None?>yes<?else?>no<?end if?>') |
| 197 | |
| 198 | |
| 199 | def test_false(): |
| 200 | for r in all_renderers: |
| 201 | yield eq, 'False', r(u'<?print False?>') |
| 202 | yield eq, 'no', r(u'<?if False?>yes<?else?>no<?end if?>') |
| 203 | |
| 204 | |
| 205 | def test_true(): |
| 206 | for r in all_renderers: |
| 207 | yield eq, 'True', r(u'<?print True?>') |
| 208 | yield eq, 'yes', r(u'<?if True?>yes<?else?>no<?end if?>') |
| 209 | |
| 210 | |
| 211 | def test_int(): |
| 212 | for r in all_renderers: |
| 213 | yield eq, '0', r(u'<?print 0?>') |
| 214 | yield eq, '42', r(u'<?print 42?>') |
| 215 | yield eq, '-42', r(u'<?print -42?>') |
| 216 | yield eq, '255', r(u'<?print 0xff?>') |
| 217 | yield eq, '255', r(u'<?print 0Xff?>') |
| 218 | yield eq, '-255', r(u'<?print -0xff?>') |
| 219 | yield eq, '-255', r(u'<?print -0Xff?>') |
| 220 | yield eq, '63', r(u'<?print 0o77?>') |
| 221 | yield eq, '63', r(u'<?print 0O77?>') |
| 222 | yield eq, '-63', r(u'<?print -0o77?>') |
| 223 | yield eq, '-63', r(u'<?print -0O77?>') |
| 224 | yield eq, '7', r(u'<?print 0b111?>') |
| 225 | yield eq, '7', r(u'<?print 0B111?>') |
| 226 | yield eq, '-7', r(u'<?print -0b111?>') |
| 227 | yield eq, '-7', r(u'<?print -0B111?>') |
| 228 | |
| 229 | yield eq, 'no', r(u'<?if 0?>yes<?else?>no<?end if?>') |
| 230 | yield eq, 'yes', r(u'<?if 1?>yes<?else?>no<?end if?>') |
| 231 | yield eq, 'yes', r(u'<?if -1?>yes<?else?>no<?end if?>') |
| 232 | |
| 233 | |
| 234 | def test_float(): |
| 235 | for r in all_renderers: |
| 236 | # str() output might differ slightly between Python and JS, so eval the output again for tests |
| 237 | yield evaleq, 0.0, r(u'<?print 0.?>') |
| 238 | yield evaleq, 42.0, r(u'<?print 42.?>') |
| 239 | yield evaleq, -42.0, r(u'<?print -42.?>') |
| 240 | yield evaleq, -42.5, r(u'<?print -42.5?>') |
| 241 | yield evaleq, 1e42, r(u'<?print 1E42?>') |
| 242 | yield evaleq, 1e42, r(u'<?print 1e42?>') |
| 243 | yield evaleq, -1e42, r(u'<?print -1E42?>') |
| 244 | yield evaleq, -1e42, r(u'<?print -1e42?>') |
| 245 | |
| 246 | yield eq, 'no', r(u'<?if 0.?>yes<?else?>no<?end if?>') |
| 247 | yield eq, 'yes', r(u'<?if 1.?>yes<?else?>no<?end if?>') |
| 248 | yield eq, 'yes', r(u'<?if -1.?>yes<?else?>no<?end if?>') |
| 249 | |
| 250 | |
| 251 | def test_string(): |
| 252 | for r in all_renderers: |
| 253 | with raises("Unterminated string"): |
| 254 | r(u'<?print "?>').renders() |
| 255 | yield eq, 'foo', r(u'<?print "foo"?>') |
| 256 | yield eq, '\n', r(u'<?print "\\n"?>') |
| 257 | yield eq, '\r', r(u'<?print "\\r"?>') |
| 258 | yield eq, '\t', r(u'<?print "\\t"?>') |
| 259 | yield eq, '\f', r(u'<?print "\\f"?>') |
| 260 | yield eq, '\b', r(u'<?print "\\b"?>') |
| 261 | yield eq, '\a', r(u'<?print "\\a"?>') |
| 262 | yield eq, '\x1b', r(u'<?print "\\e"?>') |
| 263 | yield eq, '"', r(u'<?print "\\""?>') |
| 264 | yield eq, "'", r(u'<?print "\\\'"?>') |
| 265 | yield eq, u'\u20ac', r(u'<?print "\u20ac"?>') |
| 266 | yield eq, u'\xff', r(u'<?print "\\xff"?>') |
| 267 | yield eq, u'\u20ac', r(u'''<?print "\\u20ac"?>''') |
| 268 | yield eq, "a\nb", r(u'<?print "a\nb"?>') |
| 269 | |
| 270 | yield eq, 'no', r(u'<?if ""?>yes<?else?>no<?end if?>') |
| 271 | yield eq, 'yes', r(u'<?if "foo"?>yes<?else?>no<?end if?>') |
| 272 | |
| 273 | |
| 274 | def test_date(): |
| 275 | for r in all_renderers: |
| 276 | yield eq, '2000-02-29', r(u'<?print @2000-02-29T.isoformat()?>') |
| 277 | yield eq, '2000-02-29T12:34:00', r(u'<?print @2000-02-29T12:34.isoformat()?>') |
| 278 | yield eq, '2000-02-29T12:34:56', r(u'<?print @2000-02-29T12:34:56.isoformat()?>') |
| 279 | yield eq, '2000-02-29T12:34:56.987000', r(u'<?print @2000-02-29T12:34:56.987000.isoformat()?>') # JS and Java only supports milliseconds |
| 280 | yield eq, 'yes', r(u'<?if @2000-02-29T12:34:56.987654?>yes<?else?>no<?end if?>') |
| 281 | |
| 282 | |
| 283 | def test_color(): |
| 284 | for r in all_renderers: |
| 285 | yield eq, '255,255,255,255', r(u'<?code c = #fff?><?print c[0]?>,<?print c[1]?>,<?print c[2]?>,<?print c[3]?>') |
| 286 | yield eq, '255,255,255,255', r(u'<?code c = #ffffff?><?print c[0]?>,<?print c[1]?>,<?print c[2]?>,<?print c[3]?>') |
| 287 | yield eq, '18,52,86,255', r(u'<?code c = #123456?><?print c[0]?>,<?print c[1]?>,<?print c[2]?>,<?print c[3]?>') |
| 288 | yield eq, '17,34,51,68', r(u'<?code c = #1234?><?print c[0]?>,<?print c[1]?>,<?print c[2]?>,<?print c[3]?>') |
| 289 | yield eq, '18,52,86,120', r(u'<?code c = #12345678?><?print c[0]?>,<?print c[1]?>,<?print c[2]?>,<?print c[3]?>') |
| 290 | yield eq, 'yes', r(u'<?if #fff?>yes<?else?>no<?end if?>') |
| 291 | |
| 292 | |
| 293 | def test_list(): |
| 294 | for r in all_renderers: |
| 295 | yield eq, '', r(u'<?for item in []?><?print item?>;<?end for?>') |
| 296 | yield eq, '1;', r(u'<?for item in [1]?><?print item?>;<?end for?>') |
| 297 | yield eq, '1;', r(u'<?for item in [1,]?><?print item?>;<?end for?>') |
| 298 | yield eq, '1;2;', r(u'<?for item in [1, 2]?><?print item?>;<?end for?>') |
| 299 | yield eq, '1;2;', r(u'<?for item in [1, 2,]?><?print item?>;<?end for?>') |
| 300 | yield eq, 'no', r(u'<?if []?>yes<?else?>no<?end if?>') |
| 301 | yield eq, 'yes', r(u'<?if [1]?>yes<?else?>no<?end if?>') |
| 302 | |
| 303 | |
| 304 | def test_dict(): |
| 305 | for r in all_renderers: |
| 306 | yield eq, '', r(u'<?for (key, value) in {}.items()?><?print key?>:<?print value?>\n<?end for?>') |
| 307 | yield eq, '1:2\n', r(u'<?for (key, value) in {1:2}.items()?><?print key?>:<?print value?>\n<?end for?>') |
| 308 | yield eq, '1:2\n', r(u'<?for (key, value) in {1:2,}.items()?><?print key?>:<?print value?>\n<?end for?>') |
| 309 | # With duplicate keys, later ones simply overwrite earlier ones |
| 310 | yield eq, '1:3\n', r(u'<?for (key, value) in {1:2, 1: 3}.items()?><?print key?>:<?print value?>\n<?end for?>') |
| 311 | # Test ** |
| 312 | yield eq, '1:2\n', r(u'<?for (key, value) in {**{1:2}}.items()?><?print key?>:<?print value?>\n<?end for?>') |
| 313 | yield eq, '1:4\n', r(u'<?for (key, value) in {1:1, **{1:2}, 1:3, **{1:4}}.items()?><?print key?>:<?print value?>\n<?end for?>') |
| 314 | yield eq, 'no', r(u'<?if {}?>yes<?else?>no<?end if?>') |
| 315 | yield eq, 'yes', r(u'<?if {1:2}?>yes<?else?>no<?end if?>') |
| 316 | |
| 317 | |
| 318 | def test_code_storevar(): |
| 319 | for r in all_renderers: |
| 320 | yield eq, '42', r(u'<?code x = 42?><?print x?>') |
| 321 | yield eq, 'xyzzy', r(u'<?code x = "xyzzy"?><?print x?>') |
| 322 | |
| 323 | |
| 324 | def test_code_addvar(): |
| 325 | for r in all_renderers: |
| 326 | yield eq, '40', r(u'<?code x = 17?><?code x += 23?><?print x?>') |
| 327 | yield eq, 'xyzzy', r(u'<?code x = "xyz"?><?code x += "zy"?><?print x?>') |
| 328 | |
| 329 | |
| 330 | def test_code_subvar(): |
| 331 | for r in all_renderers: |
| 332 | yield eq, '-6', r(u'<?code x = 17?><?code x -= 23?><?print x?>') |
| 333 | |
| 334 | |
| 335 | def test_code_mulvar(): |
| 336 | for r in all_renderers: |
| 337 | yield eq, '391', r(u'<?code x = 17?><?code x *= 23?><?print x?>') |
| 338 | yield eq, 17*'xyzzy', r(u'<?code x = 17?><?code x *= "xyzzy"?><?print x?>') |
| 339 | yield eq, 17*'xyzzy', r(u'<?code x = "xyzzy"?><?code x *= 17?><?print x?>') |
| 340 | |
| 341 | |
| 342 | def test_code_floordivvar(): |
| 343 | for r in all_renderers: |
| 344 | yield eq, '2', r(u'<?code x = 5?><?code x //= 2?><?print x?>') |
| 345 | yield eq, '-3', r(u'<?code x = -5?><?code x //= 2?><?print x?>') |
| 346 | |
| 347 | |
| 348 | def test_code_truedivvar(): |
| 349 | for r in all_renderers: |
| 350 | yield eq, '2.5', r(u'<?code x = 5?><?code x /= 2?><?print x?>') |
| 351 | yield eq, '-2.5', r(u'<?code x = -5?><?code x /= 2?><?print x?>') |
| 352 | |
| 353 | |
| 354 | def test_code_modvar(): |
| 355 | for r in all_renderers: |
| 356 | yield eq, '4', r(u'<?code x = 1729?><?code x %= 23?><?print x?>') |
| 357 | |
| 358 | |
| 359 | def test_code_delvar(): |
| 360 | for r in all_renderers: |
| 361 | yield raises, "(KeyError|not found)", r(u'<?code x = 1729?><?code del x?><?print x?>') |
| 362 | |
| 363 | |
| 364 | def test_for_string(): |
| 365 | for r in all_renderers: |
| 366 | yield eq, '', r(u'<?for c in data?>(<?print c?>)<?end for?>', data="") |
| 367 | yield eq, '(g)(u)(r)(k)', r(u'<?for c in data?>(<?print c?>)<?end for?>', data="gurk") |
| 368 | |
| 369 | |
| 370 | def test_for_list(): |
| 371 | for r in all_renderers: |
| 372 | yield eq, '', r(u'<?for c in data?>(<?print c?>)<?end for?>', data="") |
| 373 | yield eq, '(g)(u)(r)(k)', r(u'<?for c in data?>(<?print c?>)<?end for?>', data=["g", "u", "r", "k"]) |
| 374 | |
| 375 | |
| 376 | def test_for_dict(): |
| 377 | for r in all_renderers: |
| 378 | yield eq, '', r(u'<?for c in data?>(<?print c?>)<?end for?>', data={}) |
| 379 | yield eq, '(a)(b)(c)', r(u'<?for c in sorted(data)?>(<?print c?>)<?end for?>', data=dict(a=1, b=2, c=3)) |
| 380 | |
| 381 | |
| 382 | def test_for_nested(): |
| 383 | for r in all_renderers: |
| 384 | yield eq, '[(1)(2)][(3)(4)]', r(u'<?for list in data?>[<?for n in list?>(<?print n?>)<?end for?>]<?end for?>', data=[[1, 2], [3, 4]]) |
| 385 | |
| 386 | |
| 387 | def test_for_unpacking(): |
369 | | assert '(spam)(gurk)(hinz)' == r(u'<?for (a,) in data?>(<?print a?>)<?end for?>', data=data) |
370 | | assert '(spam,eggs)(gurk,hurz)(hinz,kunz)' == r(u'<?for (a, b) in data?>(<?print a?>,<?print b?>)<?end for?>', data=data) |
371 | | assert '(spam,eggs,17)(gurk,hurz,23)(hinz,kunz,42)' == r(u'<?for (a, b, c) in data?>(<?print a?>,<?print b?>,<?print c?>)<?end for?>', data=data) |
372 | | |
373 | | |
374 | | @with_all_renderers |
375 | | def test_break(r): |
376 | | assert '1, 2, ' == r(u'<?for i in [1,2,3]?><?print i?>, <?if i==2?><?break?><?end if?><?end for?>') |
377 | | |
378 | | |
379 | | @with_all_renderers |
380 | | def test_break_nested(r): |
381 | | assert '1, 1, 2, 1, 2, 3, ' == r(u'<?for i in [1,2,3,4]?><?for j in [1,2,3,4]?><?print j?>, <?if j>=i?><?break?><?end if?><?end for?><?if i>=3?><?break?><?end if?><?end for?>') |
382 | | |
383 | | |
384 | | @with_all_renderers |
385 | | def test_continue(r): |
386 | | assert '1, 3, ' == r(u'<?for i in [1,2,3]?><?if i==2?><?continue?><?end if?><?print i?>, <?end for?>') |
387 | | |
388 | | |
389 | | @with_all_renderers |
390 | | def test_continue_nested(r): |
391 | | assert '1, 3, \n1, 3, \n' == r(u'<?for i in [1,2,3]?><?if i==2?><?continue?><?end if?><?for j in [1,2,3]?><?if j==2?><?continue?><?end if?><?print j?>, <?end for?>\n<?end for?>') |
392 | | |
393 | | |
394 | | @with_all_renderers |
395 | | def test_if(r): |
396 | | assert '42' == r(u'<?if data?><?print data?><?end if?>', data=42) |
397 | | |
398 | | |
399 | | @with_all_renderers |
400 | | def test_else(r): |
401 | | assert '42' == r(u'<?if data?><?print data?><?else?>no<?end if?>', data=42) |
402 | | assert 'no' == r(u'<?if data?><?print data?><?else?>no<?end if?>', data=0) |
| 394 | for r in all_renderers: |
| 395 | yield eq, '(spam)(gurk)(hinz)', r(u'<?for (a,) in data?>(<?print a?>)<?end for?>', data=data) |
| 396 | yield eq, '(spam,eggs)(gurk,hurz)(hinz,kunz)', r(u'<?for (a, b) in data?>(<?print a?>,<?print b?>)<?end for?>', data=data) |
| 397 | yield eq, '(spam,eggs,17)(gurk,hurz,23)(hinz,kunz,42)', r(u'<?for (a, b, c) in data?>(<?print a?>,<?print b?>,<?print c?>)<?end for?>', data=data) |
| 398 | |
| 399 | |
| 400 | def test_break(): |
| 401 | for r in all_renderers: |
| 402 | yield eq, '1, 2, ', r(u'<?for i in [1,2,3]?><?print i?>, <?if i==2?><?break?><?end if?><?end for?>') |
| 403 | |
| 404 | |
| 405 | def test_break_nested(): |
| 406 | for r in all_renderers: |
| 407 | yield eq, '1, 1, 2, 1, 2, 3, ', r(u'<?for i in [1,2,3,4]?><?for j in [1,2,3,4]?><?print j?>, <?if j>=i?><?break?><?end if?><?end for?><?if i>=3?><?break?><?end if?><?end for?>') |
| 408 | |
| 409 | |
| 410 | def test_continue(): |
| 411 | for r in all_renderers: |
| 412 | yield eq, '1, 3, ', r(u'<?for i in [1,2,3]?><?if i==2?><?continue?><?end if?><?print i?>, <?end for?>') |
| 413 | |
| 414 | |
| 415 | def test_continue_nested(): |
| 416 | for r in all_renderers: |
| 417 | yield eq, '1, 3, \n1, 3, \n', r(u'<?for i in [1,2,3]?><?if i==2?><?continue?><?end if?><?for j in [1,2,3]?><?if j==2?><?continue?><?end if?><?print j?>, <?end for?>\n<?end for?>') |
| 418 | |
| 419 | |
| 420 | def test_if(): |
| 421 | for r in all_renderers: |
| 422 | yield eq, '42', r(u'<?if data?><?print data?><?end if?>', data=42) |
| 423 | |
| 424 | |
| 425 | def test_else(): |
| 426 | for r in all_renderers: |
| 427 | yield eq, '42', r(u'<?if data?><?print data?><?else?>no<?end if?>', data=42) |
| 428 | yield eq, 'no', r(u'<?if data?><?print data?><?else?>no<?end if?>', data=0) |
569 | | assert "False" == r(code, x=2, y=[1, 2, 3]) |
570 | | assert "True" == r(code, x=4, y=[1, 2, 3]) |
571 | | assert "False" == r(code, x="ur", y="gurk") |
572 | | assert "True" == r(code, x="un", y="gurk") |
573 | | assert "False" == r(code, x="a", y={"a": 1, "b": 2}) |
574 | | assert "True" == r(code, x="c", y={"a": 1, "b": 2}) |
575 | | assert "False" == r(code, x=0xff, y=color.Color(0x00, 0x80, 0xff, 0x42)) |
576 | | assert "True" == r(code, x=0x23, y=color.Color(0x00, 0x80, 0xff, 0x42)) |
577 | | |
578 | | |
579 | | @with_all_renderers |
580 | | def test_and(r): |
581 | | assert "False" == r(u'<?print x and y?>', x=False, y=False) |
582 | | assert "False" == r(u'<?print x and y?>', x=False, y=True) |
583 | | assert "0" == r(u'<?print x and y?>', x=0, y=True) |
584 | | |
585 | | |
586 | | @with_all_renderers |
587 | | def test_or(r): |
588 | | assert "False" == r(u'<?print x or y?>', x=False, y=False) |
589 | | assert "True" == r(u'<?print x or y?>', x=False, y=True) |
590 | | assert "42" == r(u'<?print x or y?>', x=42, y=True) |
591 | | |
592 | | |
593 | | @with_all_renderers |
594 | | def test_not(r): |
595 | | assert "True" == r(u'<?print not x?>', x=False) |
596 | | assert "False" == r(u'<?print not x?>', x=42) |
597 | | |
598 | | |
599 | | @with_all_renderers |
600 | | def test_getitem(r): |
601 | | assert "u" == r(u"<?print 'gurk'[1]?>") |
602 | | assert "u" == r(u"<?print x[1]?>", x="gurk") |
603 | | assert "u" == r(u"<?print 'gurk'[-3]?>") |
604 | | assert "u" == r(u"<?print x[-3]?>", x="gurk") |
605 | | with raises("IndexError"): |
606 | | r(u"<?print 'gurk'[4]?>") |
607 | | with raises("index (4 )?out of range"): |
608 | | r(u"<?print x[4]?>", x="gurk") |
609 | | with raises("IndexError"): |
610 | | r(u"<?print 'gurk'[-5]?>") |
611 | | with raises("index (-5 )?out of range"): |
612 | | r(u"<?print x[-5]?>", x="gurk") |
613 | | |
614 | | |
615 | | @with_all_renderers |
616 | | def test_getslice12(r): |
617 | | assert "ur" == r(u"<?print 'gurk'[1:3]?>") |
618 | | assert "ur" == r(u"<?print x[1:3]?>", x="gurk") |
619 | | assert "ur" == r(u"<?print 'gurk'[-3:-1]?>") |
620 | | assert "ur" == r(u"<?print x[-3:-1]?>", x="gurk") |
621 | | assert "" == r(u"<?print 'gurk'[4:10]?>") |
622 | | assert "" == r(u"<?print x[4:10]?>", x="gurk") |
623 | | assert "" == r(u"<?print 'gurk'[-10:-5]?>") |
624 | | assert "" == r(u"<?print x[-10:-5]?>", x="gurk") |
625 | | |
626 | | |
627 | | @with_all_renderers |
628 | | def test_getslice1(r): |
629 | | assert "urk" == r(u"<?print 'gurk'[1:]?>") |
630 | | assert "urk" == r(u"<?print x[1:]?>", x="gurk") |
631 | | assert "urk" == r(u"<?print 'gurk'[-3:]?>") |
632 | | assert "urk" == r(u"<?print x[-3:]?>", x="gurk") |
633 | | assert "" == r(u"<?print 'gurk'[4:]?>") |
634 | | assert "" == r(u"<?print x[4:]?>", x="gurk") |
635 | | assert "gurk" == r(u"<?print 'gurk'[-10:]?>") |
636 | | assert "gurk" == r(u"<?print x[-10:]?>", x="gurk") |
637 | | |
638 | | |
639 | | @with_all_renderers |
640 | | def test_getslice2(r): |
641 | | assert "gur" == r(u"<?print 'gurk'[:3]?>") |
642 | | assert "gur" == r(u"<?print x[:3]?>", x="gurk") |
643 | | assert "gur" == r(u"<?print 'gurk'[:-1]?>") |
644 | | assert "gur" == r(u"<?print x[:-1]?>", x="gurk") |
645 | | assert "gurk" == r(u"<?print 'gurk'[:10]?>") |
646 | | assert "gurk" == r(u"<?print x[:10]?>", x="gurk") |
647 | | assert "" == r(u"<?print 'gurk'[:-5]?>") |
648 | | assert "" == r(u"<?print x[:-5]?>", x="gurk") |
649 | | |
650 | | |
651 | | @with_all_renderers |
652 | | def test_nested(r): |
| 577 | for r in all_renderers: |
| 578 | yield eq, "False", r(code, x=2, y=[1, 2, 3]) |
| 579 | yield eq, "True", r(code, x=4, y=[1, 2, 3]) |
| 580 | yield eq, "False", r(code, x="ur", y="gurk") |
| 581 | yield eq, "True", r(code, x="un", y="gurk") |
| 582 | yield eq, "False", r(code, x="a", y={"a": 1, "b": 2}) |
| 583 | yield eq, "True", r(code, x="c", y={"a": 1, "b": 2}) |
| 584 | yield eq, "False", r(code, x=0xff, y=color.Color(0x00, 0x80, 0xff, 0x42)) |
| 585 | yield eq, "True", r(code, x=0x23, y=color.Color(0x00, 0x80, 0xff, 0x42)) |
| 586 | |
| 587 | |
| 588 | def test_and(): |
| 589 | for r in all_renderers: |
| 590 | yield eq, "False", r(u'<?print x and y?>', x=False, y=False) |
| 591 | yield eq, "False", r(u'<?print x and y?>', x=False, y=True) |
| 592 | yield eq, "0", r(u'<?print x and y?>', x=0, y=True) |
| 593 | |
| 594 | |
| 595 | def test_or(): |
| 596 | for r in all_renderers: |
| 597 | yield eq, "False", r(u'<?print x or y?>', x=False, y=False) |
| 598 | yield eq, "True", r(u'<?print x or y?>', x=False, y=True) |
| 599 | yield eq, "42", r(u'<?print x or y?>', x=42, y=True) |
| 600 | |
| 601 | |
| 602 | def test_not(): |
| 603 | for r in all_renderers: |
| 604 | yield eq, "True", r(u'<?print not x?>', x=False) |
| 605 | yield eq, "False", r(u'<?print not x?>', x=42) |
| 606 | |
| 607 | |
| 608 | def test_getitem(): |
| 609 | for r in all_renderers: |
| 610 | yield eq, "u", r(u"<?print 'gurk'[1]?>") |
| 611 | yield eq, "u", r(u"<?print x[1]?>", x="gurk") |
| 612 | yield eq, "u", r(u"<?print 'gurk'[-3]?>") |
| 613 | yield eq, "u", r(u"<?print x[-3]?>", x="gurk") |
| 614 | yield raises, "IndexError", r(u"<?print 'gurk'[4]?>") |
| 615 | yield raises, "index (4 )?out of range", r(u"<?print x[4]?>", x="gurk") |
| 616 | yield raises, "IndexError", r(u"<?print 'gurk'[-5]?>") |
| 617 | yield raises, "index (-5 )?out of range", r(u"<?print x[-5]?>", x="gurk") |
| 618 | |
| 619 | |
| 620 | def test_getslice12(): |
| 621 | for r in all_renderers: |
| 622 | yield eq, "ur", r(u"<?print 'gurk'[1:3]?>") |
| 623 | yield eq, "ur", r(u"<?print x[1:3]?>", x="gurk") |
| 624 | yield eq, "ur", r(u"<?print 'gurk'[-3:-1]?>") |
| 625 | yield eq, "ur", r(u"<?print x[-3:-1]?>", x="gurk") |
| 626 | yield eq, "", r(u"<?print 'gurk'[4:10]?>") |
| 627 | yield eq, "", r(u"<?print x[4:10]?>", x="gurk") |
| 628 | yield eq, "", r(u"<?print 'gurk'[-10:-5]?>") |
| 629 | yield eq, "", r(u"<?print x[-10:-5]?>", x="gurk") |
| 630 | |
| 631 | |
| 632 | def test_getslice1(): |
| 633 | for r in all_renderers: |
| 634 | yield eq, "urk", r(u"<?print 'gurk'[1:]?>") |
| 635 | yield eq, "urk", r(u"<?print x[1:]?>", x="gurk") |
| 636 | yield eq, "urk", r(u"<?print 'gurk'[-3:]?>") |
| 637 | yield eq, "urk", r(u"<?print x[-3:]?>", x="gurk") |
| 638 | yield eq, "", r(u"<?print 'gurk'[4:]?>") |
| 639 | yield eq, "", r(u"<?print x[4:]?>", x="gurk") |
| 640 | yield eq, "gurk", r(u"<?print 'gurk'[-10:]?>") |
| 641 | yield eq, "gurk", r(u"<?print x[-10:]?>", x="gurk") |
| 642 | |
| 643 | |
| 644 | def test_getslice2(): |
| 645 | for r in all_renderers: |
| 646 | yield eq, "gur", r(u"<?print 'gurk'[:3]?>") |
| 647 | yield eq, "gur", r(u"<?print x[:3]?>", x="gurk") |
| 648 | yield eq, "gur", r(u"<?print 'gurk'[:-1]?>") |
| 649 | yield eq, "gur", r(u"<?print x[:-1]?>", x="gurk") |
| 650 | yield eq, "gurk", r(u"<?print 'gurk'[:10]?>") |
| 651 | yield eq, "gurk", r(u"<?print x[:10]?>", x="gurk") |
| 652 | yield eq, "", r(u"<?print 'gurk'[:-5]?>") |
| 653 | yield eq, "", r(u"<?print x[:-5]?>", x="gurk") |
| 654 | |
| 655 | |
| 656 | def test_nested(): |
720 | | with raises("vars.*unknown"): |
721 | | r("<?print vars(1)?>") |
722 | | with raises("vars.*unknown"): |
723 | | r("<?print vars(1, 2)?>") |
724 | | assert "yes" == r(code, var="spam", spam="eggs") |
725 | | assert "no" == r(code, var="nospam", spam="eggs") |
726 | | |
727 | | |
728 | | @with_all_renderers |
729 | | def test_function_random(r): |
730 | | with raises("random.*unknown"): |
731 | | r("<?print random(1)?>") |
732 | | with raises("random.*unknown"): |
733 | | r("<?print random(1, 2)?>") |
734 | | assert "ok" == r(u"<?code r = random()?><?if r>=0 and r<1?>ok<?else?>fail<?end if?>") |
735 | | |
736 | | |
737 | | @with_all_renderers |
738 | | def test_function_randrange(r): |
739 | | with raises("randrange.*unknown"): |
740 | | r("<?print randrange()?>") |
741 | | assert "ok" == r(u"<?code r = randrange(4)?><?if r>=0 and r<4?>ok<?else?>fail<?end if?>") |
742 | | assert "ok" == r(u"<?code r = randrange(17, 23)?><?if r>=17 and r<23?>ok<?else?>fail<?end if?>") |
743 | | assert "ok" == r(u"<?code r = randrange(17, 23, 2)?><?if r>=17 and r<23 and r%2?>ok<?else?>fail<?end if?>") |
744 | | |
745 | | |
746 | | @with_all_renderers |
747 | | def test_function_randchoice(r): |
748 | | with raises("randchoice.*unknown"): |
749 | | r("<?print randchoice()?>") |
750 | | assert "ok" == r(u"<?code r = randchoice('abc')?><?if r in 'abc'?>ok<?else?>fail<?end if?>") |
751 | | assert "ok" == r(u"<?code s = [17, 23, 42]?><?code r = randchoice(s)?><?if r in s?>ok<?else?>fail<?end if?>") |
752 | | assert "ok" == r(u"<?code s = #12345678?><?code sl = [0x12, 0x34, 0x56, 0x78]?><?code r = randchoice(s)?><?if r in sl?>ok<?else?>fail<?end if?>") |
753 | | |
754 | | |
755 | | @with_all_renderers |
756 | | def test_function_xmlescape(r): |
757 | | with raises("xmlescape.*unknown"): |
758 | | r(u"<?print xmlescape()?>") |
759 | | with raises("xmlescape.*unknown"): |
760 | | r(u"<?print xmlescape(1, 2)?>") |
761 | | assert "<<>>&'"gurk" == r(u"<?print xmlescape(data)?>", data='<<>>&\'"gurk') |
762 | | |
763 | | |
764 | | @with_all_renderers |
765 | | def test_function_csv(r): |
766 | | with raises("csv.*unknown"): |
767 | | r(u"<?print csv()?>") |
768 | | with raises("csv.*unknown"): |
769 | | r(u"<?print csv(1, 2)?>") |
770 | | assert "" == r(u"<?print csv(data)?>", data=None) |
771 | | assert "False" == r(u"<?print csv(data)?>", data=False) |
772 | | assert "True" == r(u"<?print csv(data)?>", data=True) |
773 | | assert "42" == r(u"<?print csv(data)?>", data=42) |
774 | | # no check for float |
775 | | assert "abc" == r(u"<?print csv(data)?>", data="abc") |
776 | | assert '"a,b,c"' == r(u"<?print csv(data)?>", data="a,b,c") |
777 | | assert '"a""b""c"' == r(u"<?print csv(data)?>", data='a"b"c') |
778 | | assert '"a\nb\nc"' == r(u"<?print csv(data)?>", data="a\nb\nc") |
779 | | |
780 | | |
781 | | @with_all_renderers |
782 | | def test_function_json(r): |
783 | | with raises("json.*unknown"): |
784 | | r(u"<?print json()?>") |
785 | | with raises("json.*unknown"): |
786 | | r(u"<?print json(1, 2)?>") |
787 | | assert "null" == r(u"<?print json(data)?>", data=None) |
788 | | assert "false" == r(u"<?print json(data)?>", data=False) |
789 | | assert "true" == r(u"<?print json(data)?>", data=True) |
790 | | assert "42" == r(u"<?print json(data)?>", data=42) |
791 | | # no check for float |
792 | | assert '"abc"' == r(u"<?print json(data)?>", data="abc") |
793 | | assert '[1, 2, 3]', r(u"<?print json(data)?>", data=[1, 2, 3]) |
794 | | assert '[1, 2, 3]', r(u"<?print json(data)?>", data=PseudoList([1, 2, 3])) |
795 | | assert '{"one": 1}' == r(u"<?print json(data)?>", data={"one": 1}) |
796 | | assert '{"one": 1}' == r(u"<?print json(data)?>", data=PseudoDict({"one": 1})) |
797 | | |
798 | | |
799 | | @with_all_renderers |
800 | | def test_function_str(r): |
801 | | with raises("str.*unknown"): |
802 | | r(u"<?print str()?>") |
803 | | with raises("str.*unknown"): |
804 | | r(u"<?print str(1, 2)?>") |
| 720 | for r in all_renderers: |
| 721 | yield raises, "vars.*unknown", r("<?print vars(1)?>") |
| 722 | yield raises, "vars.*unknown", r("<?print vars(1, 2)?>") |
| 723 | yield eq, "yes", r(code, var="spam", spam="eggs") |
| 724 | yield eq, "no", r(code, var="nospam", spam="eggs") |
| 725 | |
| 726 | |
| 727 | def test_function_random(): |
| 728 | for r in all_renderers: |
| 729 | yield raises, "random.*unknown", r("<?print random(1)?>") |
| 730 | yield raises, "random.*unknown", r("<?print random(1, 2)?>") |
| 731 | yield eq, "ok", r(u"<?code r = random()?><?if r>=0 and r<1?>ok<?else?>fail<?end if?>") |
| 732 | |
| 733 | |
| 734 | def test_function_randrange(): |
| 735 | for r in all_renderers: |
| 736 | yield raises, "randrange.*unknown", r("<?print randrange()?>") |
| 737 | yield eq, "ok", r(u"<?code r = randrange(4)?><?if r>=0 and r<4?>ok<?else?>fail<?end if?>") |
| 738 | yield eq, "ok", r(u"<?code r = randrange(17, 23)?><?if r>=17 and r<23?>ok<?else?>fail<?end if?>") |
| 739 | yield eq, "ok", r(u"<?code r = randrange(17, 23, 2)?><?if r>=17 and r<23 and r%2?>ok<?else?>fail<?end if?>") |
| 740 | |
| 741 | |
| 742 | def test_function_randchoice(): |
| 743 | for r in all_renderers: |
| 744 | yield raises, "randchoice.*unknown", r("<?print randchoice()?>") |
| 745 | yield eq, "ok", r(u"<?code r = randchoice('abc')?><?if r in 'abc'?>ok<?else?>fail<?end if?>") |
| 746 | yield eq, "ok", r(u"<?code s = [17, 23, 42]?><?code r = randchoice(s)?><?if r in s?>ok<?else?>fail<?end if?>") |
| 747 | yield eq, "ok", r(u"<?code s = #12345678?><?code sl = [0x12, 0x34, 0x56, 0x78]?><?code r = randchoice(s)?><?if r in sl?>ok<?else?>fail<?end if?>") |
| 748 | |
| 749 | |
| 750 | def test_function_xmlescape(): |
| 751 | for r in all_renderers: |
| 752 | yield raises, "xmlescape.*unknown", r(u"<?print xmlescape()?>") |
| 753 | yield raises, "xmlescape.*unknown", r(u"<?print xmlescape(1, 2)?>") |
| 754 | yield eq, "<<>>&'"gurk", r(u"<?print xmlescape(data)?>", data='<<>>&\'"gurk') |
| 755 | |
| 756 | |
| 757 | def test_function_csv(): |
| 758 | for r in all_renderers: |
| 759 | yield raises, "csv.*unknown", r(u"<?print csv()?>") |
| 760 | yield raises, "csv.*unknown", r(u"<?print csv(1, 2)?>") |
| 761 | yield eq, "", r(u"<?print csv(data)?>", data=None) |
| 762 | yield eq, "False", r(u"<?print csv(data)?>", data=False) |
| 763 | yield eq, "True", r(u"<?print csv(data)?>", data=True) |
| 764 | yield eq, "42", r(u"<?print csv(data)?>", data=42) |
| 765 | # no check for float |
| 766 | yield eq, "abc", r(u"<?print csv(data)?>", data="abc") |
| 767 | yield eq, '"a,b,c"', r(u"<?print csv(data)?>", data="a,b,c") |
| 768 | yield eq, '"a""b""c"', r(u"<?print csv(data)?>", data='a"b"c') |
| 769 | yield eq, '"a\nb\nc"', r(u"<?print csv(data)?>", data="a\nb\nc") |
| 770 | |
| 771 | |
| 772 | def test_function_json(): |
| 773 | for r in all_renderers: |
| 774 | yield raises, "json.*unknown", r(u"<?print json()?>") |
| 775 | yield raises, "json.*unknown", r(u"<?print json(1, 2)?>") |
| 776 | yield eq, "null", r(u"<?print json(data)?>", data=None) |
| 777 | yield eq, "false", r(u"<?print json(data)?>", data=False) |
| 778 | yield eq, "true", r(u"<?print json(data)?>", data=True) |
| 779 | yield eq, "42", r(u"<?print json(data)?>", data=42) |
| 780 | # no check for float |
| 781 | yield eq, '"abc"', r(u"<?print json(data)?>", data="abc") |
| 782 | yield eq, '[1, 2, 3]', r(u"<?print json(data)?>", data=[1, 2, 3]) |
| 783 | yield eq, '[1, 2, 3]', r(u"<?print json(data)?>", data=PseudoList([1, 2, 3])) |
| 784 | yield eq, '{"one": 1}', r(u"<?print json(data)?>", data={"one": 1}) |
| 785 | yield eq, '{"one": 1}', r(u"<?print json(data)?>", data=PseudoDict({"one": 1})) |
| 786 | |
| 787 | |
| 788 | def test_function_str(): |
806 | | assert "" == r(code, data=None) |
807 | | assert "True" == r(code, data=True) |
808 | | assert "False" == r(code, data=False) |
809 | | assert "42" == r(code, data=42) |
810 | | assert "4.2" == r(code, data=4.2) |
811 | | assert "foo" == r(code, data="foo") |
812 | | assert "2011-02-09" == r(code, data=datetime.date(2011, 2, 9)) |
813 | | assert "2011-02-09 12:34:56" == r(code, data=datetime.datetime(2011, 2, 9, 12, 34, 56)) |
814 | | assert "2011-02-09 12:34:56.987000" == r(code, data=datetime.datetime(2011, 2, 9, 12, 34, 56, 987000)) |
815 | | |
816 | | |
817 | | @with_all_renderers |
818 | | def test_function_int(r): |
819 | | with raises("int.*unknown"): |
820 | | r(u"<?print int()?>") |
821 | | with raises("int.*unknown"): |
822 | | r(u"<?print int(1, 2, 3)?>") |
823 | | with raises("int\\(\\) argument must be a string or a number|int\\(null\\) not supported"): |
824 | | r(u"<?print int(data)?>", data=None) |
825 | | with raises("invalid literal for int|NumberFormatException"): |
826 | | r(u"<?print int(data)?>", data="foo") |
827 | | assert "1" == r(u"<?print int(data)?>", data=True) |
828 | | assert "0" == r(u"<?print int(data)?>", data=False) |
829 | | assert "42" == r(u"<?print int(data)?>", data=42) |
830 | | assert "4" == r(u"<?print int(data)?>", data=4.2) |
831 | | assert "42" == r(u"<?print int(data)?>", data="42") |
832 | | assert "66" == r(u"<?print int(data, 16)?>", data="42") |
833 | | |
834 | | |
835 | | @with_all_renderers |
836 | | def test_function_float(r): |
| 790 | for r in all_renderers: |
| 791 | yield raises, "str.*unknown", r(u"<?print str()?>") |
| 792 | yield raises, "str.*unknown", r(u"<?print str(1, 2)?>") |
| 793 | yield eq, "", r(code, data=None) |
| 794 | yield eq, "True", r(code, data=True) |
| 795 | yield eq, "False", r(code, data=False) |
| 796 | yield eq, "42", r(code, data=42) |
| 797 | yield eq, "4.2", r(code, data=4.2) |
| 798 | yield eq, "foo", r(code, data="foo") |
| 799 | yield eq, "2011-02-09", r(code, data=datetime.date(2011, 2, 9)) |
| 800 | yield eq, "2011-02-09 12:34:56", r(code, data=datetime.datetime(2011, 2, 9, 12, 34, 56)) |
| 801 | yield eq, "2011-02-09 12:34:56.987000", r(code, data=datetime.datetime(2011, 2, 9, 12, 34, 56, 987000)) |
| 802 | |
| 803 | |
| 804 | def test_function_int(): |
| 805 | for r in all_renderers: |
| 806 | yield raises, "int.*unknown", RenderPython(u"<?print int()?>") |
| 807 | yield raises, "int.*unknown", RenderPython(u"<?print int(1, 2, 3)?>") |
| 808 | yield raises, "int\\(\\) argument must be a string or a number|int\\(null\\) not supported", RenderPython(u"<?print int(data)?>", data=None) |
| 809 | yield raises, "invalid literal for int|NumberFormatException", RenderPython(u"<?print int(data)?>", data="foo") |
| 810 | yield eq, "1", r(u"<?print int(data)?>", data=True) |
| 811 | yield eq, "0", r(u"<?print int(data)?>", data=False) |
| 812 | yield eq, "42", r(u"<?print int(data)?>", data=42) |
| 813 | yield eq, "4", r(u"<?print int(data)?>", data=4.2) |
| 814 | yield eq, "42", r(u"<?print int(data)?>", data="42") |
| 815 | yield eq, "66", r(u"<?print int(data, 16)?>", data="42") |
| 816 | |
| 817 | |
| 818 | def test_function_float(): |
1137 | | with raises("repr.*unknown"): |
1138 | | r(u"<?print repr()?>") |
1139 | | with raises("repr.*unknown"): |
1140 | | r(u"<?print repr(1, 2)?>") |
1141 | | assert "None" == r(code, data=None) |
1142 | | assert "True" == r(code, data=True) |
1143 | | assert "False" == r(code, data=False) |
1144 | | assert "42" == r(code, data=42) |
1145 | | assert 42.5 == eval(r(code, data=42.5)) |
1146 | | assert r(code, data="foo") in ('"foo"', "'foo'") |
1147 | | assert [1, 2, 3] == eval(r(code, data=[1, 2, 3])) |
1148 | | if r is not renderjs: |
1149 | | assert [1, 2, 3] == eval(r(code, data=(1, 2, 3))) |
1150 | | assert {"a": 1, "b": 2} == eval(r(code, data={"a": 1, "b": 2})) |
1151 | | assert "@2011-02-07T12:34:56.123000" == r(code, data=datetime.datetime(2011, 2, 7, 12, 34, 56, 123000)) |
1152 | | assert "@2011-02-07T12:34:56" == r(code, data=datetime.datetime(2011, 2, 7, 12, 34, 56)) |
1153 | | assert "@2011-02-07T" == r(code, data=datetime.datetime(2011, 2, 7)) |
1154 | | assert "@2011-02-07T" == r(code, data=datetime.date(2011, 2, 7)) |
1155 | | |
1156 | | |
1157 | | @with_all_renderers |
1158 | | def test_function_chr(r): |
| 1081 | for r in all_renderers: |
| 1082 | yield raises, "repr.*unknown", r(u"<?print repr()?>") |
| 1083 | yield raises, "repr.*unknown", r(u"<?print repr(1, 2)?>") |
| 1084 | yield eq, "None", r(code, data=None) |
| 1085 | yield eq, "True", r(code, data=True) |
| 1086 | yield eq, "False", r(code, data=False) |
| 1087 | yield eq, "42", r(code, data=42) |
| 1088 | yield evaleq, 42.5, r(code, data=42.5) |
| 1089 | yield evalcontains, ('"foo"', "'foo'"), r(code, data="foo") |
| 1090 | yield evaleq, [1, 2, 3], r(code, data=[1, 2, 3]) |
| 1091 | if r is not renderjs: |
| 1092 | yield evaleq, [1, 2, 3], r(code, data=(1, 2, 3)) |
| 1093 | yield evaleq, {"a": 1, "b": 2}, r(code, data={"a": 1, "b": 2}) |
| 1094 | yield eq, "@2011-02-07T12:34:56.123000", r(code, data=datetime.datetime(2011, 2, 7, 12, 34, 56, 123000)) |
| 1095 | yield eq, "@2011-02-07T12:34:56", r(code, data=datetime.datetime(2011, 2, 7, 12, 34, 56)) |
| 1096 | yield eq, "@2011-02-07T", r(code, data=datetime.datetime(2011, 2, 7)) |
| 1097 | yield eq, "@2011-02-07T", r(code, data=datetime.date(2011, 2, 7)) |
| 1098 | |
| 1099 | |
| 1100 | def test_function_chr(): |
1241 | | with raises("sorted.*unknown"): |
1242 | | r(u"<?print sorted()?>") |
1243 | | with raises("sorted.*unknown"): |
1244 | | r(u"<?print sorted(1, 2)?>") |
1245 | | assert "gkru" == r(code, data="gurk") |
1246 | | assert "24679" == r(code, data="92746") |
1247 | | assert "012" == r(code, data={0: "zero", 1: "one", 2: "two"}) |
1248 | | |
1249 | | |
1250 | | @with_all_renderers |
1251 | | def test_function_range(r): |
1252 | | with raises("range.*unknown"): |
1253 | | r(u"<?print range()?>") |
1254 | | code = u"<?for i in range(data)?><?print i?><?end for?>" |
1255 | | assert "" == r(code, data=-10) |
1256 | | assert "" == r(code, data=0) |
1257 | | assert "0" == r(code, data=1) |
1258 | | assert "01234" == r(code, data=5) |
1259 | | code = u"<?for i in range(data[0], data[1])?><?print i?><?end for?>" |
1260 | | assert "" == r(code, data=[0, -10]) |
1261 | | assert "" == r(code, data=[0, 0]) |
1262 | | assert "01234" == r(code, data=[0, 5]) |
1263 | | assert "-5-4-3-2-101234" == r(code, data=[-5, 5]) |
1264 | | code = u"<?for i in range(data[0], data[1], data[2])?><?print i?><?end for?>" |
1265 | | assert "" == r(code, data=[0, -10, 1]) |
1266 | | assert "" == r(code, data=[0, 0, 1]) |
1267 | | assert "02468" == r(code, data=[0, 10, 2]) |
1268 | | assert "" == r(code, data=[0, 10, -2]) |
1269 | | assert "108642" == r(code, data=[10, 0, -2]) |
1270 | | assert "" == r(code, data=[10, 0, 2]) |
1271 | | |
1272 | | |
1273 | | @with_all_renderers |
1274 | | def test_function_zip(r): |
1275 | | with raises("zip.*unknown"): |
1276 | | r(u"<?print zip()?>") |
1277 | | with raises("zip.*unknown"): |
1278 | | r(u"<?print zip(1)?>") |
1279 | | code = u"<?for (ix, iy) in zip(x, y)?><?print ix?>-<?print iy?>;<?end for?>" |
1280 | | assert "" == r(code, x=[], y=[]) |
1281 | | assert "1-3;2-4;" == r(code, x=[1, 2], y=[3, 4]) |
1282 | | assert "1-4;2-5;" == r(code, x=[1, 2, 3], y=[4, 5]) |
1283 | | code = u"<?for (ix, iy, iz) in zip(x, y, z)?><?print ix?>-<?print iy?>+<?print iz?>;<?end for?>" |
1284 | | assert "" == r(code, x=[], y=[], z=[]) |
1285 | | assert "1-3+5;2-4+6;" == r(code, x=[1, 2], y=[3, 4], z=[5, 6]) |
1286 | | assert "1-4+6;" == r(code, x=[1, 2, 3], y=[4, 5], z=[6]) |
1287 | | |
1288 | | |
1289 | | @with_all_renderers |
1290 | | def test_function_type(r): |
| 1171 | for r in all_renderers: |
| 1172 | yield raises, "sorted.*unknown", r(u"<?print sorted()?>") |
| 1173 | yield raises, "sorted.*unknown", r(u"<?print sorted(1, 2)?>") |
| 1174 | yield eq, "gkru", r(code, data="gurk") |
| 1175 | yield eq, "24679", r(code, data="92746") |
| 1176 | yield eq, "012", r(code, data={0: "zero", 1: "one", 2: "two"}) |
| 1177 | |
| 1178 | |
| 1179 | def test_function_range(): |
| 1180 | code1 = u"<?for i in range(data)?><?print i?><?end for?>" |
| 1181 | code2 = u"<?for i in range(data[0], data[1])?><?print i?><?end for?>" |
| 1182 | code3 = u"<?for i in range(data[0], data[1], data[2])?><?print i?><?end for?>" |
| 1183 | |
| 1184 | for r in all_renderers: |
| 1185 | yield raises, "range.*unknown", r(u"<?print range()?>") |
| 1186 | yield eq, "", r(code1, data=-10) |
| 1187 | yield eq, "", r(code1, data=0) |
| 1188 | yield eq, "0", r(code1, data=1) |
| 1189 | yield eq, "01234", r(code1, data=5) |
| 1190 | yield eq, "", r(code2, data=[0, -10]) |
| 1191 | yield eq, "", r(code2, data=[0, 0]) |
| 1192 | yield eq, "01234", r(code2, data=[0, 5]) |
| 1193 | yield eq, "-5-4-3-2-101234", r(code2, data=[-5, 5]) |
| 1194 | yield eq, "", r(code3, data=[0, -10, 1]) |
| 1195 | yield eq, "", r(code3, data=[0, 0, 1]) |
| 1196 | yield eq, "02468", r(code3, data=[0, 10, 2]) |
| 1197 | yield eq, "", r(code3, data=[0, 10, -2]) |
| 1198 | yield eq, "108642", r(code3, data=[10, 0, -2]) |
| 1199 | yield eq, "", r(code3, data=[10, 0, 2]) |
| 1200 | |
| 1201 | |
| 1202 | def test_function_zip(): |
| 1203 | code2 = u"<?for (ix, iy) in zip(x, y)?><?print ix?>-<?print iy?>;<?end for?>" |
| 1204 | code3 = u"<?for (ix, iy, iz) in zip(x, y, z)?><?print ix?>-<?print iy?>+<?print iz?>;<?end for?>" |
| 1205 | |
| 1206 | for r in all_renderers: |
| 1207 | yield raises, "zip.*unknown", r(u"<?print zip(1)?>") |
| 1208 | yield eq, "", r(code2, x=[], y=[]) |
| 1209 | yield eq, "1-3;2-4;", r(code2, x=[1, 2], y=[3, 4]) |
| 1210 | yield eq, "1-4;2-5;", r(code2, x=[1, 2, 3], y=[4, 5]) |
| 1211 | yield eq, "", r(code3, x=[], y=[], z=[]) |
| 1212 | yield eq, "1-3+5;2-4+6;", r(code3, x=[1, 2], y=[3, 4], z=[5, 6]) |
| 1213 | yield eq, "1-4+6;", r(code3, x=[1, 2, 3], y=[4, 5], z=[6]) |
| 1214 | |
| 1215 | |
| 1216 | def test_function_type(): |
1320 | | with raises("reversed.*unknown"): |
1321 | | r(u"<?print reversed()?>") |
1322 | | with raises("reversed.*unknown"): |
1323 | | r(u"<?print reversed(1, 2)?>") |
1324 | | assert "(3)(2)(1)" == r(code, x="123") |
1325 | | assert "(3)(2)(1)" == r(code, x=[1, 2, 3]) |
1326 | | assert "(3)(2)(1)" == r(code, x=(1, 2, 3)) |
1327 | | |
1328 | | |
1329 | | @with_all_renderers |
1330 | | def test_function_rgb(r): |
1331 | | assert "#369" == r("<?print repr(rgb(0.2, 0.4, 0.6))?>") |
1332 | | assert "#369c" == r("<?print repr(rgb(0.2, 0.4, 0.6, 0.8))?>") |
1333 | | |
1334 | | |
1335 | | @with_all_renderers |
1336 | | def test_function_hls(r): |
1337 | | assert "#fff" == r("<?print repr(hls(0, 1, 0))?>") |
1338 | | assert "#fff0" == r("<?print repr(hls(0, 1, 0, 0))?>") |
1339 | | |
1340 | | |
1341 | | @with_all_renderers |
1342 | | def test_function_hsv(r): |
1343 | | assert "#fff" == r("<?print repr(hsv(0, 0, 1))?>") |
1344 | | assert "#fff0" == r("<?print repr(hsv(0, 0, 1, 0))?>") |
1345 | | |
1346 | | |
1347 | | @with_all_renderers |
1348 | | def test_method_upper(r): |
1349 | | assert "GURK" == r(u"<?print 'gurk'.upper()?>") |
1350 | | |
1351 | | |
1352 | | @with_all_renderers |
1353 | | def test_method_lower(r): |
1354 | | assert "gurk" == r(u"<?print 'GURK'.lower()?>") |
1355 | | |
1356 | | |
1357 | | @with_all_renderers |
1358 | | def test_method_capitalize(r): |
1359 | | assert "Gurk" == r(u"<?print 'gURK'.capitalize()?>") |
1360 | | |
1361 | | |
1362 | | @with_all_renderers |
1363 | | def test_method_startswith(r): |
1364 | | assert "True" == r(u"<?print 'gurkhurz'.startswith('gurk')?>") |
1365 | | assert "False" == r(u"<?print 'gurkhurz'.startswith('hurz')?>") |
1366 | | |
1367 | | |
1368 | | @with_all_renderers |
1369 | | def test_method_endswith(r): |
1370 | | assert "True" == r(u"<?print 'gurkhurz'.endswith('hurz')?>") |
1371 | | assert "False" == r(u"<?print 'gurkhurz'.endswith('gurk')?>") |
1372 | | |
1373 | | |
1374 | | @with_all_renderers |
1375 | | def test_method_strip(r): |
1376 | | assert "gurk" == r(r"<?print ' \t\r\ngurk \t\r\n'.strip()?>") |
1377 | | assert "gurk" == r(r"<?print 'xyzzygurkxyzzy'.strip('xyz')?>") |
1378 | | |
1379 | | |
1380 | | @with_all_renderers |
1381 | | def test_method_lstrip(r): |
1382 | | assert "gurk \t\r\n" == r(ur"<?print ' \t\r\ngurk \t\r\n'.lstrip()?>") |
1383 | | assert "gurkxyzzy" == r(ur"<?print 'xyzzygurkxyzzy'.lstrip('xyz')?>") |
1384 | | |
1385 | | |
1386 | | @with_all_renderers |
1387 | | def test_method_rstrip(r): |
1388 | | assert " \t\r\ngurk" == r(ur"<?print ' \t\r\ngurk \t\r\n'.rstrip()?>") |
1389 | | assert "xyzzygurk" == r(ur"<?print 'xyzzygurkxyzzy'.rstrip('xyz')?>") |
1390 | | |
1391 | | |
1392 | | @with_all_renderers |
1393 | | def test_method_split(r): |
1394 | | assert "(g)(u)(r)(k)" == r(ur"<?for item in ' \t\r\ng \t\r\nu \t\r\nr \t\r\nk \t\r\n'.split()?>(<?print item?>)<?end for?>") |
1395 | | assert "(g)(u \t\r\nr \t\r\nk \t\r\n)" == r(ur"<?for item in ' \t\r\ng \t\r\nu \t\r\nr \t\r\nk \t\r\n'.split(None, 1)?>(<?print item?>)<?end for?>") |
1396 | | assert "()(g)(u)(r)(k)()" == r(ur"<?for item in 'xxgxxuxxrxxkxx'.split('xx')?>(<?print item?>)<?end for?>") |
1397 | | assert "()(g)(uxxrxxkxx)" == r(ur"<?for item in 'xxgxxuxxrxxkxx'.split('xx', 2)?>(<?print item?>)<?end for?>") |
1398 | | |
1399 | | |
1400 | | @with_all_renderers |
1401 | | def test_method_rsplit(r): |
1402 | | assert "(g)(u)(r)(k)" == r(ur"<?for item in ' \t\r\ng \t\r\nu \t\r\nr \t\r\nk \t\r\n'.rsplit()?>(<?print item?>)<?end for?>") |
1403 | | assert "( \t\r\ng \t\r\nu \t\r\nr)(k)" == r(ur"<?for item in ' \t\r\ng \t\r\nu \t\r\nr \t\r\nk \t\r\n'.rsplit(None, 1)?>(<?print item?>)<?end for?>") |
1404 | | assert "()(g)(u)(r)(k)()" == r(ur"<?for item in 'xxgxxuxxrxxkxx'.rsplit('xx')?>(<?print item?>)<?end for?>") |
1405 | | assert "(xxgxxuxxr)(k)()" == r(ur"<?for item in 'xxgxxuxxrxxkxx'.rsplit('xx', 2)?>(<?print item?>)<?end for?>") |
1406 | | |
1407 | | |
1408 | | @with_all_renderers |
1409 | | def test_method_replace(r): |
1410 | | assert 'goork' == r(ur"<?print 'gurk'.replace('u', 'oo')?>") |
1411 | | |
1412 | | |
1413 | | @with_all_renderers |
1414 | | def test_method_render(r): |
1415 | | t = ul4c.compile(u'(<?print data?>)') |
1416 | | assert '(GURK)' == r(u"<?print t.render(data='gurk').upper()?>", t=t) |
1417 | | assert '(GURK)' == r(u"<?print t.render(**{'data': 'gurk'}).upper()?>", t=t) |
1418 | | |
1419 | | t = ul4c.compile(u'(gurk)') |
1420 | | assert '(GURK)' == r(u"<?print t.render().upper()?>", t=t) |
1421 | | |
1422 | | |
1423 | | @with_all_renderers |
1424 | | def test_method_format(r): |
| 1244 | for r in all_renderers: |
| 1245 | yield raises, "reversed.*unknown", r(u"<?print reversed()?>") |
| 1246 | yield raises, "reversed.*unknown", r(u"<?print reversed(1, 2)?>") |
| 1247 | yield eq, "(3)(2)(1)", r(code, x="123") |
| 1248 | yield eq, "(3)(2)(1)", r(code, x=[1, 2, 3]) |
| 1249 | yield eq, "(3)(2)(1)", r(code, x=(1, 2, 3)) |
| 1250 | |
| 1251 | |
| 1252 | def test_function_rgb(): |
| 1253 | for r in all_renderers: |
| 1254 | yield eq, "#369", r("<?print repr(rgb(0.2, 0.4, 0.6))?>") |
| 1255 | yield eq, "#369c", r("<?print repr(rgb(0.2, 0.4, 0.6, 0.8))?>") |
| 1256 | |
| 1257 | |
| 1258 | def test_function_hls(): |
| 1259 | for r in all_renderers: |
| 1260 | yield eq, "#fff", r("<?print repr(hls(0, 1, 0))?>") |
| 1261 | yield eq, "#fff0", r("<?print repr(hls(0, 1, 0, 0))?>") |
| 1262 | |
| 1263 | |
| 1264 | def test_function_hsv(): |
| 1265 | for r in all_renderers: |
| 1266 | yield eq, "#fff", r("<?print repr(hsv(0, 0, 1))?>") |
| 1267 | yield eq, "#fff0", r("<?print repr(hsv(0, 0, 1, 0))?>") |
| 1268 | |
| 1269 | |
| 1270 | def test_method_upper(): |
| 1271 | for r in all_renderers: |
| 1272 | yield eq, "GURK", r(u"<?print 'gurk'.upper()?>") |
| 1273 | |
| 1274 | |
| 1275 | def test_method_lower(): |
| 1276 | for r in all_renderers: |
| 1277 | yield eq, "gurk", r(u"<?print 'GURK'.lower()?>") |
| 1278 | |
| 1279 | |
| 1280 | def test_method_capitalize(): |
| 1281 | for r in all_renderers: |
| 1282 | yield eq, "Gurk", r(u"<?print 'gURK'.capitalize()?>") |
| 1283 | |
| 1284 | |
| 1285 | def test_method_startswith(): |
| 1286 | for r in all_renderers: |
| 1287 | yield eq, "True", r(u"<?print 'gurkhurz'.startswith('gurk')?>") |
| 1288 | yield eq, "False", r(u"<?print 'gurkhurz'.startswith('hurz')?>") |
| 1289 | |
| 1290 | |
| 1291 | def test_method_endswith(): |
| 1292 | for r in all_renderers: |
| 1293 | yield eq, "True", r(u"<?print 'gurkhurz'.endswith('hurz')?>") |
| 1294 | yield eq, "False", r(u"<?print 'gurkhurz'.endswith('gurk')?>") |
| 1295 | |
| 1296 | |
| 1297 | def test_method_strip(): |
| 1298 | for r in all_renderers: |
| 1299 | yield eq, "gurk", r(r"<?print ' \t\r\ngurk \t\r\n'.strip()?>") |
| 1300 | yield eq, "gurk", r(r"<?print 'xyzzygurkxyzzy'.strip('xyz')?>") |
| 1301 | |
| 1302 | |
| 1303 | def test_method_lstrip(): |
| 1304 | for r in all_renderers: |
| 1305 | yield eq, "gurk \t\r\n", r(ur"<?print ' \t\r\ngurk \t\r\n'.lstrip()?>") |
| 1306 | yield eq, "gurkxyzzy", r(ur"<?print 'xyzzygurkxyzzy'.lstrip('xyz')?>") |
| 1307 | |
| 1308 | |
| 1309 | def test_method_rstrip(): |
| 1310 | for r in all_renderers: |
| 1311 | yield eq, " \t\r\ngurk", r(ur"<?print ' \t\r\ngurk \t\r\n'.rstrip()?>") |
| 1312 | yield eq, "xyzzygurk", r(ur"<?print 'xyzzygurkxyzzy'.rstrip('xyz')?>") |
| 1313 | |
| 1314 | |
| 1315 | def test_method_split(): |
| 1316 | for r in all_renderers: |
| 1317 | yield eq, "(g)(u)(r)(k)", r(ur"<?for item in ' \t\r\ng \t\r\nu \t\r\nr \t\r\nk \t\r\n'.split()?>(<?print item?>)<?end for?>") |
| 1318 | yield eq, "(g)(u \t\r\nr \t\r\nk \t\r\n)", r(ur"<?for item in ' \t\r\ng \t\r\nu \t\r\nr \t\r\nk \t\r\n'.split(None, 1)?>(<?print item?>)<?end for?>") |
| 1319 | yield eq, "()(g)(u)(r)(k)()", r(ur"<?for item in 'xxgxxuxxrxxkxx'.split('xx')?>(<?print item?>)<?end for?>") |
| 1320 | yield eq, "()(g)(uxxrxxkxx)", r(ur"<?for item in 'xxgxxuxxrxxkxx'.split('xx', 2)?>(<?print item?>)<?end for?>") |
| 1321 | |
| 1322 | |
| 1323 | def test_method_rsplit(): |
| 1324 | data = [ |
| 1325 | (' \t\r\ng \t\r\nu \t\r\nr \t\r\nk \t\r\n',), |
| 1326 | (' \t\r\ng \t\r\nu \t\r\nr \t\r\nk \t\r\n', None, 1), |
| 1327 | ('xxgxxuxxrxxkxx', 'xx'), |
| 1328 | ('xxgxxuxxrxxkxx', 'xx', 2), |
| 1329 | ] |
| 1330 | for r in all_renderers: |
| 1331 | for args in data: |
| 1332 | obj = args[0] |
| 1333 | args = args[1:] |
| 1334 | expected = "".join("({})".format(f) for f in obj.rsplit(*args)) |
| 1335 | code = ur"<?for item in {}.rsplit({})?>(<?print item?>)<?end for?>".format(ul4c._repr(obj), ", ".join(ul4c._repr(arg) for arg in args)) |
| 1336 | yield eq, expected, r(code) |
| 1337 | |
| 1338 | |
| 1339 | def test_method_replace(): |
| 1340 | for r in all_renderers: |
| 1341 | yield eq, 'goork', r(ur"<?print 'gurk'.replace('u', 'oo')?>") |
| 1342 | |
| 1343 | |
| 1344 | def test_method_render(): |
| 1345 | for r in all_renderers: |
| 1346 | t = ul4c.compile(u'(<?print data?>)') |
| 1347 | yield eq, '(GURK)', r(u"<?print t.render(data='gurk').upper()?>", t=t) |
| 1348 | yield eq, '(GURK)', r(u"<?print t.render(**{'data': 'gurk'}).upper()?>", t=t) |
| 1349 | |
| 1350 | t = ul4c.compile(u'(gurk)') |
| 1351 | yield eq, '(GURK)', r(u"<?print t.render().upper()?>", t=t) |
| 1352 | |
| 1353 | |
| 1354 | def test_method_format(): |
1433 | | assert 'Mon, 22 Feb 2010 12:34:56 GMT' == r(ur"<?print data.mimeformat()?>", data=t) |
1434 | | |
1435 | | |
1436 | | @with_all_renderers |
1437 | | def test_method_get(r): |
1438 | | assert "42" == r(u"<?print {}.get('foo', 42)?>") |
1439 | | assert "17" == r(u"<?print {'foo': 17}.get('foo', 42)?>") |
1440 | | assert "" == r(u"<?print {}.get('foo')?>") |
1441 | | assert "17" == r(u"<?print {'foo': 17}.get('foo')?>") |
1442 | | |
1443 | | |
1444 | | @with_all_renderers |
1445 | | def test_method_r_g_b_a(r): |
1446 | | assert '0x11' == r(u'<?code c = #123?><?print hex(c.r())?>') |
1447 | | assert '0x22' == r(u'<?code c = #123?><?print hex(c.g())?>') |
1448 | | assert '0x33' == r(u'<?code c = #123?><?print hex(c.b())?>') |
1449 | | assert '0xff' == r(u'<?code c = #123?><?print hex(c.a())?>') |
1450 | | |
1451 | | |
1452 | | @with_all_renderers |
1453 | | def test_method_hls(r): |
1454 | | assert '0' == r(u'<?code c = #fff?><?print int(c.hls()[0])?>') |
1455 | | assert '1' == r(u'<?code c = #fff?><?print int(c.hls()[1])?>') |
1456 | | assert '0' == r(u'<?code c = #fff?><?print int(c.hls()[2])?>') |
1457 | | |
1458 | | |
1459 | | @with_all_renderers |
1460 | | def test_method_hlsa(r): |
1461 | | assert '0' == r(u'<?code c = #fff?><?print int(c.hlsa()[0])?>') |
1462 | | assert '1' == r(u'<?code c = #fff?><?print int(c.hlsa()[1])?>') |
1463 | | assert '0' == r(u'<?code c = #fff?><?print int(c.hlsa()[2])?>') |
1464 | | assert '1' == r(u'<?code c = #fff?><?print int(c.hlsa()[3])?>') |
1465 | | |
1466 | | |
1467 | | @with_all_renderers |
1468 | | def test_method_hsv(r): |
1469 | | assert '0' == r(u'<?code c = #fff?><?print int(c.hsv()[0])?>') |
1470 | | assert '0' == r(u'<?code c = #fff?><?print int(c.hsv()[1])?>') |
1471 | | assert '1' == r(u'<?code c = #fff?><?print int(c.hsv()[2])?>') |
1472 | | |
1473 | | |
1474 | | @with_all_renderers |
1475 | | def test_method_hsva(r): |
1476 | | assert '0' == r(u'<?code c = #fff?><?print int(c.hsva()[0])?>') |
1477 | | assert '0' == r(u'<?code c = #fff?><?print int(c.hsva()[1])?>') |
1478 | | assert '1' == r(u'<?code c = #fff?><?print int(c.hsva()[2])?>') |
1479 | | assert '1' == r(u'<?code c = #fff?><?print int(c.hsva()[3])?>') |
1480 | | |
1481 | | |
1482 | | @with_all_renderers |
1483 | | def test_method_lum(r): |
1484 | | assert 'True' == r(u'<?print #fff.lum() == 1?>') |
1485 | | |
1486 | | |
1487 | | @with_all_renderers |
1488 | | def test_method_withlum(r): |
1489 | | assert '#fff' == r(u'<?print #000.withlum(1)?>') |
1490 | | |
1491 | | |
1492 | | @with_all_renderers |
1493 | | def test_method_witha(r): |
1494 | | assert '#0063a82a' == r(u'<?print repr(#0063a8.witha(42))?>') |
1495 | | |
1496 | | |
1497 | | @with_all_renderers |
1498 | | def test_method_join(r): |
1499 | | assert '1,2,3,4' == r(u'<?print ",".join("1234")?>') |
1500 | | assert '1,2,3,4' == r(u'<?print ",".join([1, 2, 3, 4])?>') |
1501 | | |
1502 | | |
1503 | | @with_all_renderers |
1504 | | def test_method_find(r): |
1505 | | assert '-1' == r(u'<?print s.find("ks")?>', s="gurkgurk") |
1506 | | assert '2' == r(u'<?print s.find("rk")?>', s="gurkgurk") |
1507 | | assert '2' == r(u'<?print s.find("rk", 2)?>', s="gurkgurk") |
1508 | | assert '2' == r(u'<?print s.find("rk", 2, 4)?>', s="gurkgurk") |
1509 | | assert '6' == r(u'<?print s.find("rk", 4, 8)?>', s="gurkgurk") |
1510 | | assert '-1' == r(u'<?print s.find("rk", 2, 3)?>', s="gurkgurk") |
1511 | | assert '-1' == r(u'<?print s.find("rk", 7)?>', s="gurkgurk") |
1512 | | |
1513 | | |
1514 | | @with_all_renderers |
1515 | | def test_method_rfind(r): |
1516 | | assert '-1' == r(u'<?print s.rfind("ks")?>', s="gurkgurk") |
1517 | | assert '6' == r(u'<?print s.rfind("rk")?>', s="gurkgurk") |
1518 | | assert '6' == r(u'<?print s.rfind("rk", 2)?>', s="gurkgurk") |
1519 | | assert '2' == r(u'<?print s.rfind("rk", 2, 4)?>', s="gurkgurk") |
1520 | | assert '6' == r(u'<?print s.rfind("rk", 4, 8)?>', s="gurkgurk") |
1521 | | assert '-1' == r(u'<?print s.rfind("rk", 2, 3)?>', s="gurkgurk") |
1522 | | assert '-1' == r(u'<?print s.rfind("rk", 7)?>', s="gurkgurk") |
1523 | | |
1524 | | |
1525 | | @with_all_renderers |
1526 | | def test_method_day(r): |
1527 | | assert '12' == r(u'<?print @2010-05-12T.day()?>') |
1528 | | assert '12' == r(u'<?print d.day()?>', d=datetime.date(2010, 5, 12)) |
1529 | | |
1530 | | |
1531 | | @with_all_renderers |
1532 | | def test_method_month(r): |
1533 | | assert '5' == r(u'<?print @2010-05-12T.month()?>') |
1534 | | assert '5' == r(u'<?print d.month()?>', d=datetime.date(2010, 5, 12)) |
1535 | | |
1536 | | |
1537 | | @with_all_renderers |
1538 | | def test_method_year(r): |
1539 | | assert '5' == r(u'<?print @2010-05-12T.month()?>') |
1540 | | assert '5' == r(u'<?print d.month()?>', d=datetime.date(2010, 5, 12)) |
1541 | | |
1542 | | |
1543 | | @with_all_renderers |
1544 | | def test_method_hour(r): |
1545 | | assert '16' == r(u'<?print @2010-05-12T16:47:56.hour()?>') |
1546 | | assert '16' == r(u'<?print d.hour()?>', d=datetime.datetime(2010, 5, 12, 16, 47, 56)) |
1547 | | |
1548 | | |
1549 | | @with_all_renderers |
1550 | | def test_method_minute(r): |
1551 | | assert '47' == r(u'<?print @2010-05-12T16:47:56.minute()?>') |
1552 | | assert '47' == r(u'<?print d.minute()?>', d=datetime.datetime(2010, 5, 12, 16, 47, 56)) |
1553 | | |
1554 | | |
1555 | | @with_all_renderers |
1556 | | def test_method_second(r): |
1557 | | assert '56' == r(u'<?print @2010-05-12T16:47:56.second()?>') |
1558 | | assert '56' == r(u'<?print d.second()?>', d=datetime.datetime(2010, 5, 12, 16, 47, 56)) |
1559 | | |
1560 | | |
1561 | | @with_all_renderers |
1562 | | def test_method_microsecond(r): |
1563 | | assert '123000' == r(u'<?print @2010-05-12T16:47:56.123000.microsecond()?>') |
1564 | | assert '123000' == r(u'<?print d.microsecond()?>', d=datetime.datetime(2010, 5, 12, 16, 47, 56, 123000)) |
1565 | | |
1566 | | |
1567 | | @with_all_renderers |
1568 | | def test_method_weekday(r): |
1569 | | assert '2' == r(u'<?print @2010-05-12T.weekday()?>') |
1570 | | assert '2' == r(u'<?print d.weekday()?>', d=datetime.date(2010, 5, 12)) |
1571 | | |
1572 | | |
1573 | | @with_all_renderers |
1574 | | def test_method_yearday(r): |
1575 | | assert '1' == r(u'<?print @2010-01-01T.yearday()?>') |
1576 | | assert '366' == r(u'<?print @2008-12-31T.yearday()?>') |
1577 | | assert '365' == r(u'<?print @2010-12-31T.yearday()?>') |
1578 | | assert '132' == r(u'<?print @2010-05-12T.yearday()?>') |
1579 | | assert '132' == r(u'<?print @2010-05-12T16:47:56.yearday()?>') |
1580 | | assert '132' == r(u'<?print d.yearday()?>', d=datetime.date(2010, 5, 12)) |
1581 | | assert '132' == r(u'<?print d.yearday()?>', d=datetime.datetime(2010, 5, 12, 16, 47, 56)) |
1582 | | |
1583 | | |
1584 | | @with_all_renderers |
1585 | | def test_render(r): |
| 1366 | for r in all_renderers: |
| 1367 | yield eq, 'Mon, 22 Feb 2010 12:34:56 GMT', r(ur"<?print data.mimeformat()?>", data=t) |
| 1368 | |
| 1369 | |
| 1370 | def test_method_get(): |
| 1371 | for r in all_renderers: |
| 1372 | yield eq, "42", r(u"<?print {}.get('foo', 42)?>") |
| 1373 | yield eq, "17", r(u"<?print {'foo': 17}.get('foo', 42)?>") |
| 1374 | yield eq, "", r(u"<?print {}.get('foo')?>") |
| 1375 | yield eq, "17", r(u"<?print {'foo': 17}.get('foo')?>") |
| 1376 | |
| 1377 | |
| 1378 | def test_method_r_g_b_a(): |
| 1379 | for r in all_renderers: |
| 1380 | yield eq, '0x11', r(u'<?code c = #123?><?print hex(c.r())?>') |
| 1381 | yield eq, '0x22', r(u'<?code c = #123?><?print hex(c.g())?>') |
| 1382 | yield eq, '0x33', r(u'<?code c = #123?><?print hex(c.b())?>') |
| 1383 | yield eq, '0xff', r(u'<?code c = #123?><?print hex(c.a())?>') |
| 1384 | |
| 1385 | |
| 1386 | def test_method_hls(): |
| 1387 | for r in all_renderers: |
| 1388 | yield eq, '0', r(u'<?code c = #fff?><?print int(c.hls()[0])?>') |
| 1389 | yield eq, '1', r(u'<?code c = #fff?><?print int(c.hls()[1])?>') |
| 1390 | yield eq, '0', r(u'<?code c = #fff?><?print int(c.hls()[2])?>') |
| 1391 | |
| 1392 | |
| 1393 | def test_method_hlsa(): |
| 1394 | for r in all_renderers: |
| 1395 | yield eq, '0', r(u'<?code c = #fff?><?print int(c.hlsa()[0])?>') |
| 1396 | yield eq, '1', r(u'<?code c = #fff?><?print int(c.hlsa()[1])?>') |
| 1397 | yield eq, '0', r(u'<?code c = #fff?><?print int(c.hlsa()[2])?>') |
| 1398 | yield eq, '1', r(u'<?code c = #fff?><?print int(c.hlsa()[3])?>') |
| 1399 | |
| 1400 | |
| 1401 | def test_method_hsv(): |
| 1402 | for r in all_renderers: |
| 1403 | yield eq, '0', r(u'<?code c = #fff?><?print int(c.hsv()[0])?>') |
| 1404 | yield eq, '0', r(u'<?code c = #fff?><?print int(c.hsv()[1])?>') |
| 1405 | yield eq, '1', r(u'<?code c = #fff?><?print int(c.hsv()[2])?>') |
| 1406 | |
| 1407 | |
| 1408 | def test_method_hsva(): |
| 1409 | for r in all_renderers: |
| 1410 | yield eq, '0', r(u'<?code c = #fff?><?print int(c.hsva()[0])?>') |
| 1411 | yield eq, '0', r(u'<?code c = #fff?><?print int(c.hsva()[1])?>') |
| 1412 | yield eq, '1', r(u'<?code c = #fff?><?print int(c.hsva()[2])?>') |
| 1413 | yield eq, '1', r(u'<?code c = #fff?><?print int(c.hsva()[3])?>') |
| 1414 | |
| 1415 | |
| 1416 | def test_method_lum(): |
| 1417 | for r in all_renderers: |
| 1418 | yield eq, 'True', r(u'<?print #fff.lum() == 1?>') |
| 1419 | |
| 1420 | |
| 1421 | def test_method_withlum(): |
| 1422 | for r in all_renderers: |
| 1423 | yield eq, '#fff', r(u'<?print #000.withlum(1)?>') |
| 1424 | |
| 1425 | |
| 1426 | def test_method_witha(): |
| 1427 | for r in all_renderers: |
| 1428 | yield eq, '#0063a82a', r(u'<?print repr(#0063a8.witha(42))?>') |
| 1429 | |
| 1430 | |
| 1431 | def test_method_join(): |
| 1432 | for r in all_renderers: |
| 1433 | yield eq, '1,2,3,4', r(u'<?print ",".join("1234")?>') |
| 1434 | yield eq, '1,2,3,4', r(u'<?print ",".join([1, 2, 3, 4])?>') |
| 1435 | |
| 1436 | |
| 1437 | def test_method_find(): |
| 1438 | for r in all_renderers: |
| 1439 | yield eq, '-1', r(u'<?print s.find("ks")?>', s="gurkgurk") |
| 1440 | yield eq, '2', r(u'<?print s.find("rk")?>', s="gurkgurk") |
| 1441 | yield eq, '2', r(u'<?print s.find("rk", 2)?>', s="gurkgurk") |
| 1442 | yield eq, '2', r(u'<?print s.find("rk", 2, 4)?>', s="gurkgurk") |
| 1443 | yield eq, '6', r(u'<?print s.find("rk", 4, 8)?>', s="gurkgurk") |
| 1444 | yield eq, '-1', r(u'<?print s.find("rk", 2, 3)?>', s="gurkgurk") |
| 1445 | yield eq, '-1', r(u'<?print s.find("rk", 7)?>', s="gurkgurk") |
| 1446 | |
| 1447 | |
| 1448 | def test_method_rfind(): |
| 1449 | for r in all_renderers: |
| 1450 | yield eq, '-1', r(u'<?print s.rfind("ks")?>', s="gurkgurk") |
| 1451 | yield eq, '6', r(u'<?print s.rfind("rk")?>', s="gurkgurk") |
| 1452 | yield eq, '6', r(u'<?print s.rfind("rk", 2)?>', s="gurkgurk") |
| 1453 | yield eq, '2', r(u'<?print s.rfind("rk", 2, 4)?>', s="gurkgurk") |
| 1454 | yield eq, '6', r(u'<?print s.rfind("rk", 4, 8)?>', s="gurkgurk") |
| 1455 | yield eq, '-1', r(u'<?print s.rfind("rk", 2, 3)?>', s="gurkgurk") |
| 1456 | yield eq, '-1', r(u'<?print s.rfind("rk", 7)?>', s="gurkgurk") |
| 1457 | |
| 1458 | |
| 1459 | def test_method_day(): |
| 1460 | for r in all_renderers: |
| 1461 | yield eq, '12', r(u'<?print @2010-05-12T.day()?>') |
| 1462 | yield eq, '12', r(u'<?print d.day()?>', d=datetime.date(2010, 5, 12)) |
| 1463 | |
| 1464 | |
| 1465 | def test_method_month(): |
| 1466 | for r in all_renderers: |
| 1467 | yield eq, '5', r(u'<?print @2010-05-12T.month()?>') |
| 1468 | yield eq, '5', r(u'<?print d.month()?>', d=datetime.date(2010, 5, 12)) |
| 1469 | |
| 1470 | |
| 1471 | def test_method_year(): |
| 1472 | for r in all_renderers: |
| 1473 | yield eq, '5', r(u'<?print @2010-05-12T.month()?>') |
| 1474 | yield eq, '5', r(u'<?print d.month()?>', d=datetime.date(2010, 5, 12)) |
| 1475 | |
| 1476 | |
| 1477 | def test_method_hour(): |
| 1478 | for r in all_renderers: |
| 1479 | yield eq, '16', r(u'<?print @2010-05-12T16:47:56.hour()?>') |
| 1480 | yield eq, '16', r(u'<?print d.hour()?>', d=datetime.datetime(2010, 5, 12, 16, 47, 56)) |
| 1481 | |
| 1482 | |
| 1483 | def test_method_minute(): |
| 1484 | for r in all_renderers: |
| 1485 | yield eq, '47', r(u'<?print @2010-05-12T16:47:56.minute()?>') |
| 1486 | yield eq, '47', r(u'<?print d.minute()?>', d=datetime.datetime(2010, 5, 12, 16, 47, 56)) |
| 1487 | |
| 1488 | |
| 1489 | def test_method_second(): |
| 1490 | for r in all_renderers: |
| 1491 | yield eq, '56', r(u'<?print @2010-05-12T16:47:56.second()?>') |
| 1492 | yield eq, '56', r(u'<?print d.second()?>', d=datetime.datetime(2010, 5, 12, 16, 47, 56)) |
| 1493 | |
| 1494 | |
| 1495 | def test_method_microsecond(): |
| 1496 | for r in all_renderers: |
| 1497 | yield eq, '123000', r(u'<?print @2010-05-12T16:47:56.123000.microsecond()?>') |
| 1498 | yield eq, '123000', r(u'<?print d.microsecond()?>', d=datetime.datetime(2010, 5, 12, 16, 47, 56, 123000)) |
| 1499 | |
| 1500 | |
| 1501 | def test_method_weekday(): |
| 1502 | for r in all_renderers: |
| 1503 | yield eq, '2', r(u'<?print @2010-05-12T.weekday()?>') |
| 1504 | yield eq, '2', r(u'<?print d.weekday()?>', d=datetime.date(2010, 5, 12)) |
| 1505 | |
| 1506 | |
| 1507 | def test_method_yearday(): |
| 1508 | for r in all_renderers: |
| 1509 | yield eq, '1', r(u'<?print @2010-01-01T.yearday()?>') |
| 1510 | yield eq, '366', r(u'<?print @2008-12-31T.yearday()?>') |
| 1511 | yield eq, '365', r(u'<?print @2010-12-31T.yearday()?>') |
| 1512 | yield eq, '132', r(u'<?print @2010-05-12T.yearday()?>') |
| 1513 | yield eq, '132', r(u'<?print @2010-05-12T16:47:56.yearday()?>') |
| 1514 | yield eq, '132', r(u'<?print d.yearday()?>', d=datetime.date(2010, 5, 12)) |
| 1515 | yield eq, '132', r(u'<?print d.yearday()?>', d=datetime.datetime(2010, 5, 12, 16, 47, 56)) |
| 1516 | |
| 1517 | |
| 1518 | def test_render(): |