| 1 | # encoding: utf-8 |
|---|
| 2 | |
|---|
| 3 | import unittest |
|---|
| 4 | import syck |
|---|
| 5 | |
|---|
| 6 | import warnings |
|---|
| 7 | |
|---|
| 8 | STRINGS = [ |
|---|
| 9 | ("John Cage", 'tag:yaml.org,2002:str'), |
|---|
| 10 | ("Béla Bartók", 'tag:python.yaml.org,2002:str'), |
|---|
| 11 | ("ÐалеМÑОМ СОлÑвеÑÑÑПв", 'tag:python.yaml.org,2002:str'), |
|---|
| 12 | (''.join([chr(k) for k in range(256)]), 'tag:yaml.org,2002:binary') |
|---|
| 13 | ] |
|---|
| 14 | |
|---|
| 15 | UNICODE_STRINGS = [ |
|---|
| 16 | (u"John Cage", 'tag:python.yaml.org,2002:unicode'), |
|---|
| 17 | (u"Béla Bartók", 'tag:yaml.org,2002:str'), |
|---|
| 18 | (u"ÐалеМÑОМ СОлÑвеÑÑÑПв", 'tag:yaml.org,2002:str'), |
|---|
| 19 | (u''.join([unichr(k) for k in range(512)]), 'tag:yaml.org,2002:str') |
|---|
| 20 | ] |
|---|
| 21 | |
|---|
| 22 | DOCUMENT = """ |
|---|
| 23 | - John Cage |
|---|
| 24 | - Béla Bartók |
|---|
| 25 | - ÐалеМÑОМ СОлÑвеÑÑÑПв |
|---|
| 26 | - \x80\x81\x82\x83\x84\x85\x86\x87 |
|---|
| 27 | """, ["John Cage", u"Béla Bartók", u"ÐалеМÑОМ СОлÑвеÑÑÑПв", '\x80\x81\x82\x83\x84\x85\x86\x87'] |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | class TestUnicode(unittest.TestCase): |
|---|
| 31 | |
|---|
| 32 | def testDumpStr(self): |
|---|
| 33 | for string, tag in STRINGS: |
|---|
| 34 | #print string |
|---|
| 35 | document = syck.dump(string) |
|---|
| 36 | #print document |
|---|
| 37 | new_tag = syck.parse(document).tag |
|---|
| 38 | new_string = syck.load(document) |
|---|
| 39 | self.assertEqual(string, new_string) |
|---|
| 40 | self.assertEqual(type(string), type(new_string)) |
|---|
| 41 | self.assertEqual(tag, new_tag) |
|---|
| 42 | |
|---|
| 43 | def testDumpUnicode(self): |
|---|
| 44 | for string, tag in UNICODE_STRINGS: |
|---|
| 45 | #print string |
|---|
| 46 | document = syck.dump(string) |
|---|
| 47 | #print document |
|---|
| 48 | new_tag = syck.parse(document).tag |
|---|
| 49 | new_string = syck.load(document) |
|---|
| 50 | self.assertEqual(string, new_string) |
|---|
| 51 | self.assertEqual(type(string), type(new_string)) |
|---|
| 52 | self.assertEqual(tag, new_tag) |
|---|
| 53 | |
|---|
| 54 | def testLoad(self): |
|---|
| 55 | self._testWarning() |
|---|
| 56 | document, values = DOCUMENT |
|---|
| 57 | new_values = syck.load(document) |
|---|
| 58 | for string, new_string in zip(values, new_values): |
|---|
| 59 | self.assertEqual(string, new_string) |
|---|
| 60 | self.assertEqual(type(string), type(new_string)) |
|---|
| 61 | |
|---|
| 62 | def _testWarning(self): |
|---|
| 63 | warnings.simplefilter('error') |
|---|
| 64 | document = '\x80\x81\x82\x83\x84\x85\x86\x87' |
|---|
| 65 | self.assertRaises(syck.NotUnicodeInputWarning, lambda: syck.load(document)) |
|---|
| 66 | warnings.resetwarnings() |
|---|
| 67 | |
|---|
| 68 | |
|---|