root/livinglogic.python.xist/test/test_ul4.py @ 3455:32328d691b0a

Revision 3455:32328d691b0a, 23.9 KB (checked in by Walter Doerwald <walter@…>, 11 years ago)

Add a function vars() that returns a dictionary with all variables.

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