summary refs log tree commit diff stats
path: root/testing/test_abdl.py
blob: d28658a780fb15e0820c5f48e9435a723341d653 (plain) (blame)
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
# Tests abdl.py

import abdl

import hypothesis
import hypothesis.strategies as st

import collections.abc
import itertools
import re
import traceback


abdl.DeprecationError.enable_key_match_compat = False

# use abdl's _pairs for consistency.
pairs = abdl._vm._pairs

# do not put integers, floats, etc here
# do not put bytes, they iterate as integers
hashables = st.deferred(lambda: st.text() | st.frozensets(hashables) | st.lists(hashables).map(tuple))
values = st.deferred(lambda: hashables | objtree)
objtree = st.deferred(lambda: st.text() | st.dictionaries(hashables, values) | st.lists(values) | st.sets(hashables) | st.lists(hashables).map(tuple))

# note: use all() so as to not eat all the RAM :p

class LogAndCompare:
    def __init__(self, *iterables):
        self.log = []
        self._iters = iterables
    def __iter__(self):
        def wrap_exc(it):
            try:
                for x in it:
                    yield x
            except abdl.ValidationError as e:
                yield e
        for res in itertools.zip_longest(*map(wrap_exc, self._iters), fillvalue=object()):
            self.log.append(res)
            yield all(x == res[0] for x in res) or all(type(x) == abdl.ValidationError for x in res)
    def __repr__(self):
        return "<LogAndCompare(log=" + repr(self.log) + ")>"

@hypothesis.given(objtree, st.just(abdl.compile("->X")))
def test_basic_iterator(foo, pat):
    assert all(LogAndCompare(pat.match(foo), map(lambda x: {"X": x}, pairs(foo))))

@hypothesis.given(objtree, st.just(abdl.compile("->X->Y")))
def test_two_depths(foo, pat):
    def deep(foo):
        for x in pairs(foo):
            for y in pairs(x[1]):
                yield {"X": x, "Y": y}
    assert all(LogAndCompare(pat.match(foo), deep(foo)))

@hypothesis.given(objtree, st.just(abdl.compile("->X->Y->Z->W")))
def test_four_depths(foo, pat):
    def deep(foo):
        for x in pairs(foo):
            for y in pairs(x[1]):
                for z in pairs(y[1]):
                    for w in pairs(z[1]):
                        yield {"X": x, "Y": y, "Z": z, "W": w}
    assert all(LogAndCompare(pat.match(foo), deep(foo)))

@hypothesis.given(st.dictionaries(st.text(), st.text()) | st.sets(st.text()), st.just(abdl.compile("->/.../")))
def test_regex(foo, pat):
    # no bindings on this one :<
    def deep(foo):
        for x in pairs(foo):
            if re.search("...", x[0]):
                    yield {}
            else:
                raise abdl.ValidationError
    assert all(LogAndCompare(pat.match(foo), deep(foo)))

@hypothesis.given(objtree, st.just(abdl.compile("->/.../?")))
def test_regex_skippable_vs_objtree(foo, pat):
    assert all(LogAndCompare(pat.match(foo), ({} for x in pairs(foo) if isinstance(x[0], str) and re.search("...", x[0]))))

@hypothesis.given(st.dictionaries(st.text(), st.text()) | st.sets(st.text()), st.just(abdl.compile("->/.../->Y")))
def test_regex_and_bind(foo, pat):
    def deep(foo):
        for x in pairs(foo):
            if re.search("...", x[0]):
                for y in pairs(x[1]):
                    yield {"Y": y}
            else:
                raise abdl.ValidationError
    assert all(LogAndCompare(pat.match(foo), deep(foo)))

@hypothesis.given(objtree, st.just(abdl.compile("->/.../?->Y")))
def test_regex_skippable_and_bind_vs_objtree(foo, pat):
    def deep(foo):
        for x in pairs(foo):
            if isinstance(x[0], str) and re.search("...", x[0]):
                for y in pairs(x[1]):
                    yield {"Y": y}
    assert all(LogAndCompare(pat.match(foo), deep(foo)))

