comparison nagslang/tests/test_yamlish.py @ 138:366b334a7018

Render tuples to lists
author Stefano Rivera <stefano@rivera.za.net>
date Mon, 02 Sep 2013 18:12:42 +0200
parents c5ff16c66100
children 1d73867becbe
comparison
equal deleted inserted replaced
137:c5ff16c66100 138:366b334a7018
32 - baz 32 - baz
33 ''', {'foo': ['bar', 'baz']}) 33 ''', {'foo': ['bar', 'baz']})
34 34
35 35
36 class TestRoundTrip(TestCase): 36 class TestRoundTrip(TestCase):
37 from_pyyaml = False
38
37 def roundtrip(self, data): 39 def roundtrip(self, data):
38 text = self.dump_s(data) 40 text = self.dump_s(data)
39 print '\n=== Begin ===\n%s\n=== End ===' % text 41 print '\n=== Begin ===\n%s\n=== End ===' % text
40 self.assertEqual(self.load_s(text), data) 42 self.assertEqual(self.load_s(text), data)
41 43
104 False: [], 106 False: [],
105 None: {}, 107 None: {},
106 0.7: -0.7, 108 0.7: -0.7,
107 }) 109 })
108 110
111 def test_tuples(self):
112 if self.from_pyyaml:
113 raise SkipTest("Can't parse PyYAML tuples")
114 orig = {
115 'polygons': {
116 1: [
117 (0, 1),
118 (2, 3),
119 ],
120 },
121 }
122 text = self.dump_s(orig)
123 result = self.load_s(text)
124 self.assertEqual(orig['polygons'][1][0],
125 tuple(result['polygons'][1][0]))
126 self.assertEqual(orig['polygons'][1][1],
127 tuple(result['polygons'][1][1]))
128
109 def test_quoted(self): 129 def test_quoted(self):
110 # a literal true is True, but 'true' is a string 130 # a literal true is True, but 'true' is a string
111 self.roundtrip({'foo': 'true'}) 131 self.roundtrip({'foo': 'true'})
112 132
113 def test_literals(self): 133 def test_literals(self):
119 def test_inline(self): 139 def test_inline(self):
120 self.roundtrip([[1, 2, "hi, there' joe", '', "'"], [3, 4]]) 140 self.roundtrip([[1, 2, "hi, there' joe", '', "'"], [3, 4]])
121 141
122 142
123 class TestFromPyYAML(TestRoundTrip): 143 class TestFromPyYAML(TestRoundTrip):
144 from_pyyaml = True
145
124 def dump_s(self, data): 146 def dump_s(self, data):
125 if yaml is None: 147 if yaml is None:
126 raise SkipTest('yaml module unavailable') 148 raise SkipTest('yaml module unavailable')
127 return yaml.dump(data, default_flow_style=False) 149 return yaml.dump(data, default_flow_style=False)
128 150
129 151
130 class TestFromPyYAMLInlineLists(TestRoundTrip): 152 class TestFromPyYAMLInlineLists(TestRoundTrip):
153 from_pyyaml = True
154
131 def dump_s(self, data): 155 def dump_s(self, data):
132 if yaml is None: 156 if yaml is None:
133 raise SkipTest('yaml module unavailable') 157 raise SkipTest('yaml module unavailable')
134 return yaml.dump(data) 158 return yaml.dump(data)
135 159