<s> from twisted . python . reflect import namedAny <EOL> from twisted . trial import unittest <EOL> from twisted . test . proto_helpers import StringTransport <EOL> from twisted . conch . insults . insults import ServerProtocol , ClientProtocol <EOL> from twisted . conch . insults . insults import CS_UK , CS_US , CS_DRAWING , CS_ALTERNATE , CS_ALTERNATE_SPECIAL <EOL> from twisted . conch . insults . insults import G0 , G1 <EOL> from twisted . conch . insults . insults import modes <EOL> def _getattr ( mock , name ) : <EOL> return super ( Mock , mock ) . __getattribute__ ( name ) <EOL> def occurrences ( mock ) : <EOL> return _getattr ( mock , '<STR_LIT>' ) <EOL> def methods ( mock ) : <EOL> return _getattr ( mock , '<STR_LIT>' ) <EOL> def _append ( mock , obj ) : <EOL> occurrences ( mock ) . append ( obj ) <EOL> default = object ( ) <EOL> class Mock ( object ) : <EOL> callReturnValue = default <EOL> def __init__ ( self , methods = None , callReturnValue = default ) : <EOL> """<STR_LIT>""" <EOL> self . occurrences = [ ] <EOL> if methods is None : <EOL> methods = { } <EOL> self . methods = methods <EOL> if callReturnValue is not default : <EOL> self . callReturnValue = callReturnValue <EOL> def __call__ ( self , * a , ** kw ) : <EOL> returnValue = _getattr ( self , '<STR_LIT>' ) <EOL> if returnValue is default : <EOL> returnValue = Mock ( ) <EOL> _append ( self , ( '<STR_LIT>' , returnValue , a , kw ) ) <EOL> return returnValue <EOL> def __getattribute__ ( self , name ) : <EOL> methods = _getattr ( self , '<STR_LIT>' ) <EOL> if name in methods : <EOL> attrValue = Mock ( callReturnValue = methods [ name ] ) <EOL> else : <EOL> attrValue = Mock ( ) <EOL> _append ( self , ( name , attrValue ) ) <EOL> return attrValue <EOL> class MockMixin : <EOL> def assertCall ( self , occurrence , methodName , expectedPositionalArgs = ( ) , <EOL> expectedKeywordArgs = { } ) : <EOL> attr , mock = occurrence <EOL> self . assertEqual ( attr , methodName ) <EOL> self . assertEqual ( len ( occurrences ( mock ) ) , <NUM_LIT:1> ) <EOL> [ ( call , result , args , kw ) ] = occurrences ( mock ) <EOL> self . assertEqual ( call , "<STR_LIT>" ) <EOL> self . assertEqual ( args , expectedPositionalArgs ) <EOL> self . assertEqual ( kw , expectedKeywordArgs ) <EOL> return result <EOL> _byteGroupingTestTemplate = """<STR_LIT>""" <EOL> class ByteGroupingsMixin ( MockMixin ) : <EOL> protocolFactory = None <EOL> for word , n in [ ( '<STR_LIT>' , <NUM_LIT:2> ) , ( '<STR_LIT>' , <NUM_LIT:3> ) , ( '<STR_LIT>' , <NUM_LIT:4> ) , ( '<STR_LIT>' , <NUM_LIT:5> ) , ( '<STR_LIT>' , <NUM_LIT:6> ) ] : <EOL> exec _byteGroupingTestTemplate % { '<STR_LIT>' : word , '<STR_LIT>' : n } <EOL> del word , n <EOL> def verifyResults ( self , transport , proto , parser ) : <EOL> result = self . assertCall ( occurrences ( proto ) . pop ( <NUM_LIT:0> ) , "<STR_LIT>" , ( parser , ) ) <EOL> self . assertEqual ( occurrences ( result ) , [ ] ) <EOL> del _byteGroupingTestTemplate <EOL> class ServerArrowKeysTests ( ByteGroupingsMixin , unittest . TestCase ) : <EOL> protocolFactory = ServerProtocol <EOL> TEST_BYTES = '<STR_LIT>' <EOL> def verifyResults ( self , transport , proto , parser ) : <EOL> ByteGroupingsMixin . verifyResults ( self , transport , proto , parser ) <EOL> for arrow in ( parser . UP_ARROW , parser . DOWN_ARROW , <EOL> parser . RIGHT_ARROW , parser . LEFT_ARROW ) : <EOL> result = self . assertCall ( occurrences ( proto ) . pop ( <NUM_LIT:0> ) , "<STR_LIT>" , ( arrow , None ) ) <EOL> self . assertEqual ( occurrences ( result ) , [ ] ) <EOL> self . assertFalse ( occurrences ( proto ) ) <EOL> class PrintableCharactersTests ( ByteGroupingsMixin , unittest . TestCase ) : <EOL> protocolFactory = ServerProtocol <EOL> TEST_BYTES = '<STR_LIT>' <EOL> def verifyResults ( self , transport , proto , parser ) : <EOL> ByteGroupingsMixin . verifyResults ( self , transport , proto , parser ) <EOL> for char in '<STR_LIT>' : <EOL> result = self . assertCall ( occurrences ( proto ) . pop ( <NUM_LIT:0> ) , "<STR_LIT>" , ( char , None ) ) <EOL> self . assertEqual ( occurrences ( result ) , [ ] ) <EOL> for char in '<STR_LIT>' : <EOL> result = self . assertCall ( occurrences ( proto ) . pop ( <NUM_LIT:0> ) , "<STR_LIT>" , ( char , parser . ALT ) ) <EOL> self . assertEqual ( occurrences ( result ) , [ ] ) <EOL> occs = occurrences ( proto ) <EOL> self . assertFalse ( occs , "<STR_LIT>" % ( occs , ) ) <EOL> class ServerFunctionKeysTests ( ByteGroupingsMixin , unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> protocolFactory = ServerProtocol <EOL> byteList = [ ] <EOL> for bytes in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> byteList . append ( '<STR_LIT>' + bytes ) <EOL> TEST_BYTES = '<STR_LIT>' . join ( byteList ) <EOL> del byteList , bytes <EOL> def verifyResults ( self , transport , proto , parser ) : <EOL> ByteGroupingsMixin . verifyResults ( self , transport , proto , parser ) <EOL> for funcNum in range ( <NUM_LIT:1> , <NUM_LIT> ) : <EOL> funcArg = getattr ( parser , '<STR_LIT>' % ( funcNum , ) ) <EOL> result = self . assertCall ( occurrences ( proto ) . pop ( <NUM_LIT:0> ) , "<STR_LIT>" , ( funcArg , None ) ) <EOL> self . assertEqual ( occurrences ( result ) , [ ] ) <EOL> self . assertFalse ( occurrences ( proto ) ) <EOL> class ClientCursorMovementTests ( ByteGroupingsMixin , unittest . TestCase ) : <EOL> protocolFactory = ClientProtocol <EOL> d2 = "<STR_LIT>" <EOL> r4 = "<STR_LIT>" <EOL> u1 = "<STR_LIT>" <EOL> l2 = "<STR_LIT>" <EOL> TEST_BYTES = d2 + r4 + u1 + l2 + u1 + l2 <EOL> del d2 , r4 , u1 , l2 <EOL> def verifyResults ( self , transport , proto , parser ) : <EOL> ByteGroupingsMixin . verifyResults ( self , transport , proto , parser ) <EOL> for ( method , count ) in [ ( '<STR_LIT>' , <NUM_LIT:2> ) , ( '<STR_LIT>' , <NUM_LIT:4> ) , ( '<STR_LIT>' , <NUM_LIT:1> ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:2> ) , ( '<STR_LIT>' , <NUM_LIT:1> ) , ( '<STR_LIT>' , <NUM_LIT:2> ) ] : <EOL> result = self . assertCall ( occurrences ( proto ) . pop ( <NUM_LIT:0> ) , "<STR_LIT>" + method , ( count , ) ) <EOL> self . assertEqual ( occurrences ( result ) , [ ] ) <EOL> self . assertFalse ( occurrences ( proto ) ) <EOL> class ClientControlSequencesTests ( unittest . TestCase , MockMixin ) : <EOL> def setUp ( self ) : <EOL> self . transport = StringTransport ( ) <EOL> self . proto = Mock ( ) <EOL> self . parser = ClientProtocol ( lambda : self . proto ) <EOL> self . parser . factory = self <EOL> self . parser . makeConnection ( self . transport ) <EOL> result = self . assertCall ( occurrences ( self . proto ) . pop ( <NUM_LIT:0> ) , "<STR_LIT>" , ( self . parser , ) ) <EOL> self . assertFalse ( occurrences ( result ) ) <EOL> def testSimpleCardinals ( self ) : <EOL> self . parser . dataReceived ( <EOL> '<STR_LIT>' . join ( [ '<STR_LIT>' . join ( [ '<STR_LIT>' + str ( n ) + ch for n in ( '<STR_LIT>' , <NUM_LIT:2> , <NUM_LIT:20> , <NUM_LIT:200> ) ] ) for ch in '<STR_LIT>' ] ) ) <EOL> occs = occurrences ( self . proto ) <EOL> for meth in ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) : <EOL> for count in ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:20> , <NUM_LIT:200> ) : <EOL> result = self . assertCall ( occs . pop ( <NUM_LIT:0> ) , "<STR_LIT>" + meth , ( count , ) ) <EOL> self . assertFalse ( occurrences ( result ) ) <EOL> self . assertFalse ( occs ) <EOL> def testScrollRegion ( self ) : <EOL> self . parser . dataReceived ( '<STR_LIT>' ) <EOL> occs = occurrences ( self . proto ) <EOL> result = self . assertCall ( occs . pop ( <NUM_LIT:0> ) , "<STR_LIT>" , ( <NUM_LIT:5> , <NUM_LIT> ) ) <EOL> self . assertFalse ( occurrences ( result ) ) <EOL> result = self . assertCall ( occs . pop ( <NUM_LIT:0> ) , "<STR_LIT>" , ( None , None ) ) <EOL> self . assertFalse ( occurrences ( result ) ) <EOL> self . assertFalse ( occs ) <EOL> def testHeightAndWidth ( self ) : <EOL> self . parser . dataReceived ( "<STR_LIT>" ) <EOL> occs = occurrences ( self . proto ) <EOL> result = self . assertCall ( occs . pop ( <NUM_LIT:0> ) , "<STR_LIT>" , ( True , ) ) <EOL> self . assertFalse ( occurrences ( result ) ) <EOL> result = self . assertCall ( occs . pop ( <NUM_LIT:0> ) , "<STR_LIT>" , ( False , ) ) <EOL> self . assertFalse ( occurrences ( result ) ) <EOL> result = self . assertCall ( occs . pop ( <NUM_LIT:0> ) , "<STR_LIT>" ) <EOL> self . assertFalse ( occurrences ( result ) ) <EOL> result = self . assertCall ( occs . pop ( <NUM_LIT:0> ) , "<STR_LIT>" ) <EOL> self . assertFalse ( occurrences ( result ) ) <EOL> self . assertFalse ( occs ) <EOL> def testCharacterSet ( self ) : <EOL> self . parser . dataReceived ( <EOL> '<STR_LIT>' . join ( [ '<STR_LIT>' . join ( [ '<STR_LIT>' + g + n for n in '<STR_LIT>' ] ) for g in '<STR_LIT>' ] ) ) <EOL> occs = occurrences ( self . proto ) <EOL> for which in ( G0 , G1 ) : <EOL> for charset in ( CS_UK , CS_US , CS_DRAWING , CS_ALTERNATE , CS_ALTERNATE_SPECIAL ) : <EOL> result = self . assertCall ( occs . pop ( <NUM_LIT:0> ) , "<STR_LIT>" , ( charset , which ) ) <EOL> self . assertFalse ( occurrences ( result ) ) <EOL> self . assertFalse ( occs ) <EOL> def testShifting ( self ) : <EOL> self . parser . dataReceived ( "<STR_LIT>" ) <EOL> occs = occurrences ( self . proto ) <EOL> result = self . assertCall ( occs . pop ( <NUM_LIT:0> ) , "<STR_LIT>" ) <EOL> self . assertFalse ( occurrences ( result ) ) <EOL> result = self . assertCall ( occs . pop ( <NUM_LIT:0> ) , "<STR_LIT>" ) <EOL> self . assertFalse ( occurrences ( result ) ) <EOL> self . assertFalse ( occs ) <EOL> def testSingleShifts ( self ) : <EOL> self . parser . dataReceived ( "<STR_LIT>" ) <EOL> occs = occurrences ( self . proto ) <EOL> result = self . assertCall ( occs . pop ( <NUM_LIT:0> ) , "<STR_LIT>" ) <EOL> self . assertFalse ( occurrences ( result ) ) <EOL> result = self . assertCall ( occs . pop ( <NUM_LIT:0> ) , "<STR_LIT>" ) <EOL> self . assertFalse ( occurrences ( result ) ) <EOL> self . assertFalse ( occs ) <EOL> def testKeypadMode ( self ) : <EOL> self . parser . dataReceived ( "<STR_LIT>" ) <EOL> occs = occurrences ( self . proto ) <EOL> result = self . assertCall ( occs . pop ( <NUM_LIT:0> ) , "<STR_LIT>" ) <EOL> self . assertFalse ( occurrences ( result ) ) <EOL> result = self . assertCall ( occs . pop ( <NUM_LIT:0> ) , "<STR_LIT>" ) <EOL> self . assertFalse ( occurrences ( result ) ) <EOL> self . assertFalse ( occs ) <EOL> def testCursor ( self ) : <EOL> self . parser . dataReceived ( "<STR_LIT>" ) <EOL> occs = occurrences ( self . proto ) <EOL> result = self . assertCall ( occs . pop ( <NUM_LIT:0> ) , "<STR_LIT>" ) <EOL> self . assertFalse ( occurrences ( result ) ) <EOL> result = self . assertCall ( occs . pop ( <NUM_LIT:0> ) , "<STR_LIT>" ) <EOL> self . assertFalse ( occurrences ( result ) ) <EOL> self . assertFalse ( occs ) <EOL> def testReset ( self ) : <EOL> self . parser . dataReceived ( "<STR_LIT>" ) <EOL> occs = occurrences ( self . proto ) <EOL> result = self . assertCall ( occs . pop ( <NUM_LIT:0> ) , "<STR_LIT>" ) <EOL> self . assertFalse ( occurrences ( result ) ) <EOL> self . assertFalse ( occs ) <EOL> def testIndex ( self ) : <EOL> self . parser . dataReceived ( "<STR_LIT>" ) <EOL> occs = occurrences ( self . proto ) <EOL> result = self . assertCall ( occs . pop ( <NUM_LIT:0> ) , "<STR_LIT:index>" ) <EOL> self . assertFalse ( occurrences ( result ) ) <EOL> result = self . assertCall ( occs . pop ( <NUM_LIT:0> ) , "<STR_LIT>" ) <EOL> self . assertFalse ( occurrences ( result ) ) <EOL> result = self . assertCall ( occs . pop ( <NUM_LIT:0> ) , "<STR_LIT>" ) <EOL> self . assertFalse ( occurrences ( result ) ) <EOL> self . assertFalse ( occs ) <EOL> def testModes ( self ) : <EOL> self . parser . dataReceived ( <EOL> "<STR_LIT>" + '<STR_LIT:;>' . join ( map ( str , [ modes . KAM , modes . IRM , modes . LNM ] ) ) + "<STR_LIT:h>" ) <EOL> self . parser . dataReceived ( <EOL> "<STR_LIT>" + '<STR_LIT:;>' . join ( map ( str , [ modes . KAM , modes . IRM , modes . LNM ] ) ) + "<STR_LIT:l>" ) <EOL> occs = occurrences ( self . proto ) <EOL> result = self . assertCall ( occs . pop ( <NUM_LIT:0> ) , "<STR_LIT>" , ( [ modes . KAM , modes . IRM , modes . LNM ] , ) ) <EOL> self . assertFalse ( occurrences ( result ) ) <EOL> result = self . assertCall ( occs . pop ( <NUM_LIT:0> ) , "<STR_LIT>" , ( [ modes . KAM , modes . IRM , modes . LNM ] , ) ) <EOL> self . assertFalse ( occurrences ( result ) ) <EOL> self . assertFalse ( occs ) <EOL> def testErasure ( self ) : <EOL> self . parser . dataReceived ( <EOL> "<STR_LIT>" ) <EOL> occs = occurrences ( self . proto ) <EOL> for meth in ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" ) : <EOL> result = self . assertCall ( occs . pop ( <NUM_LIT:0> ) , meth ) <EOL> self . assertFalse ( occurrences ( result ) ) <EOL> result = self . assertCall ( occs . pop ( <NUM_LIT:0> ) , "<STR_LIT>" , ( <NUM_LIT:3> , ) ) <EOL> self . assertFalse ( occurrences ( result ) ) <EOL> self . assertFalse ( occs ) <EOL> def testLineDeletion ( self ) : <EOL> self . parser . dataReceived ( "<STR_LIT>" ) <EOL> occs = occurrences ( self . proto ) <EOL> for arg in ( <NUM_LIT:1> , <NUM_LIT:3> ) : <EOL> result = self . assertCall ( occs . pop ( <NUM_LIT:0> ) , "<STR_LIT>" , ( arg , ) ) <EOL> self . assertFalse ( occurrences ( result ) ) <EOL> self . assertFalse ( occs ) <EOL> def testLineInsertion ( self ) : <EOL> self . parser . dataReceived ( "<STR_LIT>" ) <EOL> occs = occurrences ( self . proto ) <EOL> for arg in ( <NUM_LIT:1> , <NUM_LIT:3> ) : <EOL> result = self . assertCall ( occs . pop ( <NUM_LIT:0> ) , "<STR_LIT>" , ( arg , ) ) <EOL> self . assertFalse ( occurrences ( result ) ) <EOL> self . assertFalse ( occs ) <EOL> def testCursorPosition ( self ) : <EOL> methods ( self . proto ) [ '<STR_LIT>' ] = ( <NUM_LIT:6> , <NUM_LIT:7> ) <EOL> self . parser . dataReceived ( "<STR_LIT>" ) <EOL> self . assertEqual ( self . transport . value ( ) , "<STR_LIT>" ) <EOL> occs = occurrences ( self . proto ) <EOL> result = self . assertCall ( occs . pop ( <NUM_LIT:0> ) , "<STR_LIT>" ) <EOL> self . assertEqual ( result , ( <NUM_LIT:6> , <NUM_LIT:7> ) ) <EOL> def test_applicationDataBytes ( self ) : <EOL> """<STR_LIT>""" <EOL> occs = occurrences ( self . proto ) <EOL> self . parser . dataReceived ( '<STR_LIT:a>' ) <EOL> self . assertCall ( occs . pop ( <NUM_LIT:0> ) , "<STR_LIT>" , ( "<STR_LIT:a>" , ) ) <EOL> self . parser . dataReceived ( '<STR_LIT>' ) <EOL> self . assertCall ( occs . pop ( <NUM_LIT:0> ) , "<STR_LIT>" , ( "<STR_LIT>" , ) ) <EOL> def _applicationDataTest ( self , data , calls ) : <EOL> occs = occurrences ( self . proto ) <EOL> self . parser . dataReceived ( data ) <EOL> while calls : <EOL> self . assertCall ( occs . pop ( <NUM_LIT:0> ) , * calls . pop ( <NUM_LIT:0> ) ) <EOL> self . assertFalse ( occs , "<STR_LIT>" % ( occs , ) ) <EOL> def test_shiftInAfterApplicationData ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _applicationDataTest ( <EOL> '<STR_LIT>' , [ <EOL> ( "<STR_LIT>" , ( "<STR_LIT>" , ) ) , <EOL> ( "<STR_LIT>" , ) ] ) <EOL> def test_shiftOutAfterApplicationData ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _applicationDataTest ( <EOL> '<STR_LIT>' , [ <EOL> ( "<STR_LIT>" , ( "<STR_LIT>" , ) ) , <EOL> ( "<STR_LIT>" , ) ] ) <EOL> def test_cursorBackwardAfterApplicationData ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _applicationDataTest ( <EOL> '<STR_LIT>' , [ <EOL> ( "<STR_LIT>" , ( "<STR_LIT>" , ) ) , <EOL> ( "<STR_LIT>" , ) ] ) <EOL> def test_escapeAfterApplicationData ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _applicationDataTest ( <EOL> '<STR_LIT>' , [ <EOL> ( "<STR_LIT>" , ( "<STR_LIT>" , ) ) , <EOL> ( "<STR_LIT:index>" , ) ] ) <EOL> self . _applicationDataTest ( <EOL> '<STR_LIT>' , [ <EOL> ( "<STR_LIT>" , ( "<STR_LIT>" , ) ) , <EOL> ( "<STR_LIT>" , ( [ <NUM_LIT:4> ] , ) ) ] ) <EOL> class ServerProtocolOutputTests ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def test_nextLine ( self ) : <EOL> """<STR_LIT>""" <EOL> protocol = ServerProtocol ( ) <EOL> transport = StringTransport ( ) <EOL> protocol . makeConnection ( transport ) <EOL> protocol . nextLine ( ) <EOL> self . assertEqual ( transport . value ( ) , "<STR_LIT:\r\n>" ) <EOL> class DeprecationsTests ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def ensureDeprecated ( self , message ) : <EOL> """<STR_LIT>""" <EOL> warnings = self . flushWarnings ( ) <EOL> self . assertIs ( warnings [ <NUM_LIT:0> ] [ '<STR_LIT>' ] , DeprecationWarning ) <EOL> self . assertEqual ( warnings [ <NUM_LIT:0> ] [ '<STR_LIT:message>' ] , message ) <EOL> self . assertEqual ( len ( warnings ) , <NUM_LIT:1> ) <EOL> def test_colors ( self ) : <EOL> """<STR_LIT>""" <EOL> namedAny ( '<STR_LIT>' ) <EOL> self . ensureDeprecated ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def test_client ( self ) : <EOL> """<STR_LIT>""" <EOL> namedAny ( '<STR_LIT>' ) <EOL> self . ensureDeprecated ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) </s>
