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
|
# coding: utf-8
from __future__ import print_function
"""
testing of anchors and the aliases referring to them
"""
import sys
import pytest
from ruamel.yaml import YAML
from ruamel.yaml.constructor import DuplicateKeyError
from ruamel.std.pathlib import Path
class TestNewAPI:
def test_duplicate_keys_00(self):
from ruamel.yaml.constructor import DuplicateKeyError
yaml = YAML()
with pytest.raises(DuplicateKeyError):
yaml.load('{a: 1, a: 2}')
def test_duplicate_keys_01(self):
yaml = YAML(typ='safe', pure=True)
with pytest.raises(DuplicateKeyError):
yaml.load('{a: 1, a: 2}')
# @pytest.mark.xfail(strict=True)
def test_duplicate_keys_02(self):
yaml = YAML(typ='safe')
with pytest.raises(DuplicateKeyError):
yaml.load('{a: 1, a: 2}')
class TestWrite:
def test_dump_path(self, tmpdir):
fn = Path(str(tmpdir)) / 'test.yaml'
yaml = YAML()
data = yaml.map()
data['a'] = 1
data['b'] = 2
yaml.dump(data, fn)
assert fn.read_text() == "a: 1\nb: 2\n"
def test_dump_file(self, tmpdir):
fn = Path(str(tmpdir)) / 'test.yaml'
yaml = YAML()
data = yaml.map()
data['a'] = 1
data['b'] = 2
with open(str(fn), 'w') as fp:
yaml.dump(data, fp)
assert fn.read_text() == "a: 1\nb: 2\n"
def test_dump_missing_stream(self):
yaml = YAML()
data = yaml.map()
data['a'] = 1
data['b'] = 2
with pytest.raises(TypeError):
yaml.dump(data)
def test_dump_too_many_args(self, tmpdir):
fn = Path(str(tmpdir)) / 'test.yaml'
yaml = YAML()
data = yaml.map()
data['a'] = 1
data['b'] = 2
with pytest.raises(TypeError):
yaml.dump(data, fn, True)
def test_transform(self, tmpdir):
def tr(s):
return s.replace(' ', ' ')
fn = Path(str(tmpdir)) / 'test.yaml'
yaml = YAML()
data = yaml.map()
data['a'] = 1
data['b'] = 2
yaml.dump(data, fn, transform=tr)
assert fn.read_text() == "a: 1\nb: 2\n"
def test_print(self, capsys):
yaml = YAML()
data = yaml.map()
data['a'] = 1
data['b'] = 2
yaml.dump(data, sys.stdout)
out, err = capsys.readouterr()
assert out == "a: 1\nb: 2\n"
|