Contact: fumanchu@aminus.org

Log in as guest/geniusql to create tickets

root/trunk/geniusql/test/test_astwalk.py

Revision 320 (checked in by lakin, 2 months ago)

properly deal with our new python jumps and fix the related tests. all of test_codewalk, test_astwalk and test_logic should now run properly in python 2.6 and 2.7

  • Property svn:eol-style set to native
Line 
1 from compiler.ast import *
2 import datetime
3 import sys
4 import unittest
5
6 from geniusql import astwalk
7
8 from geniusql.test.lambdas import *
9
10 #-------------------------------------------------------------------------------
11 # Now we'll map up these same functions to their decompiled versions based on
12 # the appropriate version of python.
13 class Results_Python26(object):
14     # Python 2.5 stopped putting arguments in co_names,
15     # and stopped prepending None to co_consts (except when CO_NESTED?).
16     lambda_parser_tests = [
17         {
18             'function': f_in_op,
19             'results':
20                 astwalk.AST(
21                   Compare(
22                       Name('x'),
23                       [('in', Const([1, 2, 3, 4, 5]))]
24                     ),
25                   ['x']
26               ),
27         },
28         {
29             'function': f_add_op,
30             'results':
31                 astwalk.AST(
32                     Add((
33                         Name('x'),
34                         Const(datetime.date(2004, 1, 1))
35                     )),
36                     ['x']
37                 )
38         },
39         {
40             'function': f_kwargs_and_op,
41             'results':
42                 astwalk.AST(
43                     And((
44                         Compare(
45                             Getattr(Name('x'), 'Date'),
46                             [('==', Const(datetime.date(2004, 1, 1)))]
47                         ),
48                         Compare(
49                             Getattr(Name('x'), 'Qty'),
50                             [(
51                                 '<',
52                                 Subscript(
53                                     Name('kw'),
54                                     'OP_APPLY',
55                                     [Const('Size')]
56                                 ))
57                             ]
58                         )
59                     )),
60                     ['x'],
61                     dstar_args='kw'
62                 )
63         },
64         {
65             'function': f_mix_names_global_locals_attrs,
66             'results':
67                 astwalk.AST(
68                     Or((
69                         Compare(
70                             Const(4),
71                             [('!=', Getattr(Name('x'), 'amount'))]
72                         ),
73                         Compare(
74                             Mul((
75                                 Name('amount'),
76                                 Const(3)
77                             )),
78                             [('>', Const(20))]
79                         )
80                     )),
81                     ['x', 'amount']
82                 )
83         },
84         {
85             'function': f_constant_multiplication,
86             'results':
87                 astwalk.AST(
88                     Mul((
89                         Const(60),
90                         Name('x')
91                     )),
92                     ['x']
93                 )
94         },
95         {
96             'function': f_array_slice_subscript,
97             'results':
98             astwalk.AST(
99                 Compare(
100                     Const([3, 4]),
101                     [(
102                         '==',
103                         UnarySub(
104                             Subscript(
105                                 Name('x'),
106                                 'OP_APPLY',
107                                 [Const('offset')]
108                             )
109                         )
110                     )]
111                 ),
112                 ['x']
113             )
114         },
115         {
116             'function': f_const_attr,
117             'results':
118                 astwalk.AST(
119                     Or((
120                         Const(True),
121                         Compare(
122                             Const(5),
123                             [('==', Getattr(Name('x'), 'Qty'))]
124                         )
125                     )),
126                     ['x']
127                 )
128         },
129         {
130             'function': f_nested_conditionals,
131             'results':
132                 astwalk.AST(
133                     Not(
134                         And((
135                             Compare(
136                                 Getattr(Name('x'), 'a'),
137                                 [('==', Const(3))]
138                             ),
139                            Or((
140                                Compare(
141                                    Getattr(Name('x'), 'b'),
142                                    [('>', Const(1))]
143                                ),
144                                Compare(
145                                    Getattr(Name('x'), 'b'),
146                                    [('<', Const(-10))]
147                                 )
148                             ))
149                         ))
150                     ),
151                     ['x']
152                 )
153         },
154         {
155             'function': f_nested_conditionals_2,
156             'results':
157                 astwalk.AST(
158                     Not(
159                         Or((
160                             And((
161                                 Compare(
162                                     Getattr(Name('x'), 'a'),
163                                     [('==', Const(3))]
164                                 ),
165                                 Compare(
166                                    Getattr(Name('x'), 'b'),
167                                    [('>', Const(1))]
168                                 ),
169                             )),
170                             Compare(
171                                Getattr(Name('x'), 'b'),
172                                [('<', Const(-10))]
173                             )
174                         ))
175                     ),
176                     ['x']
177                 )
178         },
179         {
180             'function': f_unicode_const,
181             'results':
182                 astwalk.AST(
183                     Compare(
184                         Getattr(Name('x'), 'Name'),
185                         [('==', Const(u'Dimsdale'))]
186                     ),
187                     ['x']
188                 )
189         },
190         {
191             'function': f_unicode_const_getattr,
192             'results':
193                 astwalk.AST(
194                     Compare(
195                         Getattr(Name('x'), 'Name'),
196                         [('==', Const(u'Dimsdale'))]
197                     ),
198                     ['x']
199                 )
200         },
201         {
202             'function': f_unicode_multiple_args,
203             'results':
204                 astwalk.AST(
205                     And((
206                         Compare(
207                             Getattr(Name('x'), 'Qty'),
208                             [('>', Const(1))]
209                         ),
210                         And((
211                             Compare(
212                                 Getattr(Name('y'), 'Qty'),
213                                 [('>', Const(20))]
214                             ),
215                             Compare(
216                                 Getattr(Name('z'), 'Type'),
217                                 [('==', Const('A'))]
218                             )
219                         ))
220                     )),
221                     ['x', 'y', 'z'],
222                     dstar_args='kw'
223                 )
224         },
225         {
226             'function': f_closure_example,
227             'results': astwalk.AST(Const(10)),
228         },
229     ]
230     lambda_deparser_tests = [
231         {
232             'function': f_in_op,
233             'results': 'lambda x: x in [1, 2, 3, 4, 5]',
234         },
235         {
236             'function': f_add_op,
237             'results': 'lambda x: x + datetime.date(2004, 1, 1)',
238         },
239         {
240             'function': f_kwargs_and_op,
241             'results': "lambda x, **kw: (x.Date == datetime.date(2004, 1, 1)) and (x.Qty < kw['Size'])",
242         },
243         {
244             'function': f_mix_names_global_locals_attrs,
245             'results': "lambda x, amount: (4 != x.amount) or (amount * 3 > 20)",
246         },
247         {
248             'function': f_constant_multiplication,
249             'results': "lambda x: 60 * x",
250         },
251         {
252             'function': f_array_slice_subscript,
253             'results': "lambda x: [3, 4] == -(x['offset'])",
254         },
255         {
256             'function': f_const_attr,
257             'results': "lambda x: (True) or (5 == x.Qty)",
258         },
259         {
260             'function': f_nested_conditionals,
261             'results': "lambda x: not ((x.a == 3) and ((x.b > 1) or (x.b < -10)))",
262         },
263         {
264             'function': f_nested_conditionals_2,
265             'results': "lambda x: not (((x.a == 3) and (x.b > 1)) or (x.b < -10))",
266         },
267         {
268             'function': f_unicode_const,
269             'results': "lambda x: x.Name == u'Dimsdale'",
270         },
271         {
272             'function': f_unicode_const_getattr,
273             'results': "lambda x: x.Name == u'Dimsdale'",
274         },
275         {
276             'function': f_unicode_multiple_args,
277             'results': "lambda x, y, z, **kw: (x.Qty > 1) and ((y.Qty > 20) and (z.Type == 'A'))",
278         },
279         {
280             'function': f_closure_example,
281             'results': "lambda: 10",
282         },
283     ]
284
285 #-------------------------------------------------------------------------------
286 class Results_Python27(object):
287     lambda_parser_tests = [
288         {
289             'function': f_in_op,
290             'results':
291                 astwalk.AST(
292                   Compare(
293                       Name('x'),
294                       [('in', Const([1, 2, 3, 4, 5]))]
295                     ),
296                   ['x']
297               ),
298         },
299         {
300             'function': f_add_op,
301             'results':
302                 astwalk.AST(
303                     Add((
304                         Name('x'),
305                         Const(datetime.date(2004, 1, 1))
306                     )),
307                     ['x']
308                 )
309         },
310         {
311             'function': f_kwargs_and_op,
312             'results':
313                 astwalk.AST(
314                     And((
315                         Compare(
316                             Getattr(Name('x'), 'Date'),
317                             [('==', Const(datetime.date(2004, 1, 1)))]
318                         ),
319                         Compare(
320                             Getattr(Name('x'), 'Qty'),
321                             [(
322                                 '<',
323                                 Subscript(
324                                     Name('kw'),
325                                     'OP_APPLY',
326                                     [Const('Size')]
327                                 ))
328                             ]
329                         )
330                     )),
331                     ['x'],
332                     dstar_args='kw'
333                 )
334         },
335         {
336             'function': f_mix_names_global_locals_attrs,
337             'results':
338                 astwalk.AST(
339                     Or((
340                         Compare(
341                             Const(4),
342                             [('!=', Getattr(Name('x'), 'amount'))]
343                         ),
344                         Compare(
345                             Mul((
346                                 Name('amount'),
347                                 Const(3)
348                             )),
349                             [('>', Const(20))]
350                         )
351                     )),
352                     ['x', 'amount']
353                 )
354         },
355         {
356             'function': f_constant_multiplication,
357             'results':
358                 astwalk.AST(
359                     Mul((
360                         Const(60),
361                         Name('x')
362                     )),
363                     ['x']
364                 )
365         },
366         {
367             'function': f_array_slice_subscript,
368             'results':
369             astwalk.AST(
370                 Compare(
371                     Const([3, 4]),
372                     [(
373                         '==',
374                         UnarySub(
375                             Subscript(
376                                 Name('x'),
377                                 'OP_APPLY',
378                                 [Const('offset')]
379                             )
380                         )
381                     )]
382                 ),
383                 ['x']
384             )
385         },
386         {
387             'function': f_const_attr,
388             'results':
389                 astwalk.AST(
390                     Or((
391                         Const(True),
392                         Compare(
393                             Const(5),
394                             [('==', Getattr(Name('x'), 'Qty'))]
395                         )
396                     )),
397                     ['x']
398                 )
399         },
400         {
401             'function': f_nested_conditionals,
402             'results':
403                 astwalk.AST(
404                     Not(
405                         And((
406                             Compare(
407                                 Getattr(Name('x'), 'a'),
408                                 [('==', Const(3))]
409                             ),
410                            Or((
411                                Compare(
412                                    Getattr(Name('x'), 'b'),
413                                    [('>', Const(1))]
414                                ),
415                                Compare(
416                                    Getattr(Name('x'), 'b'),
417                                    [('<', Const(-10))]
418                                 )
419                             ))
420                         ))
421                     ),
422                     ['x']
423                 )
424         },
425         {
426             'function': f_nested_conditionals_2,
427             'results':
428                 astwalk.AST(
429                     Not(
430                         Or((
431                             And((
432                                 Compare(
433                                     Getattr(Name('x'), 'a'),
434                                     [('==', Const(3))]
435                                 ),
436                                 Compare(
437                                    Getattr(Name('x'), 'b'),
438                                    [('>', Const(1))]
439                                 ),
440                             )),
441                             Compare(
442                                Getattr(Name('x'), 'b'),
443                                [('<', Const(-10))]
444                             )
445                         ))
446                     ),
447                     ['x']
448                 )
449         },
450         {
451             'function': f_unicode_const,
452             'results':
453                 astwalk.AST(
454                     Compare(
455                         Getattr(Name('x'), 'Name'),
456                         [('==', Const(u'Dimsdale'))]
457                     ),
458                     ['x']
459                 )
460         },
461         {
462             'function': f_unicode_const_getattr,
463             'results':
464                 astwalk.AST(
465                     Compare(
466                         Getattr(Name('x'), 'Name'),
467                         [('==', Const(u'Dimsdale'))]
468                     ),
469                     ['x']
470                 )
471         },
472         {
473             'function': f_unicode_multiple_args,
474             'results':
475                 astwalk.AST(
476                     And((
477                         Compare(
478                             Getattr(Name('x'), 'Qty'),
479                             [('>', Const(1))]
480                         ),
481                         And((
482                             Compare(
483                                 Getattr(Name('y'), 'Qty'),
484                                 [('>', Const(20))]
485                             ),
486                             Compare(
487                                 Getattr(Name('z'), 'Type'),
488                                 [('==', Const('A'))]
489                             )
490                         ))
491                     )),
492                     ['x', 'y', 'z'],
493                     dstar_args='kw'
494                 )
495         },
496         {
497             'function': f_closure_example,
498             'results': astwalk.AST(Const(10)),
499         },
500     ]
501     lambda_deparser_tests = [
502         {
503             'function': f_in_op,
504             'results': 'lambda x: x in [1, 2, 3, 4, 5]',
505         },
506         {
507             'function': f_add_op,
508             'results': 'lambda x: x + datetime.date(2004, 1, 1)',
509         },
510         {
511             'function': f_kwargs_and_op,
512             'results': "lambda x, **kw: (x.Date == datetime.date(2004, 1, 1)) and (x.Qty < kw['Size'])",
513         },
514         {
515             'function': f_mix_names_global_locals_attrs,
516             'results': "lambda x, amount: (4 != x.amount) or (amount * 3 > 20)",
517         },
518         {
519             'function': f_constant_multiplication,
520             'results': "lambda x: 60 * x",
521         },
522         {
523             'function': f_array_slice_subscript,
524             'results': "lambda x: [3, 4] == -(x['offset'])",
525         },
526         {
527             'function': f_const_attr,
528             'results': "lambda x: (True) or (5 == x.Qty)",
529         },
530         {
531             'function': f_nested_conditionals,
532             'results': "lambda x: not ((x.a == 3) and ((x.b > 1) or (x.b < -10)))",
533         },
534         {
535             'function': f_nested_conditionals_2,
536             'results': "lambda x: not (((x.a == 3) and (x.b > 1)) or (x.b < -10))",
537         },
538         {
539             'function': f_unicode_const,
540             'results': "lambda x: x.Name == u'Dimsdale'",
541         },
542         {
543             'function': f_unicode_const_getattr,
544             'results': "lambda x: x.Name == u'Dimsdale'",
545         },
546         {
547             'function': f_unicode_multiple_args,
548             'results': "lambda x, y, z, **kw: (x.Qty > 1) and ((y.Qty > 20) and (z.Type == 'A'))",
549         },
550         {
551             'function': f_closure_example,
552             'results': "lambda: 10",
553         },
554     ]
555
556
557 Results = Results_Python26
558 if sys.version_info >= (2, 7):
559     Results = Results_Python27
560 elif sys.version_info >= (2, 6):
561     Results = Results_Python26
562 else:
563     raise RuntimeError("Python 2.5 and below no longer supported.")
564
565 class LambdaParserTests(unittest.TestCase):
566
567     def test_LambdaParser(self):
568         for test_case in Results.lambda_parser_tests:
569             lp = astwalk.LambdaParser(test_case['function'])
570 ##            lp.verbose = True
571             lp.walk()
572             self.assertEqual(repr(lp.ast), repr(test_case['results']))
573
574     def test_LambdaDeparser(self):
575         count = 0
576         for test_case in Results.lambda_deparser_tests:
577             lp = astwalk.LambdaParser(test_case['function'])
578             lp.walk()
579             r = astwalk.LambdaDeparser(lp.ast).code()
580             self.assertEqual(r, test_case['results'])
581             count += 1
582         self.assertEqual(count, 13, "There are 13 deparser tests, not all ran.")
583 ##
584 ##        # Test BUILD_LIST, BUILD_TUPLE
585 ##        builder = lambda a, b: [a, min(b)]
586 ##        self.assertEqual(astwalk.LambdaDeparser(builder).code(),
587 ##                         "lambda a, b: [a, min(b)]")
588 ##        builder = lambda a, b, c: (c, [b.next(), a], c.ID)
589 ##        self.assertEqual(astwalk.LambdaDeparser(builder).code(),
590 ##                         "lambda a, b, c: (c, [b.next(), a], c.ID)")
591 ##
592 ##        # Test MapStackObject
593 ##        e = lambda t: {'x': 3, 'y': 5}
594 ##        ld = astwalk.LambdaDeparser(e)
595 ##        c = ld.code()
596 ##        self.assertEqual(c, "lambda t: {'y': 5, 'x': 3}", (c, ld.stack))
597 ##
598 ##        e = lambda t:{'x':{'a':10}, 'y':5}
599 ##        ld = astwalk.LambdaDeparser(e)
600 ##        c = ld.code()
601 ##        self.assertEqual(c, "lambda t: {'y': 5, 'x': {'a': 10}}", (c, ld.stack))
602 ##
603 ##        e = lambda t:{'x':str({'a':10}), 'y':5}
604 ##        ld = astwalk.LambdaDeparser(e)
605 ##        c = ld.code()
606 ##        self.assertEqual(c, "lambda t: {'y': 5, 'x': str({'a': 10})}", (c, ld.stack))
607 ##
608 ##        e = lambda t: [{'x': 10}, {'y': 5}]
609 ##        ld = astwalk.LambdaDeparser(e)
610 ##        c = ld.code()
611 ##        self.assertEqual(c, "lambda t: [{'x': 10}, {'y': 5}]", (c, ld.stack))
612 ##
613
614 if __name__ == "__main__":
615     unittest.main(__name__)
616
Note: See TracBrowser for help on using the browser.