Current File : //lib/python3.6/site-packages/bs4/tests/__pycache__/test_soup.cpython-36.pyc
3

*6]dO�@s~dZddlmZddlZddlZddlZddlZddlmZm	Z	ddl
mZmZm
Z
mZddlZddlmZmZmZddlmZmZddlZyddlmZmZd	ZWn&ek
r�Zz
d
ZWYddZ[XnXejddko�ejdkZGd
d�de�ZGdd�de�Z Gdd�de�Z Gdd�de�Z!Gdd�dej"�Z#Gdd�de�Z$Gdd�dej"�Z%Gdd�de�Z&Gdd�dej"�Z'dS)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$eZdZdd�Zdd�Zdd�ZdS)�TestConstructorcCs"d}|j|�}|jd|jj�dS)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_inputcCs"d}|j|�}|jd|jj�dS)Nz<h1>foobar</h1>zfoobar)rrrr)rrrrrr�test_embedded_null/s
z"TestConstructor.test_embedded_nullcCs,djd�}|j|dgd�}|jd|j�dS)Nu
Räksmörgåszutf-8)�exclude_encodingszwindows-1252)�encoderr�original_encoding)r�	utf8_datarrrr�test_exclude_encodings4s
z&TestConstructor.test_exclude_encodingsN)�__name__�
__module__�__qualname__rrr!rrrrr(src@sFeZdZddd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Zdd�Z	dS)�TestWarningsTcCs"|jtjdd��}|j|�dS)N�P)�
startswithrZNO_PARSER_SPECIFIED_WARNING�
assertTrue)r�sZis_there�vrrr�_no_parser_specified<sz!TestWarnings._no_parser_specifiedc
Cs>tjdd��}|jd�}WdQRXt|dj�}|j|�dS)NT)�recordz<a><b></b></a>r)�warnings�catch_warningsr�str�message�_assert_no_parser_specified)r�wr�msgrrr�#test_warning_if_no_parser_specified@sz0TestWarnings.test_warning_if_no_parser_specifiedc
Cs@tjdd��}|jdd�}WdQRXt|dj�}|j|�dS)NT)r,z<a><b></b></a>�htmlr)r-r.rr/r0r1)rr2rr3rrr�*test_warning_if_parser_specified_too_vagueFsz7TestWarnings.test_warning_if_parser_specified_too_vaguec
Cs4tjdd��}|jdd�}WdQRX|jg|�dS)NT)r,z<a><b></b></a>zhtml.parser)r-r.rr)rr2rrrr�,test_no_warning_if_explicit_parser_specifiedLsz9TestWarnings.test_no_warning_if_explicit_parser_specifiedcCshtjdd��}|jdtd�d�}WdQRXt|dj�}|jd|k�|jd|k�|jd	|j��dS)
NT)r,z<a><b></b></a>�b)�parseOnlyTheserr9�
parse_onlys<b></b>)	r-r.rrr/r0r(rr)rr2rr3rrr�)test_parseOnlyThese_renamed_to_parse_onlyQsz6TestWarnings.test_parseOnlyThese_renamed_to_parse_onlycCsftjdd��}d}|j|dd�}WdQRXt|dj�}|jd|k�|jd|k�|jd|j�dS)	NT)r,sé�utf8)�fromEncodingrr=Z
from_encoding)r-r.rr/r0r(rr)rr2r<rr3rrr�*test_fromEncoding_renamed_to_from_encodingYsz7TestWarnings.test_fromEncoding_renamed_to_from_encodingcCs|jt|jddd�dS)Nz<a>T)Zno_such_argument)�assertRaises�	TypeErrorr)rrrr�"test_unrecognized_keyword_argumentbsz/TestWarnings.test_unrecognized_keyword_argumentN)T)
r"r#r$r+r4r6r7r;r>rArrrrr%:s
	r%c@s4eZdZdd�Zdd�Zdd�Zdd�Zd	d
