Skip to content

Commit

Permalink
url: added wpt tests using git node wpt url
Browse files Browse the repository at this point in the history
Added the wpt tests using git node wpt url

Fixes: nodejs#47883
  • Loading branch information
sankalp1999 committed May 5, 2023
1 parent 7cc66e4 commit 12b462b
Show file tree
Hide file tree
Showing 18 changed files with 793 additions and 598 deletions.
11 changes: 5 additions & 6 deletions lib/internal/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -446,19 +446,18 @@ class URLSearchParams {

const list = this.#searchParams;
name = toUSVString(name);

if (value !== undefined) {
value = toUSVString(value);
}

for (let i = 0; i < list.length;) {
if (value !== undefined) {
const key = list[i]
const val = list[i + 1]
const key = list[i];
const val = list[i + 1];
if (key === name && val === value) {
list.splice(i, 2);
}
else {
} else {
i += 2;
}
} else {
Expand Down Expand Up @@ -528,7 +527,7 @@ class URLSearchParams {
}

for (let i = 0; i < list.length; i += 2) {
if(value !== undefined && list[i] === name && list[i + 1] === value) {
if (value !== undefined && list[i] === name && list[i + 1] === value) {
return true;
}
if (value === undefined && list[i] === name) {
Expand Down
67 changes: 67 additions & 0 deletions output1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
TAP version 13
# Subtest: /Users/sshubham/Desktop/open_source/node/custom_test_dir/test1.mjs starting...
# Subtest: addition starting...
# Subtest: math
# Subtest: addition
ok 1 - addition
---
duration_ms: 0.309375
...
# Subtest: subtraction starting...
# Subtest: subtraction
not ok 2 - subtraction
---
duration_ms: 0.860166
failureType: 'testCodeFailure'
error: |-
Expected values to be strictly deep-equal:

5 !== 2

code: 'ERR_ASSERTION'
name: 'AssertionError'
expected: 2
actual: 5
operator: 'deepStrictEqual'
stack: |-
TestContext.<anonymous> (file:///Users/sshubham/Desktop/open_source/node/custom_test_dir/test1.mjs:9:7)
Test.runInAsyncScope (node:async_hooks:206:9)
Test.run (node:internal/test_runner/test:571:25)
Suite.processPendingSubtests (node:internal/test_runner/test:315:27)
Test.postRun (node:internal/test_runner/test:640:19)
Test.run (node:internal/test_runner/test:599:10)
async Promise.all (index 0)
async Suite.run (node:internal/test_runner/test:804:7)
async startSubtest (node:internal/test_runner/harness:203:3)
...
1..2
not ok 1 - math
---
duration_ms: 2.00525
type: 'suite'
failureType: 'subtestsFailed'
error: '1 subtest failed'
code: 'ERR_TEST_FAILURE'
...
# Subtest: photosynthesis starting...
# Subtest: science
# Subtest: photosynthesis
ok 1 - photosynthesis
---
duration_ms: 0.055125
...
1..1
ok 2 - science
---
duration_ms: 0.106167
type: 'suite'
...
1..2
# tests 3
# suites 2
# pass 2
# fail 1
# cancelled 0
# skipped 0
# todo 0
# duration_ms 45.467584
39 changes: 39 additions & 0 deletions output2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
TAP version 13
# Subtest: math
# Subtest: addition
not ok 1 - addition
---
duration_ms: 0.387
failureType: 'testCodeFailure'
error: 'strictEqual is not defined'
code: 'ERR_TEST_FAILURE'
stack: |-
TestContext.<anonymous> (file:///Users/sshubham/Desktop/open_source/node/custom_test_dir/test1.mjs:6:7)
Test.runInAsyncScope (node:async_hooks:206:9)
Test.run (node:internal/test_runner/test:570:25)
Test.start (node:internal/test_runner/test:483:17)
node:internal/test_runner/test:801:71
node:internal/per_context/primordials:482:82
new Promise (<anonymous>)
new SafePromise (node:internal/per_context/primordials:450:29)
node:internal/per_context/primordials:482:9
Array.map (<anonymous>)
...
1..1
not ok 1 - math
---
duration_ms: 1.2205
type: 'suite'
failureType: 'subtestsFailed'
error: '1 subtest failed'
code: 'ERR_TEST_FAILURE'
...
1..1
# tests 1
# suites 1
# pass 0
# fail 1
# cancelled 0
# skipped 0
# todo 0
# duration_ms 43.611417
2 changes: 1 addition & 1 deletion test/fixtures/wpt/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Last update:
- resource-timing: https://github.com/web-platform-tests/wpt/tree/22d38586d0/resource-timing
- resources: https://github.com/web-platform-tests/wpt/tree/919874f84f/resources
- streams: https://github.com/web-platform-tests/wpt/tree/51750bc8d7/streams
- url: https://github.com/web-platform-tests/wpt/tree/7c5c3cc125/url
- url: https://github.com/web-platform-tests/wpt/tree/c4726447f3/url
- user-timing: https://github.com/web-platform-tests/wpt/tree/df24fb604e/user-timing
- wasm/jsapi: https://github.com/web-platform-tests/wpt/tree/cde25e7e3c/wasm/jsapi
- wasm/webapi: https://github.com/web-platform-tests/wpt/tree/fd1b23eeaa/wasm/webapi
Expand Down
50 changes: 24 additions & 26 deletions test/fixtures/wpt/url/README.md
Original file line number Diff line number Diff line change
@@ -1,31 +1,29 @@
## urltestdata.json

These tests are for browsers, but the data for
`a-element.html`, `url-constructor.html`, `a-element-xhtml.xhtml`, and `failure.html`
is in `resources/urltestdata.json` and can be re-used by non-browser implementations.
This file contains a JSON array of comments as strings and test cases as objects.
The keys for each test case are:

* `base`: an absolute URL as a string whose [parsing] without a base of its own must succeed.
This key is always present,
and may have a value like `"about:blank"` when `input` is an absolute URL.
* `input`: an URL as a string to be [parsed][parsing] with `base` as its base URL.
* Either:
* `failure` with the value `true`, indicating that parsing `input` should return failure,
* or `href`, `origin`, `protocol`, `username`, `password`, `host`, `hostname`, `port`,
`pathname`, `search`, and `hash` with string values;
indicating that parsing `input` should return an URL record
and that the getters of each corresponding attribute in that URL’s [API]
should return the corresponding value.

The `origin` key may be missing.
In that case, the API’s `origin` attribute is not tested.

In addition to testing that parsing `input` against `base` gives the result, a test harness for the
`URL` constructor (or similar APIs) should additionally test the following pattern: if `failure` is
true, parsing `about:blank` against `input` must give failure. This tests that the logic for
converting base URLs into strings properly fails the whole parsing algorithm if the base URL cannot
be parsed.
`resources/urltestdata.json` contains URL parsing tests suitable for any URL parser implementation.

It's used as a source of tests by `a-element.html`, `failure.html`, `url-constructor.any.js`, and
other test files in this directory.

The format of `resources/urltestdata.json` is a JSON array of comments as strings and test cases as
objects. The keys for each test case are:

* `input`: a string to be parsed as URL.
* `base`: null or a serialized URL (i.e., does not fail parsing).
* Then either

* `failure` whose value is `true`, indicating that parsing `input` relative to `base` returns
failure
* `relativeTo` whose value is "`non-opaque-path-base`" (input does parse against a non-null base
URL without an opaque path) or "`any-base`" (input parses against any non-null base URL), or is
omitted in its entirety (input never parses successfully)

or `href`, `origin`, `protocol`, `username`, `password`, `host`, `hostname`, `port`,
`pathname`, `search`, and `hash` with string values; indicating that parsing `input` should return
an URL record and that the getters of each corresponding attribute in that URL’s [API] should
return the corresponding value.

The `origin` key may be missing. In that case, the API’s `origin` attribute is not tested.

## setters_tests.json

Expand Down
11 changes: 6 additions & 5 deletions test/fixtures/wpt/url/failure.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@
promise_test(() => fetch("resources/urltestdata.json").then(res => res.json()).then(runTests), "Loading data…")

function runTests(testData) {
for(const test of testData) {
if (typeof test === "string" || !test.failure || test.base !== "about:blank") {
continue
for (const test of testData) {
if (typeof test === "string" || !test.failure || test.base !== null) {
continue;
}

const name = test.input + " should throw"

self.test(() => { // URL's constructor's first argument is tested by url-constructor.html
self.test(() => {
// URL's constructor's first argument is tested by url-constructor.html
// If a URL fails to parse with any valid base, it must also fail to parse with no base, i.e.
// when used as a base URL itself.
assert_throws_js(TypeError, () => new URL("about:blank", test.input));
Expand All @@ -30,7 +31,7 @@
// The following use cases resolve the URL input relative to the current
// document's URL. If this test input could be construed as a valid URL
// when resolved against a base URL, skip these cases.
if (!test.inputCanBeRelative) {
if (test.relativeTo === undefined) {
self.test(() => {
const client = new XMLHttpRequest()
assert_throws_dom("SyntaxError", () => client.open("GET", test.input))
Expand Down
7 changes: 7 additions & 0 deletions test/fixtures/wpt/url/historical.any.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,11 @@ test(() => {
assert_throws_dom("DataCloneError", () => self.structuredClone(new URLSearchParams()));
}, "URLSearchParams: no structured serialize/deserialize support");

test(() => {
const url = new URL("about:blank");
url.toString = () => { throw 1 };
assert_throws_exactly(1, () => new URL(url), "url argument");
assert_throws_exactly(1, () => new URL("about:blank", url), "base argument");
}, "Constructor only takes strings");

done();
31 changes: 18 additions & 13 deletions test/fixtures/wpt/url/resources/a-element-origin.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,28 @@ function setBase(base) {
}

function bURL(url, base) {
base = base || "about:blank"
setBase(base)
var a = document.createElement("a")
a.setAttribute("href", url)
return a
setBase(base);
const a = document.createElement("a");
a.setAttribute("href", url);
return a;
}

function runURLTests(urltests) {
for(var i = 0, l = urltests.length; i < l; i++) {
var expected = urltests[i]
if (typeof expected === "string" || !("origin" in expected)) continue
// skip without base because you cannot unset the baseURL of a document
if (expected.base === null) continue;
function runURLTests(urlTests) {
for (const expected of urlTests) {
// Skip comments and tests without "origin" expectation
if (typeof expected === "string" || !("origin" in expected))
continue;

// Fragments are relative against "about:blank" (this might always be redundant due to requiring "origin" in expected)
if (expected.base === null && expected.input.startsWith("#"))
continue;

// We cannot use a null base for HTML tests
const base = expected.base === null ? "about:blank" : expected.base;

test(function() {
var url = bURL(expected.input, expected.base)
var url = bURL(expected.input, base)
assert_equals(url.origin, expected.origin, "origin")
}, "Parsing origin: <" + expected.input + "> against <" + expected.base + ">")
}, "Parsing origin: <" + expected.input + "> against <" + base + ">")
}
}
33 changes: 19 additions & 14 deletions test/fixtures/wpt/url/resources/a-element.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
promise_test(() => fetch("resources/urltestdata.json").then(res => res.json()).then(runURLTests), "Loading data…");

function setBase(base) {
document.getElementById("base").href = base
document.getElementById("base").href = base;
}

function bURL(url, base) {
base = base || "about:blank"
setBase(base)
var a = document.createElement("a")
a.setAttribute("href", url)
return a
setBase(base);
const a = document.createElement("a");
a.setAttribute("href", url);
return a;
}

function runURLTests(urltests) {
for(var i = 0, l = urltests.length; i < l; i++) {
var expected = urltests[i]
if (typeof expected === "string") continue // skip comments
// skip without base because you cannot unset the baseURL of a document
if (expected.base === null) continue;
function runURLTests(urlTests) {
for (const expected of urlTests) {
// Skip comments
if (typeof expected === "string")
continue;

// Fragments are relative against "about:blank"
if (expected.relativeTo === "any-base")
continue;

// We cannot use a null base for HTML tests
const base = expected.base === null ? "about:blank" : expected.base;

function getKey(expected) {
if (expected.protocol) {
Expand All @@ -30,7 +35,7 @@ function runURLTests(urltests) {
}

subsetTestByKey(getKey(expected), test, function() {
var url = bURL(expected.input, expected.base)
var url = bURL(expected.input, base)
if(expected.failure) {
if(url.protocol !== ':') {
assert_unreached("Expected URL to fail parsing")
Expand All @@ -49,6 +54,6 @@ function runURLTests(urltests) {
assert_equals(url.pathname, expected.pathname, "pathname")
assert_equals(url.search, expected.search, "search")
assert_equals(url.hash, expected.hash, "hash")
}, "Parsing: <" + expected.input + "> against <" + expected.base + ">")
}, "Parsing: <" + expected.input + "> against <" + base + ">")
}
}

0 comments on commit 12b462b

Please sign in to comment.