content/test/unit/test_isequalnode.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/content/test/unit/test_isequalnode.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,435 @@
     1.4 +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +function run_test()
    1.10 +{
    1.11 +  /*
    1.12 +   * NOTE: [i] is not allowed in this test, since it's done via classinfo and
    1.13 +   * we don't have that in xpcshell; the workaround is item(i).  Suck.
    1.14 +   */
    1.15 +  init();
    1.16 +
    1.17 +  test_isEqualNode_setAttribute();
    1.18 +  test_isEqualNode_clones();
    1.19 +  test_isEqualNode_variety();
    1.20 +  test_isEqualNode_normalization();
    1.21 +  test_isEqualNode_whitespace();
    1.22 +  test_isEqualNode_namespaces();
    1.23 +  test_isEqualNode_wholeDoc();
    1.24 +
    1.25 +  // XXX should Node.isEqualNode(null) throw or return false?
    1.26 +  //test_isEqualNode_null();
    1.27 +
    1.28 +}
    1.29 +
    1.30 +// TEST CODE
    1.31 +
    1.32 +var doc, root; // cache for use in all tests
    1.33 +
    1.34 +function init()
    1.35 +{
    1.36 +  doc = ParseFile("isequalnode_data.xml");
    1.37 +  root = doc.documentElement;
    1.38 +}
    1.39 +
    1.40 +function test_isEqualNode_setAttribute()
    1.41 +{
    1.42 +  // NOTE: 0, 2 are whitespace
    1.43 +  var test1 = doc.getElementById("test_setAttribute");
    1.44 +  var node1 = test1.childNodes.item(1);
    1.45 +  var node2 = test1.childNodes.item(3);
    1.46 +
    1.47 +  check_eq_nodes(node1, node2);
    1.48 +
    1.49 +
    1.50 +  el(node1).setAttribute("bar", "baz");
    1.51 +  check_neq_nodes(node1, node2);
    1.52 +
    1.53 +  el(node2).setAttribute("bar", "baz");
    1.54 +  check_eq_nodes(node1, node2);
    1.55 +
    1.56 +
    1.57 +  // the null namespace is equivalent to no namespace -- section 1.3.3
    1.58 +  // (XML Namespaces) of DOM 3 Core
    1.59 +  node1.setAttributeNS(null, "quux", "17");
    1.60 +  check_neq_nodes(node1, node2);
    1.61 +
    1.62 +  node2.setAttribute("quux", "17");
    1.63 +  check_eq_nodes(node1, node2);
    1.64 +
    1.65 +
    1.66 +  node2.setAttributeNS("http://mozilla.org/", "seamonkey", "rheet");
    1.67 +  check_neq_nodes(node1, node2);
    1.68 +
    1.69 +  node1.setAttribute("seamonkey", "rheet");
    1.70 +  check_neq_nodes(node1, node2);
    1.71 +
    1.72 +  node1.setAttributeNS("http://mozilla.org/", "seamonkey", "rheet");
    1.73 +  check_neq_nodes(node1, node2);
    1.74 +
    1.75 +  // this overwrites the namespaced "seamonkey" attribute added to node2
    1.76 +  // earlier, because this simply sets whatever attribute has the fully
    1.77 +  // qualified name "seamonkey" (the setAttributeNS attribute string wasn't
    1.78 +  // prefixed) -- consequently, node1 and node2 are still unequal
    1.79 +  node2.setAttribute("seamonkey", "rheet");
    1.80 +  check_neq_nodes(node1, node2);
    1.81 +}
    1.82 +
    1.83 +function test_isEqualNode_clones()
    1.84 +{
    1.85 +  // tests all elements and attributes in the document
    1.86 +  var all_elts = doc.getElementsByTagName("*");
    1.87 +  for (var i = 0; i < all_elts.length; i++)
    1.88 +  {
    1.89 +    var elt = el(all_elts.item(i));
    1.90 +    check_eq_nodes(elt, elt.cloneNode(true));
    1.91 +
    1.92 +    var attrs = elt.attributes;
    1.93 +    for (var j = 0; j < attrs.length; j++)
    1.94 +    {
    1.95 +      var attr = attrs.item(j);
    1.96 +      check_eq_nodes(attr, attr.cloneNode(true));
    1.97 +    }
    1.98 +  }
    1.99 +
   1.100 +  var elm = doc.createElement("foo");
   1.101 +  check_eq_nodes(elm, elm.cloneNode(true));
   1.102 +  check_eq_nodes(elm, elm.cloneNode(false));
   1.103 +
   1.104 +  elm.setAttribute("fiz", "eit");
   1.105 +  check_eq_nodes(elm, elm.cloneNode(true));
   1.106 +  check_eq_nodes(elm, elm.cloneNode(false));
   1.107 +
   1.108 +  elm.setAttributeNS("http://example.com/", "trendoid", "arthroscope");
   1.109 +  check_eq_nodes(elm, elm.cloneNode(true));
   1.110 +  check_eq_nodes(elm, elm.cloneNode(false));
   1.111 +
   1.112 +  var elm2 = elm.cloneNode(true);
   1.113 +  check_eq_nodes(elm, elm2);
   1.114 +
   1.115 +  const TEXT = "fetishist";
   1.116 +
   1.117 +  elm.textContent = TEXT;
   1.118 +  check_neq_nodes(elm, elm2);
   1.119 +
   1.120 +  check_neq_nodes(elm, elm.cloneNode(false));
   1.121 +  check_eq_nodes(elm, elm.cloneNode(true));
   1.122 +
   1.123 +  elm2.appendChild(doc.createTextNode(TEXT));
   1.124 +  check_eq_nodes(elm, elm2);
   1.125 +
   1.126 +  var att = doc.createAttribute("bar");
   1.127 +  check_eq_nodes(att, att.cloneNode(true));
   1.128 +  check_eq_nodes(att, att.cloneNode(false));
   1.129 +}
   1.130 +
   1.131 +function test_isEqualNode_variety()
   1.132 +{
   1.133 +  const nodes =
   1.134 +    [
   1.135 +      doc.createElement("foo"),
   1.136 +      doc.createElementNS("http://example.com/", "foo"),
   1.137 +      doc.createElementNS("http://example.org/", "foo"),
   1.138 +      doc.createElementNS("http://example.com/", "FOO"),
   1.139 +      doc.createAttribute("foo", "href='biz'"),
   1.140 +      doc.createAttributeNS("http://example.com/", "foo", "href='biz'"),
   1.141 +      doc.createTextNode("foo"),
   1.142 +      doc.createTextNode("   "),
   1.143 +      doc.createTextNode("    "),
   1.144 +      doc.createComment("foo"),
   1.145 +      doc.createProcessingInstruction("foo", "href='biz'"),
   1.146 +      doc.implementation.createDocumentType("foo", "href='biz'", ""),
   1.147 +      doc.implementation.createDocument("http://example.com/", "foo", null),
   1.148 +      doc.createDocumentFragment()
   1.149 +    ];
   1.150 +
   1.151 +  for (var i = 0; i < nodes.length; i++)
   1.152 +  {
   1.153 +    for (var j = i; j < nodes.length; j++)
   1.154 +    {
   1.155 +      if (i == j)
   1.156 +        check_eq_nodes(nodes[i], nodes[j]);
   1.157 +      else
   1.158 +        check_neq_nodes(nodes[i], nodes[j]);
   1.159 +    }
   1.160 +  }
   1.161 +}
   1.162 +
   1.163 +function test_isEqualNode_normalization()
   1.164 +{
   1.165 +  var norm = doc.getElementById("test_normalization");
   1.166 +  var node1 = norm.childNodes.item(1);
   1.167 +  var node2 = norm.childNodes.item(3);
   1.168 +
   1.169 +  check_eq_nodes(node1, node2);
   1.170 +
   1.171 +  node1.appendChild(doc.createTextNode(""));
   1.172 +  check_neq_nodes(node1, node2);
   1.173 +
   1.174 +  node1.normalize();
   1.175 +  check_eq_nodes(node1, node2);
   1.176 +
   1.177 +  node2.appendChild(doc.createTextNode("fun"));
   1.178 +  node2.appendChild(doc.createTextNode("ctor"));
   1.179 +  node1.appendChild(doc.createTextNode("functor"));
   1.180 +  check_neq_nodes(node1, node2);
   1.181 +
   1.182 +  node1.normalize();
   1.183 +  check_neq_nodes(node1, node2);
   1.184 +
   1.185 +  node2.normalize();
   1.186 +  check_eq_nodes(node1, node2);
   1.187 +
   1.188 +  // reset
   1.189 +  while (node1.hasChildNodes())
   1.190 +    node1.removeChild(node1.childNodes.item(0));
   1.191 +  while (node2.hasChildNodes())
   1.192 +    node2.removeChild(node2.childNodes.item(0));
   1.193 +
   1.194 +  // attribute normalization testing
   1.195 +
   1.196 +  var at1 = doc.createAttribute("foo");
   1.197 +  var at2 = doc.createAttribute("foo");
   1.198 +  check_eq_nodes(at1, at2);
   1.199 +
   1.200 +  // Attr.appendChild isn't implemented yet (bug 56758), so don't run this yet
   1.201 +  if (false)
   1.202 +  {
   1.203 +    at1.appendChild(doc.createTextNode("rasp"));
   1.204 +    at2.appendChild(doc.createTextNode("rasp"));
   1.205 +    check_eq_nodes(at1, at2);
   1.206 +
   1.207 +    at1.appendChild(doc.createTextNode(""));
   1.208 +    check_neq_nodes(at1, at2);
   1.209 +
   1.210 +    at1.normalize();
   1.211 +    check_eq_nodes(at1, at2);
   1.212 +
   1.213 +    at1.appendChild(doc.createTextNode("berry"));
   1.214 +    check_neq_nodes(at1, at2);
   1.215 +
   1.216 +    at2.appendChild(doc.createTextNode("ber"));
   1.217 +    check_neq_nodes(at1, at2);
   1.218 +
   1.219 +    at2.appendChild(doc.createTextNode("ry"));
   1.220 +    check_neq_nodes(at1, at2);
   1.221 +
   1.222 +    at1.normalize();
   1.223 +    check_neq_nodes(at1, at2);
   1.224 +
   1.225 +    at2.normalize();
   1.226 +    check_eq_nodes(at1, at2);
   1.227 +  }
   1.228 +
   1.229 +  node1.setAttributeNode(at1);
   1.230 +  check_neq_nodes(node1, node2);
   1.231 +
   1.232 +  node2.setAttributeNode(at2);
   1.233 +  check_eq_nodes(node1, node2);
   1.234 +
   1.235 +  var n1text1 = doc.createTextNode("ratfink");
   1.236 +  var n1elt = doc.createElement("fruitcake");
   1.237 +  var n1text2 = doc.createTextNode("hydrospanner");
   1.238 +
   1.239 +  node1.appendChild(n1text1);
   1.240 +  node1.appendChild(n1elt);
   1.241 +  node1.appendChild(n1text2);
   1.242 +
   1.243 +  check_neq_nodes(node1, node2);
   1.244 +
   1.245 +  var n2text1a = doc.createTextNode("rat");
   1.246 +  var n2text1b = doc.createTextNode("fink");
   1.247 +  var n2elt = doc.createElement("fruitcake");
   1.248 +  var n2text2 = doc.createTextNode("hydrospanner");
   1.249 +
   1.250 +  node2.appendChild(n2text1b);
   1.251 +  node2.appendChild(n2elt);
   1.252 +  node2.appendChild(n2text2);
   1.253 +  check_neq_nodes(node1, node2);
   1.254 +
   1.255 +  node2.insertBefore(n2text1a, n2text1b);
   1.256 +  check_neq_nodes(node1, node2);
   1.257 +
   1.258 +  var tmp_node1 = node1.cloneNode(true);
   1.259 +  tmp_node1.normalize();
   1.260 +  var tmp_node2 = node2.cloneNode(true);
   1.261 +  tmp_node2.normalize();
   1.262 +  check_eq_nodes(tmp_node1, tmp_node2);
   1.263 +
   1.264 +  n2elt.appendChild(doc.createTextNode(""));
   1.265 +  check_neq_nodes(node1, node2);
   1.266 +
   1.267 +  tmp_node1 = node1.cloneNode(true);
   1.268 +  tmp_node1.normalize();
   1.269 +  tmp_node2 = node2.cloneNode(true);
   1.270 +  tmp_node2.normalize();
   1.271 +  check_eq_nodes(tmp_node1, tmp_node2);
   1.272 +
   1.273 +  var typeText1 = doc.createTextNode("type");
   1.274 +  n2elt.appendChild(typeText1);
   1.275 +  tmp_node1 = node1.cloneNode(true);
   1.276 +  tmp_node1.normalize();
   1.277 +  tmp_node2 = node2.cloneNode(true);
   1.278 +  tmp_node2.normalize();
   1.279 +  check_neq_nodes(tmp_node1, tmp_node2);
   1.280 +
   1.281 +  n1elt.appendChild(doc.createTextNode("typedef"));
   1.282 +  tmp_node1 = node1.cloneNode(true);
   1.283 +  tmp_node1.normalize();
   1.284 +  tmp_node2 = node2.cloneNode(true);
   1.285 +  tmp_node2.normalize();
   1.286 +  check_neq_nodes(tmp_node1, tmp_node2);
   1.287 +  check_neq_nodes(n1elt, n2elt);
   1.288 +
   1.289 +  var typeText2 = doc.createTextNode("def");
   1.290 +  n2elt.appendChild(typeText2);
   1.291 +  tmp_node1 = node1.cloneNode(true);
   1.292 +  tmp_node1.normalize();
   1.293 +  tmp_node2 = node2.cloneNode(true);
   1.294 +  tmp_node2.normalize();
   1.295 +  check_eq_nodes(tmp_node1, tmp_node2);
   1.296 +  check_neq_nodes(node1, node2);
   1.297 +
   1.298 +  n2elt.insertBefore(doc.createTextNode(""), typeText2);
   1.299 +  check_neq_nodes(node1, node2);
   1.300 +
   1.301 +  n2elt.insertBefore(doc.createTextNode(""), typeText2);
   1.302 +  check_neq_nodes(node1, node2);
   1.303 +
   1.304 +  n2elt.insertBefore(doc.createTextNode(""), typeText1);
   1.305 +  check_neq_nodes(node1, node2);
   1.306 +
   1.307 +  node1.normalize();
   1.308 +  node2.normalize();
   1.309 +  check_eq_nodes(node1, node2);
   1.310 +}
   1.311 +
   1.312 +function test_isEqualNode_whitespace()
   1.313 +{
   1.314 +  equality_check_kids("test_pi1", true);
   1.315 +  equality_check_kids("test_pi2", true);
   1.316 +  equality_check_kids("test_pi3", false);
   1.317 +  equality_check_kids("test_pi4", true);
   1.318 +  equality_check_kids("test_pi5", true);
   1.319 +
   1.320 +  equality_check_kids("test_elt1", false);
   1.321 +  equality_check_kids("test_elt2", false);
   1.322 +  equality_check_kids("test_elt3", true);
   1.323 +  equality_check_kids("test_elt4", false);
   1.324 +  equality_check_kids("test_elt5", false);
   1.325 +
   1.326 +  equality_check_kids("test_comment1", true);
   1.327 +  equality_check_kids("test_comment2", false);
   1.328 +  equality_check_kids("test_comment3", false);
   1.329 +  equality_check_kids("test_comment4", true);
   1.330 +
   1.331 +  equality_check_kids("test_text1", true);
   1.332 +  equality_check_kids("test_text2", false);
   1.333 +  equality_check_kids("test_text3", false);
   1.334 +
   1.335 +  equality_check_kids("test_cdata1", false);
   1.336 +  equality_check_kids("test_cdata2", true);
   1.337 +  equality_check_kids("test_cdata3", false);
   1.338 +  equality_check_kids("test_cdata4", false);
   1.339 +  equality_check_kids("test_cdata5", false);
   1.340 +}
   1.341 +
   1.342 +function test_isEqualNode_namespaces()
   1.343 +{
   1.344 +  equality_check_kids("test_ns1", false);
   1.345 +  equality_check_kids("test_ns2", false);
   1.346 +
   1.347 +  // XXX want more tests here!
   1.348 +}
   1.349 +
   1.350 +function test_isEqualNode_null()
   1.351 +{
   1.352 +  check_neq_nodes(doc, null);
   1.353 +
   1.354 +  var elts = doc.getElementsByTagName("*");
   1.355 +  for (var i = 0; i < elts.length; i++)
   1.356 +  {
   1.357 +    var elt = elts.item(i);
   1.358 +    check_neq_nodes(elt, null);
   1.359 +
   1.360 +    var attrs = elt.attributes;
   1.361 +    for (var j = 0; j < attrs.length; j++)
   1.362 +    {
   1.363 +      var att = attrs.item(j);
   1.364 +      check_neq_nodes(att, null);
   1.365 +
   1.366 +      for (var k = 0; k < att.childNodes.length; k++)
   1.367 +      {
   1.368 +        check_neq_nodes(att.childNodes.item(k), null);
   1.369 +      }
   1.370 +    }
   1.371 +  }
   1.372 +}
   1.373 +
   1.374 +function test_isEqualNode_wholeDoc()
   1.375 +{
   1.376 +  doc = ParseFile("isequalnode_data.xml");
   1.377 +  var doc2 = ParseFile("isequalnode_data.xml");
   1.378 +  var tw1 =
   1.379 +    doc.createTreeWalker(doc, Components.interfaces.nsIDOMNodeFilter.SHOW_ALL,
   1.380 +                         null);
   1.381 +  var tw2 =
   1.382 +    doc2.createTreeWalker(doc2, Components.interfaces.nsIDOMNodeFilter.SHOW_ALL,
   1.383 +                          null);
   1.384 +  do {
   1.385 +    check_eq_nodes(tw1.currentNode, tw2.currentNode);
   1.386 +    tw1.nextNode();
   1.387 +  } while(tw2.nextNode());
   1.388 +}
   1.389 +
   1.390 +// UTILITY FUNCTIONS
   1.391 +
   1.392 +function n(node)  { return node ? node.QueryInterface(nsIDOMNode) : null; }
   1.393 +function el(node) { return node ? node.QueryInterface(nsIDOMElement) : null; }
   1.394 +function at(node) { return node ? node.QueryInterface(nsIDOMAttr) : null; }
   1.395 +
   1.396 +
   1.397 +// TESTING FUNCTIONS
   1.398 +
   1.399 +/**
   1.400 + * Compares the first and third (zero-indexed) child nodes of the element
   1.401 + * (typically to allow whitespace) referenced by parentId for isEqualNode
   1.402 + * equality or inequality based on the value of areEqual.
   1.403 + *
   1.404 + * Note that this means that the contents of the element referenced by parentId
   1.405 + * are whitespace-sensitive, and a stray space introduced during an edit to the
   1.406 + * file could result in a correct but unexpected (in)equality failure.
   1.407 + */
   1.408 +function equality_check_kids(parentId, areEqual)
   1.409 +{
   1.410 +  var parent = doc.getElementById(parentId);
   1.411 +  var kid1 = parent.childNodes.item(1);
   1.412 +  var kid2 = parent.childNodes.item(3);
   1.413 +
   1.414 +  if (areEqual)
   1.415 +    check_eq_nodes(kid1, kid2);
   1.416 +  else
   1.417 +    check_neq_nodes(kid1, kid2);
   1.418 +}
   1.419 +
   1.420 +function check_eq_nodes(n1, n2)
   1.421 +{
   1.422 +  if (n1 && !n1.isEqualNode(n2))
   1.423 +    do_throw(n1 + " should be equal to " + n2);
   1.424 +  if (n2 && !n2.isEqualNode(n1))
   1.425 +    do_throw(n2 + " should be equal to " + n1);
   1.426 +  if (!n1 && !n2)
   1.427 +    do_throw("nodes both null!");
   1.428 +}
   1.429 +
   1.430 +function check_neq_nodes(n1, n2)
   1.431 +{
   1.432 +  if (n1 && n1.isEqualNode(n2))
   1.433 +    do_throw(n1 + " should not be equal to " + n2);
   1.434 +  if (n2 && n2.isEqualNode(n1))
   1.435 +    do_throw(n2 + " should not be equal to " + n1);
   1.436 +  if (!n1 && !n2)
   1.437 +    do_throw("n1 and n2 both null!");
   1.438 +}

mercurial