3
dsF                 @   s  d Z ddlmZ ddlZddlZddlZddlZddlZddlZddl	m
Z
mZmZmZmZ ddlmZmZmZ ddlmZmZmZmZ ddlmZmZmZ ddlZG d	d
 d
eZG dd deZG dd deZG dd deZ G dd deZ!G dd deZ"G dd deZ#dS )z#Tests of Beautiful Soup as a whole.    )	set_traceN)BeautifulSoupBeautifulStoneSoupGuessedAtParserWarningMarkupResemblesLocatorWarningdammit)builder_registryTreeBuilderParserRejectedMarkup)CommentSoupStrainerTagNavigableString   )default_builderLXML_PRESENTSoupTestc               @   sL   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S )TestConstructorc             C   s"   d}| j |}d|jjkstd S )Nu   <h1>éé</h1>u   éé)souph1stringAssertionError)selfdatar    r   =/tmp/pip-build-8z3xcdsh/beautifulsoup4/bs4/tests/test_soup.pytest_short_unicode_input(   s    
z(TestConstructor.test_short_unicode_inputc             C   s"   d}| j |}d|jjkstd S )Nz<h1>foo bar</h1>zfoo bar)r   r   r   r   )r   r   r   r   r   r   test_embedded_null-   s    
z"TestConstructor.test_embedded_nullc             C   s,   dj d}| j|dgd}d|jks(td S )Nu   Räksmörgåszutf-8)Zexclude_encodingszwindows-1252)encoder   original_encodingr   )r   	utf8_datar   r   r   r   test_exclude_encodings2   s    
z&TestConstructor.test_exclude_encodingsc             C   s   G dd dt }tddd}tjdd tdd|i|}W d Q R X t|j|sVttdd	|jjksltd
|jj	ks|t|f |}tjdd}td|dd}W d Q R X t
|d j}|jdst||jkst||jkstd S )Nc               @   sD   e Zd Zdd Zdd Zdd Zdd Zd	d
 Ze ZZ	dd Z
dS )z7TestConstructor.test_custom_builder_class.<locals>.Mockc             [   s(   || _ d| _d| _g | _g | _i | _d S )NTF)called_withZis_xmlZstore_line_numbersZcdata_list_attributesZpreserve_whitespace_tagsstring_containers)r   kwargsr   r   r   __init__;   s    z@TestConstructor.test_custom_builder_class.<locals>.Mock.__init__c             S   s   d S )Nr   )r   r   r   r   r   initialize_soupB   s    zGTestConstructor.test_custom_builder_class.<locals>.Mock.initialize_soupc             S   s
   || _ d S )N)fed)r   markupr   r   r   feedD   s    z<TestConstructor.test_custom_builder_class.<locals>.Mock.feedc             S   s   d S )Nr   )r   r   r   r   resetF   s    z=TestConstructor.test_custom_builder_class.<locals>.Mock.resetc             S   s   d S )Nr   )r   ignorer   r   r   r+   H   s    z>TestConstructor.test_custom_builder_class.<locals>.Mock.ignorec                s
   dV  d S )Nprepared markuporiginal encodingdeclared encodingcontains replacement characters)r,   r-   r.   r/   r   )r   argsr$   r   r   r   prepare_markupK   s    zFTestConstructor.test_custom_builder_class.<locals>.Mock.prepare_markupN)__name__
