source: nagslang/tests/test_yamlish.py@ 112:c28f2fc2bb05

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

Test with dump_s and load_s

File size: 1.9 KB
RevLine 
[90]1from unittest import TestCase, SkipTest
[89]2
[90]3try:
4 import yaml
5except ImportError:
6 yaml = None # pyflakes:ignore
7
[112]8from nagslang.yamlish import load_s, dump_s
[103]9
10
11class TestParse(TestCase):
[112]12 def assertParsesAs(self, text, expected):
13 self.assertEqual(load_s(text.strip()), expected)
[103]14
15 def test_dict_list_1(self):
16 self.assertParsesAs('''
17foo:
18- bar
19- baz
20 ''', {'foo': ['bar', 'baz']})
21
22 def test_dict_list_2(self):
23 self.assertParsesAs('''
24foo:
25 - bar
26 - baz
27 ''', {'foo': ['bar', 'baz']})
[89]28
29
[90]30class TestRoundTrip(TestCase):
[89]31 def roundtrip(self, data):
[112]32 text = self.dump_s(data)
33 print '\n=== Begin ===\n%s\n=== End ===' % text
34 self.assertEqual(self.load_s(text), data)
[90]35
[112]36 def dump_s(self, data):
37 return dump_s(data)
[90]38
[112]39 def load_s(self, text):
40 return load_s(text)
[89]41
42 def test_simple_dict(self):
43 self.roundtrip({'foo': 'bar'})
44
45 def test_dict_of_dicts(self):
46 self.roundtrip({'foo': {'bar': 'baz'}})
47
48 def test_dict_tree(self):
49 self.roundtrip({
50 'foo': {
51 'bar': {
52 'baz': 'qux'
53 },
54 'quux': 'corge',
55 }
56 })
57
58 def test_dict_list(self):
59 self.roundtrip({
60 'foo': ['bar', 'baz'],
61 })
62
63 def test_nested_lists(self):
64 self.roundtrip({
65 'foo': [['bar', 'baz', 'qux'], 'quux'],
66 })
67
68 def test_list_of_dicts(self):
69 self.roundtrip({
70 'foo': [
71 {'bar': 'baz'},
72 {'qux': 'quux'},
73 ],
74 })
[90]75
76
77class TestFromPyYAML(TestRoundTrip):
[112]78 def dump_s(self, data):
[90]79 if yaml is None:
80 raise SkipTest('yaml module unavailable')
[112]81 return yaml.dump(data, default_flow_style=False)
[90]82
83
84class TestToPyYAML(TestRoundTrip):
[112]85 def load_s(self, text):
[90]86 if yaml is None:
87 raise SkipTest('yaml module unavailable')
[112]88 return yaml.load(text)
Note: See TracBrowser for help on using the repository browser.