Left: | ||
Right: |
OLD | NEW |
---|---|
1 """Tests for binary operators on subtypes of built-in types.""" | 1 """Tests for binary operators on subtypes of built-in types.""" |
2 | 2 |
3 import unittest | 3 import unittest |
4 from test import support | 4 from test import support |
5 from operator import eq, ne, lt, gt, le, ge | 5 from operator import eq, ne, lt, gt, le, ge |
6 from abc import ABCMeta | 6 from abc import ABCMeta |
7 | 7 |
8 def gcd(a, b): | 8 def gcd(a, b): |
9 """Greatest common divisor using Euclid's algorithm.""" | 9 """Greatest common divisor using Euclid's algorithm.""" |
10 while a: | 10 while a: |
(...skipping 370 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
381 self.assertEqual(op_sequence(le, A, A), ['A.__le__', 'A.__ge__']) | 381 self.assertEqual(op_sequence(le, A, A), ['A.__le__', 'A.__ge__']) |
382 self.assertEqual(op_sequence(le, A, B), ['A.__le__', 'B.__ge__']) | 382 self.assertEqual(op_sequence(le, A, B), ['A.__le__', 'B.__ge__']) |
383 self.assertEqual(op_sequence(le, B, A), ['B.__le__', 'A.__ge__']) | 383 self.assertEqual(op_sequence(le, B, A), ['B.__le__', 'A.__ge__']) |
384 self.assertEqual(op_sequence(le, B, C), ['C.__ge__', 'B.__le__']) | 384 self.assertEqual(op_sequence(le, B, C), ['C.__ge__', 'B.__le__']) |
385 self.assertEqual(op_sequence(le, C, B), ['C.__le__', 'B.__ge__']) | 385 self.assertEqual(op_sequence(le, C, B), ['C.__le__', 'B.__ge__']) |
386 | 386 |
387 self.assertTrue(issubclass(V, B)) | 387 self.assertTrue(issubclass(V, B)) |
388 self.assertEqual(op_sequence(eq, B, V), ['B.__eq__', 'V.__eq__']) | 388 self.assertEqual(op_sequence(eq, B, V), ['B.__eq__', 'V.__eq__']) |
389 self.assertEqual(op_sequence(le, B, V), ['B.__le__', 'V.__ge__']) | 389 self.assertEqual(op_sequence(le, B, V), ['B.__le__', 'V.__ge__']) |
390 | 390 |
391 class E(object): | |
storchaka
2016/01/06 21:55:31
(object) is not needed in Python 3.
abarnert
2016/01/06 23:04:32
Of course, but it's used in all of the other test
Martin Panter
2016/01/09 02:34:56
IMO it is better to be locally consistent (see cla
| |
392 """Class that can test equality""" | |
393 def __eq__(self, other): | |
394 return True | |
395 | |
396 class S(E): | |
397 """Subclass of E that should fail""" | |
398 __eq__ = None | |
391 | 399 |
400 class F(object): | |
401 """Independent class that should fall back""" | |
402 | |
403 class X(object): | |
storchaka
2016/01/06 21:55:31
(object) is not needed in Python 3.
| |
404 """Independent class that should fail""" | |
405 __eq__ = None | |
406 | |
407 class SN(E): | |
408 """Subclass of E that can test equality, but not non-equality""" | |
409 __ne__ = None | |
410 | |
411 class XN: | |
412 """Independent class that can test equality, but not non-equality""" | |
413 def __eq__(self, other): | |
414 return True | |
415 __ne__ = None | |
416 | |
417 class FallbackBlockingTests(unittest.TestCase): | |
418 def test_fallback_rmethod_blocking(self): | |
419 e, f, s, x = E(), F(), S(), X() | |
storchaka
2016/01/06 21:55:31
Since there are 4 classes, shouldn't you test all
abarnert
2016/01/06 23:04:32
There's no need for the full cartesian product of
Martin Panter
2016/01/09 02:34:56
I tend to agree, unless there is any particular co
| |
420 self.assertEqual(e, e) | |
storchaka
2016/01/06 21:55:31
Perhaps it would be better to write explicitly as
abarnert
2016/01/06 23:04:32
Maybe? But the other tests for __eq__ use assertEq
Martin Panter
2016/01/09 02:34:56
Your tests for __ne__ use “!=”. It is more explici
| |
421 self.assertEqual(e, f) | |
422 self.assertEqual(f, e) | |
423 # left operand is checked first | |
424 self.assertEqual(e, x) | |
425 self.assertRaises(TypeError, eq, x, e) | |
426 # S is a subclass, so it's always checked first | |
427 self.assertRaises(TypeError, eq, e, s) | |
428 self.assertRaises(TypeError, eq, s, e) | |
429 def test_fallback_ne_blocking(self): | |
storchaka
2016/01/06 21:55:31
Needed an empty line between methods.
abarnert
2016/01/06 23:04:32
Oops; thanks, I'll fix that.
| |
430 e, sn, xn = E(), SN(), XN() | |
431 self.assertFalse(e != e) | |
432 self.assertRaises(TypeError, ne, e, sn) | |
433 self.assertRaises(TypeError, ne, sn, e) | |
434 self.assertFalse(e != xn) | |
435 self.assertRaises(TypeError, ne, xn, e) | |
436 | |
storchaka
2016/01/06 21:55:31
Default __ne__ falls back to __eq__. It is worth t
abarnert
2016/01/06 23:04:32
I don't think so. What could an implementation do
Martin Panter
2016/01/09 02:34:56
The key question is: given a.__eq__ has priority b
| |
392 if __name__ == "__main__": | 437 if __name__ == "__main__": |
393 unittest.main() | 438 unittest.main() |
OLD | NEW |