diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2015-01-26 08:11:39 (GMT) |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2015-01-26 08:11:39 (GMT) |
commit | ffd48c9e3d0b9bd6fffd4a5ada6a112fea767e2e (patch) | |
tree | 93a1d720e8ba0e133aa422e94431a4e33a7ab744 /Lib/test/test_ipaddress.py | |
parent | 34af5023fc1e74c2d6a7537a11148885c84d9cb4 (diff) | |
parent | f186e128b6d4e6031e1e7e97b26b3eda64c1b88f (diff) | |
download | cpython-ffd48c9e3d0b9bd6fffd4a5ada6a112fea767e2e.zip cpython-ffd48c9e3d0b9bd6fffd4a5ada6a112fea767e2e.tar.gz cpython-ffd48c9e3d0b9bd6fffd4a5ada6a112fea767e2e.tar.bz2 |
Issue #23268: Fixed bugs in the comparison of ipaddress classes.
Diffstat (limited to 'Lib/test/test_ipaddress.py')
-rw-r--r-- | Lib/test/test_ipaddress.py | 47 |
1 files changed, 42 insertions, 5 deletions
diff --git a/Lib/test/test_ipaddress.py b/Lib/test/test_ipaddress.py index 5ec2cd4..e985329 100644 --- a/Lib/test/test_ipaddress.py +++ b/Lib/test/test_ipaddress.py @@ -7,6 +7,7 @@ import unittest import re import contextlib +import functools import operator import pickle import ipaddress @@ -552,6 +553,20 @@ class FactoryFunctionErrors(BaseTestCase): self.assertFactoryError(ipaddress.ip_network, "network") +@functools.total_ordering +class LargestObject: + def __eq__(self, other): + return isinstance(other, LargestObject) + def __lt__(self, other): + return False + +@functools.total_ordering +class SmallestObject: + def __eq__(self, other): + return isinstance(other, SmallestObject) + def __gt__(self, other): + return False + class ComparisonTests(unittest.TestCase): v4addr = ipaddress.IPv4Address(1) @@ -605,6 +620,28 @@ class ComparisonTests(unittest.TestCase): self.assertRaises(TypeError, lambda: lhs <= rhs) self.assertRaises(TypeError, lambda: lhs >= rhs) + def test_foreign_type_ordering(self): + other = object() + smallest = SmallestObject() + largest = LargestObject() + for obj in self.objects: + with self.assertRaises(TypeError): + obj < other + with self.assertRaises(TypeError): + obj > other + with self.assertRaises(TypeError): + obj <= other + with self.assertRaises(TypeError): + obj >= other + self.assertTrue(obj < largest) + self.assertFalse(obj > largest) + self.assertTrue(obj <= largest) + self.assertFalse(obj >= largest) + self.assertFalse(obj < smallest) + self.assertTrue(obj > smallest) + self.assertFalse(obj <= smallest) + self.assertTrue(obj >= smallest) + def test_mixed_type_key(self): # with get_mixed_type_key, you can sort addresses and network. v4_ordered = [self.v4addr, self.v4net, self.v4intf] @@ -625,7 +662,7 @@ class ComparisonTests(unittest.TestCase): v4addr = ipaddress.ip_address('1.1.1.1') v4net = ipaddress.ip_network('1.1.1.1') v6addr = ipaddress.ip_address('::1') - v6net = ipaddress.ip_address('::1') + v6net = ipaddress.ip_network('::1') self.assertRaises(TypeError, v4addr.__lt__, v6addr) self.assertRaises(TypeError, v4addr.__gt__, v6addr) @@ -1383,10 +1420,10 @@ class IpaddrUnitTest(unittest.TestCase): unsorted = [ip4, ip1, ip3, ip2] unsorted.sort() self.assertEqual(sorted, unsorted) - self.assertRaises(TypeError, ip1.__lt__, - ipaddress.ip_address('10.10.10.0')) - self.assertRaises(TypeError, ip2.__lt__, - ipaddress.ip_address('10.10.10.0')) + self.assertIs(ip1.__lt__(ipaddress.ip_address('10.10.10.0')), + NotImplemented) + self.assertIs(ip2.__lt__(ipaddress.ip_address('10.10.10.0')), + NotImplemented) # <=, >= self.assertTrue(ipaddress.ip_network('1.1.1.1') <= |