1import unittest
2from simpleparse.parser import Parser
3from simpleparse.common import numbers
4from simpleparse import dispatchprocessor
5
6_data = [
7    (
8        "int_unsigned", numbers.IntInterpreter,
9        [ # should match, value, length that should match, expected result
10            ("0 ", 1, 0),
11            ("1 ", 1, 1),
12            ("23 ",2, 23),
13            ("0x ", 1,0),
14            ("0. ", 1,0),
15        ],
16        [ # should not match...
17            ".0",
18            "a",
19        ],
20    ),
21    (
22        "int", numbers.IntInterpreter,
23        [ # should match, value, length that should match, expected result
24            ("0 ", 1, 0),
25            ("1 ", 1, 1),
26            ("23 ",2, 23),
27            ("0x ", 1,0),
28            ("0. ", 1,0),
29            ("+0 ", 2, 0),
30            ("+1 ", 2, 1),
31            ("+23 ",3, 23),
32            ("+0x ", 2,0),
33            ("+0. ", 2,0),
34            ("-0 ", 2, 0),
35            ("-1 ", 2, -1),
36            ("-23 ",3, -23),
37            ("-0x ", 2,0),
38            ("-0. ", 2,0),
39        ],
40        [ # should not match...
41            ".0",
42            "a",
43            "+.0",
44            "+a",
45            "-.0",
46            "-a",
47        ],
48    ),
49    (
50        "hex", numbers.HexInterpreter,
51        [ # should match, value, length that should match, expected result
52            ("0x0 ", 3, 0),
53            ("0x1 ", 3, 1),
54            ("0x23 ",4, 35),
55            ("0x0x ", 3,0),
56            ("0x0. ", 3,0),
57            ("+0x0 ", 4, 0),
58            ("+0x1 ", 4, 1),
59            ("+0x23 ",5, 35),
60            ("+0x0x ", 4,0),
61            ("+0x0. ", 4,0),
62            ("-0x0 ", 4, 0),
63            ("-0x1 ", 4, -1),
64            ("-0x23 ",5, -35),
65            ("-0x0x ", 4,0),
66            ("-0x0. ", 4,0),
67            ("0xa ", 3, 10),
68            ("0xaaaaaaaaaaaaaaaaa ", 19, 196765270119568550570),
69            ("0xA ", 3, 10),
70            ("0xAAAAAAAAAAAAAAAAA ", 19, 196765270119568550570),
71        ],
72        [ # should not match...
73            ".0",
74            "a",
75            "+.0",
76            "+a",
77            "-.0",
78            "-a",
79            "0x ",
80            "0xg",
81            "0x",
82        ],
83    ),
84    (
85        "binary_number", numbers.BinaryInterpreter,
86        [ # should match, value, length that should match, expected result
87            ("0b0 ", 2, 0),
88            ("1b0 ", 2, 1),
89            ("10b0 ", 3, 2),
90            ("10000000000b0 ", 12, 1024),
91            ("0B0 ", 2, 0),
92            ("1B0 ", 2, 1),
93            ("10B0 ", 3, 2),
94            ("10000000000B0 ", 12, 1024),
95        ],
96        [ # should not match...
97            ".0",
98            "a",
99            "+.0",
100            "+a",
101            "-.0",
102            "-a",
103            "0x ",
104            "0xg",
105            "0x",
106        ],
107    ),
108    (
109        "float", numbers.FloatInterpreter,
110        [ # should match, value, length that should match, expected result
111            ("0. ", 2, 0),
112            ("1. ", 2, 1),
113            ("23. ",3, 23),
114            (".0 ", 2, 0),
115            (".1 ", 2, .1),
116            (".23 ",3, .23),
117            ("0.0x ", 3,0),
118            ("1.1x ", 3,1.1),
119            ("2000000.22222222x ", 16, 2000000.22222222),
120            ("1.1e20 ", 6, 1.1e20),
121            ("1.1e-20 ",7, 1.1e-20),
122            ("-1.1e20 ", 7, -1.1e20),
123        ],
124        [ # should not match...
125            "0x.0",
126            "23",
127            "-23",
128            "-43*2a",
129            "+23",
130            "-a",
131        ],
132    ),
133    (
134        "float_floatexp", numbers.FloatFloatExpInterpreter,
135        [ # should match, value, length that should match, expected result
136            ("0. ", 2, 0),
137            ("1. ", 2, 1),
138            ("23. ",3, 23),
139            (".0 ", 2, 0),
140            (".1 ", 2, .1),
141            (".23 ",3, .23),
142            ("0.0x ", 3,0),
143            ("1.1x ", 3,1.1),
144            ("2000000.22222222x ", 16, 2000000.22222222),
145            ("1.1e20 ", 6, 1.1* (1e20)),
146            ("1.1e-20 ",7, 1.1* (1e-20)),
147            ("-1.1e20 ", 7, -1.1* (1e20)),
148            ("1.1e20.34 ", 9, 1.1* (10 ** 20.34)),
149            ("1.1e-.34 ", 8, 1.1*( 10 ** -.34)),
150        ],
151        [ # should not match...
152            "0x.0",
153            "23",
154            "-23",
155            "-43*2a",
156            "+23",
157            "-a",
158        ],
159    ),
160]
161
162
163class CommonTests(unittest.TestCase):
164    def testBasic( self ):
165        for production, processor, yestable, notable in _data:
166            p = Parser( "x := %s"%production, 'x')
167            proc = dispatchprocessor.DispatchProcessor()
168            setattr(proc, production, processor())
169            for data, length, value in yestable:
170                success, results, next = p.parse( data, processor = proc)
171                assert next == length, """Did not parse string %s of %s as a %s result=%s"""%( repr(data[:length]), repr(data), production, (success, results, next))
172                assert results[0] == value, """Didn't get expected value from processing value %s, expected %s, got %s"""%( data[:length], value, results[0])
173
174            for data in notable:
175                success, results, next = p.parse( data)
176                assert not success, """Parsed %s of %s as a %s result=%s"""%( repr(data[:length]), repr(data), production, (success, results, next))
177
178
179
180def getSuite():
181    return unittest.makeSuite(CommonTests, 'test')
182
183if __name__ == "__main__":
184    unittest.main(defaultTest="getSuite")
185