summaryrefslogtreecommitdiff
path: root/_test/test_cyaml.py
blob: f6b54d5d9261556629bf279a9b21a24dbdf7b5d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# coding: utf-8

import platform
import pytest
from textwrap import dedent


@pytest.mark.skipif(
    platform.python_implementation() == 'Jython', reason='Jython throws RepresenterError'
)
def test_load_cyaml():
    import ruamel.yaml

    assert ruamel.yaml.__with_libyaml__
    from ruamel.yaml.cyaml import CLoader

    ruamel.yaml.load('abc: 1', Loader=CLoader)


def test_dump_cyaml():
    import ruamel.yaml

    data = {'a': 1, 'b': 2}
    res = ruamel.yaml.dump(
        data,
        Dumper=ruamel.yaml.cyaml.CSafeDumper,
        default_flow_style=False,
        allow_unicode=True,
    )
    assert res == 'a: 1\nb: 2\n'

@pytest.mark.xfail(strict=True)
def test_load_cyaml_1_2():
    # issue 155
    import ruamel.yaml

    assert ruamel.yaml.__with_libyaml__
    inp = dedent("""\
    %YAML 1.2
    ---
    num_epochs: 70000
    """)
    yaml = ruamel.yaml.YAML(typ='safe')
    yaml.load(inp)

@pytest.mark.xfail(strict=True)
def test_dump_cyaml_1_2():
    # issue 155
    import ruamel.yaml
    from ruamel.yaml.compat import StringIO

    assert ruamel.yaml.__with_libyaml__
    inp = dedent("""\
    %YAML 1.2
    ---
    num_epochs: 70000
    """)
    yaml = ruamel.yaml.YAML(typ='safe')
    yaml.version = (1, 2)
    yaml.default_flow_style = False
    data = {'a': 1, 'b': 2}
    exp = dedent("""\
    %YAML 1.2
    ---
    a: 1
    b: 2
    """)
    buf = StringIO()
    yaml.dump(data, buf)
    assert buf.getvalue() == exp