-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
580 lines (481 loc) · 17.3 KB
/
test.py
File metadata and controls
580 lines (481 loc) · 17.3 KB
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
from dataclasses import dataclass
from enum import Enum
from inspect import signature
import typing
from unittest import TestCase
from io import StringIO
from datetime import date, time, datetime
from doctest import DocFileSuite
import sys
import traceback
from ags import _mapping
def load_tests(loader, tests, ignore):
if sys.version_info >= (3, 11):
tests.addTests(DocFileSuite("README.md"))
return tests
class Mapping(TestCase):
def myinject(self, obj):
return obj
def mysurject(self, obj, T):
self.assertIs(type(obj), T)
return obj
def check(self, obj, T):
m = _mapping.mapping_for(T)
low = m.lower(obj, self.myinject)
high = m.unlower(low, self.mysurject)
self.assertEqual(high, obj)
return low
def test_primitive(self):
for obj in "abc", 123, 1.5, True, False:
T = type(obj)
with self.subTest(T.__name__):
self.assertEqual(self.check(obj, T), obj)
def test_literal(self):
T = typing.Literal["abc", 123]
for obj in "abc", 123:
self.assertEqual(self.check(obj, T), obj)
def test_complex(self):
self.assertEqual(self.check(1 + 2j, complex), 1 + 2j)
self.assertEqual(self.check(3 + 0j, complex), 3 + 0j)
def test_bytes(self):
self.check(b"abc", bytes)
def test_list(self):
for modern in False, True:
with self.subTest(modern=modern):
List = list if modern else typing.List
self.assertEqual(self.check([1, 2, 3], List[int]), [1, 2, 3])
def test_tuple(self):
for modern in False, True:
Tuple = tuple if modern else typing.Tuple
with self.subTest("uniform", modern=modern):
self.assertEqual(self.check((1, 2, 3), Tuple[int, ...]), [1, 2, 3])
with self.subTest("pluriform", modern=modern):
self.assertEqual(
self.check((123, "abc"), Tuple[int, str]), [123, "abc"]
)
def test_dict(self):
for modern in False, True:
with self.subTest(modern=modern):
Dict = dict if modern else typing.Dict
self.check({"a": 10, "b": 20}, Dict[str, int]), {"a": 10, "b": 20}
def test_dataclass(self):
@dataclass
class A:
i: int
s: str
self.assertEqual(self.check(A(123, "abc"), A), {"i": 123, "s": "abc"})
def test_dataclass_defaults(self):
@dataclass
class A:
i: int = 10
s: str = 20
with self.assertRaises(ValueError) as cm:
_mapping.mapping_for(A)
s = traceback.format_exception(cm.exception)
self.assertEqual(
s,
[
"ValueError: expects str, got int\n",
"In: .s(default)\n",
]
if sys.version_info >= (3, 11)
else [
"ValueError: expects str, got int\n",
],
)
def test_boundargs(self):
def f(i: int, s: str):
pass
sig = signature(f)
bound = sig.bind(123, "abc")
self.assertEqual(self.check(bound, sig), {"i": 123, "s": "abc"})
def test_boundargs_defaults(self):
def f(i: int = 10, s: str = 20):
pass
sig = signature(f)
with self.assertRaises(ValueError) as cm:
_mapping.mapping_for(sig)
s = traceback.format_exception(cm.exception)
self.assertEqual(
s,
[
"ValueError: expects str, got int\n",
"In: .s(default)\n",
]
if sys.version_info >= (3, 11)
else [
"ValueError: expects str, got int\n",
],
)
def test_union(self):
for modern in False, True:
with self.subTest("optional", modern=modern):
T = int | None if modern else typing.Optional[int]
self.assertEqual(self.check(123, T), _mapping.OptionalValue(123))
self.assertEqual(self.check(None, T), _mapping.OptionalValue(None))
with self.subTest("union", modern=modern):
T = int | str if modern else typing.Union[int, str]
self.assertEqual(self.check(123, T), _mapping.UnionValue("int", 123))
self.assertEqual(
self.check("abc", T), _mapping.UnionValue("str", "abc")
)
with self.subTest("optional-union", modern=modern):
T = (
int | str | None
if modern
else typing.Optional[typing.Union[int, str]]
)
self.assertEqual(
self.check(123, T),
_mapping.OptionalValue(_mapping.UnionValue("int", 123)),
)
self.assertEqual(
self.check("abc", T),
_mapping.OptionalValue(_mapping.UnionValue("str", "abc")),
)
self.assertEqual(self.check(None, T), _mapping.OptionalValue(None))
def test_enum(self):
class E(Enum):
a = 1
b = 2
self.assertEqual(self.check(E.a, E), "a")
self.assertEqual(self.check(E.b, E), "b")
def test_reduce(self):
if sys.version_info < (3, 11):
self.skipTest("reduce is supported as of Python 3.11")
for modern in False, True:
with self.subTest(modern=modern):
List = list if modern else typing.List
Tuple = tuple if modern else typing.Tuple
Type = type if modern else typing.Type
class A:
def __init__(self, x: List[int]):
self.x = x
def __reduce__(
self,
) -> Tuple[Type[typing.Self], Tuple[List[int]]]:
return A, (self.x,)
def __eq__(self, other):
return isinstance(other, A) and other.x == self.x
a = A([2, 3, 4])
self.assertEqual(self.check(a, A), [2, 3, 4])
def test_ags_reduce(self):
class A:
def __init__(self, x: int):
self.x = x
def __into_ags__(self) -> int:
return self.x
@classmethod
def __from_ags__(cls, obj: int):
return cls(obj)
def __eq__(self, other):
return isinstance(other, A) and other.x == self.x
a = A(5)
self.assertEqual(self.check(a, A), 5)
def test_exception(self):
T = dict[str, list[int]]
m = _mapping.mapping_for(T)
with self.assertRaises(AssertionError) as cm:
m.unlower({"a": [10, 20], "b": [30, "40", 50]}, self.mysurject)
s = traceback.format_exception(cm.exception)
self.assertEqual(
s,
[
"AssertionError: <class 'str'> is not <class 'int'>\n",
"In: [b][1]\n",
]
if sys.version_info >= (3, 11)
else [
"AssertionError: <class 'str'> is not <class 'int'>\n",
],
)
class Demo:
@dataclass
class A:
x: int
y: float
@dataclass
class B:
@dataclass
class Sub:
b: bytes
greek: typing.Optional[str]
abc: typing.Literal["a", "b", "c"]
sub: Sub
@dataclass
class Left:
b: bool
@dataclass
class Right:
when: datetime
def func(a: A, b: typing.List[B], direction: typing.Union[Left, Right]):
pass
class Backend:
def check_lower(self, obj, expect):
low = self.mod._inject(obj)
self.assertEqual(low, expect)
high = self.mod._surject(low, type(obj))
self.assertEqual(high, obj)
return low
def check_load_dump(self, expect):
sig = signature(Demo.func)
bound = sig.bind(
a=Demo.A(1, 2.5),
b=[
Demo.B("a", Demo.B.Sub(b"foo", "αβγ")),
Demo.B("b", Demo.B.Sub(b"bar", None)),
],
direction=Demo.Right(datetime.fromisoformat("2025-07-27T09:06:40")),
)
with self.subTest("load"):
obj = self.mod.load(StringIO(expect), sig)
self.assertEqual(obj, bound)
with self.subTest("loads"):
obj = self.mod.loads(expect, sig)
self.assertEqual(obj, bound)
with self.subTest("dump"):
f = StringIO()
self.mod.dump(f, bound, sig)
self.assertEqual(f.getvalue(), expect)
with self.subTest("dumps"):
s = self.mod.dumps(bound, sig)
self.assertEqual(s, expect)
class JSON(Backend, TestCase):
from ags import json as mod
def test_bool(self):
for obj in True, False:
self.check_lower(obj, expect=obj)
def test_int(self):
for obj in 0, 1, 2, 10, -5:
self.check_lower(obj, expect=obj)
def test_float(self):
for obj in 0.0, 1.0, 2.0, -2.5:
self.check_lower(obj, expect=obj)
def test_complex(self):
for obj in 0 + 0j, 1 + 0j:
self.check_lower(obj, expect=obj.real)
self.check_lower(1j, expect="1j")
self.check_lower(-2.5 + 3.5j, "-2.5+3.5j")
def test_str(self):
for obj in "foo", "bar":
self.check_lower(obj, expect=obj)
def test_bytes(self):
for obj in b"foo", b"bar", "αβγ".encode():
self.check_lower(obj, expect="utf8:" + obj.decode("utf8"))
self.check_lower(bytes([0xC0, 0xC1, 0xF5]), expect="z`^w")
def test_date(self):
for obj in (
date.fromisoformat("2000-10-15"),
date.fromisoformat("2025-12-31"),
):
self.check_lower(obj, expect=obj.isoformat())
def test_time(self):
for obj in (
time.fromisoformat("10:32"),
time.fromisoformat("22:33"),
):
self.check_lower(obj, expect=obj.isoformat())
def test_datetime(self):
for obj in (
datetime.fromisoformat("2000-10-15 10:32"),
datetime.fromisoformat("2025-12-31 22:33"),
):
self.check_lower(obj, expect=obj.isoformat())
def test_list(self):
obj = [123, "abc", ["x", "y", "z"]]
self.check_lower(obj, expect=obj)
def test_dict(self):
obj = {"a": 123, 10: "abc", True: ["x", "y", "z"]}
self.check_lower(obj, expect=obj)
def test_union(self):
self.check_lower(_mapping.UnionValue("abc", 123), expect={"abc": 123})
def test_optional(self):
self.check_lower(_mapping.OptionalValue("abc"), expect="abc")
self.check_lower(_mapping.OptionalValue(None), expect=None)
def test_load_dump(self):
self.check_load_dump("""\
{
"a": {
"x": 1,
"y": 2.5
},
"b": [
{
"abc": "a",
"sub": {
"b": "utf8:foo",
"greek": "αβγ"
}
},
{
"abc": "b",
"sub": {
"b": "utf8:bar",
"greek": null
}
}
],
"direction": {
"Right": {
"when": "2025-07-27T09:06:40"
}
}
}
""")
class YAML(Backend, TestCase):
from ags import yaml as mod
def test_bool(self):
for obj in True, False:
self.check_lower(obj, expect=obj)
def test_int(self):
for obj in 0, 1, 2, 10, -5:
self.check_lower(obj, expect=obj)
def test_float(self):
for obj in 0.0, 1.0, 2.0, -2.5:
self.check_lower(obj, expect=obj)
def test_complex(self):
for obj in 0 + 0j, 1 + 0j:
self.check_lower(obj, expect=obj.real)
self.check_lower(1j, expect="1j")
self.check_lower(-2.5 + 3.5j, "-2.5+3.5j")
def test_str(self):
for obj in "foo", "bar":
self.check_lower(obj, expect=obj)
def test_bytes(self):
for obj in b"foo", b"bar", "αβγ".encode():
self.check_lower(obj, expect=obj)
def test_date(self):
for obj in (
date.fromisoformat("2000-10-15"),
date.fromisoformat("2025-12-31"),
):
self.check_lower(obj, expect=obj)
def test_time(self):
for obj in (
time.fromisoformat("10:32"),
time.fromisoformat("22:33"),
):
self.check_lower(obj, expect=obj)
def test_datetime(self):
for obj in (
datetime.fromisoformat("2000-10-15 10:32"),
datetime.fromisoformat("2025-12-31 22:33"),
):
self.check_lower(obj, expect=obj)
def test_list(self):
obj = [123, "abc", ["x", "y", "z"]]
self.check_lower(obj, expect=obj)
def test_dict(self):
obj = {"a": 123, 10: "abc", True: ["x", "y", "z"]}
self.check_lower(obj, expect=obj)
def test_union(self):
self.check_lower(_mapping.UnionValue("abc", 123), expect={"abc": 123})
def test_optional(self):
self.check_lower(_mapping.OptionalValue("abc"), expect="abc")
self.check_lower(_mapping.OptionalValue(None), expect=None)
def test_load_dump(self):
self.check_load_dump("""\
a:
x: 1
y: 2.5
b:
- abc: a
sub:
b: !!binary |
Zm9v
greek: αβγ
- abc: b
sub:
b: !!binary |
YmFy
greek: null
direction:
Right:
when: 2025-07-27 09:06:40
""")
class UCSL(Backend, TestCase):
from ags import ucsl as mod
def test_bool(self):
self.check_lower(True, expect="true")
self.check_lower(False, expect="false")
def test_int(self):
for obj in 0, 1, 2, 10, -5:
self.check_lower(obj, expect=str(obj))
def test_float(self):
for obj in 0.0, 1.0, 2.0, -2.5:
self.check_lower(obj, expect=str(obj))
def test_complex(self):
for obj in 0 + 0j, 1 + 0j, 0 + 1j, -2.5 + 3.5j:
self.check_lower(obj, expect=str(obj).lstrip("(").rstrip(")"))
def test_str(self):
for obj in "foo", "bar":
self.check_lower(obj, expect=obj)
def test_bytes(self):
for obj in b"foo", b"bar", "αβγ".encode():
self.check_lower(obj, expect="utf8:" + obj.decode("utf8"))
self.check_lower(bytes([0xC0, 0xC1, 0xF5]), expect="z`^w")
def test_date(self):
for obj in (
date.fromisoformat("2000-10-15"),
date.fromisoformat("2025-12-31"),
):
self.check_lower(obj, expect=obj.isoformat())
def test_time(self):
for obj in (
time.fromisoformat("10:32"),
time.fromisoformat("22:33"),
):
self.check_lower(obj, expect=obj.isoformat())
def test_datetime(self):
for obj in (
datetime.fromisoformat("2000-10-15 10:32"),
datetime.fromisoformat("2025-12-31 22:33"),
):
self.check_lower(obj, expect=obj.isoformat())
def test_list(self):
self.check_lower(["123", "abc", "xyz"], expect="123,abc,xyz")
self.check_lower([], expect="")
self.check_lower(["", ""], expect=",")
self.check_lower([""], expect="[]")
def test_dict(self):
self.check_lower(
{"a": "123", "b": "abc", "c": "xyz"}, expect="a=123,b=abc,c=xyz"
)
self.check_lower({}, expect="")
def test_union(self):
self.check_lower(_mapping.UnionValue("abc", "123"), expect="abc[123]")
self.check_lower(_mapping.UnionValue("abc", ""), expect="abc")
def test_optional(self):
self.check_lower(_mapping.OptionalValue("abc"), expect="abc")
self.check_lower(_mapping.OptionalValue("-"), expect="~-")
self.check_lower(_mapping.OptionalValue("~-"), expect="~~-")
self.check_lower(_mapping.OptionalValue("a-z"), expect="a-z")
self.check_lower(_mapping.OptionalValue(None), expect="-")
def test_load_dump(self):
self.check_load_dump(
"a=[x=1,y=2.5],b=[[abc=a,sub=[b=utf8:foo,greek=αβγ]],[abc=b,sub=[b=utf8:bar,greek=-]]],direction=Right[when=2025-07-27T09:06:40]"
)
## internals
def test_balance(self):
self.assertEqual(self.mod._balance("foo", "x"), (0, 0))
self.assertEqual(self.mod._balance("foo[bar", "o"), (1, 2))
self.assertEqual(self.mod._balance("foo[bar", "a"), (0, 1))
self.assertEqual(self.mod._balance("foo]bar", "x"), (1, 0))
self.assertEqual(self.mod._balance("[foobar]", "x"), (0, 0))
self.assertEqual(self.mod._balance("[foobar]", "a"), (0, 0))
self.assertEqual(self.mod._balance("[foo][bar]", "x"), (0, 0))
self.assertEqual(self.mod._balance("foo]bar]baz", "x"), (2, 0))
self.assertEqual(self.mod._balance("foo]bar]baz", "r"), (2, 0))
self.assertEqual(self.mod._balance("foo]bar]baz", "z"), (3, 1))
self.assertEqual(self.mod._balance("foo][bar", "x"), (1, 1))
def check_cover(self, s, chars):
hidden = self.mod._cover(s, chars)
self.assertEqual(self.mod._expose(hidden), s)
return hidden
def test_cover(self):
self.assertEqual(self.check_cover("foo", "o"), "[foo]")
self.assertEqual(self.check_cover("foo", "a"), "foo")
self.assertEqual(self.check_cover("[foo]", "o"), "~[foo]~")
self.assertEqual(self.check_cover("[foo", "o"), "~[foo~]")
self.assertEqual(self.check_cover("foo][bar", "o"), "[foo][bar]")
self.assertEqual(self.check_cover("foo]bar]baz", "o"), "[[~foo]bar]baz~")