"""Test that we don't fail the same test cases that Beautiful Soup used to fail prior to Soup Sieve.""" from bs4 import BeautifulSoup import unittest import soupsieve as sv from . import util class SelectorNthOfTypeBugTest(unittest.TestCase): """ Original Beautiful soup test html document. http://bazaar.launchpad.net/~leonardr/beautifulsoup/bs4/view/head:/bs4/tests/test_tree.py, line 1627. """ HTML = """ The title Hello there.

An H1

Some text

Some more text

An H2

Another

Bob

Another H2

me span1a1 span1a2 test span2a1

English

English UK

English US

French

""" def setUp(self): """Setup.""" self.soup = BeautifulSoup(self.HTML, 'html.parser') def test_parent_nth_of_type_preconditions(self): """Test `nth` type preconditions.""" els = sv.select('div > h1', self.soup) # check that there is a unique selection self.assertEqual(len(els), 1) self.assertEqual(els[0].string, 'An H1') # Show that the `h1`'s parent `div#inner` is the first child of type `div` of the grandparent `div#main`. # so that the selector `div:nth-of-type(1) > h1` should also give `h1`. h1 = els[0] div_inner = h1.parent div_main = div_inner.parent div_main_children = list(div_main.children) self.assertEqual(div_main_children[0], '\n') self.assertEqual(div_main_children[1], div_inner) def test_parent_nth_of_type(self): """Test parent of `nth` of type.""" els = sv.select('div:nth-of-type(1) > h1', self.soup) self.assertEqual(len(els), 1) self.assertEqual(els[0].string, 'An H1') SIMPLE_XML = """
...
""" NAMESPACE_XML = """ http://docs.oasis-open.org/ws-sx/ws-trust/200512/RST/Issue """.strip() NAMESPACES = { 'x': "http://www.w3.org/2003/05/soap-envelope", 'y': "http://www.w3.org/2005/08/addressing", 'z': "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" } @util.requires_lxml def test_simple_xml(): """Test basic XML.""" xml = BeautifulSoup(SIMPLE_XML, "xml") assert xml.select_one("Envelope") assert xml.select_one("Envelope Header") assert xml.select_one("Header") assert not xml.select_one("envelope") assert not xml.select_one("envelope header") assert not xml.select_one("header") @util.requires_lxml def test_namespace_xml(): """Test namespace XML.""" xml = BeautifulSoup(NAMESPACE_XML, "xml") assert xml.select_one("Envelope") assert xml.select_one("Envelope Action") assert xml.select_one("Action") assert not xml.select_one("envelope") assert not xml.select_one("envelope action") assert not xml.select_one("action") @util.requires_lxml def test_namespace_xml_with_namespace(): """Test namespace selectors with XML.""" xml = BeautifulSoup(NAMESPACE_XML, "xml") assert xml.select_one("x|Envelope", namespaces=NAMESPACES) assert xml.select_one("x|Envelope y|Action", namespaces=NAMESPACES) assert xml.select_one("y|Action", namespaces=NAMESPACES) assert not xml.select_one("x|envelope", namespaces=NAMESPACES) assert not xml.select_one("x|envelope y|action", namespaces=NAMESPACES) assert not xml.select_one("y|action", namespaces=NAMESPACES)