Changeset 2633:be517f8a7897 in livinglogic.python.xist

Show
Ignore:
Timestamp:
01/31/07 12:30:59 (13 years ago)
Author:
Walter Doerwald <walter@…>
Branch:
default
Message:

Check thet the prefix mapping is correct (i.e. prefix->nsname) in the
parsers constructor to guard against user errors.

Move some of the parsing test that don't fiddle with the warning registry
out of the testing class.

Fix test_parse.test_nsparse (nested class didn't pick up the local var).

Enhance namespace parsing test (and adapt them to the new namespace machinery).

Files:
2 modified

Legend:

Unmodified
Added
Removed
  • src/ll/xist/parsers.py

    r2632 r2633  
    612612        if prefixes is None: 
    613613            # make all currently known namespaces available without prefix 
    614             # (if there are elements with colliding namespace, which one will be used is random (based on dict iteration order) 
     614            # (if there are elements with colliding namespace, which one will be used is random (based on dict iteration order)) 
    615615            self.prefixes = {None: list(set(xsc.nsname(c.xmlns) for c in self.pool.element_values()))} 
    616616        else: 
    617617            self.prefixes = {} 
    618618            for (prefix, xmlns) in prefixes.iteritems(): 
     619                if prefix is not None and not isinstance(prefix, basestring): 
     620                    raise TypeError("Prefix must be None or string, not %r" % prefix) 
    619621                if isinstance(xmlns, (list, tuple)): 
    620622                    self.prefixes[prefix] = map(xsc.nsname, xmlns) 
  • test/test_parse.py

    r2623 r2633  
    2828 
    2929def raisesSAX(exception, func, *args, **kwargs): 
    30     # assert that func(*args, **kwargs) raises exception either directly or wrapped in a SAXParseException 
     30    # assert that func(*args, **kwargs) raises exception (either directly or wrapped in a SAXParseException) 
    3131    try: 
    3232        func(*args, **kwargs) 
     
    9696 
    9797 
     98def test_parselocationsgmlop(): 
     99    # Check that SGMLOP gets the location info right (at least the line numbers) 
     100    node = parsers.parseString("<z>gurk&amp;hurz&#42;hinz&#x666;hunz</z>", saxparser=parsers.SGMLOPParser) 
     101    assert len(node) == 1 
     102    assert len(node[0]) == 1 
     103    assert node[0][0].startloc.getSystemId() == "STRING" 
     104    assert node[0][0].startloc.getLineNumber() == 1 
     105 
     106 
     107def test_parselocationexpat(): 
     108    # Check that expat gets the location info right 
     109    node = parsers.parseString("<z>gurk&amp;hurz&#42;hinz&#x666;hunz</z>", saxparser=parsers.ExpatParser) 
     110    assert len(node) == 1 
     111    assert len(node[0]) == 1 
     112    assert node[0][0].startloc.getSystemId() == "STRING" 
     113    assert node[0][0].startloc.getLineNumber() == 1 
     114    assert node[0][0].startloc.getColumnNumber() == 3 
     115 
     116 
    98117class Test: 
    99118    def setup_method(self, method): 
     
    103122    def teardown_method(self, method): 
    104123        warnings.filters = oldfilters 
    105  
    106     def test_parselocationsgmlop(self): 
    107         # Check that SGMLOP gets the location info right (at least the line numbers) 
    108         node = parsers.parseString("<z>gurk&amp;hurz&#42;hinz&#x666;hunz</z>", saxparser=parsers.SGMLOPParser) 
    109         assert len(node) == 1 
    110         assert len(node[0]) == 1 
    111         assert node[0][0].startloc.getSystemId() == "STRING" 
    112         assert node[0][0].startloc.getLineNumber() == 1 
    113  
    114     def test_parselocationexpat(self): 
    115         # Check that expat gets the location info right 
    116         node = parsers.parseString("<z>gurk&amp;hurz&#42;hinz&#x666;hunz</z>", saxparser=parsers.ExpatParser) 
    117         assert len(node) == 1 
    118         assert len(node[0]) == 1 
    119         assert node[0][0].startloc.getSystemId() == "STRING" 
    120         assert node[0][0].startloc.getLineNumber() == 1 
    121         assert node[0][0].startloc.getColumnNumber() == 3 
    122124 
    123125    def test_nsparse(self): 
     
    157159        with xsc.Pool(): 
    158160            class Test(xsc.Element): 
    159                 xmlns = xmlns 
     161                xmlns = "http://www.example.com/required" 
    160162                class Attrs(xsc.Element.Attrs): 
    161163                    class required(xsc.TextAttr): 
     
    168170            raisesSAX(xsc.RequiredAttrMissingWarning, parsers.parseString, '<Test/>', prefixes=prefixes) 
    169171 
    170         py.test.assertRaises(xsc.IllegalElementError, parsers.parseString, '<Test required="foo"/>', prefixes=prefixes) 
     172        py.test.raises(xsc.IllegalElementError, parsers.parseString, '<Test required="foo"/>', prefixes=prefixes) 
    171173 
    172174    def test_parsevalueattrs(self): 
     
    224226            yield check, saxparser 
    225227 
    226     def test_sysid(self): 
    227         # Default system ids and explicitely specified system ids should end up in the location info of the resulting XML tree 
    228         node = parsers.parseString("gurk") 
    229         assert node[0].startloc.sysid == "STRING" 
    230  
    231         node = parsers.parseString("gurk", base="root:gurk.xmlxsc") 
    232         assert node[0].startloc.sysid == "root:gurk.xmlxsc" 
    233  
    234         node = parsers.parseString("gurk", base="root:gurk.xmlxsc", sysid="hurz") 
    235         assert node[0].startloc.sysid == "hurz" 
     228 
     229def test_sysid(): 
     230    # Default system ids and explicitely specified system ids should end up in the location info of the resulting XML tree 
     231    node = parsers.parseString("gurk") 
     232    assert node[0].startloc.sysid == "STRING" 
     233 
     234    node = parsers.parseString("gurk", base="root:gurk.xmlxsc") 
     235    assert node[0].startloc.sysid == "root:gurk.xmlxsc" 
     236 
     237    node = parsers.parseString("gurk", base="root:gurk.xmlxsc", sysid="hurz") 
     238    assert node[0].startloc.sysid == "hurz" 
    236239 
    237240 
    238241def test_xmlns(): 
    239     s = '''<z xmlns="%s"><rb xmlns="%s"/><z/></z>''' % (specials.xmlns, ruby.xmlns) 
    240     # After the <rb/> element is left the parser must return to the proper previous mapping 
     242    s = "<z xmlns=%r><rb xmlns=%r/><z/></z>" % (specials.xmlns, ruby.xmlns) 
    241243    e = parsers.parseString(s) 
    242244 
    243245    assert e[0].xmlns == specials.xmlns 
    244246    assert e[0][0].xmlns == ruby.xmlns 
     247 
     248    s = "<a xmlns=%r><a xmlns=%r/></a>" % (html.xmlns, ihtml.xmlns) 
     249    e = parsers.parseString(s, pool=xsc.Pool(html, ihtml)) 
     250    assert isinstance(e[0], html.a) 
     251    assert isinstance(e[0][0], ihtml.a) 
     252 
     253    s = "<a><a xmlns=%r/></a>" % ihtml.xmlns 
     254    py.test.raises(xsc.IllegalElementError, parsers.parseString, s, prefixes={None: html}, pool=xsc.Pool(ihtml)) 
     255    e = parsers.parseString(s, prefixes={None: html}, pool=xsc.Pool(html, ihtml)) 
     256    assert isinstance(e[0], html.a) 
     257    assert isinstance(e[0][0], ihtml.a) 
     258 
     259    s = "<z xmlns=%r/>" % specials.xmlns 
     260    e = parsers.parseString(s, pool=xsc.Pool(specials.z)) 
     261    assert isinstance(e[0], specials.z) 
     262    py.test.raises(xsc.IllegalElementError, parsers.parseString, s, pool=xsc.Pool())