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