| [3] | 1 | |
|---|
| 2 | import unittest |
|---|
| 3 | import _syck, syck |
|---|
| [4] | 4 | import StringIO |
|---|
| [3] | 5 | |
|---|
| [4] | 6 | EXAMPLE = """ |
|---|
| 7 | - |
|---|
| 8 | avg: 0.278 |
|---|
| 9 | hr: 65 |
|---|
| 10 | name: Mark McGwire |
|---|
| 11 | - |
|---|
| 12 | avg: 0.288 |
|---|
| 13 | hr: 63 |
|---|
| 14 | name: Sammy Sosa |
|---|
| 15 | """ |
|---|
| [3] | 16 | |
|---|
| [4] | 17 | INVALID = """ |
|---|
| 18 | - invalid |
|---|
| 19 | - document |
|---|
| 20 | """, 2, 0 |
|---|
| [3] | 21 | |
|---|
| [4] | 22 | COMPARE1 = """ |
|---|
| 23 | one: foo |
|---|
| 24 | two: bar |
|---|
| 25 | three: baz |
|---|
| 26 | """, { |
|---|
| 27 | 'one': 'foo', |
|---|
| 28 | 'two': 'bar', |
|---|
| 29 | 'three': 'baz', |
|---|
| 30 | } |
|---|
| [3] | 31 | |
|---|
| [4] | 32 | COMPARE2 = """ |
|---|
| 33 | - Mark McGwire |
|---|
| 34 | - Sammy Sosa |
|---|
| 35 | - Ken Griffey |
|---|
| 36 | """, [ |
|---|
| 37 | 'Mark McGwire', |
|---|
| 38 | 'Sammy Sosa', |
|---|
| 39 | 'Ken Griffey', |
|---|
| 40 | ] |
|---|
| [3] | 41 | |
|---|
| [4] | 42 | COMPARE3 = """ |
|---|
| 43 | american: |
|---|
| 44 | - Boston Red Sox |
|---|
| 45 | - Detroit Tigers |
|---|
| 46 | - New York Yankees |
|---|
| 47 | national: |
|---|
| 48 | - New York Mets |
|---|
| 49 | - Chicago Cubs |
|---|
| 50 | - Atlanta Braves |
|---|
| 51 | """, { |
|---|
| 52 | 'american': [ |
|---|
| 53 | 'Boston Red Sox', |
|---|
| 54 | 'Detroit Tigers', |
|---|
| 55 | 'New York Yankees', |
|---|
| 56 | ], |
|---|
| 57 | 'national': [ |
|---|
| 58 | 'New York Mets', |
|---|
| 59 | 'Chicago Cubs', |
|---|
| 60 | 'Atlanta Braves', |
|---|
| 61 | ], |
|---|
| 62 | } |
|---|
| [3] | 63 | |
|---|
| [4] | 64 | DOCUMENTS0 = "" |
|---|
| 65 | |
|---|
| 66 | DOCUMENTS1 = """ |
|---|
| 67 | --- |
|---|
| 68 | Time: 2001-11-23 15:01:42 -05:00 |
|---|
| 69 | User: ed |
|---|
| 70 | Warning: > |
|---|
| 71 | This is an error message |
|---|
| 72 | for the log file |
|---|
| 73 | """ |
|---|
| 74 | |
|---|
| 75 | DOCUMENTS2 = """ |
|---|
| 76 | --- |
|---|
| 77 | Time: 2001-11-23 15:01:42 -05:00 |
|---|
| 78 | User: ed |
|---|
| 79 | Warning: > |
|---|
| 80 | This is an error message |
|---|
| 81 | for the log file |
|---|
| 82 | --- |
|---|
| 83 | Time: 2001-11-23 15:02:31 -05:00 |
|---|
| 84 | User: ed |
|---|
| 85 | Warning: > |
|---|
| 86 | A slightly different error |
|---|
| 87 | message. |
|---|
| 88 | """ |
|---|
| 89 | |
|---|
| 90 | DOCUMENTS3 = """ |
|---|
| 91 | --- |
|---|
| 92 | Time: 2001-11-23 15:01:42 -05:00 |
|---|
| 93 | User: ed |
|---|
| 94 | Warning: > |
|---|
| 95 | This is an error message |
|---|
| 96 | for the log file |
|---|
| 97 | --- |
|---|
| 98 | Time: 2001-11-23 15:02:31 -05:00 |
|---|
| 99 | User: ed |
|---|
| 100 | Warning: > |
|---|
| 101 | A slightly different error |
|---|
| 102 | message. |
|---|
| 103 | --- |
|---|
| 104 | Date: 2001-11-23 15:03:17 -05:00 |
|---|
| 105 | User: ed |
|---|
| 106 | Fatal: > |
|---|
| 107 | Unknown variable "bar" |
|---|
| 108 | Stack: |
|---|
| 109 | - file: TopClass.py |
|---|
| 110 | line: 23 |
|---|
| 111 | code: | |
|---|
| 112 | x = MoreObject("345\\n") |
|---|
| 113 | - file: MoreClass.py |
|---|
| 114 | line: 58 |
|---|
| 115 | code: |- |
|---|
| 116 | foo = bar |
|---|
| 117 | """ |
|---|
| 118 | |
|---|
| 119 | IMPLICIT_TYPING = """ |
|---|
| 120 | - 'foo' |
|---|
| 121 | - >- |
|---|
| 122 | bar |
|---|
| 123 | - baz |
|---|
| 124 | - 123 |
|---|
| 125 | - 3.14 |
|---|
| 126 | - true |
|---|
| 127 | - false |
|---|
| 128 | - [] |
|---|
| 129 | - {} |
|---|
| 130 | """, [ |
|---|
| 131 | ('str', True), |
|---|
| 132 | ('str', True), |
|---|
| 133 | ('str', False), |
|---|
| 134 | ('int', False), |
|---|
| 135 | ('float#fix', False), |
|---|
| 136 | ('bool#yes', False), |
|---|
| 137 | ('bool#no', False), |
|---|
| 138 | (None, False), |
|---|
| 139 | (None, False), |
|---|
| 140 | ] |
|---|
| 141 | |
|---|
| 142 | EXPLICIT_TYPING = """ |
|---|
| 143 | - !int '123' |
|---|
| 144 | - !yamltype 'foo' |
|---|
| 145 | - !python/type 'bar' |
|---|
| 146 | - !domain.tld,2002/type 'baz' |
|---|
| 147 | - !!private 'private' |
|---|
| 148 | - !map {} |
|---|
| 149 | - !seq [] |
|---|
| 150 | """, [ |
|---|
| 151 | 'tag:yaml.org,2002:int', |
|---|
| 152 | 'tag:yaml.org,2002:yamltype', |
|---|
| 153 | 'tag:python.yaml.org,2002:type', |
|---|
| 154 | 'tag:domain.tld,2002:type', |
|---|
| 155 | 'x-private:private', |
|---|
| 156 | 'tag:yaml.org,2002:map', |
|---|
| 157 | 'tag:yaml.org,2002:seq', |
|---|
| 158 | ] |
|---|
| 159 | |
|---|
| 160 | class TestTypes(unittest.TestCase): |
|---|
| 161 | |
|---|
| 162 | def testParserType(self): |
|---|
| 163 | parser = _syck.Parser(EXAMPLE) |
|---|
| 164 | self.assertEqual(type(parser), _syck.ParserType) |
|---|
| 165 | parser.close() |
|---|
| 166 | |
|---|
| 167 | def testNodeType(self): |
|---|
| 168 | parser = _syck.Parser(EXAMPLE) |
|---|
| 169 | document = parser.parse() |
|---|
| 170 | self.assertEqual(type(document), _syck.NodeType) |
|---|
| 171 | parser.close() |
|---|
| 172 | |
|---|
| 173 | def testNodeType2(self): |
|---|
| 174 | self.assertRaises(TypeError, (lambda: _syck.Node())) |
|---|
| 175 | |
|---|
| 176 | class TestErrors(unittest.TestCase): |
|---|
| 177 | |
|---|
| 178 | def testError(self): |
|---|
| 179 | parser = _syck.Parser(INVALID[0]) |
|---|
| 180 | self.assertRaises(_syck.error, (lambda: parser.parse())) |
|---|
| 181 | parser.close() |
|---|
| 182 | |
|---|
| 183 | def testErrorLocation(self): |
|---|
| 184 | source, line, column = INVALID |
|---|
| 185 | parser = _syck.Parser(source) |
|---|
| 186 | try: |
|---|
| 187 | parser.parse() |
|---|
| 188 | raise Exception |
|---|
| 189 | except _syck.error, e: |
|---|
| 190 | self.assertEqual(e.args[1], line) |
|---|
| 191 | self.assertEqual(e.args[2], column) |
|---|
| 192 | |
|---|
| 193 | class TestValuesAndSources(unittest.TestCase): |
|---|
| 194 | |
|---|
| 195 | def testValues1(self): |
|---|
| 196 | self._testValues(COMPARE1) |
|---|
| 197 | |
|---|
| 198 | def testValues2(self): |
|---|
| 199 | self._testValues(COMPARE2) |
|---|
| 200 | |
|---|
| 201 | def testValues3(self): |
|---|
| 202 | self._testValues(COMPARE3) |
|---|
| 203 | |
|---|
| 204 | def testFileValues1(self): |
|---|
| 205 | self._testFileValues(COMPARE1) |
|---|
| 206 | |
|---|
| 207 | def testFileValues2(self): |
|---|
| 208 | self._testFileValues(COMPARE2) |
|---|
| 209 | |
|---|
| 210 | def testFileValues3(self): |
|---|
| 211 | self._testFileValues(COMPARE3) |
|---|
| 212 | |
|---|
| 213 | def testNonsense(self): |
|---|
| 214 | parser = _syck.Parser(None) |
|---|
| 215 | self.assertRaises(AttributeError, (lambda: parser.parse())) |
|---|
| 216 | parser.close() |
|---|
| 217 | |
|---|
| 218 | def _testValues(self, (source, structure)): |
|---|
| 219 | parser = _syck.Parser(source) |
|---|
| 220 | document = parser.parse() |
|---|
| 221 | self.assertEqualStructure(document, structure) |
|---|
| 222 | parser.close() |
|---|
| 223 | |
|---|
| 224 | def _testFileValues(self, (source, structure)): |
|---|
| 225 | parser = _syck.Parser(StringIO.StringIO(source)) |
|---|
| 226 | document = parser.parse() |
|---|
| 227 | self.assertEqualStructure(document, structure) |
|---|
| 228 | parser.close() |
|---|
| 229 | |
|---|
| 230 | def assertEqualStructure(self, node, structure): |
|---|
| 231 | if node.kind == 'scalar': |
|---|
| 232 | self.assertEqual(type(structure), str) |
|---|
| 233 | self.assertEqual(node.value, structure) |
|---|
| 234 | elif node.kind == 'seq': |
|---|
| 235 | self.assertEqual(type(structure), list) |
|---|
| 236 | self.assertEqual(len(node.value), len(structure)) |
|---|
| 237 | for i, item in enumerate(node.value): |
|---|
| 238 | self.assertEqualStructure(item, structure[i]) |
|---|
| 239 | elif node.kind == 'map': |
|---|
| 240 | self.assertEqual(type(structure), dict) |
|---|
| 241 | self.assertEqual(len(node.value), len(structure)) |
|---|
| 242 | for key in node.value: |
|---|
| 243 | self.assert_(key.value in structure) |
|---|
| 244 | self.assertEqualStructure(node.value[key], structure[key.value]) |
|---|
| 245 | |
|---|
| 246 | class TestDocuments(unittest.TestCase): |
|---|
| 247 | |
|---|
| 248 | def testDocuments0(self): |
|---|
| 249 | self._testDocuments(DOCUMENTS0, 0) |
|---|
| 250 | |
|---|
| 251 | def testDocuments1(self): |
|---|
| 252 | self._testDocuments(DOCUMENTS1, 1) |
|---|
| 253 | |
|---|
| 254 | def testDocuments2(self): |
|---|
| 255 | self._testDocuments(DOCUMENTS2, 2) |
|---|
| 256 | |
|---|
| 257 | def testDocuments3(self): |
|---|
| 258 | self._testDocuments(DOCUMENTS3, 3) |
|---|
| 259 | |
|---|
| 260 | def _testDocuments(self, source, length): |
|---|
| 261 | parser = _syck.Parser(source) |
|---|
| 262 | documents = parser.parse_documents() |
|---|
| 263 | self.assertEqual(len(documents), length) |
|---|
| 264 | parser.close() |
|---|
| 265 | parser = _syck.Parser(source) |
|---|
| 266 | for k in range(length): |
|---|
| 267 | document = parser.parse() |
|---|
| 268 | self.assert_(document) |
|---|
| 269 | self.assertEqual(parser.parse(), None) |
|---|
| 270 | parser.close() |
|---|
| 271 | |
|---|
| 272 | class TestImplicitTyping(unittest.TestCase): |
|---|
| 273 | |
|---|
| 274 | def testImplicitAndExpansionTyping(self): |
|---|
| 275 | self._testTyping(True, True) |
|---|
| 276 | |
|---|
| 277 | def testImplicitTyping(self): |
|---|
| 278 | self._testTyping(True, False) |
|---|
| 279 | |
|---|
| 280 | def testExpansionTyping(self): |
|---|
| 281 | self._testTyping(False, True) |
|---|
| 282 | |
|---|
| 283 | def testNoTyping(self): |
|---|
| 284 | self._testTyping(False, False) |
|---|
| 285 | |
|---|
| 286 | def _testTyping(self, implicit_typing, taguri_expansion): |
|---|
| 287 | parser = _syck.Parser(IMPLICIT_TYPING[0], implicit_typing, taguri_expansion) |
|---|
| 288 | for node, (type_id, explicit) in zip(parser.parse().value, IMPLICIT_TYPING[1]): |
|---|
| 289 | if type_id is not None and taguri_expansion: |
|---|
| 290 | type_id = 'tag:yaml.org,2002:%s' % type_id |
|---|
| 291 | if implicit_typing or explicit: |
|---|
| 292 | self.assertEqual(node.type_id, type_id) |
|---|
| 293 | else: |
|---|
| 294 | self.assertEqual(node.type_id, None) |
|---|
| 295 | |
|---|
| 296 | class TestExplicitTyping(unittest.TestCase): |
|---|
| 297 | |
|---|
| 298 | def testExplicitTyping(self): |
|---|
| 299 | parser = _syck.Parser(EXPLICIT_TYPING[0]) |
|---|
| 300 | for node, type_id in zip(parser.parse().value, EXPLICIT_TYPING[1]): |
|---|
| 301 | self.assertEqual(node.type_id, type_id) |
|---|
| 302 | |
|---|
| [3] | 303 | def main(module='__main__'): |
|---|
| 304 | unittest.main(module) |
|---|
| 305 | |
|---|
| 306 | if __name__ == '__main__': |
|---|
| 307 | main() |
|---|
| 308 | |
|---|