__module____qualname__r%   r&   r)   r*   r+   Zset_up_substitutionsZcan_be_empty_elementr1   r   r   r   r   Mock:   s   r5   valueT)varZconvertEntities)record builder)r7   zprepared markup)r:   Zignored_valuer   zCKeyword arguments to the BeautifulSoup constructor will be ignored.)r9   )objectdictwarningscatch_warningsr   
isinstancer:   r   r"   r'   strmessage
startswith)r   r5   r$   r   r:   wmsgr   r   r   test_custom_builder_class7   s"    
z)TestConstructor.test_custom_builder_classc             C   sX   G dd dt }dd }dd l}tjt}td|d W d Q R X dt|jksTtd S )	Nc               @   s   e Zd Zdd ZdS )z:TestConstructor.test_parser_markup_rejection.<locals>.Mockc             _   s   t dd S )NzNope.)r
   )r   r0   r$   r   r   r   r)   k   s    z?TestConstructor.test_parser_markup_rejection.<locals>.Mock.feedN)r2   r3   r4   r)   r   r   r   r   r5   j   s   r5   c                s    t d d dfV  t d d dfV  d S )NF)r(   )r   r0   r$   r   r   r   r1   n   s    zDTestConstructor.test_parser_markup_rejection.<locals>.prepare_markupr   r9   )r:   zoThe markup you provided was rejected by the parser. Trying a different parser or a different encoding may help.)	r	   repytestraisesr
   r   r@   r6   r   )r   r5   r1   rF   exc_infor   r   r   test_parser_markup_rejectiong   s    z,TestConstructor.test_parser_markup_rejectionc             C   s   d}| j |}|j}d|d ks$tddg|d ks8t| j |td d}d|jd ksZtxjddiddifD ]V}tjd	d
}| j |d |d}W d Q R X |j}ddg|d kstd|d ksltqlW d S )Nz&<a id=" an id " class=" a class "></a>z an id idaclass)r:   Zmulti_valued_attributesz	 a class *T)r8   Zan)r   rL   r   r   r=   r>   )r   r(   r   rL   Z
switcheroorC   r   r   r   test_cdata_list_attributesz   s    
z*TestConstructor.test_cdata_list_attributesc                sp   G dd dt G dd dtG dd dt | jdt tt id}t fd	d
|j D sltd S )Nc               @   s   e Zd ZdS )z9TestConstructor.test_replacement_classes.<locals>.TagPlusN)r2   r3   r4   r   r   r   r   TagPlus   s   rP   c               @   s   e Zd ZdS )z<TestConstructor.test_replacement_classes.<locals>.StringPlusN)r2   r3   r4   r   r   r   r   
StringPlus   s   rQ   c               @   s   e Zd ZdS )z=TestConstructor.test_replacement_classes.<locals>.CommentPlusN)r2   r3   r4   r   r   r   r   CommentPlus   s   rR   z<a><b>foo</b>bar</a><!--whee-->)Zelement_classesc             3   s   | ]}t | fV  qd S )N)r?   ).0x)rR   rQ   rP   r   r   	<genexpr>   s   z;TestConstructor.test_replacement_classes.<locals>.<genexpr>)r   r   r   r   allZrecursiveChildGeneratorr   )r   r   r   )rR   rQ   rP   r   test_replacement_classes   s    z(TestConstructor.test_replacement_classesc             C   s   G dd dt }G dd dt }| jd||dd}t|jjd t sJtt|jjd |s`tx|jjD ]}t||sjtqjW g |j	kstd S )	Nc               @   s   e Zd ZdS )zATestConstructor.test_alternate_string_containers.<locals>.PStringN)r2   r3   r4   r   r   r   r   PString   s   rX   c               @   s   e Zd ZdS )zATestConstructor.test_alternate_string_containers.<locals>.BStringN)r2   r3   r4   r   r   r   r   BString   s   rY   z4<div>Hello.<p>Here is <b>some <i>bolded</i></b> text)bp)r#   r   )
r   r   r?   divcontentsr   r[   rZ   stringsZstring_container_stack)r   rX   rY   r   sr   r   r    test_alternate_string_containers   s    z0TestConstructor.test_alternate_string_containersN)r2   r3   r4   r   r   r!   rE   rJ   rO   rW   r`   r   r   r   r   r   &   s   0r   c            
   @   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
