def test_xmlparser_CustomXMLParser_contains(self): iparse = xmlparser.CustomXMLParser() self.assertFalse("root" in iparse) res = iparse.load(self.dummyXML, sourceIsFile=False) self.assertNotEqual(res, None) self.assertTrue("root" in iparse)
def test_xmlparser_CustomXMLParser_getRoot(self): iparse = xmlparser.CustomXMLParser() res = iparse.load(self.dummyXML, sourceIsFile=False) self.assertNotEqual(res, None) root = iparse.getRoot() self.assertEqual(root.getData(), "root") self.assertEqual(root.numChildren(), 3)
def test_xmlparser_CustomXMLParser_getitem(self): iparse = xmlparser.CustomXMLParser() self.assertEqual(iparse["root"], None) res = iparse.load(self.dummyXML, sourceIsFile=False) self.assertNotEqual(res, None) root = iparse["root"] self.assertEqual(root["a"], "b") self.assertEqual(root["d"].numChildren(), 3)
def test_xmlparser_load_singletag_xml(self): singletag = """<first> <tag name="%s" /> </first> """ iparse = xmlparser.CustomXMLParser() res = iparse.load(singletag % 'tmp', sourceIsFile=False) self.assertNotEqual(type(res), list) self.assertEqual(len(res.getRoot()), 1) self.assertEqual(len(res), len(res.getRoot())) assertEqualString(res.getRoot().toString(), singletag % 'tmp', ignoreSpace=True)
def test_xmlparser_load_invalid_file_raise_IOError(self): iparse = xmlparser.CustomXMLParser() try: orig_open = __builtin__.open __builtin__.open = lambda filen, mode: self._raise_exception( IOError("Error")) self.assertRaisesRegexp(ValueError, 'File dummy_file not found!', iparse.load, 'dummy_file', sourceIsFile=True) except: raise finally: __builtin__.open = orig_open
def test_xmlparser_load_invalid_xml_string_raise_IOError(self): singletag = """<first> <tag name=" </first> """ try: sys.stdout = StringIO() iparse = xmlparser.CustomXMLParser() self.assertRaisesRegexp(ValueError, 'Input is not valid XML: not well-formed', iparse.load, singletag, sourceIsFile=False) except: raise finally: sys.stdout = sys.__stdout__
def test_xmlparser_start_and_end_basic(self): data = """<tag1> <tag2 a="1" b="2" c="3" d="4" /> <tag3 attr1="1234567890" attr2="abcd1234" /> </tag1> """ tag2_attr = {'b': '2', 'c': '3', 'd': '4', 'a': '1'} tag3_attr = {'attr1': '1234567890', 'attr2': 'abcd1234'} iparse = xmlparser.CustomXMLParser() iparse.start('tag1', None) iparse.end('tag1') iparse.start('tag2', tag2_attr) iparse.end('tag2') iparse.start('tag3', tag3_attr) iparse.end('tag3') assertEqualString(iparse.getRoot().toString(), data, ignoreSpace=True)
def test_xmlparser_load_invalid_xml_file_raise_IOError(self): singletag = """<first> <tag name= </first> """ iparse = xmlparser.CustomXMLParser() io = StringIO(singletag) try: orig_open = __builtin__.open __builtin__.open = lambda filen, mode: self._dummy_return(io) self.assertRaisesRegexp( ValueError, 'Input is not valid XML: dummy_file, not well-formed', iparse.load, 'dummy_file', sourceIsFile=True) except: raise finally: __builtin__.open = orig_open
def test_xmlparser_load_invalid_file_no_IOError_only_print(self): iparse = xmlparser.CustomXMLParser() try: sys.stdout = StringIO() orig_open = __builtin__.open __builtin__.open = lambda filen, mode: self._raise_exception( IOError("Error")) iparse.ignoreErrors(True) ret = iparse.load('dummy_file', sourceIsFile=True) self.assertEqual(ret, None) output = sys.stdout.getvalue().strip() self.assertEqual(output, "ERROR: File dummy_file not found!") except: raise finally: __builtin__.open = orig_open sys.stdout = sys.__stdout__
def test_xmlparser_load_singletag_xml_with_comment_lines(self): singletag = """<!-- THIS IS COMMENT --> <first> <tag name="tmp" /> <!-- ANOTHER COMMENT WITH TWO LINES --> </first> """ expected_output = """<first> <!-- THIS IS COMMENT --> <tag name="tmp" /> <!-- ANOTHER COMMENT WITH TWO LINES --> </first> """ iparse = xmlparser.CustomXMLParser() res = iparse.load(singletag, sourceIsFile=False) assertEqualString(res.getRoot().toString(), expected_output, ignoreSpace=True)
def test_xmlparser_load_multitag_xml_special_with_addDummy(self): tag1 = """<first> <tag args="tmp1" /> </first> """ tag2 = """<second> <tag /> <tag name="tmp2" /> </second> """ multitag_special = tag1 + tag2 multitag_special_with_dummy = """<dummy> <first> <tag args="tmp1" /> </first> <second> <tag /> <tag name="tmp2" /> </second> </dummy> """ parse = xmlparser.CustomXMLParser() res = parse.load(multitag_special, sourceIsFile=False, addDummy=True) self.assertNotEqual(type(res), list) assertEqualString(res.getRoot().toString(), multitag_special_with_dummy, ignoreSpace=True) res = res.getRoot().getChildren() self.assertEqual(type(res), list) self.assertEqual(len(res), 2) self.assertEqual(res[0].getData(), "first") self.assertEqual(res[1].getData(), "second") assertEqualString(res[0].toString(), tag1, ignoreSpace=True) assertEqualString(res[1].toString(), tag2, ignoreSpace=True)
def test_xmlparser_len_empty_zero(self): iparse = xmlparser.CustomXMLParser() self.assertEqual(len(iparse), 0)