summaryrefslogtreecommitdiff
path: root/_test/test_copy.py
blob: 37f978de2962d51db8c9295843ad5e590042e01e (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
117
118
# coding: utf-8

"""
Testing copy and deepcopy, instigated by Issue 84 (Peter Amstutz)
"""

import copy

import pytest            # NOQA
import ruamel.yaml       # NOQA

from roundtrip import dedent, round_trip_load, round_trip_dump


class TestDeepCopy:
    def test_preserve_flow_style_simple(self):
        x = dedent("""\
        {foo: bar, baz: quux}
        """)
        data = round_trip_load(x)
        data_copy = copy.deepcopy(data)
        y = round_trip_dump(data_copy)
        print('x [{}]'.format(x))
        print('y [{}]'.format(y))
        assert y == x
        assert data.fa.flow_style() == data_copy.fa.flow_style()

    def test_deepcopy_flow_style_nested_dict(self):
        x = dedent("""\
        a: {foo: bar, baz: quux}
        """)
        data = round_trip_load(x)
        assert data['a'].fa.flow_style() is True
        data_copy = copy.deepcopy(data)
        assert data_copy['a'].fa.flow_style() is True
        data_copy['a'].fa.set_block_style()
        assert data['a'].fa.flow_style() != data_copy['a'].fa.flow_style()
        assert data['a'].fa._flow_style is True
        assert data_copy['a'].fa._flow_style is False
        y = round_trip_dump(data_copy)

        print('x [{}]'.format(x))
        print('y [{}]'.format(y))
        assert y == dedent("""\
        a:
          foo: bar
          baz: quux
        """)

    def test_deepcopy_flow_style_nested_list(self):
        x = dedent("""\
        a: [1, 2, 3]
        """)
        data = round_trip_load(x)
        assert data['a'].fa.flow_style() is True
        data_copy = copy.deepcopy(data)
        assert data_copy['a'].fa.flow_style() is True
        data_copy['a'].fa.set_block_style()
        assert data['a'].fa.flow_style() != data_copy['a'].fa.flow_style()
        assert data['a'].fa._flow_style is True
        assert data_copy['a'].fa._flow_style is False
        y = round_trip_dump(data_copy)

        print('x [{}]'.format(x))
        print('y [{}]'.format(y))
        assert y == dedent("""\
        a:
        - 1
        - 2
        - 3
        """)


class TestCopy:
    def test_copy_flow_style_nested_dict(self):
        x = dedent("""\
        a: {foo: bar, baz: quux}
        """)
        data = round_trip_load(x)
        assert data['a'].fa.flow_style() is True
        data_copy = copy.copy(data)
        assert data_copy['a'].fa.flow_style() is True
        data_copy['a'].fa.set_block_style()
        assert data['a'].fa.flow_style() == data_copy['a'].fa.flow_style()
        assert data['a'].fa._flow_style is False
        assert data_copy['a'].fa._flow_style is False
        y = round_trip_dump(data_copy)
        z = round_trip_dump(data)
        assert y == z

        assert y == dedent("""\
        a:
          foo: bar
          baz: quux
        """)

    def test_copy_flow_style_nested_list(self):
        x = dedent("""\
        a: [1, 2, 3]
        """)
        data = round_trip_load(x)
        assert data['a'].fa.flow_style() is True
        data_copy = copy.copy(data)
        assert data_copy['a'].fa.flow_style() is True
        data_copy['a'].fa.set_block_style()
        assert data['a'].fa.flow_style() == data_copy['a'].fa.flow_style()
        assert data['a'].fa._flow_style is False
        assert data_copy['a'].fa._flow_style is False
        y = round_trip_dump(data_copy)

        print('x [{}]'.format(x))
        print('y [{}]'.format(y))
        assert y == dedent("""\
        a:
        - 1
        - 2
        - 3
        """)