+++ /dev/null
-from test.test_support import have_unicode, run_unittest\r
-import unittest\r
-\r
-\r
-class base_set:\r
- def __init__(self, el):\r
- self.el = el\r
-\r
-class set(base_set):\r
- def __contains__(self, el):\r
- return self.el == el\r
-\r
-class seq(base_set):\r
- def __getitem__(self, n):\r
- return [self.el][n]\r
-\r
-\r
-class TestContains(unittest.TestCase):\r
- def test_common_tests(self):\r
- a = base_set(1)\r
- b = set(1)\r
- c = seq(1)\r
- self.assertIn(1, b)\r
- self.assertNotIn(0, b)\r
- self.assertIn(1, c)\r
- self.assertNotIn(0, c)\r
- self.assertRaises(TypeError, lambda: 1 in a)\r
- self.assertRaises(TypeError, lambda: 1 not in a)\r
-\r
- # test char in string\r
- self.assertIn('c', 'abc')\r
- self.assertNotIn('d', 'abc')\r
-\r
- self.assertIn('', '')\r
- self.assertIn('', 'abc')\r
-\r
- self.assertRaises(TypeError, lambda: None in 'abc')\r
-\r
- if have_unicode:\r
- def test_char_in_unicode(self):\r
- self.assertIn('c', unicode('abc'))\r
- self.assertNotIn('d', unicode('abc'))\r
-\r
- self.assertIn('', unicode(''))\r
- self.assertIn(unicode(''), '')\r
- self.assertIn(unicode(''), unicode(''))\r
- self.assertIn('', unicode('abc'))\r
- self.assertIn(unicode(''), 'abc')\r
- self.assertIn(unicode(''), unicode('abc'))\r
-\r
- self.assertRaises(TypeError, lambda: None in unicode('abc'))\r
-\r
- # test Unicode char in Unicode\r
- self.assertIn(unicode('c'), unicode('abc'))\r
- self.assertNotIn(unicode('d'), unicode('abc'))\r
-\r
- # test Unicode char in string\r
- self.assertIn(unicode('c'), 'abc')\r
- self.assertNotIn(unicode('d'), 'abc')\r
-\r
- def test_builtin_sequence_types(self):\r
- # a collection of tests on builtin sequence types\r
- a = range(10)\r
- for i in a:\r
- self.assertIn(i, a)\r
- self.assertNotIn(16, a)\r
- self.assertNotIn(a, a)\r
-\r
- a = tuple(a)\r
- for i in a:\r
- self.assertIn(i, a)\r
- self.assertNotIn(16, a)\r
- self.assertNotIn(a, a)\r
-\r
- class Deviant1:\r
- """Behaves strangely when compared\r
-\r
- This class is designed to make sure that the contains code\r
- works when the list is modified during the check.\r
- """\r
- aList = range(15)\r
- def __cmp__(self, other):\r
- if other == 12:\r
- self.aList.remove(12)\r
- self.aList.remove(13)\r
- self.aList.remove(14)\r
- return 1\r
-\r
- self.assertNotIn(Deviant1(), Deviant1.aList)\r
-\r
- class Deviant2:\r
- """Behaves strangely when compared\r
-\r
- This class raises an exception during comparison. That in\r
- turn causes the comparison to fail with a TypeError.\r
- """\r
- def __cmp__(self, other):\r
- if other == 4:\r
- raise RuntimeError, "gotcha"\r
-\r
- try:\r
- self.assertNotIn(Deviant2(), a)\r
- except TypeError:\r
- pass\r
-\r
-\r
-def test_main():\r
- run_unittest(TestContains)\r
-\r
-if __name__ == '__main__':\r
- test_main()\r