pypeg /pypeg2/test/test_pyPEG2.py

Language Python Lines 355
MD5 Hash 480a62b36238cc7fc5fadd6052f58052 Estimated Cost $7,445 (why?)
Repository https://bitbucket.org/apalala/pypeg View Raw File View Project SPDX
  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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
from __future__ import unicode_literals

import unittest
import pypeg2
import re

class GrammarTestCase1(unittest.TestCase):
    def runTest(self):
        x = pypeg2.some("thing")
        y = pypeg2.maybe_some("thing")
        z = pypeg2.optional("hello", "world")
        self.assertEqual(x, (-2, "thing"))
        self.assertEqual(y, (-1, "thing"))
        self.assertEqual(z, (0, ("hello", "world")))

class GrammarTestCase2(unittest.TestCase):
    def runTest(self):
        L1 = pypeg2.csl("thing")
        L2 = pypeg2.csl("hello", "world")
        self.assertEqual(L1, ("thing", -1, (",", pypeg2.blank, "thing")))
        self.assertEqual(L2, ("hello", "world", -1, (",", pypeg2.blank, "hello", "world")))

class ParserTestCase(unittest.TestCase): pass

class TypeErrorTestCase(ParserTestCase):
    def runTest(self):
        parser = pypeg2.Parser()
        with self.assertRaises(pypeg2.GrammarTypeError):
            parser.parse("hello, world", 23)

class ParseTerminalStringTestCase1(ParserTestCase):
    def runTest(self):
        parser = pypeg2.Parser()
        r = parser.parse("hello, world", "hello")
        self.assertEqual(r, (", world", None))

class ParseTerminalStringTestCase2(ParserTestCase):
    def runTest(self):
        parser = pypeg2.Parser()
        with self.assertRaises(SyntaxError):
            r = parser.parse("hello, world", "world")

class ParseKeywordTestCase1(ParserTestCase):
    def runTest(self):
        parser = pypeg2.Parser()
        r = parser.parse("hallo, world", pypeg2.K("hallo"))
        self.assertEqual(r, (", world", None))
        pypeg2.Keyword.table[pypeg2.K("hallo")]

class ParseKeywordTestCase2(ParserTestCase):
    def runTest(self):
        parser = pypeg2.Parser()
        with self.assertRaises(SyntaxError):
            r = parser.parse("hello, world", pypeg2.K("werld"))
        pypeg2.Keyword.table[pypeg2.K("werld")]

class ParseKeywordTestCase3(ParserTestCase):
    def runTest(self):
        parser = pypeg2.Parser()
        with self.assertRaises(SyntaxError):
            r = parser.parse(", world", pypeg2.K("hallo"))
        pypeg2.Keyword.table[pypeg2.K("hallo")]

class ParseRegexTestCase1(ParserTestCase):
    def runTest(self):
        parser = pypeg2.Parser()
        r = parser.parse("hello, world", re.compile(r"h.[lx]l\S", re.U))
        self.assertEqual(r, (", world", "hello"))

class ParseRegexTestCase2(ParserTestCase):
    def runTest(self):
        parser = pypeg2.Parser()
        with self.assertRaises(SyntaxError):
            r = parser.parse("hello, world", re.compile(r"\d", re.U))

class ParseSymbolTestCase1(ParserTestCase):
    def runTest(self):
        parser = pypeg2.Parser()
        r = parser.parse("hello, world", pypeg2.Symbol)
        self.assertEqual(r, (", world", pypeg2.Symbol("hello")))

class ParseSymbolTestCase2(ParserTestCase):
    def runTest(self):
        parser = pypeg2.Parser()
        with self.assertRaises(SyntaxError):
            r = parser.parse(", world", pypeg2.Symbol)

class ParseAttributeTestCase(ParserTestCase):
    def runTest(self):
        parser = pypeg2.Parser()
        r = parser.parse("hello, world", pypeg2.attr("some", pypeg2.Symbol))
        self.assertEqual(
            r,
            (
                ', world',
                pypeg2.attr.Class(name='some', thing=pypeg2.Symbol('hello'),
                    subtype=None)
            )
        )

class ParseTupleTestCase1(ParserTestCase):
    def runTest(self):
        parser = pypeg2.Parser()
        r = parser.parse("hello, world", (pypeg2.name(), ",", pypeg2.name()))
        self.assertEqual(
            r,
            (
                '',
                [
                    pypeg2.attr.Class(name='name',
                        thing=pypeg2.Symbol('hello'), subtype=None),
                    pypeg2.attr.Class(name='name',
                        thing=pypeg2.Symbol('world'), subtype=None)
                ]
            )
        )

