def test_optional_attributes(self): doc = Element("root", bar=(bool_attr, "optional")) dom = doc.parse_xml_str('<root />') self.assertEquals(hasattr(dom, "bar"), False) dom = doc.parse_xml_str('<root bar="true" />') self.assertEquals(hasattr(dom, "bar"), True) self.assertEquals(dom.bar, True)
def test_bool_attributes(self): doc = Element("root", foo=(bool_attr, "required")) dom = doc.parse_xml_str('<root foo="true" />') self.assertEquals(dom.foo, True) dom = doc.parse_xml_str('<root foo="false" />') self.assertEquals(dom.foo, False) self.assertRaises(EzXMLError, doc.parse_xml_str, '<root foo="foo" />')
def test_inclusion(self): """Test the processing of the include element.""" child = Element("child", name=(str_attr, "required")) doc = Element("root", child) # Test file not found. self.assertRaises(EzXMLError, doc.parse_xml_str, '<root><include file="nofile" /></root>') # Test a simple include. dom = doc.parse_xml_str('<root><include file="data/ezxml_include.xml" /></root>') # Include element should have been removed. includes = dom.find_children("include") self.assertEqual(len(includes), 0) child_els = dom.find_children("child") self.assertEqual(len(child_els), 1) self.assertEqual(child_els[0].name, "included") # Test including a file with multiple elements and thatalso # includes another file. dom = doc.parse_xml_str('<root><include file="data/ezxml_include2.xml" /></root>') # Include element should have been removed. includes = dom.find_children("include") self.assertEqual(len(includes), 0) child_els = dom.find_children("child") self.assertEqual(len(child_els), 7) self.assertEqual(child_els[0].name, "included1") self.assertEqual(child_els[6].name, "included")
def test_inclusion(self): """Test the processing of the include element.""" child = Element("child", name=(str_attr, "required")) doc = Element("root", child) # Test file not found. self.assertRaises(EzXMLError, doc.parse_xml_str, '<root><include file="nofile" /></root>') # Test a simple include. dom = doc.parse_xml_str( '<root><include file="data/ezxml_include.xml" /></root>') # Include element should have been removed. includes = dom.find_children("include") self.assertEqual(len(includes), 0) child_els = dom.find_children("child") self.assertEqual(len(child_els), 1) self.assertEqual(child_els[0].name, "included") # Test including a file with multiple elements and thatalso # includes another file. dom = doc.parse_xml_str( '<root><include file="data/ezxml_include2.xml" /></root>') # Include element should have been removed. includes = dom.find_children("include") self.assertEqual(len(includes), 0) child_els = dom.find_children("child") self.assertEqual(len(child_els), 7) self.assertEqual(child_els[0].name, "included1") self.assertEqual(child_els[6].name, "included")
def test_size_attributes(self): doc = Element("root", foo=(size_attr, "required")) dom = doc.parse_xml_str('<root foo="1234" />') self.assertEquals(dom.foo, 1234) dom = doc.parse_xml_str('<root foo="1K" />') self.assertEquals(dom.foo, 1024L) dom = doc.parse_xml_str('<root foo="1M" />') self.assertEquals(dom.foo, 1024L * 1024L) dom = doc.parse_xml_str('<root foo="1g" />') self.assertEquals(dom.foo, 1024L * 1024L * 1024L) self.assertRaises(EzXMLError, doc.parse_xml_str, '<root foo="foo" />')
def test_find_children(self): child = Element("child", name=(str_attr, "required")) doc = Element("root", child) dom = doc.parse_xml_str('<root><child name="1" /></root>') child_els = dom.find_children("child") self.assertEqual(len(child_els), 1) self.assertEqual(child_els[0].name, "1") dom = doc.parse_xml_str('<root></root>') child_els = dom.find_children("child") self.assertEqual(len(child_els), 0) dom = doc.parse_xml_str('<root><child name="1" /><child name="2" /></root>') child_els = dom.find_children("child") self.assertEqual(len(child_els), 2) self.assertEqual(child_els[0].name, "1") self.assertEqual(child_els[1].name, "2")
def test_find_children(self): child = Element("child", name=(str_attr, "required")) doc = Element("root", child) dom = doc.parse_xml_str('<root><child name="1" /></root>') child_els = dom.find_children("child") self.assertEqual(len(child_els), 1) self.assertEqual(child_els[0].name, "1") dom = doc.parse_xml_str('<root></root>') child_els = dom.find_children("child") self.assertEqual(len(child_els), 0) dom = doc.parse_xml_str( '<root><child name="1" /><child name="2" /></root>') child_els = dom.find_children("child") self.assertEqual(len(child_els), 2) self.assertEqual(child_els[0].name, "1") self.assertEqual(child_els[1].name, "2")
def test_find_child(self): child = Element("child", name=(str_attr, "required")) doc = Element("root", child) # 1 child dom = doc.parse_xml_str('<root><child name="1" /></root>') child_el = dom.find_child("child") self.assertEquals(child_el.tag, "child") # 0 children dom = doc.parse_xml_str('<root></root>') child_el = dom.find_child("child") self.assertEquals(child_el, None) # Multiple chidren dom = doc.parse_xml_str('<root><child name="2" /><child name="1" /></root>') child_el = dom.find_child("child") self.assertEquals(child_el.tag, "child") self.assertEquals(child_el.name, "2")
def test_find_child(self): child = Element("child", name=(str_attr, "required")) doc = Element("root", child) # 1 child dom = doc.parse_xml_str('<root><child name="1" /></root>') child_el = dom.find_child("child") self.assertEquals(child_el.tag, "child") # 0 children dom = doc.parse_xml_str('<root></root>') child_el = dom.find_child("child") self.assertEquals(child_el, None) # Multiple chidren dom = doc.parse_xml_str( '<root><child name="2" /><child name="1" /></root>') child_el = dom.find_child("child") self.assertEquals(child_el.tag, "child") self.assertEquals(child_el.name, "2")
def test_find_all_children(self): leaf = Element("leaf", name=(str_attr, "required")) child = Element("child", leaf, name=(str_attr, "required")) doc = Element("root", child, leaf) dom = doc.parse_xml_str(""" <root> <child name="1"> <leaf name="bottom_leaf" /> </child> <leaf name="top_leaf" /> </root>""") leaf_els = dom.find_all_children("leaf") self.assertEqual(len(leaf_els), 2) self.assertEqual(leaf_els[0].name, "bottom_leaf") self.assertEqual(leaf_els[1].name, "top_leaf") dom = doc.parse_xml_str('<root></root>') leaf_els = dom.find_children("leaf") self.assertEqual(len(leaf_els), 0)
def test_dump(self): # Test the dumped output. output = """\ <root > <child name="1" > </child> <child value="0x32" name="2" > </child> </root> """ child = Element("child", name=(str_attr, "required"), value=(long_attr, "optional")) doc = Element("root", child) dom = doc.parse_xml_str('<root><child name="1" /><child name="2" value="50"/></root>') self.assertEqual(dom.dump(), output)
def test_dump(self): # Test the dumped output. output = """\ <root > <child name="1" > </child> <child value="0x32" name="2" > </child> </root> """ child = Element("child", name=(str_attr, "required"), value=(long_attr, "optional")) doc = Element("root", child) dom = doc.parse_xml_str( '<root><child name="1" /><child name="2" value="50"/></root>') self.assertEqual(dom.dump(), output)
def test_ignoring_comments(self): doc = Element("root") parsed = doc.parse_xml_str("<root><!-- This is a comment --></root>") self.assertEquals(parsed.tag, "root") self.assertEquals(len(parsed.children), 0)
def test_simple_child(self): doc = Element("root", Element("child")) dom = doc.parse_xml_str('<root><child /></root>') self.assertEquals(len(dom.children), 1)
def test_basic_usage(self): doc = Element("root") parsed = doc.parse_xml_str("<root />") self.assertEquals(parsed.tag, "root") parsed = doc.parse_xml_file("data/ezxml.1") self.assertEquals(parsed.tag, "root")
def test_repr(self): doc = Element("root", foo=(bool_attr, "required"), bar=(long_attr, "required")) parse = doc.parse_xml_str("<root foo='true' bar='16' />") self.assertEquals(repr(parse), '<root foo="True" bar="0x10" >')
def test_empty_tag(self): doc = Element("root") doc.parse_xml_str('<root> </root>')
def test_str_attributes(self): doc = Element("root", foo=(str_attr, "required")) dom = doc.parse_xml_str('<root foo="1234" />') self.assertEquals(dom.foo, "1234")
def test_repr(self): doc = Element("root", foo=(bool_attr, "required"), bar=(long_attr, "required")) parse = doc.parse_xml_str("<root foo='true' bar='16' />") self.assertEquals(repr(parse), '<root bar="0x10" foo="True" >')
def test_long_attributes(self): doc = Element("root", foo=(long_attr, "required")) dom = doc.parse_xml_str('<root foo="1234" />') self.assertEquals(dom.foo, 1234) self.assertRaises(EzXMLError, doc.parse_xml_str, '<root foo="foo" />')