import textwrap import unittest from parsing import ( FirstMatchingElementParser, HTMLTagParser, MatchingElementParser, ) from yt_dlp.compat import compat_HTMLParseError get_element_by_attribute = FirstMatchingElementParser get_element_by_class = FirstMatchingElementParser get_element_html_by_attribute = FirstMatchingElementParser get_element_html_by_class = FirstMatchingElementParser.get_element_html_by_class get_element_text_and_html_by_tag = FirstMatchingElementParser.get_element_text_and_html_by_tag get_elements_by_attribute = MatchingElementParser get_elements_by_class = MatchingElementParser get_elements_html_by_attribute = MatchingElementParser get_elements_html_by_class = FirstMatchingElementParser.get_elements_html_by_class get_elements_text_and_html_by_attribute = MatchingElementParser class TestParsing(unittest.TestCase): GET_ELEMENT_BY_CLASS_TEST_STRING = '''
''' def test_get_element_by_class(self): html = self.GET_ELEMENT_BY_CLASS_TEST_STRING self.assertEqual(get_element_by_class('foo', html), 'nice') self.assertEqual(get_element_by_class('no-such-class', html), None) def test_get_element_html_by_class(self): html = self.GET_ELEMENT_BY_CLASS_TEST_STRING self.assertEqual(get_element_html_by_class('foo', html), html.strip()) self.assertEqual(get_element_by_class('no-such-class', html), None) GET_ELEMENT_BY_ATTRIBUTE_TEST_STRING = '''', reset=True) with self.assertRaisesRegex(compat_HTMLParseError, "malnested closing tag 'div', expected after '
'"): parser.taglist('/p>
', reset=True), []) tags = parser.taglist('
/p>
paragraph
paragraph
must be empty', reset=True) self.assertEqual(tags, [Tag('img')]) self.assertEqual(tags[0].text_and_html(), ('', '