U
    n5{i=*  ã                   @   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œdd„Zddœdd„Zddœdd„Zddœd	d
„Zddœdd„Zddœdd„Zddœdd„Z	ddœdd„Z
ddœdd„Zddœdd„Zddœdd„Zddœdd„Zddœdd„Zddœdd„Zddœdd „Zddœd!d"„Zddœd#d$„Zddœd%d&„Zddœd'd(„Zddœd)d*„Zddœd+d,„Zddœd-d.„Zddœd/d0„Zddœd1d2„Zddœd3d4„Zddœd5d6„Zddœd7d8„ZdS )9Ú
XPathTestsN)Úreturnc                 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)ZtestnsÚfooZvalue1Zattrib1zuser@host/resourceZattrib3ÚsomecontentÚbarr	   ZgarÚDEFÚsomemorecontentZvalue2Zattrib2ÚABCZvalue4Zattrib4Zvalue5Zattrib5ÚJKLÚMNOZvalue6õ   Ã¡Zattrib6Úquuxu   â˜ƒ)r   ÚeZ
addContentZ
addElementÚbar1ÚsubfooÚgar1Úbar2Úbar3Zsubfoo2Úgar2Úbar4Úbar5Úsubfoo3Úgar3Úbar6Úsubfoo4Úgar4Úbar7r   ©Úself© r$   úA/tmp/pip-unpacked-wheel-mnhg7r0q/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   r!   ÚqueryForStringÚqueryForStringListr"   r$   r$   r%   Útest_staticMethodsL   s    þ ÿzXPathTests.test_staticMethodsc                 C   s    t dƒ}|  | | j¡d¡ dS )z3
        Test matching foo with child bar.
        r'   é   N©r   r)   r*   r   ©r#   Zxpr$   r$   r%   Útest_locationFooBarZ   s    zXPathTests.test_locationFooBarc                 C   s@   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   r)   r*   r   r+   r   r   r   r1   r$   r$   r%   Útest_locationFooBarFooa   s    
 ÿz!XPathTests.test_locationFooBarFooc                 C   s    t dƒ}|  | | j¡d¡ dS )z(
        Test not finding bar3.
        z	/foo/bar3r   Nr0   r1   r$   r$   r%   Útest_locationNoBar3k   s    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   r)   r*   r   r+   r   r   r   r   r   r!   r1   r$   r$   r%   Útest_locationAllChildsr   s    
