source: nagslang/tests/test_level_serializer.py@ 90:a8d83de5b460

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

Dump our YAML subset too

File size: 1.6 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.level_serializer import load, dump
10
11
12class TestRoundTrip(TestCase):
13 def roundtrip(self, data):
14 f = StringIO()
15 self.dump(data, f)
16 f.seek(0)
17 print '\n=== Begin ===\n%s\n=== End ===' % f.buf.rstrip()
18 self.assertEqual(self.load(f), data)
19
20 def dump(self, data, f):
21 dump(data, f)
22
23 def load(self, f):
24 return load(f)
25
26 def test_simple_dict(self):
27 self.roundtrip({'foo': 'bar'})
28
29 def test_dict_of_dicts(self):
30 self.roundtrip({'foo': {'bar': 'baz'}})
31
32 def test_dict_tree(self):
33 self.roundtrip({
34 'foo': {
35 'bar': {
36 'baz': 'qux'
37 },
38 'quux': 'corge',
39 }
40 })
41
42 def test_dict_list(self):
43 self.roundtrip({
44 'foo': ['bar', 'baz'],
45 })
46
47 def test_nested_lists(self):
48 self.roundtrip({
49 'foo': [['bar', 'baz', 'qux'], 'quux'],
50 })
51
52 def test_list_of_dicts(self):
53 self.roundtrip({
54 'foo': [
55 {'bar': 'baz'},
56 {'qux': 'quux'},
57 ],
58 })
59
60
61class TestFromPyYAML(TestRoundTrip):
62 def dump(self, data, f):
63 if yaml is None:
64 raise SkipTest('yaml module unavailable')
65 yaml.dump(data, f, default_flow_style=False)
66
67
68class TestToPyYAML(TestRoundTrip):
69 def load(self, f):
70 if yaml is None:
71 raise SkipTest('yaml module unavailable')
72 return yaml.load(f)
Note: See TracBrowser for help on using the repository browser.