Guido van Rossum | 24512e6 | 2000-03-06 21:00:29 +0000 | [diff] [blame] | 1 | from test_support import TestFailed |
| 2 | |
| 3 | class base_set: |
| 4 | |
| 5 | def __init__(self, el): |
| 6 | self.el = el |
| 7 | |
| 8 | class set(base_set): |
| 9 | |
| 10 | def __contains__(self, el): |
| 11 | return self.el == el |
| 12 | |
| 13 | class seq(base_set): |
| 14 | |
| 15 | def __getitem__(self, n): |
| 16 | return [self.el][n] |
| 17 | |
| 18 | def check(ok, *args): |
| 19 | if not ok: |
| 20 | raise TestFailed, join(map(str, args), " ") |
| 21 | |
| 22 | a = base_set(1) |
| 23 | b = set(1) |
| 24 | c = seq(1) |
| 25 | |
| 26 | check(1 in b, "1 not in set(1)") |
| 27 | check(0 not in b, "0 in set(1)") |
| 28 | check(1 in c, "1 not in seq(1)") |
| 29 | check(0 not in c, "0 in seq(1)") |
| 30 | |
| 31 | try: |
| 32 | 1 in a |
| 33 | check(0, "in base_set did not raise error") |
| 34 | except AttributeError: |
| 35 | pass |
| 36 | |
| 37 | try: |
| 38 | 1 not in a |
| 39 | check(0, "not in base_set did not raise error") |
| 40 | except AttributeError: |
| 41 | pass |
Guido van Rossum | da2361a | 2000-03-07 15:52:01 +0000 | [diff] [blame^] | 42 | |
| 43 | # Test char in string |
| 44 | |
| 45 | check('c' in 'abc', "'c' not in 'abc'") |
| 46 | check('d' not in 'abc', "'d' in 'abc'") |
| 47 | |
| 48 | try: |
| 49 | '' in 'abc' |
| 50 | check(0, "'' in 'abc' did not raise error") |
| 51 | except TypeError: |
| 52 | pass |
| 53 | |
| 54 | try: |
| 55 | 'ab' in 'abc' |
| 56 | check(0, "'ab' in 'abc' did not raise error") |
| 57 | except TypeError: |
| 58 | pass |
| 59 | |
| 60 | try: |
| 61 | None in 'abc' |
| 62 | check(0, "None in 'abc' did not raise error") |
| 63 | except TypeError: |
| 64 | pass |