File: parameter_test.py

package info (click to toggle)
python-tatsu 5.15.1%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 904 kB
  • sloc: python: 10,128; makefile: 54
file content (213 lines) | stat: -rw-r--r-- 6,211 bytes parent folder | download | duplicates (2)
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
import contextlib
import pathlib
import sys
import tempfile
import unittest

from tatsu.ngcodegen import codegen
from tatsu.parser import GrammarGenerator
from tatsu.tool import compile
from tatsu.util import trim


class ParameterTests(unittest.TestCase):
    def test_keyword_params(self):
        grammar = """
            start(k1=1, k2=2)
                =
                {'a'} $
                ;
        """
        g = GrammarGenerator('Keywords')
        model = g.parse(grammar)
        code = codegen(model)
        self.assertEqual('#!/usr/bin/env python3', code.splitlines()[0])

    def test_35_only_keyword_params(self):
        grammar = """
            rule(kwdA=A, kwdB=B)
                =
                'a'
                ;
        """
        model = compile(grammar, 'test')
        self.assertEqual(trim(grammar), str(model))

    def test_36_params_and_keyword_params(self):
        grammar = """
            rule(A, kwdB=B)
                =
                'a'
                ;
        """
        model = compile(grammar, 'test')
        self.assertEqual(trim(grammar), str(model))

    def test_36_param_combinations(self):
        def assert_equal(target, value):
            self.assertEqual(target, value)

        class TC36Semantics:

            """Check all rule parameters for expected types and values"""

            def rule_positional(self, ast, p1, p2, p3, p4):
                assert_equal('ABC', p1)
                assert_equal(123, p2)
                assert_equal('=', p3)
                assert_equal('+', p4)
                return ast

            def rule_keyword(self, ast, k1, k2, k3, k4):
                assert_equal('ABC', k1)
                assert_equal(123, k2)
                assert_equal('=', k3)
                assert_equal('+', k4)
                return ast

            def rule_all(self, ast, p1, p2, p3, p4, k1, k2, k3, k4):
                assert_equal('DEF', p1)
                assert_equal(456, p2)
                assert_equal('=', p3)
                assert_equal('+', p4)
                assert_equal('HIJ', k1)
                assert_equal(789, k2)
                assert_equal('=', k3)
                assert_equal('+', k4)
                return ast

        grammar = """
            @@ignorecase::False
            @@nameguard

            start
                = {rule_positional | rule_keywords | rule_all} $ ;
            rule_positional('ABC', 123, '=', '+')
                = 'a' ;
            rule_keywords(k1=ABC, k3='=', k4='+', k2=123)
                = 'b' ;
            rule_all('DEF', 456, '=', '+', k1=HIJ, k3='=', k4='+', k2=789)
                = 'c' ;
        """

        pretty = """
            @@ignorecase :: False
            @@nameguard :: True

            start
                =
                {rule_positional | rule_keywords | rule_all} $
                ;


            rule_positional(ABC, 123, '=', '+')
                =
                'a'
                ;


            rule_keywords(k1=ABC, k3='=', k4='+', k2=123)
                =
                'b'
                ;


            rule_all(DEF, 456, '=', '+', k1=HIJ, k3='=', k4='+', k2=789)
                =
                'c'
                ;
        """

        model = compile(grammar, 'RuleArguments')
        self.assertEqual(trim(pretty), str(model))
        model = compile(pretty, 'RuleArguments')

        ast = model.parse('a b c')
        self.assertEqual(['a', 'b', 'c'], ast)
        semantics = TC36Semantics()
        ast = model.parse('a b c', semantics=semantics)
        self.assertEqual(['a', 'b', 'c'], ast)
        codegen(model)

    def test_36_unichars(self):
        grammar = """
            start = { rule_positional | rule_keywords | rule_all }* $ ;

            rule_positional("ÄÖÜäöüß") = 'a' ;

            rule_keywords(k1='äöüÄÖÜß') = 'b' ;

            rule_all('ßÄÖÜäöü', k1="ßäöüÄÖÜ") = 'c' ;
        """

        def _trydelete(pypath, pymodule):
            module_with_path = pypath / pymodule

            with contextlib.suppress(OSError):
                module_with_path.with_suffix('.py').unlink()
            with contextlib.suppress(OSError):
                module_with_path.with_suffix('.pyc').unlink()
            with contextlib.suppress(OSError):
                module_with_path.with_suffix('.pyo').unlink()

        def assert_equal(target, value):
            self.assertEqual(target, value)

        class UnicharsSemantics:
            """Check all rule parameters for expected types and values"""

            def rule_positional(self, ast, p1):
                assert_equal('ÄÖÜäöüß', p1)
                return ast

            def rule_keyword(self, ast, k1):
                assert_equal('äöüÄÖÜß', k1)
                return ast

            def rule_all(self, ast, p1, k1):
                assert_equal('ßÄÖÜäöü', p1)
                assert_equal('ßäöüÄÖÜ', k1)
                return ast

        m = compile(grammar, 'UnicodeRuleArguments')
        ast = m.parse('a b c')
        self.assertEqual(['a', 'b', 'c'], ast)

        semantics = UnicharsSemantics()
        ast = m.parse('a b c', semantics=semantics)
        self.assertEqual(['a', 'b', 'c'], ast)

        code = codegen(m)
        import codecs
        module_name = 'tc36unicharstest'
        temp_dir = pathlib.Path(tempfile.mkdtemp()) / module_name
        temp_dir.mkdir(exist_ok=True)
        py_file_path = temp_dir / f'{module_name}.py'

        with codecs.open(py_file_path, 'w', 'utf-8') as f:
            f.write(code)
        try:
            sys.path.append(str(temp_dir))
            import tc36unicharstest  # pylint: disable=E0401

            assert tc36unicharstest
            _trydelete(temp_dir, module_name)
        except Exception as e:
            self.fail(e)

    def test_numbers_and_unicode(self):
        grammar = """
            rúle(1, -23, 4.56, 7.89e-11, Añez)
                =
                'a'
                ;


            rúlé::Añez
                =
                'ñ'
                ;
        """

        model = compile(grammar, 'test')
        self.assertEqual(trim(grammar), str(model))