Bug 345700 - Tests for Node.isEqualNode. r+sr=bz

This commit is contained in:
jwalden%mit.edu 2006-07-30 20:55:58 +00:00
Родитель 285beff7f9
Коммит cda65e0199
3 изменённых файлов: 610 добавлений и 0 удалений

Просмотреть файл

@ -50,6 +50,7 @@ const nsIDOMSerializer = I.nsIDOMSerializer;
const nsIDOMDocument = I.nsIDOMDocument;
const nsIDOMElement = I.nsIDOMElement;
const nsIDOMNode = I.nsIDOMNode;
const nsIDOM3Node = I.nsIDOM3Node;
const nsIDOMCharacterData = I.nsIDOMCharacterData;
const nsIDOMAttr = I.nsIDOMAttr;
const nsIDOMNodeList = I.nsIDOMNodeList;

Просмотреть файл

@ -0,0 +1,150 @@
<?xml version="1.0" ?>
<!DOCTYPE Test [
<!ATTLIST test id ID #REQUIRED>
]>
<root>
<test id="test_setAttribute">
<foo/>
<foo/>
</test>
<test id="test_normalization">
<bar/>
<bar/>
</test>
<test id="test_whitespace">
<!--
Tests here consist of isEqualNode comparisons of the first and third
(zero-indexed) child nodes of each test.
In the typical case this means that the zeroth, second, and fourth
children are whitespace and the first and third are the nodes being
compared for equality or inequality.
In some cases, however, where either node is a Text node, this pattern
must of necessity be violated. Examples of such tests include the
test_text# tests.
As a result of this, BE CAREFUL NOT TO INTRODUCE STRAY WHITESPACE WHEN
EDITING THIS FILE. You have been warned.
-->
<test id="test_pi1">
<?pi data?>
<?pi data?>
</test>
<test id="test_pi2">
<?pi data?>
<?pi data?>
</test>
<test id="test_pi3">
<?pi data?>
<?pi data ?>
</test>
<test id="test_pi4">
<?pi ?>
<?pi ?>
</test>
<test id="test_pi5">
<?pi?>
<?pi ?>
</test>
<test id="test_elt1">
<foo></foo>
<foo> </foo>
</test>
<test id="test_elt2">
<foo></foo>
<foo>
</foo>
</test>
<test id="test_elt3">
<foo ></foo>
<foo></foo>
</test>
<test id="test_elt4">
<bar xmlns="http://example.com/"/>
<bar/>
</test>
<test id="test_elt5">
<bar xmlns="http://example.com/"/>
<bar xmlns="http://example.com"/>
</test>
<test id="test_comment1">
<!--foo-->
<!--foo-->
</test>
<test id="test_comment2">
<!--foo-->
<!--foo -->
</test>
<test id="test_comment3">
<!--foo-->
<!--foo
-->
</test>
<test id="test_comment4">
<!--
foo-->
<!--
foo-->
</test>
<test id="test_text1"><placeholder-dont-move/>
<placeholder-dont-move/>
<placeholder-dont-move/>
</test>
<test id="test_text2"><placeholder-dont-move/>
<placeholder-dont-move/> <placeholder-dont-move/>
</test>
<test id="test_text3"><placeholder-dont-move/>
<placeholder-dont-move/><![CDATA[
]]>
</test>
<test id="test_cdata1">
<![CDATA[ ]]><placeholder-dont-move/> <placeholder-dont-move/>
</test>
<test id="test_cdata2">
<![CDATA[ ]]>
<![CDATA[ ]]>
</test>
<test id="test_cdata3">
<![CDATA[ ]]>
<![CDATA[ ]]>
</test>
<test id="test_cdata4">
<![CDATA[]]>
<![CDATA[
]]>
</test>
<test id="test_cdata5">
<![CDATA[ ]]>
<![CDATA[
]]>
</test>
</test>
<test id="test_namespaces">
<test id="test_ns1">
<foo xmlns:quiz="http://example.com/"
quiz:q="fun"/>
<foo xmlns:f="http://example.com/"
f:q="fun"/>
</test>
<test id="test_ns2">
<quiz:foo xmlns:quiz="http://example.com/"
q="fun"/>
<f:foo xmlns:f="http://example.com/"
q="fun"/>
</test>
</test>
</root>

Просмотреть файл

