|
| 1 | +import unittest |
| 2 | +import mock |
| 3 | +import sys |
| 4 | + |
| 5 | +try: |
| 6 | + from StringIO import StringIO |
| 7 | +except ImportError: |
| 8 | + from io import StringIO |
| 9 | + |
| 10 | +import vdf |
| 11 | + |
| 12 | + |
| 13 | +class testcase_helpers_load(unittest.TestCase): |
| 14 | + def setUp(self): |
| 15 | + self.f = StringIO() |
| 16 | + |
| 17 | + def tearDown(self): |
| 18 | + self.f.close() |
| 19 | + |
| 20 | + @mock.patch("vdf.parse") |
| 21 | + def test_routine_loads(self, mock_parse): |
| 22 | + vdf.loads("") |
| 23 | + mock_parse.assert_called_with("") |
| 24 | + |
| 25 | + def test_routine_loads_assert(self): |
| 26 | + for t in [5, 5.5, 1.0j, None, [], (), {}, lambda: 0, sys.stdin, self.f]: |
| 27 | + self.assertRaises(AssertionError, vdf.loads, t) |
| 28 | + |
| 29 | + @mock.patch("vdf.parse") |
| 30 | + def test_routine_load(self, mock_parse): |
| 31 | + vdf.load(sys.stdin) |
| 32 | + mock_parse.assert_called_with(sys.stdin) |
| 33 | + |
| 34 | + vdf.load(self.f) |
| 35 | + mock_parse.assert_called_with(self.f) |
| 36 | + |
| 37 | + def test_routine_load_assert(self): |
| 38 | + for t in [5, 5.5, 1.0j, None, [], (), {}, lambda: 0, '']: |
| 39 | + self.assertRaises(AssertionError, vdf.load, t) |
| 40 | + |
| 41 | + |
| 42 | +class testcase_helpers_dump(unittest.TestCase): |
| 43 | + def setUp(self): |
| 44 | + self.f = StringIO() |
| 45 | + |
| 46 | + def tearDown(self): |
| 47 | + self.f.close() |
| 48 | + |
| 49 | + def test_routine_dumps_asserts(self): |
| 50 | + for x in [5, 5.5, 1.0j, True, None, (), {}, lambda: 0, sys.stdin, self.f]: |
| 51 | + for y in [5, 5.5, 1.0j, None, [], (), {}, lambda: 0, sys.stdin, self.f]: |
| 52 | + self.assertRaises(ValueError, vdf.dumps, x, y) |
| 53 | + |
| 54 | + def test_routine_dump_asserts(self): |
| 55 | + for x in [5, 5.5, 1.0j, True, None, (), {}, lambda: 0, sys.stdin, self.f]: |
| 56 | + for y in [5, 5.5, 1.0j, True, None, [], (), {}, lambda: 0]: |
| 57 | + self.assertRaises(ValueError, vdf.dump, x, y) |
| 58 | + |
| 59 | + def test_routine_dump_writing(self): |
| 60 | + src = {"asd": "123"} |
| 61 | + expected = vdf.dumps(src) |
| 62 | + |
| 63 | + vdf.dump(src, self.f) |
| 64 | + self.f.seek(0) |
| 65 | + |
| 66 | + self.assertEqual(expected, self.f.read()) |
| 67 | + |
| 68 | + |
| 69 | +class testcase_parse_routine(unittest.TestCase): |
| 70 | + def test_parse_bom_removal(self): |
| 71 | + for mark in vdf.BOMS: |
| 72 | + result = vdf.loads(mark + "") |
| 73 | + self.assertEqual(result, {}) |
| 74 | + |
| 75 | + def test_parse_asserts(self): |
| 76 | + for t in [5, 5.5, 1.0j, True, None, (), {}, lambda: 0]: |
| 77 | + self.assertRaises(ValueError, vdf.parse, t) |
| 78 | + |
| 79 | + def test_parse_file_source(self): |
| 80 | + self.assertEqual(vdf.parse(StringIO(" ")), {}) |
| 81 | + |
| 82 | + |
| 83 | +class testcase_VDF(unittest.TestCase): |
| 84 | + def test_format(self): |
| 85 | + tests = [ |
| 86 | + # empty test |
| 87 | + ['', {}], |
| 88 | + |
| 89 | + # simple key and values |
| 90 | + [ |
| 91 | + {'1': '1'}, |
| 92 | + '"1" "1"\n' |
| 93 | + ], |
| 94 | + [ |
| 95 | + {"a": "1", "b": "2"}, |
| 96 | + '"a" "1"\n"b" "2"\n' |
| 97 | + ], |
| 98 | + |
| 99 | + # nesting |
| 100 | + [ |
| 101 | + {"a": {"b": {"c": {"d": "1", "e": "2"}}}}, |
| 102 | + '"a"\n{\n"b"\n{\n"c"\n{\n"e" "2"\n"d" "1"\n}\n}\n}\n' |
| 103 | + ], |
| 104 | + [ |
| 105 | + '"a"\n{\n"b"\n{\n"c"\n{\n"e" "2"\n"d" "1"\n}\n}\n}\n"b" "2"\n', |
| 106 | + {"a": {"b": {"c": {"d": "1", "e": "2"}}}, "b": "2"} |
| 107 | + ], |
| 108 | + |
| 109 | + # ignoring comment lines |
| 110 | + [ |
| 111 | + "//comment text\n//comment", |
| 112 | + {} |
| 113 | + ], |
| 114 | + [ |
| 115 | + "//comment text\n//comment", |
| 116 | + {} |
| 117 | + ], |
| 118 | + [ |
| 119 | + '"a" "b" //comment text', |
| 120 | + {"a": "b"}], |
| 121 | + [ |
| 122 | + '//comment\n"a" "1"\n"b" "2" //comment', |
| 123 | + {"a": "1", "b": "2"} |
| 124 | + ], |
| 125 | + [ |
| 126 | + '"a"\n{//comment\n}//comment', |
| 127 | + {"a": {}} |
| 128 | + ], |
| 129 | + [ |
| 130 | + '"a" //comment\n{\n}', |
| 131 | + {"a": {}} |
| 132 | + ], |
| 133 | + |
| 134 | + |
| 135 | + # new lines in value |
| 136 | + [ |
| 137 | + r'"a" "xx\"xxx"', |
| 138 | + {"a": r'xx\"xxx'} |
| 139 | + ], |
| 140 | + [ |
| 141 | + '"a" "xx\\"\nxxx"', |
| 142 | + {"a": 'xx\\"\nxxx'} |
| 143 | + ], |
| 144 | + [ |
| 145 | + '"a" "\n\n\n\n"', |
| 146 | + {"a": '\n\n\n\n'} |
| 147 | + ], |
| 148 | + ] |
| 149 | + |
| 150 | + for test, expected in tests: |
| 151 | + if isinstance(test, dict): |
| 152 | + self.assertEqual(vdf.dumps(test), expected) |
| 153 | + else: |
| 154 | + self.assertEqual(vdf.loads(test), expected) |
| 155 | + |
| 156 | + def test_parse_exceptions(self): |
| 157 | + tests = [ |
| 158 | + |
| 159 | + # expect bracket - invalid syntax |
| 160 | + '"asd"\n"zxc" "333"\n"', |
| 161 | + |
| 162 | + # invalid syntax |
| 163 | + '"asd" "123"\n"zxc" "333"\n"', |
| 164 | + |
| 165 | + # unclosed parenthasis |
| 166 | + '"asd"\n{\n"zxc" "333"\n' |
| 167 | + ] |
| 168 | + |
| 169 | + for test in tests: |
| 170 | + self.assertRaises(SyntaxError, vdf.parse, test) |
0 commit comments