ejjdddddddddgdd Zejjdddddgd d! Zd"d# Zd$d% Zd&d' Zd(d) Zd*S )+TestWarningsc             C   s@   x*|D ]"}t |j|r|jtks$t|S qW td||f d S )Nz%s warning not found in %r)r?   rA   filename__file__r   	Exception)r   r=   clsrC   r   r   r   _assert_warning   s
    
zTestWarnings._assert_warningc             C   s2   | j |t}t|j}|jtjd d s.td S )N<   )rf   r   r@   rA   rB   r   ZNO_PARSER_SPECIFIED_WARNINGr   )r   rC   warningrA   r   r   r   _assert_no_parser_specified   s    
z(TestWarnings._assert_no_parser_specifiedc          	   C   s.   t jdd}td}W d Q R X | j| d S )NT)r8   z<a><b></b></a>)r=   r>   r   ri   )r   rC   r   r   r   r   #test_warning_if_no_parser_specified   s    z0TestWarnings.test_warning_if_no_parser_specifiedc          
   C   s0   t jdd}tdd}W d Q R X | j| d S )NT)r8   z<a><b></b></a>html)r=   r>   r   ri   )r   rC   r   r   r   r   *test_warning_if_parser_specified_too_vague   s    z7TestWarnings.test_warning_if_parser_specified_too_vaguec          	   C   s2   t jdd}| jd}W d Q R X g |ks.td S )NT)r8   z<a><b></b></a>)r=   r>   r   r   )r   rC   r   r   r   r   ,test_no_warning_if_explicit_parser_specified   s    z9TestWarnings.test_no_warning_if_explicit_parser_specifiedc             C   sl   t jdd}tddtdd}W d Q R X | j|t}t|j}d|ksLtd|ksXtd	|j	 kshtd S )
NT)r8   z<a><b></b></a>zhtml.parserrZ   )parseOnlyThesern   
parse_onlys   <b></b>)
r=   r>   r   r   rf   DeprecationWarningr@   rA   r   r   )r   rC   r   rh   rD   r   r   r   )test_parseOnlyThese_renamed_to_parse_only   s    
z6TestWarnings.test_parseOnlyThese_renamed_to_parse_onlyc             C   sj   t jdd}d}t|ddd}W d Q R X | j|t}t|j}d|ksLtd|ksXtd|jksftd S )	NT)r8   s   ézhtml.parserutf8)fromEncodingrs   Zfrom_encoding)	r=   r>   r   rf   rp   r@   rA   r   r   )r   rC   rr   r   rh   rD   r   r   r   *test_fromEncoding_renamed_to_from_encoding  s    
z7TestWarnings.test_fromEncoding_renamed_to_from_encodingc             C   s(   t jt | jddd W d Q R X d S )Nz<a>T)Zno_such_argument)rG   rH   	TypeErrorr   )r   r   r   r   "test_unrecognized_keyword_argument  s    z/TestWarnings.test_unrecognized_keyword_argument	extensionzmarkup.htmlz
markup.htmzmarkup.HTMLz
markup.txtzmarkup.xhtmlz
markup.xmlz/home/user/filezc:\userilec          
   C   sH   t jdd2}td| d}| j|t}dt|jks:tW d Q R X d S )NT)r8   r(   zhtml.parserzlooks more like a filename)r=   r>   r   rf   r   r@   rA   r   )r   rw   rC   r   rh   r   r   r   test_resembles_filename_warning  s    z,TestWarnings.test_resembles_filename_warningZ
