Beispiel #1
0
    def test__render(self):
        attrs = dict(attr1=xmlutil.ConstantSelector(1),
                     attr2=xmlutil.ConstantSelector(2),
                     attr3=xmlutil.ConstantSelector(3))

        # Create a master template element
        master_elem = xmlutil.TemplateElement('test', attr1=attrs['attr1'])

        # Create a couple of slave template element
        slave_elems = [
            xmlutil.TemplateElement('test', attr2=attrs['attr2']),
            xmlutil.TemplateElement('test', attr3=attrs['attr3']),
            ]

        # Try the render
        elem = master_elem._render(None, None, slave_elems, None)

        # Verify the particulars of the render
        self.assertEqual(elem.tag, 'test')
        self.assertEqual(len(elem.nsmap), 0)
        for k, v in elem.items():
            self.assertEqual(str(attrs[k].value), v)

        # Create a parent for the element to be rendered
        parent = etree.Element('parent')

        # Try the render again...
        elem = master_elem._render(parent, None, slave_elems, dict(a='foo'))

        # Verify the particulars of the render
        self.assertEqual(len(parent), 1)
        self.assertEqual(parent[0], elem)
        self.assertEqual(len(elem.nsmap), 1)
        self.assertEqual(elem.nsmap['a'], 'foo')
Beispiel #2
0
    def test_apply_attrs(self):
        # Create a template element
        attrs = dict(attr1=xmlutil.ConstantSelector(1),
                     attr2=xmlutil.ConstantSelector(2))
        tmpl_elem = xmlutil.TemplateElement('test', attrib=attrs)

        # Create an etree element
        elem = etree.Element('test')

        # Apply the template to the element
        tmpl_elem.apply(elem, None)

        # Now, verify the correct attributes were set
        for k, v in elem.items():
            self.assertEqual(str(attrs[k].value), v)
Beispiel #3
0
    def test_apply_text(self):
        # Create a template element
        tmpl_elem = xmlutil.TemplateElement('test')
        tmpl_elem.text = xmlutil.ConstantSelector(1)

        # Create an etree element
        elem = etree.Element('test')

        # Apply the template to the element
        tmpl_elem.apply(elem, None)

        # Now, verify the text was set
        self.assertEqual(str(tmpl_elem.text.value), elem.text)
Beispiel #4
0
    def test_render(self):
        # Create a template element
        tmpl_elem = xmlutil.TemplateElement('test')
        tmpl_elem.text = xmlutil.Selector()

        # Create the object we're going to render
        obj = ['elem1', 'elem2', 'elem3', 'elem4']

        # Try a render with no object
        elems = tmpl_elem.render(None, None)
        self.assertEqual(len(elems), 0)

        # Try a render with one object
        elems = tmpl_elem.render(None, 'foo')
        self.assertEqual(len(elems), 1)
        self.assertEqual(elems[0][0].text, 'foo')
        self.assertEqual(elems[0][1], 'foo')

        # Now, try rendering an object with multiple entries
        parent = etree.Element('parent')
        elems = tmpl_elem.render(parent, obj)
        self.assertEqual(len(elems), 4)

        # Check the results
        for idx in range(len(obj)):
            self.assertEqual(elems[idx][0].text, obj[idx])
            self.assertEqual(elems[idx][1], obj[idx])

        # Check with a subselector
        tmpl_elem = xmlutil.TemplateElement(
            'test',
            subselector=xmlutil.ConstantSelector('foo'))
        parent = etree.Element('parent')

        # Try a render with no object
        elems = tmpl_elem.render(parent, obj)
        self.assertEqual(len(elems), 4)
Beispiel #5
0
 def test_constant_selector(self):
     sel = xmlutil.ConstantSelector('Foobar')
     self.assertEqual(sel.value, 'Foobar')
     self.assertEqual(sel(self.obj_for_test), 'Foobar')
     self.assertEqual(repr(sel), "'Foobar'")