1 # Copyright (c) Twisted Matrix Laboratories.
2 # See LICENSE for details.
5 Tests for L{twisted.internet.serialport}.
8 from twisted.trial import unittest
9 from twisted.python.failure import Failure
10 from twisted.internet.protocol import Protocol
11 from twisted.internet.error import ConnectionDone
13 from twisted.internet import serialport
19 class DoNothing(object):
21 Object with methods that do nothing.
24 def __init__(self, *args, **kwargs):
28 def __getattr__(self, attr):
29 return lambda *args, **kwargs: None
33 class SerialPortTests(unittest.TestCase):
35 Minimal testing for Twisted's serial port support.
37 See ticket #2462 for the eventual full test suite.
40 if serialport is None:
41 skip = "Serial port support is not available."
44 def test_connectionMadeLost(self):
46 C{connectionMade} and C{connectionLost} are called on the protocol by
49 # Serial port that doesn't actually connect to anything:
50 class DummySerialPort(serialport.SerialPort):
51 _serialFactory = DoNothing
53 def _finishPortSetup(self):
54 pass # override default win32 actions
58 class SerialProtocol(Protocol):
59 def connectionMade(self):
60 events.append("connectionMade")
62 def connectionLost(self, reason):
63 events.append(("connectionLost", reason))
65 # Creation of port should result in connectionMade call:
66 port = DummySerialPort(SerialProtocol(), "", reactor=DoNothing())
67 self.assertEqual(events, ["connectionMade"])
69 # Simulate reactor calling connectionLost on the SerialPort:
70 f = Failure(ConnectionDone())
71 port.connectionLost(f)
72 self.assertEqual(events, ["connectionMade", ("connectionLost", f)])