test_structseq.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. __author__ = 'Jacco Kulman'
  2. import unittest
  3. import time
  4. class StructSeqTest(unittest.TestCase):
  5. def _test_tuple(self):
  6. t = time.gmtime()
  7. self.assertIsInstance(t, tuple)
  8. astuple = tuple(t)
  9. self.assertEqual(len(t), len(astuple))
  10. self.assertEqual(t, astuple)
  11. # Check that slicing works the same way; at one point, slicing t[i:j] with
  12. # 0 < i < j could produce NULLs in the result.
  13. for i in range(-len(t), len(t)):
  14. self.assertEqual(t[i:], astuple[i:])
  15. for j in range(-len(t), len(t)):
  16. self.assertEqual(t[i:j], astuple[i:j])
  17. for j in range(-len(t), len(t)):
  18. self.assertEqual(t[:j], astuple[:j])
  19. self.assertRaises(IndexError, lambda: t.__getitem__(-len(t)-1))
  20. self.assertRaises(IndexError, lambda: t.__getitem__(len(t)))
  21. for i in range(-len(t), len(t)-1):
  22. self.assertEqual(t[i], astuple[i])
  23. def test_repr(self):
  24. t = time.gmtime()
  25. self.assertTrue(repr(t))
  26. t = time.gmtime(0)
  27. self.assertEqual(repr(t),
  28. "time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=0, "
  29. "tm_min=0, tm_sec=0, tm_wday=3, tm_yday=1, tm_isdst=0)")
  30. def test_concat(self):
  31. t1 = time.gmtime()
  32. t2 = t1 + tuple(t1)
  33. for i in xrange(len(t1)):
  34. self.assertEqual(t2[i], t2[i+len(t1)])
  35. def test_repeat(self):
  36. t1 = time.gmtime()
  37. t2 = 3 * t1
  38. for i in xrange(len(t1)):
  39. self.assertEqual(t2[i], t2[i+len(t1)])
  40. self.assertEqual(t2[i], t2[i+2*len(t1)])
  41. def test_contains(self):
  42. t1 = time.gmtime()
  43. for item in t1:
  44. self.assertIn(item, t1)
  45. self.assertNotIn(-42, t1)
  46. def test_hash(self):
  47. t1 = time.gmtime()
  48. self.assertEqual(hash(t1), hash(tuple(t1)))
  49. def test_cmp(self):
  50. t1 = time.gmtime()
  51. t2 = type(t1)(t1)
  52. self.assertEqual(t1, t2)
  53. self.assertTrue(not (t1 < t2))
  54. self.assertTrue(t1 <= t2)
  55. self.assertTrue(not (t1 > t2))
  56. self.assertTrue(t1 >= t2)
  57. self.assertTrue(not (t1 != t2))
  58. def _test_fields(self):
  59. t = time.gmtime()
  60. self.assertEqual(len(t), t.n_fields)
  61. self.assertEqual(t.n_fields, t.n_sequence_fields+t.n_unnamed_fields)
  62. def test_getattr(self):
  63. t = time.gmtime(0)
  64. self.assertEqual(t.tm_year, 1970)
  65. self.assertRaises(AttributeError, lambda : t.tm_nonexistent_attr)
  66. def test_constructor(self):
  67. t = time.struct_time
  68. self.assertRaises(TypeError, t)
  69. self.assertRaises(TypeError, t, None)
  70. self.assertRaises(TypeError, t, "123")
  71. #self.assertRaises(TypeError,t, "123", dict={})
  72. #self.assertRaises(ValueError,t, "123456789", dict=None)
  73. s = "123456789"
  74. self.assertEqual("".join(t(s)), s)
  75. def test_eviltuple(self):
  76. # Devious code could crash structseqs' contructors
  77. class C:
  78. def __getitem__(self, i):
  79. raise TypeError("eviltuple error")
  80. def __len__(self):
  81. return 9
  82. self.assertRaises(TypeError, time.struct_time, C())
  83. def _test_reduce(self):
  84. t = time.gmtime()
  85. x = t.__reduce__()
  86. def test_extended_getslice(self):
  87. # Test extended slicing by comparing with list slicing.
  88. t = time.gmtime()
  89. L = list(t)
  90. indices = (0, None, 1, 3, 19, 300, -1, -2, -31, -300)
  91. for start in indices:
  92. for stop in indices:
  93. # Skip step 0 (invalid)
  94. for step in indices[1:]:
  95. self.assertEqual(list(t[start:stop:step]),
  96. L[start:stop:step])
  97. def _test_writability(self):
  98. def change(s):
  99. s.tm_mon = 30
  100. x = time.struct_time((1,2,3,4,5,6,7,8,9))
  101. self.assertRaises(AttributeError, lambda: change(x))
  102. if __name__ == '__main__':
  103. unittest.main()