source: nagslang/tests/test_yamlish.py@ 380:a106d7e1415b

Last change on this file since 380:a106d7e1415b was 380:a106d7e1415b, checked in by Stefano Rivera <stefano@…>, 8 years ago

Colons are allowed in strings

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