1 | from unittest import TestCase, SkipTest
|
---|
2 | from StringIO import StringIO
|
---|
3 |
|
---|
4 | try:
|
---|
5 | import yaml
|
---|
6 | except ImportError:
|
---|
7 | yaml = None # pyflakes:ignore
|
---|
8 |
|
---|
9 | from nagslang.yamlish import load, dump
|
---|
10 |
|
---|
11 |
|
---|
12 | class 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 |
|
---|
61 | class 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 |
|
---|
68 | class TestToPyYAML(TestRoundTrip):
|
---|
69 | def load(self, f):
|
---|
70 | if yaml is None:
|
---|
71 | raise SkipTest('yaml module unavailable')
|
---|
72 | return yaml.load(f)
|
---|