Package lxml :: Package tests :: Module test_builder |
[hide private]
[frames] | no frames]

# Source Code for Module lxml.tests.test_builder

     1  # -*- coding: utf-8 -*-
     2  import unittest
     3
     4  """
     5  Tests that ElementMaker works properly.
     6  """
     7
     8  import sys, os.path
     9  from lxml import etree
    10  from lxml.builder import E
    11
    12  this_dir = os.path.dirname(__file__)
    13  if this_dir not in sys.path:
    14      sys.path.insert(0, this_dir) # needed for Py3
    15
    16  from common_imports import HelperTestCase, BytesIO, _bytes
    17
    18
    19 -class BuilderTestCase(HelperTestCase):
    20      etree = etree
    21
    22 -    def test_build_from_xpath_result(self):
    23          class StringSubclass(str): pass
    24          wrapped = E.b(StringSubclass('Hello'))
    25          self.assertEqual(_bytes('<b>Hello</b>'), etree.tostring(wrapped))
    26
    27 -    def test_unknown_type_raises(self):
    28          class UnknownType(object):
    29              pass
    30          self.assertRaises(TypeError, E.b, UnknownType())
    31
    32 -    def test_cdata(self):
    33          wrapped = E.b(etree.CDATA('Hello'))
    34          self.assertEqual(_bytes('<b><![CDATA[Hello]]></b>'), etree.tostring(wrapped))
    35
    36 -    def test_cdata_solo(self):
    37          self.assertRaises(ValueError, E.b, 'Hello', etree.CDATA('World'))
    38
    39
    40 -def test_suite():
    41      suite = unittest.TestSuite()
    42      suite.addTests([unittest.makeSuite(BuilderTestCase)])
    43      return suite
    44
    45  if __name__ == '__main__':
    46      print('to test use test.py %s' % __file__)
    47