dom/url/tests/test_urlSearchParams_sorting.html
author danielleleb <danielleleb12@gmail.com>
Tue, 12 Mar 2019 17:38:47 +0000
changeset 521608 f87b1c69a88d995473d2351fa55247774be9c395
parent 378165 86490dddb115b446423e74dc4bb22a5fd58fb1f2
child 528458 ba6f655fd68963530c866d0d4a48c3db3d307777
permissions -rw-r--r--
Bug 1532934 - Enable ESLint for dom/url (automatic changes). r=Standard8,baku Differential Revision: https://phabricator.services.mozilla.com/D22736


<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  <title>Test for URLSearchParams.sort()</title>
  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<script type="application/javascript">

function compareArray(a, b) {
  is(a.length, b.length, "Length matches");
  for (let i = 0; i < a.length; ++i) {
    is(a[i], b[i], "Values " + i + " match");
  }
}

[
  {
    "input": "z=b&a=b&z=a&a=a",
    "output": [["a", "b"], ["a", "a"], ["z", "b"], ["z", "a"]],
  },
  {
    "input": "\uFFFD=x&\uFFFC&\uFFFD=a",
    "output": [["\uFFFC", ""], ["\uFFFD", "x"], ["\uFFFD", "a"]],
  },
  {
    "input": "ffi&🌈", // 🌈 > code point, but < code unit because two code units
    "output": [["🌈", ""], ["ffi", ""]],
  },
  {
    "input": "é&e\uFFFD&e\u0301",
    "output": [["e\u0301", ""], ["e\uFFFD", ""], ["é", ""]],
  },
  {
    "input": "z=z&a=a&z=y&a=b&z=x&a=c&z=w&a=d&z=v&a=e&z=u&a=f&z=t&a=g",
    "output": [["a", "a"], ["a", "b"], ["a", "c"], ["a", "d"], ["a", "e"], ["a", "f"], ["a", "g"], ["z", "z"], ["z", "y"], ["z", "x"], ["z", "w"], ["z", "v"], ["z", "u"], ["z", "t"]],
  },
].forEach((val) => {
  info("Run test: " + val.toSource() + "\n");

  let params = new URLSearchParams(val.input);
  params.sort();

  let i = 0;
  for (let param of params) {
    compareArray(param, val.output[i++]);
  }

  let url = new URL("?" + val.input, "https://example/");
  url.searchParams.sort();
  params = new URLSearchParams(url.search);
  i = 0;
  for (let param of params) {
    compareArray(param, val.output[i++]);
  }
});

</script>
</body>
</html>