source: nagslang/tests/test_yamlish.py@ 130:67f18e72024d

Last change on this file since 130:67f18e72024d was 130:67f18e72024d, checked in by Stefano Rivera <stefano@…>, 7 years ago

Handle corner cases around quotes in strings

File size: 2.9 KB
Line 
1from unittest import TestCase
2
3from nagslang.yamlish import load_s, dump_s
4
5try:
6 import yaml
7except ImportError:
8 yaml = None # pyflakes:ignore
9
10try:
11 from unittest import SkipTest
12except ImportError:
13 from pytest import skip
14 SkipTest = skip.Exception # pyflakes:ignore
15
16
17class TestParse(TestCase):
18 def assertParsesAs(self, text, expected):
19 self.assertEqual(load_s(text.strip()), expected)
20
21 def test_dict_list_1(self):
22 self.assertParsesAs('''
23foo:
24- bar
25- baz
26 ''', {'foo': ['bar', 'baz']})
27
28 def test_dict_list_2(self):
29 self.assertParsesAs('''
30foo:
31 - bar
32 - baz
33 ''', {'foo': ['bar', 'baz']})
34
35
36class TestRoundTrip(TestCase):
37 def roundtrip(self, data):
38 text = self.dump_s(data)
39 print '\n=== Begin ===\n%s\n=== End ===' % text
40 self.assertEqual(self.load_s(text), data)
41
42 def dump_s(self, data):
43 return dump_s(data)
44
45 def load_s(self, text):
46 return load_s(text)
47
48 def test_simple_dict(self):
49 self.roundtrip({'foo': 'bar'})
50
51 def test_simple_list(self):
52 self.roundtrip(['foo', 'bar'])
53
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
67 def test_list_of_lists(self):
68 self.roundtrip(['foo', ['bar', 'baz'], 'qux'])
69
70 def test_dict_list(self):
71 self.roundtrip({
72 'foo': ['bar', 'baz'],
73 })
74
75 def test_list_dict(self):
76 self.roundtrip([
77 {'foo': 'bar'},
78 {'baz': 'qux', 'quux': 'corge'},
79 ])
80
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 })
93
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
101 def test_numeric(self):
102 self.roundtrip({'foo': [1, 2.0, -1, -2.5]})
103
104 def test_inline(self):
105 self.roundtrip([[1, 2, "hi, there' joe", '', "'"], [3, 4]])
106
107
108class TestFromPyYAML(TestRoundTrip):
109 def dump_s(self, data):
110 if yaml is None:
111 raise SkipTest('yaml module unavailable')
112 return yaml.dump(data, default_flow_style=False)
113
114
115class TestFromPyYAMLInlineLists(TestRoundTrip):
116 def dump_s(self, data):
117 if yaml is None:
118 raise SkipTest('yaml module unavailable')
119 return yaml.dump(data)
120
121
122class TestToPyYAML(TestRoundTrip):
123 def load_s(self, text):
124 if yaml is None:
125 raise SkipTest('yaml module unavailable')
126 return yaml.load(text)
Note: See TracBrowser for help on using the repository browser.