U
    luf7                     @   s   d Z ddlZddlZddlZddlZddlmZ ddlmZm	Z	m
Z
 ddlmZ G dd deZG d	d
 d
eZG dd deZdS )z*Tests of the bs4.element.PageElement class    N)BeautifulSoup)Comment	ResultSetSoupStrainer   )SoupTestc                   @   sX   e Zd 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S )TestEncodingz0Test the ability to encode objects into strings.c                 C   s.   d}|  |}|jjdddks*td S )N
   <b>☃</b>utf-8   ☃)soupbstringencodeAssertionErrorselfhtmlr    r   >/tmp/pip-unpacked-wheel-pg4vfqbd/bs4/tests/test_pageelement.py"test_unicode_string_can_be_encoded   s    
z/TestEncoding.test_unicode_string_can_be_encodedc                 C   s,   d}|  |}|jd|dks(td S )Nr	   r
   r   r   r   r   r   r   r   r   1test_tag_containing_unicode_string_can_be_encoded   s    
z>TestEncoding.test_tag_containing_unicode_string_can_be_encodedc                 C   s&   d}|  |}|jddks"td S )Nr	   asciis   <b>&#9731;</b>r   r   r   r   r   <test_encoding_substitutes_unrecognized_characters_by_default   s    
zITestEncoding.test_encoding_substitutes_unrecognized_characters_by_defaultc              	   C   s6   d}|  |}tt |jddd W 5 Q R X d S )Nr	   r   strict)errors)r   pytestZraisesUnicodeEncodeErrorr   r   r   r   r    test_encoding_can_be_made_strict#   s    
z-TestEncoding.test_encoding_can_be_made_strictc                 C   s$   d}|  |}d|j ks td S )Nr	   r   )r   r   Zdecode_contentsr   r   r   r   r   test_decode_contents)   s    
z!TestEncoding.test_decode_contentsc                 C   s.   d}|  |}dd|jjddks*td S )Nr	   r   utf8)encoding)r   r   r   Zencode_contentsr   r   r   r   r   test_encode_contents.   s
    
z!TestEncoding.test_encode_contentsc                 C   s<   t  d }d| }| |}| }||dks8td S )Nr   <span>s   <span>)sysgetrecursionlimitr   r   countr   )r   limitmarkupr   encodedr   r   r   "test_encode_deeply_nested_document5   s
    
z/TestEncoding.test_encode_deeply_nested_documentc                 C   s2   d}|  |}|  dd|j ks.td S )Nr	   r   r!   )r   ZrenderContentsr   r   r   r   r   r   r   test_deprecated_renderContents?   s    
z+TestEncoding.test_deprecated_renderContentsc                 C   s"   d}|  |}|t|kstd S Nr	   )r   reprr   r   r   r   r   	test_reprE   s    
zTestEncoding.test_reprN)__name__
__module____qualname____doc__r   r   r   r   r    r#   r+   r,   r/   r   r   r   r   r      s   
r   c                   @   s   e Zd 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$S )%TestFormatterszrTest the formatting feature, used by methods like decode() and
    prettify(), and the formatters themselves.
    c                 C   s0   d}|  |}|jdd}|| dks,td S N#   <b>&lt;&lt;Sacré bleu!&gt;&gt;</b>minimal	formatterr   decodeZdocument_forr   r   r)   r   decodedr   r   r   !test_default_formatter_is_minimalP   s    
z0TestFormatters.test_default_formatter_is_minimalc                 C   s0   d}|  |}|jdd}|| dks,td S )N'   <br><b>&lt;&lt;Sacré bleu!&gt;&gt;</b>r   r8   z.<br/><b>&lt;&lt;Sacr&eacute; bleu!&gt;&gt;</b>r:   r<   r   r   r   test_formatter_htmlY   s    
z"TestFormatters.test_formatter_htmlc                 C   s0   d}|  |}|jdd}|| dks,td S )Nr?   html5r8   z-<br><b>&lt;&lt;Sacr&eacute; bleu!&gt;&gt;</b>r:   r<   r   r   r   test_formatter_html5a   s    
z#TestFormatters.test_formatter_html5c                 C   s0   d}|  |}|jdd}|| dks,td S r5   r:   r<   r   r   r   test_formatter_minimali   s    
z%TestFormatters.test_formatter_minimalc                 C   s0   d}|  |}|jd d}|| dks,td S )Nr6   r8   u   <b><<Sacré bleu!>></b>r:   r<   r   r   r   test_formatter_nullr   s    
z"TestFormatters.test_formatter_nullc                 C   s4   d}|  |}|jdd d}|| dks0td S )Nz!<b>&lt;foo&gt;</b><b>bar</b><br/>c                 S   s   |   S Nupperxr   r   r   <lambda>       z6TestFormatters.test_formatter_custom.<locals>.<lambda>r8   z<b><FOO></b><b>BAR</b><br/>r:   r<   r   r   r   test_formatter_custom|   s    
z$TestFormatters.test_formatter_customc                 C   s   d}|  |}|j}d}|| ks(t||jddks<td}||jddksTt||jd dkshtd}||jdd	 dkstd S )
Nu%   <a href="http://a.com?a=b&c=é">e</a>u)   <a href="http://a.com?a=b&amp;c=é">e</a>r7   r8   z/<a href="http://a.com?a=b&amp;c=&eacute;">e</a>r   u%   <a href="HTTP://A.COM?A=B&C=É">E</a>c                 S   s   |   S rE   rF   rH   r   r   r   rJ      rK   zJTestFormatters.test_formatter_is_run_on_attribute_values.<locals>.<lambda>)r   ar;   r   )r   r)   r   rM   Zexpect_minimalZexpect_htmlZexpect_upperr   r   r   )test_formatter_is_run_on_attribute_values   s    
z8TestFormatters.test_formatter_is_run_on_attribute_valuesc                 C   s"   d}t |d }d|kstd S )NzO
  <script type="text/javascript">
   console.log("< < hey > > ");
  </script>
