
­­­­­­­­­­­­­­­­­­
<!DOCTYPE html>
<html>
3
*6]dO                 @   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mZm	Z	 ddl
mZmZmZmZ ddlZddlmZmZmZ ddlmZmZ ddlZyddlmZmZ d	ZW n& ek
r Z z
d
ZW Y ddZ[X nX ejd dkoejdk 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j"Z#G dd deZ$G dd dej"Z%G dd deZ&G dd dej"Z'dS )z#Tests of Beautiful Soup as a whole.    )	set_traceN)BeautifulSoupBeautifulStoneSoup)CharsetMetaAttributeValueContentMetaAttributeValueSoupStrainerNamespacedAttribute)EntitySubstitutionUnicodeDammitEncodingDetector)SoupTestskipIf)LXMLTreeBuilderLXMLTreeBuilderForXMLTF      c               @   s$   e Zd Zdd Zdd Zdd ZdS )TestConstructorc             C   s"   d}| j |}| jd|jj d S )Nu   <h1>éé</h1>u   éé)soupassertEqualh1string)selfdatar    r   /usr/lib/python3.6/test_soup.pytest_short_unicode_input*   s    
z(TestConstructor.test_short_unicode_inputc             C   s"   d}| j |}| jd|jj 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}| jd|j d S )Nu   Räksmörgåszutf-8)exclude_encodingszwindows-1252)encoder   r   original_encoding)r   	utf8_datar   r   r   r   test_exclude_encodings4   s    
z&TestConstructor.test_exclude_encodingsN)__name__
__module____qualname__r   r   r!   r   r   r   r   r   (   s   r   c               @   sF   e Zd ZdddZdd Zdd Zdd	 Zd
d Zdd Zdd Z	dS )TestWarningsTc             C   s"   |j tjd d }| j| d S )NP   )
startswithr   ZNO_PARSER_SPECIFIED_WARNING
assertTrue)r   sZis_therevr   r   r   _no_parser_specified<   s    z!TestWarnings._no_parser_specifiedc          
   C   s>   t jdd}| jd}W d Q R X t|d j}| j| d S )NT)recordz<a><b></b></a>r   )warningscatch_warningsr   strmessage_assert_no_parser_specified)r   wr   msgr   r   r   #test_warning_if_no_parser_specified@   s    z0TestWarnings.test_warning_if_no_parser_specifiedc          
   C   s@   t jdd}| jdd}W d Q R X t|d j}| j| d S )NT)r,   z<a><b></b></a>htmlr   )r-   r.   r   r/   r0   r1   )r   r2   r   r3   r   r   r   *test_warning_if_parser_specified_too_vagueF   s    z7TestWarnings.test_warning_if_parser_specified_too_vaguec          
   C   s4   t jdd}| jdd}W d Q R X | jg | d S )NT)r,   z<a><b></b></a>zhtml.parser)r-   r.   r   r   )r   r2   r   r   r   r   ,test_no_warning_if_explicit_parser_specifiedL   s    z9TestWarnings.test_no_warning_if_explicit_parser_specifiedc             C   sh   t jdd}| jdtdd}W d Q R X t|d j}| jd|k | jd|k | jd	|j  d S )