markuphtmlz
markup.comr9   z	markup.jsc          
   C   s6   t jdd}| jd| }W d Q R X g |ks2td S )NT)r8   r(   )r=   r>   r   r   )r   rw   rC   r   r   r   r   "test_resembles_filename_no_warning!  s    z/TestWarnings.test_resembles_filename_no_warningc          
   C   s`   d}t jdd}t|d}W d Q R X | j|t}dt|jksDt|t|jjdks\td S )Ns   http://www.crummybytes.com/T)r8   zhtml.parserzlooks more like a URLrr   )	r=   r>   r   rf   r   r@   rA   r   r   )r   urlwarning_listr   rh   r   r   r   test_url_warning_with_bytes_url-  s    z,TestWarnings.test_url_warning_with_bytes_urlc          
   C   sZ   d}t jdd}t|d}W d Q R X | j|t}dt|jksDt|t|jksVtd S )Nzhttp://www.crummyunicode.com/T)r8   zhtml.parserzlooks more like a URL)r=   r>   r   rf   r   r@   rA   r   )r   rz   r{   r   rh   r   r   r   !test_url_warning_with_unicode_url7  s    z.TestWarnings.test_url_warning_with_unicode_urlc          
   C   s>   t jdd}| jd}W d Q R X tdd |D  s:td S )NT)r8   s$   http://www.crummybytes.com/ is greatc             s   s   | ]}d t |jkV  qdS )zlooks more like a URLN)r@   rA   )rS   rC   r   r   r   rU   H  s   zETestWarnings.test_url_warning_with_bytes_and_space.<locals>.<genexpr>)r=   r>   r   anyr   )r   r{   r   r   r   r   %test_url_warning_with_bytes_and_spaceC  s    z2TestWarnings.test_url_warning_with_bytes_and_spacec          
   C   s>   t jdd}| jd}W d Q R X tdd |D  s:td S )NT)r8   z&http://www.crummyunicode.com/ is greatc             s   s   | ]}d t |jkV  qdS )zlooks more like a URLN)r@   rA   )rS   rC   r   r   r   rU   N  s   zGTestWarnings.test_url_warning_with_unicode_and_space.<locals>.<genexpr>)r=   r>   r   r~   r   )r   r{   r   r   r   r   'test_url_warning_with_unicode_and_spaceK  s    z4TestWarnings.test_url_warning_with_unicode_and_spaceN)r2   r3   r4   rf   ri   rj   rl   rm   rq   rt   rv   rG   markZparametrizerx   ry   r|   r}   r   r   r   r   r   r   ra      s&   


ra   c               @   s   e Zd Zdd ZdS )TestSelectiveParsingc             C   s.   d}t d}| j||d}|j dks*td S )Nz&No<b>Yes</b><a>No<b>Yes <c>Yes</c></b>rZ   )ro   s   <b>Yes</b><b>Yes <c>Yes</c></b>)r   r   r   r   )r   r(   Zstrainerr   r   r   r   test_parse_with_soupstrainerT  s    z1TestSelectiveParsing.test_parse_with_soupstrainerN)r2   r3   r4   r   r   r   r   r   r   R  s   r   c               @   s:   e Zd ZdZdd Zejje dddd Z	dd	 Z
d
S )
TestNewTagz(Test the BeautifulSoup.new_tag() method.c             C   sb   | j d}|jddddid}t|ts,td|jks:ttddd|jksPtd |jks^td S )Nr9   fooZbaznameza name)barattrs)r   r   )	r   new_tagr?   r   r   r   r<   r   parent)r   r   r   r   r   r   test_new_tag]  s    
zTestNewTag.test_new_tagz-lxml not installed, cannot parse XML document)reasonc             C   sB   t dd}|jd}|jd}d|j ks.td|j ks>td S )Nr9   xmlbrr[   s   <br/>s   <p/>)r   r   r   r   )r   Zxml_soupZxml_brZxml_pr   r   r   5test_xml_tag_inherits_self_closing_rules_from_buildere  s
    


z@TestNewTag.test_xml_tag_inherits_self_closing_rules_from_builderc             C   sB   t dd}|jd}|jd}d|j ks.td|j ks>td S )Nr9   zhtml.parserr   r[   s   <br/>s   <p></p>)r   r   r   r   )r   Z	html_soupZhtml_brZhtml_pr   r   r   1test_tag_inherits_self_closing_rules_from_builders  s
    


z<TestNewTag.test_tag_inherits_self_closing_rules_from_builderN)r2   r3   r4   __doc__r   rG   r   Zskipifr   r   r   r   r   r   r   r   [  s   r   c               @   s    e Zd ZdZdd Zdd ZdS )TestNewStringz+Test the BeautifulSoup.new_string() method.c             C   s2   | j d}|jd}d|ks tt|ts.td S )Nr9   r   )r   
new_stringr   r?   r   )r   r   r_   r   r   r   'test_new_string_creates_navigablestring  s    

z5TestNewString.test_new_string_creates_navigablestringc             C   s4   | j d}|jdt}d|ks"tt|ts0td S )Nr9   r   )r   r   r   r   r?   )r   r   r_   r   r   r   3test_new_string_can_create_navigablestring_subclass  s    
zATestNewString.test_new_string_can_create_navigablestring_subclassN)r2   r3   r4   r   r   r   r   r   r   r   r   }  s   r   c               @   s   e Zd Zdd Zdd ZdS )
TestPicklec             C   s2   | j d}tj|}tj|}d|jjks.td S )Nz<a>some markup</a>zsome markup)r   pickledumpsloadsrL   r   r   )r   r   pickled	unpickledr   r   r   test_normal_pickle  s    


zTestPickle.test_normal_picklec             C   s6   | j d}d |_tj|}tj|}d|jks2td S )Nzsome markup)r   r:   r   r   r   r   r   )r   r   r   r   r   r   r   test_pickle_with_no_builder  s
    


z&TestPickle.test_pickle_with_no_builderN)r2   r3   r4   r   r   r   r   r   r   r     s   r   c               @   s4   e Zd Zdd Zdd Zdd Zdd Zd	d
 ZdS )TestEncodingConversionc             C   s&   d| _ | j jd| _| jdks"td S )NuU   <html><head><meta charset="utf-8"/></head><body><foo>Sacré bleu!</foo></body></html>zutf-8sU   <html><head><meta charset="utf-8"/></head><body><foo>Sacré bleu!</foo></body></html>)unicode_datar   r    r   )r   r   r   r   setup_method  s    z#TestEncodingConversion.setup_methodc             C   s   t j}tjtj z^dd }|t _d}| j|}|j }t|tsFt	|| j
|j ks\t	|jj dksnt	W d tjtj |t _X d S )Nc             S   s   d S )Nr   )r@   r   r   r   noop  s    z>TestEncodingConversion.test_ascii_in_unicode_out.<locals>.noops   <foo>a</foo>zutf-8)r   Zchardet_dammitloggingdisableWARNINGr   decoder?   r@   r   Zdocument_forr   lowerNOTSET)r   chardetr   asciiZsoup_from_asciiZunicode_outputr   r   r   test_ascii_in_unicode_out  s    
z0TestEncodingConversion.test_ascii_in_unicode_outc             C   s@   | j | j}|j | jkst|jjdks.t|jd ks<td S )Nu   Sacré bleu!)r   r   r   r   r   r   r   )r   soup_from_unicoder   r   r   test_unicode_in_unicode_out  s    z2TestEncodingConversion.test_unicode_in_unicode_outc             C   s2   | j | j}|j | jkst|jjdks.td S )Nu   Sacré bleu!)r   r    r   r   r   r   r   )r   Zsoup_from_utf8r   r   r   test_utf8_in_unicode_out  s    z/TestEncodingConversion.test_utf8_in_unicode_outc             C   s$   | j | j}|jd| jks td S )Nzutf-8)r   r   r   r    r   )r   r   r   r   r   test_utf8_out  s    z$TestEncodingConversion.test_utf8_outN)r2   r3   r4   r   r   r   r   r   r   r   r   r   r     s
   r   )$r   pdbr   r   osr   rG   systempfileZbs4r   r   r   r   r   Zbs4.builderr   r	   r
   Zbs4.elementr   r   r   r   r9   r   r   r   r=   r   ra   r   r   r   r   r   r   r   r   r   <module>   s(    0}	"