html.parser   < < hey > >r   r   r   r   docr*   r   r   r   2test_formatter_skips_script_tag_for_html_documents   s    zATestFormatters.test_formatter_skips_script_tag_for_html_documentsc                 C   s"   d}t |d }d|kstd S )NzF
  <style type="text/css">
   console.log("< < hey > > ");
  </style>
rO   rP   rQ   rR   r   r   r   1test_formatter_skips_style_tag_for_html_documents   s    z@TestFormatters.test_formatter_skips_style_tag_for_html_documentsc                 C   s    |  d}d|j kstd S )NzN<div>  foo  <pre>  	bar
  
  </pre>  baz  <textarea> eee
fff	</textarea></div>zP<div>
 foo
 <pre>  	bar
  
  </pre>
 baz
 <textarea> eee
fff	</textarea>
</div>
r   divprettifyr   r   r   r   r   r   ,test_prettify_leaves_preformatted_text_alone   s    
z;TestFormatters.test_prettify_leaves_preformatted_text_alonec                 C   s(   d}d}|  |}||j ks$td S )NzR<div><pre><code>some
<script><pre>code</pre></script> for you 
</code></pre></div>zV<div>
 <pre><code>some
<script><pre>code</pre></script> for you 
</code></pre>
</div>
rV   )r   r)   expectr   r   r   r   0test_prettify_handles_nested_string_literal_tags   s    
z?TestFormatters.test_prettify_handles_nested_string_literal_tagsc                 C   s*   t dd}|jdd d}d|ks&td S )Nz<html><body>foo</body></html>rO   c                 S   s   |   S rE   rF   rH   r   r   r   rJ      rK   zITestFormatters.test_prettify_accepts_formatter_function.<locals>.<lambda>r8   ZFOO)r   rX   r   )r   r   prettyr   r   r   (test_prettify_accepts_formatter_function   s    
z7TestFormatters.test_prettify_accepts_formatter_functionc                 C   s"   |  d}tt| kstd S )N<a></a>)r   strtyperX   r   rY   r   r   r   (test_prettify_outputs_unicode_by_default   s    
z7TestFormatters.test_prettify_outputs_unicode_by_defaultc                 C   s$   |  d}tt|dks td S )Nr_   r
   )r   bytesra   rX   r   rY   r   r   r   test_prettify_can_encode_data   s    
z,TestFormatters.test_prettify_can_encode_datac                 C   s0   d}|  |}|jd}||dks,td S )Nu   <b>Sacré bleu!</b>r
   r   )r   r)   r   r*   r   r   r   ,test_html_entity_substitution_off_by_default   s    
z;TestFormatters.test_html_entity_substitution_off_by_defaultc                 C   s   d}|  |}|jd dks t|d}d|ks6t|d}d|ksLt|d}d	|ksbt|d
d
}d|ks~td S )NzE<meta content="text/html; charset=x-sjis" http-equiv="Content-type"/>contentztext/html; charset=x-sjisr
   s   charset=utf-8euc_jps   charset=euc_jpz	shift-jiss   charset=shift-jiszutf-16zcharset=utf-16)r   metar   r   r;   )r   Zmeta_tagr   utf_8rg   	shift_jisZutf_16_ur   r   r   test_encoding_substitution   s    



