def test_append(self): div1 = Element('div') div2 = Element('div') div1.append(div2) self.assertEqual(1, len(div1)) childElement = div1[0] self.assertTrue(div2 is childElement) p = Element('p') p.append('some text') self.assertEqual(1, len(p)) textNode = p[0] self.assertTrue(isinstance(textNode, TextNode))
def test_getitem(self): className = 'description' attributes = {'class': className} div = Element('div', **attributes) p = Element('p') div.append(p) href = 'http://www.python.org' a = Element('a', href=href) div.append(a) self.assertIs(p, div[0]) self.assertEqual(div['class'], 'description') self.assertIs(a, div[1]) self.assertEqual(href, div[1]['href'])
def test_hasElementChildren(self): div = Element('div') div.append('foo') self.assertEqual(False, div.hasElementChild()) div = Element('div') div.append('text child') div.append(Element('hr')) self.assertEqual(True, div.hasElementChild())
def test_toprettyxml(self): svg = Element('svg') self.assertEqual('<svg/>', svg.toprettyxml()) svg.setAttribute('width', '200') self.assertEqual('<svg width="200"/>', svg.toprettyxml()) p = Element('p') p.append('some text') self.assertEqual('<p>some text</p>', p.toprettyxml()) div = Element('div') p = Element('p') p.append('one') div.append(p) self.assertEqual('<div>\n\t<p>one</p>\n</div>', div.toprettyxml()) p = Element('p') p.append('two') div.append(p) self.assertEqual('<div>\n\t<p>one</p>\n\t<p>two</p>\n</div>', div.toprettyxml())