source: nagslang/tests/test_yamlish.py

Last change on this file was 689:45d2a6aef3a4, checked in by Neil Muller <drnlmuller@…>, 2 years ago

Some flake8 fixes

File size: 4.3 KB
Line 
1from __future__ import print_function
2
3from unittest import TestCase
4
5from nagslang.yamlish import load_s, dump_s
6
7try:
8 import yaml
9except ImportError:
10 yaml = None # pyflakes:ignore
11
12try:
13 from unittest import SkipTest
14except ImportError:
15 from pytest import skip
16 SkipTest = skip.Exception # pyflakes:ignore
17
18
19class TestParse(TestCase):
20 def assertParsesAs(self, text, expected):
21 self.assertEqual(load_s(text.strip()), expected)
22
23 def test_dict_list_1(self):
24 self.assertParsesAs('''
25foo:
26- bar
27- baz
28 ''', {'foo': ['bar', 'baz']})
29
30 def test_dict_list_2(self):
31 self.assertParsesAs('''
32foo:
33 - bar
34 - baz
35 ''', {'foo': ['bar', 'baz']})
36
37
38class TestRoundTrip(TestCase):
39 from_pyyaml = False
40
41 def roundtrip(self, data):
42 text = self.dump_s(data)
43 print('\n=== Begin ===\n%s\n=== End ===' % text)
44 self.assertEqual(self.load_s(text), data)
45
46 def dump_s(self, data):
47 return dump_s(data)
48
49 def load_s(self, text):
50 return load_s(text)
51
52 def test_simple_dict(self):
53 self.roundtrip({'foo': 'bar'})
54
55 def test_simple_list(self):
56 self.roundtrip(['foo', 'bar'])
57
58 def test_dict_of_dicts(self):
59 self.roundtrip({'foo': {'bar': 'baz'}})
60
61 def test_dict_tree(self):
62 self.roundtrip({
63 'foo': {
64 'bar': {
65 'baz': 'qux'
66 },
67 'quux': 'corge',
68 }
69 })
70
71 def test_list_of_lists(self):
72 self.roundtrip(['foo', ['bar', 'baz'], 'qux'])
73
74 def test_dict_list(self):
75 self.roundtrip({
76 'foo': ['bar', 'baz'],
77 })
78
79 def test_list_dict(self):
80 self.roundtrip([
81 {'foo': 'bar'},
82 {'baz': 'qux', 'quux': 'corge'},
83 ])
84
85 def test_nested_lists(self):
86 self.roundtrip({
87 'foo': [['bar', 'baz', 'qux'], 'quux'],
88 })
89
90 def test_list_of_dicts(self):
91 self.roundtrip({
92 'foo': [
93 {'bar': 'baz'},
94 {'qux': 'quux'},
95 ],
96 })
97
98 def test_int_dict(self):
99 self.roundtrip({
100 1: 'foo',
101 2: 'bar',
102 3: ['baz', 'qux'],
103 })
104
105 def test_dict_keys(self):
106 self.roundtrip({
107 True: 'true',
108 False: [],
109 None: {},
110 0.7: -0.7,
111 })
112
113 def test_dictish_string(self):
114 self.roundtrip({
115 'strings': [
116 'Foo: bar',
117 'Baz: qux',
118 ],
119 })
120
121 def test_tuples(self):
122 if self.from_pyyaml:
123 raise SkipTest("Can't parse PyYAML tuples")
124 orig = {
125 'polygons': {
126 1: [
127 (0, 1),
128 (2, 3),
129 ],
130 },
131 }
132 text = self.dump_s(orig)
133 result = self.load_s(text)
134 self.assertEqual(orig['polygons'][1][0],
135 tuple(result['polygons'][1][0]))
136 self.assertEqual(orig['polygons'][1][1],
137 tuple(result['polygons'][1][1]))
138
139 def test_dict_tuples(self):
140 if self.from_pyyaml:
141 raise SkipTest("Can't parse PyYAML tuples")
142 orig = {'tuple': (0, 1)}
143 text = self.dump_s(orig)
144 result = self.load_s(text)
145 self.assertEqual(orig['tuple'],
146 tuple(result['tuple']))
147
148 def test_quoted(self):
149 # a literal true is True, but 'true' is a string
150 self.roundtrip({'foo': 'true'})
151
152 def test_literals(self):
153 self.roundtrip({'foo': [True, False, None]})
154
155 def test_numeric(self):
156 self.roundtrip({'foo': [1, 2.0, -1, -2.5]})
157
158 def test_inline(self):
159 self.roundtrip([[1, 2, "hi, there' joe", '', "'"], [3, 4]])
160
161
162class TestFromPyYAML(TestRoundTrip):
163 from_pyyaml = True
164
165 def dump_s(self, data):
166 if yaml is None:
167 raise SkipTest('yaml module unavailable')
168 return yaml.dump(data, default_flow_style=False)
169
170
171class TestFromPyYAMLInlineLists(TestRoundTrip):
172 from_pyyaml = True
173
174 def dump_s(self, data):
175 if yaml is None:
176 raise SkipTest('yaml module unavailable')
177 return yaml.dump(data)
178
179
180class TestToPyYAML(TestRoundTrip):
181 def load_s(self, text):
182 if yaml is None:
183 raise SkipTest('yaml module unavailable')
184 return yaml.load(text)
Note: See TracBrowser for help on using the repository browser.