0001 from test.test_support import TestFailed, have_unicode 0002 0003 class base_set: 0004 0005 def __init__(self, el): 0006 self.el = el 0007 0008 class set(base_set): 0009 0010 def __contains__(self, el): 0011 return self.el == el 0012 0013 class seq(base_set): 0014 0015 def __getitem__(self, n): 0016 return [self.el][n] 0017 0018 def check(ok, *args): 0019 if not ok: 0020 raise TestFailed, " ".join(map(str, args)) 0021 0022 a = base_set(1) 0023 b = set(1) 0024 c = seq(1) 0025 0026 check(1 in b, "1 not in set(1)") 0027 check(0 not in b, "0 in set(1)") 0028 check(1 in c, "1 not in seq(1)") 0029 check(0 not in c, "0 in seq(1)") 0030 0031 try: 0032 1 in a 0033 check(0, "in base_set did not raise error") 0034 except TypeError: 0035 pass 0036 0037 try: 0038 1 not in a 0039 check(0, "not in base_set did not raise error") 0040 except TypeError: 0041 pass 0042 0043 # Test char in string 0044 0045 check('c' in 'abc', "'c' not in 'abc'") 0046 check('d' not in 'abc', "'d' in 'abc'") 0047 0048 check('' in '', "'' not in ''") 0049 check('' in 'abc', "'' not in 'abc'") 0050 0051 try: 0052 None in 'abc' 0053 check(0, "None in 'abc' did not raise error") 0054 except TypeError: 0055 pass 0056 0057 0058 if have_unicode: 0059 0060 # Test char in Unicode 0061 0062 check('c' in unicode('abc'), "'c' not in u'abc'") 0063 check('d' not in unicode('abc'), "'d' in u'abc'") 0064 0065 check('' in unicode(''), "'' not in u''") 0066 check(unicode('') in '', "u'' not in ''") 0067 check(unicode('') in unicode(''), "u'' not in u''") 0068 check('' in unicode('abc'), "'' not in u'abc'") 0069 check(unicode('') in 'abc', "u'' not in 'abc'") 0070 check(unicode('') in unicode('abc'), "u'' not in u'abc'") 0071 0072 try: 0073 None in unicode('abc') 0074 check(0, "None in u'abc' did not raise error") 0075 except TypeError: 0076 pass 0077 0078 # Test Unicode char in Unicode 0079 0080 check(unicode('c') in unicode('abc'), "u'c' not in u'abc'") 0081 check(unicode('d') not in unicode('abc'), "u'd' in u'abc'") 0082 0083 # Test Unicode char in string 0084 0085 check(unicode('c') in 'abc', "u'c' not in 'abc'") 0086 check(unicode('d') not in 'abc', "u'd' in 'abc'") 0087 0088 # A collection of tests on builtin sequence types 0089 a = range(10) 0090 for i in a: 0091 check(i in a, "%r not in %r" % (i, a)) 0092 check(16 not in a, "16 not in %r" % (a,)) 0093 check(a not in a, "%s not in %r" % (a, a)) 0094 0095 a = tuple(a) 0096 for i in a: 0097 check(i in a, "%r not in %r" % (i, a)) 0098 check(16 not in a, "16 not in %r" % (a,)) 0099 check(a not in a, "%r not in %r" % (a, a)) 0100 0101 class Deviant1: 0102 """Behaves strangely when compared 0103 0104 This class is designed to make sure that the contains code 0105 works when the list is modified during the check. 0106 """ 0107 0108 aList = range(15) 0109 0110 def __cmp__(self, other): 0111 if other == 12: 0112 self.aList.remove(12) 0113 self.aList.remove(13) 0114 self.aList.remove(14) 0115 return 1 0116 0117 check(Deviant1() not in Deviant1.aList, "Deviant1 failed") 0118 0119 class Deviant2: 0120 """Behaves strangely when compared 0121 0122 This class raises an exception during comparison. That in 0123 turn causes the comparison to fail with a TypeError. 0124 """ 0125 0126 def __cmp__(self, other): 0127 if other == 4: 0128 raise RuntimeError, "gotcha" 0129 0130 try: 0131 check(Deviant2() not in a, "oops") 0132 except TypeError: 0133 pass 0134
Generated by PyXR 0.9.4