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@…>, 8 years ago

Support other interesting keys, too

File size: 3.2 KB
RevLine 
[114]1from unittest import TestCase
2
3from nagslang.yamlish import load_s, dump_s
[89]4
[90]5try:
6 import yaml
7except ImportError:
8 yaml = None # pyflakes:ignore
9
[114]10try:
11 from unittest import SkipTest
12except ImportError:
13 from pytest import skip
14 SkipTest = skip.Exception # pyflakes:ignore
[103]15
16
17class TestParse(TestCase):
[112]18 def assertParsesAs(self, text, expected):
19 self.assertEqual(load_s(text.strip()), expected)
[103]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']})
[89]34
35
[90]36class TestRoundTrip(TestCase):
[89]37 def roundtrip(self, data):
[112]38 text = self.dump_s(data)
39 print '\n=== Begin ===\n%s\n=== End ===' % text
40 self.assertEqual(self.load_s(text), data)
[90]41
[112]42 def dump_s(self, data):
43 return dump_s(data)
[90]44
[112]45 def load_s(self, text):
46 return load_s(text)
[89]47
48 def test_simple_dict(self):
49 self.roundtrip({'foo': 'bar'})
50
[129]51 def test_simple_list(self):
52 self.roundtrip(['foo', 'bar'])
53
[89]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
[129]67 def test_list_of_lists(self):
68 self.roundtrip(['foo', ['bar', 'baz'], 'qux'])
69
[89]70 def test_dict_list(self):
71 self.roundtrip({
72 'foo': ['bar', 'baz'],
73 })
74
[129]75 def test_list_dict(self):
76 self.roundtrip([
77 {'foo': 'bar'},
78 {'baz': 'qux', 'quux': 'corge'},
79 ])
80
[89]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 })
[90]93
[136]94 def test_int_dict(self):
95 self.roundtrip({
96 1: 'foo',
97 2: 'bar',
98 3: ['baz', 'qux'],
99 })
100
[137]101 def test_dict_keys(self):
102 self.roundtrip({
103 True: 'true',
104 False: [],
105 None: {},
106 0.7: -0.7,
107 })
108
[119]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
[120]116 def test_numeric(self):
117 self.roundtrip({'foo': [1, 2.0, -1, -2.5]})
118
[130]119 def test_inline(self):
120 self.roundtrip([[1, 2, "hi, there' joe", '', "'"], [3, 4]])
121
[90]122
123class TestFromPyYAML(TestRoundTrip):
[112]124 def dump_s(self, data):
[90]125 if yaml is None:
126 raise SkipTest('yaml module unavailable')
[112]127 return yaml.dump(data, default_flow_style=False)
[90]128
129
[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
[90]137class TestToPyYAML(TestRoundTrip):
[112]138 def load_s(self, text):
[90]139 if yaml is None:
140 raise SkipTest('yaml module unavailable')
[112]141 return yaml.load(text)
Note: See TracBrowser for help on using the repository browser.