test_contains.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. from test.test_support import have_unicode, run_unittest
  2. import unittest
  3. class base_set:
  4. def __init__(self, el):
  5. self.el = el
  6. class set(base_set):
  7. def __contains__(self, el):
  8. return self.el == el
  9. class seq(base_set):
  10. def __getitem__(self, n):
  11. return [self.el][n]
  12. class TestContains(unittest.TestCase):
  13. def test_common_tests(self):
  14. a = base_set(1)
  15. b = set(1)
  16. c = seq(1)
  17. self.assertIn(1, b)
  18. self.assertNotIn(0, b)
  19. self.assertIn(1, c)
  20. self.assertNotIn(0, c)
  21. self.assertRaises(TypeError, lambda: 1 in a)
  22. self.assertRaises(TypeError, lambda: 1 not in a)
  23. # test char in string
  24. self.assertIn('c', 'abc')
  25. self.assertNotIn('d', 'abc')
  26. self.assertIn('', '')
  27. self.assertIn('', 'abc')
  28. self.assertRaises(TypeError, lambda: None in 'abc')
  29. if have_unicode:
  30. def test_char_in_unicode(self):
  31. self.assertIn('c', unicode('abc'))
  32. self.assertNotIn('d', unicode('abc'))
  33. self.assertIn('', unicode(''))
  34. self.assertIn(unicode(''), '')
  35. self.assertIn(unicode(''), unicode(''))
  36. self.assertIn('', unicode('abc'))
  37. self.assertIn(unicode(''), 'abc')
  38. self.assertIn(unicode(''), unicode('abc'))
  39. self.assertRaises(TypeError, lambda: None in unicode('abc'))
  40. # test Unicode char in Unicode
  41. self.assertIn(unicode('c'), unicode('abc'))
  42. self.assertNotIn(unicode('d'), unicode('abc'))
  43. # test Unicode char in string
  44. self.assertIn(unicode('c'), 'abc')
  45. self.assertNotIn(unicode('d'), 'abc')
  46. def test_builtin_sequence_types(self):
  47. # a collection of tests on builtin sequence types
  48. a = range(10)
  49. for i in a:
  50. self.assertIn(i, a)
  51. self.assertNotIn(16, a)
  52. self.assertNotIn(a, a)
  53. a = tuple(a)
  54. for i in a:
  55. self.assertIn(i, a)
  56. self.assertNotIn(16, a)
  57. self.assertNotIn(a, a)
  58. class Deviant1:
  59. """Behaves strangely when compared
  60. This class is designed to make sure that the contains code
  61. works when the list is modified during the check.
  62. """
  63. aList = range(15)
  64. def __cmp__(self, other):
  65. if other == 12:
  66. self.aList.remove(12)
  67. self.aList.remove(13)
  68. self.aList.remove(14)
  69. return 1
  70. self.assertNotIn(Deviant1(), Deviant1.aList)
  71. class Deviant2:
  72. """Behaves strangely when compared
  73. This class raises an exception during comparison. That in
  74. turn causes the comparison to fail with a TypeError.
  75. """
  76. def __cmp__(self, other):
  77. if other == 4:
  78. raise RuntimeError, "gotcha"
  79. try:
  80. self.assertNotIn(Deviant2(), a)
  81. except TypeError:
  82. pass
  83. def test_main():
  84. run_unittest(TestContains)
  85. if __name__ == '__main__':
  86. test_main()