1a5f9918aSopenharmony_ci 2a5f9918aSopenharmony_ciimport yaml 3a5f9918aSopenharmony_ci 4a5f9918aSopenharmony_cidef _compare_events(events1, events2): 5a5f9918aSopenharmony_ci assert len(events1) == len(events2), (events1, events2) 6a5f9918aSopenharmony_ci for event1, event2 in zip(events1, events2): 7a5f9918aSopenharmony_ci assert event1.__class__ == event2.__class__, (event1, event2) 8a5f9918aSopenharmony_ci if isinstance(event1, yaml.NodeEvent): 9a5f9918aSopenharmony_ci assert event1.anchor == event2.anchor, (event1, event2) 10a5f9918aSopenharmony_ci if isinstance(event1, yaml.CollectionStartEvent): 11a5f9918aSopenharmony_ci assert event1.tag == event2.tag, (event1, event2) 12a5f9918aSopenharmony_ci if isinstance(event1, yaml.ScalarEvent): 13a5f9918aSopenharmony_ci if True not in event1.implicit+event2.implicit: 14a5f9918aSopenharmony_ci assert event1.tag == event2.tag, (event1, event2) 15a5f9918aSopenharmony_ci assert event1.value == event2.value, (event1, event2) 16a5f9918aSopenharmony_ci 17a5f9918aSopenharmony_cidef test_emitter_on_data(data_filename, canonical_filename, verbose=False): 18a5f9918aSopenharmony_ci with open(data_filename, 'rb') as file: 19a5f9918aSopenharmony_ci events = list(yaml.parse(file)) 20a5f9918aSopenharmony_ci output = yaml.emit(events) 21a5f9918aSopenharmony_ci if verbose: 22a5f9918aSopenharmony_ci print("OUTPUT:") 23a5f9918aSopenharmony_ci print(output) 24a5f9918aSopenharmony_ci new_events = list(yaml.parse(output)) 25a5f9918aSopenharmony_ci _compare_events(events, new_events) 26a5f9918aSopenharmony_ci 27a5f9918aSopenharmony_citest_emitter_on_data.unittest = ['.data', '.canonical'] 28a5f9918aSopenharmony_ci 29a5f9918aSopenharmony_cidef test_emitter_on_canonical(canonical_filename, verbose=False): 30a5f9918aSopenharmony_ci with open(canonical_filename, 'rb') as file: 31a5f9918aSopenharmony_ci events = list(yaml.parse(file)) 32a5f9918aSopenharmony_ci for canonical in [False, True]: 33a5f9918aSopenharmony_ci output = yaml.emit(events, canonical=canonical) 34a5f9918aSopenharmony_ci if verbose: 35a5f9918aSopenharmony_ci print("OUTPUT (canonical=%s):" % canonical) 36a5f9918aSopenharmony_ci print(output) 37a5f9918aSopenharmony_ci new_events = list(yaml.parse(output)) 38a5f9918aSopenharmony_ci _compare_events(events, new_events) 39a5f9918aSopenharmony_ci 40a5f9918aSopenharmony_citest_emitter_on_canonical.unittest = ['.canonical'] 41a5f9918aSopenharmony_ci 42a5f9918aSopenharmony_cidef test_emitter_styles(data_filename, canonical_filename, verbose=False): 43a5f9918aSopenharmony_ci for filename in [data_filename, canonical_filename]: 44a5f9918aSopenharmony_ci with open(filename, 'rb') as file: 45a5f9918aSopenharmony_ci events = list(yaml.parse(file)) 46a5f9918aSopenharmony_ci for flow_style in [False, True]: 47a5f9918aSopenharmony_ci for style in ['|', '>', '"', '\'', '']: 48a5f9918aSopenharmony_ci styled_events = [] 49a5f9918aSopenharmony_ci for event in events: 50a5f9918aSopenharmony_ci if isinstance(event, yaml.ScalarEvent): 51a5f9918aSopenharmony_ci event = yaml.ScalarEvent(event.anchor, event.tag, 52a5f9918aSopenharmony_ci event.implicit, event.value, style=style) 53a5f9918aSopenharmony_ci elif isinstance(event, yaml.SequenceStartEvent): 54a5f9918aSopenharmony_ci event = yaml.SequenceStartEvent(event.anchor, event.tag, 55a5f9918aSopenharmony_ci event.implicit, flow_style=flow_style) 56a5f9918aSopenharmony_ci elif isinstance(event, yaml.MappingStartEvent): 57a5f9918aSopenharmony_ci event = yaml.MappingStartEvent(event.anchor, event.tag, 58a5f9918aSopenharmony_ci event.implicit, flow_style=flow_style) 59a5f9918aSopenharmony_ci styled_events.append(event) 60a5f9918aSopenharmony_ci output = yaml.emit(styled_events) 61a5f9918aSopenharmony_ci if verbose: 62a5f9918aSopenharmony_ci print("OUTPUT (filename=%r, flow_style=%r, style=%r)" % (filename, flow_style, style)) 63a5f9918aSopenharmony_ci print(output) 64a5f9918aSopenharmony_ci new_events = list(yaml.parse(output)) 65a5f9918aSopenharmony_ci _compare_events(events, new_events) 66a5f9918aSopenharmony_ci 67a5f9918aSopenharmony_citest_emitter_styles.unittest = ['.data', '.canonical'] 68a5f9918aSopenharmony_ci 69a5f9918aSopenharmony_ciclass EventsLoader(yaml.Loader): 70a5f9918aSopenharmony_ci 71a5f9918aSopenharmony_ci def construct_event(self, node): 72a5f9918aSopenharmony_ci if isinstance(node, yaml.ScalarNode): 73a5f9918aSopenharmony_ci mapping = {} 74a5f9918aSopenharmony_ci else: 75a5f9918aSopenharmony_ci mapping = self.construct_mapping(node) 76a5f9918aSopenharmony_ci class_name = str(node.tag[1:])+'Event' 77a5f9918aSopenharmony_ci if class_name in ['AliasEvent', 'ScalarEvent', 'SequenceStartEvent', 'MappingStartEvent']: 78a5f9918aSopenharmony_ci mapping.setdefault('anchor', None) 79a5f9918aSopenharmony_ci if class_name in ['ScalarEvent', 'SequenceStartEvent', 'MappingStartEvent']: 80a5f9918aSopenharmony_ci mapping.setdefault('tag', None) 81a5f9918aSopenharmony_ci if class_name in ['SequenceStartEvent', 'MappingStartEvent']: 82a5f9918aSopenharmony_ci mapping.setdefault('implicit', True) 83a5f9918aSopenharmony_ci if class_name == 'ScalarEvent': 84a5f9918aSopenharmony_ci mapping.setdefault('implicit', (False, True)) 85a5f9918aSopenharmony_ci mapping.setdefault('value', '') 86a5f9918aSopenharmony_ci value = getattr(yaml, class_name)(**mapping) 87a5f9918aSopenharmony_ci return value 88a5f9918aSopenharmony_ci 89a5f9918aSopenharmony_ciEventsLoader.add_constructor(None, EventsLoader.construct_event) 90a5f9918aSopenharmony_ci 91a5f9918aSopenharmony_cidef test_emitter_events(events_filename, verbose=False): 92a5f9918aSopenharmony_ci with open(events_filename, 'rb') as file: 93a5f9918aSopenharmony_ci events = list(yaml.load(file, Loader=EventsLoader)) 94a5f9918aSopenharmony_ci output = yaml.emit(events) 95a5f9918aSopenharmony_ci if verbose: 96a5f9918aSopenharmony_ci print("OUTPUT:") 97a5f9918aSopenharmony_ci print(output) 98a5f9918aSopenharmony_ci new_events = list(yaml.parse(output)) 99a5f9918aSopenharmony_ci _compare_events(events, new_events) 100a5f9918aSopenharmony_ci 101a5f9918aSopenharmony_ciif __name__ == '__main__': 102a5f9918aSopenharmony_ci import test_appliance 103a5f9918aSopenharmony_ci test_appliance.run(globals()) 104a5f9918aSopenharmony_ci 105