source: nagslang/tests/test_yamlish.py@ 137:c5ff16c66100

Last change on this file since 137:c5ff16c66100 was 137:c5ff16c66100, checked in by Stefano Rivera <stefano@…>, 7 years ago

Support other interesting keys, too

File size: 3.2 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 def roundtrip(self, data):
38 text = self.dump_s(data)
39 print '\n=== Begin ===\n%s\n=== End ===' % text
40 self.assertEqual(self.load_s(text), data)
41
42 def dump_s(self, data):
43 return dump_s(data)
44
45 def load_s(self, text):
46 return load_s(text)
47
48 def test_simple_dict(self):
49 self.roundtrip({'foo': 'bar'})
50
51 def test_simple_list(self):
52 self.roundtrip(['foo', 'bar'])
53
54 def test_dict_of_dicts(self):
55 self.roundtrip({'foo': {'bar': 'baz'}})
56
57 def test_dict_tree(self):
58 self.roundtrip({
59 'foo': {
60 'bar': {
61 'baz': 'qux'
62 },
63 'quux': 'corge',
64 }
65 })
66
67 def test_list_of_lists(self):
68 self.roundtrip(['foo', ['bar', 'baz'], 'qux'])
69
70 def test_dict_list(self):
71 self.roundtrip({
72 'foo': ['bar', 'baz'],
73 })
74
75 def test_list_dict(self):
76 self.roundtrip([
77 {'foo': 'bar'},
78 {'baz': 'qux', 'quux': 'corge'},
79 ])
80
81 def test_nested_lists(self):
82 self.roundtrip({
83 'foo': [['bar', 'baz', 'qux'], 'quux'],
84 })
85
86 def test_list_of_dicts(self):
87 self.roundtrip({
88 'foo': [
89 {'bar': 'baz'},
90 {'qux': 'quux'},
91 ],
92 })
93
94 def test_int_dict(self):
95 self.roundtrip({
96 1: 'foo',
97 2: 'bar',
98 3: ['baz', 'qux'],
99 })
100
101 def test_dict_keys(self):
102 self.roundtrip({
103 True: 'true',
104 False: [],
105 None: {},
106 0.7: -0.7,
107 })
108
109 def test_quoted(self):
110 # a literal true is True, but 'true' is a string
111 self.roundtrip({'foo': 'true'})
112
113 def test_literals(self):
114 self.roundtrip({'foo': [True, False, None]})
115
116 def test_numeric(self):
117 self.roundtrip({'foo': [1, 2.0, -1, -2.5]})
118
119 def test_inline(self):
120 self.roundtrip([[1, 2, "hi, there' joe", '', "'"], [3, 4]])
121
122
123class TestFromPyYAML(TestRoundTrip):
124 def dump_s(self, data):
125 if yaml is None:
126 raise SkipTest('yaml module unavailable')
127 return yaml.dump(data, default_flow_style=False)
128
129
130class TestFromPyYAMLInlineLists(TestRoundTrip):
131 def dump_s(self, data):
132 if yaml is None:
133 raise SkipTest('yaml module unavailable')
134 return yaml.dump(data)
135
136
137class TestToPyYAML(TestRoundTrip):
138 def load_s(self, text):
139 if yaml is None:
140 raise SkipTest('yaml module unavailable')
141 return yaml.load(text)
Note: See TracBrowser for help on using the repository browser.