1import yaml 2import pprint 3import sys 4 5def test_sort_keys(input_filename, sorted_filename, verbose=False): 6 with open(input_filename, 'rb') as file: 7 input = file.read().decode('utf-8') 8 with open(sorted_filename, 'rb') as file: 9 sorted = file.read().decode('utf-8') 10 data = yaml.load(input, Loader=yaml.FullLoader) 11 dump_sorted = yaml.dump(data, default_flow_style=False, sort_keys=True) 12 dump_unsorted = yaml.dump(data, default_flow_style=False, sort_keys=False) 13 dump_unsorted_safe = yaml.dump(data, default_flow_style=False, sort_keys=False, Dumper=yaml.SafeDumper) 14 if verbose: 15 print("INPUT:") 16 print(input) 17 print("DATA:") 18 print(data) 19 20 assert dump_sorted == sorted 21 22 if sys.version_info>=(3,7): 23 assert dump_unsorted == input 24 assert dump_unsorted_safe == input 25 26test_sort_keys.unittest = ['.sort', '.sorted'] 27 28if __name__ == '__main__': 29 import test_appliance 30 test_appliance.run(globals()) 31 32