summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_tools/test_c_analyzer/test_variables/test_info.py
blob: d424d8eebb811149354a464bc4e09afbb54b48f4 (plain)
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
import string
import unittest

from ..util import PseudoStr, StrProxy, Object
from .. import tool_imports_for_tests
with tool_imports_for_tests():
    from c_analyzer.common.info import UNKNOWN, ID
    from c_analyzer.variables.info import (
            normalize_vartype, Variable
            )


class NormalizeVartypeTests(unittest.TestCase):

    def test_basic(self):
        tests = [
                (None, None),
                ('', ''),
                ('int', 'int'),
                (PseudoStr('int'), 'int'),
                (StrProxy('int'), 'int'),
                ]
        for vartype, expected in tests:
            with self.subTest(vartype):
                normalized = normalize_vartype(vartype)

                self.assertEqual(normalized, expected)


class VariableTests(unittest.TestCase):

    VALID_ARGS = (
            ('x/y/z/spam.c', 'func', 'eggs'),
            'static',
            'int',
            )
    VALID_KWARGS = dict(zip(Variable._fields, VALID_ARGS))
    VALID_EXPECTED = VALID_ARGS

    def test_init_typical_global(self):
        for storage in ('static', 'extern', 'implicit'):
            with self.subTest(storage):
                static = Variable(
                        id=ID(
                            filename='x/y/z/spam.c',
                            funcname=None,
                            name='eggs',
                            ),
                        storage=storage,
                        vartype='int',
                        )

                self.assertEqual(static, (
                        ('x/y/z/spam.c', None, 'eggs'),
                        storage,
                        'int',
                        ))

    def test_init_typical_local(self):
        for storage in ('static', 'local'):
            with self.subTest(storage):
                static = Variable(
                        id=ID(
                            filename='x/y/z/spam.c',
                            funcname='func',
                            name='eggs',
                            ),
                        storage=storage,
                        vartype='int',
                        )

        self.assertEqual(static, (
                ('x/y/z/spam.c', 'func', 'eggs'),
                storage,
                'int',
                ))

    def test_init_all_missing(self):
        for value in ('', None):
            with self.subTest(repr(value)):
                static = Variable(
                        id=value,
                        storage=value,
                        vartype=value,
                        )

                self.assertEqual(static, (
                        None,
                        None,
                        None,
                        ))

    def test_init_all_coerced(self):
        id = ID('x/y/z/spam.c', 'func', 'spam')
        tests = [
            ('str subclass',
             dict(
                 id=(
                    PseudoStr('x/y/z/spam.c'),
                    PseudoStr('func'),
                    PseudoStr('spam'),
                    ),
                 storage=PseudoStr('static'),
                 vartype=PseudoStr('int'),
                 ),
             (id,
              'static',
              'int',
              )),
            ('non-str 1',
             dict(
                 id=id,
                 storage=Object(),
                 vartype=Object(),
                 ),
             (id,
              '<object>',
              '<object>',
              )),
            ('non-str 2',
             dict(
                 id=id,
                 storage=StrProxy('static'),
                 vartype=StrProxy('variable'),
                 ),
             (id,
              'static',
              'variable',
              )),
            ('non-str',
             dict(
                 id=id,
                 storage=('a', 'b', 'c'),
                 vartype=('x', 'y', 'z'),
                 ),
             (id,
              "('a', 'b', 'c')",
              "('x', 'y', 'z')",
              )),
            ]
        for summary, kwargs, expected in tests:
            with self.subTest(summary):
                static = Variable(**kwargs)

                for field in Variable._fields:
                    value = getattr(static, field)
                    if field == 'id':
                        self.assertIs(type(value), ID)
                    else:
                        self.assertIs(type(value), str)
                self.assertEqual(tuple(static), expected)

    def test_iterable(self):
        static = Variable(**self.VALID_KWARGS)

        id, storage, vartype = static

        values = (id, storage, vartype)
        for value, expected in zip(values, self.VALID_EXPECTED):
            self.assertEqual(value, expected)

    def test_fields(self):
        static = Variable(('a', 'b', 'z'), 'x', 'y')

        self.assertEqual(static.id, ('a', 'b', 'z'))
        self.assertEqual(static.storage, 'x')
        self.assertEqual(static.vartype, 'y')

    def test___getattr__(self):
        static = Variable(('a', 'b', 'z'), 'x', 'y')

        self.assertEqual(static.filename, 'a')
        self.assertEqual(static.funcname, 'b')
        self.assertEqual(static.name, 'z')

    def test_validate_typical(self):
        validstorage = ('static', 'extern', 'implicit', 'local')
        self.assertEqual(set(validstorage), set(Variable.STORAGE))

        for storage in validstorage:
            with self.subTest(storage):
                static = Variable(
                        id=ID(
                            filename='x/y/z/spam.c',
                            funcname='func',
                            name='eggs',
                            ),
                        storage=storage,
                        vartype='int',
                        )

                static.validate()  # This does not fail.

    def test_validate_missing_field(self):
        for field in Variable._fields:
            with self.subTest(field):
                static = Variable(**self.VALID_KWARGS)
                static = static._replace(**{field: None})

                with self.assertRaises(TypeError):
                    static.validate()
        for field in ('storage', 'vartype'):
            with self.subTest(field):
                static = Variable(**self.VALID_KWARGS)
                static = static._replace(**{field: UNKNOWN})

                with self.assertRaises(TypeError):
                    static.validate()

    def test_validate_bad_field(self):
        badch = tuple(c for c in string.punctuation + string.digits)
        notnames = (
                '1a',
                'a.b',
                'a-b',
                '&a',
                'a++',
                ) + badch
        tests = [
            ('id', ()),  # Any non-empty str is okay.
            ('storage', ('external', 'global') + notnames),
            ('vartype', ()),  # Any non-empty str is okay.
            ]
        seen = set()
        for field, invalid in tests:
            for value in invalid:
                seen.add(value)
                with self.subTest(f'{field}={value!r}'):
                    static = Variable(**self.VALID_KWARGS)
                    static = static._replace(**{field: value})

                    with self.assertRaises(ValueError):
                        static.validate()

        for field, invalid in tests:
            if field == 'id':
                continue
            valid = seen - set(invalid)
            for value in valid:
                with self.subTest(f'{field}={value!r}'):
                    static = Variable(**self.VALID_KWARGS)
                    static = static._replace(**{field: value})

                    static.validate()  # This does not fail.