Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 1 | "Test the functionality of Python classes implementing operators." |
| 2 | |
| 3 | |
| 4 | testmeths = [ |
| 5 | |
| 6 | # Binary operations |
| 7 | "add", |
| 8 | "radd", |
| 9 | "sub", |
| 10 | "rsub", |
| 11 | "mul", |
| 12 | "rmul", |
| 13 | "div", |
| 14 | "rdiv", |
| 15 | "mod", |
| 16 | "rmod", |
| 17 | "divmod", |
| 18 | "rdivmod", |
| 19 | "pow", |
| 20 | "rpow", |
| 21 | "rshift", |
| 22 | "rrshift", |
| 23 | "lshift", |
| 24 | "rlshift", |
| 25 | "and", |
| 26 | "rand", |
| 27 | "or", |
| 28 | "ror", |
| 29 | "xor", |
| 30 | "rxor", |
| 31 | |
| 32 | # List/dict operations |
| 33 | "contains", |
| 34 | "getitem", |
| 35 | "getslice", |
| 36 | "setitem", |
| 37 | "setslice", |
| 38 | "delitem", |
| 39 | "delslice", |
| 40 | |
| 41 | # Unary operations |
| 42 | "neg", |
| 43 | "pos", |
| 44 | "abs", |
| 45 | "int", |
| 46 | "long", |
| 47 | "float", |
| 48 | "oct", |
| 49 | "hex", |
| 50 | |
| 51 | # generic operations |
| 52 | "init", |
| 53 | "del", |
| 54 | ] |
| 55 | |
| 56 | # These need to return something other than None |
| 57 | # "coerce", |
| 58 | # "hash", |
| 59 | # "str", |
| 60 | # "repr", |
| 61 | |
| 62 | # These are separate because they can influence the test of other methods. |
| 63 | # "getattr", |
| 64 | # "setattr", |
| 65 | # "delattr", |
| 66 | |
| 67 | class AllTests: |
| 68 | def __coerce__(self, *args): |
| 69 | print "__coerce__:", args |
| 70 | return (self,) + args |
| 71 | |
| 72 | def __hash__(self, *args): |
| 73 | print "__hash__:", args |
| 74 | return id(self) |
| 75 | |
| 76 | def __str__(self, *args): |
| 77 | print "__str__:", args |
| 78 | return "AllTests" |
| 79 | |
| 80 | def __repr__(self, *args): |
| 81 | print "__repr__:", args |
| 82 | return "AllTests" |
| 83 | |
| 84 | def __cmp__(self, *args): |
| 85 | print "__cmp__:", args |
| 86 | return 0 |
| 87 | |
| 88 | for method in testmeths: |
| 89 | exec("""def __%(method)s__(self, *args): |
| 90 | print "__%(method)s__:", args |
| 91 | """%locals(), AllTests.__dict__); |
| 92 | |
| 93 | # this also tests __init__ of course. |
| 94 | testme = AllTests() |
| 95 | |
| 96 | # Binary operations |
| 97 | |
| 98 | testme + 1 |
| 99 | 1 + testme |
| 100 | |
| 101 | testme - 1 |
| 102 | 1 - testme |
| 103 | |
| 104 | testme * 1 |
| 105 | 1 * testme |
| 106 | |
| 107 | testme / 1 |
| 108 | 1 / testme |
| 109 | |
| 110 | testme % 1 |
| 111 | 1 % testme |
| 112 | |
| 113 | divmod(testme,1) |
| 114 | divmod(1, testme) |
| 115 | |
| 116 | testme ** 1 |
| 117 | 1 ** testme |
| 118 | |
| 119 | testme >> 1 |
| 120 | 1 >> testme |
| 121 | |
| 122 | testme << 1 |
| 123 | 1 << testme |
| 124 | |
| 125 | testme & 1 |
| 126 | 1 & testme |
| 127 | |
| 128 | testme | 1 |
| 129 | 1 | testme |
| 130 | |
| 131 | testme ^ 1 |
| 132 | 1 ^ testme |
| 133 | |
| 134 | |
| 135 | # List/dict operations |
| 136 | |
| 137 | 1 in testme |
| 138 | |
| 139 | testme[1] |
| 140 | testme[1] = 1 |
| 141 | del testme[1] |
| 142 | |
| 143 | testme[:42] |
| 144 | testme[:42] = "The Answer" |
| 145 | del testme[:42] |
| 146 | |
| 147 | testme[2:1024:10] |
| 148 | testme[2:1024:10] = "A lot" |
| 149 | del testme[2:1024:10] |
| 150 | |
| 151 | testme[:42, ..., :24:, 24, 100] |
| 152 | testme[:42, ..., :24:, 24, 100] = "Strange" |
| 153 | del testme[:42, ..., :24:, 24, 100] |
| 154 | |
| 155 | |
| 156 | # Now remove the slice hooks to see if converting normal slices to slice |
| 157 | # object works. |
| 158 | |
| 159 | del AllTests.__getslice__ |
| 160 | del AllTests.__setslice__ |
| 161 | del AllTests.__delslice__ |
| 162 | |
| 163 | testme[:42] |
| 164 | testme[:42] = "The Answer" |
| 165 | del testme[:42] |
| 166 | |
| 167 | |
| 168 | # Unary operations |
| 169 | |
| 170 | -testme |
| 171 | +testme |
| 172 | abs(testme) |
| 173 | int(testme) |
| 174 | long(testme) |
| 175 | float(testme) |
| 176 | oct(testme) |
| 177 | hex(testme) |
| 178 | |
| 179 | |
| 180 | # And the rest... |
| 181 | |
| 182 | hash(testme) |
| 183 | repr(testme) |
| 184 | str(testme) |
| 185 | |
| 186 | testme == 1 |
| 187 | testme < 1 |
| 188 | testme > 1 |
| 189 | testme <> 1 |
| 190 | testme != 1 |
| 191 | 1 == testme |
| 192 | 1 < testme |
| 193 | 1 > testme |
| 194 | 1 <> testme |
| 195 | 1 != testme |
| 196 | |
| 197 | # This test has to be last (duh.) |
| 198 | |
| 199 | del testme |
| 200 | |
| 201 | |
| 202 | # Interfering tests |
| 203 | |
| 204 | class ExtraTests: |
| 205 | def __getattr__(self, *args): |
| 206 | print "__getattr__:", args |
| 207 | return "SomeVal" |
| 208 | |
| 209 | def __setattr__(self, *args): |
| 210 | print "__setattr__:", args |
| 211 | |
| 212 | def __delattr__(self, *args): |
| 213 | print "__delattr__:", args |
| 214 | |
| 215 | testme = ExtraTests() |
| 216 | testme.spam |
| 217 | testme.eggs = "spam, spam, spam and ham" |
| 218 | del testme.cardinal |
| 219 | |