summaryrefslogtreecommitdiff
path: root/bs4/tests
diff options
context:
space:
mode:
authorLeonard Richardson <leonardr@segfault.org>2019-07-07 14:01:40 -0400
committerLeonard Richardson <leonardr@segfault.org>2019-07-07 14:01:40 -0400
commit0c3c1970dcb93bbe591707e43cfba9b24de45d05 (patch)
tree48be5f0ef368309d401cee00bd0f2d504fdde85f /bs4/tests
parent66d2597c36b1923c487daae55bfa73e4bb4a66d1 (diff)
It's now possible to customize the TreeBuilder object by passing
keyword arguments into the BeautifulSoup constructor. The main reason to do this right now is to change how multi-valued attributes are treated. [bug=1832978]
Diffstat (limited to 'bs4/tests')
-rw-r--r--bs4/tests/test_soup.py35
1 files changed, 35 insertions, 0 deletions
diff --git a/bs4/tests/test_soup.py b/bs4/tests/test_soup.py
index f3e69ed..1c6b7a6 100644
--- a/bs4/tests/test_soup.py
+++ b/bs4/tests/test_soup.py
@@ -54,6 +54,41 @@ class TestConstructor(SoupTest):
soup = self.soup(utf8_data, exclude_encodings=["utf-8"])
self.assertEqual("windows-1252", soup.original_encoding)
+ def test_custom_builder_class(self):
+ # Verify that you can pass in a custom Builder class and
+ # it'll be instantiated with the appropriate keyword arguments.
+ class Mock(object):
+ def __init__(self, **kwargs):
+ self.called_with = kwargs
+ self.is_xml = True
+ def initialize_soup(self, soup):
+ pass
+ def prepare_markup(self, *args, **kwargs):
+ return ''
+
+ kwargs = dict(
+ var="value",
+ # This is a deprecated BS3-era keyword argument, which
+ # will be stripped out.
+ convertEntities=True,
+ )
+ soup = BeautifulSoup('', builder=Mock, **kwargs)
+ assert isinstance(soup.builder, Mock)
+ self.assertEqual(dict(var="value"), soup.builder.called_with)
+
+ # You can also instantiate the TreeBuilder yourself. In this
+ # case, that specific object is used and any keyword arguments
+ # to the BeautifulSoup constructor are ignored.
+ builder = Mock(**kwargs)
+ with warnings.catch_warnings(record=True) as w:
+ soup = BeautifulSoup(
+ '', builder=builder, ignored_value=True,
+ )
+ msg = str(w[0].message)
+ assert msg.startswith("Keyword arguments to the BeautifulSoup constructor will be ignored.")
+ self.assertEqual(builder, soup.builder)
+ self.assertEqual(kwargs, builder.called_with)
+
class TestWarnings(SoupTest):