/Lib/test/test_contains.py

http://unladen-swallow.googlecode.com/ · Python · 111 lines · 74 code · 23 blank · 14 comment · 8 complexity · 9d7d115f3183f262c63e259762a48162 MD5 · raw file

  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.assert_(1 in b)
  18. self.assert_(0 not in b)
  19. self.assert_(1 in c)
  20. self.assert_(0 not in 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.assert_('c' in 'abc')
  25. self.assert_('d' not in 'abc')
  26. self.assert_('' in '')
  27. self.assert_('' in 'abc')
  28. self.assertRaises(TypeError, lambda: None in 'abc')
  29. if have_unicode:
  30. def test_char_in_unicode(self):
  31. self.assert_('c' in unicode('abc'))
  32. self.assert_('d' not in unicode('abc'))
  33. self.assert_('' in unicode(''))
  34. self.assert_(unicode('') in '')
  35. self.assert_(unicode('') in unicode(''))
  36. self.assert_('' in unicode('abc'))
  37. self.assert_(unicode('') in 'abc')
  38. self.assert_(unicode('') in unicode('abc'))
  39. self.assertRaises(TypeError, lambda: None in unicode('abc'))
  40. # test Unicode char in Unicode
  41. self.assert_(unicode('c') in unicode('abc'))
  42. self.assert_(unicode('d') not in unicode('abc'))
  43. # test Unicode char in string
  44. self.assert_(unicode('c') in 'abc')
  45. self.assert_(unicode('d') not in '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.assert_(i in a)
  51. self.assert_(16 not in a)
  52. self.assert_(a not in a)
  53. a = tuple(a)
  54. for i in a:
  55. self.assert_(i in a)
  56. self.assert_(16 not in a)
  57. self.assert_(a not in 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.assert_(Deviant1() not in 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.assert_(Deviant2() not in a)
  81. except TypeError:
  82. pass
  83. def test_main():
  84. run_unittest(TestContains)
  85. if __name__ == '__main__':
  86. test_main()