示例#1
0
 def test_builder_empty_not_closed(self):
     builder = htree.TreeBuilder()
     builder.start('div')
     builder.data('before')
     builder.start('hr')
     with self.assertRaises(TypeError):
         builder.data('after')
示例#2
0
 def test_builder_multiple_children(self):
     builder = htree.TreeBuilder()
     builder.start('div')
     builder.start('p', id='1')
     builder.data('Paragraph 1')
     builder.end('p')
     builder.start('p', id='2')
     builder.data('Paragraph 2')
     builder.end('p')
     builder.start('p', id='3')
     builder.data('Paragraph 3')
     builder.end('p')
     builder.end('div')
     doc = builder.close()
     self.assertEqual(
         doc.to_string(),
         dedent(
             '''
             <div>
             <p id="1">Paragraph 1</p>
             <p id="2">Paragraph 2</p>
             <p id="3">Paragraph 3</p>
             </div>
             '''
         )
     )
示例#3
0
 def test_builder_Entity(self):
     builder = htree.TreeBuilder()
     builder.start('p')
     builder.data('&', node_type=htree.Entity)
     builder.end('p')
     doc = builder.close()
     self.assertEqual(doc.to_string(), '<p>&amp;</p>\n')
     self.assertTrue(isinstance(doc[0], htree.Entity))
示例#4
0
 def test_builder_Comment(self):
     builder = htree.TreeBuilder()
     builder.start('p')
     builder.data('a comment', node_type=htree.Comment)
     builder.end('p')
     doc = builder.close()
     self.assertEqual(doc.to_string(), '<p><!-- a comment --></p>\n')
     self.assertTrue(isinstance(doc[0], htree.Comment))
示例#5
0
 def test_builder_RawText(self):
     builder = htree.TreeBuilder()
     builder.start('p')
     builder.data('<em>bar</em>', node_type=htree.RawText)
     builder.end('p')
     doc = builder.close()
     self.assertEqual(doc.to_string(), '<p><em>bar</em></p>\n')
     self.assertTrue(isinstance(doc[0], htree.RawText))
示例#6
0
 def test_builder_Text(self):
     builder = htree.TreeBuilder()
     builder.start('p', id='foo')
     builder.data('bar')
     builder.end('p')
     doc = builder.close()
     self.assertEqual(doc.to_string(), '<p id="foo">bar</p>\n')
     self.assertTrue(isinstance(doc[0], htree.Text))
示例#7
0
 def test_builder_close_then_open(self):
     builder = htree.TreeBuilder()
     builder.start('div')
     builder.end('div')
     builder.start('p')
     builder.end('p')
     doc = builder.close()
     # The first root element is lost. Can only have one root.
     self.assertEqual(doc.to_string(),'<p></p>\n')
示例#8
0
 def test_builder_none_root(self):
     builder = htree.TreeBuilder()
     builder.start(None)
     builder.start('div')
     builder.end('div')
     builder.start('p')
     builder.end('p')
     builder.end(None)
     doc = builder.close()
     self.assertEqual(doc.to_string(), '<div></div>\n<p></p>\n')
示例#9
0
 def test_builder_empty_tag(self):
     builder = htree.TreeBuilder()
     builder.start('div')
     builder.data('before')
     builder.start('hr')
     builder.end('hr')  # <= Must explicitly close empty tag!
     builder.data('after')
     builder.end('div')
     doc = builder.close()
     self.assertEqual(
         doc.to_string(),
         dedent(
             '''
             <div>
             before<hr>
             after</div>
             '''
         )
     )
示例#10
0
 def test_builder_nesting(self):
     builder = htree.TreeBuilder()
     builder.start('p', id='foo')
     builder.data('Bar ')
     builder.start('em')
     builder.data('italics ')
     builder.start('strong')
     builder.data('bold ')
     builder.start('a', href='http://example.com')
     builder.data('link')
     builder.end('a')
     builder.data(' bold')
     builder.end('strong')
     builder.data(' italics')
     builder.end('em')
     builder.data('.')
     builder.end('p')
     doc = builder.close()
     self.assertEqual(
         doc.to_string(),
         '<p id="foo">Bar <em>italics <strong>bold '
         '<a href="http://example.com">link</a> '
         'bold</strong> italics</em>.</p>\n'
     )
示例#11
0
 def test_builder_data_prematurely(self):
     builder = htree.TreeBuilder()
     with self.assertRaises(htree.TreeBuilderError):
         builder.data('some text')
示例#12
0
 def test_builder_end_prematurely(self):
     builder = htree.TreeBuilder()
     with self.assertRaises(htree.TreeBuilderError):
         builder.end('p')
示例#13
0
 def test_builder_tag_mismatch(self):
     builder = htree.TreeBuilder()
     builder.start('div')
     with self.assertRaises(htree.TreeBuilderError):
         builder.end('p')
示例#14
0
 def test_builder_close_prematurely(self):
     builder = htree.TreeBuilder()
     builder.start('div')
     with self.assertRaises(htree.TreeBuilderError):
         builder.close()
示例#15
0
 def test_builder_close_no_open(self):
     builder = htree.TreeBuilder()
     with self.assertRaises(htree.TreeBuilderError):
         builder.close()