root/livinglogic.python.xist/test/test_ul4.py @ 3546:cd35d2265b48

Revision 3546:cd35d2265b48, 30.5 KB (checked in by Walter Doerwald <walter@…>, 11 years ago)

Fix format() for truediv opcode. Add tests for format.

Line 
1#! /usr/bin/env/python
2# -*- coding: utf-8 -*-
3
4## Copyright 2008 by LivingLogic AG, Bayreuth/Germany
5## Copyright 2008 by Walter Dörwald
6##
7## All Rights Reserved
8##
9## See ll/__init__.py for the license
10
11
12import re, datetime, StringIO
13
14import py.test
15
16from ll import ul4c
17
18
19def check(result, source, templates={}, **variables):
20    # Check with template compiled from source
21    t1 = ul4c.compile(source)
22    s1 = unicode(t1)
23    assert result == t1.renders(templates, **variables)
24
25    # Check with template loaded again via the string interface
26    t2 = ul4c.loads(t1.dumps())
27    s2 = unicode(t2)
28    assert result == t2.renders(templates, **variables)
29
30    # Check with template loaded again via the stream interface
31    stream = StringIO.StringIO()
32    t1.dump(stream)
33    stream.seek(0)
34    t3 = ul4c.load(stream)
35    s3 = unicode(t3)
36    assert result == t3.renders(templates, **variables)
37    assert s1 == s2 == s3
38
39
40def checkle(result, source, templates={}, **variables):
41    # Check with template compiled from source
42    t1 = ul4c.compile(source)
43    s1 = unicode(t1)
44    assert result <= t1.renders(templates, **variables)
45
46    # Check with template loaded again via the string interface
47    t2 = ul4c.loads(t1.dumps())
48    s2 = unicode(t2)
49    assert result <= t2.renders(templates, **variables)
50
51    # Check with template loaded again via the stream interface
52    stream = StringIO.StringIO()
53    t1.dump(stream)
54    stream.seek(0)
55    t3 = ul4c.load(stream)
56    s3 = unicode(t3)
57    assert result <= t3.renders(templates, **variables)
58    assert s1 == s2 == s3
59
60
61def checkcompileerror(msg, source):
62    try:
63        ul4c.compile(source)
64    except Exception, exc:
65        assert re.search(msg, str(exc)) is not None
66    else:
67        py.test.fail("Didn't raise exception")
68
69
70def checkrunerror(msg, source, templates={}, **variables):
71    # Check with template compiled from source
72    t1 = ul4c.compile(source)
73    s1 = unicode(t1)
74    try:
75        t1.renders(templates, **variables)
76    except Exception, exc:
77        assert re.search(msg, "%s.%s: %s" % (exc.__class__.__module__, exc.__class__.__name__, exc)) is not None
78    else:
79        py.test.fail("Didn't raise exception")
80
81    # Check with template loaded again via the string interface
82    t2 = ul4c.loads(t1.dumps())
83    s2 = unicode(t2)
84    try:
85        t2.renders(templates, **variables)
86    except Exception, exc:
87        assert re.search(msg, "%s.%s: %s" % (exc.__class__.__module__, exc.__class__.__name__, exc)) is not None
88    else:
89        py.test.fail("Didn't raise exception")
90
91    # Check with template loaded again via the stream interface
92    stream = StringIO.StringIO()
93    t1.dump(stream)
94    stream.seek(0)
95    t3 = ul4c.load(stream)
96    s3 = unicode(t3)
97    try:
98        t3.renders(templates, **variables)
99    except Exception, exc:
100        assert re.search(msg, "%s.%s: %s" % (exc.__class__.__module__, exc.__class__.__name__, exc)) is not None
101    else:
102        py.test.fail("Didn't raise exception")
103    assert s1 == s2 == s3
104
105
106def test_text():
107    check(u'gurk', u'gurk')
108    check(u'g\xfcrk', u'g\xfcrk')
109
110
111def test_none():
112    check('', u'<?print None?>')
113
114    check('no', u'<?if None?>yes<?else?>no<?end if?>')
115
116
117def test_false():
118    check('False', u'<?print False?>')
119
120    check('no', u'<?if False?>yes<?else?>no<?end if?>')
121
122
123def test_true():
124    check('True', u'<?print True?>')
125
126    check('yes', u'<?if True?>yes<?else?>no<?end if?>')
127
128
129def test_int():
130    check('0', u'<?print 0?>')
131    check('42', u'<?print 42?>')
132    check('-42', u'<?print -42?>')
133    check('255', u'<?print 0xff?>')
134    check('255', u'<?print 0Xff?>')
135    check('-255', u'<?print -0xff?>')
136    check('-255', u'<?print -0Xff?>')
137    check('63', u'<?print 0o77?>')
138    check('63', u'<?print 0O77?>')
139    check('-63', u'<?print -0o77?>')
140    check('-63', u'<?print -0O77?>')
141    check('7', u'<?print 0b111?>')
142    check('7', u'<?print 0B111?>')
143    check('-7', u'<?print -0b111?>')
144    check('-7', u'<?print -0B111?>')
145
146    check('no', u'<?if 0?>yes<?else?>no<?end if?>')
147    check('yes', u'<?if 1?>yes<?else?>no<?end if?>')
148    check('yes', u'<?if -1?>yes<?else?>no<?end if?>')
149
150
151def test_float():
152    check('0.0', u'<?print 0.?>')
153    check('42.0', u'<?print 42.?>')
154    check('-42.0', u'<?print -42.?>')
155    check('1e+42', u'<?print 1E42?>')
156    check('1e+42', u'<?print 1e42?>')
157    check('-1e+42', u'<?print -1E42?>')
158    check('-1e+42', u'<?print -1e42?>')
159
160    check('no', u'<?if 0.?>yes<?else?>no<?end if?>')
161    check('yes', u'<?if 1.?>yes<?else?>no<?end if?>')
162    check('yes', u'<?if -1.?>yes<?else?>no<?end if?>')
163
164
165def test_string():
166    check('foo', u'<?print "foo"?>')
167    check('\n', u'<?print "\\n"?>')
168    check('\r', u'<?print "\\r"?>')
169    check('\t', u'<?print "\\t"?>')
170    check('\f', u'<?print "\\f"?>')
171    check('\b', u'<?print "\\b"?>')
172    check('\a', u'<?print "\\a"?>')
173    check('\x1b', u'<?print "\\e"?>')
174    check('"', u'<?print "\\""?>')
175    check("'", u'<?print "\\\'"?>')
176    check(u'\u20ac', u'<?print "\u20ac"?>')
177    check(u'\xff', u'<?print "\\xff"?>')
178    check(u'\u20ac', u'''<?print "\\u20ac"?>''')
179    checkcompileerror("Unterminated string", u'<?print "?>')
180    check("\\xxx", u'<?print "\\xxx"?>')
181    check("a\nb", u'<?print "a\nb"?>')
182
183    check('no', u'<?if ""?>yes<?else?>no<?end if?>')
184    check('yes', u'<?if "foo"?>yes<?else?>no<?end if?>')
185
186
187def test_date():
188    check('2000-02-29T00:00:00', u'<?print 2000-02-29T.isoformat()?>')
189    check('2000-02-29T12:34:00', u'<?print 2000-02-29T12:34.isoformat()?>')
190    check('2000-02-29T12:34:56', u'<?print 2000-02-29T12:34:56.isoformat()?>')
191    check('2000-02-29T12:34:56.987654', u'<?print 2000-02-29T12:34:56.987654.isoformat()?>')
192    check('yes', u'<?if 2000-02-29T12:34:56.987654?>yes<?else?>no<?end if?>')
193
194
195def test_list():
196    check('', u'<?for item in []?><?print item?>;<?end for?>')
197    check('1;', u'<?for item in [1]?><?print item?>;<?end for?>')
198    check('1;', u'<?for item in [1,]?><?print item?>;<?end for?>')
199    check('1;2;', u'<?for item in [1, 2]?><?print item?>;<?end for?>')
200    check('1;2;', u'<?for item in [1, 2,]?><?print item?>;<?end for?>')
201    check('no', u'<?if []?>yes<?else?>no<?end if?>')
202    check('yes', u'<?if [1]?>yes<?else?>no<?end if?>')
203
204
205def test_dict():
206    check('', u'<?for (key, value) in {}.items()?><?print key?>:<?print value?>\n<?end for?>')
207    check('1:2\n', u'<?for (key, value) in {1:2}.items()?><?print key?>:<?print value?>\n<?end for?>')
208    check('1:2\n', u'<?for (key, value) in {1:2,}.items()?><?print key?>:<?print value?>\n<?end for?>')
209    # With duplicate keys, later ones simply overwrite earlier ones
210    check('1:3\n', u'<?for (key, value) in {1:2, 1: 3}.items()?><?print key?>:<?print value?>\n<?end for?>')
211    check('no', u'<?if {}?>yes<?else?>no<?end if?>')
212    check('yes', u'<?if {1:2}?>yes<?else?>no<?end if?>')
213
214
215def test_code_storevar():
216    check('42', u'<?code x = 42?><?print x?>')
217    check('xyzzy', u'<?code x = "xyzzy"?><?print x?>')
218
219
220def test_code_addvar():
221    check('40', u'<?code x = 17?><?code x += 23?><?print x?>')
222    check('xyzzy', u'<?code x = "xyz"?><?code x += "zy"?><?print x?>')
223
224
225def test_code_subvar():
226    check('-6', u'<?code x = 17?><?code x -= 23?><?print x?>')
227
228
229def test_code_mulvar():
230    check('391', u'<?code x = 17?><?code x *= 23?><?print x?>')
231    check(17*'xyzzy', u'<?code x = 17?><?code x *= "xyzzy"?><?print x?>')
232    check(17*'xyzzy', u'<?code x = "xyzzy"?><?code x *= 17?><?print x?>')
233
234
235def test_code_floordivvar():
236    check('2', u'<?code x = 5?><?code x //= 2?><?print x?>')
237    check('-3', u'<?code x = -5?><?code x //= 2?><?print x?>')
238
239
240def test_code_truedivvar():
241    check('2.5', u'<?code x = 5?><?code x /= 2?><?print x?>')
242    check('-2.5', u'<?code x = -5?><?code x /= 2?><?print x?>')
243
244
245def test_code_modvar():
246    check('4', u'<?code x = 1729?><?code x %= 23?><?print x?>')
247
248
249def test_code_delvar():
250    checkrunerror('KeyError', u'<?code x = 1729?><?code del x?><?print x?>')
251
252
253def test_for_string():
254    check('', u'<?for c in data?>(<?print c?>)<?end for?>', data="")
255    check('(g)(u)(r)(k)', u'<?for c in data?>(<?print c?>)<?end for?>', data="gurk")
256
257
258def test_for_list():
259    check('', u'<?for c in data?>(<?print c?>)<?end for?>', data="")
260    check('(g)(u)(r)(k)', u'<?for c in data?>(<?print c?>)<?end for?>', data=["g", "u", "r", "k"])
261
262
263def test_for_dict():
264    check('', u'<?for c in data?>(<?print c?>)<?end for?>', data={})
265    check('(a)(b)(c)', u'<?for c in sorted(data)?>(<?print c?>)<?end for?>', data=dict(a=1, b=2, c=3))
266
267
268def test_for_nested():
269    check('[(1)(2)][(3)(4)]', u'<?for list in data?>[<?for n in list?>(<?print n?>)<?end for?>]<?end for?>', data=[[1, 2], [3, 4]])
270
271
272def test_break():
273    check('1, 2, ', u'<?for i in [1,2,3]?><?print i?>, <?if i==2?><?break?><?end if?><?end for?>')
274
275
276def test_break_nested():
277    check('1, 1, 2, 1, 2, 3, ', 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?>')
278
279
280def test_continue():
281    check('1, 3, ', u'<?for i in [1,2,3]?><?if i==2?><?continue?><?end if?><?print i?>, <?end for?>')
282
283
284def test_continue_nested():
285    check('1, 3, \n1, 3, \n', 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?>')
286
287
288def test_if():
289    check('42', u'<?if data?><?print data?><?end if?>', data=42)
290
291
292def test_else():
293    check('42', u'<?if data?><?print data?><?else?>no<?end if?>', data=42)
294    check('no', u'<?if data?><?print data?><?else?>no<?end if?>', data=0)
295
296
297def test_block_errors():
298    checkcompileerror("in u?.<.for x in data.>..*block unclosed", u'<?for x in data?>')
299    checkcompileerror("endif doesn't match any if", u'<?for x in data?><?end if?>')
300    checkcompileerror("not in any block", u'<?end?>')
301    checkcompileerror("not in any block", u'<?end for?>')
302    checkcompileerror("not in any block", u'<?end if?>')
303    checkcompileerror("else doesn't match any if", u'<?else?>')
304    checkcompileerror("in u?.<.if data.>..*block unclosed", u'<?if data?>')
305    checkcompileerror("in u?.<.if data.>..*block unclosed", u'<?if data?><?else?>')
306    checkcompileerror("duplicate else", u'<?if data?><?else?><?else?>')
307    checkcompileerror("else already seen in elif", u'<?if data?><?else?><?elif data?>')
308    checkcompileerror("else already seen in elif", u'<?if data?><?elif data?><?elif data?><?else?><?elif data?>')
309
310
311def test_empty():
312    checkcompileerror("expression required", u'<?print?>')
313    checkcompileerror("expression required", u'<?if?>')
314    checkcompileerror("expression required", u'<<?if x?><?elif?><?end if?>')
315    checkcompileerror("loop expression required", u'<?for?>')
316    checkcompileerror("statement required", u'<?code?>')
317    checkcompileerror("render statement required", u'<?render?>')
318
319
320def test_add():
321    check('42', u'<?print 21+21?>')
322    check('42', u'<?code x=21?><?code y=21?><?print x+y?>')
323    check('foobar', u'<?code x="foo"?><?code y="bar"?><?print x+y?>')
324    check('(f)(o)(o)(b)(a)(r)', u'<?for i in data.foo+data.bar?>(<?print i?>)<?end for?>', data=dict(foo="foo", bar="bar"))
325
326
327def test_sub():
328    check('0', u'<?print 21-21?>')
329    check('0', u'<?code x=21?><?code y=21?><?print x-y?>')
330
331
332
333def test_mul():
334    check(str(17*23), u'<?print 17*23?>')
335    check(str(17*23), u'<?code x=17?><?code y=23?><?print x*y?>')
336    check(17*"foo", u'<?print 17*"foo"?>')
337    check(17*"foo", u'<?code x=17?><?code y="foo"?><?print x*y?>')
338    check("foo"*17, u'<?code x="foo"?><?code y=17?><?print x*y?>')
339    check("foo"*17, u'<?print "foo"*17?>')
340    check("(foo)(bar)(foo)(bar)(foo)(bar)", u'<?for i in 3*data?>(<?print i?>)<?end for?>', data=["foo", "bar"])
341
342
343def test_truediv():
344    check("0.5", u'<?print 1/2?>')
345    check("0.5", u'<?code x=1?><?code y=2?><?print x/y?>')
346
347
348def test_floordiv():
349    check("0", u'<?print 1//2?>')
350    check("0", u'<?code x=1?><?code y=2?><?print x//y?>')
351
352
353def test_mod():
354    check(str(42%17), u'<?print 42%17?>')
355    check(str(42%17), u'<?code x=42?><?code y=17?><?print x%y?>')
356
357
358def test_eq():
359    check("False", u'<?print 17==23?>')
360    check("True", u'<?print 17==17?>')
361    check("False", u'<?print x==23?>', x=17)
362    check("True", u'<?print x==23?>', x=23)
363
364
365def test_ne():
366    check("True", u'<?print 17!=23?>')
367    check("False", u'<?print 17!=17?>')
368    check("True", u'<?print x!=23?>', x=17)
369    check("False", u'<?print x!=23?>', x=23)
370
371
372def test_lt():
373    check("True", u'<?print 1<2?>')
374    check("False", u'<?print 2<2?>')
375    check("False", u'<?print 3<2?>')
376    check("True", u'<?print x<2?>', x=1)
377    check("False", u'<?print x<2?>', x=2)
378    check("False", u'<?print x<2?>', x=3)
379
380
381def test_le():
382    check("True", u'<?print 1<=2?>')
383    check("True", u'<?print 2<=2?>')
384    check("False", u'<?print 3<=2?>')
385    check("True", u'<?print x<=2?>', x=1)
386    check("True", u'<?print x<=2?>', x=2)
387    check("False", u'<?print x<=2?>', x=3)
388
389
390def test_gt():
391    check("False", u'<?print 1>2?>')
392    check("False", u'<?print 2>2?>')
393    check("True", u'<?print 3>2?>')
394    check("False", u'<?print x>2?>', x=1)
395    check("False", u'<?print x>2?>', x=2)
396    check("True", u'<?print x>2?>', x=3)
397
398
399def test_and():
400    check("False", u'<?print x and y?>', x=False, y=False)
401    check("False", u'<?print x and y?>', x=False, y=True)
402    check("0", u'<?print x and y?>', x=0, y=True)
403
404
405def test_or():
406    check("False", u'<?print x or y?>', x=False, y=False)
407    check("True", u'<?print x or y?>', x=False, y=True)
408    check("42", u'<?print x or y?>', x=42, y=True)
409
410
411def test_not():
412    check("True", u'<?print not x?>', x=False)
413    check("False", u'<?print not x?>', x=42)
414
415
416def test_ge():
417    check("False", u'<?print 1>=2?>')
418    check("True", u'<?print 2>=2?>')
419    check("True", u'<?print 3>=2?>')
420    check("False", u'<?print x>=2?>', x=1)
421    check("True", u'<?print x>=2?>', x=2)
422    check("True", u'<?print x>=2?>', x=3)
423
424
425def test_getitem():
426    check("u", u"<?print 'gurk'[1]?>")
427    check("u", u"<?print x[1]?>", x="gurk")
428    check("u", u"<?print 'gurk'[-3]?>")
429    check("u", u"<?print x[-3]?>", x="gurk")
430    checkcompileerror("IndexError", u"<?print 'gurk'[4]?>")
431    checkrunerror("IndexError", u"<?print x[4]?>", x="gurk")
432    checkcompileerror("IndexError", u"<?print 'gurk'[-5]?>")
433    checkrunerror("IndexError", u"<?print x[-5]?>", x="gurk")
434
435
436def test_getslice12():
437    check("ur", u"<?print 'gurk'[1:3]?>")
438    check("ur", u"<?print x[1:3]?>", x="gurk")
439    check("ur", u"<?print 'gurk'[-3:-1]?>")
440    check("ur", u"<?print x[-3:-1]?>", x="gurk")
441    check("", u"<?print 'gurk'[4:10]?>")
442    check("", u"<?print x[4:10]?>", x="gurk")
443    check("", u"<?print 'gurk'[-10:-5]?>")
444    check("", u"<?print x[-10:-5]?>", x="gurk")
445
446
447def test_getslice1():
448    check("urk", u"<?print 'gurk'[1:]?>")
449    check("urk", u"<?print x[1:]?>", x="gurk")
450    check("urk", u"<?print 'gurk'[-3:]?>")
451    check("urk", u"<?print x[-3:]?>", x="gurk")
452    check("", u"<?print 'gurk'[4:]?>")
453    check("", u"<?print x[4:]?>", x="gurk")
454    check("gurk", u"<?print 'gurk'[-10:]?>")
455    check("gurk", u"<?print x[-10:]?>", x="gurk")
456
457
458def test_getslice2():
459    check("gur", u"<?print 'gurk'[:3]?>")
460    check("gur", u"<?print x[:3]?>", x="gurk")
461    check("gur", u"<?print 'gurk'[:-1]?>")
462    check("gur", u"<?print x[:-1]?>", x="gurk")
463    check("gurk", u"<?print 'gurk'[:10]?>")
464    check("gurk", u"<?print x[:10]?>", x="gurk")
465    check("", u"<?print 'gurk'[:-5]?>")
466    check("", u"<?print x[:-5]?>", x="gurk")
467
468
469def test_nested():
470    sc = u"4"
471    sv = u"x"
472    n = 4
473    for i in xrange(8): # when using 10 compiling the variable will run out of registers
474        sc = u"(%s)+(%s)" % (sc, sc)
475        sv = u"(%s)+(%s)" % (sv, sv)
476        n = n+n
477    check(str(n), u'<?print %s?>' % sc)
478    check(str(n), u'<?code x=4?><?print %s?>' % sv)
479
480
481def test_precedence():
482    check("14", u'<?print 2+3*4?>')
483    check("20", u'<?print (2+3)*4?>')
484    check("10", u'<?print -2+-3*-4?>')
485    check("14", u'<?print --2+--3*--4?>')
486    check("14", u'<?print (-(-2))+(-((-3)*-(-4)))?>')
487    check("42", u'<?print 2*data.value?>', data=dict(value=21))
488    check("42", u'<?print data.value[0]?>', data=dict(value=[42]))
489    check("42", u'<?print data[0].value?>', data=[dict(value=42)])
490    check("42", u'<?print data[0][0][0]?>', data=[[[42]]])
491    check("42", u'<?print data.value.value[0]?>', data=dict(value=dict(value=[42])))
492    check("42", u'<?print data.value.value[0].value.value[0]?>', data=dict(value=dict(value=[dict(value=dict(value=[42]))])))
493
494
495def test_bracket():
496    sc = u"4"
497    sv = u"x"
498    for i in xrange(10):
499        sc = u"(%s)" % sc
500        sv = u"(%s)" % sv
501
502    check("4", u'<?print %s?>' % sc)
503    check("4", u'<?code x=4?><?print %s?>' % sv)
504
505
506def test_function_now():
507    checkrunerror("function u?'now' unknown", u"<?print now(1)?>")
508    checkrunerror("function u?'now' unknown", u"<?print now(1, 2)?>")
509    now = unicode(datetime.datetime.now())
510    checkle(now, u"<?print now()?>")
511
512
513def test_function_xmlescape():
514    checkrunerror("function u?'xmlescape' unknown", u"<?print xmlescape()?>")
515    checkrunerror("function u?'xmlescape' unknown", u"<?print xmlescape(1, 2)?>")
516    check("&lt;&gt;&amp;&#39;&quot;gurk", u"<?print xmlescape(data)?>", data='<>&\'"gurk')
517
518
519def test_function_csvescape():
520    checkrunerror("function u?'csvescape' unknown", u"<?print csvescape()?>")
521    checkrunerror("function u?'csvescape' unknown", u"<?print csvescape(1, 2)?>")
522    check("", u"<?print csvescape(data)?>", data=None)
523    check("False", u"<?print csvescape(data)?>", data=False)
524    check("True", u"<?print csvescape(data)?>", data=True)
525    check("42", u"<?print csvescape(data)?>", data=42)
526    # no check for float
527    check("abc", u"<?print csvescape(data)?>", data="abc")
528    check('"a,b,c"', u"<?print csvescape(data)?>", data="a,b,c")
529    check('"a""b""c"', u"<?print csvescape(data)?>", data='a"b"c')
530    check('"a\nb\nc"', u"<?print csvescape(data)?>", data="a\nb\nc")
531
532
533def test_function_str():
534    checkrunerror("function u?'str' unknown", u"<?print str()?>")
535    checkrunerror("function u?'str' unknown", u"<?print str(1, 2)?>")
536    check("", u"<?print str(data)?>", data=None)
537    check("True", u"<?print str(data)?>", data=True)
538    check("False", u"<?print str(data)?>", data=False)
539    check("42", u"<?print str(data)?>", data=42)
540    check("4.2", u"<?print str(data)?>", data=4.2)
541    check("foo", u"<?print str(data)?>", data="foo")
542
543
544def test_function_int():
545    checkrunerror("function u?'int' unknown", u"<?print int()?>")
546    checkrunerror("function u?'int' unknown", u"<?print int(1, 2)?>")
547    checkrunerror("int\\(\\) argument must be a string or a number, not 'NoneType'", u"<?print int(data)?>", data=None)
548    check("1", u"<?print int(data)?>", data=True)
549    check("0", u"<?print int(data)?>", data=False)
550    check("42", u"<?print int(data)?>", data=42)
551    check("4", u"<?print int(data)?>", data=4.2)
552    check("42", u"<?print int(data)?>", data="42")
553    checkrunerror("invalid literal for int\\(\\) with base 10: 'foo'", u"<?print int(data)?>", data="foo")
554
555
556def test_function_len():
557    checkrunerror("function u?'len' unknown", u"<?print len()?>")
558    checkrunerror("function u?'len' unknown", u"<?print len(1, 2)?>")
559    checkrunerror("object of type 'NoneType' has no len", u"<?print len(data)?>", data=None)
560    checkrunerror("object of type 'bool' has no len", u"<?print len(data)?>", data=True)
561    checkrunerror("object of type 'bool' has no len", u"<?print len(data)?>", data=False)
562    checkrunerror("object of type 'int' has no len", u"<?print len(data)?>", data=42)
563    checkrunerror("object of type 'float' has no len", u"<?print len(data)?>", data=4.2)
564    check("42", u"<?print len(data)?>", data=42*"?")
565    check("42", u"<?print len(data)?>", data=42*[None])
566    check("42", u"<?print len(data)?>", data=dict.fromkeys(xrange(42)))
567
568
569def test_function_enumerate():
570    checkrunerror("function u?'enumerate' unknown", u"<?print enumerate()?>")
571    checkrunerror("function u?'enumerate' unknown", u"<?print enumerate(1, 2)?>")
572    code = u"<?for (i, value) in enumerate(data)?><?print i?>:<?print value?>\n<?end for?>"
573    checkrunerror("'NoneType' object is not iterable", code, data=None)
574    checkrunerror("'bool' object is not iterable", code, data=True)
575    checkrunerror("'bool' object is not iterable", code, data=False)
576    checkrunerror("'int' object is not iterable", code, data=42)
577    checkrunerror("'float' object is not iterable", code, data=4.2)
578    check("0:f\n1:o\n2:o\n", code, data="foo")
579    check("0:foo\n1:bar\n", code, data=["foo", "bar"])
580    check("0:foo\n", code, data=dict(foo=True))
581
582
583def test_function_isnone():
584    checkrunerror("function u?'isnone' unknown", u"<?print isnone()?>")
585    checkrunerror("function u?'isnone' unknown", u"<?print isnone(1, 2)?>")
586    code = u"<?print isnone(data)?>"
587    check("True", code, data=None)
588    check("False", code, data=True)
589    check("False", code, data=False)
590    check("False", code, data=42)
591    check("False", code, data=4.2)
592    check("False", code, data="foo")
593    check("False", code, data=datetime.datetime.now())
594    check("False", code, data=())
595    check("False", code, data=[])
596    check("False", code, data={})
597
598
599def test_function_isbool():
600    checkrunerror("function u?'isbool' unknown", u"<?print isbool()?>")
601    checkrunerror("function u?'isbool' unknown", u"<?print isbool(1, 2)?>")
602    code = u"<?print isbool(data)?>"
603    check("False", code, data=None)
604    check("True", code, data=True)
605    check("True", code, data=False)
606    check("False", code, data=42)
607    check("False", code, data=4.2)
608    check("False", code, data="foo")
609    check("False", code, data=datetime.datetime.now())
610    check("False", code, data=())
611    check("False", code, data=[])
612    check("False", code, data={})
613
614
615def test_function_isint():
616    checkrunerror("function u?'isint' unknown", u"<?print isint()?>")
617    checkrunerror("function u?'isint' unknown", u"<?print isint(1, 2)?>")
618    code = u"<?print isint(data)?>"
619    check("False", code, data=None)
620    check("False", code, data=True)
621    check("False", code, data=False)
622    check("True", code, data=42)
623    check("False", code, data=4.2)
624    check("False", code, data="foo")
625    check("False", code, data=datetime.datetime.now())
626    check("False", code, data=())
627    check("False", code, data=[])
628    check("False", code, data={})
629
630
631def test_function_isfloat():
632    checkrunerror("function u?'isfloat' unknown", u"<?print isfloat()?>")
633    checkrunerror("function u?'isfloat' unknown", u"<?print isfloat(1, 2)?>")
634    code = u"<?print isfloat(data)?>"
635    check("False", code, data=None)
636    check("False", code, data=True)
637    check("False", code, data=False)
638    check("False", code, data=42)
639    check("True", code, data=4.2)
640    check("False", code, data="foo")
641    check("False", code, data=datetime.datetime.now())
642    check("False", code, data=())
643    check("False", code, data=[])
644    check("False", code, data={})
645
646
647def test_function_isstr():
648    checkrunerror("function u?'isstr' unknown", u"<?print isstr()?>")
649    checkrunerror("function u?'isstr' unknown", u"<?print isstr(1, 2)?>")
650    code = u"<?print isstr(data)?>"
651    check("False", code, data=None)
652    check("False", code, data=True)
653    check("False", code, data=False)
654    check("False", code, data=42)
655    check("False", code, data=4.2)
656    check("True", code, data="foo")
657    check("False", code, data=datetime.datetime.now())
658    check("False", code, data=())
659    check("False", code, data=[])
660    check("False", code, data={})
661
662
663def test_function_isdate():
664    checkrunerror("function u?'isdate' unknown", u"<?print isdate()?>")
665    checkrunerror("function u?'isdate' unknown", u"<?print isdate(1, 2)?>")
666    code = u"<?print isdate(data)?>"
667    check("False", code, data=None)
668    check("False", code, data=True)
669    check("False", code, data=False)
670    check("False", code, data=42)
671    check("False", code, data=4.2)
672    check("False", code, data="foo")
673    check("True", code, data=datetime.datetime.now())
674    check("False", code, data=())
675    check("False", code, data=[])
676    check("False", code, data={})
677
678
679def test_function_islist():
680    checkrunerror("function u?'islist' unknown", u"<?print islist()?>")
681    checkrunerror("function u?'islist' unknown", u"<?print islist(1, 2)?>")
682    code = u"<?print islist(data)?>"
683    check("False", code, data=None)
684    check("False", code, data=True)
685    check("False", code, data=False)
686    check("False", code, data=42)
687    check("False", code, data=4.2)
688    check("False", code, data="foo")
689    check("False", code, data=datetime.datetime.now())
690    check("True", code, data=())
691    check("True", code, data=[])
692    check("False", code, data={})
693
694
695def test_function_isdict():
696    checkrunerror("function u?'isdict' unknown", u"<?print isdict()?>")
697    checkrunerror("function u?'isdict' unknown", u"<?print isdict(1, 2)?>")
698    code = u"<?print isdict(data)?>"
699    check("False", code, data=None)
700    check("False", code, data=True)
701    check("False", code, data=False)
702    check("False", code, data=42)
703    check("False", code, data=4.2)
704    check("False", code, data="foo")
705    check("False", code, data=datetime.datetime.now())
706    check("False", code, data=())
707    check("False", code, data=[])
708    check("True", code, data={})
709
710
711def test_function_get():
712    checkrunerror("function u?'get' unknown", u"<?print get()?>")
713    check("", u"<?print get('x')?>")
714    check("42", u"<?print get('x')?>", x=42)
715    check("17", u"<?print get('x', 17)?>")
716    check("42", u"<?print get('x', 17)?>", x=42)
717
718
719def test_function_repr():
720    checkrunerror("function u?'repr' unknown", u"<?print repr()?>")
721    checkrunerror("function u?'repr' unknown", u"<?print repr(1, 2)?>")
722    code = u"<?print repr(data)?>"
723    check("None", code, data=None)
724    check("True", code, data=True)
725    check("False", code, data=False)
726    check("42", code, data=42)
727    # no test for float
728    check("'foo'", code, data="foo")
729    # no test for tuples, lists and dicts
730
731
732def test_function_chr():
733    checkrunerror("function u?'chr' unknown", u"<?print chr()?>")
734    checkrunerror("function u?'chr' unknown", u"<?print chr(1, 2)?>")
735    code = u"<?print chr(data)?>"
736    check("\x00", code, data=0)
737    check("a", code, data=ord("a"))
738    check(u"\u20ac", code, data=0x20ac)
739
740
741def test_function_ord():
742    checkrunerror("function u?'ord' unknown", u"<?print ord()?>")
743    checkrunerror("function u?'ord' unknown", u"<?print ord(1, 2)?>")
744    code = u"<?print ord(data)?>"
745    check("0", code, data="\x00")
746    check(str(ord("a")), code, data="a")
747    check(str(0x20ac), code, data=u"\u20ac")
748
749
750def test_function_hex():
751    checkrunerror("function u?'hex' unknown", u"<?print hex()?>")
752    checkrunerror("function u?'hex' unknown", u"<?print hex(1, 2)?>")
753    code = u"<?print hex(data)?>"
754    check("0x0", code, data=0)
755    check("0xff", code, data=0xff)
756    check("0xffff", code, data=0xffff)
757    check("-0xffff", code, data=-0xffff)
758
759
760def test_function_oct():
761    checkrunerror("function u?'oct' unknown", u"<?print oct()?>")
762    checkrunerror("function u?'oct' unknown", u"<?print oct(1, 2)?>")
763    code = u"<?print oct(data)?>"
764    check("0o0", code, data=0)
765    check("0o77", code, data=077)
766    check("0o7777", code, data=07777)
767    check("-0o7777", code, data=-07777)
768
769
770def test_function_bin():
771    checkrunerror("function u?'bin' unknown", u"<?print bin()?>")
772    checkrunerror("function u?'bin' unknown", u"<?print bin(1, 2)?>")
773    code = u"<?print bin(data)?>"
774    check("0b0", code, data=0)
775    check("0b11", code, data=3)
776    check("-0b1111", code, data=-15)
777
778
779def test_function_sorted():
780    checkrunerror("function u?'sorted' unknown", u"<?print sorted()?>")
781    checkrunerror("function u?'sorted' unknown", u"<?print sorted(1, 2)?>")
782    code = u"<?for i in sorted(data)?><?print i?><?end for?>"
783    check("gkru", code, data="gurk")
784    check("24679", code, data="92746")
785    check("012", code, data={0: "zero", 1: "one", 2: "two"})
786
787
788def test_function_range():
789    checkrunerror("function u?'sorted' unknown", u"<?print sorted()?>")
790    code = u"<?for i in range(data)?><?print i?><?end for?>"
791    check("", code, data=-10)
792    check("", code, data=0)
793    check("0", code, data=1)
794    check("01234", code, data=5)
795    code = u"<?for i in range(data[0], data[1])?><?print i?><?end for?>"
796    check("", code, data=[0, -10])
797    check("", code, data=[0, 0])
798    check("01234", code, data=[0, 5])
799    check("-5-4-3-2-101234", code, data=[-5, 5])
800    code = u"<?for i in range(data[0], data[1], data[2])?><?print i?><?end for?>"
801    check("", code, data=[0, -10, 1])
802    check("", code, data=[0, 0, 1])
803    check("02468", code, data=[0, 10, 2])
804    check("", code, data=[0, 10, -2])
805    check("108642", code, data=[10, 0, -2])
806    check("", code, data=[10, 0, 2])
807
808
809def test_function_zip():
810    checkrunerror("function u?'zip' unknown", u"<?print zip()?>")
811    checkrunerror("function u?'zip' unknown", u"<?print zip(1)?>")
812    code = u"<?for (ix, iy) in zip(x, y)?><?print ix?>-<?print iy?>;<?end for?>"
813    check("", code, x=[], y=[])
814    check("1-3;2-4;", code, x=[1, 2], y=[3, 4])
815    check("1-4;2-5;", code, x=[1, 2, 3], y=[4, 5])
816    code = u"<?for (ix, iy, iz) in zip(x, y, z)?><?print ix?>-<?print iy?>+<?print iz?>;<?end for?>"
817    check("", code, x=[], y=[], z=[])
818    check("1-3+5;2-4+6;", code, x=[1, 2], y=[3, 4], z=[5, 6])
819    check("1-4+6;", code, x=[1, 2, 3], y=[4, 5], z=[6])
820
821
822def test_method_upper():
823    check("GURK", u"<?print 'gurk'.upper()?>")
824
825
826def test_method_lower():
827    check("gurk", u"<?print 'GURK'.lower()?>")
828
829
830def test_method_capitalize():
831    check("Gurk", u"<?print 'gURK'.capitalize()?>")
832
833
834def test_method_startswith():
835    check("True", u"<?print 'gurkhurz'.startswith('gurk')?>")
836    check("False", u"<?print 'gurkhurz'.startswith('hurz')?>")
837
838
839def test_method_endswith():
840    check("True", u"<?print 'gurkhurz'.endswith('hurz')?>")
841    check("False", u"<?print 'gurkhurz'.endswith('gurk')?>")
842
843
844def test_method_strip():
845    check("gurk", r"<?print ' \t\r\ngurk \t\r\n'.strip()?>")
846    check("gurk", r"<?print 'xyzzygurkxyzzy'.strip('xyz')?>")
847
848
849def test_method_lstrip():
850    check("gurk \t\r\n", ur"<?print ' \t\r\ngurk \t\r\n'.lstrip()?>")
851    check("gurkxyzzy", ur"<?print 'xyzzygurkxyzzy'.lstrip('xyz')?>")
852
853
854def test_method_rstrip():
855    check(" \t\r\ngurk", ur"<?print ' \t\r\ngurk \t\r\n'.rstrip()?>")
856    check("xyzzygurk", ur"<?print 'xyzzygurkxyzzy'.rstrip('xyz')?>")
857
858
859def test_method_split():
860    check("(g)(u)(r)(k)", ur"<?for item in ' \t\r\ng \t\r\nu \t\r\nr \t\r\nk \t\r\n'.split()?>(<?print item?>)<?end for?>")
861    check("(g)(u \t\r\nr \t\r\nk \t\r\n)", 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?>")
862    check("()(g)(u)(r)(k)()", ur"<?for item in 'xxgxxuxxrxxkxx'.split('xx')?>(<?print item?>)<?end for?>")
863    check("()(g)(uxxrxxkxx)", ur"<?for item in 'xxgxxuxxrxxkxx'.split('xx', 2)?>(<?print item?>)<?end for?>")
864
865
866def test_method_rsplit():
867    check("(g)(u)(r)(k)", ur"<?for item in ' \t\r\ng \t\r\nu \t\r\nr \t\r\nk \t\r\n'.rsplit()?>(<?print item?>)<?end for?>")
868    check("( \t\r\ng \t\r\nu \t\r\nr)(k)", 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?>")
869    check("()(g)(u)(r)(k)()", ur"<?for item in 'xxgxxuxxrxxkxx'.rsplit('xx')?>(<?print item?>)<?end for?>")
870    check("(xxgxxuxxr)(k)()", ur"<?for item in 'xxgxxuxxrxxkxx'.rsplit('xx', 2)?>(<?print item?>)<?end for?>")
871
872
873def test_method_replace():
874    check('goork', ur"<?print 'gurk'.replace('u', 'oo')?>")
875
876
877def test_method_format():
878    now = datetime.datetime.now()
879    format = "%Y-%m-%d %H:%M:%S"
880    check(now.strftime(format), ur"<?print data.format('%s')?>" % format, data=now)
881
882
883def test_method_isoformat():
884    now = datetime.datetime.now()
885    check(now.isoformat(), ur"<?print data.isoformat()?>", data=now)
886
887
888def test_method_get():
889    check("42", u"<?print {}.get('foo', 42)?>")
890    check("17", u"<?print {'foo': 17}.get('foo', 42)?>")
891    check("", u"<?print {}.get('foo')?>")
892    check("17", u"<?print {'foo': 17}.get('foo')?>")
893
894
895def test_render():
896    t = ul4c.compile(u'<?print prefix?><?print data?><?print suffix?>')
897    check('(f)(o)(o)', u'<?for c in data?><?render t(data=c, prefix="(", suffix=")")?><?end for?>', dict(t=t), data='foo')
898
899
900def test_render_var():
901    t = ul4c.compile(u'<?code x += 1?><?print x?>')
902    check('42,43,42', u'<?print x?>,<?render t(x=x)?>,<?print x?>', dict(t=t), x=42)
903
904
905def test_parse():
906    check('42', u'<?print data.Noner?>', data=dict(Noner=42))
907
908
909def test_nested_exceptions():
910    tmpl1 = ul4c.compile(u"<?print 2*x?>")
911    tmpl2 = ul4c.compile(u"<?render tmpl1(x=x)?>")
912    tmpl3 = ul4c.compile(u"<?render tmpl2(x=x)?>")
913
914    checkrunerror(r"TypeError .*render tmpl3.*render tmpl2.*render tmpl1.*print 2.*unsupported operand type", u"<?render tmpl3(x=x)?>", dict(tmpl1=tmpl1, tmpl2=tmpl2, tmpl3=tmpl3), x=None)
Note: See TracBrowser for help on using the browser.