source: nagslang/tests/test_yamlish.py@ 128:fbb073720bac

Last change on this file since 128:fbb073720bac was 120:e6e7a471146a, checked in by Stefano Rivera <stefano@…>, 9 years ago

Support numeric types

File size: 2.4 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
51 def test_dict_of_dicts(self):
52 self.roundtrip({'foo': {'bar': 'baz'}})
53
54 def test_dict_tree(self):
55 self.roundtrip({
56 'foo': {
57 'bar': {
58 'baz': 'qux'
59 },
60 'quux': 'corge',
61 }
62 })
63
64 def test_dict_list(self):
65 self.roundtrip({
66 'foo': ['bar', 'baz'],
67 })
68
69 def test_nested_lists(self):
70 self.roundtrip({
71 'foo': [['bar', 'baz', 'qux'], 'quux'],
72 })
73
74 def test_list_of_dicts(self):
75 self.roundtrip({
76 'foo': [
77 {'bar': 'baz'},
78 {'qux': 'quux'},
79 ],
80 })
[90]81
[119]82 def test_quoted(self):
83 # a literal true is True, but 'true' is a string
84 self.roundtrip({'foo': 'true'})
85
86 def test_literals(self):
87 self.roundtrip({'foo': [True, False, None]})
88
[120]89 def test_numeric(self):
90 self.roundtrip({'foo': [1, 2.0, -1, -2.5]})
91
[90]92
93class TestFromPyYAML(TestRoundTrip):
[112]94 def dump_s(self, data):
[90]95 if yaml is None:
96 raise SkipTest('yaml module unavailable')
[112]97 return yaml.dump(data, default_flow_style=False)
[90]98
99
100class TestToPyYAML(TestRoundTrip):
[112]101 def load_s(self, text):
[90]102 if yaml is None:
103 raise SkipTest('yaml module unavailable')
[112]104 return yaml.load(text)
Note: See TracBrowser for help on using the repository browser.