�ZdS)r%cCs�tj�}|j}zBtjdd��}|j|�}WdQRXt|dj�}|jd|k�Wd|j	�Xtjdd��}|j|�}WdQRX|j
dt|��dS)NT)r,rzlooks like a filename)�tempfileZNamedTemporaryFile�namer-r.rr/r0r(�closer�len)rZ
filehandle�filenamer2rr3rrr�test_disk_file_warninghs
z#TestWarnings.test_disk_file_warningcCs>tjdd��}|jd�}WdQRX|jtdd�|D���dS)NT)r,shttp://www.crummybytes.com/css|]}dt|j�kVqdS)zlooks like a URLN)r/r0)�.0r2rrr�	<genexpr>}sz?TestWarnings.test_url_warning_with_bytes_url.<locals>.<genexpr>)r-r.rr(�any)r�warning_listrrrr�test_url_warning_with_bytes_urlxsz,TestWarnings.test_url_warning_with_bytes_urlcCs>tjdd��}|jd�}WdQRX|jtdd�|D���dS)NT)r,zhttp://www.crummyunicode.com/css|]}dt|j�kVqdS)zlooks like a URLN)r/r0)rHr2rrrrI�szATestWarnings.test_url_warning_with_unicode_url.<locals>.<genexpr>)r-r.rr(rJ)rrKrrrr�!test_url_warning_with_unicode_url�sz.TestWarnings.test_url_warning_with_unicode_urlcCs>tjdd��}|jd�}WdQRX|jtdd�|D���dS)NT)r,s$http://www.crummybytes.com/ is greatcss|]}dt|j�kVqdS)zlooks like a URLN)r/r0)rHr2rrrrI�szETestWarnings.test_url_warning_with_bytes_and_space.<locals>.<genexpr>)r-r.r�assertFalserJ)rrKrrrr�%test_url_warning_with_bytes_and_space�sz2TestWarnings.test_url_warning_with_bytes_and_spacecCs>tjdd��}|jd�}WdQRX|jtdd�|D���dS)NT)r,z%http://www.crummyuncode.com/ is greatcss|]}dt|j�kVqdS)zlooks like a URLN)r/r0)rHr2rrrrI�szGTestWarnings.test_url_warning_with_unicode_and_space.<locals>.<genexpr>)r-r.rrNrJ)rrKrrrr�'test_url_warning_with_unicode_and_space�sz4TestWarnings.test_url_warning_with_unicode_and_spaceN)r"r#r$rGrLrMrOrPrrrrr%fs
c@seZdZdd�ZdS)�TestSelectiveParsingcCs.d}td�}|j||d�}|j|j�d�dS)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>)rrrr)r�markupZstrainerrrrr�test_parse_with_soupstrainer�sz1TestSelectiveParsing.test_parse_with_soupstrainerN)r"r#r$rSrrrrrQ�srQc@sxeZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�ZdS)�TestEntitySubstitutionz1Standalone tests of the EntitySubstitution class.cCs
t|_dS)N)r	�sub)rrrr�setUp�szTestEntitySubstitution.setUpcCsd}|j|jj|�d�dS)Nufoo\u2200☃\u00f5barufoo&forall;☃&otilde;bar)rrU�substitute_html)rr)rrr�test_simple_html_substitution�sz4TestEntitySubstitution.test_simple_html_substitutioncCs&d}t|�}|j|jj|j�d�dS)Ns��foo��z&lsquo;&rsquo;foo&ldquo;&rdquo;)r
rrUrWrR)rZquotes�dammitrrr�test_smart_quote_substitution�sz4TestEntitySubstitution.test_smart_quote_substitutioncCsd}|j|jj|d�|�dS)NzWelcome to "my bar"F)rrU�substitute_xml)rr)rrr�Itest_xml_converstion_includes_no_quotes_if_make_quoted_attribute_is_false�sz`TestEntitySubstitution.test_xml_converstion_includes_no_quotes_if_make_quoted_attribute_is_falsecCs0|j|jjdd�d�|j|jjdd�d�dS)NZWelcomeTz	"Welcome"z	Bob's Barz"Bob's Bar")rrUr[)rrrr�6test_xml_attribute_quoting_normally_uses_double_quotes�szMTestEntitySubstitution.test_xml_attribute_quoting_normally_uses_double_quotescCsd}|j|jj|d�d�dS)NzWelcome to "my bar"Tz'Welcome to "my bar"')rrUr[)rr)rrr�Otest_xml_attribute_quoting_uses_single_quotes_when_value_contains_double_quotes�szfTestEntitySubstitution.test_xml_attribute_quoting_uses_single_quotes_when_value_contains_double_quotescCsd}|j|jj|d�d�dS)NzWelcome to "Bob's Bar"Tz""Welcome to &quot;Bob's Bar&quot;")rrUr[)rr)rrr�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_quotescCsd}|j|jj|�|�dS)NzWelcome to "Bob's Bar")rrUr[)rZquotedrrr�<test_xml_quotes_arent_escaped_when_value_is_not_being_quoted�szSTestEntitySubstitution.test_xml_quotes_arent_escaped_when_value_is_not_being_quotedcCs|j|jjd�d�dS)Nzfoo<bar>zfoo&lt;bar&gt;)rrUr[)rrrr�'test_xml_quoting_handles_angle_brackets�s
z>TestEntitySubstitution.test_xml_quoting_handles_angle_bracketscCs|j|jjd�d�dS)NzAT&TzAT&amp;T)rrUr[)rrrr�#test_xml_quoting_handles_ampersands�sz:TestEntitySubstitution.test_xml_quoting_handles_ampersandscCs|j|jjd�d�dS)Nz&Aacute;T&Tz&amp;Aacute;T&amp;T)rrUr[)rrrr�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_entitycCs|j|jjd�d�dS)Nz&Aacute;T&Tz&Aacute;T&amp;T)rrUZ"substitute_xml_containing_entities)rrrr�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_entitycCsd}|j|jj|�|�dS)z:There's no need to do this except inside attribute values.zBob's "bar"N)rrUrW)r�textrrr� test_quotes_not_html_substituted�sz7TestEntitySubstitution.test_quotes_not_html_substitutedN)r"r#r$�__doc__rVrXrZr\r]r^r_r`rarbrcrdrfrrrrrT�srTcsNeZdZ�fdd�Zdd�Zdd�Zdd�Zd	d
�Zee	d�dd
��Z
�ZS)�TestEncodingConversioncs4tt|�j�d|_|jjd�|_|j|jd�dS)NuU<html><head><meta charset="utf-8"/></head><body><foo>Sacré bleu!</foo></body></html>zutf-8sU<html><head><meta charset="utf-8"/></head><body><foo>Sacré bleu!</foo></body></html>)�superrhrV�unicode_datarr r)r)�	__class__rrrV�szTestEncodingConversion.setUpcCs�tjj}tjtj�zbdd�}|tj_d}|j|�}|j�}|jt	|t
��|j||j|j���|j|j
j�d�Wdtjtj�|tj_XdS)NcSsdS)Nr)r/rrr�noop�sz>TestEncodingConversion.test_ascii_in_unicode_out.<locals>.noops<foo>a</foo>zutf-8)�bs4rY�chardet_dammit�logging�disable�WARNINGr�decoder(�
isinstancer/rZdocument_forr�lower�NOTSET)r�chardetrl�asciiZsoup_from_asciiZunicode_outputrrr�test_ascii_in_unicode_out�s
z0TestEncodingConversion.test_ascii_in_unicode_outcCs@|j|j�}|j|j�|j�|j|jjd�|j|jd�dS)NuSacré bleu!)rrjrrr�foorr)r�soup_from_unicoderrr�test_unicode_in_unicode_outsz2TestEncodingConversion.test_unicode_in_unicode_outcCs2|j|j�}|j|j�|j�|j|jjd�dS)NuSacré bleu!)rr rrrrjryr)rZsoup_from_utf8rrr�test_utf8_in_unicode_outsz/TestEncodingConversion.test_utf8_in_unicode_outcCs$|j|j�}|j|jd�|j�dS)Nzutf-8)rrjrrr )rrzrrr�
test_utf8_outsz$TestEncodingConversion.test_utf8_outzQBad HTMLParser detected; skipping test of non-ASCII characters in attribute name.cCs(d}|j|j|�jjd�|jd��dS)Nu <div><a ☃="snowman"></a></div>r<)rrZdivr)rrRrrr�1test_attribute_name_containing_unicode_charactersszHTestEncodingConversion.test_attribute_name_containing_unicode_characters)r"r#r$rVrxr{r|r}r
�PYTHON_3_PRE_3_2r~�
__classcell__rr)rkrrh�s	rhc@s�eZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�Zdd
�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�Zdd�Zd d!�Zd"d#�Zd$S)%�TestUnicodeDammitz"Standalone tests of UnicodeDammit.cCsd}t|�}|j|j|�dS)NuI'm already Unicode! ☃)r
r�unicode_markup)rrRrYrrr�test_unicode_input"sz$TestUnicodeDammit.test_unicode_inputcCsd}t|�}|j|jd�dS)Ns<foo>����</foo>z#<foo>\u2018\u2019\u201c\u201d</foo>)r
rr�)rrRrYrrr�test_smart_quotes_to_unicode'sz.TestUnicodeDammit.test_smart_quotes_to_unicodecCs"d}t|dd�}|j|jd�dS)Ns<foo>����</foo>Zxml)�smart_quotes_toz+<foo>&#x2018;&#x2019;&#x201C;&#x201D;</foo>)r
rr�)rrRrYrrr�!test_smart_quotes_to_xml_entities-sz3TestUnicodeDammit.test_smart_quotes_to_xml_entitiescCs"d}t|dd�}|j|jd�dS)Ns<foo>����</foo>r5)r�z'<foo>&lsquo;&rsquo;&ldquo;&rdquo;</foo>)r
rr�)rrRrYrrr�"test_smart_quotes_to_html_entities3sz4TestUnicodeDammit.test_smart_quotes_to_html_entitiescCs"d}t|dd�}|j|jd�dS)Ns<foo>����</foo>rw)r�z<foo>''""</foo>)r
rr�)rrRrYrrr�test_smart_quotes_to_ascii9sz,TestUnicodeDammit.test_smart_quotes_to_asciicCs0d}t|�}|j|jj�d�|j|jd�dS)NsSacré bleu! ☃zutf-8uSacré bleu! ☃)r
rrrtr�)rr<rYrrr�test_detect_utf8?sz"TestUnicodeDammit.test_detect_utf8cCs4d}t|dg�}|j|jj�d�|j|jd�dS)Ns���z
iso-8859-8z\u05dd\u05d5\u05dc\u05e9)r
rrrtr�)r�hebrewrYrrr�test_convert_hebrewFsz%TestUnicodeDammit.test_convert_hebrewcCs6d}t|�}|j|jj�d�|j|jjd�|�dS)Nsケータイ Watchzutf-8)r
rrrtr�r)r�utf_8rYrrr�/test_dont_see_smart_quotes_where_there_are_noneLszATestUnicodeDammit.test_dont_see_smart_quotes_where_there_are_nonecCs,djd�}t|dg�}|j|jj�d�dS)Nu
Räksmörgåszutf-8z
iso-8859-8)rr
rrrt)rr rYrrr� test_ignore_inappropriate_codecsRs
z2TestUnicodeDammit.test_ignore_inappropriate_codecscCs:djd�}x*dD]"}t||g�}|j|jj�d�qWdS)Nu
Räksmörgåszutf-8�.utf8�...�
utF---16.!)r�r�r�)rr
rrrt)rr Zbad_encodingrYrrr�test_ignore_invalid_codecsWs

