Skip to content

Commit c0b8c16

Browse files
committed
Relocate marshal tests by our convention
1 parent e8c5936 commit c0b8c16

File tree

2 files changed

+75
-73
lines changed

2 files changed

+75
-73
lines changed
Lines changed: 75 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,78 @@
1+
import unittest
12
import marshal
2-
orig = compile("1 + 1", "", 'eval')
33

4-
dumped = marshal.dumps(orig)
5-
loaded = marshal.loads(dumped)
4+
class MarshalTests(unittest.TestCase):
5+
"""
6+
Testing the (incomplete) marshal module.
7+
"""
8+
9+
def dump_then_load(self, data):
10+
return marshal.loads(marshal.dumps(data))
611

7-
assert eval(loaded) == eval(orig)
12+
def _test_marshal(self, data):
13+
self.assertEqual(self.dump_then_load(data), data)
14+
15+
def test_marshal_int(self):
16+
self._test_marshal(0)
17+
self._test_marshal(-1)
18+
self._test_marshal(1)
19+
self._test_marshal(100000000)
20+
21+
def test_marshal_bool(self):
22+
self._test_marshal(True)
23+
self._test_marshal(False)
24+
25+
def test_marshal_float(self):
26+
self._test_marshal(0.0)
27+
self._test_marshal(-10.0)
28+
self._test_marshal(10.0)
29+
30+
def test_marshal_str(self):
31+
self._test_marshal("")
32+
self._test_marshal("Hello, World")
33+
34+
def test_marshal_list(self):
35+
self._test_marshal([])
36+
self._test_marshal([1, "hello", 1.0])
37+
self._test_marshal([[0], ['a','b']])
38+
39+
def test_marshal_tuple(self):
40+
self._test_marshal(())
41+
self._test_marshal((1, "hello", 1.0))
42+
43+
def test_marshal_dict(self):
44+
self._test_marshal({})
45+
self._test_marshal({'a':1, 1:'a'})
46+
self._test_marshal({'a':{'b':2}, 'c':[0.0, 4.0, 6, 9]})
47+
48+
def test_marshal_set(self):
49+
self._test_marshal(set())
50+
self._test_marshal({1, 2, 3})
51+
self._test_marshal({1, 'a', 'b'})
52+
53+
def test_marshal_frozen_set(self):
54+
self._test_marshal(frozenset())
55+
self._test_marshal(frozenset({1, 2, 3}))
56+
self._test_marshal(frozenset({1, 'a', 'b'}))
57+
58+
def test_marshal_bytearray(self):
59+
self.assertEqual(
60+
self.dump_then_load(bytearray([])),
61+
bytearray(b''),
62+
)
63+
self.assertEqual(
64+
self.dump_then_load(bytearray([1, 2])),
65+
bytearray(b'\x01\x02'),
66+
)
67+
68+
def test_roundtrip(self):
69+
orig = compile("1 + 1", "", 'eval')
70+
71+
dumped = marshal.dumps(orig)
72+
loaded = marshal.loads(dumped)
73+
74+
assert eval(loaded) == eval(orig)
75+
76+
77+
if __name__ == "__main__":
78+
unittest.main()

extra_tests/snippets/test_marshal.py

Lines changed: 0 additions & 69 deletions
This file was deleted.

0 commit comments

Comments
 (0)