def test_emitter_error(error_filename, verbose=False): events = list( hughml.load(open(error_filename, 'rb'), Loader=test_emitter.EventsLoader)) try: hughml.emit(events) except hughml.hughmlError, exc: if verbose: print "%s:" % exc.__class__.__name__, exc
def test_emitter_error(error_filename, verbose=False): events = list(hughml.load(open(error_filename, 'rb'), Loader=test_emitter.EventsLoader)) try: hughml.emit(events) except hughml.hughmlError as exc: if verbose: print("%s:" % exc.__class__.__name__, exc) else: raise AssertionError("expected an exception")
def test_emitter_styles(data_filename, canonical_filename, verbose=False): for filename in [data_filename, canonical_filename]: events = list(hughml.parse(open(filename, 'rb'))) for flow_style in [False, True]: for style in ['|', '>', '"', '\'', '']: styled_events = [] for event in events: if isinstance(event, hughml.ScalarEvent): event = hughml.ScalarEvent(event.anchor, event.tag, event.implicit, event.value, style=style) elif isinstance(event, hughml.SequenceStartEvent): event = hughml.SequenceStartEvent( event.anchor, event.tag, event.implicit, flow_style=flow_style) elif isinstance(event, hughml.MappingStartEvent): event = hughml.MappingStartEvent(event.anchor, event.tag, event.implicit, flow_style=flow_style) styled_events.append(event) output = hughml.emit(styled_events) if verbose: print "OUTPUT (filename=%r, flow_style=%r, style=%r)" % ( filename, flow_style, style) print output new_events = list(hughml.parse(output)) _compare_events(events, new_events)
def _compare_emitters(data, verbose): events = list(hughml.parse(data, Loader=hughml.PyLoader)) c_data = hughml.emit(events, Dumper=hughml.CDumper) if verbose: print c_data py_events = list(hughml.parse(c_data, Loader=hughml.PyLoader)) c_events = list(hughml.parse(c_data, Loader=hughml.CLoader)) try: assert len(events) == len(py_events), (len(events), len(py_events)) assert len(events) == len(c_events), (len(events), len(c_events)) for event, py_event, c_event in zip(events, py_events, c_events): for attribute in [ '__class__', 'anchor', 'tag', 'implicit', 'value', 'explicit', 'version', 'tags' ]: value = getattr(event, attribute, None) py_value = getattr(py_event, attribute, None) c_value = getattr(c_event, attribute, None) if attribute == 'tag' and value in [None, u'!'] \ and py_value in [None, u'!'] and c_value in [None, u'!']: continue if attribute == 'explicit' and (py_value or c_value): continue assert value == py_value, (event, py_event, attribute) assert value == c_value, (event, c_event, attribute) finally: if verbose: print "EVENTS:" pprint.pprint(events) print "PY_EVENTS:" pprint.pprint(py_events) print "C_EVENTS:" pprint.pprint(c_events)
def test_emitter_on_data(data_filename, canonical_filename, verbose=False): events = list(hughml.parse(open(data_filename, 'rb'))) output = hughml.emit(events) if verbose: print "OUTPUT:" print output new_events = list(hughml.parse(output)) _compare_events(events, new_events)
def test_emitter_on_canonical(canonical_filename, verbose=False): events = list(hughml.parse(open(canonical_filename, 'rb'))) for canonical in [False, True]: output = hughml.emit(events, canonical=canonical) if verbose: print "OUTPUT (canonical=%s):" % canonical print output new_events = list(hughml.parse(output)) _compare_events(events, new_events)
def test_emitter_events(events_filename, verbose=False): events = list(hughml.load(open(events_filename, 'rb'), Loader=EventsLoader)) output = hughml.emit(events) if verbose: print "OUTPUT:" print output new_events = list(hughml.parse(output)) _compare_events(events, new_events)
def test_unicode_transfer(unicode_filename, verbose=False): data = open(unicode_filename, 'rb').read().decode('utf-8') for encoding in [None, 'utf-8', 'utf-16-be', 'utf-16-le']: input = data if encoding is not None: input = (u'\ufeff' + input).encode(encoding) output1 = hughml.emit(hughml.parse(input), allow_unicode=True) stream = StringIO.StringIO() hughml.emit(hughml.parse(input), _unicode_open(stream, 'utf-8'), allow_unicode=True) output2 = stream.getvalue() if encoding is None: assert isinstance(output1, unicode), (type(output1), encoding) else: assert isinstance(output1, str), (type(output1), encoding) output1.decode(encoding) assert isinstance(output2, str), (type(output2), encoding) output2.decode('utf-8')