U
    
W[ś*  ć                   @   sb   d dl mZmZ 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 )
é    )Śabsolute_importŚdivision)Ś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)ZtestnsŚfooZvalue1Zattrib1zuser@host/resourceZattrib3ŚsomecontentZ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$   ś?/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   r!   ŚqueryForStringŚqueryForStringListr"   r$   r$   r%   Śtest_staticMethodsN   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_locationFooBar\   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_locationFooBarFooc   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_locationNoBar3m   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_locationAllChildst   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   Z
assertTrueŚmsgŚ
startswith)r#   Śexcr$   r$   r%   Śtest_badXPathNoClosingBracket!  s    ’’z(XPathTests.test_badXPathNoClosingBracketN)Ś__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   rQ   r$   r$   r$   r%   r	      s6   ?

		
r	   N)Z
__future__r   r   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   