o
    ¯be)  ã                   @   sR   d dl mZ d dlmZ d dlmZ d dlmZ d dlm	Z	 G dd„ dej
ƒZdS )	é    )Úunittest)Úxpath)ÚElement)Ú
XPathQuery)ÚSyntaxErrorc                   @   sä   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd „ Zd!d"„ Zd#d$„ Zd%d&„ Zd'd(„ Zd)d*„ Zd+d,„ Zd-d.„ Zd/d0„ Zd1d2„ Zd3d4„ Zd5d6„ Zd7S )8Ú
XPathTestsc                 C   s¦  t dƒ| _d| jd< d| jd< | j d¡ | j d¡| _| j d¡| _| j d	¡| _| j d
¡ | j d¡ | j d¡| _d| jd< | j d¡| _| j d¡| _	| j d	¡| _
| j
 d¡ | j d¡| _| j d¡| _d| jd< d| jd< | j d¡| _| j d	¡| _| j d¡ | j d¡| _d| jd< d| jd< | j d¡| _| j d	¡| _| j d¡ | j d¡| _d| jd< d| jd< d| jd< | j d¡| _| j d¡ d S )N)ÚtestnsÚfooÚvalue1Úattrib1zuser@host/resourceÚattrib3ÚsomecontentÚbarr	   ÚgarÚDEFÚsomemorecontentÚvalue2Úattrib2ÚABCÚvalue4Úattrib4Úvalue5Úattrib5ÚJKLÚMNOÚvalue6õ   Ã¡Úattrib6Úquuxu   â˜ƒ)r   ÚeÚ
addContentÚ
addElementÚbar1ÚsubfooÚgar1Úbar2Úbar3Úsubfoo2Úgar2Úbar4Úbar5Úsubfoo3Úgar3Úbar6Úsubfoo4Úgar4Úbar7r   ©Úself© r3   ú?/usr/lib/python3/dist-packages/twisted/words/test/test_xpath.pyÚsetUp   sD   










zXPathTests.setUpc              	   C   sx   |   t d| j¡d¡ |   t d| j¡| j| j| j| j| j	| j
g¡ |   t d| j¡d¡ |   t d| j¡ddg¡ dS )z=
        Test basic operation of the static methods.
        ú/foo/barTú/foor   r   N)ÚassertEqualr   Úmatchesr   ÚqueryForNodesr"   r%   r)   r*   r-   r0   ÚqueryForStringÚqueryForStringListr1   r3   r3   r4   Útest_staticMethodsL   s   þÿzXPathTests.test_staticMethodsc                 C   ó    t dƒ}|  | | j¡d¡ dS )z3
        Test matching foo with child bar.
        r6   é   N©r   r8   r9   r   ©r2   Úxpr3   r3   r4   Útest_locationFooBarZ   ó   zXPathTests.test_locationFooBarc                 C   ó@   t dƒ}|  | | j¡d¡ |  | | j¡| j| j| jg¡ dS )z8
        Test finding foos at the second level.
        z/foo/bar/foor?   N)r   r8   r9   r   r:   r#   r+   r.   rA   r3   r3   r4   Útest_locationFooBarFooa   s
   ÿz!XPathTests.test_locationFooBarFooc                 C   r>   )z(
        Test not finding bar3.
        z	/foo/bar3r   Nr@   rA   r3   r3   r4   Útest_locationNoBar3k   rD   zXPathTests.test_locationNoBar3c              	   C   sL   t dƒ}|  | | j¡d¡ |  | | j¡| j| j| j| j| j	| j
