source: nagslang/tests/test_yamlish.py@ 138:366b334a7018

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

Render tuples to lists

File size: 3.8 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_tuples(self):
112 if self.from_pyyaml:
113 raise SkipTest("Can't parse PyYAML tuples")
114 orig = {
115 'polygons': {
116 1: [
117 (0, 1),
118 (2, 3),
119 ],
120 },
121 }
122 text = self.dump_s(orig)
123 result = self.load_s(text)
124 self.assertEqual(orig['polygons'][1][0],
125 tuple(result['polygons'][1][0]))
126 self.assertEqual(orig['polygons'][1][1],
127 tuple(result['polygons'][1][1]))
128
129 def test_quoted(self):
130 # a literal true is True, but 'true' is a string
131 self.roundtrip({'foo': 'true'})
132
133 def test_literals(self):
134 self.roundtrip({'foo': [True, False, None]})
135
136 def test_numeric(self):
137 self.roundtrip({'foo': [1, 2.0, -1, -2.5]})
138
139 def test_inline(self):
140 self.roundtrip([[1, 2, "hi, there' joe", '', "'"], [3, 4]])
141
142
143class TestFromPyYAML(TestRoundTrip):
144 from_pyyaml = True
145
146 def dump_s(self, data):
147 if yaml is None:
148 raise SkipTest('yaml module unavailable')
149 return yaml.dump(data, default_flow_style=False)
150
151
152class TestFromPyYAMLInlineLists(TestRoundTrip):
153 from_pyyaml = True
154
155 def dump_s(self, data):
156 if yaml is None:
157 raise SkipTest('yaml module unavailable')
158 return yaml.dump(data)
159
160
161class TestToPyYAML(TestRoundTrip):
162 def load_s(self, text):
163 if yaml is None:
164 raise SkipTest('yaml module unavailable')
165 return yaml.load(text)
Note: See TracBrowser for help on using the repository browser.