@hypothesis.given(objtree, st.just(abdl.compile("->/^...$/?->Y")))
def test_regex_anchored_skippable_and_bind_vs_objtree(foo, pat):
    def deep(foo):
        for x in pairs(foo):
            if isinstance(x[0], str) and re.search("^...$", x[0]):
                for y in pairs(x[1]):
                    yield {"Y": y}
    assert all(LogAndCompare(pat.match(foo), deep(foo)))

@hypothesis.given(objtree, st.just(abdl.compile("->''?->Y")))
def test_empty_literal_vs_objtree(foo, pat):
    def deep(foo):
        for x in pairs(foo):
            if x[0] == '':
                for y in pairs(x[1]):
                    yield {"Y": y}
    assert all(LogAndCompare(pat.match(foo), deep(foo)))

defs = {'a': (dict, list, set)}
@hypothesis.given(objtree, st.just(abdl.compile("->X:?$a->Y", defs=defs)))
def test_type(foo, pat):
    def deep(foo):
        for x in pairs(foo):
            if isinstance(x[1], (dict, list, set)):
                for y in pairs(x[1]):
                    yield {"X": x, "Y": y}
    assert all(LogAndCompare(pat.match(foo), deep(foo)))

defs = {'a': (dict, list, set), 'b': (dict, set), 'c': dict}
@hypothesis.given(objtree, st.just(abdl.compile("->X:?$a:?$b:?$c->Y", defs=defs)))
def test_multi_type(foo, pat):
    def deep(foo):
        for x in pairs(foo):
            if isinstance(x[1], dict):
                for y in pairs(x[1]):
                    yield {"X": x, "Y": y}
    assert all(LogAndCompare(pat.match(foo), deep(foo)))

defs = {'a': (dict, list, set), 'b': (dict, set), 'c': dict}
@hypothesis.given(objtree, st.just(abdl.compile("->X:$a:$b:$c->Y", defs=defs)))
@hypothesis.settings(suppress_health_check=[hypothesis.HealthCheck.too_slow])
def test_multi_type_with_validation_errors(foo, pat):
    def deep(foo):
        for x in pairs(foo):
            if isinstance(x[1], dict):
                for y in pairs(x[1]):
                    yield {"X": x, "Y": y}
            else:
                raise abdl.ValidationError
    assert all(LogAndCompare(pat.match(foo), deep(foo)))

defs = {'a': (dict, list, set), 'b': (dict, set), 'c': dict}
@hypothesis.given(objtree, st.just(abdl.compile("->X:?$a:?$b:?$c", defs=defs)))
def test_multi_type_at_end(foo, pat):
    def deep(foo):
        for x in pairs(foo):
            if isinstance(x[1], dict):
                    yield {"X": x}
    assert all(LogAndCompare(pat.match(foo), deep(foo)))

@hypothesis.given(st.dictionaries(st.frozensets(st.text()), st.text()), st.just(abdl.compile("->[:?$sets->A]->D", {'sets': collections.abc.Set})))
def test_subtree_partial(foo, pat):
    def deep(foo):
        for x in pairs(foo):
            if isinstance(x[0], collections.abc.Set):
                for a in pairs(x[0]):
                    for d in pairs(x[1]):
                        yield {"A": a, "D": d}
    assert all(LogAndCompare(pat.match(foo), deep(foo)))

@hypothesis.given(objtree, st.just(abdl.compile("->tag[]")))
def test_tagged_key(foo, pat):
    def deep(foo):
        for x in pairs(foo):
            yield {"tag": x}
    assert all(LogAndCompare(pat.match(foo), deep(foo)))

@hypothesis.given(objtree, st.just(abdl.compile("->[]->D")))
def test_key_dummy(foo, pat):
    def deep(foo):
        for x in pairs(foo):
                for d in pairs(x[1]):
                    yield {"D": d}
    assert all(LogAndCompare(pat.match(foo), deep(foo)))

