a ze2@sddlZddlZddlZddlmZddlmZddlm Z ddl m Z e dddZ d Z Gd d d ejZGd d d ejZGdddeZGdddeZGddde jZGdddejZedkredS)N)AttributesImpl)feature_external_ges)pulldom)findfileztest.xmlZ xmltestdata)Zsubdira  Introduction to XSL

A. Namespace

c@sPeZdZddZddZddZejddZejd d Z d d Z d dZ dS)PullDOMTestCasecCs\tt}||jjt|ttd}tt|Wdn1sN0YdS)z&Minimal test of DOMEventStream.parse()rbN)rparsetstfileZ addCleanupstreamcloselistopen)selfhandlerfinrE/opt/bitninja-python-dojo/embedded/lib/python3.9/test/test_pulldom.py test_parses   zPullDOMTestCase.test_parsecCstt}t|\}}|t|d|tj|t|\}}|tj||d|j |dt |j ||j dj dt|\}}|tj|t|\}}|tj|t|\}}|d|j |}t|\}}|tj||d|jt|\}}|tj||d|j |||ut|\}}|tj|t|\}}|tj||d|j t|\}}|tj||d|j t|\}}|tj|t|\}}|tj||d |j t|\}}|tj||d |j t|\}}|tj|t|\}}|tj||d |j t|\}}|tj|t|\}}|tj|t|\}}|tj|d S) z&Test DOMEventStream parsing semantics. createElementhtmlz xmlns:xdczhttp://www.xml.com/bookstitlezIntroduction to XSLhrpz xdc:authorN)r parseString SMALL_SAMPLEnext assertTruehasattr assertEqualSTART_DOCUMENT START_ELEMENTtagNamelenZ attributesZ getNamedItemvalue CHARACTERSdata END_ELEMENT)ritemsevtnodeZ title_noderrrtest_parse_semantics+sh                   z$PullDOMTestCase.test_parse_semanticscCs@tt}|D]<\}}|tjkr|jdkr|||dt|jqVq| d|D]\}}|tjkrZqrqZ|d|jd|||t |dtj t |\}}||jd||t |t |\}}||jd| t t |Wd n1s0Y|||j||jd S) z$Ensure expandItem works as expected.rz,No "title" element detected in SMALL_SAMPLE!rz=expandNode did not leave DOMEventStream in the correct state.rrrN)rrrr!r"Z expandNoderr# childNodesfailrr%Z assertRaises StopIterationclear assertIsNoneparserr )rr(r)itemr*rrrtest_expandItemis4            ( zPullDOMTestCase.test_expandItemcCs4tt}|D]\}}|tjkrq0q|ddS)z*PullDOM does not receive "comment" events.zNo comment was encounteredN)rrrCOMMENTr.)rr(r)_rrr test_comments    zPullDOMTestCase.test_commentcCsptt}|D] \}}|tjkr|jdkrq0qzt|\}}|tj|Wntyj| dYn0dS)z/PullDOM does not receive "end-document" events.rz8Ran out of events, but should have received END_DOCUMENTN) rrrr'r"rr END_DOCUMENTr/r.)rr(r)r*rrrtest_end_documents    z!PullDOMTestCase.test_end_documentcCsPtt}|td&||ddtjWdn1sB0YdS)NzUse iterator protocol insteadr)rrrZassertWarnsRegexDeprecationWarningrr )rr2rrrtest_getitem_deprecations  z(PullDOMTestCase.test_getitem_deprecationcCs*tt}|j}|t}||ddS)NF)rrrr2Z getFeaturerr)rr2Z saxparserZgesrrrtest_external_ges_defaults  z)PullDOMTestCase.test_external_ges_defaultN) __name__ __module__ __qualname__rr+r4unittestexpectedFailurer7r9r<r=rrrrrs>   rc@s8eZdZdZddZejddZddZd d d Z d S) ThoroughTestCasez(Test the hard-to-reach parts of pulldom.cCs|tjdtddS)z0Test some of the hard-to-reach parts of PullDOM.N)r2)_test_thoroughrr SAXExerciserrrrrtest_thorough_parsesz$ThoroughTestCase.test_thorough_parsecCstdtd}||dS)z2SAX2DOM can"t handle a PI before the root element.N )SAX2DOMTestHelperrErDrpdrrrtest_sax2dom_failsz"ThoroughTestCase.test_sax2dom_failcCstdtd}||ddS)z0Test some of the hard-to-reach parts of SAX2DOM.NrHF)rISAX2DOMExerciserrDrJrrrtest_thorough_sax2domsz&ThoroughTestCase.test_thorough_sax2domTcCst|\}}|tj||t|d|rt|\}}|tj||d|jt|\}}|tj||d|j |d|jt|\}}|tj ||d|j t|\}}|tj||d|jt|\}}|tj||d|j |d|jt|\}}|tj ||d|j t|\}}|tj ||d|jt|\}}|tj ||d|j t|\}}|tj ||d|j t|\}}|tj|dS) zPTest some of the hard-to-reach parts of the parser, using a mock parser.r a commenttargetr&rrtextN)rrrr rrr5r&ZPROCESSING_INSTRUCTIONrPr!r"r%r'r8)rrKZ before_rootr)r*rrrrDsF           zThoroughTestCase._test_thoroughN)T) r>r?r@__doc__rGrArBrLrNrDrrrrrCs  rCc@s0eZdZdZddZddZddZeZeZdS) rEznA fake sax parser that calls some of the harder-to-reach sax methods to ensure it emits the correct eventscCs ||_dSN)_handler)rrrrrsetContentHandlerszSAXExerciser.setContentHandlercCs|j}||d|dd|dti|d|dd|dtddi|d|d|d|dS) NrOrPr&rrclass paraclassrQ) rT startDocumentcommentprocessingInstruction startElementr characters endElement endDocumentrr6hrrrrs       zSAXExerciser.parsecOsdS)zStub method. Does nothing.Nr)rargskwargsrrrstub szSAXExerciser.stubN) r>r?r@rRrUrrcZ setProperty setFeaturerrrrrEs rEc@seZdZdZddZdS)rMzThe same as SAXExerciser, but without the processing instruction and comment before the root element, because S2D can"t handle itcCsr|j}||dti|d|dd|dtddi|d|d|d|dS) NrrOrPr&rrVrWrQ) rTrXr[rrYrZr\r]r^r_rrrrs     zSAX2DOMExerciser.parseN)r>r?r@rRrrrrrrMsrMc@seZdZdZddZdS)rIz1Allows us to drive SAX2DOM from a DOMEventStream.cCs0t|_|jtjjjd|j|jdS)Nr,) rSAX2DOMr2rdxmlsaxrZfeature_namespacesrUrFrrrreset(s zSAX2DOMTestHelper.resetN)r>r?r@rRrhrrrrrI%srIc@s&eZdZd ddZddZddZdS) SAX2DOMTestCaseTestcCs|||dSrS)r)rtestZtestnamerrrconfirm1szSAX2DOMTestCase.confirmcCs|tt^}t|tjtt}|D] \}}|tj kr&|j dkr&qHq&| t|j dWdn1sn0YdS)z'Ensure SAX2DOM can parse from a stream.rrN) ioStringIOrrIrfrgZ make_parserr#rr!r"Z assertGreaterr-)rrsdr)r*rrr test_basic4s   zSAX2DOMTestCase.test_basiccCs>t}||di|d|di|d|d|d|d||j}|j}|j \}}}|j d}| |j | |j || |j || |j || |j || |j | |j | |j | |j|| |j|| |j|| |j|| |j||dS)z)Ensure SAX2DOM expands nodes as expected.docrQZsubelmrN)rrerXr[r\r]r^ZdocumentZdocumentElementr-r1ZpreviousSiblingZassertIsZ nextSiblingZ parentNodeunlink)rZsax2domrqrootZtext1Zelm1Ztext2Ztext3rrr testSAX2DOM@s8             zSAX2DOMTestCase.testSAX2DOMN)rj)r>r?r@rlrprtrrrrri/s  ri__main__)rmrAZxml.saxrfZxml.sax.xmlreaderrZxml.sax.handlerrZxml.domrZ test.supportrr rZTestCaserrCobjectrErMZDOMEventStreamrIrir>mainrrrrs"      A" 4