source: trunk/tests/test_loader.py @ 22

Revision 22, 8.9 KB checked in by xi, 9 years ago (diff)

Add test cases for customization API.

Line 
1
2import unittest
3import syck
4import test_parser
5import os
6
7try:
8    import datetime
9except ImportError:
10    pass
11
12try:
13    import mx.DateTime
14except ImportError:
15    pass
16
17try:
18    import sets
19except ImportError:
20    class _sets:
21        def Set(self, items):
22            set = {}
23            for items in items:
24                set[items] = None
25            return set
26    sets = _sets()
27
28INF = 1e300000
29NAN = INF/INF
30
31ARROW = "GIF89a\x0c\x00\x0c\x00\x84\x00\x00\xff\xff\xf7\xf5\xf5\xee\xe9\xe9\xe5fff\x00\x00\x00\xe7\xe7\xe7^^^\xf3\xf3\xed\x8e\x8e\x8e\xe0\xe0\xe0\x9f\x9f\x9f\x93\x93\x93\xa7\xa7\xa7\x9e\x9e\x9eiiiccc\xa3\xa3\xa3\x84\x84\x84\xff\xfe\xf9\xff\xfe\xf9\xff\xfe\xf9\xff\xfe\xf9\xff\xfe\xf9\xff\xfe\xf9\xff\xfe\xf9\xff\xfe\xf9\xff\xfe\xf9\xff\xfe\xf9\xff\xfe\xf9\xff\xfe\xf9\xff\xfe\xf9\xff\xfe\xf9!\xfe\x0eMade with GIMP\x00,\x00\x00\x00\x00\x0c\x00\x0c\x00\x00\x05\x8e\x810\x9e\xe3@\x14\xe8i\x10\xc4\xd1\x8a\x08\x1c\xcf\x80M$z\xef\xff0\x85p\xb8\xb01f\r\x1b\xce\x01\xc3\x01\x1e\x10' \x82\n\x01\x00;"
32BINARY = r"""
33- !binary "\
34 R0lGODlhDAAMAIQAAP//9/X17unp5WZmZgAAAOfn515eXvPz7Y6OjuDg4J+fn5\
35 OTk6enp56enmlpaWNjY6Ojo4SEhP/++f/++f/++f/++f/++f/++f/++f/++f/+\
36 +f/++f/++f/++f/++f/++SH+Dk1hZGUgd2l0aCBHSU1QACwAAAAADAAMAAAFLC\
37 AgjoEwnuNAFOhpEMTRiggcz4BNJHrv/zCFcLiwMWYNG84BwwEeECcgggoBADs="
38-
39 !binary |
40  R0lGODlhDAAMAIQAAP//9/X17unp5WZmZgAAAOfn515eXvPz7Y6OjuDg4J+fn5
41  OTk6enp56enmlpaWNjY6Ojo4SEhP/++f/++f/++f/++f/++f/++f/++f/++f/+
42  +f/++f/++f/++f/++f/++SH+Dk1hZGUgd2l0aCBHSU1QACwAAAAADAAMAAAFLC
43  AgjoEwnuNAFOhpEMTRiggcz4BNJHrv/zCFcLiwMWYNG84BwwEeECcgggoBADs=
44"""
45
46MERGE = """
47---
48- &CENTER { x: 1, y: 2 }
49- &LEFT { x: 0, y: 2 }
50- &BIG { r: 10 }
51- &SMALL { r: 1 }
52
53# All the following maps are equal:
54
55- # Explicit keys
56  x: 1
57  y: 2
58  r: 10
59  label: center/big
60
61- # Merge one map
62  << : *CENTER
63  r: 10
64  label: center/big
65
66- # Merge multiple maps
67  << : [ *CENTER, *BIG ]
68  label: center/big
69
70- # Override
71  << : [ *BIG, *LEFT, *SMALL ]
72  x: 1
73  label: center/big
74""", { 'x': 1, 'y': 2, 'r': 10, 'label': 'center/big' }
75
76OMAP = """
77# Explicitly typed ordered map (dictionary).
78Bestiary: !omap
79  - aardvark: African pig-like ant eater. Ugly.
80  - anteater: South-American ant eater. Two species.
81  - anaconda: South-American constrictor snake. Scaly.
82  # Etc.
83# Flow style
84Numbers: !omap [ one: 1, two: 2, three : 3 ]
85""", {
86    'Bestiary': [
87        ('aardvark', 'African pig-like ant eater. Ugly.'),
88        ('anteater', 'South-American ant eater. Two species.'),
89        ('anaconda', 'South-American constrictor snake. Scaly.'),
90    ],
91    'Numbers': [
92        ('one', 1),
93        ('two', 2),
94        ('three', 3),
95    ],
96}
97
98PAIRS = """
99# Explicitly typed pairs.
100Block tasks: !pairs
101  - meeting: with team.
102  - meeting: with boss.
103  - break: lunch.
104  - meeting: with client.
105Flow tasks: !pairs [ meeting: with team, meeting: with boss ]
106""", {
107    'Block tasks': [
108        ('meeting', 'with team.'),
109        ('meeting', 'with boss.'),
110        ('break', 'lunch.'),
111        ('meeting', 'with client.'),
112    ],
113    'Flow tasks': [
114        ('meeting', 'with team'),
115        ('meeting', 'with boss'),
116    ],
117}
118
119SET = """
120# Syck does not understand it
121## Explicitly typed set.
122#baseball players: !set
123#  ? Mark McGwire
124#  ? Sammy Sosa
125#  ? Ken Griffey
126# Flow style
127baseball teams: !set { Boston Red Sox, Detroit Tigers, New York Yankees }
128""", {
129#    'baseball players': sets.Set(['Mark McGwire', 'Sammy Sosa', 'Ken Griffey']),
130    'baseball teams': sets.Set(['Boston Red Sox', 'Detroit Tigers', 'New York Yankees']),
131}
132
133ALIASES = """
134foo: &bar baz
135bar: *bar
136"""
137
138MUTABLE_KEY = """
139? []
140: []
141"""
142
143class TestDocuments(test_parser.TestDocuments):
144
145    def _testDocuments(self, source, length):
146        actual_length = len(list(syck.load_documents(source)))
147        self.assertEqual(actual_length, length)
148        actual_length = len(list(syck.parse_documents(source)))
149        self.assertEqual(actual_length, length)
150
151class TestValuesAndSources(test_parser.TestValuesAndSources):
152
153    def testValues1(self):
154        self._testValues(test_parser.COMPARE1)
155
156    def testValues2(self):
157        self._testValues(test_parser.COMPARE2)
158
159    def testValues3(self):
160        self._testValues(test_parser.COMPARE3)
161
162    def testFileValues1(self):
163        self._testFileValues(test_parser.COMPARE1)
164
165    def testFileValues2(self):
166        self._testFileValues(test_parser.COMPARE2)
167
168    def testFileValues3(self):
169        self._testFileValues(test_parser.COMPARE3)
170
171    def testNonsense(self):
172        class MyFile1:
173            def read(self, max_length):
174                return ' '*(max_length+1)
175        class MyFile2:
176            def read(self, max_length):
177                return None
178        self.assertRaises(ValueError, lambda: syck.parse(MyFile1()))
179        self.assertRaises(ValueError, lambda: syck.load(MyFile1()))
180        self.assertRaises(TypeError, lambda: syck.parse(MyFile2()))
181        self.assertRaises(TypeError, lambda: syck.load(MyFile2()))
182
183    def _testValues(self, (source, structure)):
184        self.assertEqualStructure(syck.parse(source), structure)
185        self.assertEqual(syck.load(source), structure)
186
187    def _testFileValues(self, (source, structure)):
188        tempfile = os.tmpfile()
189        tempfile.write(source)
190        tempfile.seek(0)
191        try:
192            self.assertEqualStructure(syck.parse(tempfile), structure)
193            tempfile.seek(0)
194            self.assertEqual(syck.load(tempfile), structure)
195            tempfile.seek(0)
196        except:
197            os.remove(filename)
198            raise
199
200class TestImplicitScalars(unittest.TestCase):
201
202
203    def testBinary(self):
204        self.assertEqual(syck.load(BINARY), [ARROW, ARROW])
205
206    def testBoolean(self):
207        # Syck does not recognize 'y'.
208        #self.assertEqual(syck.load('- y\n- NO\n- True\n- on\n'), [True, False, True, True])
209        self.assertEqual(syck.load('- yes\n- NO\n- True\n- on\n'), [True, False, True, True])
210
211    def testFloat(self):
212        self.assertAlmostEqual(syck.load('6.8523015e+5'), 685230.15)
213        # Syck does not understand '_'.
214        #self.assertAlmostEqual(syck.load('685.230_15e+03'), 685230.15)
215        #self.assertAlmostEqual(syck.load('685_230.15'), 685230.15)
216        self.assertAlmostEqual(syck.load('685.23015e+03'), 685230.15)
217        self.assertAlmostEqual(syck.load('685230.15'), 685230.15)
218        self.assertAlmostEqual(syck.load('190:20:30.15'), 685230.15)
219        self.assertEqual(syck.load('-.inf'), -INF)
220        self.assertEqual(syck.load('.nan'), NAN)
221
222    def testInteger(self):
223        # Syck does not understand '_' and binary integer.
224        #self.assertEqual(syck.load(
225        #    '- 685230\n- +685_230\n- 02472256\n- 0x_0A_74_AE\n'
226        #    '- 0b1010_0111_0100_1010_1110\n- 190:20:30\n'), [685230]*6)
227        self.assertEqual(syck.load(
228            '- 685230\n- +685230\n- 02472256\n- 0x0A74AE\n'
229            '- 190:20:30\n'), [685230]*5)
230
231    def testNull(self):
232        self.assertEqual(syck.load('-\n- NULL\n- ~\n'), [None]*3)
233
234    def testTimestamp(self):
235        # Again, Syck does not understand the latest two forms.
236        #self.assertEqual(syck.load(
237        #        '- 2001-12-15T02:59:43.1Z\n'
238        #        '- 2001-12-14t21:59:43.10-05:00\n'
239        #        '- 2001-12-14 21:59:43.10 -5\n'
240        #        '- 2001-12-15 2:59:43.10\n'),
241        #    [datetime.datetime(2001, 12, 15, 2, 59, 43, 100000)]*4)
242        self.assertEqual(syck.load(
243                '- 2001-12-15T02:59:43.1Z\n'
244                '- 2001-12-14t21:59:43.10-05:00\n'
245                '- 2001-12-14 21:59:43.10 -05\n'
246                '- 2001-12-15 02:59:43.10 Z\n'),
247            [datetime.datetime(2001, 12, 15, 2, 59, 43, 100000)]*4)
248        self.assertEqual(syck.load('2002-12-14'), datetime.datetime(2002, 12, 14))
249
250    def testString(self):
251        self.assertEqual('abcd', 'abcd')
252
253class TestMerge(unittest.TestCase):
254
255    def testMerge(self):
256        document = syck.load(MERGE[0])
257        self.assertEqual(document[4], MERGE[1])
258        self.assertEqual(document[5], MERGE[1])
259        self.assertEqual(document[6], MERGE[1])
260        self.assertEqual(document[7], MERGE[1])
261
262class TestCollections(unittest.TestCase):
263
264    def testOmap(self):
265        self.assertEqual(syck.load(OMAP[0]), OMAP[1])
266
267    def testPairs(self):
268        self.assertEqual(syck.load(PAIRS[0]), PAIRS[1])
269
270    def testSet(self):
271        self.assertEqual(syck.load(SET[0]), SET[1])
272
273class TestAliasesParsingAndLoading(unittest.TestCase):
274
275    def testAliasesParsing(self):
276        node = syck.parse(ALIASES)
277        values = node.value.values()
278        self.assert_(values[0] is values[1])
279
280    def testAliasesLoading(self):
281        document = syck.load(ALIASES)
282        self.assert_(document['foo'] is document['bar'])
283
284class TestMutableKey(unittest.TestCase):
285
286    def testMutableKey(self):
287        document = syck.load(MUTABLE_KEY)
288        self.assertEqual(type(document), list)
289        self.assertEqual(len(document), 1)
290        self.assertEqual(type(document[0]), tuple)
291        self.assertEqual(len(document[0]), 2)
292        self.assertEqual(document[0][0], document[0][1])
Note: See TracBrowser for help on using the repository browser.