source: nagslang/tests/test_yamlish.py@ 111:16d9f3a0dc29

Last change on this file since 111:16d9f3a0dc29 was 103:adf3cd83bf7a, checked in by Stefano Rivera <stefano@…>, 8 years ago

Support a common stylistic variation

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