Ejemplo n.º 1
0
def test_emitter_error(error_filename, verbose=False):
    events = list(yaml.load(open(error_filename, "rb"), Loader=test_emitter.EventsLoader))
    try:
        yaml.emit(events)
    except yaml.YAMLError, exc:
        if verbose:
            print "%s:" % exc.__class__.__name__, exc
Ejemplo n.º 2
0
def test_emitter_events(events_filename, verbose=False):
    events = list(yaml.load(open(events_filename, 'rb'), Loader=EventsLoader))
    output = yaml.emit(events)
    if verbose:
        print "OUTPUT:"
        print output
    new_events = list(yaml.parse(output))
    _compare_events(events, new_events)
Ejemplo n.º 3
0
def test_emitter_on_data(data_filename, canonical_filename, verbose=False):
    events = list(yaml.parse(open(data_filename, 'rb')))
    output = yaml.emit(events)
    if verbose:
        print "OUTPUT:"
        print output
    new_events = list(yaml.parse(output))
    _compare_events(events, new_events)
Ejemplo n.º 4
0
def test_emitter_on_canonical(canonical_filename, verbose=False):
    events = list(yaml.parse(open(canonical_filename, 'rb')))
    for canonical in [False, True]:
        output = yaml.emit(events, canonical=canonical)
        if verbose:
            print "OUTPUT (canonical=%s):" % canonical
            print output
        new_events = list(yaml.parse(output))
        _compare_events(events, new_events)
Ejemplo n.º 5
0
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 = yaml.emit(yaml.parse(input), allow_unicode=True)
        stream = StringIO.StringIO()
        yaml.emit(yaml.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')
Ejemplo n.º 6
0
def test_emitter_styles(data_filename, canonical_filename, verbose=False):
    for filename in [data_filename, canonical_filename]:
        events = list(yaml.parse(open(filename, 'rb')))
        for flow_style in [False, True]:
            for style in ['|', '>', '"', '\'', '']:
                styled_events = []
                for event in events:
                    if isinstance(event, yaml.ScalarEvent):
                        event = yaml.ScalarEvent(event.anchor, event.tag,
                                event.implicit, event.value, style=style)
                    elif isinstance(event, yaml.SequenceStartEvent):
                        event = yaml.SequenceStartEvent(event.anchor, event.tag,
                                event.implicit, flow_style=flow_style)
                    elif isinstance(event, yaml.MappingStartEvent):
                        event = yaml.MappingStartEvent(event.anchor, event.tag,
                                event.implicit, flow_style=flow_style)
                    styled_events.append(event)
                output = yaml.emit(styled_events)
                if verbose:
                    print "OUTPUT (filename=%r, flow_style=%r, style=%r)" % (filename, flow_style, style)
                    print output
                new_events = list(yaml.parse(output))
                _compare_events(events, new_events)