1import yaml 2import sys 3import pprint 4import math 5 6def check_bool(value, expected): 7 if expected == 'false()' and value is False: 8 return 1 9 if expected == 'true()' and value is True: 10 return 1 11 print(value) 12 print(expected) 13 return 0 14 15def check_int(value, expected): 16 if (int(expected) == value): 17 return 1 18 print(value) 19 print(expected) 20 return 0 21 22def check_float(value, expected): 23 if expected == 'inf()': 24 if value == math.inf: 25 return 1 26 elif expected == 'inf-neg()': 27 if value == -math.inf: 28 return 1 29 elif expected == 'nan()': 30 if math.isnan(value): 31 return 1 32 elif (float(expected) == value): 33 return 1 34 else: 35 print(value) 36 print(expected) 37 return 0 38 39def check_str(value, expected): 40 if value == expected: 41 return 1 42 print(value) 43 print(expected) 44 return 0 45 46 47def _fail(input, test): 48 print("Input: >>" + input + "<<") 49 print(test) 50 51# The tests/data/yaml11.schema file is copied from 52# https://github.com/perlpunk/yaml-test-schema/blob/master/data/schema-yaml11.yaml 53def test_implicit_resolver(data_filename, skip_filename, verbose=False): 54 types = { 55 'str': [str, check_str], 56 'int': [int, check_int], 57 'float': [float, check_float], 58 'inf': [float, check_float], 59 'nan': [float, check_float], 60 'bool': [bool, check_bool], 61 } 62 with open(skip_filename, 'rb') as file: 63 skipdata = yaml.load(file, Loader=yaml.SafeLoader) 64 skip_load = skipdata['load'] 65 skip_dump = skipdata['dump'] 66 if verbose: 67 print(skip_load) 68 with open(data_filename, 'rb') as file: 69 tests = yaml.load(file, Loader=yaml.SafeLoader) 70 71 i = 0 72 fail = 0 73 for i, (input, test) in enumerate(sorted(tests.items())): 74 if verbose: 75 print('-------------------- ' + str(i)) 76 77 # Skip known loader bugs 78 if input in skip_load: 79 continue 80 81 exp_type = test[0] 82 data = test[1] 83 exp_dump = test[2] 84 85 # Test loading 86 try: 87 loaded = yaml.safe_load(input) 88 except: 89 print("Error:", sys.exc_info()[0], '(', sys.exc_info()[1], ')') 90 fail+=1 91 _fail(input, test) 92 continue 93 94 if verbose: 95 print(input) 96 print(test) 97 print(loaded) 98 print(type(loaded)) 99 100 if exp_type == 'null': 101 if loaded is None: 102 pass 103 else: 104 fail+=1 105 _fail(input, test) 106 else: 107 t = types[exp_type][0] 108 code = types[exp_type][1] 109 if isinstance(loaded, t): 110 if code(loaded, data): 111 pass 112 else: 113 fail+=1 114 _fail(input, test) 115 else: 116 fail+=1 117 _fail(input, test) 118 119 # Skip known dumper bugs 120 if input in skip_dump: 121 continue 122 123 dump = yaml.safe_dump(loaded, explicit_end=False) 124 # strip trailing newlines and footers 125 if dump.endswith('\n...\n'): 126 dump = dump[:-5] 127 if dump.endswith('\n'): 128 dump = dump[:-1] 129 if dump == exp_dump: 130 pass 131 else: 132 print("Compare: >>" + dump + "<< >>" + exp_dump + "<<") 133 fail+=1 134 _fail(input, test) 135 136# if i >= 80: 137# break 138 139 if fail > 0: 140 print("Failed " + str(fail) + " / " + str(i) + " tests") 141 assert(False) 142 else: 143 print("Passed " + str(i) + " tests") 144 print("Skipped " + str(len(skip_load)) + " load tests") 145 print("Skipped " + str(len(skip_dump)) + " dump tests") 146 147test_implicit_resolver.unittest = ['.schema', '.schema-skip'] 148 149if __name__ == '__main__': 150 import test_appliance 151 test_appliance.run(globals()) 152 153