-
Notifications
You must be signed in to change notification settings - Fork 5
/
test.py
executable file
·86 lines (62 loc) · 2.16 KB
/
test.py
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
#!/usr/bin/env python
import unittest
from flattery import flatten, unflatten
class FunctionTestCase(unittest.TestCase):
def __init__(self, **keywords):
unittest.TestCase.__init__(self)
self.args = []
self.kwargs = {}
for k, v in keywords.items():
setattr(self, k, v)
def runTest(self):
self.assertEqual( self.func(*self.args,**self.kwargs), self.expected )
def shortDescription(self):
return self.name
# TODO add more coverage
# TODO add bi-directional tests of flatten()
TEST_UNFLATTEN = 0x1
TEST_FLATTEN = 0x2
TEST_BOTH = TEST_UNFLATTEN | TEST_FLATTEN
TRUTH = [
("empty", TEST_BOTH, {}, {}),
("empty key", TEST_BOTH, {'':1}, {'':1}),
("depth 2 path x 1", TEST_BOTH, {"a.b":42}, {"a":{"b":42}}),
("depth 2 path x 2", TEST_BOTH, {"a.b":42,"c.d":"x"}, {"a":{"b":42},"c":{"d":"x"}}),
("depth 2 path x 2, overlap", TEST_BOTH, {"a.b":42,"a.c":"x"}, {"a":{"b":42,"c":"x"}}),
("simple list", TEST_BOTH, {"a.0":1,"a.1":2}, {"a":[1,2]}),
("sparse list", TEST_BOTH, {"a.0":1,"a.9":10}, {"a":[1,None,None,None,None,None,None,None,None,10]}),
("deep dict", TEST_BOTH, {"a.b.c.d.e":1}, {"a":{"b":{"c":{"d":{"e":1}}}}}),
("list under dict", TEST_BOTH, {"a.b.0":1,"a.b.1":2}, {"a":{"b":[1,2]}}),
("dict under list x 1", TEST_BOTH, {"a.0.b":1}, {"a":[{"b":1}]}),
("dict under list x 2, overlap", TEST_BOTH, {"a.0.b":1,"a.0.c":2}, {"a":[{"b":1,"c":2}]}),
("deep list", TEST_BOTH, {"a.0.0.0.0":42}, {"a":[[[[42]]]]}),
]
def main():
suite = unittest.TestSuite()
i = 0
for name, mode, flat, unflat in TRUTH:
if mode & TEST_FLATTEN:
suite.addTest(FunctionTestCase(
name="test %d flatten %s" % (i,name),
func=flatten,
args=[unflat],
expected=flat,
))
i += 1
if mode & TEST_UNFLATTEN:
suite.addTest(FunctionTestCase(
name="test %d unflatten %s" % (i,name),
func=unflatten,
args=[flat],
expected=unflat,
))
i += 1
runner = unittest.TextTestRunner(verbosity=2)
results = runner.run(suite)
if len(results.failures) or len(results.errors):
return 1
else:
return 0
if __name__ == '__main__':
import sys
sys.exit(main())