ó
ç'ÊUc           @   s¢   d  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
 d d l m Z d Z d	 e j f d
 „  ƒ  YZ d e j f d „  ƒ  YZ d S(   s&   
Selector tests for cssselect backend
iÿÿÿÿ(   t   unittest(   t   HtmlResponse(   t   ScrapyHTMLTranslator(   t   Selector(   t   SelectorSyntaxError(   t   ExpressionErrors¥  
<html>
<body>
<div>
 <a id="name-anchor" name="foo"></a>
 <a id="tag-anchor" rel="tag" href="http://localhost/foo">link</a>
 <a id="nofollow-anchor" rel="nofollow" href="https://example.org"> link</a>
 <p id="paragraph">
   lorem ipsum text
   <b id="p-b">hi</b> <em id="p-em">there</em>
   <b id="p-b2">guy</b>
   <input type="checkbox" id="checkbox-unchecked" />
   <input type="checkbox" id="checkbox-disabled" disabled="" />
   <input type="text" id="text-checked" checked="checked" />
   <input type="hidden" />
   <input type="hidden" disabled="disabled" />
   <input type="checkbox" id="checkbox-checked" checked="checked" />
   <input type="checkbox" id="checkbox-disabled-checked"
          disabled="disabled" checked="checked" />
   <fieldset id="fieldset" disabled="disabled">
     <input type="checkbox" id="checkbox-fieldset-disabled" />
     <input type="hidden" />
   </fieldset>
 </p>
 <map name="dummymap">
   <area shape="circle" coords="200,250,25" href="foo.html" id="area-href" />
   <area shape="default" id="area-nohref" />
 </map>
</div>
<div class="cool-footer" id="foobar-div" foobar="ab bc cde">
    <span id="foobar-span">foo ter</span>
</div>
</body></html>
t   TranslatorMixinTestc           B   sM   e  Z e Z d  „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 RS(   c         C   s"   |  j  ƒ  |  _ |  j j |  _ d  S(   N(   t   tr_clst   trt   css_to_xpatht   c2x(   t   self(    (    sE   /home/travis/build/scrapy/scrapy/tests/test_selector_csstranslator.pyt   setUp4   s    c         C   sI   d	 d
 d d g } x0 | D]( \ } } |  j  |  j | ƒ | | ƒ q Wd  S(   Ns   ::attr(name)u   descendant-or-self::*/@names   a::attr(href)u   descendant-or-self::a/@hrefs   a ::attr(img)u0   descendant-or-self::a/descendant-or-self::*/@imgs   a > ::attr(class)u   descendant-or-self::a/*/@class(   s   ::attr(name)u   descendant-or-self::*/@name(   s   a::attr(href)u   descendant-or-self::a/@href(   s   a ::attr(img)u0   descendant-or-self::a/descendant-or-self::*/@img(   s   a > ::attr(class)u   descendant-or-self::a/*/@class(   t   assertEqualR
   (   R   t   casest   csst   xpath(    (    sE   /home/travis/build/scrapy/scrapy/tests/test_selector_csstranslator.pyt   test_attr_function8   s    	c         C   sR   d t  f d t  f d t f g } x* | D]" \ } } |  j | |  j | ƒ q( Wd  S(   Ns
   ::attr(12)s   ::attr(34test)s   ::attr(@href)(   R   R   t   assertRaisesR
   (   R   R   R   t   exc(    (    sE   /home/travis/build/scrapy/scrapy/tests/test_selector_csstranslator.pyt   test_attr_function_exceptionB   s
    		c         C   s^   d d d d d d d d d d  d! g } x0 | D]( \ } } |  j  |  j | ƒ | | ƒ q. Wd  S("   Ns   ::textu   descendant-or-self::text()s   p::textu   descendant-or-self::p/text()s   p ::textu0   descendant-or-self::p/descendant-or-self::text()s	   #id::textu(   descendant-or-self::*[@id = 'id']/text()s
   p#id::textu(   descendant-or-self::p[@id = 'id']/text()s   p#id ::textu<   descendant-or-self::p[@id = 'id']/descendant-or-self::text()s   p#id > ::textu*   descendant-or-self::p[@id = 'id']/*/text()s   p#id ~ ::textu=   descendant-or-self::p[@id = 'id']/following-sibling::*/text()s   a[href]::textu#   descendant-or-self::a[@href]/text()s   a[href] ::textu7   descendant-or-self::a[@href]/descendant-or-self::text()s   p::text, a::textu;   descendant-or-self::p/text() | descendant-or-self::a/text()(   s   ::textu   descendant-or-self::text()(   s   p::textu   descendant-or-self::p/text()(   s   p ::textu0   descendant-or-self::p/descendant-or-self::text()(   s	   #id::textu(   descendant-or-self::*[@id = 'id']/text()(   s
   p#id::textu(   descendant-or-self::p[@id = 'id']/text()(   s   p#id ::textu<   descendant-or-self::p[@id = 'id']/descendant-or-self::text()(   s   p#id > ::textu*   descendant-or-self::p[@id = 'id']/*/text()(   s   p#id ~ ::textu=   descendant-or-self::p[@id = 'id']/following-sibling::*/text()(   s   a[href]::textu#   descendant-or-self::a[@href]/text()(   s   a[href] ::textu7   descendant-or-self::a[@href]/descendant-or-self::text()(   s   p::text, a::textu;   descendant-or-self::p/text() | descendant-or-self::a/text()(   R   R
   (   R   R   R   R   (    (    sE   /home/travis/build/scrapy/scrapy/tests/test_selector_csstranslator.pyt   test_text_pseudo_elementK   s    	c         C   sR   d t  f d t  f d t f g } x* | D]" \ } } |  j | |  j | ƒ q( Wd  S(   Ns   ::attribute(12)s   ::text()s   ::attr(@href)(   R   R   R   R
   (   R   R   R   R   (    (    sE   /home/travis/build/scrapy/scrapy/tests/test_selector_csstranslator.pyt   test_pseudo_function_exception\   s
    		c         C   s@   d t  f g } x* | D]" \ } } |  j | |  j | ƒ q Wd  S(   Ns   ::text-node(   R   R   R
   (   R   R   R   R   (    (    sE   /home/travis/build/scrapy/scrapy/tests/test_selector_csstranslator.pyt   test_unknown_pseudo_elemente   s    c         C   sI   d t  f d t  f g } x* | D]" \ } } |  j | |  j | ƒ q Wd  S(   Ns   :texts   :attribute(name)(   R   R   R
   (   R   R   R   R   (    (    sE   /home/travis/build/scrapy/scrapy/tests/test_selector_csstranslator.pyt   test_unknown_pseudo_classl   s    	(   t   __name__t
   __module__R   R   R   R   R   R   R   R   R   (    (    (    sE   /home/travis/build/scrapy/scrapy/tests/test_selector_csstranslator.pyR   0   s   		
						t   CSSSelectorTestc           B   sD   e  Z e Z d  „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 RS(   c         C   s.   t  d d t ƒ|  _ |  j |  j ƒ |  _ d  S(   Ns   http://example.comt   body(   R   t   HTMLBODYt   htmlresponset   ssclst   sel(   R   (    (    sE   /home/travis/build/scrapy/scrapy/tests/test_selector_csstranslator.pyR   y   s    c         O   s>   g  |  j  j | | Ž  j ƒ  D] } | j ƒ  r | j ƒ  ^ q S(   N(   R    R   t   extractt   strip(   R   t   at   kwt   v(    (    sE   /home/travis/build/scrapy/scrapy/tests/test_selector_csstranslator.pyt   x}   s    c         C   s‡   x9 |  j  j d ƒ D]% } |  j t | |  j  j ƒ | ƒ q W|  j |  j  j d ƒ j ƒ  g  |  j  j d ƒ D] } | j ƒ  ^ qj ƒ d  S(   Nt   input(   R    R   t
   assertTruet
   isinstancet	   __class__R   R!   (   R   R&   (    (    sE   /home/travis/build/scrapy/scrapy/tests/test_selector_csstranslator.pyt   test_selector_simple€   s    #c         C   sÚ   |  j  |  j d ƒ d g ƒ |  j  |  j d ƒ d g ƒ |  j  |  j d ƒ d g ƒ |  j  |  j d ƒ d g ƒ |  j  |  j d ƒ d d	 d
 d g ƒ |  j  |  j d ƒ d g ƒ |  j  |  j d ƒ d d	 d
 d g ƒ d  S(   Ns   #p-b2u   <b id="p-b2">guy</b>s   #p-b2::textu   guys   #p-b2 ::texts   #paragraph::textu   lorem ipsum texts   #paragraph ::textu   hiu   theres   p::texts   p ::text(   R   R&   (   R   (    (    sE   /home/travis/build/scrapy/scrapy/tests/test_selector_csstranslator.pyR   †   s    %c         C   sz   |  j  |  j d ƒ d g ƒ |  j  |  j d ƒ d g ƒ |  j  |  j d ƒ d d g ƒ |  j  |  j d ƒ d	 d
 g ƒ d  S(   Ns   #p-b2::attr(id)u   p-b2s   .cool-footer::attr(class)u   cool-footers   .cool-footer ::attr(id)u
   foobar-divu   foobar-spans"   map[name="dummymap"] ::attr(shape)u   circleu   default(   R   R&   (   R   (    (    sE   /home/travis/build/scrapy/scrapy/tests/test_selector_csstranslator.pyt   test_attribute_function   s    c         C   sc   |  j  |  j j d ƒ j d ƒ j ƒ  d d g ƒ |  j  |  j j d ƒ j d ƒ j ƒ  d g ƒ d  S(   Nt   ps   b::textu   hiu   guyt   divs   area:last-childu'   <area shape="default" id="area-nohref">(   R   R    R   R!   (   R   (    (    sE   /home/travis/build/scrapy/scrapy/tests/test_selector_csstranslator.pyt   test_nested_selector•   s    $$(
   R   R   R   R   R   R&   R+   R   R,   R/   (    (    (    sE   /home/travis/build/scrapy/scrapy/tests/test_selector_csstranslator.pyR   u   s   						N(   t   __doc__t   twisted.trialR    t   scrapy.httpR   t   scrapy.selector.csstranslatorR   t   scrapy.selectorR   t   cssselect.parserR   t   cssselect.xpathR   R   t   TestCaseR   R   (    (    (    sE   /home/travis/build/scrapy/scrapy/tests/test_selector_csstranslator.pyt   <module>   s   $E