/
test_schema.py
152 lines (132 loc) · 3.82 KB
/
test_schema.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
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
import yaml
import sys
import pprint
import math
def check_bool(value, expected):
if expected == 'false()' and value is False:
return 1
if expected == 'true()' and value is True:
return 1
print(value)
print(expected)
return 0
def check_int(value, expected):
if (int(expected) == value):
return 1
print(value)
print(expected)
return 0
def check_float(value, expected):
if expected == 'inf()':
if value == math.inf:
return 1
elif expected == 'inf-neg()':
if value == -math.inf:
return 1
elif expected == 'nan()':
if math.isnan(value):
return 1
elif (float(expected) == value):
return 1
else:
print(value)
print(expected)
return 0
def check_str(value, expected):
if value == expected:
return 1
print(value)
print(expected)
return 0
def _fail(input, test):
print("Input: >>" + input + "<<")
print(test)
# The tests/data/yaml11.schema file is copied from
# https://github.com/perlpunk/yaml-test-schema/blob/master/data/schema-yaml11.yaml
def test_implicit_resolver(data_filename, skip_filename, verbose=False):
types = {
'str': [str, check_str],
'int': [int, check_int],
'float': [float, check_float],
'inf': [float, check_float],
'nan': [float, check_float],
'bool': [bool, check_bool],
}
with open(skip_filename, 'rb') as file:
skipdata = yaml.load(file)
skip_load = skipdata['load']
skip_dump = skipdata['dump']
if verbose:
print(skip_load)
with open(data_filename, 'rb') as file:
tests = yaml.load(file)
i = 0
fail = 0
for i, (input, test) in enumerate(sorted(tests.items())):
if verbose:
print('-------------------- ' + str(i))
# Skip known loader bugs
if input in skip_load:
continue
exp_type = test[0]
data = test[1]
exp_dump = test[2]
# Test loading
try:
loaded = yaml.safe_load(input)
except:
print("Error:", sys.exc_info()[0], '(', sys.exc_info()[1], ')')
fail+=1
_fail(input, test)
continue
if verbose:
print(input)
print(test)
print(loaded)
print(type(loaded))
if exp_type == 'null':
if loaded is None:
pass
else:
fail+=1
_fail(input, test)
else:
t = types[exp_type][0]
code = types[exp_type][1]
if isinstance(loaded, t):
if code(loaded, data):
pass
else:
fail+=1
_fail(input, test)
else:
fail+=1
_fail(input, test)
# Skip known dumper bugs
if input in skip_dump:
continue
dump = yaml.safe_dump(loaded, explicit_end=False)
# strip trailing newlines and footers
if dump.endswith('\n...\n'):
dump = dump[:-5]
if dump.endswith('\n'):
dump = dump[:-1]
if dump == exp_dump:
pass
else:
print("Compare: >>" + dump + "<< >>" + exp_dump + "<<")
fail+=1
_fail(input, test)
# if i >= 80:
# break
if fail > 0:
print("Failed " + str(fail) + " / " + str(i) + " tests")
assert(False)
else:
print("Passed " + str(i) + " tests")
print("Skipped " + str(len(skip_load)) + " load tests")
print("Skipped " + str(len(skip_dump)) + " dump tests")
test_implicit_resolver.unittest = ['.schema', '.schema-skip']
if __name__ == '__main__':
import test_appliance
test_appliance.run(globals())