class ParseTupleTestCase2(ParserTestCase):
    def runTest(self):
        parser = pypeg2.Parser()
        with self.assertRaises(ValueError):
            parser.parse("hello, world", (-23, "x"))

class ParseSomeTestCase1(ParserTestCase):
    def runTest(self):
        parser = pypeg2.Parser()
        r = parser.parse("hello, world", pypeg2.some(re.compile(r"\w", re.U)))
        self.assertEqual(r, (', world', ['h', 'e', 'l', 'l', 'o']))

class ParseSomeTestCase2(ParserTestCase):
    def runTest(self):
        parser = pypeg2.Parser()
        with self.assertRaises(SyntaxError):
            r = parser.parse("hello, world", pypeg2.some(re.compile(r"\d", re.U)))

class ParseMaybeSomeTestCase1(ParserTestCase):
    def runTest(self):
        parser = pypeg2.Parser()
        r = parser.parse("hello, world", pypeg2.maybe_some(re.compile(r"\w", re.U)))
        self.assertEqual(r, (', world', ['h', 'e', 'l', 'l', 'o']))

class ParseMaybeSomeTestCase2(ParserTestCase):
    def runTest(self):
        parser = pypeg2.Parser()
        r = parser.parse("hello, world", pypeg2.maybe_some(re.compile(r"\d", re.U)))
        self.assertEqual(r, ('hello, world', []))

class ParseCardinalityTestCase1(ParserTestCase):
    def runTest(self):
        parser = pypeg2.Parser()
        r = parser.parse("hello, world", (5, re.compile(r"\w", re.U)))
        self.assertEqual(r, (', world', ['h', 'e', 'l', 'l', 'o']))

class ParseCardinalityTestCase2(ParserTestCase):
    def runTest(self):
        parser = pypeg2.Parser()
        with self.assertRaises(SyntaxError):
            r = parser.parse("hello, world", (6, re.compile(r"\w", re.U)))

class ParseOptionsTestCase1(ParserTestCase):
    def runTest(self):
        parser = pypeg2.Parser()
        r = parser.parse("hello, world", [re.compile(r"\d+", re.U), pypeg2.word])
        self.assertEqual(r, (', world', 'hello'))

class ParseOptionsTestCase2(ParserTestCase):
    def runTest(self):
        parser = pypeg2.Parser()
        with self.assertRaises(SyntaxError):
            r = parser.parse("hello, world", ["x", "y"])

class ParseListTestCase1(ParserTestCase):
    class Chars(pypeg2.List):
        grammar = pypeg2.some(re.compile(r"\w", re.U)), pypeg2.attr("comma", ",")

    def runTest(self):
        parser = pypeg2.Parser()
        r = parser.parse("hello, world", ParseListTestCase1.Chars)
        self.assertEqual(r, (
            'world',
            ParseListTestCase1.Chars(['h', 'e', 'l', 'l', 'o']))
        )
        self.assertEqual(r[1].comma, None)

class ParseListTestCase2(ParserTestCase):
    class Digits(pypeg2.List):
        grammar = pypeg2.some(re.compile(r"\d", re.U))

    def runTest(self):
        parser = pypeg2.Parser()
        with self.assertRaises(SyntaxError):
            r = parser.parse("hello, world", ParseListTestCase2.Digits)

class ParseClassTestCase1(ParserTestCase):
    class Word(str):
        grammar = pypeg2.word
 
    def runTest(self):
        parser = pypeg2.Parser()
        r = parser.parse("hello, world", ParseClassTestCase1.Word)
        self.assertEqual(type(r[1]), ParseClassTestCase1.Word)
        self.assertEqual(r[1], "hello")

class ParseClassTestCase2(ParserTestCase):
    class Word(str):
        grammar = pypeg2.word, pypeg2.attr("comma", ",")
        def __init__(self, data):
            self.polished = False
        def polish(self):
            self.polished = True
 
    def runTest(self):
        parser = pypeg2.Parser()
        r = parser.parse("hello, world", ParseClassTestCase2.Word)
        self.assertEqual(type(r[1]), ParseClassTestCase2.Word)
        self.assertEqual(r[1], "hello")
        self.assertTrue(r[1].polished)
        self.assertEqual(r[1].comma, None)

class Parm(object):
    grammar = pypeg2.name(), "=", pypeg2.attr("value", int)

