def test_render_condensed_node(self): parent = nodes.HtmlNode('div', condensed=True) child = nodes.HtmlNode('p') text = nodes.TextNode('content') parent.add_child(child) child.add_child(text) lines = parent.render_lines() self.assertEqual(['<div><p>', 'content', '</p></div>'], lines)
def test_nested(self): parent = nodes.HtmlNode('div') child = nodes.HtmlNode('p') text = nodes.TextNode('content') parent.add_child(child) child.add_child(text) lines = parent.render_lines() self.assertEqual(['<div>', '<p>', 'content', '</p>', '</div>'], lines)
def test_render_condensed_node_child(self): parent = nodes.HtmlNode('div') child1 = nodes.HtmlNode('p', condensed=True) child2 = nodes.HtmlNode('span') child1.add_child(nodes.TextNode('ptext')) child2.add_child(nodes.TextNode('spantext')) parent.add_child(child1) parent.add_child(child2) lines = parent.render_lines() self.assertEqual([ '<div>', '<p>ptext</p>', '<span>', 'spantext', '</span>', '</div>' ], lines)
def test_multiple_trees(self): parent = nodes.HtmlNode('div') child1 = nodes.HtmlNode('p') child2 = nodes.HtmlNode('span') child1.add_child(nodes.TextNode('ptext')) child2.add_child(nodes.TextNode('spantext')) parent.add_child(child1) parent.add_child(child2) lines = parent.render_lines() self.assertEqual([ '<div>', '<p>', 'ptext', '</p>', '<span>', 'spantext', '</span>', '</div>' ], lines)
def test_duplicate_attributes_only_allowed_for_class(self): node = nodes.HtmlNode('div') self.assertEqual({}, node.attributes) node.add_attribute('foo', 'bar') self.assertEqual({'foo': 'bar'}, node.attributes) with self.assertRaises(KeyError): node.add_attribute('foo', 'other') self.assertEqual({'foo': 'bar'}, node.attributes) node.add_attribute('class', 'cls1') self.assertEqual({'foo': 'bar', 'class': 'cls1'}, node.attributes) node.add_attribute('class', 'cls2') self.assertEqual({'foo': 'bar', 'class': 'cls1 cls2'}, node.attributes)
def test_simple(self): node = nodes.HtmlNode('div') lines = node.render_lines() self.assertEqual(['<div>', '</div>'], lines)
def test_simple_content(self): node = nodes.HtmlNode('div') node.add_child(nodes.TextNode('content')) lines = node.render_lines() self.assertEqual(['<div>', 'content', '</div>'], lines)
def test_stores_tag_and_attribute_dictionary(self): node = nodes.HtmlNode('div', {'class': 'foo'}) self.assertEqual('div', node.tag) self.assertEqual({'class': 'foo'}, node.attributes)
def test_render_end(self): node = nodes.HtmlNode('div', {'a': '1', 'b': '2'}) self.assertEqual('</div>', node.render_end()) node = nodes.HtmlNode('div') self.assertEqual('</div>', node.render_end())
def test_render_start(self): node = nodes.HtmlNode('div', {'a': '1', 'b': '2'}) self.assertEqual('<div a="1" b="2">', node.render_start()) node = nodes.HtmlNode('div') self.assertEqual('<div>', node.render_start())
def test_render_attributes(self): node = nodes.HtmlNode('div', {'a': '1', 'b': '2'}) self.assertEqual('a="1" b="2"', node.render_attributes())
def test_attributes_are_optional(self): node = nodes.HtmlNode('div') self.assertEqual('div', node.tag) self.assertEqual({}, node.attributes)
def test_render_condensed_single_node(self): node = nodes.HtmlNode('div', condensed=True) self.assertEqual(['<div></div>'], node.render_lines())