1- import sys
21from collections import OrderedDict
32from types import GeneratorType
43
54import pytest
5+ from yaml .representer import RepresenterError
66
77import oyaml as yaml
8+ from oyaml import _std_dict_is_order_preserving
89
910
10- data = OrderedDict ([('x' , 1 ), ('z' , 3 ), ('y' , 2 )])
11-
12-
13- # this release was pulled from index, but still might be seen in the wild
14- pyyaml_41 = yaml .pyyaml .__version__ == '4.1'
11+ data = OrderedDict ([("x" , 1 ), ("z" , 3 ), ("y" , 2 )])
1512
1613
1714def test_dump ():
18- assert yaml .dump (data ) == ' {x: 1, z: 3, y: 2}\n '
15+ assert yaml .dump (data , default_flow_style = None ) == " {x: 1, z: 3, y: 2}\n "
1916
2017
2118def test_safe_dump ():
22- assert yaml .safe_dump (data ) == '{x: 1, z: 3, y: 2}\n '
23-
24-
25- @pytest .mark .skipif (not pyyaml_41 , reason = "requires PyYAML version == 4.1" )
26- def test_danger_dump ():
27- assert yaml .danger_dump (data ) == '{x: 1, z: 3, y: 2}\n '
19+ assert yaml .safe_dump (data , default_flow_style = None ) == "{x: 1, z: 3, y: 2}\n "
2820
2921
3022def test_dump_all ():
31- assert yaml .dump_all (documents = [data , {}]) == '{x: 1, z: 3, y: 2}\n --- {}\n '
23+ assert (
24+ yaml .dump_all (documents = [data , {}], default_flow_style = None )
25+ == "{x: 1, z: 3, y: 2}\n --- {}\n "
26+ )
3227
3328
34- @pytest .mark .skipif (pyyaml_41 , reason = "requires PyYAML version != 4.1" )
3529def test_dump_and_safe_dump_match ():
36- mydict = {'x' : 1 , 'z' : 2 , 'y' : 3 }
30+ mydict = {"x" : 1 , "z" : 2 , "y" : 3 }
3731 # don't know if mydict is ordered in the implementation or not (but don't care)
3832 assert yaml .dump (mydict ) == yaml .safe_dump (mydict )
3933
4034
41- @pytest .mark .skipif (not pyyaml_41 , reason = "requires PyYAML version == 4.1" )
42- def test_danger_dump_and_safe_dump_match ():
43- mydict = {'x' : 1 , 'z' : 2 , 'y' : 3 }
44- assert yaml .danger_dump (mydict ) == yaml .safe_dump (mydict )
45-
46-
4735def test_safe_dump_all ():
48- assert yaml .safe_dump_all (documents = [data , {}]) == '{x: 1, z: 3, y: 2}\n --- {}\n '
36+ assert (
37+ yaml .safe_dump_all (documents = [data , {}], default_flow_style = None )
38+ == "{x: 1, z: 3, y: 2}\n --- {}\n "
39+ )
4940
5041
5142def test_load ():
52- loaded = yaml .load (' {x: 1, z: 3, y: 2}' )
53- assert loaded == {'x' : 1 , 'z' : 3 , 'y' : 2 }
43+ loaded = yaml .load (" {x: 1, z: 3, y: 2}" )
44+ assert loaded == {"x" : 1 , "z" : 3 , "y" : 2 }
5445
5546
5647def test_safe_load ():
57- loaded = yaml .safe_load (' {x: 1, z: 3, y: 2}' )
58- assert loaded == {'x' : 1 , 'z' : 3 , 'y' : 2 }
48+ loaded = yaml .safe_load (" {x: 1, z: 3, y: 2}" )
49+ assert loaded == {"x" : 1 , "z" : 3 , "y" : 2 }
5950
6051
6152def test_load_all ():
62- gen = yaml .load_all (' {x: 1, z: 3, y: 2}\n --- {}\n ' )
53+ gen = yaml .load_all (" {x: 1, z: 3, y: 2}\n --- {}\n " )
6354 assert isinstance (gen , GeneratorType )
6455 ordered_data , empty_dict = gen
6556 assert empty_dict == {}
6657 assert ordered_data == data
6758
6859
69- @pytest .mark .skipif (sys . version_info >= ( 3 , 7 ), reason = "requires python3.6- " )
60+ @pytest .mark .skipif (_std_dict_is_order_preserving , reason = "requires old dict impl " )
7061def test_loads_to_ordered_dict ():
71- loaded = yaml .load (' {x: 1, z: 3, y: 2}' )
62+ loaded = yaml .load (" {x: 1, z: 3, y: 2}" )
7263 assert isinstance (loaded , OrderedDict )
7364
7465
75- @pytest .mark .skipif (sys . version_info < ( 3 , 7 ), reason = "requires python3.7+ " )
66+ @pytest .mark .skipif (not _std_dict_is_order_preserving , reason = "requires new dict impl " )
7667def test_loads_to_std_dict ():
77- loaded = yaml .load (' {x: 1, z: 3, y: 2}' )
68+ loaded = yaml .load (" {x: 1, z: 3, y: 2}" )
7869 assert not isinstance (loaded , OrderedDict )
7970 assert isinstance (loaded , dict )
8071
8172
82- @pytest .mark .skipif (sys . version_info >= ( 3 , 7 ), reason = "requires python3.6- " )
73+ @pytest .mark .skipif (_std_dict_is_order_preserving , reason = "requires old dict impl " )
8374def test_safe_loads_to_ordered_dict ():
84- loaded = yaml .safe_load (' {x: 1, z: 3, y: 2}' )
75+ loaded = yaml .safe_load (" {x: 1, z: 3, y: 2}" )
8576 assert isinstance (loaded , OrderedDict )
8677
8778
88- @pytest .mark .skipif (sys . version_info < ( 3 , 7 ), reason = "requires python3.7+ " )
79+ @pytest .mark .skipif (not _std_dict_is_order_preserving , reason = "requires new dict impl " )
8980def test_safe_loads_to_std_dict ():
90- loaded = yaml .safe_load (' {x: 1, z: 3, y: 2}' )
81+ loaded = yaml .safe_load (" {x: 1, z: 3, y: 2}" )
9182 assert not isinstance (loaded , OrderedDict )
9283 assert isinstance (loaded , dict )
9384
@@ -96,24 +87,15 @@ class MyOrderedDict(OrderedDict):
9687 pass
9788
9889
99- @pytest .mark .skipif (pyyaml_41 , reason = "requires PyYAML version != 4.1" )
100- def test_subclass_dump_pyyaml3 ():
101- data = MyOrderedDict ([('x' , 1 ), ('y' , 2 )])
102- assert '!!python/object/apply:test_oyaml.MyOrderedDict' in yaml .dump (data )
103- with pytest .raises (yaml .pyyaml .representer .RepresenterError , match = 'cannot represent an object' ) as cm :
90+ def test_subclass_dump ():
91+ data = MyOrderedDict ([("x" , 1 ), ("y" , 2 )])
92+ assert "!!python/object/apply:test_oyaml.MyOrderedDict" in yaml .dump (data )
93+ with pytest .raises (RepresenterError , match = "cannot represent an object" ):
10494 yaml .safe_dump (data )
10595
10696
107- @pytest .mark .skipif (not pyyaml_41 , reason = "requires PyYAML version == 4.1" )
108- def test_subclass_dump_pyyaml4 ():
109- data = MyOrderedDict ([('x' , 1 ), ('y' , 2 )])
110- assert '!!python/object/apply:test_oyaml.MyOrderedDict' in yaml .danger_dump (data )
111- with pytest .raises (yaml .pyyaml .representer .RepresenterError , match = 'cannot represent an object' ) as cm :
112- yaml .dump (data )
113-
114-
11597def test_anchors_and_references ():
116- text = '''
98+ text = """
11799 defaults:
118100 all: &all
119101 product: foo
@@ -125,53 +107,46 @@ def test_anchors_and_references():
125107 platform:
126108 <<: *development
127109 host: baz
128- '''
110+ """
129111 expected_load = {
130- 'defaults' : {
131- 'all' : {
132- 'product' : 'foo' ,
133- },
134- 'development' : {
135- 'product' : 'foo' ,
136- 'profile' : 'bar' ,
137- },
112+ "defaults" : {
113+ "all" : {"product" : "foo" },
114+ "development" : {"product" : "foo" , "profile" : "bar" },
138115 },
139- 'development' : {
140- 'platform' : {
141- 'host' : 'baz' ,
142- 'product' : 'foo' ,
143- 'profile' : 'bar' ,
144- },
116+ "development" : {
117+ "platform" : {"host" : "baz" , "product" : "foo" , "profile" : "bar" }
145118 },
146119 }
147120 assert yaml .load (text ) == expected_load
148121
149122
150123def test_omap ():
151- text = '''
124+ text = """
152125 Bestiary: !!omap
153126 - aardvark: African pig-like ant eater. Ugly.
154127 - anteater: South-American ant eater. Two species.
155128 - anaconda: South-American constrictor snake. Scaly.
156- '''
129+ """
157130 expected_load = {
158- 'Bestiary' : ([
159- ('aardvark' , 'African pig-like ant eater. Ugly.' ),
160- ('anteater' , 'South-American ant eater. Two species.' ),
161- ('anaconda' , 'South-American constrictor snake. Scaly.' ),
162- ])
131+ "Bestiary" : (
132+ [
133+ ("aardvark" , "African pig-like ant eater. Ugly." ),
134+ ("anteater" , "South-American ant eater. Two species." ),
135+ ("anaconda" , "South-American constrictor snake. Scaly." ),
136+ ]
137+ )
163138 }
164139 assert yaml .load (text ) == expected_load
165140
166141
167142def test_omap_flow_style ():
168- text = ' Numbers: !!omap [ one: 1, two: 2, three : 3 ]'
169- expected_load = {' Numbers' : ([(' one' , 1 ), (' two' , 2 ), (' three' , 3 )])}
143+ text = " Numbers: !!omap [ one: 1, two: 2, three : 3 ]"
144+ expected_load = {" Numbers" : ([(" one" , 1 ), (" two" , 2 ), (" three" , 3 )])}
170145 assert yaml .load (text ) == expected_load
171146
172147
173148def test_merge ():
174- text = '''
149+ text = """
175150 - &CENTER { x: 1, y: 2 }
176151 - &LEFT { x: 0, y: 2 }
177152 - &BIG { r: 10 }
@@ -198,15 +173,15 @@ def test_merge():
198173 << : [ *BIG, *LEFT, *SMALL ]
199174 x: 1
200175 label: center/big
201- '''
176+ """
202177 data = yaml .load (text )
203178 assert len (data ) == 8
204179 center , left , big , small , map1 , map2 , map3 , map4 = data
205- assert center == {'x' : 1 , 'y' : 2 }
206- assert left == {'x' : 0 , 'y' : 2 }
207- assert big == {'r' : 10 }
208- assert small == {'r' : 1 }
209- expected = {'x' : 1 , 'y' : 2 , 'r' : 10 , ' label' : ' center/big' }
180+ assert center == {"x" : 1 , "y" : 2 }
181+ assert left == {"x" : 0 , "y" : 2 }
182+ assert big == {"r" : 10 }
183+ assert small == {"r" : 1 }
184+ expected = {"x" : 1 , "y" : 2 , "r" : 10 , " label" : " center/big" }
210185 assert map1 == expected
211186 assert map2 == expected
212187 assert map3 == expected
0 commit comments