def test_isEqualToNode(self):
     """
     L{Document.isEqualToNode} returns C{True} if and only if passed a
     L{Document} with the same C{doctype} and C{documentElement}.
     """
     # A document is equal to itself
     document = microdom.Document()
     self.assertTrue(document.isEqualToNode(document))
     # A document without a doctype or documentElement is equal to another
     # document without a doctype or documentElement.
     another = microdom.Document()
     self.assertTrue(document.isEqualToNode(another))
     # A document with a doctype is not equal to a document without a
     # doctype.
     document.doctype = self.doctype
     self.assertFalse(document.isEqualToNode(another))
     # Two documents with the same doctype are equal
     another.doctype = self.doctype
     self.assertTrue(document.isEqualToNode(another))
     # A document with a documentElement is not equal to a document without
     # a documentElement
     document.appendChild(microdom.Node(object()))
     self.assertFalse(document.isEqualToNode(another))
     # Two documents with equal documentElements are equal.
     another.appendChild(microdom.Node(object()))
     self.assertTrue(document.isEqualToNode(another))
     # Two documents with documentElements which are not equal are not
     # equal.
     document.documentElement.appendChild(microdom.Node(object()))
     self.assertFalse(document.isEqualToNode(another))
 def test_childRestriction(self):
     """
     L{Document.appendChild} raises L{ValueError} if the document already
     has a child.
     """
     document = microdom.Document()
     child = microdom.Node()
     another = microdom.Node()
     document.appendChild(child)
     self.assertRaises(ValueError, document.appendChild, another)
 def test_validChildInstance(self):
     """
     Children of L{Node} instances must also be L{Node} instances.
     """
     node = microdom.Node()
     child = microdom.Node()
     # Node.appendChild() only accepts Node instances.
     node.appendChild(child)
     self.assertRaises(TypeError, node.appendChild, None)
     # Node.insertBefore() only accepts Node instances.
     self.assertRaises(TypeError, node.insertBefore, child, None)
     self.assertRaises(TypeError, node.insertBefore, None, child)
     self.assertRaises(TypeError, node.insertBefore, None, None)
     # Node.removeChild() only accepts Node instances.
     node.removeChild(child)
     self.assertRaises(TypeError, node.removeChild, None)
     # Node.replaceChild() only accepts Node instances.
     self.assertRaises(TypeError, node.replaceChild, child, None)
     self.assertRaises(TypeError, node.replaceChild, None, child)
     self.assertRaises(TypeError, node.replaceChild, None, None)
 def test_isNodeEqualTo(self):
     """
     L{Node.isEqualToNode} returns C{True} if and only if passed a L{Node}
     with the same children.
     """
     # A node is equal to itself
     node = microdom.Node(object())
     self.assertTrue(node.isEqualToNode(node))
     another = microdom.Node(object())
     # Two nodes with no children are equal
     self.assertTrue(node.isEqualToNode(another))
     node.appendChild(microdom.Node(object()))
     # A node with no children is not equal to a node with a child
     self.assertFalse(node.isEqualToNode(another))
     another.appendChild(microdom.Node(object()))
     # A node with a child and no grandchildren is equal to another node
     # with a child and no grandchildren.
     self.assertTrue(node.isEqualToNode(another))
     # A node with a child and a grandchild is not equal to another node
     # with a child and no grandchildren.
     node.firstChild().appendChild(microdom.Node(object()))
     self.assertFalse(node.isEqualToNode(another))
     # A node with a child and a grandchild is equal to another node with a
     # child and a grandchild.
     another.firstChild().appendChild(microdom.Node(object()))
     self.assertTrue(node.isEqualToNode(another))
    def test_isEqualToNode(self):
        """
        L{Element.isEqualToNode} returns C{True} if and only if passed a
        L{Element} with the same C{nodeName}, C{namespace}, C{childNodes}, and
        C{attributes}.
        """
        self.assertTrue(
            microdom.Element("foo", {
                "a": "b"
            }, object(), namespace="bar").isEqualToNode(
                microdom.Element("foo", {"a": "b"}, object(),
                                 namespace="bar")))

        # Elements with different nodeName values do not compare equal.
        self.assertFalse(
            microdom.Element("foo", {
                "a": "b"
            }, object(), namespace="bar").isEqualToNode(
                microdom.Element("bar", {"a": "b"}, object(),
                                 namespace="bar")))

        # Elements with different namespaces do not compare equal.
        self.assertFalse(
            microdom.Element("foo", {
                "a": "b"
            }, object(), namespace="bar").isEqualToNode(
                microdom.Element("foo", {"a": "b"}, object(),
                                 namespace="baz")))

        # Elements with different childNodes do not compare equal.
        one = microdom.Element("foo", {"a": "b"}, object(), namespace="bar")
        two = microdom.Element("foo", {"a": "b"}, object(), namespace="bar")
        two.appendChild(microdom.Node(object()))
        self.assertFalse(one.isEqualToNode(two))

        # Elements with different attributes do not compare equal.
        self.assertFalse(
            microdom.Element("foo", {
                "a": "b"
            }, object(), namespace="bar").isEqualToNode(
                microdom.Element("foo", {"a": "c"}, object(),
                                 namespace="bar")))