1#!/usr/bin/env python
2
3import srclexer, srcparser, globals
4
5class TestCase:
6
7    @staticmethod
8    def run (tokens, defines):
9        mcExpander = srcparser.MacroExpander(tokens, defines)
10        mcExpander.debug = True
11        mcExpander.expand()
12        tokens = mcExpander.getTokens()
13        print tokens
14
15    @staticmethod
16    def simpleNoArgs ():
17        tokens = ['FUNC_FOO', '(', 'left', ',', 'right', ')']
18        defines = {}
19        macro = globals.Macro('FUNC_FOO')
20        macro.tokens = ['Here', 'comes', 'X', 'and', 'Y']
21        defines['FUNC_FOO'] = macro
22        TestCase.run(tokens, defines)
23
24    @staticmethod
25    def simpleArgs ():
26        tokens = ['FUNC_FOO', '(', 'left', ',', 'right', ')']
27        defines = {}
28        macro = globals.Macro('FUNC_FOO')
29        macro.tokens = ['Here', 'comes', 'X', 'and', 'Y']
30        macro.vars['X'] = 0
31        macro.vars['Y'] = 1
32        defines['FUNC_FOO'] = macro
33        TestCase.run(tokens, defines)
34
35    @staticmethod
36    def multiTokenArgs ():
37        tokens = ['FUNC_FOO', '(', 'left1', 'left2', 'left3', ',', 'right', ')']
38        defines = {}
39        macro = globals.Macro('FUNC_FOO')
40        macro.tokens = ['Here', 'comes', 'X', 'and', 'Y']
41        macro.vars['X'] = 0
42        macro.vars['Y'] = 1
43        defines['FUNC_FOO'] = macro
44        TestCase.run(tokens, defines)
45
46    @staticmethod
47    def nestedTokenArgs ():
48        tokens = ['FUNC_BAA', '(', 'left', ',', 'right', ')']
49        defines = {}
50        macro = globals.Macro('FUNC_FOO')
51        macro.tokens = ['Here', 'comes', 'X', 'and', 'Y']
52        macro.vars['X'] = 0
53        macro.vars['Y'] = 1
54        defines['FUNC_FOO'] = macro
55        macro = globals.Macro('FUNC_BAA')
56        macro.tokens = ['FUNC_FOO']
57        defines['FUNC_BAA'] = macro
58        TestCase.run(tokens, defines)
59
60def main ():
61    print "simple expansion with no arguments"
62    TestCase.simpleNoArgs()
63    print "simple argument expansion"
64    TestCase.simpleArgs()
65    print "multi-token argument expansion"
66    TestCase.multiTokenArgs()
67    print "nested argument expansion"
68    TestCase.nestedTokenArgs()
69
70if __name__ ==  '__main__':
71    main()
72