NT)r,   z<a><b></b></a>b)parseOnlyTheser   r9   
parse_onlys   <b></b>)	r-   r.   r   r   r/   r0   r(   r   r   )r   r2   r   r3   r   r   r   )test_parseOnlyThese_renamed_to_parse_onlyQ   s    z6TestWarnings.test_parseOnlyThese_renamed_to_parse_onlyc             C   sf   t jdd}d}| j|dd}W d Q R X t|d j}| jd|k | jd|k | jd|j d S )	NT)r,   s   éutf8)fromEncodingr   r=   Zfrom_encoding)r-   r.   r   r/   r0   r(   r   r   )r   r2   r<   r   r3   r   r   r   *test_fromEncoding_renamed_to_from_encodingY   s    z7TestWarnings.test_fromEncoding_renamed_to_from_encodingc             C   s   | j t| jddd d S )Nz<a>T)Zno_such_argument)assertRaises	TypeErrorr   )r   r   r   r   "test_unrecognized_keyword_argumentb   s    z/TestWarnings.test_unrecognized_keyword_argumentN)T)
r"   r#   r$   r+   r4   r6   r7   r;   r>   rA   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 )r%   c             C   s   t j }|j}zBtjdd}| j|}W d Q R X t|d j}| jd|k W d |j	  X tjdd}| j|}W d Q R X | j
dt| d S )NT)r,   r   zlooks like a filename)tempfileZNamedTemporaryFilenamer-   r.   r   r/   r0   r(   closer   len)r   Z
filehandlefilenamer2   r   r3   r   r   r   test_disk_file_warningh   s    
z#TestWarnings.test_disk_file_warningc             C   s>   t jdd}| jd}W d Q R X | jtdd |D  d S )NT)r,   s   http://www.crummybytes.com/c             s   s   | ]}d t |jkV  qdS )zlooks like a URLN)r/   r0   ).0r2   r   r   r   	<genexpr>}   s   z?TestWarnings.test_url_warning_with_bytes_url.<locals>.<genexpr>)r-   r.   r   r(   any)r   warning_listr   r   r   r   test_url_warning_with_bytes_urlx   s    z,TestWarnings.test_url_warning_with_bytes_urlc             C   s>   t jdd}| jd}W d Q R X | jtdd |D  d S )NT)r,   zhttp://www.crummyunicode.com/c             s   s   | ]}d t |jkV  qdS )zlooks like a URLN)r/   r0   )rH   r2   r   r   r   rI      s   zATestWarnings.test_url_warning_with_unicode_url.<locals>.<genexpr>)r-   r.   r   r(   rJ   )r   rK   r   r   r   r   !test_url_warning_with_unicode_url   s    z.TestWarnings.test_url_warning_with_unicode_urlc             C   s>   t jdd}| jd}W d Q R X | jtdd |D  d S )NT)r,   s$   http://www.crummybytes.com/ is greatc             s   s   | ]}d t |jkV  qdS )zlooks like a URLN)r/   r0   )rH   r2   r   r   r   rI      s   zETestWarnings.test_url_warning_with_bytes_and_space.<locals>.<genexpr>)r-   r.   r   assertFalserJ   )r   rK   r   r   r   r   %test_url_warning_with_bytes_and_space   s    z2TestWarnings.test_url_warning_with_bytes_and_spacec             C   s>   t jdd}| jd}W d Q R X | jtdd |D  d S )NT)r,   z%http://www.crummyuncode.com/ is greatc             s   s   | ]}d t |jkV  qdS )zlooks like a URLN)r/   r0   )rH   r2   r   r   r   rI      s   zGTestWarnings.test_url_warning_with_unicode_and_space.<locals>.<genexpr>)r-   r.   r   rN   rJ   )r   rK   r   r   r   r   'test_url_warning_with_unicode_and_space   s    z4TestWarnings.test_url_warning_with_unicode_and_spaceN)r"   r#   r$   rG   rL   rM   rO   rP   r   r   r   r   r%   f   s
   c               @   s   e Zd Zdd ZdS )TestSelectiveParsingc             C   s.   d}t d}| j||d}| j|j d d S )Nz&No<b>Yes</b><a>No<b>Yes <c>Yes</c></b>r8   )r:   s   <b>Yes</b><b>Yes <c>Yes</c></b>)r   r   r   r   )r   markupZstrainerr   r   r   r   test_parse_with_soupstrainer   s    z1TestSelectiveParsing.test_parse_with_soupstrainerN)r"   r#   r$   rS   r   r   r   r   rQ      s   rQ   c               @   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d Zdd Zdd Zdd ZdS )TestEntitySubstitutionz1Standalone tests of the EntitySubstitution class.c             C   s
   t | _d S )N)r	   sub)r   r   r   r   setUp   s    zTestEntitySubstitution.setUpc             C   s   d}| j | jj|d d S )Nu   foo\u2200☃\u00f5baru   foo&forall;☃&otilde;bar)r   rU   substitute_html)r   r)   r   r   r   test_simple_html_substitution   s    z4TestEntitySubstitution.test_simple_html_substitutionc             C   s&   d}t |}| j| jj|jd d S )Ns   fooz&lsquo;&rsquo;foo&ldquo;&rdquo;)r
   r   rU   rW   rR   )r   Zquotesdammitr   r   r   test_smart_quote_substitution   s    z4TestEntitySubstitution.test_smart_quote_substitutionc             C   s   d}| j | jj|d| d S )NzWelcome to "my bar"F)r   rU   substitute_xml)r   r)   r   r   r   Itest_xml_converstion_includes_no_quotes_if_make_quoted_attribute_is_false   s    z`TestEntitySubstitution.test_xml_converstion_includes_no_quotes_if_make_quoted_attribute_is_falsec             C   s0   | j | jjddd | j | jjddd d S )NZWelcomeTz	"Welcome"z	Bob's Barz"Bob's Bar")r   rU   r[   )r   r   r   r   6test_xml_attribute_quoting_normally_uses_double_quotes   s    zMTestEntitySubstitution.test_xml_attribute_quoting_normally_uses_double_quotesc             C   s   d}| j | jj|dd d S )NzWelcome to "my bar"Tz'Welcome to "my bar"')r   rU   r[   )r   r)   r   r   r   Otest_xml_attribute_quoting_uses_single_quotes_when_value_contains_double_quotes   s    zfTestEntitySubstitution.test_xml_attribute_quoting_uses_single_quotes_when_value_contains_double_quotesc             C   s   d}| j | jj|dd d S )NzWelcome to "Bob's Bar"Tz""Welcome to &quot;Bob's Bar&quot;")r   rU   r[   )r   r)   r   r   r   btest_xml_attribute_quoting_escapes_single_quotes_when_value_contains_both_single_and_double_quotes   s    zyTestEntitySubstitution.test_xml_attribute_quoting_escapes_single_quotes_when_value_contains_both_single_and_double_quotesc             C   s   d}| j | jj|| d S )NzWelcome to "Bob's Bar")r   rU   r[   )r   Zquotedr   r   r   <test_xml_quotes_arent_escaped_when_value_is_not_being_quoted   s    zSTestEntitySubstitution.test_xml_quotes_arent_escaped_when_value_is_not_being_quotedc             C   s   | j | jjdd d S )Nzfoo<bar>zfoo&lt;bar&gt;)r   rU   r[   )r   r   r   r   'test_xml_quoting_handles_angle_brackets   s    
z>TestEntitySubstitution.test_xml_quoting_handles_angle_bracketsc             C   s   | j | jjdd d S )NzAT&TzAT&amp;T)r   rU   r[   )r   r   r   r   #test_xml_quoting_handles_ampersands   s    z:TestEntitySubstitution.test_xml_quoting_handles_ampersandsc             C   s   | j | jjdd d S )Nz&Aacute;T&Tz&amp;Aacute;T&amp;T)r   rU   r[   )r   r   r   r   Etest_xml_quoting_including_ampersands_when_they_are_part_of_an_entity   s    
z\TestEntitySubstitution.test_xml_quoting_including_ampersands_when_they_are_part_of_an_entityc             C   s   | j | jjdd d S )Nz&Aacute;T&Tz&Aacute;T&amp;T)r   rU   Z"substitute_xml_containing_entities)r   r   r   r   Dtest_xml_quoting_ignoring_ampersands_when_they_are_part_of_an_entity   s    
z[TestEntitySubstitution.test_xml_quoting_ignoring_ampersands_when_they_are_part_of_an_entityc             C   s   d}| j | jj|| dS )z:There's no need to do this except inside attribute values.zBob's "bar"N)r   rU   rW   )r   textr   r   r    test_quotes_not_html_substituted   s    z7TestEntitySubstitution.test_quotes_not_html_substitutedN)r"   r#   r$   __doc__rV   rX   rZ   r\   r]   r^   r_   r`   ra   rb   rc   rd   rf   r   r   r   r   rT      s   rT   c                   sN   e Zd Z fddZdd Zdd Zdd Zd	d
 Zee	ddd Z
  ZS )TestEncodingConversionc                s4   t t| j  d| _| jjd| _| j| jd 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>)superrh   rV   unicode_datar   r    r   )r   )	__class__r   r   rV      s    zTestEncodingConversion.setUpc             C   s   t jj}tjtj zbdd }|t j_d}| j|}|j }| jt	|t
 | j|| j|j  | j|jj d W d tjtj |t j_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)bs4rY   chardet_dammitloggingdisableWARNINGr   decoder(   
isinstancer/   r   Zdocument_forr   lowerNOTSET)r   chardetrl   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 | j | j|jjd | j|jd  d S )Nu   Sacré bleu!)r   rj   r   rr   foor   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 | j | j|jjd d S )Nu   Sacré bleu!)r   r    r   rr   rj   ry   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|jd| j d S )Nzutf-8)r   rj   r   r   r    )r   rz   r   r   r   test_utf8_out  s    z$TestEncodingConversion.test_utf8_outzQBad HTMLParser detected; skipping test of non-ASCII characters in attribute name.c             C   s(   d}| j | j|jjd|jd d S )Nu    <div><a ☃="snowman"></a></div>r<   )r   r   Zdivr   )r   rR   r   r   r   1test_attribute_name_containing_unicode_characters  s    zHTestEncodingConversion.test_attribute_name_containing_unicode_characters)r"   r#   r$   rV   rx   r{   r|   r}   r   PYTHON_3_PRE_3_2r~   __classcell__r   r   )rk   r   rh      s   	rh   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 )%TestUnicodeDammitz"Standalone tests of UnicodeDammit.c             C   s   d}t |}| j|j| d S )Nu   I'm already Unicode! ☃)r
   r   unicode_markup)r   rR   rY   r   r   r   test_unicode_input"  s    z$TestUnicodeDammit.test_unicode_inputc             C   s   d}t |}| j|jd d S )Ns   <foo></foo>z#<foo>\u2018\u2019\u201c\u201d</foo>)r
   r   r   )r   rR   rY   r   r   r   test_smart_quotes_to_unicode'  s    z.TestUnicodeDammit.test_smart_quotes_to_unicodec             C   s"   d}t |dd}| j|jd d S )Ns   <foo></foo>Zxml)smart_quotes_toz+<foo>&#x2018;&#x2019;&#x201C;&#x201D;</foo>)r
   r   r   )r   rR   rY   r   r   r   !test_smart_quotes_to_xml_entities-  s    z3TestUnicodeDammit.test_smart_quotes_to_xml_entitiesc             C   s"   d}t |dd}| j|jd d S )Ns   <foo></foo>r5   )r   z'<foo>&lsquo;&rsquo;&ldquo;&rdquo;</foo>)r
   r   r   )r   rR   rY   r   r   r   "test_smart_quotes_to_html_entities3  s    z4TestUnicodeDammit.test_smart_quotes_to_html_entitiesc             C   s"   d}t |dd}| j|jd d S )Ns   <foo></foo>rw   )r   z<foo>''""</foo>)r
   r   r   )r   rR   rY   r   r   r   test_smart_quotes_to_ascii9  s    z,TestUnicodeDammit.test_smart_quotes_to_asciic             C   s0   d}t |}| j|jj d | j|jd d S )Ns   Sacré bleu! ☃zutf-8u   Sacré bleu! ☃)r
   r   r   rt   r   )r   r<   rY   r   r   r   test_detect_utf8?  s    z"TestUnicodeDammit.test_detect_utf8c             C   s4   d}t |dg}| j|jj d | j|jd d S )Ns   z
