ยปCore Development>Code coverage>Lib/test/test_structseq.py

Python code coverage for Lib/test/test_structseq.py

#countcontent
1n/aimport os
2n/aimport time
3n/aimport unittest
4n/a
5n/a
6n/aclass StructSeqTest(unittest.TestCase):
7n/a
8n/a def test_tuple(self):
9n/a t = time.gmtime()
10n/a self.assertIsInstance(t, tuple)
11n/a astuple = tuple(t)
12n/a self.assertEqual(len(t), len(astuple))
13n/a self.assertEqual(t, astuple)
14n/a
15n/a # Check that slicing works the same way; at one point, slicing t[i:j] with
16n/a # 0 < i < j could produce NULLs in the result.
17n/a for i in range(-len(t), len(t)):
18n/a self.assertEqual(t[i:], astuple[i:])
19n/a for j in range(-len(t), len(t)):
20n/a self.assertEqual(t[i:j], astuple[i:j])
21n/a
22n/a for j in range(-len(t), len(t)):
23n/a self.assertEqual(t[:j], astuple[:j])
24n/a
25n/a self.assertRaises(IndexError, t.__getitem__, -len(t)-1)
26n/a self.assertRaises(IndexError, t.__getitem__, len(t))
27n/a for i in range(-len(t), len(t)-1):
28n/a self.assertEqual(t[i], astuple[i])
29n/a
30n/a def test_repr(self):
31n/a t = time.gmtime()
32n/a self.assertTrue(repr(t))
33n/a t = time.gmtime(0)
34n/a self.assertEqual(repr(t),
35n/a "time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=0, "
36n/a "tm_min=0, tm_sec=0, tm_wday=3, tm_yday=1, tm_isdst=0)")
37n/a # os.stat() gives a complicated struct sequence.
38n/a st = os.stat(__file__)
39n/a rep = repr(st)
40n/a self.assertTrue(rep.startswith("os.stat_result"))
41n/a self.assertIn("st_mode=", rep)
42n/a self.assertIn("st_ino=", rep)
43n/a self.assertIn("st_dev=", rep)
44n/a
45n/a def test_concat(self):
46n/a t1 = time.gmtime()
47n/a t2 = t1 + tuple(t1)
48n/a for i in range(len(t1)):
49n/a self.assertEqual(t2[i], t2[i+len(t1)])
50n/a
51n/a def test_repeat(self):
52n/a t1 = time.gmtime()
53n/a t2 = 3 * t1
54n/a for i in range(len(t1)):
55n/a self.assertEqual(t2[i], t2[i+len(t1)])
56n/a self.assertEqual(t2[i], t2[i+2*len(t1)])
57n/a
58n/a def test_contains(self):
59n/a t1 = time.gmtime()
60n/a for item in t1:
61n/a self.assertIn(item, t1)
62n/a self.assertNotIn(-42, t1)
63n/a
64n/a def test_hash(self):
65n/a t1 = time.gmtime()
66n/a self.assertEqual(hash(t1), hash(tuple(t1)))
67n/a
68n/a def test_cmp(self):
69n/a t1 = time.gmtime()
70n/a t2 = type(t1)(t1)
71n/a self.assertEqual(t1, t2)
72n/a self.assertTrue(not (t1 < t2))
73n/a self.assertTrue(t1 <= t2)
74n/a self.assertTrue(not (t1 > t2))
75n/a self.assertTrue(t1 >= t2)
76n/a self.assertTrue(not (t1 != t2))
77n/a
78n/a def test_fields(self):
79n/a t = time.gmtime()
80n/a self.assertEqual(len(t), t.n_sequence_fields)
81n/a self.assertEqual(t.n_unnamed_fields, 0)
82n/a self.assertEqual(t.n_fields, time._STRUCT_TM_ITEMS)
83n/a
84n/a def test_constructor(self):
85n/a t = time.struct_time
86n/a
87n/a self.assertRaises(TypeError, t)
88n/a self.assertRaises(TypeError, t, None)
89n/a self.assertRaises(TypeError, t, "123")
90n/a self.assertRaises(TypeError, t, "123", dict={})
91n/a self.assertRaises(TypeError, t, "123456789", dict=None)
92n/a
93n/a s = "123456789"
94n/a self.assertEqual("".join(t(s)), s)
95n/a
96n/a def test_eviltuple(self):
97n/a class Exc(Exception):
98n/a pass
99n/a
100n/a # Devious code could crash structseqs' constructors
101n/a class C:
102n/a def __getitem__(self, i):
103n/a raise Exc
104n/a def __len__(self):
105n/a return 9
106n/a
107n/a self.assertRaises(Exc, time.struct_time, C())
108n/a
109n/a def test_reduce(self):
110n/a t = time.gmtime()
111n/a x = t.__reduce__()
112n/a
113n/a def test_extended_getslice(self):
114n/a # Test extended slicing by comparing with list slicing.
115n/a t = time.gmtime()
116n/a L = list(t)
117n/a indices = (0, None, 1, 3, 19, 300, -1, -2, -31, -300)
118n/a for start in indices:
119n/a for stop in indices:
120n/a # Skip step 0 (invalid)
121n/a for step in indices[1:]:
122n/a self.assertEqual(list(t[start:stop:step]),
123n/a L[start:stop:step])
124n/a
125n/aif __name__ == "__main__":
126n/a unittest.main()