z,TestUnicodeDammit.test_ignore_invalid_codecscCsLdjd�}t|dgd�}|j|jj�d�t|ddgd�}|j|jd�dS)Nu
Räksmörgåszutf-8)rzwindows-1252)rr
rrrt)rr rYrrrr!]s
z(TestUnicodeDammit.test_exclude_encodingscCs"td�}t|j�}d|kst�dS)Ns'<?xml version="1.0" encoding="UTF-�" ?>uutf-�)r�list�	encodings�AssertionError)rZdetectedr�rrr�Ptest_encoding_detector_replaces_junk_in_encoding_name_with_replacement_characterks
zbTestUnicodeDammit.test_encoding_detector_replaces_junk_in_encoding_name_with_replacement_charactercCs,x&dD]}t|dd�}|jd|j�qWdS)	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)Zis_htmlzeuc-jp)r�r�r�r�)r
rr)rrrYrrr� test_detect_html5_style_meta_tagqsz2TestUnicodeDammit.test_detect_html5_style_meta_tagcCs�d}tjj}tjtj�zPdd�}|tj_t|�}|jd|j�|j	d|j
k�t|d�}|j	|j�Wdtjtj�|tj_XdS)NsT<?xml version="1.0" encoding="UTF-8"?>
<html><b>بتر</b>
<i>��ѐ����</i></html>cSsdS)Nr)r/rrrrl�szBTestUnicodeDammit.test_last_ditch_entity_replacement.<locals>.noopTz\ufffdzhtml.parser)
rmrYrnrorprqr
rZcontains_replacement_charactersr(r�rru)r�docrvrlrYrrrr�"test_last_ditch_entity_replacement|s
z4TestUnicodeDammit.test_last_ditch_entity_replacementcCs,d}t|�}|jd|j�|jd|j�dS)Ns��<a>��</a>u<a>áé</a>zutf-16le)r
rr�r)rrrYrrr�test_byte_order_mark_removed�sz.TestUnicodeDammit.test_byte_order_mark_removedcCsPdjd�}djd�}|||}|jt|jd�tj|�}|jd|jd��dS)Nu☃rr<u“Hi, I like Windows!”�windows_1252u+☃☃☃“Hi, I like Windows!”☃☃☃u	☃☃☃)rr?�UnicodeDecodeErrorrrr
�	detwingler)rr<r�r�Zfixedrrr�test_detwingle�s

