summaryrefslogtreecommitdiffstats
path: root/_test/test_contextmanager.py
blob: 4539614bbdca5f58d1c3dee254acbf11cb80342c (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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# coding: utf-8

"""
testing of anchors and the aliases referring to them
"""

import sys

import pytest

single_doc = """\
- a: 1
- b:
  - 2
  - 3
"""

single_data = [dict(a=1), dict(b=[2, 3])]

multi_doc = """\
---
- abc
- xyz
---
- a: 1
- b:
  - 2
  - 3
"""

multi_doc_data = [['abc', 'xyz'], single_data]


def get_yaml():
    from ruyaml import YAML

    return YAML()


class TestOldStyle:
    def test_single_load(self):
        d = get_yaml().load(single_doc)
        print(d)
        print(type(d[0]))
        assert d == single_data

    def test_single_load_no_arg(self):
        with pytest.raises(TypeError):
            assert get_yaml().load() == single_data

    def test_multi_load(self):
        data = list(get_yaml().load_all(multi_doc))
        assert data == multi_doc_data

    def test_single_dump(self, capsys):
        get_yaml().dump(single_data, sys.stdout)
        out, err = capsys.readouterr()
        assert out == single_doc

    def test_multi_dump(self, capsys):
        yaml = get_yaml()
        yaml.explicit_start = True
        yaml.dump_all(multi_doc_data, sys.stdout)
        out, err = capsys.readouterr()
        assert out == multi_doc


class TestContextManager:
    def test_single_dump(self, capsys):
        from ruyaml import YAML

        with YAML(output=sys.stdout) as yaml:
            yaml.dump(single_data)
        out, err = capsys.readouterr()
        print(err)
        assert out == single_doc

    def test_multi_dump(self, capsys):
        from ruyaml import YAML

        with YAML(output=sys.stdout) as yaml:
            yaml.explicit_start = True
            yaml.dump(multi_doc_data[0])
            yaml.dump(multi_doc_data[1])

        out, err = capsys.readouterr()
        print(err)
        assert out == multi_doc

    # input is not as simple with a context manager
    # you need to indicate what you expect hence load and load_all

    # @pytest.mark.xfail(strict=True)
    # def test_single_load(self):
    #     from ruyaml import YAML
    #     with YAML(input=single_doc) as yaml:
    #         assert yaml.load() == single_data
    #
    # @pytest.mark.xfail(strict=True)
    # def test_multi_load(self):
    #     from ruyaml import YAML
    #     with YAML(input=multi_doc) as yaml:
    #         for idx, data in enumerate(yaml.load()):
    #             assert data == multi_doc_data[0]

    def test_roundtrip(self, capsys):
        from ruyaml import YAML

        with YAML(output=sys.stdout) as yaml:
            yaml.explicit_start = True
            for data in yaml.load_all(multi_doc):
                yaml.dump(data)

        out, err = capsys.readouterr()
        print(err)
        assert out == multi_doc