a
    ÷£BeÂ7  ã                   @   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*J ‚d S )Nõ
   <b>â˜ƒ</b>úutf-8õ   â˜ƒ)ÚsoupÚbÚstringÚencode©ÚselfÚhtmlr   © r   ú]/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/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(J ‚d S )Nr	   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"J ‚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   sJ   d}|   |¡}t t¡� |jddd� W d   ƒ n1 s<0    Y  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 J ‚d S )Nr	   r   )r   r   Zdecode_contentsr   r   r   r   Útest_decode_contents)   s    
z!TestEncoding.test_decode_contentsc                 C   s.   d}|   |¡}d d¡|jjdd�ks*J ‚d S )Nr	   r   Úutf8)Úencoding)r   r   r   Zencode_contentsr   r   r   r   Útest_encode_contents.   s
    
ÿz!TestEncoding.test_encode_contentsc                 C   s<   t  ¡ d }d| }|  |¡}| ¡ }|| d¡ks8J ‚d S )Nr   ú<span>s   <span>)ÚsysÚgetrecursionlimitr   r   Úcount)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.J ‚d S )Nr	   r   r    )r   ZrenderContentsr   r   r   r   r   r   Útest_deprecated_renderContents?   s    
z+TestEncoding.test_deprecated_renderContentsc                 C   s"   d}|   |¡}|t|ƒksJ ‚d S ©Nr	   )r   Úreprr   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,J ‚d S ©Nõ#   <b>&lt;&lt;SacrÃ© bleu!&gt;&gt;</b>Úminimal©Ú	formatter©r   ÚdecodeZdocument_for©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,J ‚d S )Nõ'   <br><b>&lt;&lt;SacrÃ© bleu!&gt;&gt;</b>r   r7   z.<br/><b>&lt;&lt;Sacr&eacute; bleu!&gt;&gt;</b>r9   r;   r   r   r   Útest_formatter_htmlY   s    
ÿz"TestFormatters.test_formatter_htmlc                 C   s0   d}|   |¡}|jdd�}||  d¡ks,J ‚d S )Nr>   Úhtml5r7   z-<br><b>&lt;&lt;Sacr&eacute; bleu!&gt;&gt;</b>r9   r;   r   r   r   Útest_formatter_html5a   s    
ÿz#TestFormatters.test_formatter_html5c                 C   s0   d}|   |¡}|jdd�}||  d¡ks,J ‚d S r4   r9   r;   r   r   r   Útest_formatter_minimali   s    
ÿz%TestFormatters.test_formatter_minimalc                 C   s0   d}|   |¡}|jd d�}||  d¡ks,J ‚d S )Nr5   r7   u   <b><<SacrÃ© bleu!>></b>r9   r;   r   r   r   Útest_formatter_nullr   s    
ÿz"TestFormatters.test_formatter_nullc                 C   s4   d}|   |¡}|jdd„ d�}||  d¡ks0J ‚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>r7   z<b><FOO></b><b>BAR</b><br/>r9   r;   r   r   r   Útest_formatter_custom|   s    
z$TestFormatters.test_formatter_customc                 C   sˆ   d}|   |¡}|j}d}|| ¡ ks(J ‚||jdd�ks<J ‚d}||jdd�ksTJ ‚||jd d�kshJ ‚d}||jdd	„ d�ks„J ‚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>r6   r7   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 rD   rE   rG   r   r   r   rI   “   rJ   zJTestFormatters.test_formatter_is_run_on_attribute_values.<locals>.<lambda>)r   Úar:   )r   r(   r   rL   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|v sJ ‚d S )NzO
  <script type="text/javascript">
   console.log("< < hey > > ");
  </script>
úhtml.parseró   < < hey > >©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|v sJ ‚d S )NzF
  <style type="text/css">
   console.log("< < hey > > ");
  </style>
rN   rO   rP   rQ   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J ‚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Úprettify©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$J ‚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>
rU   )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|v s&J ‚d S )Nz<html><body>foo</body></html>rN   c                 S   s   |   ¡ S rD   rE   rG   r   r   r   rI   Ç   rJ   zITestFormatters.test_prettify_accepts_formatter_function.<locals>.<lambda>r7   ZFOO)r   rW   )r   r   Úprettyr   r   r   Ú(test_prettify_accepts_formatter_functionÅ   s    
z7TestFormatters.test_prettify_accepts_formatter_functionc                 C   s"   |   d¡}tt| ¡ ƒksJ ‚d S )Nú<a></a>)r   ÚstrÚtyperW   rX   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 J ‚d S )Nr^   r
   )r   Úbytesr`   rW   rX   r   r   r   Útest_prettify_can_encode_dataÎ   s    
z,TestFormatters.test_prettify_can_encode_datac                 C   s0   d}|   |¡}|j d¡}|| d¡ks,J ‚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 J ‚| d¡}d|v s6J ‚| d¡}d|v sLJ ‚| d¡}d	|v sbJ ‚| d
¡ d
¡}d|v s~J ‚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   Zmeta_tagr   Úutf_8rf   Ú	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.J ‚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Úname)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)r/   r0   r1   r2   r=   r?   rA   rB   rC   rK   rM   rS   rT   rY   r[   r]   ra   rc   rd   rj   rn   r   r   r   r   r3   K   s$   		
		r3   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&J ‚| ¡ | j ¡ ks<J ‚d S )Né   )ÚpickleÚdumpsrq   ÚloadsÚ	__class__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"J ‚d S rD   )ÚcopyÚdeepcopyrq   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(J ‚||jks6J ‚d S )Ns   <p>&nbsp;</p>rN   u	   <p>Â </p>)r   Zoriginal_encodingÚ__copy__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s0J ‚|j|jks@J ‚|j|jksPJ ‚|j|jks`J ‚|j|jkspJ ‚|j|jks€J ‚d S )Nz<p></p>é
   é!   )	r   ÚpZ
sourcelineZ	sourceposr€   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:J ‚d S r,   )r   rt   ru   ÚHIGHEST_PROTOCOLrv   r:   )r   r   r   rx   ry   r   r   r   Útest_unicode_pickleE  s
    

z#TestPersistence.test_unicode_picklec                 C   sz   d}|   |¡}|jdd�}t |¡}||ks0J ‚d |jks>J ‚d |jksLJ ‚d |jksZJ ‚d |jkshJ ‚d |jksvJ ‚d S )Nz<b>Foo<a></a></b><b>Bar</b>ZFoo©r   )r   Úfindr{   Ú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*J ‚t|tƒs8J ‚d S )Nz<b><!--Foo--></b>)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$J ‚d S )Nú)<div><b>Foo<a></a></b><b>Bar</b></div>end)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s2J ‚||ks>J ‚||usJJ ‚d |jksXJ ‚d |jksfJ ‚d |jdd�jks|J ‚d |jdd�jks’J ‚d S )Nr”   ZBarr‰   )r   rV   r{   r_   r‹   r�   rŠ   rŒ   )r   r   r   rV   Zdiv_copyr   r   r   Útest_copy_tag_copies_contentsg  s    

z-TestPersistence.test_copy_tag_copies_contentsN)r/   r0   r1   r2   rr   rz   r~   r   r�   r†   rˆ   r‘   r“   r•   r–   r   r   r   r   ro   ü   s   ro   )r2   r{   rt   r   r$   Zbs4r   Zbs4.elementr   r   r   Ú r   r   r3   ro   r   r   r   r   Ú<module>   s   : 2