iso-8859-8z\u05dd\u05d5\u05dc\u05e9)r
   r   r   rt   r   )r   hebrewrY   r   r   r   test_convert_hebrewF  s    z%TestUnicodeDammit.test_convert_hebrewc             C   s6   d}t |}| j|jj d | j|jjd| d S )Ns   ケータイ Watchzutf-8)r
   r   r   rt   r   r   )r   utf_8rY   r   r   r   /test_dont_see_smart_quotes_where_there_are_noneL  s    zATestUnicodeDammit.test_dont_see_smart_quotes_where_there_are_nonec             C   s,   dj d}t|dg}| j|jj d d S )Nu   Räksmörgåszutf-8z
iso-8859-8)r   r
   r   r   rt   )r   r    rY   r   r   r    test_ignore_inappropriate_codecsR  s    
z2TestUnicodeDammit.test_ignore_inappropriate_codecsc             C   s:   dj d}x*dD ]"}t||g}| j|jj d qW d S )Nu   Räksmörgåszutf-8.utf8...
utF---16.!)r   r   r   )r   r
   r   r   rt   )r   r    Zbad_encodingrY   r   r   r   test_ignore_invalid_codecsW  s    

z,TestUnicodeDammit.test_ignore_invalid_codecsc             C   sL   dj d}t|dgd}| j|jj d t|ddgd}| j|jd  d S )Nu   Räksmörgåszutf-8)r   zwindows-1252)r   r
   r   r   rt   )r   r    rY   r   r   r   r!   ]  s    