þz!XPathTests.test_locationAllChildsc                 C   s    t dƒ}|  | | j¡d¡ dS )z3
        Test matching foo with attribute.
        z/foo[@attrib1]TNr0   r1   r$   r$   r%   Útest_attribute}   s    zXPathTests.test_attributec                 C   s8   t dƒ}|  | | j¡d¡ |  | | j¡| jg¡ dS )z>
        Test find nodes with attribute having value.
        z/foo/*[@attrib2='value2']TN)r   r)   r*   r   r+   r   r1   r$   r$   r%   Útest_attributeWithValueAny„   s    z%XPathTests.test_attributeWithValueAnyc                 C   s8   t dƒ}|  | | j¡d¡ |  | | j¡| jg¡ dS )zI
        Nodes' attributes can be matched with non-ASCII values.
        u   /foo/*[@attrib6='Ã¡']TN)r   r)   r*   r   r+   r!   r1   r$   r$   r%   Útest_locationWithValueUnicodeŒ   s    z(XPathTests.test_locationWithValueUnicodec                 C   s    t dƒ}|  | | j¡d¡ dS )z4
        Test matching node with namespace.
        z/foo[@xmlns='testns']/barr/   Nr0   r1   r$   r$   r%   Útest_namespaceFound”   s    zXPathTests.test_namespaceFoundc                 C   s    t dƒ}|  | | j¡d¡ dS )z>
        Test not matching node with wrong namespace.
        z/foo[@xmlns='badns']/bar2r   Nr0   r1   r$   r$   r%   Útest_namespaceNotFound›   s    z!XPathTests.test_namespaceNotFoundc                 C   s    t dƒ}|  | | j¡d¡ dS )zA
        Test matching node with attribute having value.
        z/foo[@attrib1='value1']r/   Nr0   r1   r$   r$   r%   Útest_attributeWithValue¢   s    z"XPathTests.test_attributeWithValuec                 C   s    t dƒ}|  | | j¡d¡ dS )zM
        queryforString on absolute paths returns their first CDATA.
        r(   r
   N)r   r)   r,   r   r1   r$   r$   r%   Útest_queryForString©   s    zXPathTests.test_queryForStringc                 C   s$   t dƒ}|  | | j¡ddg¡ dS )zO
        queryforStringList on absolute paths returns all their CDATA.
        r(   r
   r   N©r   r)   r-   r   r1   r$   r$   r%   Útest_queryForStringList°   s
    
 ÿz"XPathTests.test_queryForStringListc                 C   s$   t dƒ}|  | | j¡ddg¡ dS )zO
        queryforStringList on relative paths returns all their CDATA.
        z//foor
   r   Nr=   r1   r$   r$   r%   Ú"test_queryForStringListAnyLocation¹   s
    
 ÿz-XPathTests.test_queryForStringListAnyLocationc              	   C   s8   t dƒ}|  | | j¡| j| j| j| j| j| j	g¡ dS )z%
        Test finding nodes.
        r'   N)
r   r)   r+   r   r   r   r   r   r   r!   r1   r$   r$   r%   Útest_queryForNodesÂ   s
    
þzXPathTests.test_queryForNodesc                 C   s    t dƒ}|  | | j¡d¡ dS )z7
        Test matching a node with given text.
        z/foo[text() = 'somecontent']TNr0   r1   r$   r$   r%   Útest_textConditionÌ   s    zXPathTests.test_textConditionc                 C   s8   t dƒ}|  | | j¡d¡ |  | | j¡| jg¡ dS )zK
        A node can be matched by text with non-ascii code points.
        u   //*[text()='â˜ƒ']TN)r   r)   r*   r   r+   r   r1   r$   r$   r%   Útest_textConditionUnicodeÓ   s    z$XPathTests.test_textConditionUnicodec                 C   s    t dƒ}|  | | j¡d¡ dS )z(
        Test for not operator.
        z/foo[not(@nosuchattrib)]TNr0   r1   r$   r$   r%   Útest_textNotOperatorÛ   s    zXPathTests.test_textNotOperatorc                 C   s`   t dƒ}|  | | j¡d¡ |  | | j¡| j| j| j| jg¡ |  | 	| j¡ddddg¡ dS )zS
        Test finding any nodes named gar and getting their text contents.
        z//garTr   r   r   r   N)
r   r)   r*   r   r+   r   r   r   r    r-   r1   r$   r$   r%   Ú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   r)   r*   r   r+   r   r   r   r   r   r   r!   r1   r$   r$   r%   Ú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.
        rE   N)r   ÚassertRaisesÚNotImplementedErrorr,   r1   r$   r$   r%   Útest_anyLocationQueryForString   s    z)XPathTests.test_anyLocationQueryForStringc                 C   s8   t dƒ}|  | | j¡d¡ |  | | j¡| jg¡ dS )z9
        Test boolean and operator in condition.
        z.//bar[@attrib4='value4' and @attrib5='value5']TN)r   r)   r*   r   r+   r   r1   r$   r$   r%   Útest_andOperator  s    zXPathTests.test_andOperatorc                 C   s<   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   r)   r*   r   r+   r   r   r1   r$   r$   r%   Útest_orOperator  s    zXPathTests.test_orOperatorc                 C   s<   t dƒ}|  | | j¡d¡ |  | | j¡| j| jg¡ dS )zK
        Test multiple boolean operators in condition with parens.
        zf//bar[@attrib4='value4' and
                                 (@attrib5='value4' or @attrib5='value6')]TN)r   r)   r*   r   r+   r   r!   r1   r$   r$   r%   Útest_booleanOperatorsParens  s
    ÿz&XPathTests.test_booleanOperatorsParensc                 C   s@   t dƒ}|  | | j¡d¡ |  | | j¡| j| j| jg¡ dS )zN
        Test multiple boolean operators in condition without parens.
        z„//bar[@attrib5='value4' or
                                 @attrib5='value5' or
                                 @attrib5='value6']TN)r   r)   r*   r   r+   r   r   r!   r1   r$   r$   r%   Ú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)rG   r   r   Ú
assertTrueÚmsgÚ
startswith)r#   Úexcr$   r$   r%   Útest_badXPathNoClosingBracket/  s    
ÿþz(XPathTests.test_badXPathNoClosingBracket)Ú__name__Ú
__module__Ú__qualname__r&   r.   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r>   r?   r@   rA   rB   rC   rD   rF   rI   rJ   rK   rL   rM   rR   r$   r$   r$   r%   r      s6   ?
		
r   N)Ztwisted.trialr   Ztwisted.words.xishr   Ztwisted.words.xish.domishr   Ztwisted.words.xish.xpathr   Ztwisted.words.xish.xpathparserr   ZTestCaser   r$   r$   r$   r%   Ú<module>   s
   