from helpers import SoupTest from beautifulsoup import BeautifulSoup from beautifulsoup.element import SoupStrainer from beautifulsoup.builder.lxml_builder import LXMLTreeBuilder import unittest class TestLXMLBuilder(SoupTest): def __init__(self, builder): super(TestLXMLBuilder, self).__init__() self.default_builder = LXMLTreeBuilder() def runTest(self): self.test_bare_string() self.test_tag_nesting() self.test_self_closing() self.test_soupstrainer() def document_for(self, s): """Turn a fragment into an HTML document. lxml does this to HTML fragments it receives, so we need to do it if we're going to understand what comes out of lxml. """ return u'%s' % s def test_bare_string(self): self.assertSoupEquals( "A bare string", self.document_for("

A bare string

")) def test_tag_nesting(self): b_tag = "Inside a B tag" self.assertSoupEquals(b_tag, self.document_for(b_tag)) nested_b_tag = "

A nested tag

" self.assertSoupEquals(nested_b_tag, self.document_for(nested_b_tag)) def test_self_closing(self): self.assertSoupEquals( "

A tag

", self.document_for("

A tag

")) def test_soupstrainer(self): strainer = SoupStrainer("b") soup = BeautifulSoup("A bold statement", self.default_builder, parseOnlyThese=strainer) self.assertEquals(soup.decode(), "bold") soup = BeautifulSoup("A bold statement", self.default_builder, parseOnlyThese=strainer) self.assertEquals(soup.decode(), "bold") def test_suite(): return unittest.TestLoader().loadTestsFromName('__name__')