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