a
    O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*J d S )N
   <b>☃</b>utf-8   ☃)soupbstringencodeselfhtmlr    r   S/var/www/ai-form-bot/venv/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sJ 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sJ 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sJ 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