Ϫf7ddlZddlmZddlmZddlmZmZddlm Z ddl m Z ddl m Z ddlmZdd lmZd ZGd d eZd ZdZGddeZGddeZGddeZGddeZy)N)partial)BytesIO)mainprotocol)StringTransport)failure) iterbytes)banana)TestCaselceZdZdZy) MathTestsc^ttddttddzttddzdgz}|D]c}t}tj||j |j }tj|}|j||ey)NrdiiLi@BiBl) listrangerr int2b128writegetvalueb1282int assertEqual)self funkylistixvys A/usr/lib/python3/dist-packages/twisted/spread/test/test_banana.py test_int2b128zMathTests.test_int2b128s q# 5t$% &5'*+ ,j    #A A OOAqww ' A"A   Q "  #N)__name__ __module__ __qualname__rrrr r s #rr c(|j|y)z Dictate a Banana dialect to use. @param protocol: A L{banana.Banana} instance which has not yet had a dialect negotiated. @param dialect: A L{bytes} instance naming a Banana dialect to select. N)_selectDialect)rdialects r selectDialectr'#sHG$rct}|}|j||j|j||j S)a Banana encode an object using L{banana.Banana.sendEncoded}. @param bananaFactory: A no-argument callable which will return a new, unconnected protocol instance to use to do the encoding (this should most likely be a L{banana.Banana} instance). @param obj: The object to encode. @type obj: Any type supported by Banana. @return: A L{bytes} instance giving the encoded form of C{obj}. )rmakeConnectionclear sendEncodedvalue) bananaFactoryobj transportr s rencoder02sJ !I _FF)$ OOFs ?? rc:eZdZdZej ZdZdZdZ y)BananaTestBasezn The base for test classes. It defines commonly used things and sets up a connection for testing. c\t|_|j|_|jj t j |jt|jd|j|j_ tt|j|_ y)Nsnone) rioencClassencr)r FileWrapperr' putResultexpressionReceivedrr0rs rsetUpzBananaTestBase.setUpPsi)==?  4 4TWW =>dhh(&*nn#fdmm4 rc||_y)z Store an expression received by C{self.enc}. @param result: The object that was received. @type result: Any type supported by Banana. N)result)rr=s rr8zBananaTestBase.putResultXs  rc|jjtjtj |`yN)r6connectionLostrFailurerCONNECTION_DONEr:s rtearDownzBananaTestBase.tearDownas( 0D0D EF HrN) r r!r"__doc__r Bananar5r;r8rCr#rrr2r2Hs! }}H5rr2ceZdZdZdZdZdZdZdZdZ dZ d Z d Z d Z d Zd ZdZdZdZdZdZdZdZdZdZdZdZy) BananaTestsz General banana tests. c|jjd|jj|jj |j dk(sJy)Nshello)r6r+ dataReceivedr4rr=r:s r test_stringzBananaTests.test_stringksE X& dgg..01{{h&&&rc(|jddy)z Banana does not support unicode. ``Banana.sendEncoded`` raises ``BananaError`` if called with an instance of ``unicode``. helloz builtins.strN)_unsupportedTypeTestr:s rtest_unsupportedUnicodez#BananaTests.test_unsupportedUnicodeps !!'>:rc0|jtdy)z Banana does not support arbitrary builtin types like L{type}. L{banana.Banana.sendEncoded} raises L{banana.BananaError} if called with an instance of L{type}. z builtins.typeN)rMtyper:s rtest_unsupportedBuiltinTypez'BananaTests.test_unsupportedBuiltinTypews !!$8rcF|jttdzy)z Banana does not support arbitrary user-defined types (such as those defined with the ``class`` statement). ``Banana.sendEncoded`` raises ``BananaError`` if called with an instance of such a type. z .MathTestsN)rMr r r:s rtest_unsupportedUserTypez$BananaTests.test_unsupportedUserTypes !!)+x,/FGrc|jtj|jj|}|j d|dt |y)a Assert that L{banana.Banana.sendEncoded} raises L{banana.BananaError} if called with the given object. @param obj: Some object that Banana does not support. @param name: The name of the type of the object. @raise: The failure exception is raised if L{Banana.sendEncoded} does not raise L{banana.BananaError} or if the message associated with the exception is not formatted to include the type of the unsupported object. zBanana cannot send z objectsN) assertRaisesr BananaErrorr6r+assertInstr)rr.nameexcs rrMz BananaTests._unsupportedTypeTestsD 2 2DHH4H4H#N +D6:CHErc|jjd|jj|jj |j |j d|j|j ty)z A positive integer less than 2 ** 32 should round-trip through banana without changing value and should come out represented as an C{int} (regardless of the type which was encoded). i'N) r6r+rIr4rrr=assertIsInstanceintr:s rtest_intzBananaTests.test_ints] U# dgg..01 e, dkk3/rcV|jj}|dz}d|zdz }|dz}|S)N)r6 prefixLimit)rbytesbitslargestsmallests r _getSmallestzBananaTests._getSmallests7$$qyT'A+Q;rc|j}|jtj|jj |y)zz Test that a long above the implementation-specific limit is rejected as too large to be encoded. N)rhrUr rVr6r+)rrgs rtest_encodeTooLargeLongz#BananaTests.test_encodeTooLargeLongs4 $$& &,,dhh.B.BHMrc|j}|jj|jjdz|jj ||j j }|j jd|jj|jjdz|jtj|jj|y)zz Test that a long above the implementation specific limit is rejected as too large to be decoded. rarN) rhr6setPrefixLimitrcr+r4rtruncaterUr rVrI)rrgencodeds rtest_decodeTooLargeLongz#BananaTests.test_decodeTooLargeLongs $$&  4 4q 89 X&''""$   4 4 9: &,,dhh.C.CWMrc$|j Sr?)rhr:s r _getLargestzBananaTests._getLargests!!###rc|j}|jtj|jj |y)z Test that a negative long below the implementation-specific limit is rejected as too small to be encoded. N)rqrUr rVr6r+)rrfs rtest_encodeTooSmallLongz#BananaTests.test_encodeTooSmallLongs4 ""$ &,,dhh.B.BGLrc|j}|jj|jjdz|jj ||j j }|j jd|jj|jjdz|jtj|jj|y)z Test that a negative long below the implementation specific limit is rejected as too small to be decoded. rarN) rqr6rlrcr+r4rrmrUr rVrI)rrfrns rtest_decodeTooSmallLongz#BananaTests.test_decodeTooSmallLongs ""$  4 4q 89 W%''""$   4 4 9: &,,dhh.C.CWMrc|jjd|jj|jj |j |j dy)Nir6r+rIr4rrr=r:s r test_integerzBananaTests.test_integersG T" dgg..01 d+rc|jjd|jj|jj |j |j dy)Ni rwr:s r test_negativezBananaTests.test_negativesG U# dgg..01 e,rc|jjd|jj|jj |j |j dy)Ng@rwr:s r test_floatzBananaTests.test_floatsG V$ dgg..01 f-rc ddddgddgddd d d ggd ggg}|jj||jj|jj |j |j |y) Nrbra>@皙D@sixseveneight rwrfoos r test_listzBananaTests.test_lists} F 4L X!} - D    S! dgg..01 c*rc Vddddgddgddd d d ggd gtjdztjdztjd zg }|jj||j |j j |j|j|y)zj Test feeding the data byte per byte to the receiver. Normally data is not split. rbrar~rrrrrrrrrN) sysmaxsizer6r+feedr4rrr=rs r test_partialzBananaTests.test_partials  F 4L X!} - D KK!O KK!O KK"    S! $''""$% c*rcZt|D]}|jj|y)z Feed the data byte per byte to the receiver. @param data: The bytes to deliver. @type data: L{bytes} N)r r6rI)rdatabytes rrzBananaTests.feeds)dO (D HH ! !$ ' (rc^d}|jtj|j|y)NsrUr rVrrrs rtest_oversizedListzBananaTests.test_oversizedList#* &,,dii>rc^d}|jtj|j|y)Nsrrs rtest_oversizedStringz BananaTests.test_oversizedString!rrcrd} |jj|y#tj$rYywxYw)Ns)r6rIr rV)r crashStrings rtest_crashStringzBananaTests.test_crashString&s61   HH ! !+ .!!   s  66c|jjd|jj|jj |j |j dy)Nrwr:s rtest_crashNegativeLongz"BananaTests.test_crashNegativeLong9sI [) dgg..01 k2rcd}d}d}|j|j|dz d|z|j|j|dz d|z|j|j|dz d |zd }|j|j|dzd |z|j|j|dzd |z|j|j|d zd|zd}|j|j|dzd|z|j|j|dzd |z|j|j|dzdd}|j|j|dz d |z|j|j|dz d|z|j|j|d z d|zy)z Test that integers below the maximum C{INT} token size cutoff are serialized as C{INT} or C{NEG} and that larger integers are serialized as C{LONGINT} or C{LONGNEG}. irsra}rb~rsr~sssN)rr0)r baseIntIn baseNegIn baseIntOutbaseLongIntOut baseNegOutbaseLongNegOuts rtest_sizedIntegerTypesz"BananaTests.test_sizedIntegerTypesEs   ,  Y]3Wz5IJ Y]3Wz5IJ Y]3Wz5IJ0 Y]3W~5MN Y]3W~5MN Y]3W~5MN,  Y]3Wz5IJ Y]3Wz5IJ Y]35PQ0 Y]3W~5MN Y]3W~5MN Y]3W~5MNrN)r r!r"rDrJrNrQrSrMr^rhrjrorqrsrurxrzr|rrrrrrrrr#rrrGrGfs' ;9HF 0 N N$M N, - . ++,(? ? & 3OrrGceZdZdZdZeejjefejzZ edejzZ dZ dZ dZdZy) DialectTestsz2 Tests for Banana's handling of dialects. sremote)zcn|jt|jj|jy)z| If no dialect has been selected and a PB VOCAB item is received, L{NotImplementedError} is raised. N)rUNotImplementedErrorr6rI legalPbItemr:s rtest_dialectNotSetzDialectTests.test_dialectNotSetls' -txx/D/DdFVFVWrct|jd|jj|j|j |j |j y)zS If the PB dialect has been selected, a PB VOCAB item is accepted. pbN)r'r6rIrrr=vocabr:s rtest_receivePbzDialectTests.test_receivePbssB dhh& d../ djj1rct|jd|jt|jj|j y)z If the PB dialect has been selected and an unrecognized PB VOCAB item is received, L{banana.Banana.dataReceived} raises L{KeyError}. rN)r'r6rUKeyErrorrI illegalPbItemr:s rtest_receiveIllegalPbz"DialectTests.test_receiveIllegalPb{s4 dhh& (DHH$9$94;M;MNrct|jd|jj|j|j |j |j jy)zl if pb dialect is selected, the sender must be able to send things in that dialect. rN)r'r6r+rrrr4rr:s r test_sendPbzDialectTests.test_sendPbsK dhh& TZZ( ))477+;+;+=>rN)r r!r"rDrrdr rEoutgoingVocabularyVOCABrrrrrrr#rrrrcs[ E99%@BCfllRK&MFLL0MX2O?rrceZdZdZdZy)GlobalCoderTestszM Tests for the free functions L{banana.encode} and L{banana.decode}. cd}|jtjtj|d}|j tj|dy)zJ Calls to L{banana.decode} are independent of each other. sBsrbN)rUr rVdecoder)r undecodable decodables rtest_statelessDecodez%GlobalCoderTests.test_statelessDecodesE -  &,,fmm[I  y115rN)r r!r"rDrr#rrrrs  6rr)r functoolsrr4rtwisted.internetrrtwisted.internet.testingrtwisted.pythonrtwisted.python.compatr twisted.spreadr twisted.trial.unittestr _maxintr r'r0r2rGrrr#rrrss  +4"+!+  # # %,X'?T6x6r