File: test_grammar.py

package info (click to toggle)
python-ptk 1.3.8%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 416 kB
  • sloc: python: 3,616; makefile: 200
file content (233 lines) | stat: -rw-r--r-- 6,992 bytes parent folder | download | duplicates (3)
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
#!/usr/bin/env python3

import base, unittest

from ptk.grammar import Production, GrammarError, Grammar, production
from ptk.lexer import token
from ptk.parser import ProductionParser
from ptk.utils import callbackByName


class GrammarUnderTest(Grammar):
    tokens = set()

    @classmethod
    def addTokenType(cls, name, callback, regex, types=None):
        if types is None:
            cls.tokens.add(name)
        else:
            for name in types:
                cls.tokens.add(name)

    @classmethod
    def tokenTypes(cls):
        return cls.tokens

    @classmethod
    def _createProductionParser(cls, name, priority, attrs):
        return ProductionParser(callbackByName(name), priority, cls, attrs)


class ProductionParserTestCase(unittest.TestCase):
    def setUp(self):
        class TestGrammar(GrammarUnderTest):
            tokens = set()
        self.parser = ProductionParser(None, None, TestGrammar, dict())
        self.grammarClass = TestGrammar

    def assertHasProduction(self, prods, prod):
        for aProduction in prods:
            if prod == (aProduction.name, aProduction.right):
                return
        self.fail('Production %s not found in %s' % (prod, prods))

    def _parse(self, string):
        self.parser.parse(string)
        return self.grammarClass.productions()

    def test_production_name(self):
        prod, = self._parse('test -> A')
        self.assertEqual(prod.name, 'test')

    def test_production_callback(self):
        prod, = self._parse('test -> A')
        self.assertEqual(prod.callback, None)

    def test_empty_production(self):
        prod, = self._parse('test -> ')
        self.assertEqual(prod.right, [])

    def test_empty_end(self):
        prod1, prod2 = self._parse('test -> A | ')
        self.assertEqual(prod1.right, ['A'])
        self.assertEqual(prod2.right, [])

    def test_empty_start(self):
        prod1, prod2 = self._parse('test -> | A')
        self.assertEqual(prod1.right, [])
        self.assertEqual(prod2.right, ['A'])

    def test_order(self):
        prod, = self._parse('test -> A B')
        self.assertEqual(prod.right, ['A', 'B'])

    def test_escape_litteral(self):
        prod, = self._parse(r'test -> "spam\"foo"')
        self.assertEqual(prod.right, [r'spam"foo'])

    def _findListSym(self, prods):
        for prod in prods:
            if prod.name != 'test':
                return prod.name
        self.fail('Cannot find list symbol in %s' % repr(prods))

    def test_list(self):
        prods = self._parse('test -> A* "+"')
        self.assertEqual(len(prods), 4, repr(prods))
        listSym = self._findListSym(prods)

        self.assertHasProduction(prods, ('test', ['+']))
        self.assertHasProduction(prods, (listSym, ['A']))
        self.assertHasProduction(prods, (listSym, [listSym, 'A']))
        self.assertHasProduction(prods, ('test', [listSym, '+']))

    def test_list_not_empty(self):
        prods = self._parse('test -> A+')
        listSym = self._findListSym(prods)
        self.assertEqual(len(prods), 3, repr(prods))

        self.assertHasProduction(prods, (listSym, ['A']))
        self.assertHasProduction(prods, (listSym, [listSym, 'A']))
        self.assertHasProduction(prods, ('test', [listSym]))

    def test_list_with_separator(self):
        prods = self._parse('test -> A+(pipe)')
        self.assertEqual(len(prods), 3, repr(prods))
        listSym = self._findListSym(prods)

        self.assertHasProduction(prods, (listSym, ['A']))
        self.assertHasProduction(prods, (listSym, [listSym, 'pipe', 'A']))
        self.assertHasProduction(prods, ('test', [listSym]))

    def test_list_with_litteral_separator(self):
        prods = self._parse('test -> A+("|")')
        self.assertEqual(len(prods), 3, repr(prods))
        listSym = self._findListSym(prods)

        self.assertHasProduction(prods, (listSym, ['A']))
        self.assertHasProduction(prods, (listSym, [listSym, '|', 'A']))
        self.assertHasProduction(prods, ('test', [listSym]))

    def test_atmostone(self):
        prods = self._parse('test -> A?')
        self.assertEqual(len(prods), 2)
        self.assertHasProduction(prods, ('test', []))
        self.assertHasProduction(prods, ('test', ['A']))


class ProductionTestCase(unittest.TestCase):
    def setUp(self):
        # A -> B<b> C
        self.production = Production('A', self.callback)
        self.production.addSymbol('B', 'b')
        self.production.addSymbol('C')
        self.calls = list()

    def callback(self, grammar, **kwargs):
        self.calls.append(kwargs)
        return 42

    def test_duplicate(self):
        try:
            self.production.addSymbol('D', 'b')
        except GrammarError:
            pass
        else:
            self.fail()

    def test_kwargs(self):
        cb, kwargs = self.production.apply([1, 2], None)
        cb(self, **kwargs)
        self.assertEqual(self.calls, [{'b': 1}])


class GrammarTestCase(unittest.TestCase):
    def test_production(self):
        class G(GrammarUnderTest):
            @production('A -> B')
            def a(self):
                pass
        prod, = G.productions()
        self.assertEqual(prod.name, 'A')
        self.assertEqual(prod.right, ['B'])

    def test_start_symbol(self):
        class G(GrammarUnderTest):
            @production('A -> B')
            def a(self):
                pass
            @production('C -> D')
            def c(self):
                pass

        grammar = G()
        self.assertEqual(grammar.startSymbol, 'A')

    def test_duplicate_name(self):
        class G(GrammarUnderTest):
            @production('A -> B')
            def a1(self):
                pass
            @production('A -> C')
            def a2(self):
                pass
        prod1, prod2 = G.productions()
        self.assertEqual(prod1.name, 'A')
        self.assertEqual(prod1.right, ['B'])
        self.assertEqual(prod2.name, 'A')
        self.assertEqual(prod2.right, ['C'])

    def test_token_name(self):
        try:
            class G(GrammarUnderTest):
                tokens = set(['spam'])
                @production('spam -> spam')
                def spam(self):
                    pass
        except GrammarError:
            pass
        else:
            self.fail()

    def test_duplicate_production_1(self):
        class G(GrammarUnderTest):
            @production('A -> B|B')
            def a(self):
                pass

        try:
            G()
        except GrammarError:
            pass
        else:
            self.fail()

    def test_duplicate_production_2(self):
        class G(GrammarUnderTest):
            @production('A -> B')
            def a1(self):
                pass
            @production('A -> B')
            def a2(self):
                pass

        try:
            G()
        except GrammarError:
            pass
        else:
            self.fail()


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