| 1 | |
|---|
| 2 | from __future__ import generators |
|---|
| 3 | |
|---|
| 4 | import unittest |
|---|
| 5 | import syck |
|---|
| 6 | import StringIO |
|---|
| 7 | import test_emitter |
|---|
| 8 | |
|---|
| 9 | try: |
|---|
| 10 | import datetime |
|---|
| 11 | except: |
|---|
| 12 | class _datetime: |
|---|
| 13 | def datetime(self, *args): |
|---|
| 14 | return args |
|---|
| 15 | datetime = _datetime() |
|---|
| 16 | |
|---|
| 17 | try: |
|---|
| 18 | import sets |
|---|
| 19 | except: |
|---|
| 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 | |
|---|
| 28 | |
|---|
| 29 | EXAMPLE = { |
|---|
| 30 | 'foo': 'bar', |
|---|
| 31 | 'sequence': ['alpha', 'beta', 'gamma'], |
|---|
| 32 | 'mapping': {'a': 'alpha', 'b': 'beta', 'c': 'gamma'}, |
|---|
| 33 | } |
|---|
| 34 | |
|---|
| 35 | SIMPLE_EXAMPLE = [ |
|---|
| 36 | 'Mark McGwire', |
|---|
| 37 | 'Sammy Sosa', |
|---|
| 38 | 'Ken Griffey', |
|---|
| 39 | ] |
|---|
| 40 | |
|---|
| 41 | ALIAS_SCALAR = ["foo, bar, and baz"] |
|---|
| 42 | ALIAS_SCALAR.append(ALIAS_SCALAR[0]) |
|---|
| 43 | ALIAS_SCALAR.append(ALIAS_SCALAR[0]) |
|---|
| 44 | ALIAS_SCALAR.append(ALIAS_SCALAR[0]) |
|---|
| 45 | |
|---|
| 46 | ALIAS_SEQ = [['foo', ['bar']]] |
|---|
| 47 | ALIAS_SEQ.append(ALIAS_SEQ[0]) |
|---|
| 48 | ALIAS_SEQ.append(ALIAS_SEQ[0]) |
|---|
| 49 | |
|---|
| 50 | ALIAS_MAP = [{'foo': 'bar'}] |
|---|
| 51 | ALIAS_MAP.append(ALIAS_MAP[0]) |
|---|
| 52 | |
|---|
| 53 | ODD_ALIASES = [ |
|---|
| 54 | [None]*2, |
|---|
| 55 | [0]*3, |
|---|
| 56 | [1]*4, |
|---|
| 57 | [100]*5, |
|---|
| 58 | ['']*6, |
|---|
| 59 | ['foo']*7, |
|---|
| 60 | ] |
|---|
| 61 | |
|---|
| 62 | INF = 1e300000 |
|---|
| 63 | NAN = INF/INF |
|---|
| 64 | |
|---|
| 65 | SCALARS = [ |
|---|
| 66 | None, |
|---|
| 67 | True, False, |
|---|
| 68 | 0, 123, -4567, |
|---|
| 69 | 123.4e-5, INF, NAN, |
|---|
| 70 | 'foo, bar, baz', |
|---|
| 71 | datetime.datetime(2001, 12, 15, 2, 59, 43, 100000), |
|---|
| 72 | ] |
|---|
| 73 | |
|---|
| 74 | COLLECTIONS = [ |
|---|
| 75 | sets.Set(range(10)), |
|---|
| 76 | ] |
|---|
| 77 | |
|---|
| 78 | class TestOutput(unittest.TestCase): |
|---|
| 79 | |
|---|
| 80 | def testStringOutput(self): |
|---|
| 81 | source = syck.dump(EXAMPLE) |
|---|
| 82 | object = syck.load(source) |
|---|
| 83 | self.assertEqual(object, EXAMPLE) |
|---|
| 84 | |
|---|
| 85 | def testFileOutput(self): |
|---|
| 86 | output = StringIO.StringIO() |
|---|
| 87 | syck.dump(EXAMPLE, output) |
|---|
| 88 | output.seek(0) |
|---|
| 89 | object = syck.load(output) |
|---|
| 90 | self.assertEqual(object, EXAMPLE) |
|---|
| 91 | |
|---|
| 92 | def testNonsenseOutput(self): |
|---|
| 93 | self.assertRaises(AttributeError, lambda: syck.dump(EXAMPLE, 'output')) |
|---|
| 94 | |
|---|
| 95 | class TestDocuments(unittest.TestCase): |
|---|
| 96 | |
|---|
| 97 | def _get_examples(self): |
|---|
| 98 | yield EXAMPLE |
|---|
| 99 | yield SIMPLE_EXAMPLE |
|---|
| 100 | yield EXAMPLE |
|---|
| 101 | |
|---|
| 102 | def testStringOutput(self): |
|---|
| 103 | source = syck.dump_documents(self._get_examples()) |
|---|
| 104 | documents = syck.load_documents(source) |
|---|
| 105 | self.assertEqual(list(documents), list(self._get_examples())) |
|---|
| 106 | |
|---|
| 107 | def testFileOutput(self): |
|---|
| 108 | output = StringIO.StringIO() |
|---|
| 109 | syck.dump_documents(self._get_examples(), output) |
|---|
| 110 | output.seek(0) |
|---|
| 111 | documents = syck.load_documents(output) |
|---|
| 112 | self.assertEqual(list(documents), list(self._get_examples())) |
|---|
| 113 | |
|---|
| 114 | class TestEmitter(unittest.TestCase): |
|---|
| 115 | |
|---|
| 116 | def _get_examples(self): |
|---|
| 117 | yield test_emitter.EXAMPLE |
|---|
| 118 | yield test_emitter.EXAMPLE |
|---|
| 119 | |
|---|
| 120 | def testStringDocument(self): |
|---|
| 121 | source = syck.emit(test_emitter.EXAMPLE) |
|---|
| 122 | node = syck.parse(source) |
|---|
| 123 | self.assertEqual(test_emitter.strip(test_emitter.EXAMPLE), |
|---|
| 124 | test_emitter.strip(node)) |
|---|
| 125 | |
|---|
| 126 | def testFileDocument(self): |
|---|
| 127 | output = StringIO.StringIO() |
|---|
| 128 | syck.emit_documents(self._get_examples(), output) |
|---|
| 129 | output.seek(0) |
|---|
| 130 | nodes = syck.parse_documents(output) |
|---|
| 131 | self.assertEqual(map(test_emitter.strip, self._get_examples()), |
|---|
| 132 | map(test_emitter.strip, nodes)) |
|---|
| 133 | |
|---|
| 134 | class TestAliases(unittest.TestCase): |
|---|
| 135 | |
|---|
| 136 | def testAliases(self): |
|---|
| 137 | self._testAlias(ALIAS_SCALAR) |
|---|
| 138 | self._testAlias(ALIAS_SEQ) |
|---|
| 139 | self._testAlias(ALIAS_MAP) |
|---|
| 140 | |
|---|
| 141 | def _testAlias(self, objects): |
|---|
| 142 | objects = syck.load(syck.dump(objects)) |
|---|
| 143 | for object in objects: |
|---|
| 144 | self.assert_(object is objects[0]) |
|---|
| 145 | |
|---|
| 146 | class TestNoOddAliases(unittest.TestCase): |
|---|
| 147 | |
|---|
| 148 | def testOddAliases(self): |
|---|
| 149 | document = syck.parse(syck.dump(ODD_ALIASES)) |
|---|
| 150 | for group in document.value: |
|---|
| 151 | for item in group.value[1:]: |
|---|
| 152 | self.assert_(item is not group.value[0]) |
|---|
| 153 | |
|---|
| 154 | class TestScalarTypes(unittest.TestCase): |
|---|
| 155 | |
|---|
| 156 | def testScalarTypes(self): |
|---|
| 157 | scalars = syck.load(syck.dump(SCALARS)) |
|---|
| 158 | for a, b in zip(scalars, SCALARS): |
|---|
| 159 | self.assertEqual(type(a), type(b)) |
|---|
| 160 | if type(a) is float: |
|---|
| 161 | self.assertEqual(repr(a), repr(b)) |
|---|
| 162 | else: |
|---|
| 163 | self.assertEqual(a, b) |
|---|
| 164 | |
|---|
| 165 | class TestCollectionTypes(unittest.TestCase): |
|---|
| 166 | |
|---|
| 167 | def testCollectionTypes(self): |
|---|
| 168 | collections = syck.load(syck.dump(COLLECTIONS)) |
|---|
| 169 | for a, b in zip(collections, COLLECTIONS): |
|---|
| 170 | self.assertEqual(type(a), type(b)) |
|---|
| 171 | self.assertEqual(a, b) |
|---|
| 172 | |
|---|
| 173 | |
|---|