Esempio n. 1
0
def test_emitter_error(error_filename, verbose=False):
    with open(error_filename, 'rb') as fp0:
        events = list(yaml.load(fp0, Loader=test_emitter.EventsLoader))
    try:
        yaml.emit(events)
    except yaml.YAMLError as exc:
        if verbose:
            print('%s:' % exc.__class__.__name__, exc)
    else:
        raise AssertionError('expected an exception')
Esempio n. 2
0
def test_emitter_on_data(data_filename, canonical_filename, verbose=False):
    with open(data_filename, 'rb') as fp0:
        events = list(yaml.parse(fp0))
    output = yaml.emit(events)
    if verbose:
        print('OUTPUT:')
        print(output)
    new_events = list(yaml.parse(output))
    _compare_events(events, new_events)
Esempio n. 3
0
def test_emitter_events(events_filename, verbose=False):
    with open(events_filename, 'rb') as fp0:
        events = list(yaml.load(fp0, Loader=EventsLoader))
    output = yaml.emit(events)
    if verbose:
        print('OUTPUT:')
        print(output)
    new_events = list(yaml.parse(output))
    _compare_events(events, new_events)
Esempio n. 4
0
def test_emitter_on_canonical(canonical_filename, verbose=False):
    with open(canonical_filename, 'rb') as fp0:
        events = list(yaml.parse(fp0))
    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)
Esempio n. 5
0
def test_unicode_transfer(unicode_filename, verbose=False):
    with open(unicode_filename, 'rb') as fp:
        data = fp.read().decode('utf-8')
    for encoding in [None, 'utf-8', 'utf-16-be', 'utf-16-le']:
        input = data
        if encoding is not None:
            input = ('\ufeff' + input).encode(encoding)
        output1 = yaml.emit(yaml.parse(input), allow_unicode=True)
        if encoding is None:
            stream = StringIO()
        else:
            stream = BytesIO()
        yaml.emit(yaml.parse(input), stream, allow_unicode=True)
        output2 = stream.getvalue()
        assert isinstance(output1, str), (type(output1), encoding)
        if encoding is None:
            assert isinstance(output2, str), (type(output1), encoding)
        else:
            assert isinstance(output2, bytes), (type(output1), encoding)
            output2.decode(encoding)
Esempio n. 6
0
def _compare_emitters(data, verbose):
    events = list(ruyaml.parse(data, Loader=ruyaml.PyLoader))
    c_data = ruyaml.emit(events, Dumper=ruyaml.CDumper)
    if verbose:
        print(c_data)
    py_events = list(ruyaml.parse(c_data, Loader=ruyaml.PyLoader))
    c_events = list(ruyaml.parse(c_data, Loader=ruyaml.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, '!']
                    and py_value in [None, '!']
                    and c_value in [None, '!']
                ):
                    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)
Esempio n. 7
0
def test_emitter_styles(data_filename, canonical_filename, verbose=False):
    for filename in [data_filename, canonical_filename]:
        with open(filename, 'rb') as fp0:
            events = list(yaml.parse(fp0))
        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)