source: trunk/tests/test_loader.py @ 20

Revision 20, 8.6 KB checked in by xi, 9 years ago (diff)

syck.dump() is implemented.

RevLine 
[7]1
2import unittest
3import syck
[16]4import test_parser
[9]5import os
[7]6
[9]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:
[18]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()
[9]27
[7]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
[8]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
[20]133ALIASES = """
134foo: &bar baz
135bar: *bar
136"""
137
[16]138class TestDocuments(test_parser.TestDocuments):
[7]139
140    def _testDocuments(self, source, length):
[16]141        actual_length = len(list(syck.load_documents(source)))
[7]142        self.assertEqual(actual_length, length)
[16]143        actual_length = len(list(syck.parse_documents(source)))
[7]144        self.assertEqual(actual_length, length)
145
[16]146class TestValuesAndSources(test_parser.TestValuesAndSources):
[7]147
148    def testValues1(self):
[16]149        self._testValues(test_parser.COMPARE1)
[7]150
151    def testValues2(self):
[16]152        self._testValues(test_parser.COMPARE2)
[7]153
154    def testValues3(self):
[16]155        self._testValues(test_parser.COMPARE3)
[7]156
157    def testFileValues1(self):
[16]158        self._testFileValues(test_parser.COMPARE1)
[7]159
160    def testFileValues2(self):
[16]161        self._testFileValues(test_parser.COMPARE2)
[7]162
163    def testFileValues3(self):
[16]164        self._testFileValues(test_parser.COMPARE3)
[7]165
166    def testNonsense(self):
167        class MyFile1:
168            def read(self, max_length):
169                return ' '*(max_length+1)
170        class MyFile2:
171            def read(self, max_length):
172                return None
173        self.assertRaises(ValueError, lambda: syck.parse(MyFile1()))
174        self.assertRaises(ValueError, lambda: syck.load(MyFile1()))
175        self.assertRaises(TypeError, lambda: syck.parse(MyFile2()))
176        self.assertRaises(TypeError, lambda: syck.load(MyFile2()))
177
178    def _testValues(self, (source, structure)):
179        self.assertEqualStructure(syck.parse(source), structure)
180        self.assertEqual(syck.load(source), structure)
181
182    def _testFileValues(self, (source, structure)):
183        filename = os.tempnam('/tmp', '_syck_test_')
184        file(filename, 'wb').write(source)
185        try:
186            self.assertEqualStructure(syck.parse(file(filename)), structure)
187            self.assertEqual(syck.load(file(filename)), structure)
188        except:
189            os.remove(filename)
190            raise
191
192class TestImplicitScalars(unittest.TestCase):
193
194
195    def testBinary(self):
196        self.assertEqual(syck.load(BINARY), [ARROW, ARROW])
197
198    def testBoolean(self):
199        # Syck does not recognize 'y'.
200        #self.assertEqual(syck.load('- y\n- NO\n- True\n- on\n'), [True, False, True, True])
201        self.assertEqual(syck.load('- yes\n- NO\n- True\n- on\n'), [True, False, True, True])
202
203    def testFloat(self):
204        self.assertAlmostEqual(syck.load('6.8523015e+5'), 685230.15)
205        # Syck does not understand '_'.
206        #self.assertAlmostEqual(syck.load('685.230_15e+03'), 685230.15)
207        #self.assertAlmostEqual(syck.load('685_230.15'), 685230.15)
208        self.assertAlmostEqual(syck.load('685.23015e+03'), 685230.15)
209        self.assertAlmostEqual(syck.load('685230.15'), 685230.15)
210        self.assertAlmostEqual(syck.load('190:20:30.15'), 685230.15)
211        self.assertEqual(syck.load('-.inf'), -INF)
212        self.assertEqual(syck.load('.nan'), NAN)
213
214    def testInteger(self):
215        # Syck does not understand '_' and binary integer.
216        #self.assertEqual(syck.load(
217        #    '- 685230\n- +685_230\n- 02472256\n- 0x_0A_74_AE\n'
218        #    '- 0b1010_0111_0100_1010_1110\n- 190:20:30\n'), [685230]*6)
219        self.assertEqual(syck.load(
220            '- 685230\n- +685230\n- 02472256\n- 0x0A74AE\n'
221            '- 190:20:30\n'), [685230]*5)
222
223    def testNull(self):
224        self.assertEqual(syck.load('-\n- NULL\n- ~\n'), [None]*3)
225
226    def testTimestamp(self):
227        # Again, Syck does not understand the latest two forms.
228        #self.assertEqual(syck.load(
229        #        '- 2001-12-15T02:59:43.1Z\n'
230        #        '- 2001-12-14t21:59:43.10-05:00\n'
231        #        '- 2001-12-14 21:59:43.10 -5\n'
232        #        '- 2001-12-15 2:59:43.10\n'),
233        #    [datetime.datetime(2001, 12, 15, 2, 59, 43, 100000)]*4)
234        self.assertEqual(syck.load(
235                '- 2001-12-15T02:59:43.1Z\n'
236                '- 2001-12-14t21:59:43.10-05:00\n'
237                '- 2001-12-14 21:59:43.10 -05\n'
238                '- 2001-12-15 02:59:43.10 Z\n'),
239            [datetime.datetime(2001, 12, 15, 2, 59, 43, 100000)]*4)
240        self.assertEqual(syck.load('2002-12-14'), datetime.datetime(2002, 12, 14))
241
242    def testString(self):
243        self.assertEqual('abcd', 'abcd')
244
[8]245class TestMerge(unittest.TestCase):
246
247    def testMerge(self):
248        document = syck.load(MERGE[0])
249        self.assertEqual(document[4], MERGE[1])
250        self.assertEqual(document[5], MERGE[1])
251        self.assertEqual(document[6], MERGE[1])
252        self.assertEqual(document[7], MERGE[1])
253
254class TestCollections(unittest.TestCase):
255
256    def testOmap(self):
257        self.assertEqual(syck.load(OMAP[0]), OMAP[1])
258
259    def testPairs(self):
260        self.assertEqual(syck.load(PAIRS[0]), PAIRS[1])
261
262    def testSet(self):
263        self.assertEqual(syck.load(SET[0]), SET[1])
264
[20]265class TestAliasesParsingAndLoading(unittest.TestCase):
266
267    def testAliasesParsing(self):
268        node = syck.parse(ALIASES)
269        values = node.value.values()
270        print values
271        print id(values[0])
272        print id(values[1])
273        self.assert_(values[0] is values[1])
274
275    def testAliasesLoading(self):
276        document = syck.load(ALIASES)
277        self.assert_(document['foo'] is document['bar'])
278
Note: See TracBrowser for help on using the repository browser.