summaryrefslogtreecommitdiff
path: root/_test/test_cyaml.py
blob: 056093b461ce9c61adf78e6b4b65f2fac84f0c11 (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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# coding: utf-8

import sys
import platform
import pytest  # type: ignore  # NOQA
from textwrap import dedent

NO_CLIB_VER = (3, 10)


@pytest.mark.skipif(  # type: ignore
    platform.python_implementation() in ['Jython', 'PyPy'],
    reason='Jython throws RepresenterError'
)
def test_load_cyaml() -> None:
    print("???????????????????????", platform.python_implementation())
    import ruamel.yaml

    if sys.version_info >= NO_CLIB_VER:
        return
    yaml = ruamel.yaml.YAML(typ='safe', pure=False)
    assert ruamel.yaml.__with_libyaml__

    yaml.load('abc: 1')


@pytest.mark.skipif(sys.version_info >= NO_CLIB_VER  # type: ignore
                    or platform.python_implementation() in ['Jython', 'PyPy'],
                    reason='no _PyGC_FINALIZED')
def test_dump_cyaml() -> None:
    import ruamel.yaml

    if sys.version_info >= NO_CLIB_VER:
        return
    data = {'a': 1, 'b': 2}
    yaml = ruamel.yaml.YAML(typ='safe', pure=False)
    yaml.default_flow_style = False
    yaml.allow_unicode = True
    buf = ruamel.yaml.compat.StringIO()
    yaml.dump(data, buf)
    assert buf.getvalue() == 'a: 1\nb: 2\n'


@pytest.mark.skipif(  # type: ignore
    platform.python_implementation() in ['Jython', 'PyPy'], reason='not avialable'
)
def test_load_cyaml_1_2() -> None:
    # issue 155
    import ruamel.yaml

    if sys.version_info >= NO_CLIB_VER:
        return
    assert ruamel.yaml.__with_libyaml__
    inp = dedent("""\
    %YAML 1.2
    ---
    num_epochs: 70000
    """)
    yaml = ruamel.yaml.YAML(typ='safe')
    yaml.load(inp)


@pytest.mark.skipif(  # type: ignore
    platform.python_implementation() in ['Jython', 'PyPy'], reason='not available'
)
def test_dump_cyaml_1_2() -> None:
    # issue 155
    import ruamel.yaml
    from ruamel.yaml.compat import StringIO

    if sys.version_info >= NO_CLIB_VER:
        return
    assert ruamel.yaml.__with_libyaml__
    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