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