test_builder.py 1.1 KB

1234567891011121314151617181920212223242526272829
  1. import pytest
  2. from unittest.mock import patch
  3. from bs4.builder import DetectsXMLParsedAsHTML
  4. class TestDetectsXMLParsedAsHTML(object):
  5. @pytest.mark.parametrize(
  6. "markup,looks_like_xml",
  7. [("No xml declaration", False),
  8. ("<html>obviously HTML</html", False),
  9. ("<?xml ><html>Actually XHTML</html>", False),
  10. ("<?xml> < html>Tricky XHTML</html>", False),
  11. ("<?xml ><no-html-tag>", True),
  12. ]
  13. )
  14. def test_warn_if_markup_looks_like_xml(self, markup, looks_like_xml):
  15. # Test of our ability to guess at whether markup looks XML-ish
  16. # _and_ not HTML-ish.
  17. with patch('bs4.builder.DetectsXMLParsedAsHTML._warn') as mock:
  18. for data in markup, markup.encode('utf8'):
  19. result = DetectsXMLParsedAsHTML.warn_if_markup_looks_like_xml(
  20. data
  21. )
  22. assert result == looks_like_xml
  23. if looks_like_xml:
  24. assert mock.called
  25. else:
  26. assert not mock.called
  27. mock.reset_mock()