3 SLj @sdZddlZddlZddlZddlZddlZy ddlZWnek rTddlZYnXddl m Z ddl m Z ddl m Z ddl m Z ddl mZddl mZdd l mZdd l mZdd lmZdd lmZdd lmZddlmZddlmZddlmZddlmZddlmZejZGdddeZGdddeZGdddeZGdddeZ GdddeZ!GdddeZ"Gdd d eZ#Gd!d"d"eZ$Gd#d$d$eZ%Gd%d&d&eZ&e'd'krej(dS)(ziUnittest for reflection.py, which also indirectly tests the output of the pure-Python protocol compiler. N)unittest_import_pb2)unittest_mset_pb2) unittest_pb2)descriptor_pb2) descriptor)message) reflection) text_format)api_implementation)more_extensions_pb2)more_messages_pb2)message_set_extensions_pb2) wire_format) test_util)testing_refleaks)decoderc@s\eZdZdZddZddZeZeZeZeZ ddZ e Z dd Z d d Z d d ZddZdS) _MiniDecoderaDecodes a stream of values from a string. Once upon a time we actually had a class called decoder.Decoder. Then we got rid of it during a redesign that made decoding much, much faster overall. But a couple tests in this file used it to check that the serialized form of a message was correct. So, this class implements just the methods that were used by said tests, so that we don't have to rewrite the tests. cCs||_d|_dS)Nr)_bytes_pos)selfbytesr%/usr/lib/python3.6/reflection_test.py__init__Osz_MiniDecoder.__init__cCstj|j|j\}|_|S)N)rZ _DecodeVarintrr)rresultrrr ReadVarintSsz_MiniDecoder.ReadVarintcCstj|jS)N)rZ ZigZagDecoder)rrrr ReadSInt64\sz_MiniDecoder.ReadSInt64cCstj|jS)N)rZ UnpackTagr)rrrrReadFieldNumberAndWireTypeasz'_MiniDecoder.ReadFieldNumberAndWireTypecCs4tjd|j|j|jdd}|jd7_|S)NzZ!d?d@Z"dAdBZ#dCdDZ$dEdFZ%dGdHZ&dIdJZ'dKdLZ(dMdNZ)dOdPZ*dQdRZ+dSdTZ,dUdVZ-dWdXZ.dYdZZ/d[d\Z0d]d^Z1d_d`Z2dadbZ3dcddZ4dedfZ5dgdhZ6didjZ7dkdlZ8dmdnZ9dodpZ:dqdrZ;dsdtZdydzZ?d{d|Z@d}d~ZAddZBddZCddZDddZEddZFddZGddZHddZIddZJddZKddZLddZMddZNddZOddZPddZQdddZRddZSeTjUeVjWdkpeVjXdkdddZYddZZddZ[ddZ\ddZ]ddZ^ddZ_dS)ReflectionTestcCsB|jt|t|x(tt|D]}|j||||q"WdS)N) assertEqualr$range)rvaluesZothersirrrassertListsEqualtszReflectionTest.assertListsEqualcCsPtjddddd}|jd|j|jd|j|jd|j|j|jddS)Ng +)K@optional_string)optional_int32optional_doubler6optional_floatr9)r TestAllTypesr0r7r8r6 assertFalseHasField)rprotorrrtestScalarConstructorysz$ReflectionTest.testScalarConstructorcCstjddddgddgdddgd gdd }|jddddgt|j|jddgt|j|jdddgt|j|jd gt|j|jgt|jdS) NrgGz?g +)K@TFr6)repeated_int32repeated_double repeated_boolrepeated_stringrepeated_float) rr:r0listrBrCrDrErF)rr=rrrtestRepeatedScalarConstructors z,ReflectionTest.testRepeatedScalarConstructorcCstjtjjtjjdtjjtjjdgtjd dtjddtjddgtjjtjjddtjjddgd }|jtjjtjjdtjjtjjdgt|j |jtjd dtjddtjddgt|j |jtjjtjjddtjjddgt|j dS) N)bb+)ci r?)ar@)repeated_nested_messagerepeated_foreign_message repeatedgroupii) rr: NestedMessageFOOBARForeignMessageZ RepeatedGroupr0rGrNrOrP)rr=rrr testRepeatedCompositeConstructors6              z/ReflectionTest.testRepeatedCompositeConstructorc Cs tjddddgdddgtjjtjjdtjjtjjdgtjdd tjd d tjd d gdd }|jd|j|jd|j|jddgt |j |jdddgt |j |jtjjtjjdtjjtjjdgt |j |jtjdd tjd d tjd d gt |j |j|jd dS)Nr5r6gGz?g +)K@TF)rIrJ)rKi rL)r7r6rCrDrNrOoptional_nested_messagerVii)rr:rQrRrSrTr0r7r6rGrCrDrNrOr;r<)rr=rrrtestMixedConstructors:            z#ReflectionTest.testMixedConstructorcCs|jttjdd|jttjdd|jttjdd|jttjdd|jttjdgd|jttjdd|jttjdgd|jttjdd|jttjdgddS) Nfoo)r7i)r6)rV)rB)rE)rN) assertRaises TypeErrorrr:)rrrrtestConstructorTypeErrors$z'ReflectionTest.testConstructorTypeErrorcCstjdd}|jd|jtjtjjd}|jd|jtjdgd}|jd|jtjtjjgd}|jd|jdS)NrL)r7r@)rVrA)rB)rN)rr:r0ByteSizerQ)rrrrr(testConstructorInvalidatesCachedByteSizes z7ReflectionTest.testConstructorInvalidatesCachedByteSizecCsptj}|j|jd |jd|j|j|jd d|_|j|jd|jd|j|jd dS)Nr7rr?)rr: assertTruer<r0r7 ClearField)rr=rrrtestSimpleHasBitss z ReflectionTest.testSimpleHasBitscs8fdd}|dd|dd|dd|d d dS) Ncs0tj}t||}t||}jd|j|j| j|j| d}t|||j|t|||}j|j|j|j||j|t||}j|j| j|j| jdt||j||k t|||j|j| j|j| jdt||dS)Nr)rr:getattrr0r^r<setattrr_)Zcomposite_field_nameZscalar_field_namer=Zcomposite_fieldZoriginal_scalar_valueZnew_valZold_composite_field)rrrTestCompositeHasBits.s,       zNReflectionTest.testHasBitsWithSinglyNestedScalar..TestCompositeHasBits optionalgrouprMrVrIoptional_foreign_messagerKZoptional_import_messagedr)rrdr)rr!testHasBitsWithSinglyNestedScalar s " *   z0ReflectionTest.testHasBitsWithSinglyNestedScalarcCstj}|j}~d|_dS)N)rr:rVrI)rr=nestedrrrtestReferencesToNestedMessage]sz,ReflectionTest.testReferencesToNestedMessagecCsTtj}|j}|jd|j||jk d|_|j|jd |jd|jjdS)NrVrir)rr:rVr_r^rIr<r0)rr=rjrrr0testDisconnectingNestedMessageBeforeSettingFieldes z?ReflectionTest.testDisconnectingNestedMessageBeforeSettingFieldcCs6tj}|j}|jd~~tjtj}|j}dS)NrV)rr:rVr_gccollect)rr=rjrrr5testGetDefaultMessageAfterDisconnectingDefaultMessagens zDReflectionTest.testGetDefaultMessageAfterDisconnectingDefaultMessagecCstj}|j}d|_|j|jd|jd|jd|j|jd|jj|j||jk d|_|j|jd |jd|jjdS)NrVrri)rr:rVrIr^r<r_r0)rr=rjrrr/testDisconnectingNestedMessageAfterSettingField{s z>ReflectionTest.testDisconnectingNestedMessageAfterSettingFieldcCs:tj}|j|jd |jd|j|jd dS)NrV)rr:r^r<r_)rr=rrr0testDisconnectingNestedMessageBeforeGettingFields z?ReflectionTest.testDisconnectingNestedMessageBeforeGettingFieldcCsRtj}tj}d|j_|j||j|jd|jd|j|jd dS)NrprV)rr:rVrI MergeFromr^r<r_)rproto1proto2rrr(testDisconnectingNestedMessageAfterMerges  z7ReflectionTest.testDisconnectingNestedMessageAfterMergecCs8tjdkrdStj}d|j_|jd~tjdS)Npythonrpoptional_lazy_message) r Typerr:rxrIr_rmrn)rr=rrr"testDisconnectingLazyNestedMessages  z1ReflectionTest.testDisconnectingLazyNestedMessagecCsttj}|jjjd|jdg|jj|j|jd|jd|j|jd |jj j |j|jddS)NrprV) rZTestNestedMessageHasBitsrVZnestedmessage_repeated_int32appendr0r^r<r_Z%nestedmessage_repeated_foreignmessageadd)rr=rrr&testHasBitsWhenModifyingRepeatedFieldss  z5ReflectionTest.testHasBitsWhenModifyingRepeatedFieldscCs tj}|j|jd |jd|jjjjjj|j|jd d|jjjjj_|jd|jjjjjj|j|jd|j|jjd|j|jjjd|j|jjjjd|j|jjjjjd|j|jjjjjjd |j|jjjjjjddS)NrIrrprMr7)rTestMutualRecursionAr^r<r0rIrMr7)rZrecursive_protorrr!testHasBitsForManyLevelsOfNestingsz0ReflectionTest.testHasBitsForManyLevelsOfNestingcCstj}d|_d|_d|_|j}|j|jjddf|jjddf|jjddfg|j d|j_ |j|jjddf|jjddf|jjddf|jjd|fg|j dS) Nr?rprXr7optional_fixed32r6{rV) rr:rr7r6rVr0 DESCRIPTORfields_by_name ListFieldsrI)rr=Znested_messagerrrtestSingularListFieldss$  z%ReflectionTest.testSingularListFieldsc Cstj}|jjd|jjd|jjd|jjddg|jjg|jjd|jjddtd Dd |_|j |j |j j d d f|j j d ddgf|j j d dgf|j j ddddddgfg|j dS)Nr?rp rXbarbazcss|]}t|VqdS)N)str).0xrrr sz8ReflectionTest.testRepeatedListFields..r@r7rBrepeated_fixed32rE01)rr:rr{rBrEextendr1r7rDr0rrr)rr=rrrtestRepeatedListFieldss"      z%ReflectionTest.testRepeatedListFieldscCsXtj}d|jtj<d|jtj<d|jtj<|jtjdftjdftjdfg|jdS)Nr?rprX)rTestAllExtensions ExtensionsZoptional_fixed32_extensionoptional_int32_extensionZoptional_string_extensionr0r)rr=rrrtestSingularListExtensionss    z)ReflectionTest.testSingularListExtensionscCstj}|jtjjd|jtjjd|jtjjd|jtjjd|jtjjd|jtjjdd|jtj<|jtjdftjddgftjdgftjdddgfg|j dS)Nr?rprrXrrr) rrrZrepeated_fixed32_extensionr{repeated_int32_extensionrepeated_string_extensionrr0r)rr=rrrtestRepeatedListExtensionss   z)ReflectionTest.testRepeatedListExtensionscCsftj}tj|tj|j|jjddftjdf|jjddftjdf|jjddfg|j dS) NZmy_intr?riZ my_stringrXrZmy_floatg?) rTestFieldOrderingsrSetAllFieldsAndExtensionsZmy_extension_intr0rrZmy_extension_stringr)rr=rrrtestListFieldsAndExtensions s z*ReflectionTest.testListFieldsAndExtensionscCstj}|jd|j|jd|j|jd|j|jd|j|jd|j|jd|j|jd|j |jd|j |jd|j |jd|j |jd|j |jd|j|jd|j|jd|j|jd|j|jd|j|jd|j|jd|j|jd |j|jd|j|jd |j|jd |j|jd |j|jd|j|jd|j|jd|j|jd|j|jd|j|jd|j|jd|j |jtjj!|j"|jtj#|j$|jt%j&|j'tj(}|jd|j)dS)NrgF)*rJ,-./012gI@gd@TZhellosworlduሴii)*rr:r0r7optional_int64optional_uint32optional_uint64optional_sint32optional_sint64roptional_fixed64optional_sfixed32optional_sfixed64r9r8 optional_boolr6optional_bytesZ default_int32Z default_int64Zdefault_uint32Zdefault_uint64Zdefault_sint32Zdefault_sint64Zdefault_fixed32Zdefault_fixed64Zdefault_sfixed32Zdefault_sfixed64Z default_floatZdefault_doubleZ default_boolZdefault_stringZ default_bytesrSZdefault_nested_enum FOREIGN_BARZdefault_foreign_enumrZ IMPORT_BARZdefault_import_enumZTestExtremeDefaultValuesZ utf8_string)rr=rrrtestDefaultValuessJz ReflectionTest.testDefaultValuescCstj}|jt|jddS)Nnonexistent_field)rr:rY ValueErrorr<)rr=rrr testHasFieldWithUnknownFieldName@sz/ReflectionTest.testHasFieldWithUnknownFieldNamecCstj}|jt|jddS)Nr)rr:rYrr_)rr=rrr"testClearFieldWithUnknownFieldNameDsz1ReflectionTest.testClearFieldWithUnknownFieldNamecsNtjxtdD]}jjqWtj}j||jtfdddS)N cs jdS)Nrp)repeated_messager)r=rrQsz9ReflectionTest.testClearRemovesChildren..)rTestRequiredForeignr1rr|CopyFromrY IndexError)rr3rur)r=rtestClearRemovesChildrenHs  z'ReflectionTest.testClearRemovesChildrencCs~tj}|jtt|dd|jtt|ddg|jtt|dd|jtt|jdd|jtt|jdd|jtt|dddS) NrBrrVrirI"Zsome_attributer)rr:rYAttributeErrorrcrNrF)rr=rrrtestDisallowedAssignmentsSs   z(ReflectionTest.testDisallowedAssignmentscCstj}|jtt|dd|jtt|dd|jtt|dd|jtt|dd|jtt|dd|jtt|dd|jtt|d dtjd kr|jtt|ddnd|_dS) Nr7g?rXr6rrrr9r8rw)rr:rYrZrcr ryr)rr=rrrtestSingleScalarTypeSafetyhs z)ReflectionTest.testSingleScalarTypeSafetyc sfdd}|ddt|ddt|ddtyt}Wntk rRt}YnXtjddkrp|dd|n |ddt|d d||d d||d d||d d|d S)z}Verifies setting of scalar integers. Args: integer_fn: A function to wrap the integers that will be assigned. csZtj}|}t|||jt|||tj}|j|jjt|||dS)N)rr:rcassertIsInstancerbParseFromStringSerializeToString) field_namevalueZ expected_typer=ru) integer_fnrrrTestGetAndDeserialize}s z@ReflectionTest.assertIntegerTypes..TestGetAndDeserializer7r?rLrr<rNi@i@lli@li@l)intZlong NameErrorrcalcsize)rrrZ integer_64r)rrrassertIntegerTypesws        z!ReflectionTest.assertIntegerTypescCs|jdddS)NcSs|S)Nr)rrrrrsz1ReflectionTest.testIntegerTypes..)r)rrrrtestIntegerTypesszReflectionTest.testIntegerTypescCs|jtjdS)N)rrNonStandardInteger)rrrrtestNonStandardIntegerTypessz*ReflectionTest.testNonStandardIntegerTypescCsNtj}|jt d|_WdQRX|jtdtjdd|_WdQRXdS)N2Zmy_errorrp) rr:rYrZrZassertRaisesRegexp RuntimeErrorrr)rpbrrrtestIllegalValuesForIntegerss  z+ReflectionTest.testIllegalValuesForIntegersc sfdd}|ddd|ddd|d dd|d dd tj}jttfd|_Wd QRXtj}d|_jd|jd S)zVerifies bounds checking for scalar integer fields. Args: integer_fn: A function to wrap the integers that will be assigned. cstj}|}|}t|||j|t||t|||j|t||jttft|||djttft|||ddS)Nr?)rr:rcr0rbrYrrZ)rZ expected_minZ expected_maxr)rrrrTestMinAndMaxIntegerss   zHReflectionTest.assetIntegerBoundsChecking..TestMinAndMaxIntegersr7r?rrrlr?rlNlilillllll)rr:rYrrZroptional_nested_enumr0)rrrrr)rrrassetIntegerBoundsCheckings     z)ReflectionTest.assetIntegerBoundsCheckingcCs|jdddS)NcSs|S)Nr)rrrrrsz?ReflectionTest.testSingleScalarBoundsChecking..)r)rrrrtestSingleScalarBoundsCheckingsz-ReflectionTest.testSingleScalarBoundsCheckingcCs|jtjdS)N)rrr)rrrr)testNonStandardSingleScalarBoundsCheckingsz8ReflectionTest.testNonStandardSingleScalarBoundsCheckingcCstj}|jt|jjd|jt|jjd|jt|jd|jt|jd|jjdd|jd<|jt|jj dd|jt|jj dd|jt|jj dg|jt|jj dd|jjd |jt|jj dddS) Ng?rXrririabcindexr) rr:rYrZrBr{rEZrepeated_bytesr __setitem__)rr=rrrtestRepeatedScalarTypeSafetys    z+ReflectionTest.testRepeatedScalarTypeSafetycCsVtj}|jd|jd|_|jd|jd|_|jd|jd|_|jd|jdS)Nrr?ll)rr:r0r7r)rr=rrr!testSingleScalarGettersAndSetterssz0ReflectionTest.testSingleScalarGettersAndSetterscCsVtj}|jdd|_|j|jd|jd|jd|j|j|jd dS)Nr7r?r)rr:r_r7r^r<r0)rr=rrrtestSingleScalarClearFields  z)ReflectionTest.testSingleScalarClearFieldcCsftj}|jd|j|jdtjj|jd|j|jdtjj|jd|j|jdtjjdS)Nr?r@rA)rr:r0rRrSBAZ)rr=rrr testEnumsszReflectionTest.testEnumscCs|jdtjjtj|jdtjjtj|jdtjjtj|jttjjdtj }|jd|j j|j |jdtj j j|j |jd|j j|j |jdtj j j|j |jd|j j|j |jdtj j j|j |jt|j jd|jttj j jddS)N FOREIGN_FOOr FOREIGN_BAZi0,rRrSr)r0r ForeignEnumNamerrrrYrr: NestedEnumrRrSr)rr=rrr testEnum_Names2  zReflectionTest.testEnum_NamecCs|jtjtjjd|jtjtjjd|jtjtjjd|jttjjdtj }|j|j |j jd|j|j tj j jd|j|j |j jd|j|j tj j jd|j|j |j jd|j|j tj j jd|jt|j jd|jttj j jddS) NrrrZFOrRrSrZFoo)r0rrrZValuerrrYrr:rRrrSr)rr=rrrtestEnum_Value"s2  zReflectionTest.testEnum_ValuecCs|jdddgttjj|jdddgttjj|jdddgttjjtj}|jddd d gt|jj|jd d d dgt|jj|jddddgt|jjdS)NrrrrrprRrSrNEGr?r@rA)rr)rrp)rr)rRr?)rSr@)rrAr)rr) r0rGrrkeysr2itemsr:r)rr=rrrtestEnum_KeysAndValues>s  z%ReflectionTest.testEnum_KeysAndValuescCs|tj}|j|j |jdt|j|jjd|jjd|jjd|j|j|jdt|j|jdddg|j|jd|jd|jd|jd|jt|jj d|jt|jj d|jt |jj d|jt |jj dd |jd<|jdd dg|j|jj dd |jdd d dg|j|jjd |jd d dg|jdd |jdd d dd g|jddd dt dD|jdd <|jddddd g|jdddg|jdd <|jddddd g|jg}x|jD]}|j|qW|jddddd g||jd=|jdddd g|j|jdd=|jddg|j|jj ddg|jddddg|j|jd|j|j |jdt|j|jjd|jd|jdd|jd<|jd|jdddddg|jdd<|jd=|jdddg|j|jd=|jddg|j|jt|jjd|jt|jjd|jd d!=|jdg|j|jdd=|jdg|jdS)"NrrprrAr?irXrarrcss|] }|VqdS)Nr)rr3rrrrrsz5ReflectionTest.testRepeatedScalars..r@#(r rBi,di'ri.rrrrrr)rr:r^rBr0r$r{rYr __getitem__rZinsertr1rr_ __delitem__)rr=rr3rrrtestRepeatedScalarsMsl              z"ReflectionTest.testRepeatedScalarscCsFtj}|j|j |jdt|j|jjd|jjd|jjd|jjd|jdt|j|jjd|jdt|j|jd|jd|jd|jd|jd|jd|jjd|jdt|j|jd|jd|jd|jd|jjd|jdt|j|jd|jd|jt |jjddS) NrrprrrAr?r@r) rr:r^rBr0r$r{removerYr)rr=rrrtestRepeatedScalarsRemoves*       z(ReflectionTest.testRepeatedScalarsRemovec Cs(tj}|j|j |jdt|j|jj}|jj}|j|j|jdt|j|j||g|j|j|tjj |j t |jj d|j t |jj d |j t |jj d|j t |jj d|jj}|jj}|jj}|j|||g|jdd|j|||||g|jdd|j||g|jdd|j|||g|jdd|j||jd|j|g|jddg}x|jD]}|j|qW|j|||||g||jd=|j||||g|j|jdd=|j||g|jtjj dd} tjj dd} |jj| | g|jdt|j|j| |jd|j| |jd|j t |jj| |j t |jjdgtj} |j t |jj| g|jd |j|j |jdt|j|jjd d|jdt|j|jd |jdj|j t |jjd |j td |jd<WdQRXdS) Nrr@irXr?r)rIrArNrii.)rr:r^rNr0r$r|r4rrQrYrrrZr{rr_rI Exception) rr=m0m1m2Zm3Zm4rr3Zn1Zn2Zwrong_message_typerrrtestRepeatedCompositess~               z%ReflectionTest.testRepeatedCompositescCs<tj}|jdt|j|jj}t|j|_|jj}t|j|_|j||k|jj}t|j|_|j|||g|j|jdt|j|jj ||jdt|j|j||jd|j||jd|j t |jj ||j t |jj d|jdt|j|jj ||jdt|j|j||jddS)NrrAr@r?) rr:r0r$rNr|rIr^r4rrYr)rr=rrrrrrtestRepeatedCompositeRemoves*        z*ReflectionTest.testRepeatedCompositeRemovecstjdkrdStj}|dddd|j|j|jddddddtjd}tj ddd dgg|ggtj d Gfd d d t j t jtj}|}|jd|j|j|jd d |_|jd |j|j|jddS)Nrw foo_fieldzMyProto.foo_fieldrr?F)name full_namernumbertypeZcpp_typelabelZ default_valuecontaining_type message_typeZ enum_typeZ is_extensionZextension_scopeoptionsZMyProtoZignored) r r filenamer nested_types enum_typesfields extensionsrcseZdZZdS)z>ReflectionTest.testHandWrittenReflection..MyProtoClassN)r&r'r(rr) mydescriptorrr MyProtoClassHsrri)r ryrFieldDescriptor TYPE_INT64Z CPPTYPE_INT64LABEL_OPTIONALrZ FieldOptions DescriptorZMessageOptionssixwith_metaclassrGeneratedProtocolMessageTyperMessager0rr^r<)rrZfoo_field_descriptorrZmyproto_instancer)rrtestHandWrittenReflection4s.    z(ReflectionTest.testHandWrittenReflectioncstjdkrdSfddd_tj}d|_tj}|d|j|d|j|d|j |d |j jd 7_|j j }d |_|j|_ j|_tjj|_tj||jdjk|jdjk|jdjk|jd jk|jd jkGfd d d tjtjtj}|}d|_d|_d|_d|_|jjddg|j }tj!|}|j||k |j"|||j"|j|j|j"|j|j|j"|j|j|j"|j|j|j"|j|jdS)Nrwcs:jd7_|jj}||_||_j|_tjj|_ dS)Nr?) field_indexfieldr|r r r rFieldDescriptorProtorr )r=rZ field_type new_field)AddDescriptorFieldrrr%Ws  zEReflectionTest.testDescriptorProtoSupport..AddDescriptorFieldrZCarr year automaticpricer?ownerscseZdZZdS)z=ReflectionTest.testDescriptorProtoSupport..CarMessageN)r&r'r(rr)descrr CarMessagewsr+priusiTg@ZbobZsusan)#r ryr!rZDescriptorProtor r#Z TYPE_STRINGrZ TYPE_BOOLZ TYPE_DOUBLEr"r|r r LABEL_REPEATEDr rMakeDescriptorr^rrrrrrrr&r'r(r)rrZ ParseMessager0)rZ desc_protoZfdpr$r+r,Zserialized_priusZ new_priusr)r%r*rtestDescriptorProtoSupportQsN        z)ReflectionTest.testDescriptorProtoSupportcCstj}tj}|j|j| |jd|j||j|j| d|j|<|jd|j||j|j||j||jd|j||j|j| dS)Nrri)rrrr^ HasExtensionr0rClearExtension)rextendee_proto extensionrrr'testTopLevelExtensionsForOptionalScalars  z6ReflectionTest.testTopLevelExtensionsForOptionalScalarcCstj}tj}|jdt|j||j|jd|jdg|j||j|}|j||jdt|j||j||j|k |j t t j |j|ddS)NrrXrM) rrrr0r$rr{r1r^rYrZoperatorsetitem)rr2r3Z string_listrrr'testTopLevelExtensionsForRepeatedScalars  z6ReflectionTest.testTopLevelExtensionsForRepeatedScalarcCstj}tj}|j|j| |jd|j|j|j|j| d|j|_|jd|j|j|j|j||j|}|j||j||j|k d|_|jd|j|j|j d|j|j| |j t t j |j|ddS)NrrirrKrM)rrZ"optional_foreign_message_extensionr^r0r0rrKr1r<rYrZr5r6)rr2r3Zforeign_messagerrr(testTopLevelExtensionsForOptionalMessages"   z7ReflectionTest.testTopLevelExtensionsForOptionalMessagecCstj}tj}|jdt|j||j|j}d|_|jd|j|djd|_|jd|j|dj|j|}|j||jdt|j||j ||j|k |j t t j |j|ddS)NrrirrM)rrrepeatedgroup_extensionr0r$rr|rMr1r^rYrZr5r6)rr2r3groupZ group_listrrr(testTopLevelExtensionsForRepeatedMessages  z7ReflectionTest.testTopLevelExtensionsForRepeatedMessagecCstj}tjj}|j|j| |j|}|jd|j|j|j| d|_|jd|j|j|j|j||j ||j||j|k |j|j| dS)Nrri) rr TestRequiredsingler^r0rr0rMr1)rr2r3ZrequiredrrrtestNestedExtensionss  z#ReflectionTest.testNestedExtensionscCsTtjj}|j|jtjjd|j|jdjtjj|j dt |j tj jdS)Nr?z*protobuf_unittest.optional_int32_extensionr) rrpoolr^ZFindExtensionByNumberrZassertIsZFindExtensionByNamerr0r$ZFindAllExtensionsr:)rr?rrrtestRegisteredExtensionss z'ReflectionTest.testRegisteredExtensionscCstj}|j|jd |jd|jjtj|j|jd d|jjtj<|jd|jjtj|j|jdtj}|j|jd |jg|jjtj|j|jd |jjtjj d|jdg|jjtj|j|jdtj}|j|jd |jd|jjtj j |j|jd d|jjtj _ |jd|jjtj j |j|jdtj}|j|jd |jdt |jjtj |j|jd |jjtj j}|j||jjtj d|j|jddS)N submessagerri)r TopLevelMessager^r<r0rAroptional_int_extensionrepeated_int_extensionr{optional_message_extensionforeign_message_intr$repeated_message_extensionr|)rtoplevelforeignrrr(testHasBitsForAncestorsOfExtendedMessagesL              z7ReflectionTest.testHasBitsForAncestorsOfExtendedMessagecCsFtj}|j}tj}|j|}|j|d|_|j||j|k dS)Nri)r rBrArErr1rFr^)rrHr2r3Zextension_protorrr*testDisconnectionAfterClearingEmptyMessage*s  z9ReflectionTest.testDisconnectionAfterClearingEmptyMessagecCstj}|jt|jd|jt|jd|jt|jjd|jt|jjddxbt j t j t j t j fD]J}|jt|j||jt|j||jt|jj||jt|jj|dqdW|jt|jtjdS)Nirp)rrrYKeyErrorr0r1rrrr rCrErDrGr)rr2Zunknown_handlerrrtestExtensionFailureModes4s&       z(ReflectionTest.testExtensionFailureModescCs6tj}tj||j}tjj|}|j||dS)N)rr:r SetAllFieldsr FromStringr0)rrtstring1rurrrtestStaticParseFromSs   z"ReflectionTest.testStaticParseFromcCsFtj}d|_tj}d|_|j||jd|j|jd|jdS)Nr?r)rr:r7r6rsr0)rrtrurrrtestMergeFromSingularField]s z)ReflectionTest.testMergeFromSingularFieldcCsxtj}|jjd|jjdtj}|jjd|j||jd|jd|jd|jd|jd|jddS)Nr?r@r)rr:rBr{rsr0)rrtrurrrtestMergeFromRepeatedFieldjs    z)ReflectionTest.testMergeFromRepeatedFieldcCs6tj}d|j_tj}|j||jd|jjdS)NrL)rr:rerMrsr0)rrtrurrrtestMergeFromOptionalGroupxs  z)ReflectionTest.testMergeFromOptionalGroupcCstj}|jj}d|_|jj}d|_tj}|jj}d|_|j||jd|jdj|jd|jdj|jd|jdjtj}|jj|j|jd|jdj|jd|jdj|jd|jdjdS)NriAirr?r@)rr:rNr|rIrsr0)rrtmruZproto3rrr"testMergeFromRepeatedNestedMessages"    z1ReflectionTest.testMergeFromRepeatedNestedMessagecCsPtj}tj|tj}|j||j|||j}|j}|j||dS)N)rr:rrNrsr0r)rrtrurPstring2rrrtestMergeFromAllFieldss   z%ReflectionTest.testMergeFromAllFieldscCs>tj}d|jtj<tj}|j||jd|jtjdS)Nr?)rrrrrsr0)rrtrurrrtestMergeFromExtensionsSingulars   z.ReflectionTest.testMergeFromExtensionsSingularcCstj}|jtjjd|jtjjdtj}|jtjjd|j||jdt|jtj|jd|jtjd|jd|jtjd|jd|jtjddS)Nr?r@rrA)rrrrr{rsr0r$)rrtrurrrtestMergeFromExtensionsRepeateds z.ReflectionTest.testMergeFromExtensionsRepeatedcCstj}|jtj}|j}d|_|j}d|_tj}|jtj}|j}d|_|j||jtj}|jdt||jd|dj|jd|dj|jd|djdS)NiMorArr?r@) rrr!repeated_nested_message_extensionr|rIrsr0r$)rrtZext1rUruZext2rrr$testMergeFromExtensionsNestedMessages& z3ReflectionTest.testMergeFromExtensionsNestedMessagecCsDtj}tj}|j|j|jd|j||j|jddS)NrV)rr:rVr;r<rs)rmessage1message2rrrtestMergeFromBugs  zReflectionTest.testMergeFromBugcCsLtj}d|_d|_tj}d|_|j||jd|j|jd|jdS)Nr?zimportant-textr)rr:r7r6rr0)rrtrurrrtestCopyFromSingularFields z(ReflectionTest.testCopyFromSingularFieldcCsftj}|jjd|jjdtj}|jjd|j||jd|jd|jd|jddS)Nr?r@r)rr:rBr{rr0)rrtrurrrtestCopyFromRepeatedFields    z(ReflectionTest.testCopyFromRepeatedFieldcCsPtj}tj|tj}|j||j|||j}|j}|j||dS)N)rr:rrNrr0r)rrtrurPrWrrrtestCopyFromAllFieldss   z$ReflectionTest.testCopyFromAllFieldscCs\tj}|jjdd|_d|_|j||jd|jd|jd|j|jd|jdS)Nr?r@zimportant-textr)rr:rBr{r7r6rr0)rrtrrrtestCopyFromSelf s  zReflectionTest.testCopyFromSelfcCs4tjdkrdStj}tj}|jt|j|dS)Nrw)r ryrr:rrYrZr)rrtrurrrtestCopyFromBadTypes  z"ReflectionTest.testCopyFromBadTypecCstj}d|_tj|}|jd|j|jjd|jjdtj|j}|jddg||jj }d|_ tj|j}|j|j|d|_ |j |j|dS)Nr?r@rA) rr:r7copydeepcopyr0rBr{rNr|rIassertNotEqual)rrtruZ containerr_Zmessagesrrr testDeepCopy s      zReflectionTest.testDeepCopycCstj}tjdkr tj|n tj||j|j|j dtj}|j||tj }tj ||j|j|j dtj }|j||dS)Nrwr) rr:r ryrrNZSetAllNonLazyFieldsClearr0r\rSetAllExtensions)rr=Z empty_protorrr testClear4s     zReflectionTest.testClearcCstj}|j}|j|j||jk d|_|j|jd |jd|jjtj}|j}d|_|j}d|_ |j|j||jk |j||jk |jd|j|jd|j d|_d|_ |j |jd|jd|jj|j |jd|jd|jj dS) NrirVrrprrrf) rr:rVrkr^rIr<r0rfrKr;)rr=rjrIrrrtestDisconnectingBeforeClearKs.z+ReflectionTest.testDisconnectingBeforeClearcCsftj}d|j_|j}|j|jjd|j|jjdd|j_|j|jjd|j|jdd|_dS)Nrprr)rZ TestOneof2Z foo_messageZqux_intr0Zfoo_lazy_message)rrUZ sub_messagerrrtestDisconnectingInOneoffsz'ReflectionTest.testDisconnectingInOneofcCsrtj}d|_d|j_|jd|jj|j|jd|j}d|_|jd|j|jd|j|j|jddS)Nrr oneof_uint32roneof_nested_message) rr:rrrsrIr0r;r<Z oneof_string)rr=rjrrr testOneOftszReflectionTest.testOneOfcCs"|j|j|j|jdS)N)r^ IsInitializedrSerializePartialToString)rr=rrrassertInitializedsz ReflectionTest.assertInitializedNcCsNg}|j|j|j|j||j|t||jtj|j|jdS)N) r;rur0r$rYr EncodeErrorrrv)rr=Z error_sizeerrorsrrrassertNotInitializeds z#ReflectionTest.assertNotInitializedcCstj}|j|tj}|j|tj}|j|dd|_|_|_|j|tj }|j|d|j _|j|dd|j _d|j _|j||j j }|j|dd|_|_|_|j|tj}tjj }|j|j }|j|j }|j|dd|_d|_d|_|j|dd|_d|_d|_|j|tj}tjj}d|j|_|j|dd|j|_d|j|_|j|g}tj}|j|j||j|dddgdS) NrAr@r?rrrMbrK)rr:rwrr<rzrMr{rKroptional_messagerr|multirr=r;rur0)rr=r_r3r`ryrrrtestIsInitializedsV                  z ReflectionTest.testIsInitializedcppr@zBErrors are only available from the most recent C++ implementation.cCsd}d}tj}||_||_|jj}d|_tj|||jdd}||_|jj}d|_|j t `}tj|||jd|j t |ddt |jd|j|jdt|j|jd t|jWdQRXdS) Nz!test_file_descriptor_errors.protoZmsg1)Z serialized_pbz)another_test_file_descriptor_errors.protoZmsg2 exceptionz %s not raisedr&z test_file_descriptor_errors.msg1)rFileDescriptorProtor packagerr|rZFileDescriptorrrYrZr^hasattrrbexpectedassertInrr)r file_nameZ package_nameZfile_descriptor_protorZanother_file_namercmrrrtestFileDescriptorErrorss0     z'ReflectionTest.testFileDescriptorErrorscCstj}|jtt|dd|jt|jtj tj d|_|j|jt dt d|_|j|jtj d|jt t|dddj d}||_d|_d|_dS) Nrzunicode objectZTestingr6saauТестzutf-8r) rr:rYrZrcr0r r6r text_typerrencode)rr=Z utf8_bytesrrrtestStringUTF8Encodings     z%ReflectionTest.testStringUTF8Encodingc Csjtj}tj}|j}d}|jd}||j|_|j}|j|j t |t j }|j |}|jt ||tj} |jdt |j|j|jdjd|j|jdjj|| j |jdj}|jt |jdj||jt| jtj|j| j||jdjj|t |d} d} y| j | Wntk rFd} YnX| j} |j| pbt| tkdS) NuТестzutf-8r?riʿFT)r TestMessageSetTestMessageSetExtension2message_set_extensionrrrrr0r\r$r RawMessageSetMergeFromStringitemtype_idr^rendswithr rrreplaceUnicodeDecodeErrorr) rr=Zextension_messager3Z test_utf8Ztest_utf8_bytes serializedraw bytes_readr`ZbadbytesZunicode_decode_failedZ string_fieldrrrtestStringUTF8Serializations8     z*ReflectionTest.testStringUTF8SerializationcCs"tjdd}|jdtj|dS)Ns)rz#optional_bytes: "\000\177\200\377" )rr:r0rr)rr=rrrtestBytesInTextFormat=s z$ReflectionTest.testBytesInTextFormatcCstj}|jjtjj|j|jdtj}|jjtjj|j|jdtj}|jjd}|j d||j|jdtj}|jj d|j|jd|j }tj}|j t ||j||j|jddS)NrVrr) rr:rVrsrQr^r<rrr0rrr$)rr=rrrurrrtestEmptyNestedMessageBs*      z%ReflectionTest.testEmptyNestedMessagecCs6tj}|j|jd|jj|j|jddS)Nre)rr:r;r<reZ SetInParentr^)rr=rrrtestSetInParent]s zReflectionTest.testSetInParentcCs*ddlm}|jj}|jd|jjdS)aTest that we can import nested messages from their __init__.py. Such setup is not trivial since at the time of processing of __init__.py one can't refer to its submodules by name in code, so expressions like google.protobuf.internal.import_test_package.inner_pb2 don't work. They do work in imports, so we have assign an alias at import and then use that alias in generated code. r)import_test_package9N)google.protobuf.internalrZmyprotoZOuterr0innerr)rrmsgrrrtestPackageInitializationImportcs  z.ReflectionTest.testPackageInitializationImport)N)`r&r'r(r4r>rHrUrWr[r]r`rhrkrlrorqrrrvrzr}rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr r/r4r7r8r;r>r@rJrKrMrQrRrSrTrVrXrYrZr^rarbrcrdrerfrjrmrorqrtrwrzr~unittestskipIfr ryVersionrrrrrrrrrrrr/rs "#Q       ) " !   ZS=1           : 6r/c@s,eZdZddZddZddZddZd S) TestAllTypesEqualityTestcCstj|_tj|_dS)N)rr: first_proto second_proto)rrrrsetUp{s zTestAllTypesEqualityTest.setUpcCs|jtt|jdS)N)rYrZhashr)rrrrtestNotHashablesz(TestAllTypesEqualityTest.testNotHashablecCs|j|j|jdS)N)r0r)rrrrtestSelfEqualitysz)TestAllTypesEqualityTest.testSelfEqualitycCs|j|j|jdS)N)r0rr)rrrrtestEmptyProtosEqualsz-TestAllTypesEqualityTest.testEmptyProtosEqualN)r&r'r(rrrrrrrrrysrc@sheZdZdZddZddZddZdd Zd d Zd d Z ddZ ddZ ddZ ddZ ddZdS)FullProtosEqualityTestz@Equality tests using completely-full protos as a starting point.cCs0tj|_tj|_tj|jtj|jdS)N)rr:rrrrN)rrrrrs   zFullProtosEqualityTest.setUpcCs|jtt|jdS)N)rYrZrr)rrrrrsz&FullProtosEqualityTest.testNotHashablecCs |j|jd|jd|jdS)N)rirr)rrrrtestNoneNotEqualsz'FullProtosEqualityTest.testNoneNotEqualcCs(tj}|j|j||j||jdS)N)rr<rirr)rZ third_protorrrtestNotEqualToOtherMessagesz1FullProtosEqualityTest.testNotEqualToOtherMessagecCs|j|j|jdS)N)r0rr)rrrrtestAllFieldsFilledEqualitysz2FullProtosEqualityTest.testAllFieldsFilledEqualitycCs@|jjd7_|j|j|j|jjd|j|j|jdS)Nr?r7)rr7rirr_)rrrrtestNonRepeatedScalars z,FullProtosEqualityTest.testNonRepeatedScalarcCs|jjjd7_|j|j|j|jjjd8_|j|j|j|jjjd|j|j|j|jjj|jj_|j|j|j|jjd|j|j|jdS)Nr?rIrV)rrVrIrirr0r_)rrrrtestNonRepeatedComposites z/FullProtosEqualityTest.testNonRepeatedCompositecCs>|jjjd|j|j|j|jjd|j|j|jdS)NrprB)rrBr{rirr_)rrrrtestRepeatedScalars z)FullProtosEqualityTest.testRepeatedScalarcCs|jjdjd7_|j|j|j|jjdjd8_|j|j|j|jjj|j|j|j|jjj|j|j|jdS)Nrr?)rrNrIrirr0r|)rrrrtestRepeatedComposites  z,FullProtosEqualityTest.testRepeatedCompositecCs(|jjdd|j_|j|j|jdS)Nr7r)rr_rr7ri)rrrrtestNonRepeatedScalarHasBitss z3FullProtosEqualityTest.testNonRepeatedScalarHasBitscCsV|jjd|jjjd|j|j|jd|jj_|jjjd|j|j|jdS)NrVrIr)rr_rrVrirIr0)rrrrtestNonRepeatedCompositeHasBitss   z6FullProtosEqualityTest.testNonRepeatedCompositeHasBitsN)r&r'r(r)rrrrrrrrrrrrrrrrs rc@seZdZddZdS)ExtensionEqualityTestcCstj}tj}|j||tj||j||tj||j|||jtjd7<|j|||jtjd8<|j|||jtjd|jtj<|j||d|jtj<|j||tj}tj}|jd|jtj|j||dS)Nr?r) rrr0rrlrirrr1)rrrrrrtestExtensionEqualitys*            z+ExtensionEqualityTest.testExtensionEqualityN)r&r'r(rrrrrrsrc@seZdZddZdS)MutualRecursionEqualityTestcCsPtj}tj}|j||d|jjj_|j||d|jjj_|j||dS)Nri)rr~r0rIrMr7ri)rrrrrrtestEqualityWithMutualRecursions    z;MutualRecursionEqualityTest.testEqualityWithMutualRecursionN)r&r'r(rrrrrrsrc@seZdZddZddZddZddZd d Zd d Zd dZ ddZ ddZ ddZ ddZ ddZddZddZddZdd Zd!d"Zd#d$Zd%d&Zd'd(Zd)d*Zd+d,Zd-S). ByteSizeTestcCs,tj|_tj|_tj|_tj|_ dS)N) rr:r=r ZExtendedMessageextended_protoTestPackedTypes packed_protoTestPackedExtensionspacked_extended_proto)rrrrrs   zByteSizeTest.setUpcCs |jjS)N)r=r\)rrrrSizeszByteSizeTest.SizecCs|jd|jjdS)Nr)r0r=r\)rrrrtestEmptyMessageszByteSizeTest.testEmptyMessagecCs8tj}|jd|jtjdd}|jd|jdS)Nrr?)rr@)rr:r0r\)rr=Z proto_kwargsrrrtestSizedOnKwargss zByteSizeTest.testSizedOnKwargscszfdd}|dd|ddx6ttdddtddD]\}}|d|>d|q:W|d d|d d|d ddS)Ncs*jj|j_j|djdS)Nr?)r=rkrr0r)r3Zexpected_varint_size)rrrTest#s z&ByteSizeTest.testVarints..Testrr?rpri'rr@rrll)zipr1)rrr3Z num_bytesr)rr testVarints"s   "  zByteSizeTest.testVarintscCshd|j_|jd|jd|j_|jdt|jj|jdd|j_|jdt|jj|jdS)Nrr@rrrA)r=r6r0rr$)rrrr testStrings1s  zByteSizeTest.testStringscCsd|j_|jd|jtj|_d|j_|jd|jtj|_d|j_|jd|jtj|_d|j_|jd|jtj|_d|j_ |jd|jtj|_dS)Nirp gX9v?@rA) r=rr0rrr:rr9r8r)rrrrtestOtherNumerics>s    zByteSizeTest.testOtherNumericscCsd|jj_|jd|jdS) Nr?rAr@i@rrprp)r=rVrIr0r)rrrrtestCompositesXs zByteSizeTest.testCompositescCsd|jj_|jd|jdS) Nr?rrr@i rrr)r=rerMr0r)rrrr testGroups`s zByteSizeTest.testGroupscCs0|jjjd|jjjd|jd|jdS)Nrrr?r@rArrp)r=rBr{r0r)rrrrrgsz ByteSizeTest.testRepeatedScalarscCs&|jjjddg|jd|jdS)Nrrr?r@rArrp)r=rBrr0r)rrrrtestRepeatedScalarsExtendmsz&ByteSizeTest.testRepeatedScalarsExtendcCsN|jjjd|jjjd|jd|j|jjjd|jd|jdS) Nrrr?r@rArrprA)r=rBr{r0rr)rrrrrrs z&ByteSizeTest.testRepeatedScalarsRemovecCs2|jjj}|jjj}d|_|jd|jdS) Nrpr@r?rArprrpr")r=rNr|rIr0r)rforeign_message_0foreign_message_1rrrrzs  z#ByteSizeTest.testRepeatedCompositescCs |jjj}|jjj}d|_|jd |jtj|jj}|jjd=|jd|j|jjj}d|_|jd|j|jjd=|jd|j|jjd=|jd|j|jdt||dd=t j dkr|jdt||d=t j dkr|jdt|dS)Nrr@r?rrLrwrArprrpr"rArrprArrprpr"rrrArrpr) r=rNr|rIr0rrgrhr$r ry)rrrrNZforeign_message_2rrrtestRepeatedCompositesDeletes,        z)ByteSizeTest.testRepeatedCompositesDeletecCs2|jjj}|jjj}d|_|jd|jdS) Nrpr@r?rrr"rr)r=rPr|rMr0r)rZgroup_0Zgroup_1rrrtestRepeatedGroupss  zByteSizeTest.testRepeatedGroupsc Csjtj}|jd|jtj}d|j|<|jd|jtjjjd}|j t d|j|<WdQRXdS)Nrrir@r7) rrr0r\rrr:rrrYrL)rr=r3r"rrrtestExtensionss  zByteSizeTest.testExtensionscCsd|j_|jd|jjd|j_|jd|jj|jjd|jd|jjtj}d|jj|<|jd|jjd|jj|<|jd|jj|jj ||jd|jjdS)Nr?r@rrAr7r) r=r7r0r\r_r rCrrr1)rr3rrr)testCacheInvalidationForNonrepeatedScalars    z6ByteSizeTest.testCacheInvalidationForNonrepeatedScalarcCs|jjjd|jd|jj|jjjd|jd|jjd|jjd<|jd|jj|jjd|jd|jjtj}|jj |}|jd|jd|jj|jd|jd |jjd|d<|jd |jj|jj ||jd|jjdS) Nr?rArrrprBrr@rrp) r=rBr{r0r\r_r rDrrr1)rr3Zrepeatedrrr&testCacheInvalidationForRepeatedScalars$      z3ByteSizeTest.testCacheInvalidationForRepeatedScalarcCs$d|jj_|jd|jjd|jj_|jd|jj|jjjd|jd|jj|jjd|jd|jjtjd kr|jj}|jjdd|_|jd|jjtj }|j j |}|jd|j jd|_ |jd |j jd|_ |jd|j j|j j ||jd|j jdS) Nr?rprrrKrArfrrwr)r=rfrKr0r\r_r ryr rErrrFr1)rchildr3rrr*testCacheInvalidationForNonrepeatedMessages,       z7ByteSizeTest.testCacheInvalidationForNonrepeatedMessagecCs|jjj}|jd|jj|jjj|jd|jjd|_|jd|jj|jjd|jd|jjtj}|j j |}|j}|jd|j j|j|jd|j jd|_ |jd|j j|jd |jd|j j|j j ||jd|j jdS) NrArr?r"rOrr@rrF) r=rOr|r0r\rKr_r rGrrrFr1)rZchild0r3Z child_listrrr'testCacheInvalidationForRepeatedMessage s(      z4ByteSizeTest.testCacheInvalidationForRepeatedMessagecCs|jd|jj|jjjd|jjjdd }|j||jj|jjjd|jjjdd}|j|||jj|jjd |j||jjdS)Nrrrr?r@rAg@g @r" packed_int32rArrn)r0rr\rr{ packed_doubler_)rZint_sizeZ double_sizerrrtestPackedRepeatedScalars s z&ByteSizeTest.testPackedRepeatedScalarscCsH|jd|jj|jjtj}|jddddg|jd|jjdS)Nrr?r@rArr)r0rr\rrZpacked_fixed32_extensionr)rr3rrrtestPackedExtensions0 s z!ByteSizeTest.testPackedExtensionsN)r&r'r(rrrrrrrrrrrrrrrrrrrrrrrrrrr s, '  rc@seZdZddZddZddZddZd d Zd d Zd dZ ddZ ddZ ddZ ddZ ddZddZddZddZdd Zd!d"Zd#d$Zd%d&Zd'd(Zd)d*Zd+d,Zd-d.Zd/d0Zd1d2Zd3d4Zd5d6Zd7d8Zd9S):SerializationTestcCsRtj}tj}|j}|j|jt||jt||j||j||dS)N)rr:rr0r\r$r)rrrrrrrtestSerializeEmtpyMessageF s z+SerializationTest.testSerializeEmtpyMessagecCs\tj}tj}tj||j}|j|jt||jt||j||j||dS)N) rr:rrNrr0r\r$r)rrrrrrrtestSerializeAllFieldsP s  z(SerializationTest.testSerializeAllFieldscCsHtj}tj}tj||j}|jt||j||j||dS)N)rrrrlrr0r$r)rrrrrrrtestSerializeAllExtensions[ s  z,SerializationTest.testSerializeAllExtensionscCsFtj}tj}d|j_|j}|jt||j||j||dS)N)rr:rerMrr0r$r)rrrrrrrtestSerializeWithOptionalGroupe s z0SerializationTest.testSerializeWithOptionalGroupcCsLtj}d|_d |_d |_d |_d|_d|_tjj|j }|j ||dS)Nr?r@rrArrprrllrllll) rr:r7rrrrrrOrr0)rrrrrrtestSerializeNegativeValueso s z-SerializationTest.testSerializeNegativeValuescCstjdkrdStj}tj||j}xtt|dD]}yntj}tj }|j |d|}|j ||y|j |d|}|j ||Wn t j k r|jdYnXWq<t j k r|jt j |j |d|Yq.InitalizeEmptyMessageWithExtraKeywordArgz9Protocol message TestEmptyMessage has no "unknown" field.)rr)rrrrrtestInitArgsUnknownFieldNameJ s z.SerializationTest.testInitArgsUnknownFieldNamecCstjdddd}|j|j|j|jd|j|jd|j|jd|j|jd |jd|j|jd|j|jd|jdS)Nr?)rMr{rKrMr{rKdummy2) rr<r^rur<r0rMr{rK)rr=rrrtestInitRequiredKwargsR sz(SerializationTest.testInitRequiredKwargscCstjtjddddd}|j|j|j|jd|j|jj|j|jjd|j|jjd|j|jjd|j|jjd |jtjdddd|j|jd|jj|jd|jj |jd|jj dS) Nr?)rMr{rK)r|r|rMr{rKr) rrr<r^rur<r|r0rMr{rK)rr=rrrtestInitRequiredForeignKwargs] sz/SerializationTest.testInitRequiredForeignKwargscCsZtjdddgd}|j|j|jd|jd|jd|jd|jd|jddS)Nr?r@rA)rBr)rr:r^rur0rB)rr=rrrtestInitRepeatedKwargsm s z(SerializationTest.testInitRepeatedKwargsN)r&r'r(rrrrrrrrrrrrrrrrrrrr r r rrrrrrrrrrrD s8    6) 3!     rc@seZdZddZddZdS) OptionsTestcCs<tj}|jd|jjjtj}|jd|jjjdS)NTF)r rr0rrrrr:)rr=rrrtestMessageOptionsw s zOptionsTest.testMessageOptionscCstj}d|_d|_x&|jD]\}}|jd|jjqWtj}|j j d|j j dx8|jD],\}}|jd|jj|jt j j|jqfWdS)Nr?g@FT)rr:r7r8rr0rrrrr{rrrr-r )rr=Zfield_descriptor_rrrtestPackedOptions s   zOptionsTest.testPackedOptionsN)r&r'r(rrrrrrru src@sreZdZejejdko"ejdkdddZddZ e j dd d Z e j dd d Z e j dd dZdS) ClassAPITestrr@z6C++ implementation requires a call to MakeDescriptor()c Cstjddddggggd}tjddddg|gggd}tjddddggggd}tjd d ddg||gggd}tj|}|jd|j|jd|j|jd|jjdS) NZleafzpackage.parent.child.leafr)rrrrrrzpackage.parent.childZsiblingzpackage.parent.siblingparentzpackage.parent)rrr MakeClassr__dict__r)rZ leaf_descZ child_descZ sibling_descZ parent_descZ message_classrrr!testMakeClassWithNestedDescriptor s(     z.ClassAPITest.testMakeClassWithNestedDescriptorcCs(d|d}tj}tj|||jS)a Get a serialized representation of a test FileDescriptorProto. Args: name: All calls to this must use a unique message name, to avoid collisions in the cpp descriptor pool. Returns: A string containing the serialized form of a test FileDescriptorProto. zmessage_type { name: "ai" field { name: "flat" number: 1 label: LABEL_REPEATED type: TYPE_UINT32 } field { name: "bar" number: 2 label: LABEL_OPTIONAL type: TYPE_MESSAGE type_name: "Bar" } nested_type { name: "Bar" field { name: "baz" number: 3 label: LABEL_OPTIONAL type: TYPE_MESSAGE type_name: "Baz" } nested_type { name: "Baz" enum_type { name: "deep_enum" value { name: "VALUE_A" number: 0 } } field { name: "deep" number: 4 label: LABEL_OPTIONAL type: TYPE_UINT32 } } }})rrr Merger)rr Zfile_descriptor_strfile_descriptorrrr_GetSerializedFileDescriptor s ) z)ClassAPITest._GetSerializedFileDescriptorz MakeDescriptor is not repeatablecstjdkrdStj}|j|jdtj|jdGfdddt j t j t j}|}d}tj|||j|jddd gdS) z7Test that the generated class can parse a flat message.rwNArcseZdZZdS)zSClassAPITest.testParsingFlatClassWithExplicitClassDeclaration..MessageClassN)r&r'r(rr)msg_descriptorrr MessageClass sr&zflat: 0 flat: 1 flat: 2 r?r@)r ryrrrr#rr.rrrrrrrr r!r0flat)rr"r&rmsg_strr)r%r0testParsingFlatClassWithExplicitClassDeclaration s    z=ClassAPITest.testParsingFlatClassWithExplicitClassDeclarationcCs`tj}|j|jdtj|jd}tj|}|}d}t j |||j |j dddgdS)z7Test that the generated class can parse a flat message.Brzflat: 0 flat: 1 flat: 2 r?r@N) rrrr#rr.rrrr r!r0r')rr"r% msg_classrr(rrrtestParsingFlatClass s   z!ClassAPITest.testParsingFlatClasscCs^tj}|j|jdtj|jd}tj|}|}d}t j |||j |j j jddS)z9Test that the generated class can parse a nested message.Crzbar { baz { deep: 4 }}rN)rrrr#rr.rrrr r!r0rrZdeep)rr"r%r+rr(rrrtestParsingNestedClass s   z#ClassAPITest.testParsingNestedClassN)r&r'r(rrr ryrr r#rZSkipReferenceLeakCheckerr)r,r.rrrrr s8r__main__))r)rgrmr5rrZ unittest2r ImportErrorZgoogle.protobufrrrrrrrr rr r r r rrrrZ BaseTestCaseobjectrr/rrrrrrrrr&mainrrrr#sv                 -V! :5