g¡ dS )z-
        Test finding childs of foo.
        z/foo/*TN)r   r8   r9   r   r:   r"   r%   r)   r*   r-   r0   rA   r3   r3   r4   Útest_locationAllChildsr   s   
þz!XPathTests.test_locationAllChildsc                 C   r>   )z3
        Test matching foo with attribute.
        z/foo[@attrib1]TNr@   rA   r3   r3   r4   Útest_attribute}   rD   zXPathTests.test_attributec                 C   ó8   t dƒ}|  | | j¡d¡ |  | | j¡| jg¡ dS )z>
        Test find nodes with attribute having value.
        z/foo/*[@attrib2='value2']TN)r   r8   r9   r   r:   r%   rA   r3   r3   r4   Útest_attributeWithValueAny„   ó   z%XPathTests.test_attributeWithValueAnyc                 C   rJ   )zI
        Nodes' attributes can be matched with non-ASCII values.
        u   /foo/*[@attrib6='Ã¡']TN)r   r8   r9   r   r:   r0   rA   r3   r3   r4   Útest_locationWithValueUnicodeŒ   rL   z(XPathTests.test_locationWithValueUnicodec                 C   r>   )z4
        Test matching node with namespace.
        z/foo[@xmlns='testns']/barr?   Nr@   rA   r3   r3   r4   Útest_namespaceFound”   rD   zXPathTests.test_namespaceFoundc                 C   r>   )z>
        Test not matching node with wrong namespace.
        z/foo[@xmlns='badns']/bar2r   Nr@   rA   r3   r3   r4   Útest_namespaceNotFound›   rD   z!XPathTests.test_namespaceNotFoundc                 C   r>   )zA
        Test matching node with attribute having value.
        z/foo[@attrib1='value1']r?   Nr@   rA   r3   r3   r4   Útest_attributeWithValue¢   rD   z"XPathTests.test_attributeWithValuec                 C   r>   )zM
        queryforString on absolute paths returns their first CDATA.
        r7   r   N)r   r8   r;   r   rA   r3   r3   r4   Útest_queryForString©   rD   zXPathTests.test_queryForStringc                 C   ó$   t dƒ}|  | | j¡ddg¡ dS )zO
        queryforStringList on absolute paths returns all their CDATA.
        r7   r   r   N©r   r8   r<   r   rA   r3   r3   r4   Útest_queryForStringList°   ó   ÿz"XPathTests.test_queryForStringListc                 C   rR   )zO
        queryforStringList on relative paths returns all their CDATA.
        z//foor   r   NrS   rA   r3   r3   r4   Ú"test_queryForStringListAnyLocation¹   rU   z-XPathTests.test_queryForStringListAnyLocationc              	   C   s8   t dƒ}|  | | j¡| j| j| j| j| j| j	g¡ dS )z%
        Test finding nodes.
        r6   N)
r   r8   r:   r   r"   r%   r)   r*   r-   r0   rA   r3   r3   r4   Útest_queryForNodesÂ   s
   
þzXPathTests.test_queryForNodesc                 C   r>   )z7
        Test matching a node with given text.
        z/foo[text() = 'somecontent']TNr@   rA   r3   r3   r4   Útest_textConditionÌ   rD   zXPathTests.test_textConditionc                 C   rJ   )zK
        A node can be matched by text with non-ascii code points.
        u   //*[text()='â˜ƒ']TN)r   r8   r9   r   r:   r   rA   r3   r3   r4   Útest_textConditionUnicodeÓ   rL   z$XPathTests.test_textConditionUnicodec                 C   r>   )z(
        Test for not operator.
        z/foo[not(@nosuchattrib)]TNr@   rA   r3   r3   r4   Útest_textNotOperatorÛ   rD   zXPathTests.test_textNotOperatorc                 C   s\   t dƒ}|  | | j¡d¡ |  | | j¡| j| j| j| jg¡ |  | 	| j¡g d¢¡ dS )zS
        Test finding any nodes named gar and getting their text contents.
        z//garT)r   r   r   r   N)
r   r8   r9   r   r:   r$   r(   r,   r/   r<   rA   r3   r3   r4   Útest_anyLocationAndTextâ   s   ÿz"XPathTests.test_anyLocationAndTextc              
   C   sP   t dƒ}|  | | j¡d¡ |  | | j¡| j| j| j| j| j	| j
| jg¡ dS )z3
        Test finding any nodes named bar.
        ú//barTN)r   r8   r9   r   r:   r"   r%   r&   r)   r*   r-   r0   rA   r3   r3   r4   Útest_anyLocationí   s   
ùþzXPathTests.test_anyLocationc                 C   s   t dƒ}|  t|jd¡ dS )zn
        L{XPathQuery.queryForString} should raise a L{NotImplementedError}
        for any location.
        r\   N)r   ÚassertRaisesÚNotImplementedErrorr;   rA   r3   r3   r4   Útest_anyLocationQueryForString   s   z)XPathTests.test_anyLocationQueryForStringc                 C   rJ   )z9
        Test boolean and operator in condition.
        z.//bar[@attrib4='value4' and @attrib5='value5']TN)r   r8   r9   r   r:   r*   rA   r3   r3   r4   Útest_andOperator  rL   zXPathTests.test_andOperatorc                 C   ó<   t dƒ}|  | | j¡d¡ |  | | j¡| j| jg¡ dS )z8
        Test boolean or operator in condition.
        z-//bar[@attrib5='value4' or @attrib5='value5']TN)r   r8   r9   r   r:   r*   r-   rA   r3   r3   r4   Útest_orOperator  s    zXPathTests.test_orOperatorc                 C   rb   )zK
        Test multiple boolean operators in condition with parens.
        zf//bar[@attrib4='value4' and
                                 (@attrib5='value4' or @attrib5='value6')]TN)r   r8   r9   r   r:   r-   r0   rA   r3   r3   r4   Útest_booleanOperatorsParens  s
   ÿ z&XPathTests.test_booleanOperatorsParensc                 C   rE   )zN
        Test multiple boolean operators in condition without parens.
        z„//bar[@attrib5='value4' or
                                 @attrib5='value5' or
                                 @attrib5='value6']TN)r   r8   r9   r   r:   r*   r-   r0   rA   r3   r3   r4   Útest_booleanOperatorsNoParens#  s
   ÿ$z(XPathTests.test_booleanOperatorsNoParensc                 C   s,   |   ttd¡}|  |j d¡d|j ¡ dS )z}
        A missing closing bracket raises a SyntaxError.

        This test excercises the most common failure mode.
        z//bar[@attrib1zTrying to find one ofzCSyntaxError message '%s' doesn't start with 'Trying to find one of'N)r^   r   r   Ú
assertTrueÚmsgÚ
startswith)r2   Úexcr3   r3   r4   Útest_badXPathNoClosingBracket/  s   
ÿþz(XPathTests.test_badXPathNoClosingBracketN)Ú__name__Ú
__module__Ú__qualname__r5   r=   rC   rF   rG   rH   rI   rK   rM   rN   rO   rP   rQ   rT   rV   rW   rX   rY   rZ   r[   r]   r`   ra   rc   rd   re   rj   r3   r3   r3   r4   r      s8    ?
		
r   N)Útwisted.trialr   Útwisted.words.xishr   Útwisted.words.xish.domishr   Útwisted.words.xish.xpathr   Útwisted.words.xish.xpathparserr   ÚTestCaser   r3   r3   r3   r4   Ú<module>   s   