class Parms(pypeg2.Namespace):
    grammar = (pypeg2.csl(Parm), pypeg2.flag("fullstop", "."),
            pypeg2.flag("semicolon", ";"))

class ParseNLTestCase1(ParserTestCase):
    def runTest(self):
        parser = pypeg2.Parser()
        parser.comment = pypeg2.comment_c
        t, parms = parser.parse("x=23 /* Illuminati */, y=42 /* the answer */;", Parms)
        self.assertEqual(parms["x"].value, 23)
        self.assertEqual(parms["y"].value, 42)
        self.assertEqual(parms.fullstop, False)
        self.assertEqual(parms.semicolon, True)

class EnumTest(pypeg2.Symbol):
    grammar = pypeg2.Enum( pypeg2.K("int"), pypeg2.K("long") )

class ParseEnumTestCase1(ParserTestCase):
    def runTest(self):
        parser = pypeg2.Parser()
        t, r = parser.parse("int", EnumTest)
        self.assertEqual(r, "int")

class ParseEnumTestCase2(ParserTestCase):
    def runTest(self):
        parser = pypeg2.Parser()
        with self.assertRaises(SyntaxError):
            t, r = parser.parse("float", EnumTest)

class ParseInvisibleTestCase(ParserTestCase):
    class C1(str):
        grammar = pypeg2.ignore("!"), pypeg2.restline
    def runTest(self):
        r = pypeg2.parse("!all", type(self).C1)
        self.assertEqual(str(r), "all")
        self.assertEqual(r._ignore1, None)

class ComposeTestCase(unittest.TestCase): pass

class ComposeString(object):
    grammar = "something"

class ComposeStringTestCase(ComposeTestCase):
    def runTest(self):
        x = ComposeString()
        t = pypeg2.compose(x)
        self.assertEqual(t, "something")

class ComposeRegex(str):
    grammar = pypeg2.word

class ComposeRegexTestCase(ComposeTestCase):
    def runTest(self):
        x = ComposeRegex("something")
        t = pypeg2.compose(x)
        self.assertEqual(t, "something")

class ComposeKeyword(object):
    grammar = pypeg2.K("hallo")

class ComposeKeywordTestCase(ComposeTestCase):
    def runTest(self):
        x = ComposeKeyword()
        t = pypeg2.compose(x)
        self.assertEqual(t, "hallo")

class ComposeSymbol(pypeg2.Symbol): pass

class ComposeSymbolTestCase(ComposeTestCase):
    def runTest(self):
        x = ComposeSymbol("hello")
        t = pypeg2.compose(x)
        self.assertEqual(t, "hello")

class ComposeAttribute(object):
    grammar = pypeg2.name()

class ComposeAttributeTestCase(ComposeTestCase):
    def runTest(self):
        x = ComposeAttribute()
        x.name = pypeg2.Symbol("hello")
        t = pypeg2.compose(x)
        self.assertEqual(t, "hello")

class ComposeFlag(object):
    grammar = pypeg2.flag("mark", "MARK")

class ComposeFlagTestCase1(ComposeTestCase):
    def runTest(self):
        x = ComposeFlag()
        x.mark = True
        t = pypeg2.compose(x)
        self.assertEqual(t, "MARK")

class ComposeFlagTestCase2(ComposeTestCase):
    def runTest(self):
        x = ComposeFlag()
        x.mark = False
        t = pypeg2.compose(x)
        self.assertEqual(t, "")

class ComposeTuple(pypeg2.List):
    grammar = pypeg2.csl(pypeg2.word)

class ComposeTupleTestCase(ComposeTestCase):
    def runTest(self):
        x = ComposeTuple(["hello", "world"])
        t = pypeg2.compose(x)
        self.assertEqual(t, "hello, world")

class ComposeList(str):
    grammar = [ re.compile(r"\d+", re.U), pypeg2.word ]

class ComposeListTestCase(ComposeTestCase):
    def runTest(self):
        x = ComposeList("hello")
        t = pypeg2.compose(x)
        self.assertEqual(t, "hello")

class C2(str):
    grammar = pypeg2.attr("some", "!"), pypeg2.restline

class ComposeInvisibleTestCase(ParserTestCase):
    def runTest(self):
        r = pypeg2.parse("!all", C2)
        self.assertEqual(str(r), "all")
        self.assertEqual(r.some, None)
        t = pypeg2.compose(r, C2)
        self.assertEqual(t, "!all")

if __name__ == '__main__':
    unittest.main()
Back to Top