~dricottone/gap

ref: 37a014fedc014cb926fcf525d9e7626c2151ecf7 gap/tests/generated_syntax_tests.py -rw-r--r-- 10.5 KiB
37a014feDominic Ricottone Adding make build 4 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#!/usr/bin/env python3

import unittest

import tests.generated_syntax.syntax0 as syntax0
import tests.generated_syntax.syntax1 as syntax1
import tests.generated_syntax.syntax2 as syntax2
import tests.generated_syntax.syntax3 as syntax3
import tests.generated_syntax.syntax4 as syntax4
import tests.generated_syntax.syntax5 as syntax5
import tests.generated_syntax.syntax6 as syntax6
import tests.generated_syntax.syntax7 as syntax7
import tests.generated_syntax.syntax8 as syntax8
import tests.generated_syntax.syntax9 as syntax9
import tests.generated_syntax.syntax10 as syntax10
import tests.generated_syntax.syntax11 as syntax11
import tests.generated_syntax.syntax12 as syntax12
import tests.generated_syntax.syntax13 as syntax13
import tests.generated_syntax.syntax14 as syntax14
import tests.generated_syntax.syntax15 as syntax15

# itertools.product([True,False], repeat=4) ->
# True,  True,  True,  True
# True,  True,  True,  False
# True,  True,  False, True
# True,  True,  False, False
# True,  False, True,  True
# True,  False, True,  False
# True,  False, False, True
# True,  False, False, False
# False, True,  True,  True
# False, True,  True,  False
# False, True,  False, True
# False, True,  False, False
# False, False, True,  True
# False, False, True,  False
# False, False, False, True
# False, False, False, False
# Order: attached_values, raise_on_overfull, executable, debug_mode