z(TestUnicodeDammit.test_exclude_encodingsc             C   s"   t d}t|j}d|kstd S )Ns'   <?xml version="1.0" encoding="UTF-" ?>u   utf-�)r   list	encodingsAssertionError)r   Zdetectedr   r   r   r   Ptest_encoding_detector_replaces_junk_in_encoding_name_with_replacement_characterk  s    
zbTestUnicodeDammit.test_encoding_detector_replaces_junk_in_encoding_name_with_replacement_characterc             C   s,   x&dD ]}t |dd}| jd|j qW d S )	N&   <html><meta charset="euc-jp" /></html>&   <html><meta charset='euc-jp' /></html>$   <html><meta charset=euc-jp /></html>#   <html><meta charset=euc-jp/></html>T)Zis_htmlzeuc-jp)r   r   r   r   )r
   r   r   )r   r   rY   r   r   r    test_detect_html5_style_meta_tagq  s       z2TestUnicodeDammit.test_detect_html5_style_meta_tagc             C   s   d}t jj}tjtj zPdd }|t j_t|}| jd|j | j	d|j
k t|d}| j	|j W d tjtj |t j_X d S )NsT   ﻿<?xml version="1.0" encoding="UTF-8"?>
<html><b>بتر</b>
<i>ѐ</i></html>c             S   s   d S )Nr   )r/   r   r   r   rl     s    zBTestUnicodeDammit.test_last_ditch_entity_replacement.<locals>.noopTz\ufffdzhtml.parser)rm   rY   rn   ro   rp   rq   r
   r   Zcontains_replacement_charactersr(   r   r   ru   )r   docrv   rl   rY   r   r   r   r   "test_last_ditch_entity_replacement|  s    