z TestUnicodeDammit.test_detwinglecCsBx<dD]4}|jd�}|j|jd��tj|�}|j||�qWdS)N�œ�ₓ�𐐓r<��)r�r�r�)rr(�endswithr
r�r)rZtricky_unicode_char�input�outputrrr�+test_detwingle_ignores_multibyte_characters�s

z=TestUnicodeDammit.test_detwingle_ignores_multibyte_charactersN)r"r#r$rgr�r�r�r�r�r�r�r�r�r�r!r�r�r�r�r�r�rrrrr�s$#r�c@s$eZdZdd�Zdd�Zdd�ZdS)�TestNamedspacedAttributecCstdd�}|j|d�dS)NZxmlns)rr)r�arrr�test_name_may_be_none�s
z.TestNamedspacedAttribute.test_name_may_be_nonecCstdd�}|jd|�dS)Nr�r8za:b)rr)rr�rrr�6test_attribute_is_equivalent_to_colon_separated_string�s
zOTestNamedspacedAttribute.test_attribute_is_equivalent_to_colon_separated_stringcCsptddd�}tddd�}|j||�tddd�}|j||�tddd�}|j||�tddd�}|j||�dS)Nr�r8�c�z)rrZassertNotEqual)rr�r8r��d�errr�;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�rrrrr��sr�c@seZdZdd�Zdd�ZdS)�)TestAttributeValueWithCharsetSubstitutioncCs8td�}|jd|�|jd|j�|jd|jd��dS)Nzeuc-jpr<)rr�original_valuer)r�valuerrr�!test_content_meta_attribute_value�szKTestAttributeValueWithCharsetSubstitution.test_content_meta_attribute_valuecCs8td�}|jd|�|jd|j�|jd|jd��dS)Nztext/html; charset=euc-jpztext/html; charset=utf8r<)rrr�r)rr�rrrr��sN)r"r#r$r�rrrrr��sr�)rr)(rgZpdbrroZunittest�sysrBrmrrZbs4.elementrrrrZ
bs4.dammitr	r
rZbs4.testingrr
r-Zbs4.builderrrZLXML_PRESENT�ImportErrorr��version_inforrr%rQZTestCaserTrhr�r�r�rrrr�<module>s6,/	E</
Page not found – Hello World !