@hypothesis.given(objtree, st.just(abdl.compile("->X->$a->Z", {'a': '0'})))
def test_param(foo, pat):
    def deep(foo):
        for x in pairs(foo):
            try:
                y = x[1]['0']
            except (TypeError, IndexError, KeyError):
                raise abdl.ValidationError
            else:
                for z in pairs(y):
                    yield {"X": x, "Z": z}
    assert all(LogAndCompare(pat.match(foo), deep(foo)))

@hypothesis.given(objtree, st.just(abdl.compile("->tag/.../?")))
def test_tagged_regex(foo, pat):
    def deep(foo):
        for x in pairs(foo):
            if isinstance(x[0], str) and re.search("...", x[0]):
                yield {"tag": x}
    assert all(LogAndCompare(pat.match(foo), deep(foo)))

@hypothesis.given(objtree, st.just(abdl.compile("->tag'0'?")))
def test_tagged_str(foo, pat):
    def deep(foo):
        try:
            yield {"tag": ('0', foo['0'])}
        except (TypeError, IndexError, KeyError):
            pass
    assert all(LogAndCompare(pat.match(foo), deep(foo)))

@hypothesis.given(objtree, st.just(abdl.compile("->tag$?a", {'a': '0'})))
def test_tagged_param(foo, pat):
    def deep(foo):
        try:
            yield {"tag": ('0', foo['0'])}
        except (TypeError, IndexError, KeyError):
            pass
    assert all(LogAndCompare(pat.match(foo), deep(foo)))

def test_basic_value_subtree():
    matcher = abdl.match("(->foo'foo')(->bar'bar')", {'foo': 1, 'bar': 2})
    assert list(matcher) == [{'foo': ('foo', 1), 'bar': ('bar', 2)}]

@hypothesis.given(objtree, st.just(abdl.compile("->[:?$sets]->D", {'sets': collections.abc.Set})))
def test_key_predicate(foo, pat):
    def deep(foo):
        for x in pairs(foo):
            if isinstance(x[0], collections.abc.Set):
                for d in pairs(x[1]):
                    yield {"D": d}
    assert all(LogAndCompare(pat.match(foo), deep(foo)))

@hypothesis.given(objtree, st.just(abdl.compile("->[:?$sets:?$sets->V]->D", {'sets': collections.abc.Set})))
def test_multi_key_predicate_with_values(foo, pat):
    def deep(foo):
        for x in pairs(foo):
            if isinstance(x[0], collections.abc.Set):
                if isinstance(x[0], collections.abc.Set):
                    for v in pairs(x[0]):
                        for d in pairs(x[1]):
                            yield {"V": v, "D": d}
    assert all(LogAndCompare(pat.match(foo), deep(foo)))

@hypothesis.given(objtree, st.just(abdl.compile("->[:?$sets:?$sets]->D", {'sets': collections.abc.Set})))
def test_multi_key_predicate(foo, pat):
    def deep(foo):
        for x in pairs(foo):
            if isinstance(x[0], collections.abc.Set):
                if isinstance(x[0], collections.abc.Set):
                    for d in pairs(x[1]):
                        yield {"D": d}
    assert all(LogAndCompare(pat.match(foo), deep(foo)))

@hypothesis.given(objtree, st.just(abdl.compile("")))
def test_empty(foo, pat):
    def deep(foo):
        yield {}
    assert all(LogAndCompare(pat.match(foo), deep(foo)))

# FIXME
#@hypothesis.given(objtree, st.text())
#def test_exhaustive(foo, pat):
#    hypothesis.assume(not re.match("^%s+$", pat))
#    hypothesis.assume(pat)
#    try:
#        compiled = abdl.compile(pat)
#        print(pat)
#    except abdl.PatternError:
#        hypothesis.assume(False)
#    compiled.match(foo)