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@…>, 8 years ago

Handle corner cases around quotes in strings

File size: 2.9 KB
RevLine 
[114]1from unittest import TestCase
2
3from nagslang.yamlish import load_s, dump_s
[89]4
[90]5try:
6 import yaml
7except ImportError:
8 yaml = None # pyflakes:ignore
9
[114]10try:
11 from unittest import SkipTest
12except ImportError:
13 from pytest import skip
14 SkipTest = skip.Exception # pyflakes:ignore
[103]15
16
17class 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('''
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']})
[89]34
35
[90]36class 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
[130]104 def test_inline(self):
105 self.roundtrip([[1, 2, "hi, there' joe", '', "'"], [3, 4]])
106
[90]107
108class TestFromPyYAML(TestRoundTrip):
[112]109 def dump_s(self, data):
[90]110 if yaml is None:
111 raise SkipTest('yaml module unavailable')
[112]112 return yaml.dump(data, default_flow_style=False)
[90]113
114
[129]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
[90]122class TestToPyYAML(TestRoundTrip):
[112]123 def load_s(self, text):
[90]124 if yaml is None:
125 raise SkipTest('yaml module unavailable')
[112]126 return yaml.load(text)
Note: See TracBrowser for help on using the repository browser.