| 1 | n/a | """Tests for binary operators on subtypes of built-in types.""" |
|---|
| 2 | n/a | |
|---|
| 3 | n/a | import unittest |
|---|
| 4 | n/a | from test import support |
|---|
| 5 | n/a | from operator import eq, le, ne |
|---|
| 6 | n/a | from abc import ABCMeta |
|---|
| 7 | n/a | |
|---|
| 8 | n/a | def gcd(a, b): |
|---|
| 9 | n/a | """Greatest common divisor using Euclid's algorithm.""" |
|---|
| 10 | n/a | while a: |
|---|
| 11 | n/a | a, b = b%a, a |
|---|
| 12 | n/a | return b |
|---|
| 13 | n/a | |
|---|
| 14 | n/a | def isint(x): |
|---|
| 15 | n/a | """Test whether an object is an instance of int.""" |
|---|
| 16 | n/a | return isinstance(x, int) |
|---|
| 17 | n/a | |
|---|
| 18 | n/a | def isnum(x): |
|---|
| 19 | n/a | """Test whether an object is an instance of a built-in numeric type.""" |
|---|
| 20 | n/a | for T in int, float, complex: |
|---|
| 21 | n/a | if isinstance(x, T): |
|---|
| 22 | n/a | return 1 |
|---|
| 23 | n/a | return 0 |
|---|
| 24 | n/a | |
|---|
| 25 | n/a | def isRat(x): |
|---|
| 26 | n/a | """Test wheter an object is an instance of the Rat class.""" |
|---|
| 27 | n/a | return isinstance(x, Rat) |
|---|
| 28 | n/a | |
|---|
| 29 | n/a | class Rat(object): |
|---|
| 30 | n/a | |
|---|
| 31 | n/a | """Rational number implemented as a normalized pair of ints.""" |
|---|
| 32 | n/a | |
|---|
| 33 | n/a | __slots__ = ['_Rat__num', '_Rat__den'] |
|---|
| 34 | n/a | |
|---|
| 35 | n/a | def __init__(self, num=0, den=1): |
|---|
| 36 | n/a | """Constructor: Rat([num[, den]]). |
|---|
| 37 | n/a | |
|---|
| 38 | n/a | The arguments must be ints, and default to (0, 1).""" |
|---|
| 39 | n/a | if not isint(num): |
|---|
| 40 | n/a | raise TypeError("Rat numerator must be int (%r)" % num) |
|---|
| 41 | n/a | if not isint(den): |
|---|
| 42 | n/a | raise TypeError("Rat denominator must be int (%r)" % den) |
|---|
| 43 | n/a | # But the zero is always on |
|---|
| 44 | n/a | if den == 0: |
|---|
| 45 | n/a | raise ZeroDivisionError("zero denominator") |
|---|
| 46 | n/a | g = gcd(den, num) |
|---|
| 47 | n/a | self.__num = int(num//g) |
|---|
| 48 | n/a | self.__den = int(den//g) |
|---|
| 49 | n/a | |
|---|
| 50 | n/a | def _get_num(self): |
|---|
| 51 | n/a | """Accessor function for read-only 'num' attribute of Rat.""" |
|---|
| 52 | n/a | return self.__num |
|---|
| 53 | n/a | num = property(_get_num, None) |
|---|
| 54 | n/a | |
|---|
| 55 | n/a | def _get_den(self): |
|---|
| 56 | n/a | """Accessor function for read-only 'den' attribute of Rat.""" |
|---|
| 57 | n/a | return self.__den |
|---|
| 58 | n/a | den = property(_get_den, None) |
|---|
| 59 | n/a | |
|---|
| 60 | n/a | def __repr__(self): |
|---|
| 61 | n/a | """Convert a Rat to a string resembling a Rat constructor call.""" |
|---|
| 62 | n/a | return "Rat(%d, %d)" % (self.__num, self.__den) |
|---|
| 63 | n/a | |
|---|
| 64 | n/a | def __str__(self): |
|---|
| 65 | n/a | """Convert a Rat to a string resembling a decimal numeric value.""" |
|---|
| 66 | n/a | return str(float(self)) |
|---|
| 67 | n/a | |
|---|
| 68 | n/a | def __float__(self): |
|---|
| 69 | n/a | """Convert a Rat to a float.""" |
|---|
| 70 | n/a | return self.__num*1.0/self.__den |
|---|
| 71 | n/a | |
|---|
| 72 | n/a | def __int__(self): |
|---|
| 73 | n/a | """Convert a Rat to an int; self.den must be 1.""" |
|---|
| 74 | n/a | if self.__den == 1: |
|---|
| 75 | n/a | try: |
|---|
| 76 | n/a | return int(self.__num) |
|---|
| 77 | n/a | except OverflowError: |
|---|
| 78 | n/a | raise OverflowError("%s too large to convert to int" % |
|---|
| 79 | n/a | repr(self)) |
|---|
| 80 | n/a | raise ValueError("can't convert %s to int" % repr(self)) |
|---|
| 81 | n/a | |
|---|
| 82 | n/a | def __add__(self, other): |
|---|
| 83 | n/a | """Add two Rats, or a Rat and a number.""" |
|---|
| 84 | n/a | if isint(other): |
|---|
| 85 | n/a | other = Rat(other) |
|---|
| 86 | n/a | if isRat(other): |
|---|
| 87 | n/a | return Rat(self.__num*other.__den + other.__num*self.__den, |
|---|
| 88 | n/a | self.__den*other.__den) |
|---|
| 89 | n/a | if isnum(other): |
|---|
| 90 | n/a | return float(self) + other |
|---|
| 91 | n/a | return NotImplemented |
|---|
| 92 | n/a | |
|---|
| 93 | n/a | __radd__ = __add__ |
|---|
| 94 | n/a | |
|---|
| 95 | n/a | def __sub__(self, other): |
|---|
| 96 | n/a | """Subtract two Rats, or a Rat and a number.""" |
|---|
| 97 | n/a | if isint(other): |
|---|
| 98 | n/a | other = Rat(other) |
|---|
| 99 | n/a | if isRat(other): |
|---|
| 100 | n/a | return Rat(self.__num*other.__den - other.__num*self.__den, |
|---|
| 101 | n/a | self.__den*other.__den) |
|---|
| 102 | n/a | if isnum(other): |
|---|
| 103 | n/a | return float(self) - other |
|---|
| 104 | n/a | return NotImplemented |
|---|
| 105 | n/a | |
|---|
| 106 | n/a | def __rsub__(self, other): |
|---|
| 107 | n/a | """Subtract two Rats, or a Rat and a number (reversed args).""" |
|---|
| 108 | n/a | if isint(other): |
|---|
| 109 | n/a | other = Rat(other) |
|---|
| 110 | n/a | if isRat(other): |
|---|
| 111 | n/a | return Rat(other.__num*self.__den - self.__num*other.__den, |
|---|
| 112 | n/a | self.__den*other.__den) |
|---|
| 113 | n/a | if isnum(other): |
|---|
| 114 | n/a | return other - float(self) |
|---|
| 115 | n/a | return NotImplemented |
|---|
| 116 | n/a | |
|---|
| 117 | n/a | def __mul__(self, other): |
|---|
| 118 | n/a | """Multiply two Rats, or a Rat and a number.""" |
|---|
| 119 | n/a | if isRat(other): |
|---|
| 120 | n/a | return Rat(self.__num*other.__num, self.__den*other.__den) |
|---|
| 121 | n/a | if isint(other): |
|---|
| 122 | n/a | return Rat(self.__num*other, self.__den) |
|---|
| 123 | n/a | if isnum(other): |
|---|
| 124 | n/a | return float(self)*other |
|---|
| 125 | n/a | return NotImplemented |
|---|
| 126 | n/a | |
|---|
| 127 | n/a | __rmul__ = __mul__ |
|---|
| 128 | n/a | |
|---|
| 129 | n/a | def __truediv__(self, other): |
|---|
| 130 | n/a | """Divide two Rats, or a Rat and a number.""" |
|---|
| 131 | n/a | if isRat(other): |
|---|
| 132 | n/a | return Rat(self.__num*other.__den, self.__den*other.__num) |
|---|
| 133 | n/a | if isint(other): |
|---|
| 134 | n/a | return Rat(self.__num, self.__den*other) |
|---|
| 135 | n/a | if isnum(other): |
|---|
| 136 | n/a | return float(self) / other |
|---|
| 137 | n/a | return NotImplemented |
|---|
| 138 | n/a | |
|---|
| 139 | n/a | def __rtruediv__(self, other): |
|---|
| 140 | n/a | """Divide two Rats, or a Rat and a number (reversed args).""" |
|---|
| 141 | n/a | if isRat(other): |
|---|
| 142 | n/a | return Rat(other.__num*self.__den, other.__den*self.__num) |
|---|
| 143 | n/a | if isint(other): |
|---|
| 144 | n/a | return Rat(other*self.__den, self.__num) |
|---|
| 145 | n/a | if isnum(other): |
|---|
| 146 | n/a | return other / float(self) |
|---|
| 147 | n/a | return NotImplemented |
|---|
| 148 | n/a | |
|---|
| 149 | n/a | def __floordiv__(self, other): |
|---|
| 150 | n/a | """Divide two Rats, returning the floored result.""" |
|---|
| 151 | n/a | if isint(other): |
|---|
| 152 | n/a | other = Rat(other) |
|---|
| 153 | n/a | elif not isRat(other): |
|---|
| 154 | n/a | return NotImplemented |
|---|
| 155 | n/a | x = self/other |
|---|
| 156 | n/a | return x.__num // x.__den |
|---|
| 157 | n/a | |
|---|
| 158 | n/a | def __rfloordiv__(self, other): |
|---|
| 159 | n/a | """Divide two Rats, returning the floored result (reversed args).""" |
|---|
| 160 | n/a | x = other/self |
|---|
| 161 | n/a | return x.__num // x.__den |
|---|
| 162 | n/a | |
|---|
| 163 | n/a | def __divmod__(self, other): |
|---|
| 164 | n/a | """Divide two Rats, returning quotient and remainder.""" |
|---|
| 165 | n/a | if isint(other): |
|---|
| 166 | n/a | other = Rat(other) |
|---|
| 167 | n/a | elif not isRat(other): |
|---|
| 168 | n/a | return NotImplemented |
|---|
| 169 | n/a | x = self//other |
|---|
| 170 | n/a | return (x, self - other * x) |
|---|
| 171 | n/a | |
|---|
| 172 | n/a | def __rdivmod__(self, other): |
|---|
| 173 | n/a | """Divide two Rats, returning quotient and remainder (reversed args).""" |
|---|
| 174 | n/a | if isint(other): |
|---|
| 175 | n/a | other = Rat(other) |
|---|
| 176 | n/a | elif not isRat(other): |
|---|
| 177 | n/a | return NotImplemented |
|---|
| 178 | n/a | return divmod(other, self) |
|---|
| 179 | n/a | |
|---|
| 180 | n/a | def __mod__(self, other): |
|---|
| 181 | n/a | """Take one Rat modulo another.""" |
|---|
| 182 | n/a | return divmod(self, other)[1] |
|---|
| 183 | n/a | |
|---|
| 184 | n/a | def __rmod__(self, other): |
|---|
| 185 | n/a | """Take one Rat modulo another (reversed args).""" |
|---|
| 186 | n/a | return divmod(other, self)[1] |
|---|
| 187 | n/a | |
|---|
| 188 | n/a | def __eq__(self, other): |
|---|
| 189 | n/a | """Compare two Rats for equality.""" |
|---|
| 190 | n/a | if isint(other): |
|---|
| 191 | n/a | return self.__den == 1 and self.__num == other |
|---|
| 192 | n/a | if isRat(other): |
|---|
| 193 | n/a | return self.__num == other.__num and self.__den == other.__den |
|---|
| 194 | n/a | if isnum(other): |
|---|
| 195 | n/a | return float(self) == other |
|---|
| 196 | n/a | return NotImplemented |
|---|
| 197 | n/a | |
|---|
| 198 | n/a | class RatTestCase(unittest.TestCase): |
|---|
| 199 | n/a | """Unit tests for Rat class and its support utilities.""" |
|---|
| 200 | n/a | |
|---|
| 201 | n/a | def test_gcd(self): |
|---|
| 202 | n/a | self.assertEqual(gcd(10, 12), 2) |
|---|
| 203 | n/a | self.assertEqual(gcd(10, 15), 5) |
|---|
| 204 | n/a | self.assertEqual(gcd(10, 11), 1) |
|---|
| 205 | n/a | self.assertEqual(gcd(100, 15), 5) |
|---|
| 206 | n/a | self.assertEqual(gcd(-10, 2), -2) |
|---|
| 207 | n/a | self.assertEqual(gcd(10, -2), 2) |
|---|
| 208 | n/a | self.assertEqual(gcd(-10, -2), -2) |
|---|
| 209 | n/a | for i in range(1, 20): |
|---|
| 210 | n/a | for j in range(1, 20): |
|---|
| 211 | n/a | self.assertTrue(gcd(i, j) > 0) |
|---|
| 212 | n/a | self.assertTrue(gcd(-i, j) < 0) |
|---|
| 213 | n/a | self.assertTrue(gcd(i, -j) > 0) |
|---|
| 214 | n/a | self.assertTrue(gcd(-i, -j) < 0) |
|---|
| 215 | n/a | |
|---|
| 216 | n/a | def test_constructor(self): |
|---|
| 217 | n/a | a = Rat(10, 15) |
|---|
| 218 | n/a | self.assertEqual(a.num, 2) |
|---|
| 219 | n/a | self.assertEqual(a.den, 3) |
|---|
| 220 | n/a | a = Rat(10, -15) |
|---|
| 221 | n/a | self.assertEqual(a.num, -2) |
|---|
| 222 | n/a | self.assertEqual(a.den, 3) |
|---|
| 223 | n/a | a = Rat(-10, 15) |
|---|
| 224 | n/a | self.assertEqual(a.num, -2) |
|---|
| 225 | n/a | self.assertEqual(a.den, 3) |
|---|
| 226 | n/a | a = Rat(-10, -15) |
|---|
| 227 | n/a | self.assertEqual(a.num, 2) |
|---|
| 228 | n/a | self.assertEqual(a.den, 3) |
|---|
| 229 | n/a | a = Rat(7) |
|---|
| 230 | n/a | self.assertEqual(a.num, 7) |
|---|
| 231 | n/a | self.assertEqual(a.den, 1) |
|---|
| 232 | n/a | try: |
|---|
| 233 | n/a | a = Rat(1, 0) |
|---|
| 234 | n/a | except ZeroDivisionError: |
|---|
| 235 | n/a | pass |
|---|
| 236 | n/a | else: |
|---|
| 237 | n/a | self.fail("Rat(1, 0) didn't raise ZeroDivisionError") |
|---|
| 238 | n/a | for bad in "0", 0.0, 0j, (), [], {}, None, Rat, unittest: |
|---|
| 239 | n/a | try: |
|---|
| 240 | n/a | a = Rat(bad) |
|---|
| 241 | n/a | except TypeError: |
|---|
| 242 | n/a | pass |
|---|
| 243 | n/a | else: |
|---|
| 244 | n/a | self.fail("Rat(%r) didn't raise TypeError" % bad) |
|---|
| 245 | n/a | try: |
|---|
| 246 | n/a | a = Rat(1, bad) |
|---|
| 247 | n/a | except TypeError: |
|---|
| 248 | n/a | pass |
|---|
| 249 | n/a | else: |
|---|
| 250 | n/a | self.fail("Rat(1, %r) didn't raise TypeError" % bad) |
|---|
| 251 | n/a | |
|---|
| 252 | n/a | def test_add(self): |
|---|
| 253 | n/a | self.assertEqual(Rat(2, 3) + Rat(1, 3), 1) |
|---|
| 254 | n/a | self.assertEqual(Rat(2, 3) + 1, Rat(5, 3)) |
|---|
| 255 | n/a | self.assertEqual(1 + Rat(2, 3), Rat(5, 3)) |
|---|
| 256 | n/a | self.assertEqual(1.0 + Rat(1, 2), 1.5) |
|---|
| 257 | n/a | self.assertEqual(Rat(1, 2) + 1.0, 1.5) |
|---|
| 258 | n/a | |
|---|
| 259 | n/a | def test_sub(self): |
|---|
| 260 | n/a | self.assertEqual(Rat(7, 2) - Rat(7, 5), Rat(21, 10)) |
|---|
| 261 | n/a | self.assertEqual(Rat(7, 5) - 1, Rat(2, 5)) |
|---|
| 262 | n/a | self.assertEqual(1 - Rat(3, 5), Rat(2, 5)) |
|---|
| 263 | n/a | self.assertEqual(Rat(3, 2) - 1.0, 0.5) |
|---|
| 264 | n/a | self.assertEqual(1.0 - Rat(1, 2), 0.5) |
|---|
| 265 | n/a | |
|---|
| 266 | n/a | def test_mul(self): |
|---|
| 267 | n/a | self.assertEqual(Rat(2, 3) * Rat(5, 7), Rat(10, 21)) |
|---|
| 268 | n/a | self.assertEqual(Rat(10, 3) * 3, 10) |
|---|
| 269 | n/a | self.assertEqual(3 * Rat(10, 3), 10) |
|---|
| 270 | n/a | self.assertEqual(Rat(10, 5) * 0.5, 1.0) |
|---|
| 271 | n/a | self.assertEqual(0.5 * Rat(10, 5), 1.0) |
|---|
| 272 | n/a | |
|---|
| 273 | n/a | def test_div(self): |
|---|
| 274 | n/a | self.assertEqual(Rat(10, 3) / Rat(5, 7), Rat(14, 3)) |
|---|
| 275 | n/a | self.assertEqual(Rat(10, 3) / 3, Rat(10, 9)) |
|---|
| 276 | n/a | self.assertEqual(2 / Rat(5), Rat(2, 5)) |
|---|
| 277 | n/a | self.assertEqual(3.0 * Rat(1, 2), 1.5) |
|---|
| 278 | n/a | self.assertEqual(Rat(1, 2) * 3.0, 1.5) |
|---|
| 279 | n/a | |
|---|
| 280 | n/a | def test_floordiv(self): |
|---|
| 281 | n/a | self.assertEqual(Rat(10) // Rat(4), 2) |
|---|
| 282 | n/a | self.assertEqual(Rat(10, 3) // Rat(4, 3), 2) |
|---|
| 283 | n/a | self.assertEqual(Rat(10) // 4, 2) |
|---|
| 284 | n/a | self.assertEqual(10 // Rat(4), 2) |
|---|
| 285 | n/a | |
|---|
| 286 | n/a | def test_eq(self): |
|---|
| 287 | n/a | self.assertEqual(Rat(10), Rat(20, 2)) |
|---|
| 288 | n/a | self.assertEqual(Rat(10), 10) |
|---|
| 289 | n/a | self.assertEqual(10, Rat(10)) |
|---|
| 290 | n/a | self.assertEqual(Rat(10), 10.0) |
|---|
| 291 | n/a | self.assertEqual(10.0, Rat(10)) |
|---|
| 292 | n/a | |
|---|
| 293 | n/a | def test_true_div(self): |
|---|
| 294 | n/a | self.assertEqual(Rat(10, 3) / Rat(5, 7), Rat(14, 3)) |
|---|
| 295 | n/a | self.assertEqual(Rat(10, 3) / 3, Rat(10, 9)) |
|---|
| 296 | n/a | self.assertEqual(2 / Rat(5), Rat(2, 5)) |
|---|
| 297 | n/a | self.assertEqual(3.0 * Rat(1, 2), 1.5) |
|---|
| 298 | n/a | self.assertEqual(Rat(1, 2) * 3.0, 1.5) |
|---|
| 299 | n/a | self.assertEqual(eval('1/2'), 0.5) |
|---|
| 300 | n/a | |
|---|
| 301 | n/a | # XXX Ran out of steam; TO DO: divmod, div, future division |
|---|
| 302 | n/a | |
|---|
| 303 | n/a | |
|---|
| 304 | n/a | class OperationLogger: |
|---|
| 305 | n/a | """Base class for classes with operation logging.""" |
|---|
| 306 | n/a | def __init__(self, logger): |
|---|
| 307 | n/a | self.logger = logger |
|---|
| 308 | n/a | def log_operation(self, *args): |
|---|
| 309 | n/a | self.logger(*args) |
|---|
| 310 | n/a | |
|---|
| 311 | n/a | def op_sequence(op, *classes): |
|---|
| 312 | n/a | """Return the sequence of operations that results from applying |
|---|
| 313 | n/a | the operation `op` to instances of the given classes.""" |
|---|
| 314 | n/a | log = [] |
|---|
| 315 | n/a | instances = [] |
|---|
| 316 | n/a | for c in classes: |
|---|
| 317 | n/a | instances.append(c(log.append)) |
|---|
| 318 | n/a | |
|---|
| 319 | n/a | try: |
|---|
| 320 | n/a | op(*instances) |
|---|
| 321 | n/a | except TypeError: |
|---|
| 322 | n/a | pass |
|---|
| 323 | n/a | return log |
|---|
| 324 | n/a | |
|---|
| 325 | n/a | class A(OperationLogger): |
|---|
| 326 | n/a | def __eq__(self, other): |
|---|
| 327 | n/a | self.log_operation('A.__eq__') |
|---|
| 328 | n/a | return NotImplemented |
|---|
| 329 | n/a | def __le__(self, other): |
|---|
| 330 | n/a | self.log_operation('A.__le__') |
|---|
| 331 | n/a | return NotImplemented |
|---|
| 332 | n/a | def __ge__(self, other): |
|---|
| 333 | n/a | self.log_operation('A.__ge__') |
|---|
| 334 | n/a | return NotImplemented |
|---|
| 335 | n/a | |
|---|
| 336 | n/a | class B(OperationLogger, metaclass=ABCMeta): |
|---|
| 337 | n/a | def __eq__(self, other): |
|---|
| 338 | n/a | self.log_operation('B.__eq__') |
|---|
| 339 | n/a | return NotImplemented |
|---|
| 340 | n/a | def __le__(self, other): |
|---|
| 341 | n/a | self.log_operation('B.__le__') |
|---|
| 342 | n/a | return NotImplemented |
|---|
| 343 | n/a | def __ge__(self, other): |
|---|
| 344 | n/a | self.log_operation('B.__ge__') |
|---|
| 345 | n/a | return NotImplemented |
|---|
| 346 | n/a | |
|---|
| 347 | n/a | class C(B): |
|---|
| 348 | n/a | def __eq__(self, other): |
|---|
| 349 | n/a | self.log_operation('C.__eq__') |
|---|
| 350 | n/a | return NotImplemented |
|---|
| 351 | n/a | def __le__(self, other): |
|---|
| 352 | n/a | self.log_operation('C.__le__') |
|---|
| 353 | n/a | return NotImplemented |
|---|
| 354 | n/a | def __ge__(self, other): |
|---|
| 355 | n/a | self.log_operation('C.__ge__') |
|---|
| 356 | n/a | return NotImplemented |
|---|
| 357 | n/a | |
|---|
| 358 | n/a | class V(OperationLogger): |
|---|
| 359 | n/a | """Virtual subclass of B""" |
|---|
| 360 | n/a | def __eq__(self, other): |
|---|
| 361 | n/a | self.log_operation('V.__eq__') |
|---|
| 362 | n/a | return NotImplemented |
|---|
| 363 | n/a | def __le__(self, other): |
|---|
| 364 | n/a | self.log_operation('V.__le__') |
|---|
| 365 | n/a | return NotImplemented |
|---|
| 366 | n/a | def __ge__(self, other): |
|---|
| 367 | n/a | self.log_operation('V.__ge__') |
|---|
| 368 | n/a | return NotImplemented |
|---|
| 369 | n/a | B.register(V) |
|---|
| 370 | n/a | |
|---|
| 371 | n/a | |
|---|
| 372 | n/a | class OperationOrderTests(unittest.TestCase): |
|---|
| 373 | n/a | def test_comparison_orders(self): |
|---|
| 374 | n/a | self.assertEqual(op_sequence(eq, A, A), ['A.__eq__', 'A.__eq__']) |
|---|
| 375 | n/a | self.assertEqual(op_sequence(eq, A, B), ['A.__eq__', 'B.__eq__']) |
|---|
| 376 | n/a | self.assertEqual(op_sequence(eq, B, A), ['B.__eq__', 'A.__eq__']) |
|---|
| 377 | n/a | # C is a subclass of B, so C.__eq__ is called first |
|---|
| 378 | n/a | self.assertEqual(op_sequence(eq, B, C), ['C.__eq__', 'B.__eq__']) |
|---|
| 379 | n/a | self.assertEqual(op_sequence(eq, C, B), ['C.__eq__', 'B.__eq__']) |
|---|
| 380 | n/a | |
|---|
| 381 | n/a | self.assertEqual(op_sequence(le, A, A), ['A.__le__', 'A.__ge__']) |
|---|
| 382 | n/a | self.assertEqual(op_sequence(le, A, B), ['A.__le__', 'B.__ge__']) |
|---|
| 383 | n/a | self.assertEqual(op_sequence(le, B, A), ['B.__le__', 'A.__ge__']) |
|---|
| 384 | n/a | self.assertEqual(op_sequence(le, B, C), ['C.__ge__', 'B.__le__']) |
|---|
| 385 | n/a | self.assertEqual(op_sequence(le, C, B), ['C.__le__', 'B.__ge__']) |
|---|
| 386 | n/a | |
|---|
| 387 | n/a | self.assertTrue(issubclass(V, B)) |
|---|
| 388 | n/a | self.assertEqual(op_sequence(eq, B, V), ['B.__eq__', 'V.__eq__']) |
|---|
| 389 | n/a | self.assertEqual(op_sequence(le, B, V), ['B.__le__', 'V.__ge__']) |
|---|
| 390 | n/a | |
|---|
| 391 | n/a | class SupEq(object): |
|---|
| 392 | n/a | """Class that can test equality""" |
|---|
| 393 | n/a | def __eq__(self, other): |
|---|
| 394 | n/a | return True |
|---|
| 395 | n/a | |
|---|
| 396 | n/a | class S(SupEq): |
|---|
| 397 | n/a | """Subclass of SupEq that should fail""" |
|---|
| 398 | n/a | __eq__ = None |
|---|
| 399 | n/a | |
|---|
| 400 | n/a | class F(object): |
|---|
| 401 | n/a | """Independent class that should fall back""" |
|---|
| 402 | n/a | |
|---|
| 403 | n/a | class X(object): |
|---|
| 404 | n/a | """Independent class that should fail""" |
|---|
| 405 | n/a | __eq__ = None |
|---|
| 406 | n/a | |
|---|
| 407 | n/a | class SN(SupEq): |
|---|
| 408 | n/a | """Subclass of SupEq that can test equality, but not non-equality""" |
|---|
| 409 | n/a | __ne__ = None |
|---|
| 410 | n/a | |
|---|
| 411 | n/a | class XN: |
|---|
| 412 | n/a | """Independent class that can test equality, but not non-equality""" |
|---|
| 413 | n/a | def __eq__(self, other): |
|---|
| 414 | n/a | return True |
|---|
| 415 | n/a | __ne__ = None |
|---|
| 416 | n/a | |
|---|
| 417 | n/a | class FallbackBlockingTests(unittest.TestCase): |
|---|
| 418 | n/a | """Unit tests for None method blocking""" |
|---|
| 419 | n/a | |
|---|
| 420 | n/a | def test_fallback_rmethod_blocking(self): |
|---|
| 421 | n/a | e, f, s, x = SupEq(), F(), S(), X() |
|---|
| 422 | n/a | self.assertEqual(e, e) |
|---|
| 423 | n/a | self.assertEqual(e, f) |
|---|
| 424 | n/a | self.assertEqual(f, e) |
|---|
| 425 | n/a | # left operand is checked first |
|---|
| 426 | n/a | self.assertEqual(e, x) |
|---|
| 427 | n/a | self.assertRaises(TypeError, eq, x, e) |
|---|
| 428 | n/a | # S is a subclass, so it's always checked first |
|---|
| 429 | n/a | self.assertRaises(TypeError, eq, e, s) |
|---|
| 430 | n/a | self.assertRaises(TypeError, eq, s, e) |
|---|
| 431 | n/a | |
|---|
| 432 | n/a | def test_fallback_ne_blocking(self): |
|---|
| 433 | n/a | e, sn, xn = SupEq(), SN(), XN() |
|---|
| 434 | n/a | self.assertFalse(e != e) |
|---|
| 435 | n/a | self.assertRaises(TypeError, ne, e, sn) |
|---|
| 436 | n/a | self.assertRaises(TypeError, ne, sn, e) |
|---|
| 437 | n/a | self.assertFalse(e != xn) |
|---|
| 438 | n/a | self.assertRaises(TypeError, ne, xn, e) |
|---|
| 439 | n/a | |
|---|
| 440 | n/a | if __name__ == "__main__": |
|---|
| 441 | n/a | unittest.main() |
|---|