def testAssertEqualsLxmlsXpathToHereWithCommentNodes(self): xml = '''\ <!-- 1st Comment --> <!-- Pre-Root Comment --> <a xmlns="n:s/#"> <!-- 1st Comment inside --> <!-- 2nd Comment inside --> <b/> </a> <!-- Post-Root Comment --> <!-- last Comment -->''' lxml = parseString(xml) _pre_root = lxml.getroot().getprevious() _1st = _pre_root.getprevious() _post_root = lxml.getroot().getnext() _last = _post_root.getnext() _1st_inside = lxml.getroot().getchildren()[0] _2nd_inside = lxml.getroot().getchildren()[1] c = CompareXml(expectedNode=lxml, resultNode=lxml) self.assertEquals('{n:s/#}a', c.xpathToHere(_1st_inside)) self.assertEquals('{n:s/#}a', c.xpathToHere(_2nd_inside)) self.assertEquals('{n:s/#}a/comment()[1]', c.xpathToHere(_1st_inside, includeCurrent=True)) self.assertEquals('{n:s/#}a/comment()[2]', c.xpathToHere(_2nd_inside, includeCurrent=True)) self.assertEquals('', c.xpathToHere(_1st)) self.assertEquals('', c.xpathToHere(_pre_root)) self.assertEquals('comment()[1]', c.xpathToHere(_1st, includeCurrent=True)) self.assertEquals('comment()[2]', c.xpathToHere(_pre_root, includeCurrent=True)) self.assertEquals('', c.xpathToHere(_post_root)) self.assertEquals('', c.xpathToHere(_last)) self.assertEquals('comment()[3]', c.xpathToHere(_post_root, includeCurrent=True)) self.assertEquals('comment()[4]', c.xpathToHere(_last, includeCurrent=True))
def assertPathToTagOkInXml(xml, tagsWithPaths, namespaces=None): namespaces = namespaces if namespaces else {} lxml = parseString(xml) lxmlNode = lxml.getroot() compareXml = CompareXml(expectedNode=lxmlNode, resultNode=lxmlNode) for d in tagsWithPaths: tag, pathExc, pathIncl = d['tag'], d['excl'], d['incl'] self.assertEquals(set(['tag', 'excl', 'incl']), set(d.keys())) t = lxmlNode.xpath('//%s' % tag, namespaces=namespaces)[0] self.assertEquals(pathExc, compareXml.xpathToHere(t, includeCurrent=False)) self.assertEquals(pathIncl, compareXml.xpathToHere(t, includeCurrent=True))
def assertPathToTagOkInXml(xml, tagsWithPaths, namespaces=None): namespaces = namespaces if namespaces else {} lxml = parseString(xml) lxmlNode = lxml.getroot() compareXml = CompareXml(expectedNode=lxmlNode, resultNode=lxmlNode) for d in tagsWithPaths: tag, pathExc, pathIncl = d['tag'], d['excl'], d['incl'] self.assertEqual(set(['tag', 'excl', 'incl']), set(d.keys())) t = lxmlNode.xpath('//%s' % tag, namespaces=namespaces)[0] self.assertEqual( pathExc, compareXml.xpathToHere(t, includeCurrent=False)) self.assertEqual( pathIncl, compareXml.xpathToHere(t, includeCurrent=True))
def testAssertEqualsLxmlsXpathToHereWithEntityNodes(self): # For a (possible) use-case of this parsing method, see: http://lxml.de/FAQ.html#how-do-i-use-lxml-safely-as-a-web-service-endpoint parser = XMLParser( resolve_entities=False) # Otherwise, no entity nodes remain. xml = '''\ <!DOCTYPE root [ <!ENTITY self "sefl"> <!ENTITY otherself "ohtersefl"> ]> <r>Ӓ &self; &otherself;<a>&self;</a></r> ''' lxml = parse(StringIO(xml), parser=parser) _inside_self = lxml.getroot().getchildren()[0] _inside_otherself = lxml.getroot().getchildren()[1] _insideA_self = lxml.getroot().getchildren()[2].getchildren()[0] c = CompareXml(expectedNode=lxml, resultNode=lxml) self.assertEqual("r", c.xpathToHere(_inside_self)) self.assertEqual("r", c.xpathToHere(_inside_otherself)) self.assertEqual("r/a", c.xpathToHere(_insideA_self)) self.assertEqual("r/?[1]", c.xpathToHere(_inside_self, includeCurrent=True)) self.assertEqual("r/?[2]", c.xpathToHere(_inside_otherself, includeCurrent=True)) self.assertEqual("r/a/?", c.xpathToHere(_insideA_self, includeCurrent=True))
def testAssertEqualsLxmlsXpathToHereWithEntityNodes(self): # For a (possible) use-case of this parsing method, see: http://lxml.de/FAQ.html#how-do-i-use-lxml-safely-as-a-web-service-endpoint parser = XMLParser(resolve_entities=False) # Otherwise, no entity nodes remain. xml = '''\ <!DOCTYPE root [ <!ENTITY self "sefl"> <!ENTITY otherself "ohtersefl"> ]> <r>Ӓ &self; &otherself;<a>&self;</a></r> ''' lxml = parse(StringIO(xml), parser=parser) _inside_self = lxml.getroot().getchildren()[0] _inside_otherself = lxml.getroot().getchildren()[1] _insideA_self = lxml.getroot().getchildren()[2].getchildren()[0] c = CompareXml(expectedNode=lxml, resultNode=lxml) self.assertEquals("r", c.xpathToHere(_inside_self)) self.assertEquals("r", c.xpathToHere(_inside_otherself)) self.assertEquals("r/a", c.xpathToHere(_insideA_self)) self.assertEquals("r/?[1]", c.xpathToHere(_inside_self, includeCurrent=True)) self.assertEquals("r/?[2]", c.xpathToHere(_inside_otherself, includeCurrent=True)) self.assertEquals("r/a/?", c.xpathToHere(_insideA_self, includeCurrent=True))
def testAssertEqualsLxmlsXpathToHereWithProcessingInstructions(self): xml = '''\ <?pro cessing instruction?> <a xmlns="n:s/#"> <?php ...?> <?notphp Intermezzo?> <?php just kidding!?> <b/> </a> <?xml-stylesheet href="what.css" type="text/ever"?> <?pro with newlines?>''' lxml = parseString(xml) _pro_cessing = lxml.getroot().getprevious() _xml_style = lxml.getroot().getnext() _pro_newline = _xml_style.getnext() _inside_1_php = lxml.getroot().getchildren()[0] _inside_2_notphp = lxml.getroot().getchildren()[1] _inside_3_php = lxml.getroot().getchildren()[2] c = CompareXml(expectedNode=lxml, resultNode=lxml) self.assertEquals("{n:s/#}a", c.xpathToHere(_inside_1_php)) self.assertEquals("{n:s/#}a", c.xpathToHere(_inside_2_notphp)) self.assertEquals("{n:s/#}a", c.xpathToHere(_inside_3_php)) self.assertEquals("{n:s/#}a/processing-instruction('php')[1]", c.xpathToHere(_inside_1_php, includeCurrent=True)) self.assertEquals("{n:s/#}a/processing-instruction('notphp')", c.xpathToHere(_inside_2_notphp, includeCurrent=True)) self.assertEquals("{n:s/#}a/processing-instruction('php')[2]", c.xpathToHere(_inside_3_php, includeCurrent=True)) self.assertEquals("", c.xpathToHere(_pro_cessing)) self.assertEquals("", c.xpathToHere(_xml_style)) self.assertEquals("", c.xpathToHere(_pro_newline)) self.assertEquals("processing-instruction('pro')[1]", c.xpathToHere(_pro_cessing, includeCurrent=True)) self.assertEquals("processing-instruction('xml-stylesheet')", c.xpathToHere(_xml_style, includeCurrent=True)) self.assertEquals("processing-instruction('pro')[2]", c.xpathToHere(_pro_newline, includeCurrent=True))
def testAssertEqualsLxmlsXpathToHereWithProcessingInstructions(self): xml = '''\ <?pro cessing instruction?> <a xmlns="n:s/#"> <?php ...?> <?notphp Intermezzo?> <?php just kidding!?> <b/> </a> <?xml-stylesheet href="what.css" type="text/ever"?> <?pro with newlines?>''' lxml = parseString(xml) _pro_cessing = lxml.getroot().getprevious() _xml_style = lxml.getroot().getnext() _pro_newline = _xml_style.getnext() _inside_1_php = lxml.getroot().getchildren()[0] _inside_2_notphp = lxml.getroot().getchildren()[1] _inside_3_php = lxml.getroot().getchildren()[2] c = CompareXml(expectedNode=lxml, resultNode=lxml) self.assertEqual("{n:s/#}a", c.xpathToHere(_inside_1_php)) self.assertEqual("{n:s/#}a", c.xpathToHere(_inside_2_notphp)) self.assertEqual("{n:s/#}a", c.xpathToHere(_inside_3_php)) self.assertEqual("{n:s/#}a/processing-instruction('php')[1]", c.xpathToHere(_inside_1_php, includeCurrent=True)) self.assertEqual("{n:s/#}a/processing-instruction('notphp')", c.xpathToHere(_inside_2_notphp, includeCurrent=True)) self.assertEqual("{n:s/#}a/processing-instruction('php')[2]", c.xpathToHere(_inside_3_php, includeCurrent=True)) self.assertEqual("", c.xpathToHere(_pro_cessing)) self.assertEqual("", c.xpathToHere(_xml_style)) self.assertEqual("", c.xpathToHere(_pro_newline)) self.assertEqual("processing-instruction('pro')[1]", c.xpathToHere(_pro_cessing, includeCurrent=True)) self.assertEqual("processing-instruction('xml-stylesheet')", c.xpathToHere(_xml_style, includeCurrent=True)) self.assertEqual("processing-instruction('pro')[2]", c.xpathToHere(_pro_newline, includeCurrent=True))
def testAssertEqualsLxmlsXpathToHereWithCommentNodes(self): xml = '''\ <!-- 1st Comment --> <!-- Pre-Root Comment --> <a xmlns="n:s/#"> <!-- 1st Comment inside --> <!-- 2nd Comment inside --> <b/> </a> <!-- Post-Root Comment --> <!-- last Comment -->''' lxml = parseString(xml) _pre_root = lxml.getroot().getprevious() _1st = _pre_root.getprevious() _post_root = lxml.getroot().getnext() _last = _post_root.getnext() _1st_inside = lxml.getroot().getchildren()[0] _2nd_inside = lxml.getroot().getchildren()[1] c = CompareXml(expectedNode=lxml, resultNode=lxml) self.assertEqual('{n:s/#}a', c.xpathToHere(_1st_inside)) self.assertEqual('{n:s/#}a', c.xpathToHere(_2nd_inside)) self.assertEqual('{n:s/#}a/comment()[1]', c.xpathToHere(_1st_inside, includeCurrent=True)) self.assertEqual('{n:s/#}a/comment()[2]', c.xpathToHere(_2nd_inside, includeCurrent=True)) self.assertEqual('', c.xpathToHere(_1st)) self.assertEqual('', c.xpathToHere(_pre_root)) self.assertEqual('comment()[1]', c.xpathToHere(_1st, includeCurrent=True)) self.assertEqual('comment()[2]', c.xpathToHere(_pre_root, includeCurrent=True)) self.assertEqual('', c.xpathToHere(_post_root)) self.assertEqual('', c.xpathToHere(_last)) self.assertEqual('comment()[3]', c.xpathToHere(_post_root, includeCurrent=True)) self.assertEqual('comment()[4]', c.xpathToHere(_last, includeCurrent=True))