[114] | 1 | from unittest import TestCase
|
---|
| 2 |
|
---|
| 3 | from nagslang.yamlish import load_s, dump_s
|
---|
[89] | 4 |
|
---|
[90] | 5 | try:
|
---|
| 6 | import yaml
|
---|
| 7 | except ImportError:
|
---|
| 8 | yaml = None # pyflakes:ignore
|
---|
| 9 |
|
---|
[114] | 10 | try:
|
---|
| 11 | from unittest import SkipTest
|
---|
| 12 | except ImportError:
|
---|
| 13 | from pytest import skip
|
---|
| 14 | SkipTest = skip.Exception # pyflakes:ignore
|
---|
[103] | 15 |
|
---|
| 16 |
|
---|
| 17 | class 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('''
|
---|
| 23 | foo:
|
---|
| 24 | - bar
|
---|
| 25 | - baz
|
---|
| 26 | ''', {'foo': ['bar', 'baz']})
|
---|
| 27 |
|
---|
| 28 | def test_dict_list_2(self):
|
---|
| 29 | self.assertParsesAs('''
|
---|
| 30 | foo:
|
---|
| 31 | - bar
|
---|
| 32 | - baz
|
---|
| 33 | ''', {'foo': ['bar', 'baz']})
|
---|
[89] | 34 |
|
---|
| 35 |
|
---|
[90] | 36 | class 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 |
|
---|
[119] | 94 | def test_quoted(self):
|
---|
| 95 | # a literal true is True, but 'true' is a string
|
---|
| 96 | self.roundtrip({'foo': 'true'})
|
---|
| 97 |
|
---|
| 98 | def test_literals(self):
|
---|
| 99 | self.roundtrip({'foo': [True, False, None]})
|
---|
| 100 |
|
---|
[120] | 101 | def test_numeric(self):
|
---|
| 102 | self.roundtrip({'foo': [1, 2.0, -1, -2.5]})
|
---|
| 103 |
|
---|
[90] | 104 |
|
---|
| 105 | class TestFromPyYAML(TestRoundTrip):
|
---|
[112] | 106 | def dump_s(self, data):
|
---|
[90] | 107 | if yaml is None:
|
---|
| 108 | raise SkipTest('yaml module unavailable')
|
---|
[112] | 109 | return yaml.dump(data, default_flow_style=False)
|
---|
[90] | 110 |
|
---|
| 111 |
|
---|
[129] | 112 | class TestFromPyYAMLInlineLists(TestRoundTrip):
|
---|
| 113 | def dump_s(self, data):
|
---|
| 114 | if yaml is None:
|
---|
| 115 | raise SkipTest('yaml module unavailable')
|
---|
| 116 | return yaml.dump(data)
|
---|
| 117 |
|
---|
| 118 |
|
---|
[90] | 119 | class TestToPyYAML(TestRoundTrip):
|
---|
[112] | 120 | def load_s(self, text):
|
---|
[90] | 121 | if yaml is None:
|
---|
| 122 | raise SkipTest('yaml module unavailable')
|
---|
[112] | 123 | return yaml.load(text)
|
---|