<s> from twisted . trial import unittest <EOL> from twisted . words . im import basesupport <EOL> from twisted . internet import error , defer <EOL> class DummyAccount ( basesupport . AbstractAccount ) : <EOL> """<STR_LIT>""" <EOL> loginHasFailed = False <EOL> loginCallbackCalled = False <EOL> def _startLogOn ( self , * args ) : <EOL> """<STR_LIT>""" <EOL> self . loginDeferred = defer . Deferred ( ) <EOL> return self . loginDeferred <EOL> def _loginFailed ( self , result ) : <EOL> self . loginHasFailed = True <EOL> return basesupport . AbstractAccount . _loginFailed ( self , result ) <EOL> def _cb_logOn ( self , result ) : <EOL> self . loginCallbackCalled = True <EOL> return basesupport . AbstractAccount . _cb_logOn ( self , result ) <EOL> class DummyUI ( object ) : <EOL> """<STR_LIT>""" <EOL> clientRegistered = False <EOL> def registerAccountClient ( self , result ) : <EOL> self . clientRegistered = True <EOL> class ClientMsgTests ( unittest . TestCase ) : <EOL> def makeUI ( self ) : <EOL> return DummyUI ( ) <EOL> def makeAccount ( self ) : <EOL> return DummyAccount ( '<STR_LIT>' , False , '<STR_LIT>' , None , '<STR_LIT:localhost>' , <NUM_LIT> ) <EOL> def test_connect ( self ) : <EOL> """<STR_LIT>""" <EOL> account = self . makeAccount ( ) <EOL> ui = self . makeUI ( ) <EOL> d = account . logOn ( ui ) <EOL> account . loginDeferred . callback ( None ) <EOL> def check ( result ) : <EOL> self . assert_ ( not account . loginHasFailed , <EOL> "<STR_LIT>" ) <EOL> self . assert_ ( account . loginCallbackCalled , <EOL> "<STR_LIT>" ) <EOL> d . addCallback ( check ) <EOL> return d <EOL> def test_failedConnect ( self ) : <EOL> """<STR_LIT>""" <EOL> account = self . makeAccount ( ) <EOL> ui = self . makeUI ( ) <EOL> d = account . logOn ( ui ) <EOL> account . loginDeferred . errback ( Exception ( ) ) <EOL> def err ( reason ) : <EOL> self . assert_ ( account . loginHasFailed , "<STR_LIT>" ) <EOL> self . assert_ ( not account . loginCallbackCalled , <EOL> "<STR_LIT>" ) <EOL> self . assert_ ( not ui . clientRegistered , <EOL> "<STR_LIT>" ) <EOL> cb = lambda r : self . assert_ ( False , "<STR_LIT>" ) <EOL> d . addCallbacks ( cb , err ) <EOL> return d <EOL> def test_alreadyConnecting ( self ) : <EOL> """<STR_LIT>""" <EOL> account = self . makeAccount ( ) <EOL> ui = self . makeUI ( ) <EOL> account . logOn ( ui ) <EOL> self . assertRaises ( error . ConnectError , account . logOn , ui ) </s>
