[Zope3-checkins] CVS: zopeproducts/xml/dom/tests - test_adapters.py:1.1

Philipp von Weitershausen philikon@philikon.de
Mon, 23 Jun 2003 14:58:46 -0400


Update of /cvs-repository/zopeproducts/xml/dom/tests
In directory cvs.zope.org:/tmp/cvs-serv28915/tests

Added Files:
	test_adapters.py 
Log Message:
The source attribute of the NodeToXMLTextAdapter is now writable. The XML
string to which it is set is parsed and used to replace to the node that is
being adapted. Added unit tests for that.


=== Added File zopeproducts/xml/dom/tests/test_adapters.py ===
##############################################################################
#
# Copyright (c) 2001-2003 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""
$Id: test_adapters.py,v 1.1 2003/06/23 18:58:45 philikon Exp $
"""

import unittest
import xml.dom
from StringIO import StringIO

from zopeproducts.xml.dom import expatbuilder
from zopeproducts.xml.dom.adapters import NodeToXMLTextAdapter

class NodeToXMLTextTests(unittest.TestCase):

    def setUp(self):
        self.source = '<?xml version="1.0" ?>\n<doc attr="attr">' \
                      '<bla><singleton/>&amp;</bla></doc>\n'
        f = StringIO(self.source)
        self.doc = expatbuilder.parse(f)    

    def tearDown(self):
        pass

    def test_get_source(self):
        xmltext = NodeToXMLTextAdapter(self.doc)
        self.assertEqual(xmltext.source, self.source)

    def test_get_node_source(self):
        node = self.doc.documentElement.firstChild
        xmltext = NodeToXMLTextAdapter(node)
        self.assertEqual('<bla><singleton/>&amp;</bla>', xmltext.source)

    def test_set_source(self):
        doc = self.doc
        xmltext = NodeToXMLTextAdapter(doc)
        self.assertRaises(xml.dom.HierarchyRequestErr, xmltext._set_source,
                          "<wont/><work/>")
        self.assertRaises(xml.dom.HierarchyRequestErr, xmltext._set_source, "")
        xmltext.source = "<mydoc><foo/></mydoc>"
        self.assertEqual(len(doc.childNodes), 1)
        self.assertEqual(len(doc.documentElement.childNodes), 1)
        self.assertEqual(doc.documentElement.tagName, 'mydoc')
        self.assertEqual(doc.documentElement.firstChild.tagName, 'foo')
        self.assertEqual(xmltext.source,
                         '<?xml version="1.0" ?>\n<mydoc><foo/></mydoc>\n')

    def test_set_node_source(self):
        doc = self.doc
        node = doc.documentElement.firstChild #bla element
        xmltext = NodeToXMLTextAdapter(node)
        self.assertRaises(xml.dom.HierarchyRequestErr, xmltext._set_source,
                          "<wont/><work/>")
        self.assertRaises(xml.dom.HierarchyRequestErr, xmltext._set_source, "")
        xmltext.source = "<mybla><foo/></mybla>"
        oldnode = node
        node = doc.documentElement.firstChild
        self.failIf(oldnode is node)
        self.assertEqual(len(doc.documentElement.childNodes), 1)
        self.assertEqual(node.tagName, 'mybla')
        self.assertEqual(node.firstChild.tagName, 'foo')
        self.assertEqual(xmltext.source, '<mybla><foo/></mybla>')

def test_suite():
    return unittest.makeSuite(NodeToXMLTextTests)