Ticket #1442: endpoints-1442-addresstests.diff

File endpoints-1442-addresstests.diff, 4.4 KB (added by rwall, 8 years ago)

Some tests and fixes for the address module.

  • twisted/test/test_address.py

     
     1import re 
     2 
     3from twisted.trial import unittest 
     4from twisted.internet.address import IPv4Address, UNIXAddress 
     5from twisted.internet.interfaces import IClientEndpoint, IServerEndpoint 
     6 
     7 
     8class AddressTestCaseMixin(object): 
     9    def test_BuildEndpoint(self): 
     10        """Test that the IAddress.buildEndpoint returns a provider of both 
     11        IClientEndpoint and IServerEndpoint""" 
     12        addr = self.buildAddress() 
     13        ep = addr.buildEndpoint() 
     14        self.assertTrue(IClientEndpoint.providedBy(ep)) 
     15        self.assertTrue(IServerEndpoint.providedBy(ep)) 
     16 
     17    def test_AddressComparison(self): 
     18        """Test that two different address instances, sharing the same  
     19        properties are considered equal.""" 
     20        self.assertEquals(self.buildAddress(), self.buildAddress()) 
     21 
     22    def test_StringRepresentation(self): 
     23        """Test that when addresses are converted to strings, they adhere to a  
     24        standard pattern. Not sure if it's worth it, but seemed like a bit of  
     25        fun and demonstrates an inconsistency with UNIXAddress.__str__ 
     26        """ 
     27        addr = self.buildAddress() 
     28        pattern = "".join([ 
     29           "^", 
     30           "([^\(]+Address)", # class name, 
     31           "\(",       # opening bracket, 
     32           "([^)]+)",  # arguments, 
     33           "\)",       # closing bracket, 
     34           "$" 
     35        ]) 
     36        m = re.match(pattern, str(addr)) 
     37        self.assertNotEqual(None, m,  
     38                            "%s does not match the standard __str__ pattern "  
     39                            "ClassName(arg1, arg2, etc)" % str(addr)) 
     40        self.assertEqual(addr.__class__.__name__, m.group(1)) 
     41         
     42        args = [x.strip() for x in m.group(2).split(",")] 
     43        self.assertEqual(len(args), len(self.addressArgSpec)) 
     44        def checkArg(arg, argSpec): 
     45            self.assertEqual(argSpec[1] % getattr(addr, argSpec[0]), arg) 
     46        map(checkArg, args, self.addressArgSpec) 
     47 
     48 
     49class IPv4AddressTestCaseMixin(AddressTestCaseMixin): 
     50    def setUpClass(self): 
     51        self.addressArgSpec = (("type", "%s"), ("host", "%r"), ("port", "%d")) 
     52 
     53 
     54class IPv4AddressTCPTestCase(unittest.TestCase, IPv4AddressTestCaseMixin): 
     55    def buildAddress(self): 
     56        return IPv4Address("TCP", "127.0.0.1", 0) 
     57 
     58 
     59class IPv4AddressUDPTestCase(unittest.TestCase, IPv4AddressTestCaseMixin): 
     60    def buildAddress(self): 
     61        return IPv4Address("UDP", "127.0.0.1", 0) 
     62     
     63    def test_BuildEndpoint(self): 
     64        pass 
     65    test_BuildEndpoint.skip = "UDP Endpoints are not yet supported" 
     66 
     67 
     68class UNIXAddressTestCase(unittest.TestCase, AddressTestCaseMixin): 
     69    def setUpClass(self): 
     70        self._socketAddress = self.mktemp() 
     71        self.addressArgSpec = (("name", "%r"),) 
     72         
     73    def buildAddress(self): 
     74        return UNIXAddress(self._socketAddress) 
  • twisted/internet/address.py

     
    4040        if self.type == "TCP": 
    4141            return TCPEndpoint(self.host, self.port) 
    4242        else: 
    43             raise NotImplementedError 
     43            raise NotImplementedError, "UDP Endpoints are not yet implemented" 
    4444 
    4545    def __getitem__(self, index): 
    4646        warnings.warn("IPv4Address.__getitem__ is deprecated.  Use attributes instead.", 
     
    9696        if isinstance(other, tuple): 
    9797            return tuple(self) == other 
    9898        elif isinstance(other, UNIXAddress): 
    99             try: 
    100                 return os.path.samefile(self.name, other.name) 
    101             except OSError: 
    102                 pass 
     99            # First do the simple thing and check to see if the names are the  
     100            # same. If not, and the paths exist, check to see if they point to  
     101            # the same file. 
     102            if self.name == other.name: 
     103                return True 
     104            else: 
     105                try: 
     106                    return os.path.samefile(self.name, other.name) 
     107                except OSError: 
     108                    pass 
    103109        return False 
    104110 
    105111    def __str__(self): 
    106         return 'UNIXSocket(%r)' % (self.name,) 
     112        return 'UNIXAddress(%r)' % (self.name,) 
    107113 
    108114 
    109115# These are for buildFactory backwards compatability due to