Best Python code snippet using localstack_python
test_sgmllib.py
Source:test_sgmllib.py  
...56        except:57            #self.events = parser.events58            raise59        return parser.get_events()60    def check_events(self, source, expected_events):61        try:62            events = self.get_events(source)63        except:64            import sys65            #print >>sys.stderr, pprint.pformat(self.events)66            raise67        if events != expected_events:68            self.fail("received events did not match expected events\n"69                      "Expected:\n" + pprint.pformat(expected_events) +70                      "\nReceived:\n" + pprint.pformat(events))71    def check_parse_error(self, source):72        parser = EventCollector()73        try:74            parser.feed(source)75            parser.close()76        except sgmllib.SGMLParseError:77            pass78        else:79            self.fail("expected SGMLParseError for %r\nReceived:\n%s"80                      % (source, pprint.pformat(parser.get_events())))81    def test_doctype_decl_internal(self):82        inside = """\83DOCTYPE html PUBLIC '-//W3C//DTD HTML 4.01//EN'84             SYSTEM 'http://www.w3.org/TR/html401/strict.dtd' [85  <!ELEMENT html - O EMPTY>86  <!ATTLIST html87      version CDATA #IMPLIED88      profile CDATA 'DublinCore'>89  <!NOTATION datatype SYSTEM 'http://xml.python.org/notations/python-module'>90  <!ENTITY myEntity 'internal parsed entity'>91  <!ENTITY anEntity SYSTEM 'http://xml.python.org/entities/something.xml'>92  <!ENTITY % paramEntity 'name|name|name'>93  %paramEntity;94  <!-- comment -->95]"""96        self.check_events(["<!%s>" % inside], [97            ("decl", inside),98            ])99    def test_doctype_decl_external(self):100        inside = "DOCTYPE html PUBLIC '-//W3C//DTD HTML 4.01//EN'"101        self.check_events("<!%s>" % inside, [102            ("decl", inside),103            ])104    def test_underscore_in_attrname(self):105        # SF bug #436621106        """Make sure attribute names with underscores are accepted"""107        self.check_events("<a has_under _under>", [108            ("starttag", "a", [("has_under", "has_under"),109                               ("_under", "_under")]),110            ])111    def test_underscore_in_tagname(self):112        # SF bug #436621113        """Make sure tag names with underscores are accepted"""114        self.check_events("<has_under></has_under>", [115            ("starttag", "has_under", []),116            ("endtag", "has_under"),117            ])118    def test_quotes_in_unquoted_attrs(self):119        # SF bug #436621120        """Be sure quotes in unquoted attributes are made part of the value"""121        self.check_events("<a href=foo'bar\"baz>", [122            ("starttag", "a", [("href", "foo'bar\"baz")]),123            ])124    def test_xhtml_empty_tag(self):125        """Handling of XHTML-style empty start tags"""126        self.check_events("<br />text<i></i>", [127            ("starttag", "br", []),128            ("data", "text"),129            ("starttag", "i", []),130            ("endtag", "i"),131            ])132    def test_processing_instruction_only(self):133        self.check_events("<?processing instruction>", [134            ("pi", "processing instruction"),135            ])136    def test_bad_nesting(self):137        self.check_events("<a><b></a></b>", [138            ("starttag", "a", []),139            ("starttag", "b", []),140            ("endtag", "a"),141            ("endtag", "b"),142            ])143    def test_bare_ampersands(self):144        self.check_events("this text & contains & ampersands &", [145            ("data", "this text & contains & ampersands &"),146            ])147    def test_bare_pointy_brackets(self):148        self.check_events("this < text > contains < bare>pointy< brackets", [149            ("data", "this < text > contains < bare>pointy< brackets"),150            ])151    def test_attr_syntax(self):152        output = [153          ("starttag", "a", [("b", "v"), ("c", "v"), ("d", "v"), ("e", "e")])154          ]155        self.check_events("""<a b='v' c="v" d=v e>""", output)156        self.check_events("""<a  b = 'v' c = "v" d = v e>""", output)157        self.check_events("""<a\nb\n=\n'v'\nc\n=\n"v"\nd\n=\nv\ne>""", output)158        self.check_events("""<a\tb\t=\t'v'\tc\t=\t"v"\td\t=\tv\te>""", output)159    def test_attr_values(self):160        self.check_events("""<a b='xxx\n\txxx' c="yyy\t\nyyy" d='\txyz\n'>""",161                        [("starttag", "a", [("b", "xxx\n\txxx"),162                                            ("c", "yyy\t\nyyy"),163                                            ("d", "\txyz\n")])164                         ])165        self.check_events("""<a b='' c="">""", [166            ("starttag", "a", [("b", ""), ("c", "")]),167            ])168    def test_attr_funky_names(self):169        self.check_events("""<a a.b='v' c:d=v e-f=v>""", [170            ("starttag", "a", [("a.b", "v"), ("c:d", "v"), ("e-f", "v")]),171            ])172    def test_illegal_declarations(self):173        s = 'abc<!spacer type="block" height="25">def'174        self.check_events(s, [175            ("data", "abc"),176            ("unknown decl", 'spacer type="block" height="25"'),177            ("data", "def"),178            ])179    def test_weird_starttags(self):180        self.check_events("<a<a>", [181            ("starttag", "a", []),182            ("starttag", "a", []),183            ])184        self.check_events("</a<a>", [185            ("endtag", "a"),186            ("starttag", "a", []),187            ])188    def test_declaration_junk_chars(self):189        self.check_parse_error("<!DOCTYPE foo $ >")190    def test_get_starttag_text(self):191        s = """<foobar   \n   one="1"\ttwo=2   >"""192        self.check_events(s, [193            ("starttag", "foobar", [("one", "1"), ("two", "2")]),194            ])195    def test_cdata_content(self):196        s = ("<cdata> <!-- not a comment --> ¬-an-entity-ref; </cdata>"197             "<notcdata> <!-- comment --> </notcdata>")198        self.collector = CDATAEventCollector199        self.check_events(s, [200            ("starttag", "cdata", []),201            ("data", " <!-- not a comment --> ¬-an-entity-ref; "),202            ("endtag", "cdata"),203            ("starttag", "notcdata", []),204            ("data", " "),205            ("comment", " comment "),206            ("data", " "),207            ("endtag", "notcdata"),208            ])209        s = """<cdata> <not a='start tag'> </cdata>"""210        self.check_events(s, [211            ("starttag", "cdata", []),212            ("data", " <not a='start tag'> "),213            ("endtag", "cdata"),214            ])215    def test_illegal_declarations(self):216        s = 'abc<!spacer type="block" height="25">def'217        self.check_events(s, [218            ("data", "abc"),219            ("unknown decl", 'spacer type="block" height="25"'),220            ("data", "def"),221            ])222    # XXX These tests have been disabled by prefixing their names with223    # an underscore.  The first two exercise outstanding bugs in the224    # sgmllib module, and the third exhibits questionable behavior225    # that needs to be carefully considered before changing it.226    def _test_starttag_end_boundary(self):227        self.check_events("""<a b='<'>""", [("starttag", "a", [("b", "<")])])228        self.check_events("""<a b='>'>""", [("starttag", "a", [("b", ">")])])229    def _test_buffer_artefacts(self):230        output = [("starttag", "a", [("b", "<")])]231        self.check_events(["<a b='<'>"], output)232        self.check_events(["<a ", "b='<'>"], output)233        self.check_events(["<a b", "='<'>"], output)234        self.check_events(["<a b=", "'<'>"], output)235        self.check_events(["<a b='<", "'>"], output)236        self.check_events(["<a b='<'", ">"], output)237        output = [("starttag", "a", [("b", ">")])]238        self.check_events(["<a b='>'>"], output)239        self.check_events(["<a ", "b='>'>"], output)240        self.check_events(["<a b", "='>'>"], output)241        self.check_events(["<a b=", "'>'>"], output)242        self.check_events(["<a b='>", "'>"], output)243        self.check_events(["<a b='>'", ">"], output)244    def _test_starttag_junk_chars(self):245        self.check_parse_error("<")246        self.check_parse_error("<>")247        self.check_parse_error("</$>")248        self.check_parse_error("</")249        self.check_parse_error("</a")250        self.check_parse_error("<$")251        self.check_parse_error("<$>")252        self.check_parse_error("<!")253        self.check_parse_error("<a $>")254        self.check_parse_error("<a")255        self.check_parse_error("<a foo='bar'")256        self.check_parse_error("<a foo='bar")257        self.check_parse_error("<a foo='>'")...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