@ -0,0 +1,459 @@
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is DOM3 isEqualNode test code.
*
* The Initial Developer of the Original Code is
* Mozilla Corporation.
* Portions created by the Initial Developer are Copyright (C) 2006
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Jeff Walden <jwalden+code@mit.edu> (original author)
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
function run_test()
{
/*
* NOTE: [i] is not allowed in this test, since it's done via classinfo and
* we don't have that in xpcshell; the workaround is item(i). Suck.
*/
init();
test_isEqualNode_setAttribute();
test_isEqualNode_clones();
test_isEqualNode_variety();
test_isEqualNode_normalization();
test_isEqualNode_whitespace();
test_isEqualNode_namespaces();
// XXX should Node.isEqualNode(null) throw or return false?
//test_isEqualNode_null();
}
// TEST CODE
var doc, root; // cache for use in all tests
function init()
{
doc = ParseFile("isequalnode_data.xml");
root = doc.documentElement;
}
function test_isEqualNode_setAttribute()
{
// NOTE: 0, 2 are whitespace
var test1 = doc.getElementById("test_setAttribute");
var node1 = test1.childNodes.item(1);
var node2 = test1.childNodes.item(3);
check_eq_nodes(node1, node2);
el(node1).setAttribute("bar", "baz");
check_neq_nodes(node1, node2);
el(node2).setAttribute("bar", "baz");
check_eq_nodes(node1, node2);
// the null namespace is equivalent to no namespace -- section 1.3.3
// (XML Namespaces) of DOM 3 Core
node1.setAttributeNS(null, "quux", "17");
check_neq_nodes(node1, node2);
node2.setAttribute("quux", "17");
check_eq_nodes(node1, node2);
node2.setAttributeNS("http://mozilla.org/", "seamonkey", "rheet");
check_neq_nodes(node1, node2);
node1.setAttribute("seamonkey", "rheet");
check_neq_nodes(node1, node2);
node1.setAttributeNS("http://mozilla.org/", "seamonkey", "rheet");
check_neq_nodes(node1, node2);
// this overwrites the namespaced "seamonkey" attribute added to node2
// earlier, because this simply sets whatever attribute has the fully
// qualified name "seamonkey" (the setAttributeNS attribute string wasn't
// prefixed) -- consequently, node1 and node2 are still unequal
node2.setAttribute("seamonkey", "rheet");
check_neq_nodes(node1, node2);
}
function test_isEqualNode_clones()
{
// tests all elements and attributes in the document
var all_elts = doc.getElementsByTagName("*");
for (var i = 0; i < all_elts.length; i++)
{
var elt = el(all_elts.item(i));
check_eq_nodes(elt, elt.cloneNode(true));
var attrs = elt.attributes;
for (var j = 0; j < attrs.length; j++)
{
var attr = attrs.item(j);
check_eq_nodes(attr, attr.cloneNode(true));
}
}
var elm = doc.createElement("foo");
check_eq_nodes(elm, elm.cloneNode(true));
check_eq_nodes(elm, elm.cloneNode(false));
elm.setAttribute("fiz", "eit");
check_eq_nodes(elm, elm.cloneNode(true));
check_eq_nodes(elm, elm.cloneNode(false));
elm.setAttributeNS("http://example.com/", "trendoid", "arthroscope");
check_eq_nodes(elm, elm.cloneNode(true));
check_eq_nodes(elm, elm.cloneNode(false));
var elm2 = elm.cloneNode(true);
check_eq_nodes(elm, elm2);
const TEXT = "fetishist";
elm.textContent = TEXT;
check_neq_nodes(elm, elm2);
check_neq_nodes(elm, elm.cloneNode(false));
check_eq_nodes(elm, elm.cloneNode(true));
elm2.appendChild(doc.createTextNode(TEXT));
check_eq_nodes(elm, elm2);
var att = doc.createAttribute("bar");
check_eq_nodes(att, att.cloneNode(true));
check_eq_nodes(att, att.cloneNode(false));
}
function test_isEqualNode_variety()
{
const nodes =
[
doc.createElement("foo"),
doc.createElementNS("http://example.com/", "foo"),
doc.createElementNS("http://example.org/", "foo"),
doc.createElementNS("http://example.com/", "FOO"),
doc.createAttribute("foo", "href='biz'"),
doc.createAttributeNS("http://example.com/", "foo", "href='biz'"),
doc.createTextNode("foo"),
doc.createTextNode(" "),
doc.createTextNode(" "),
doc.createComment("foo"),
doc.createProcessingInstruction("foo", "href='biz'"),
doc.implementation.createDocumentType("foo", "href='biz'", ""),
doc.implementation.createDocument("http://example.com/", "foo", null),
doc.createDocumentFragment()
];
for (var i = 0; i < nodes.length; i++)
{
for (var j = i; j < nodes.length; j++)
{
if (i == j)
check_eq_nodes(nodes[i], nodes[j]);
else
check_neq_nodes(nodes[i], nodes[j]);
}
}
}
function test_isEqualNode_normalization()
{
var norm = doc.getElementById("test_normalization");
var node1 = norm.childNodes.item(1);
var node2 = norm.childNodes.item(3);
check_eq_nodes(node1, node2);
node1.appendChild(doc.createTextNode(""));
check_neq_nodes(node1, node2);
node1.normalize();
check_eq_nodes(node1, node2);
node2.appendChild(doc.createTextNode("fun"));
node2.appendChild(doc.createTextNode("ctor"));
node1.appendChild(doc.createTextNode("functor"));
check_neq_nodes(node1, node2);
node1.normalize();
check_neq_nodes(node1, node2);
node2.normalize();
check_eq_nodes(node1, node2);
// reset
while (node1.hasChildNodes())
node1.removeChild(node1.childNodes.item(0));
while (node2.hasChildNodes())
node2.removeChild(node2.childNodes.item(0));
// attribute normalization testing
var at1 = doc.createAttribute("foo");
var at2 = doc.createAttribute("foo");
check_eq_nodes(at1, at2);
// Attr.appendChild isn't implemented yet (bug 56758), so don't run this yet
if (false)
{
at1.appendChild(doc.createTextNode("rasp"));
at2.appendChild(doc.createTextNode("rasp"));
check_eq_nodes(at1, at2);
at1.appendChild(doc.createTextNode(""));
check_neq_nodes(at1, at2);
at1.normalize();
check_eq_nodes(at1, at2);
at1.appendChild(doc.createTextNode("berry"));
check_neq_nodes(at1, at2);
at2.appendChild(doc.createTextNode("ber"));
check_neq_nodes(at1, at2);
at2.appendChild(doc.createTextNode("ry"));
check_neq_nodes(at1, at2);
at1.normalize();
check_neq_nodes(at1, at2);
at2.normalize();
check_eq_nodes(at1, at2);
}
node1.setAttributeNode(at1);
check_neq_nodes(node1, node2);
node2.setAttributeNode(at2);
check_eq_nodes(node1, node2);
var n1text1 = doc.createTextNode("ratfink");
var n1elt = doc.createElement("fruitcake");
var n1text2 = doc.createTextNode("hydrospanner");
node1.appendChild(n1text1);
node1.appendChild(n1elt);
node1.appendChild(n1text2);
check_neq_nodes(node1, node2);
var n2text1a = doc.createTextNode("rat");
var n2text1b = doc.createTextNode("fink");
var n2elt = doc.createElement("fruitcake");
var n2text2 = doc.createTextNode("hydrospanner");
node2.appendChild(n2text1b);
node2.appendChild(n2elt);
node2.appendChild(n2text2);
check_neq_nodes(node1, node2);
node2.insertBefore(n2text1a, n2text1b);
check_neq_nodes(node1, node2);
var tmp_node1 = node1.cloneNode(true);
tmp_node1.normalize();
var tmp_node2 = node2.cloneNode(true);
tmp_node2.normalize();
check_eq_nodes(tmp_node1, tmp_node2);
n2elt.appendChild(doc.createTextNode(""));
check_neq_nodes(node1, node2);
tmp_node1 = node1.cloneNode(true);
tmp_node1.normalize();
tmp_node2 = node2.cloneNode(true);
tmp_node2.normalize();
check_eq_nodes(tmp_node1, tmp_node2);
var typeText1 = doc.createTextNode("type");
n2elt.appendChild(typeText1);
tmp_node1 = node1.cloneNode(true);
tmp_node1.normalize();
tmp_node2 = node2.cloneNode(true);
tmp_node2.normalize();
check_neq_nodes(tmp_node1, tmp_node2);
n1elt.appendChild(doc.createTextNode("typedef"));
tmp_node1 = node1.cloneNode(true);
tmp_node1.normalize();
tmp_node2 = node2.cloneNode(true);
tmp_node2.normalize();
check_neq_nodes(tmp_node1, tmp_node2);
check_neq_nodes(n1elt, n2elt);
var typeText2 = doc.createTextNode("def");
n2elt.appendChild(typeText2);
tmp_node1 = node1.cloneNode(true);
tmp_node1.normalize();
tmp_node2 = node2.cloneNode(true);
tmp_node2.normalize();
check_eq_nodes(tmp_node1, tmp_node2);
check_neq_nodes(node1, node2);
n2elt.insertBefore(doc.createTextNode(""), typeText2);
check_neq_nodes(node1, node2);
n2elt.insertBefore(doc.createTextNode(""), typeText2);
check_neq_nodes(node1, node2);
n2elt.insertBefore(doc.createTextNode(""), typeText1);
check_neq_nodes(node1, node2);
node1.normalize();
node2.normalize();
check_eq_nodes(node1, node2);
}
function test_isEqualNode_whitespace()
{
equality_check_kids("test_pi1", true);
equality_check_kids("test_pi2", true);
equality_check_kids("test_pi3", false);
equality_check_kids("test_pi4", true);
equality_check_kids("test_pi5", true);
equality_check_kids("test_elt1", false);
equality_check_kids("test_elt2", false);
equality_check_kids("test_elt3", true);
equality_check_kids("test_elt4", false);
equality_check_kids("test_elt5", false);
equality_check_kids("test_comment1", true);
equality_check_kids("test_comment2", false);
equality_check_kids("test_comment3", false);
equality_check_kids("test_comment4", true);
equality_check_kids("test_text1", true);
equality_check_kids("test_text2", false);
equality_check_kids("test_text3", false);
equality_check_kids("test_cdata1", false);
equality_check_kids("test_cdata2", true);
equality_check_kids("test_cdata3", false);
equality_check_kids("test_cdata4", false);
equality_check_kids("test_cdata5", false);
}
function test_isEqualNode_namespaces()
{
equality_check_kids("test_ns1", false);
equality_check_kids("test_ns2", false);
// XXX want more tests here!
}
function test_isEqualNode_null()
{
check_neq_nodes(doc, null);
var elts = doc.getElementsByTagName("*");
for (var i = 0; i < elts.length; i++)
{
var elt = elts.item(i);
check_neq_nodes(elt, null);
var attrs = elt.attributes;
for (var j = 0; j < attrs.length; j++)
{
var att = attrs.item(j);
check_neq_nodes(att, null);
for (var k = 0; k < att.childNodes.length; k++)
{
check_neq_nodes(att.childNodes.item(k), null);
}
}
}
}
// UTILITY FUNCTIONS
function n(node) { return node ? node.QueryInterface(nsIDOMNode) : null; }
function n3(node) { return node ? node.QueryInterface(nsIDOM3Node) : null; }
function el(node) { return node ? node.QueryInterface(nsIDOMElement) : null; }
function at(node) { return node ? node.QueryInterface(nsIDOMAttr) : null; }
// TESTING FUNCTIONS
/**
* Compares the first and third (zero-indexed) child nodes of the element
* (typically to allow whitespace) referenced by parentId for isEqualNode
* equality or inequality based on the value of areEqual.
*
* Note that this means that the contents of the element referenced by parentId
* are whitespace-sensitive, and a stray space introduced during an edit to the
* file could result in a correct but unexpected (in)equality failure.
*/
function equality_check_kids(parentId, areEqual)
{
var parent = doc.getElementById(parentId);
var kid1 = parent.childNodes.item(1);
var kid2 = parent.childNodes.item(3);
if (areEqual)
check_eq_nodes(kid1, kid2);
else
check_neq_nodes(kid1, kid2);
}
function check_eq_nodes(n1, n2)
{
n1 = n3(n1);
n2 = n3(n2);
if (n1 && !n1.isEqualNode(n2))
do_throw(n1 + " should be equal to " + n2);
if (n2 && !n2.isEqualNode(n1))
do_throw(n2 + " should be equal to " + n1);
if (!n1 && !n2)
do_throw("nodes both null!");
}
function check_neq_nodes(n1, n2)
{
n1 = n3(n1);
n2 = n3(n2);
if (n1 && n1.isEqualNode(n2))
do_throw(n1 + " should not be equal to " + n2);
if (n2 && n2.isEqualNode(n1))
do_throw(n2 + " should not be equal to " + n1);
if (!n1 && !n2)
do_throw("n1 and n2 both null!");
}