B ҳ ,.@sddlZddlZddlZddlZddlZddlmZddlmZddlm Z m Z m Z m Z m Z mZmZmZmZmZmZGdddeZGdddeZd d ZGd d d ejZed kredS)N) is_instance)mock) callDEFAULTpatchsentinel MagicMockMockNonCallableMockNonCallableMagicMock_Call _CallListcreate_autospecc@s(eZdZddZddZddZeZdS)ItercCstddddg|_dS)Nthisisaniter)rthing)selfrD/opt/alt/python37/lib64/python3.7/unittest/test/testmock/testmock.py__init__sz Iter.__init__cCs|S)Nr)rrrr__iter__sz Iter.__iter__cCs t|jS)N)nextr)rrrrrsz Iter.nextN)__name__ __module__ __qualname__rrr__next__rrrrrsrc@s2eZdZdddZed ddZed ddZdS) SomethingNcCsdS)Nr)rabcdrrrmethszSomething.methcCsdS)Nr)clsr r!r"r#rrrcmeth"szSomething.cmethcCsdS)Nr)r r!r"r#rrrsmeth&szSomething.smeth)N)N)N)rrrr$ classmethodr& staticmethodr'rrrrrs   rcCsdS)Nr)r rrr something+r*c@seZdZddZddZddZddZd d Zd d Zd dZ ddZ e de jkdddZddZddZddZddZddZdd Zd!d"Zd#d$Zd%d&Zd'd(Zd)d*Zd+d,Zd-d.Zd/d0Zd1d2Zd3d4Zd5d6Z d7d8Z!d9d:Z"d;d<Z#d=d>Z$d?d@Z%dAdBZ&dCdDZ'dEdFZ(dGdHZ)dIdJZ*dKdLZ+dMdNZ,dOdPZ-dQdRZ.dSdTZ/dUdVZ0dWdXZ1dYdZZ2d[d\Z3d]d^Z4d_d`Z5dadbZ6dcddZ7dedfZ8dgdhZ9didjZ:dkdlZ;dmdnZdsdtZ?dudvZ@dwdxZAdydzZBd{d|ZCd}d~ZDddZEddZFddZGddZHddZIddZJddZKddZLddZMddZNddZOddZPddZQddZRddZSddZTddZUddZVddZWddZXddZYddZZddZ[ddZ\ddZ]ddZ^ddZ_ddZ`ddZaddZbddZcddZdddZedd„ZfddĄZgddƄZhddȄZiddʄZjdd̄Zkdd΄ZlddЄZmdd҄ZnddԄZoddքZpdd؄ZqddڄZrdd܄ZsddބZtddZuddZvddZwddZxddZyddZzddZ{ddZ|ddZ}dS)MockTestcCs tddS)Nzfrom unittest.mock import *)exec)rrrrtest_all0szMockTest.test_allcCst}||jd||jdd|t|jtd||jdd||j gd||j gd| d|j d | |jd | |jd ||jid dS) Nz called not initialised correctlyrz$call_count not initialised correctlyz&return_value not initialised correctlyz#call_args not initialised correctlyz(call_args_list not initialised correctlyz&method_calls not initialised correctlyZ_itemsz/default mock should not have '_items' attributez parent not initialised correctlyz!methods not initialised correctlyz$children not initialised incorrectly)r assertFalsecalled assertEqual call_count assertTruer return_value call_argscall_args_list method_calls assertNotIn__dict__ assertIsNone _mock_parent _mock_methods_mock_children)rrrrrtest_constructor7s(      zMockTest.test_constructorcCstdd}||jddS)N)r4z(return value in constructor not honoured)r r:r4)rrrrr test_return_value_in_constructorSs z)MockTest.test_return_value_in_constructorcCstdd}|dt||dt|t|tdftdddfg}x|D]\}}|d|t|j|d|t||d|t|j|d |t||d |t||d |t|jjjqPWdS) Nfoo)namez'%s'rbarz%s.barz%s.foo()z %s.foo().bingz%s()z%s()()z%s()().foo.bar.baz().bing)r assertInrepridrBr@Zbingbaz)rrmocksrArrr test_reprYs  zMockTest.test_reprcCsGdddt}t|d}|dt|t|d}|dt|t|d}|dt|t|d}|dt|t|dd}|dt||d t|tdd }|d t|t}|d t|tdgd}|d t|dS) Nc@s eZdZdS)z'MockTest.test_repr_with_spec..XN)rrrrrrrXjsrI)specz spec='X' )spec_setz spec_set='X' r@)rJrAz name='foo' )rArJ)objectr rCrDr8)rrIrrrrtest_repr_with_specis$       zMockTest.test_repr_with_speccst}dd}||_|jt|dddd|jdddddddgfdd}||_||||gdddgdttjd }||jtjd d d }t|tjd }||tjdS)Nc_s tddS)NZkablooie) SystemError)argskwargsrrreffectsz)MockTest.test_side_effect..effect)fishcsS)N)popr)resultsrrrQszside effect not used correctly) side_effectz#side effect in constructor not usedcSstS)N)rrrrrrXsz.MockTest.test_side_effect..side_effect)rXr4) r rX assertRaisesrNassert_called_withr1r SideEffectRETURN)rrrQrXr)rWrtest_side_effects     zMockTest.test_side_effectcsdddgfdd}dd}t|}dddg|_||||gdddgddddgt|}||_||||gdddgd dS) NrRrSrTcsS)N)rVr)rWrrrQsz2MockTest.test_autospec_side_effect..effectcSsdS)Nrrrrrfsz-MockTest.test_autospec_side_effect..fz1side effect not used correctly in create_autospecz'callable side effect not used correctly)rrXr1)rrQr^rr)rWrtest_autospec_side_effects    z"MockTest.test_autospec_side_effectcCs,dd}t|}td|_|td|dS)NcSsdS)Nrrrrrr^sz7MockTest.test_autospec_side_effect_exception..fzBazinga!)r ValueErrorrXassertRaisesRegex)rr^rrrr#test_autospec_side_effect_exceptions z,MockTest.test_autospec_side_effect_exceptionjavaz This test only applies to JythoncCsdddl}t|jdd}y|ddddWn|jjk rDYn X|d|jdddddS) NrzBoom!)rXrRrSrT)rUzjava exception not raised)rcr ZlangZRuntimeExceptionfailrZ)rrcrrrrtest_java_exception_side_effects z(MockTest.test_java_exception_side_effectcCsRt}dg}td||d}|tjtjd|j}|tj|_|j}||| |j dd| |j |d| |j |d| |jd| |jd d | |jdd | |jgd | |jgd |jgf| |jg| |jtjd| |j|d| |jd| |jd|id| |j|d| |jjddS)Nr*child)rAparentrJ)r*zname incorrectly resetzparent incorrectly resetzmethods incorrectly resetzcalled not resetrzcall_count not resetzcall_args not resetzcall_args_list not resetz0method_calls not initialised correctly: %r != %rzside_effect incorrectly resetzreturn_value incorrectly resetzreturn value mock not resetzchildren reset incorrectlyzchildren incorrectly clearedzchild not reset)r rrZ SomethingElser*r[rXr4 reset_mockr1 _mock_namer;r<r/r0r2r5r6r7 mock_callsr=)rrgrJrr*r4rrrtest_reset_mocksD       zMockTest.test_reset_mockcCst}||_|dS)N)r r4rh)rrrrrtest_reset_mock_recursionsz"MockTest.test_reset_mock_recursioncCst}|dS)N)r mock_openrh)rr rrr(test_reset_mock_on_mock_open_issue_18622sz1MockTest.test_reset_mock_on_mock_open_issue_18622cCst}|t|jtd|}|||d||tj}||jd||j dd||j tjfifd||j tjfifgdtj |_|tjtj d}||tj d ||j d d ||j tjfd tj ifd||j tjfiftjfd tj ifgd dS)Nz%Default return_value should be a Mockz'different result from consecutive callszcalled not setrRzcall_count incoreectzcall_args not setz(call_args_list not initialised correctly)keyzincorrect return valuerSzcall_count incorrectrozcall_args_list not set)r r3rr4r1rhrArgr0r2r5r6Z ReturnValueZKeyArg)rrresultZret_valrrr test_calls4    zMockTest.test_callc Cst}||tj|tjd|tjtjd||jdtjffdtjiftjfdtjifg||jtjfdtjif||jdkdS)N)kwrrsza long sequence)r rrpZKwargr1r6r5r/)rrrrrtest_call_args_comparison$s   z"MockTest.test_call_args_comparisoncCst}|||k|||k||tk||tk||tjk||tjk|tj|k|tj|ktt}ttj}|||k|||k|||k|||kdS)N)rrr3r/ANYr)rZmmZcall1Zcall2rrrtest_calls_equal_with_any8s z"MockTest.test_calls_equal_with_anycCsdt}|||t|jd||t|j|dddddd|jdddddddS)NrRrSrTrUZnothing)r r!)r rZrYAssertionErrorrh)rrrrrtest_assert_called_withMsz MockTest.test_assert_called_withcCs t}|t|tjdS)N)rrZrru)rmrrrtest_assert_called_with_any\s z$MockTest.test_assert_called_with_anyc Csd dd}t|d}|dddd|ddd|jdddd|jt|jdddd|t}|jd d WdQRX||jjtdS) NcSsdS)Nr)r r!r"r#rrrr^csz9MockTest.test_assert_called_with_function_spec..f)rJrRrSrT)r!r")r r!r")e)N)r rZrYrwassertIsInstance exception __cause__ TypeError)rr^rcmrrr%test_assert_called_with_function_specbs     z.MockTest.test_assert_called_with_function_speccszfdd}ttjd}||ttjd}||ttjd}||ttjd}||ttjd}||dS)NcsF|dddd|ddd|jddddjt|jdddddS)NrRrSrT)r!r")r r!r")rZrYrw)r)rrr_checkts  z._check)rJ)r rr$r&r')rrrr)rr#test_assert_called_with_method_specss   z,MockTest.test_assert_called_with_method_speccst|tj|tjddddjdddddddd|tfdddS)Nr@rBrS)rFcsjddddS)NbobrBrS)rF)assert_called_once_withr)rrrr+z7MockTest.test_assert_called_once_with..)r rrYrwrh)rr)rrtest_assert_called_once_withsz%MockTest.test_assert_called_once_withc Csddd}t|d}|dddd|ddd|jdddd|jt|jdddd|t}|jd d WdQRX||jjt|d d d |t|jddd|t|jd d d dS)NcSsdS)Nr)r r!r"r#rrrr^sz>MockTest.test_assert_called_once_with_function_spec..f)rJrRrSrT)r!r")r r!r"r{)r|)N)r rrYrwr}r~rr)rr^rrrrr*test_assert_called_once_with_function_specs         z3MockTest.test_assert_called_once_with_function_speccCsbt}|j}|t|td||j|dt}d|j_||dd||jjddS)Nzattribute isn't a mockz+different attributes returned for same namerTzmethod returned wrong valuez!method didn't record being called)r r*r3rr1r4r0)rrr*rrr#test_attribute_access_returns_mockss  z,MockTest.test_attribute_access_returns_mockscCs0t}|j}||jdd||j|ddS)Nr*z attribute name not set correctlyz"attribute parent not set correctly)r r*r1rir;)rrr*rrr(test_attributes_have_name_and_parent_sets   z1MockTest.test_attributes_have_name_and_parent_setcCstt}|jddd|jjdtjd||jjdddtjifgd||jdd d difd ddtjifgddS) NrT)rUr)caker*)rrz#method calls not recorded correctly)rTrUzsomething_else.something)r r*something_elserZCaker1r7)rrrrrtest_method_calls_recordeds  z#MockTest.test_method_calls_recordedcCst}|||jdg||jddifgt}|d||jdg||jddifgt}|jdd||jdd difg||jddd difgt}|jdd d ||jddd d ifgdS) N)r*r*r different)r*)r)rrR)xrZmore)somer)r r*r1r7)rrrrr test_method_calls_compare_easilys"  z)MockTest.test_method_calls_compare_easilyc CsLxFdgdfD]8}x2dD]*}tf||i}|j|tdt|dqWq WdS)Nr*)r*)rJrKz-Mock object has no attribute 'something_else'r)r r*raAttributeErrorgetattr)rrJargrrrrtest_only_allowed_methods_exists z(MockTest.test_only_allowed_methods_existcs>Gdddt}fdd}|t|d|t|ddS)Nc@seZdZdZdZddZdS)z*MockTest.test_from_spec..SomethingrTNcSsdS)Nr)rrrry sz,MockTest.test_from_spec..Something.y)rrrr __something__rrrrrrsrcs:|j|j|jtdt|dtdt|ddS)Nz Mock object has no attribute 'z'zz)Mock object has no attribute '__foobar__'Z __foobar__)rrrrarr)r)rrrtest_attributes s z0MockTest.test_from_spec..test_attributes)rJ)rLr )rrrr)rrtest_from_specs zMockTest.test_from_speccCsJt}t|d}|||||dddd|jdddddS)N)wrapsrRrSrT)rU)r r1rhrZ)rrealrrrrtest_wraps_calls s  zMockTest.test_wraps_callscs8Gdddt}|}t|d|tfdddS)Nc@s eZdZdS)zFMockTest.test_wraps_prevents_automatic_creation_of_mocks..RealN)rrrrrrrReal-sr)rcsS)N)Znew_attrr)rrrr3r+zJMockTest.test_wraps_prevents_automatic_creation_of_mocks..)rLr rYr)rrrr)rr/test_wraps_prevents_automatic_creation_of_mocks,s z8MockTest.test_wraps_prevents_automatic_creation_of_mockscCs4t}t|d}d|_||d||jdS)N)rrT)r r4r1r/r0)rrrrrr,test_wraps_call_with_nondefault_return_value6s  z5MockTest.test_wraps_call_with_nondefault_return_valuecsGdddt}|}t|d|||tfdd|j|jjjdddd }|jjjdddd |||jdS) Nc@seZdZeZdS)z,MockTest.test_wraps_attributes..RealN)rrrr attributerrrrrAsr)rcsjS)N)rUr)rrrrHr+z0MockTest.test_wraps_attributes..rRrSrT)rU) rLr r1rrYrassertNotEqualZfrogrZ)rrrrqr)rrtest_wraps_attributes@s zMockTest.test_wraps_attributescCsdGdddt}|}t|d}tjtg|j_||tj||tj| t |jdS)Nc@seZdZddZdS)z[MockTest.test_customize_wrapped_object_with_side_effect_iterable_with_default..RealcSstjS)N)rORIGINAL_VALUE)rrrrmethodRszbMockTest.test_customize_wrapped_object_with_side_effect_iterable_with_default..Real.methodN)rrrrrrrrrQsr)r) rLr rVALUE1rrrXr1rrY StopIteration)rrrrrrrDtest_customize_wrapped_object_with_side_effect_iterable_with_defaultPs zMMockTest.test_customize_wrapped_object_with_side_effect_iterable_with_defaultcCsfGdddt}|}t|d}tjtjg|j_||tj||tj|t |jdS)Nc@seZdZddZdS)zNMockTest.test_customize_wrapped_object_with_side_effect_iterable..RealcSs tdS)N)NotImplementedError)rrrrr`szUMockTest.test_customize_wrapped_object_with_side_effect_iterable..Real.methodN)rrrrrrrrr_sr)r) rLr rrVALUE2rrXr1rYr)rrrrrrr7test_customize_wrapped_object_with_side_effect_iterable^s z@MockTest.test_customize_wrapped_object_with_side_effect_iterablecCs:Gdddt}|}t|d}t|j_|t|jdS)Nc@seZdZddZdS)zOMockTest.test_customize_wrapped_object_with_side_effect_exception..RealcSs tdS)N)r)rrrrrnszVMockTest.test_customize_wrapped_object_with_side_effect_exception..Real.methodN)rrrrrrrrrmsr)r)rLr RuntimeErrorrrXrY)rrrrrrr8test_customize_wrapped_object_with_side_effect_exceptionls  zAMockTest.test_customize_wrapped_object_with_side_effect_exceptioncCsFGdddt}dd}|}t|d}||j_||tjdS)Nc@seZdZddZdS)zNMockTest.test_customize_wrapped_object_with_side_effect_function..RealcSs tdS)N)r)rrrrrzszUMockTest.test_customize_wrapped_object_with_side_effect_function..Real.methodN)rrrrrrrrrysrcSstjS)N)rVALUErrrrrX}szUMockTest.test_customize_wrapped_object_with_side_effect_function..side_effect)r)rLr rrXr1rr)rrrXrrrrr7test_customize_wrapped_object_with_side_effect_functionxs  z@MockTest.test_customize_wrapped_object_with_side_effect_functioncCs@Gdddt}|}t|d}tj|j_||tjdS)Nc@seZdZddZdS)zFMockTest.test_customize_wrapped_object_with_return_value..RealcSs tdS)N)r)rrrrrszMMockTest.test_customize_wrapped_object_with_return_value..Real.methodN)rrrrrrrrrsr)r)rLr rrrr4r1)rrrrrrr/test_customize_wrapped_object_with_return_values   z8MockTest.test_customize_wrapped_object_with_return_valuecCspGdddt}|}t|d}tjtjg|j_tj|j_| |tj| |tj| t |jdS)Nc@seZdZddZdS)zVMockTest.test_customize_wrapped_object_with_return_value_and_side_effect..RealcSs tdS)N)r)rrrrrsz]MockTest.test_customize_wrapped_object_with_return_value_and_side_effect..Real.methodN)rrrrrrrrrsr)r) rLr rrrrrXZ WRONG_VALUEr4r1rYr)rrrrrrr?test_customize_wrapped_object_with_return_value_and_side_effects  zHMockTest.test_customize_wrapped_object_with_return_value_and_side_effectcCsLGdddt}|}t|d}dd|j_tj|j_||tjdS)Nc@seZdZddZdS)zWMockTest.test_customize_wrapped_object_with_return_value_and_side_effect2..RealcSs tdS)N)r)rrrrrsz^MockTest.test_customize_wrapped_object_with_return_value_and_side_effect2..Real.methodN)rrrrrrrrrsr)rcSstS)N)rrrrrrr+z[MockTest.test_customize_wrapped_object_with_return_value_and_side_effect2..)rLr rrXrrr4r1)rrrrrrr@test_customize_wrapped_object_with_return_value_and_side_effect2s    zIMockTest.test_customize_wrapped_object_with_return_value_and_side_effect2cCsnGdddt}|}t|d}tjtg|j_tj|j_| |tj| |tj| t |jdS)Nc@seZdZddZdS)z^MockTest.test_customize_wrapped_object_with_return_value_and_side_effect_default..RealcSs tdS)N)r)rrrrrszeMockTest.test_customize_wrapped_object_with_return_value_and_side_effect_default..Real.methodN)rrrrrrrrrsr)r) rLr rrrrrXr\r4r1rYr)rrrrrrrGtest_customize_wrapped_object_with_return_value_and_side_effect_defaults  zPMockTest.test_customize_wrapped_object_with_return_value_and_side_effect_defaultcCs4ttd}|t|ttdd}|t|dS)N)rXr@)r rrY)rrrrrtest_exceptional_side_effects  z%MockTest.test_exceptional_side_effectcCs4ttd}|t|ttdd}|t|dS)N)rXr@)r KeyboardInterruptrY)rrrrr test_baseexceptional_side_effects  z)MockTest.test_baseexceptional_side_effectcCst}|td|jdS)Nz Not called)r rarwrZ)rrrrrtest_assert_called_with_messagesz(MockTest.test_assert_called_with_messagecCstdd}|td|jdS)NZgeoffrey)rAz'Expected 'geoffrey' to be called once\.)r rarwr)rrrrr$test_assert_called_once_with_messages z-MockTest.test_assert_called_once_with_messagecs2t|tfddd_|jddS)NcsjS)N)rr)rrrrr+z'MockTest.test__name__..r@)r rYrrr1)rr)rr test__name__szMockTest.test__name__cCsJGdddt}t|dgd}|d|jd|tt|ddS)Nc@s eZdZdS)z-MockTest.test_spec_list_subclass..SubN)rrrrrrrSubsrr@)rJrT)listr appendrZrYrr)rrrrrrtest_spec_list_subclasss   z MockTest.test_spec_list_subclasscCsGdddt}t|d}|||t|d}|||||j||tjjdt|d}|||t|d}|||dS)Nc@s eZdZdS)z#MockTest.test_spec_class..XN)rrrrrrrrIsrI)rJr )rK)rLr r}assertIs __class__r1r)rrIrrrrtest_spec_classs       zMockTest.test_spec_classcsLGdddt}t|dd_t|dfdd}d_|t|dS)Nc@seZdZdZdS)z8MockTest.test_setting_attribute_with_spec_set..XrTN)rrrrrrrrrIsrI)rJr@)rKcs d_dS)Nr@)rr)rrrset_attr sz?MockTest.test_setting_attribute_with_spec_set..set_attr)rLr rrrYr)rrIrr)rr$test_setting_attribute_with_spec_sets   z-MockTest.test_setting_attribute_with_spec_setcCs4t}|tj|ttdttdS)NgeA)sysgetrecursionlimit addCleanupsetrecursionlimitintcopyr )rZcurrentrrr test_copyszMockTest.test_copycs\Gdddt}||d|jdfdd}|t|fdd}|t|dS)Nc@s&eZdZddZddZeeeZdS)z8MockTest.test_subclass_with_properties..SubClasscSsdS)NrTr)rrrr_getsz=MockTest.test_subclass_with_properties..SubClass._getcSs tddS)Nz strange error) NameError)rvaluerrr_set sz=MockTest.test_subclass_with_properties..SubClass._setN)rrrrrpropertysome_attributerrrrSubClasssr)rKrTcs d_dS)NrT)rr)srrtest'sz4MockTest.test_subclass_with_properties..testcs d_dS)NrB)r@r)rrrr+s)r r1rrYrr)rrrr)rrtest_subclass_with_propertiess    z&MockTest.test_subclass_with_propertiescCs>t}dd}|t|_|d|d|t|dddS)NcSs ||S)N)Z _mock_call)rr rrr__call__2sz,MockTest.test_setting_call..__call__onetwo)r typerrZrYr)rrrrrrtest_setting_call0s   zMockTest.test_setting_callcCst}tt|}tddttD}|t|||j|jf|dt||dt|d|_|_|dt||dt|dd|_ |d t|dS) NcSsg|]}|ds|qS)_) startswith).0ryrrr ?sz%MockTest.test_dir..r r!r"r#cSstgS)N)r)rrrrrOr+z#MockTest.test_dir..r) r setdirr1r r!rCr"r#r)rrattrs type_attrsrrrtest_dir<s    zMockTest.test_dircCsXttjd}tttj}tt|}|t||d|_|t|dddS)N)rJrTversionrR)r unittestTestCaserrr1rcount)rrZtestcase_attrsrrrrtest_dir_from_specSs   zMockTest.test_dir_from_speccCsVttdd}|z0ttt}ttt}|t||Wd|XdS)NZ FILTER_DIRF) rrLrstartrrr r1stop)rZpatcherrrrrrtest_filter_dir`s zMockTest.test_filter_dircCs6t}d|j_|dt||`|dt|dS)NrRrf)r rfr4rCrr8)rrrrr,test_dir_does_not_include_deleted_attributesms z5MockTest.test_dir_does_not_include_deleted_attributescCstdd}||jdtdd}||jdtdtd}tf|}|t|||jd||jtt}|jf||t|||jd||jtdS)NrB)r@!)rXzfoo.bar.return_valuer@) r r1r@rKeyErrorrYrBr}Zconfigure_mock)rrrPrrrtest_configure_mockvs       zMockTest.test_configure_mockcOs\y|||Wn$td}|||YnX|d|ft|}|||dS)NrRzException %r not raised)rexc_infor}rdstrr1)rr~messagefuncrOrPinstancemsgrrrassertRaisesWithMsgs zMockTest.assertRaisesWithMsgc Cs(t}d}d}|jt||f|jddddd|jddddd|jj|jjg}x8|D]0}d }d }d }|jt|||f|dddddqVWx2|D]*}d }d }d }|jt|||f|ddqWx4|D],}d }d }d }|t|||f|dddqWx.|D]&}d }d}d }|t|||f|qWdS)Nzmock(1, '2', 3, bar='foo')zExpected call: %s Not calledrR2rTr@)rB)r@zfoo(1, '2', 3, foo='foo')zfoo(1, '2', 3, bar='foo')z!Expected call: %s Actual call: %szfoo(bar='foo')z foo(1, 2, 3)rSzfoo())r rrwrZr@r)rrexpectedrZ assertersr$Zactualrrr'test_assert_called_with_failure_messagesF        z0MockTest.test_assert_called_with_failure_messagec Cst}||jgkdt}|ddifg}||j|||t||j|||jjddifgt}|jdddddd ddifd d tddd fg}||j|||jjjdd tddd fg||jjd d tddd fgt}|j ddifd difddifg}||j|||jtj x:ttddfD]$}tf|}t |jddifg}||j|tf|}| ddifddifg}||j||| jtgtf|}|ddd||jtddd ||jtdd ||jtd tf|}|dddj jd||jtdddj jd ||jtddj jd ||jtdj jd tf|}t |j ddif}||jd|||jtj  ||j jt  ||j j jt qtWdS)NTrrRrSrTrr)r r!z().foo)rRrSrTr@z ().foo.barz().foo.bar().bazrB)rAz foo.__int__r za()z().foo.bar().baz().__int__)rrrjr1r@rrdictr4rBrF call_listrr r!r"__int__)rrrrPZ last_callrrrtest_mock_callsst                  zMockTest.test_mock_callscCsFt}|}|||jttg||jtgdS)N)r Zwibbler1rjr)rryrqrrrtest_child_mock_call_equals z#MockTest.test_child_mock_call_equalcCsFt}|||jdt||jdtdS)NrRr)r r@r*rrjrrr1)rryrrrtest_mock_call_not_equal_leaf(s z&MockTest.test_mock_call_not_equal_leafcCsFt}|||jdt||jdtdS)NrRr)r r@rBrrjrrF)rryrrr!test_mock_call_not_equal_non_leaf/s z*MockTest.test_mock_call_not_equal_non_leafcCs8t}|jdd||jdtjdddS)NrR)rrS)r r@rBr1rjr)rryrrr2test_mock_call_not_equal_non_leaf_params_different6sz;MockTest.test_mock_call_not_equal_non_leaf_params_differentcCs,t}|j||jdtjdS)Nr)r r@rBrrjrrF)rryrrr&test_mock_call_not_equal_non_leaf_attr=s z/MockTest.test_mock_call_not_equal_non_leaf_attrcCs.t}|j||jdtdS)Nr)r r@rBrrjr)rryrrr2test_mock_call_not_equal_non_leaf_call_versus_attrCs z;MockTest.test_mock_call_not_equal_non_leaf_call_versus_attrcCs^t}|j|t|jdd|t|jdd|t|jdddS)Nrz call.foo()rRzcall.foo().bar()rSzcall.foo().bar().baz.bob())r r@rBrFrr1rDrj)rryrrrtest_mock_call_reprIs zMockTest.test_mock_call_reprcCshGdddt}|}||j||||Gdddt}|}||j||||dS)Nc@s eZdZdS)z+MockTest.test_subclassing..SubclassN)rrrrrrrSubclassRsr c@seZdZddZdS)z+MockTest.test_subclassing..Subclassc[s tf|S)N)r )rrPrrr_get_child_mockZsz;MockTest.test_subclassing..Subclass._get_child_mockN)rrrr rrrrr Ys)r r}r@assertNotIsInstance)rr rrrrtest_subclassingQszMockTest.test_subclassingcsttttg}fdd}x|D]z}||t|rd||dd|dd|||||jjddd|dd|||q&WdS)NcsDd}x:|D]2}t||}|t|t|gq WdS)N)r6r7rj)rr}r rr1)rnamesrAattr)rrr assert_attrsjs     z-MockTest.test_arg_lists..assert_attrsrRrSrT)r ) r rr r callablerhr@rBrF)rrGrrr)rrtest_arg_listsbs$    zMockTest.test_arg_listscCst}|ddd|ddd|t|jd|j\}}||d||tdddtddfdtddfg}xPt||jD]@\}}|t|d||d |d ||d|dqWdS) NrRrT)r rSr)r!)rS)rRr)r r1lenr5rzipr6)rrrOrPZ expected_listrr5rrrtest_call_args_two_tuples    z!MockTest.test_call_args_two_tuplecCsttdddgd}||||gdddg|t|tdddgd}||||gdddg|t|tdd}||||gd d d g|t|Gd d d t}t|d}|||ttd}|||||gddddg|t|dS)NrRrSrT)rXr r!r"Zghighic@s eZdZdS)z/MockTest.test_side_effect_iterator..FooN)rrrrrrrFoosrrrrr) r rr1rYrrrLr}r)rrrrrrtest_side_effect_iterators       z"MockTest.test_side_effect_iteratorcCs`xZttfD]N}tdtdf}||d}|t|||d|t|||dq WdS)NrTr)rX)r rr`rrYr1)rKlassiterableryrrr$test_side_effect_iterator_exceptionss    z-MockTest.test_side_effect_iterator_exceptionscCst}tdddg|_||||gdddg|t||j}||ttgdddg|_||||gdddg|t||j}||ttgt}||_|||||gddd d g|t|| |j|dS) NrRrSrTr r!r"rrrr) r rrXr1rYrr}rrr)rrrXZ this_iterrrr!test_side_effect_setting_iterators"    z*MockTest.test_side_effect_setting_iteratorcCs4tdd}tdtg|_|||gddgdS)NrS)r4rR)r rrrXr1)rrrrr!test_side_effect_iterator_defaults z*MockTest.test_side_effect_iterator_defaultc Cst}|dd|dd|dd|dd|ddtddddifd d ddifd d ddifd d ifd d ddifg}x|D]}|j|gddq~WxWq(WdS)NrRrSr rT))rTrr)r!r)rRr!)r )r@r)rRrSrT)rr@rBextendZspamrFrZbamrrUr rrrangerr!rYrwrreversed) rZkalls1Zkalls2rGrr"rstepZtheserrrtest_assert_has_callss< "        zMockTest.test_assert_has_callsc CsRGddd}t|}xV||gD]H}|jddddd|tjdddddg|tddddgq"W|xz||gD]l}|t|jtg|ddd|tdtdddg|jtdtddgqW|tdt dt dtddddg}x |D]}|t|j|gq2WdS)Nc@s,eZdZddZdddZGdddZdS) z=MockTest.test_assert_has_calls_nested_spec..SomethingcSsdS)Nr)rrrrr)r+zFMockTest.test_assert_has_calls_nested_spec..Something.__init__NcSsdS)Nr)rr r!r"r#rrrr$*r+zBMockTest.test_assert_has_calls_nested_spec..Something.methc@seZdZddZddZdS)zAMockTest.test_assert_has_calls_nested_spec..Something.FoocSsdS)Nr)rr rrrr.r+zJMockTest.test_assert_has_calls_nested_spec..Something.Foo.__init__cSsdS)Nr)rr r!rrrmeth1/r+zGMockTest.test_assert_has_calls_nested_spec..Something.Foo.meth1N)rrrrr*rrrrr,sr)N)rrrrr$rrrrrr's rrRrSrT)r#r) rr$r!rrhrYrwrr*Z non_existent)rrZ mock_classryZ invalid_callsr#rrr!test_assert_has_calls_nested_spec&s* ""  z*MockTest.test_assert_has_calls_nested_speccCsJt}||t}| |dS)N) rr@rBrFrrthreerrr!)rrycallsrrr)test_assert_has_calls_nested_without_specNs z2MockTest.test_assert_has_calls_nested_without_specc Csddd}t|d}|dddd|dd d d d |d dddddifdddd ifdifg}|||j|dd||dd|j|dddd||dd|j|ddddtt|}|t||WdQRX|j|dd|t||ddWdQRX|j|dddd|t||ddWdQRX|j|dddddS)NcSsdS)Nr)r r!r"r#rrrr^Wsz.f)rJrRrSrT)r!r"rrrr )r"r# )r"r)rRrSrT)rrrr#)r/r0r1T)rr)N)r r!rr'rYrw)rr^rr-rrr(test_assert_has_calls_with_function_specVs0         z1MockTest.test_assert_has_calls_with_function_specc Csd dd}t|d}|d|tdtd}|tgWdQRX||j j |tdtd}|ttddgWdQRX| |j j t dS) NcSsdS)Nr)rrrrr^wr+zAMockTest.test_assert_has_calls_not_matching_spec_error..f)rJrRz^{}$z5Calls not found. Expected: [call()] Actual: [call(1)]zError processing expected calls. Errors: [None, TypeError('too many positional arguments')] Expected: [call(), call(1, 2)] Actual: [call(1)]rS)N) r rarwformatreescaper!rr:r~rr}r)rr^rrrrr-test_assert_has_calls_not_matching_spec_errorvs$    z6MockTest.test_assert_has_calls_not_matching_spec_errorcCst}|dd|dd|ddd|dd|jdd|jddd|t|j|t|jdd|jt|jdddS)NrRrSrT)r r)r!r)r assert_any_callrYrw)rrrrrtest_assert_any_calls$     zMockTest.test_assert_any_callc Csddd}t|d}|dddd|dd d d d |ddd|jdddd |dd d d |jdd d d d|jt|jdddd|t}|jddWdQRX||jjtdS)NcSsdS)Nr)r r!r"r#rrrr^sz;MockTest.test_assert_any_call_with_function_spec..f)rJrRrSrT)r!r"rrrr )r"r#)r r!r")r r!r"r#r{)r|)N)r r7rYrwr}r~rr)rr^rrrrr'test_assert_any_call_with_function_specs     z0MockTest.test_assert_any_call_with_function_speccCsjdd}t}||_t|t|jg}x>|D]6}|dd|dd||jtddtddgq,WdS)NcSsdS)Nr)r r!rrrr^sz3MockTest.test_mock_calls_create_autospec..frRrSrTr)rr^rr1rjr)rr^objZfuncsrrrrtest_mock_calls_create_autospecs    z(MockTest.test_mock_calls_create_autospeccCs$tjtdd}|dt|dS)NZ sweet_func)rA)rrrLrCrD)rryrrrtest_create_autospec_with_namesz'MockTest.test_create_autospec_with_namec CsdGddd}xPdD]H}|j|d2tt||}|||t|dWdQRXqWdS)Nc@s$eZdZeddZeddZdS)zMMockTest.test_create_autospec_classmethod_and_staticmethod..TestClasscSsdS)Nr)r%rrr class_methodszZMockTest.test_create_autospec_classmethod_and_staticmethod..TestClass.class_methodcSsdS)Nrrrrr static_methodsz[MockTest.test_create_autospec_classmethod_and_staticmethod..TestClass.static_methodN)rrrr(r=r)r>rrrr TestClasss r?)r=r>)rZ extra_arg)ZsubTestrrrrrYr)rr?rZ mock_methodrrr1test_create_autospec_classmethod_and_staticmethods z:MockTest.test_create_autospec_classmethod_and_staticmethodc Cs`t}|t|WdQRX|t|WdQRXtdd}||dS)NT)Zunsafe)r rYrZassert_foo_callZassret_foo_call)rryrrrtest_mock_unsafes   zMockTest.test_mock_unsafec Cs<t}|j||t|jWdQRXdS)N)r helloZassert_not_calledrYrw)rryrrrtest_assert_not_calleds   zMockTest.test_assert_not_calledc CsNt}|t|jWdQRX||j||jdS)N)r rYrwrBZ assert_called)rryrrrtest_assert_calleds  zMockTest.test_assert_calledc Csdt}|t|jWdQRX||j||t|jWdQRXdS)N)r rYrwrBZassert_called_once)rryrrrtest_assert_called_onces   z MockTest.test_assert_called_oncecCs0t}|jdddd}|t|jj|dS)NrBZhero)rAZdaddyz call(daddy='hero', name='hello'))r rBr1rDr5)rrytextrrrtest_sorted_call_signaturesz#MockTest.test_sorted_call_signaturecCsVt}tdd}t}||dd||jd|||jd|dS)NrBrrR)rrindexr r1r7)rr"rryrrrtest_override_tuple_methodss  z$MockTest.test_override_tuple_methodscCs>tdddgd}|jddd||jt||jddS)Nr/rSrT)r4rXT)r rhr}r4r1rX)rryrrrtest_reset_return_sideeffect sz%MockTest.test_reset_return_sideeffectcCs<tdddgd}|jdd||jt||jddS)Nr/rSrT)r4rXT)r4)r rhr}r4rrX)rryrrrtest_reset_return&s zMockTest.test_reset_returncCs<tdddgd}|jdd||jd||jddS)Nr/rSrT)r4rXT)rX)r rhr1r4rX)rryrrrtest_reset_sideeffect,s zMockTest.test_reset_sideeffectc sGdddt}Gdddt}Gdddtttttg}x._OnerRN)rrrrrrrr_One3srNc@seZdZdZdS)z)MockTest.test_mock_add_spec.._TworSN)rrrrrrrr_Two5srOc@seZdZdZZZdS)z-MockTest.test_mock_add_spec..AnythingZfourN)rrrrrr,rrrrAnything7srPcs |dS)N)rJr)K)rPrrr>r+z-MockTest.test_mock_add_spec..cs |dS)N)rKr)rQ)rPrrr?r+T)rKrrr,rK)rLr rr r rrrrrr, mock_add_specrYrrsetattr) rrNrOZklassesrrPrZOneZTwor)rPrtest_mock_add_spec2s>      zMockTest.test_mock_add_speccsxzttfD]n}|tt|tt|ddj_t| td|tfddq WdS)Nr@rcsdS)Nr@rr)rrrrur+z;MockTest.test_mock_add_spec_magic_methods..) rr rrRrLrYrrr4r1)rrr)rr test_mock_add_spec_magic_methodsgs  z)MockTest.test_mock_add_spec_magic_methodscCsxttttfD]}|}t|_|||jtg||jtg|}tdd|_ | ||jg||jg|}t|_ | ||jg||jgqWdS)NrA)rA) r r rr r@r1r7rrjrBrF)rrrrrrtest_adding_child_mockxs   zMockTest.test_adding_child_mockcCsDx>ttfD]2}|}t|_|||jttgq WdS)N)r rr4r1rjr)rrrrrrtest_adding_return_value_mocks z&MockTest.test_adding_return_value_mockcCsGdddt}t}t|d}t|d}|}||j|}||j||d||d||| |j t t gdS)Nc@seZdZdZdZdS)z'MockTest.test_manager_mock..FoorrN)rrrrrrrrrrsrrr) rLr rrrr attach_mockrrr1rjr)rrmanagerZp1Zp2Zmock_oneZmock_tworrrtest_manager_mocks      zMockTest.test_manager_mockcCslxfttfD]Z}|}tdd|_tdd|_t|t|||jttg||j gq WdS)NrT)r4g@) r rr __float__rfloatr1rjrr7)rrryrrrtest_magic_methods_mock_callss  z&MockTest.test_magic_methods_mock_callscCs<tjdd}|d}|}|d}|}|||dS)Ndata) read_dataza-namez another-name)rrmreadr1)r mocked_openf1Zf1_dataf2Zf2_datarrr test_mock_open_reuse_issue_21750s  z)MockTest.test_mock_open_reuse_issue_21750cCsVtjdd}|d}dd|D}||dd||dd |t|gdS) NzRemarkable Norwegian Blue)r_za-namecSsg|]}|qSrr)rlinerrrrsz=MockTest.test_mock_open_dunder_iter_issue..rz Remarkable rRzNorwegian Blue)rrmr1r)rrarblinesrrr test_mock_open_dunder_iter_issues  z)MockTest.test_mock_open_dunder_iter_issuec Cstjdd}|d}t|}|}dd|D}||d||d||dd |t|g|tt|WdQRXdS) Nz1st line 2nd line 3rd line)r_za-namecSsg|]}|qSrr)rrerrrrsz6MockTest.test_mock_open_using_next..z 1st line z 2nd line rz3rd line)rrmrrr1rrYr)rrarbZline1Zline2rfrrrtest_mock_open_using_nexts    z"MockTest.test_mock_open_using_nextc CsXttjdd}td|0|j}|j}td|_dd}|t|WdQRXdS)NZJLV)rAztempfile.NamedTemporaryFilez Test 2 ErrorcSstddS)NZasd)tempfileZNamedTemporaryFilewriterrrrattemptsz.MockTest.test_mock_open_write..attempt) rrmrrr4rjOSErrorrXrY)rZmock_namedtempZmock_filehandleZ mock_writerkrrrtest_mock_open_writes zMockTest.test_mock_open_writecCsJtjdd}dd|jj_|}|}|d||d|dS)Nzfoo barn)r_cWsdS)Nabcr)rOrrrrr+z8MockTest.test_mock_open_alter_readline..rn)rrmr4readlinerXr1)rZmopenfirstsecondrrrtest_mock_open_alter_readlines     z&MockTest.test_mock_open_alter_readlinecCstjdd}|d}||d||d||d||d||g||g|dS)Nr@)r_rBr)rrmr`r1ro readlines)rZ_openrrrrtest_mock_open_after_eofs z!MockTest.test_mock_open_after_eofcCsxttfD]}|}t|}||_||||t|||||||t|||}|j|_| dt|j| dt|j|| dt|j| dt|j|}t|}||_||j_|t|||t||q WdS)Nz name='mock.a') r rrDr4rr1rhr r!rC)rrryZ original_reprrrrtest_mock_parentss,zMockTest.test_mock_parentscCsttttf}x|D]}x|D]z}|}|dd}||d||j||dt||j d| |j t j dg| |j t j dgqWqWdS)Nr@)rArBzname='mock.bar'rR)r rr r rXrrBrCrDrFr1rjrr7)rclassesrKlass2rym2rrrtest_attach_mocks      zMockTest.test_attach_mockcCsttttf}xxttfD]l}xf|D]^}|}|dd}||d||||dt||| |j t  q WqWdS)Nr@)rAr4z name='mock()') r rr r rXrrCrDr@r1rjrr)rrvrrwryrxrrrtest_attach_mock_return_value&s    z&MockTest.test_attach_mock_return_valuec Cst}tjtddd}||jd||d|dtd|dt dt dt dg}t dt dt dg}||j |||jj ||tj |||j || d t |jj||jd WdQRXdS) Nz .somethingT)autospecr*rfrRrSrTz mock.child) r rrrr1Z_extract_mock_namerXrfr*rrjrCrD)rrgZ mock_funcZ parent_callsZ child_callsrrrtest_attach_mock_patch_autospec6s  z(MockTest.test_attach_mock_patch_autospecc Cstjtddd}t}||dt}|jddddd |tj tj ddddd g|jttj ddddd g|ttj ddddd gWdQRXtjtd ddR}t}||d t d|t dgt tdg|tdgWdQRXtjtd dd}t}||d t}|jddddd |t t jddddd g|jtddddd g|ttjddddd gWdQRXdS)Nz.Something.methT)r{ attach_methrRrSrTr)r#z .something attach_funcz .Something attach_obj)rrrr rXrr$r!rr}rur*r~r)rZmockedrYr:rrr)test_attach_mock_patch_autospec_signatureJs0 &   z2MockTest.test_attach_mock_patch_autospec_signaturecCsjxdttttfD]L}|t|d|`|t|d|`|t|d| t t |dqWdS)Nryr^) r rr r r3hasattrryr/r^rYrr)rrrrrtest_attribute_deletiongs z MockTest.test_attribute_deletioncCsxttttfD]t}d|_|t|d||jd|`|t|dd|_|t|d||jd|`|t|dqWdS)NrTr@r) r rr r r@r3rr1r/)rrrrr7test_mock_does_not_raise_on_repeated_attribute_deletionts z@MockTest.test_mock_does_not_raise_on_repeated_attribute_deletionc Cs@x:ttttfD]"}|`|t |`WdQRXqWdS)N)r rr r r@rYr)rrrrr4test_mock_raises_when_deleting_nonexistent_attributes   z=MockTest.test_mock_raises_when_deleting_nonexistent_attributecCs,t}d|_|`||t|ddS)NTrf)r rfrhr/r)rrrrr/test_reset_mock_does_not_raise_on_attr_deletions z8MockTest.test_reset_mock_does_not_raise_on_attr_deletioncCs>x8ttfD](}||tt|_||t|jqWdS)N)r rr rrr}r@)rrrrrtest_class_assignables   zMockTest.test_class_assignablecCs8|tj|ttjt|ttjtdS)N)assertIsNotNonerrAr1rr )rrrrtest_name_attribute_of_calls z$MockTest.test_name_attribute_of_callcCs8|tj|ttjt|ttjtdS)N)rrrgr1rr )rrrrtest_parent_attribute_of_calls z&MockTest.test_parent_attribute_of_callcCsddd}t}t||_|dd|t|jd||jtddg|dt |jj dS)NcSsdS)Nr)r r!rrrr@szBMockTest.test_parent_propagation_with_create_autospec..foorRrSz mock.child) r rrfrYrr1rjrrCrDr)rr@rrrr,test_parent_propagation_with_create_autospecs  z5MockTest.test_parent_propagation_with_create_autospeccCspdd}t}|t|ddd|dd|t|jd||jjtddg| dt |jj dS) NcSsdS)Nr)r r!rrrr@r+zGMockTest.test_parent_propagation_with_autospec_attach_mock..foorB)rArfrRrSz mock.child) r rXrrfrYrr1rjrrCrDr)rr@rgrrr1test_parent_propagation_with_autospec_attach_mocks z:MockTest.test_parent_propagation_with_autospec_attach_mockc stjj}|dd|tdztjd=fddt|tjdddlm }m }m }m }||||g}x"|D]}|t d }||t qzWWdQRXdS) NcSsttjd|S)Nr)rSrr)rrrrrr+z9MockTest.test_isinstance_under_settrace..z sys.modulesz unittest.mockcsS)Nr)frameZeventr)tracerrrsz6MockTest.test_isinstance_under_settrace..tracer)r rr r )rJ)rrrrrrmodulessettrace unittest.mockr rr r rr}) rZ old_patchr rr r rGrr:r)rrtest_isinstance_under_settraces        z'MockTest.test_isinstance_under_settraceN)~rrrr.r>r?rHrMr]r_rbrZ skipUnlessrplatformrerkrlrnrrrtrvrxrzrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr rrrrrrr$r)r+r.r2r6r8r9r;r<r@rArCrDrErGrJrKrLrMrTrUrVrWrZr]rdrgrhrmrrrtruryrzr|rrrrrrrrrrrrrrrr,.s )"            7N # ,((     5          r,__main__)rr4rrirZunittest.test.testmock.supportrrrrrrrrr r r r r rrLrrr*rr,rmainrrrrs8  4  R