2 from test
import test_support
6 class StructSeqTest(unittest
.TestCase
):
11 self
.assertEqual(len(t
), len(astuple
))
12 self
.assertEqual(t
, astuple
)
14 # Check that slicing works the same way; at one point, slicing t[i:j] with
15 # 0 < i < j could produce NULLs in the result.
16 for i
in xrange(-len(t
), len(t
)):
17 self
.assertEqual(t
[i
:], astuple
[i
:])
18 for j
in xrange(-len(t
), len(t
)):
19 self
.assertEqual(t
[i
:j
], astuple
[i
:j
])
21 for j
in xrange(-len(t
), len(t
)):
22 self
.assertEqual(t
[:j
], astuple
[:j
])
24 self
.assertRaises(IndexError, t
.__getitem
__, -len(t
)-1)
25 self
.assertRaises(IndexError, t
.__getitem
__, len(t
))
26 for i
in xrange(-len(t
), len(t
)-1):
27 self
.assertEqual(t
[i
], astuple
[i
])
31 self
.assertTrue(repr(t
))
33 self
.assertEqual(repr(t
),
34 "time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=0, "
35 "tm_min=0, tm_sec=0, tm_wday=3, tm_yday=1, tm_isdst=0)")
37 def test_concat(self
):
40 for i
in xrange(len(t1
)):
41 self
.assertEqual(t2
[i
], t2
[i
+len(t1
)])
43 def test_repeat(self
):
46 for i
in xrange(len(t1
)):
47 self
.assertEqual(t2
[i
], t2
[i
+len(t1
)])
48 self
.assertEqual(t2
[i
], t2
[i
+2*len(t1
)])
50 def test_contains(self
):
53 self
.assertTrue(item
in t1
)
54 self
.assertTrue(-42 not in t1
)
58 self
.assertEqual(hash(t1
), hash(tuple(t1
)))
63 self
.assertEqual(t1
, t2
)
64 self
.assertTrue(not (t1
< t2
))
65 self
.assertTrue(t1
<= t2
)
66 self
.assertTrue(not (t1
> t2
))
67 self
.assertTrue(t1
>= t2
)
68 self
.assertTrue(not (t1
!= t2
))
70 def test_fields(self
):
72 self
.assertEqual(len(t
), t
.n_fields
)
73 self
.assertEqual(t
.n_fields
, t
.n_sequence_fields
+t
.n_unnamed_fields
)
75 def test_constructor(self
):
78 self
.assertRaises(TypeError, t
)
79 self
.assertRaises(TypeError, t
, None)
80 self
.assertRaises(TypeError, t
, "123")
81 self
.assertRaises(TypeError, t
, "123", dict={})
82 self
.assertRaises(TypeError, t
, "123456789", dict=None)
85 self
.assertEqual("".join(t(s
)), s
)
87 def test_eviltuple(self
):
91 # Devious code could crash structseqs' contructors
93 def __getitem__(self
, i
):
98 self
.assertRaises(Exc
, time
.struct_time
, C())
100 def test_reduce(self
):
104 def test_extended_getslice(self
):
105 # Test extended slicing by comparing with list slicing.
108 indices
= (0, None, 1, 3, 19, 300, -1, -2, -31, -300)
109 for start
in indices
:
111 # Skip step 0 (invalid)
112 for step
in indices
[1:]:
113 self
.assertEqual(list(t
[start
:stop
:step
]),
117 test_support
.run_unittest(StructSeqTest
)
119 if __name__
== "__main__":