class Test_GeneratedSyntax(unittest.TestCase):
    def read(self, filename):
        try:
            with open(filename, 'r') as f:
                return f.readlines()
        except OSError:
            self.skipTest("cannot read file '{0}'".format(filename))

    def test_attached_values(self):
        # The below tests shoudl parse apart "-b=1" and set beta to "1".
        args = ["-b=1"]
        cfg, pos = syntax0.main(args)
        self.assertEqual(pos, [])
        self.assertEqual(cfg, {"beta": "1"})

        args = ["-b=1"]
        cfg, pos = syntax1.main(args)
        self.assertEqual(pos, [])
        self.assertEqual(cfg, {"beta": "1"})

        args = ["-b=1"]
        cfg, pos = syntax2.main(args)
        self.assertEqual(pos, [])
        self.assertEqual(cfg, {"beta": "1"})

        args = ["-b=1"]
        cfg, pos = syntax3.main(args)
        self.assertEqual(pos, [])
        self.assertEqual(cfg, {"beta": "1"})

        args = ["-b=1"]
        cfg, pos = syntax4.main(args)
        self.assertEqual(pos, [])
        self.assertEqual(cfg, {"beta": "1"})

        args = ["-b=1"]
        cfg, pos = syntax5.main(args)
        self.assertEqual(pos, [])
        self.assertEqual(cfg, {"beta": "1"})

        args = ["-b=1"]
        cfg, pos = syntax6.main(args)
        self.assertEqual(pos, [])
        self.assertEqual(cfg, {"beta": "1"})

        args = ["-b=1"]
        cfg, pos = syntax7.main(args)
        self.assertEqual(pos, [])
        self.assertEqual(cfg, {"beta": "1"})

        # The below tests should set "-b=1" into positionals.
        args = ["-b=1"]
        cfg, pos = syntax8.main(args)
        self.assertEqual(pos, ["-b=1"])
        self.assertEqual(cfg, {})

        args = ["-b=1"]
        cfg, pos = syntax9.main(args)
        self.assertEqual(pos, ["-b=1"])
        self.assertEqual(cfg, {})

        args = ["-b=1"]
        cfg, pos = syntax10.main(args)
        self.assertEqual(pos, ["-b=1"])
        self.assertEqual(cfg, {})

        args = ["-b=1"]
        cfg, pos = syntax11.main(args)
        self.assertEqual(pos, ["-b=1"])
        self.assertEqual(cfg, {})

        args = ["-b=1"]
        cfg, pos = syntax12.main(args)
        self.assertEqual(pos, ["-b=1"])
        self.assertEqual(cfg, {})

        args = ["-b=1"]
        cfg, pos = syntax13.main(args)
        self.assertEqual(pos, ["-b=1"])
        self.assertEqual(cfg, {})

        args = ["-b=1"]
        cfg, pos = syntax14.main(args)
        self.assertEqual(pos, ["-b=1"])
        self.assertEqual(cfg, {})

        args = ["-b=1"]
        cfg, pos = syntax15.main(args)
        self.assertEqual(pos, ["-b=1"])
        self.assertEqual(cfg, {})

    def test_attached_values_raise_on_overfull(self):
        # The below tests should raise on finding the "=1".
        args = ["-a=1"]
        with self.assertRaises(ValueError):
            cfg, pos = syntax0.main(args)

        args = ["-a=1"]
        with self.assertRaises(ValueError):
            cfg, pos = syntax1.main(args)

        args = ["-a=1"]
        with self.assertRaises(ValueError):
            cfg, pos = syntax2.main(args)

        args = ["-a=1"]
        with self.assertRaises(ValueError):
            cfg, pos = syntax3.main(args)

        # The below tests should set alpha to True and ignore the "=1".
        args = ["-a=1"]
        cfg, pos = syntax4.main(args)
        self.assertEqual(cfg, {"alpha": True})
        self.assertEqual(pos, [])

        args = ["-a=1"]
        cfg, pos = syntax5.main(args)
        self.assertEqual(cfg, {"alpha": True})
        self.assertEqual(pos, [])

        args = ["-a=1"]
        cfg, pos = syntax6.main(args)
        self.assertEqual(cfg, {"alpha": True})
        self.assertEqual(pos, [])

        args = ["-a=1"]
        cfg, pos = syntax7.main(args)
        self.assertEqual(cfg, {"alpha": True})
        self.assertEqual(pos, [])

        # The below tests should set "-a=1" into positionals.
        # raise_on_overfull only matters for options that take 0 values but
        #   have an attached value. Therefore, this option is only effective
        #   if attached_values is also True.
        args = ["-a=1"]
        cfg, pos = syntax8.main(args)
        self.assertEqual(cfg, {})
        self.assertEqual(pos, ["-a=1"])

        args = ["-a=1"]
        cfg, pos = syntax9.main(args)
        self.assertEqual(cfg, {})
        self.assertEqual(pos, ["-a=1"])

        args = ["-a=1"]
        cfg, pos = syntax10.main(args)
        self.assertEqual(cfg, {})
        self.assertEqual(pos, ["-a=1"])

        args = ["-a=1"]
        cfg, pos = syntax11.main(args)
        self.assertEqual(cfg, {})
        self.assertEqual(pos, ["-a=1"])

        args = ["-a=1"]
        cfg, pos = syntax12.main(args)
        self.assertEqual(cfg, {})
        self.assertEqual(pos, ["-a=1"])

        args = ["-a=1"]
        cfg, pos = syntax13.main(args)
        self.assertEqual(cfg, {})
        self.assertEqual(pos, ["-a=1"])

        args = ["-a=1"]
        cfg, pos = syntax14.main(args)
        self.assertEqual(cfg, {})
        self.assertEqual(pos, ["-a=1"])

        args = ["-a=1"]
        cfg, pos = syntax15.main(args)
        self.assertEqual(cfg, {})
        self.assertEqual(pos, ["-a=1"])

    def test_executable(self):
        for fn in enumerate([
            "tests/generated_syntax/syntax0.py",
            "tests/generated_syntax/syntax1.py",
            "tests/generated_syntax/syntax2.py",
            "tests/generated_syntax/syntax3.py",
            "tests/generated_syntax/syntax4.py",
            "tests/generated_syntax/syntax5.py",
            "tests/generated_syntax/syntax6.py",
            "tests/generated_syntax/syntax7.py",
            "tests/generated_syntax/syntax8.py",
            "tests/generated_syntax/syntax9.py",
            "tests/generated_syntax/syntax10.py",
            "tests/generated_syntax/syntax11.py",
            "tests/generated_syntax/syntax12.py",
            "tests/generated_syntax/syntax13.py",
            "tests/generated_syntax/syntax14.py",
            "tests/generated_syntax/syntax15.py",
        ]):
            with self.subTest(fn=fn):
                number, filename = fn
                lines = self.read(filename)
                # These files should have a line 'if __name__=="__main__":'
                if number in (0,1,4,5,8,9,12,13,):
                    self.assertIn('if __name__=="__main__":\n', lines)

    def test_debugmode_catch_option(self):
        # The below tests should pull "--debug-gap-behavior" from the arguments
        args = ["--", "--debug-gap-behavior"]
        cfg, pos = syntax0.main(args)
        self.assertEqual(cfg, {})
        self.assertEqual(pos, [])

        args = ["--", "--debug-gap-behavior"]
        cfg, pos = syntax2.main(args)
        self.assertEqual(cfg, {})
        self.assertEqual(pos, [])

        args = ["--", "--debug-gap-behavior"]
        cfg, pos = syntax4.main(args)
        self.assertEqual(cfg, {})
        self.assertEqual(pos, [])

        args = ["--", "--debug-gap-behavior"]
        cfg, pos = syntax6.main(args)
        self.assertEqual(cfg, {})
        self.assertEqual(pos, [])

        args = ["--", "--debug-gap-behavior"]
        cfg, pos = syntax8.main(args)
        self.assertEqual(cfg, {})
        self.assertEqual(pos, [])

        args = ["--", "--debug-gap-behavior"]
        cfg, pos = syntax10.main(args)
        self.assertEqual(cfg, {})
        self.assertEqual(pos, [])

        args = ["--", "--debug-gap-behavior"]
        cfg, pos = syntax12.main(args)
        self.assertEqual(cfg, {})
        self.assertEqual(pos, [])

        args = ["--", "--debug-gap-behavior"]
        cfg, pos = syntax14.main(args)
        self.assertEqual(cfg, {})
        self.assertEqual(pos, [])

        # The below tests should set "--debug-gap-behavior" into positionals
        args = ["--", "--debug-gap-behavior"]
        cfg, pos = syntax1.main(args)
        self.assertEqual(cfg, {})
        self.assertEqual(pos, ["--debug-gap-behavior"])

        args = ["--", "--debug-gap-behavior"]
        cfg, pos = syntax3.main(args)
        self.assertEqual(cfg, {})
        self.assertEqual(pos, ["--debug-gap-behavior"])

        args = ["--", "--debug-gap-behavior"]
        cfg, pos = syntax5.main(args)
        self.assertEqual(cfg, {})
        self.assertEqual(pos, ["--debug-gap-behavior"])

        args = ["--", "--debug-gap-behavior"]
        cfg, pos = syntax7.main(args)
        self.assertEqual(cfg, {})
        self.assertEqual(pos, ["--debug-gap-behavior"])

        args = ["--", "--debug-gap-behavior"]
        cfg, pos = syntax9.main(args)
        self.assertEqual(cfg, {})
        self.assertEqual(pos, ["--debug-gap-behavior"])

        args = ["--", "--debug-gap-behavior"]
        cfg, pos = syntax11.main(args)
        self.assertEqual(cfg, {})
        self.assertEqual(pos, ["--debug-gap-behavior"])

        args = ["--", "--debug-gap-behavior"]
        cfg, pos = syntax13.main(args)
        self.assertEqual(cfg, {})
        self.assertEqual(pos, ["--debug-gap-behavior"])

        args = ["--", "--debug-gap-behavior"]
        cfg, pos = syntax15.main(args)
        self.assertEqual(cfg, {})
        self.assertEqual(pos, ["--debug-gap-behavior"])

if __name__ == "__main__":
    unittest.main()