z)TestFormatters.test_encoding_substitutionc                 C   s2   d}t d}| j||d}|jd jdks.td S )Nz`<head><meta content="text/html; charset=x-sjis" http-equiv="Content-type"/></head><pre>foo</pre>pre)Z
parse_onlyr   )r   r   contentsnamer   )r   r)   Zstrainerr   r   r   r   ;test_encoding_substitution_doesnt_happen_if_tag_is_strained   s    zJTestFormatters.test_encoding_substitution_doesnt_happen_if_tag_is_strainedN)r0   r1   r2   r3   r>   r@   rB   rC   rD   rL   rN   rT   rU   rZ   r\   r^   rb   rd   re   rk   ro   r   r   r   r   r4   K   s$   		
		r4   c                   @   sh   e Zd 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S )TestPersistencez*Testing features like pickle and deepcopy.c                 C   s   d| _ | | j | _d S )Nay  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
"http://www.w3.org/TR/REC-html40/transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Beautiful Soup: We called him Tortoise because he taught us.</title>
<link rev="made" href="mailto:leonardr@segfault.org">
<meta name="Description" content="Beautiful Soup: an HTML parser optimized for screen-scraping.">
<meta name="generator" content="Markov Approximation 1.4 (module: leonardr)">
<meta name="author" content="Leonard Richardson">
</head>
<body>
<a href="foo">foo</a>
<a href="foo"><b>bar</b></a>
</body>
</html>)pager   tree)r   r   r   r   setup_method   s    zTestPersistence.setup_methodc                 C   s@   t | jd}t |}|jtks&t| | j ks<td S )N   )pickledumpsrr   loads	__class__r   r   r;   )r   dumpedloadedr   r   r   !test_pickle_and_unpickle_identity  s    
z1TestPersistence.test_pickle_and_unpickle_identityc                 C   s&   t | j}| | j ks"td S rE   )copydeepcopyrr   r;   r   )r   copiedr   r   r   test_deepcopy_identity  s    z&TestPersistence.test_deepcopy_identityc                 C   s6   t  d }d| }| |}t|}t|}d S )Nr   r$   )r%   r&   r   r|   r}   )r   r(   r)   r   r~   r   r   r    test_copy_deeply_nested_document  s
    

z0TestPersistence.test_copy_deeply_nested_documentc                 C   s:   t dd}|j}| }dt|ks(t||jks6td S )Ns   <p>&nbsp;</p>rO   u	   <p> </p>)r   Zoriginal_encoding__copy__r`   r   )r   r   r"   r|   r   r   r   test_copy_preserves_encoding+  s
    
z,TestPersistence.test_copy_preserves_encodingc                 C   s   |  dj}d|_d|_| }|j|jks0t|j|jks@t|j|jksPt|j|jks`t|j|jkspt|j	|j	kstd S )Nz<p></p>
   !   )
r   pZ
sourcelineZ	sourceposr   r   Zcan_be_empty_elementZcdata_list_attributesZpreserve_whitespace_tagsZinteresting_string_types)r   tagr~   r   r   r   'test_copy_preserves_builder_information2  s    z7TestPersistence.test_copy_preserves_builder_informationc                 C   s>   d}|  |}t|tj}t|}| | ks:td S r-   )r   ru   rv   HIGHEST_PROTOCOLrw   r;   r   )r   r   r   ry   rz   r   r   r   test_unicode_pickleE  s
    

z#TestPersistence.test_unicode_picklec                 C   sz   d}|  |}|jdd}t|}||ks0td |jks>td |jksLtd |jksZtd |jkshtd |jksvtd S )Nz<b>Foo<a></a></b><b>Bar</b>ZFoor   )r   findr|   r   parentnext_elementZnext_siblingprevious_elementr   r   r   s1s2r   r   r   1test_copy_navigablestring_is_not_attached_to_treeM  s    

zATestPersistence.test_copy_navigablestring_is_not_attached_to_treec                 C   s<   d}|  |}|j}t|}||ks*tt|ts8td S )Nz<b><!--Foo--></b>)r   r   r|   r   
isinstancer   r   r   r   r   0test_copy_navigablestring_subclass_has_same_typeY  s    

z@TestPersistence.test_copy_navigablestring_subclass_has_same_typec                 C   s(   d}|  |}t|}||ks$td S )N)<div><b>Foo<a></a></b><b>Bar</b></div>end)r   r|   r   )r   r   r   Z	soup_copyr   r   r   test_copy_entire_soupa  s    

z%TestPersistence.test_copy_entire_soupc                 C   s   d}|  |}|j}t|}t|t|ks2t||ks>t||k	sJtd |jksXtd |jksftd |jddjks|td |jddjkstd S )Nr   Barr   )	r   rW   r|   r`   r   r   r   r   r   )r   r   r   rW   Zdiv_copyr   r   r   test_copy_tag_copies_contentsg  s    

z-TestPersistence.test_copy_tag_copies_contentsN)r0   r1   r2   r3   rs   r{   r   r   r   r   r   r   r   r   r   r   r   r   r   rp      s   rp   )r3   r|   ru   r   r%   Zbs4r   Zbs4.elementr   r   r    r   r   r4   rp   r   r   r   r   <module>   s   : 2