root/livinglogic.python.xist/test/test_ul4.py @ 3468:e656913270cd

Revision 3468:e656913270cd, 25.5 KB (checked in by Walter Doerwald <walter@…>, 11 years ago)

Implement operators, <, <=, >, >=. Rename == and != opcodes. Add tests.

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