z4TestUnicodeDammit.test_last_ditch_entity_replacementc             C   s,   d}t |}| jd|j | jd|j d S )Ns   < a >   < / a > u   <a>áé</a>zutf-16le)r
   r   r   r   )r   r   rY   r   r   r   test_byte_order_mark_removed  s    z.TestUnicodeDammit.test_byte_order_mark_removedc             C   sP   dj d}dj d}|| | }| jt|jd tj|}| jd|jd d S )Nu   ☃r   r<   u   “Hi, I like Windows!”windows_1252u+   ☃☃☃“Hi, I like Windows!”☃☃☃u	   ☃☃☃)r   r?   UnicodeDecodeErrorrr   r
   	detwingler   )r   r<   r   r   Zfixedr   r   r   test_detwingle  s    

z TestUnicodeDammit.test_detwinglec             C   sB   x<dD ]4}|j d}| j|jd tj|}| j|| qW d S )N   œ   ₓ   ðr<      )r   r   r   )r   r(   endswithr
   r   r   )r   Ztricky_unicode_charinputoutputr   r   r   +test_detwingle_ignores_multibyte_characters  s      

z=TestUnicodeDammit.test_detwingle_ignores_multibyte_charactersN)r"   r#   r$   rg   r   r   r   r   r   r   r   r   r   r   r!   r   r   r   r   r   r   r   r   r   r   r     s$   #r   c               @   s$   e Zd Zdd Zdd Zdd ZdS )TestNamedspacedAttributec             C   s   t dd }| j|d d S )NZxmlns)r   r   )r   ar   r   r   test_name_may_be_none  s    
z.TestNamedspacedAttribute.test_name_may_be_nonec             C   s   t dd}| jd| d S )Nr   r8   za:b)r   r   )r   r   r   r   r   6test_attribute_is_equivalent_to_colon_separated_string  s    
zOTestNamedspacedAttribute.test_attribute_is_equivalent_to_colon_separated_stringc             C   sp   t ddd}t ddd}| j|| t ddd }| j|| t ddd}| j|| t ddd}| j|| d S )Nr   r8   cz)r   r   ZassertNotEqual)r   r   r8   r   der   r   r   ;test_attributes_are_equivalent_if_prefix_and_name_identical  s    zTTestNamedspacedAttribute.test_attributes_are_equivalent_if_prefix_and_name_identicalN)r"   r#   r$   r   r   r   r   r   r   r   r     s   r   c               @   s   e Zd Zdd Zdd ZdS ))TestAttributeValueWithCharsetSubstitutionc             C   s8   t d}| jd| | jd|j | jd|jd d S )Nzeuc-jpr<   )r   r   original_valuer   )r   valuer   r   r   !test_content_meta_attribute_value  s    zKTestAttributeValueWithCharsetSubstitution.test_content_meta_attribute_valuec             C   s8   t d}| jd| | jd|j | jd|jd d S )Nztext/html; charset=euc-jpztext/html; charset=utf8r<   )r   r   r   r   )r   r   r   r   r   r     s    N)r"   r#   r$   r   r   r   r   r   r     s   r   )r   r   )(rg   Zpdbr   ro   ZunittestsysrB   rm   r   r   Zbs4.elementr   r   r   r   Z
bs4.dammitr	   r
   r   Zbs4.testingr   r   r-   Zbs4.builderr   r   ZLXML_PRESENTImportErrorr   version_infor   r   r%   rQ   ZTestCaserT   rh   r   r   r   r   r   r   r   <module>   s6   ,/	E< /