Skip to content

Commit 7a1d633

Browse files
anonrigRafaelGSS
authored andcommitted
test: move more url tests to node:test
PR-URL: #54636 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Moshe Atlow <moshe@atlow.co.il> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
1 parent ee385d6 commit 7a1d633

9 files changed

+533
-528
lines changed
+11-15
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
'use strict';
22

3-
const common = require('../common');
4-
if (!common.hasIntl)
5-
common.skip('missing Intl');
3+
const { hasIntl } = require('../common');
64

7-
const strictEqual = require('assert').strictEqual;
8-
const url = require('url');
9-
10-
const domainToASCII = url.domainToASCII;
11-
const domainToUnicode = url.domainToUnicode;
5+
const { strictEqual } = require('node:assert');
6+
const { domainToASCII, domainToUnicode } = require('node:url');
7+
const { test } = require('node:test');
128

139
const domainWithASCII = [
1410
['ıíd', 'xn--d-iga7r'],
@@ -21,11 +17,11 @@ const domainWithASCII = [
2117
['भारत.org', 'xn--h2brj9c.org'],
2218
];
2319

24-
domainWithASCII.forEach((pair) => {
25-
const domain = pair[0];
26-
const ascii = pair[1];
27-
const domainConvertedToASCII = domainToASCII(domain);
28-
strictEqual(domainConvertedToASCII, ascii);
29-
const asciiConvertedToUnicode = domainToUnicode(ascii);
30-
strictEqual(asciiConvertedToUnicode, domain);
20+
test('domainToASCII and domainToUnicode', { skip: !hasIntl }, () => {
21+
for (const [domain, ascii] of domainWithASCII) {
22+
const domainConvertedToASCII = domainToASCII(domain);
23+
strictEqual(domainConvertedToASCII, ascii);
24+
const asciiConvertedToUnicode = domainToUnicode(ascii);
25+
strictEqual(asciiConvertedToUnicode, domain);
26+
}
3127
});

test/parallel/test-url-fileurltopath.js

+45-36
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
'use strict';
22
const { isWindows } = require('../common');
3-
const assert = require('assert');
4-
const url = require('url');
53

6-
function testInvalidArgs(...args) {
7-
for (const arg of args) {
4+
const { test } = require('node:test');
5+
const assert = require('node:assert');
6+
const url = require('node:url');
7+
8+
test('invalid arguments', () => {
9+
for (const arg of [null, undefined, 1, {}, true]) {
810
assert.throws(() => url.fileURLToPath(arg), {
911
code: 'ERR_INVALID_ARG_TYPE'
1012
});
1113
}
12-
}
13-
14-
// Input must be string or URL
15-
testInvalidArgs(null, undefined, 1, {}, true);
14+
});
1615

17-
// Input must be a file URL
18-
assert.throws(() => url.fileURLToPath('https://a/b/c'), {
19-
code: 'ERR_INVALID_URL_SCHEME'
16+
test('input must be a file URL', () => {
17+
assert.throws(() => url.fileURLToPath('https://a/b/c'), {
18+
code: 'ERR_INVALID_URL_SCHEME'
19+
});
2020
});
2121

22-
{
22+
test('fileURLToPath with host', () => {
2323
const withHost = new URL('file://host/a');
2424

2525
if (isWindows) {
@@ -29,9 +29,9 @@ assert.throws(() => url.fileURLToPath('https://a/b/c'), {
2929
code: 'ERR_INVALID_FILE_URL_HOST'
3030
});
3131
}
32-
}
32+
});
3333

34-
{
34+
test('fileURLToPath with invalid path', () => {
3535
if (isWindows) {
3636
assert.throws(() => url.fileURLToPath('file:///C:/a%2F/'), {
3737
code: 'ERR_INVALID_FILE_URL_PATH'
@@ -47,7 +47,7 @@ assert.throws(() => url.fileURLToPath('https://a/b/c'), {
4747
code: 'ERR_INVALID_FILE_URL_PATH'
4848
});
4949
}
50-
}
50+
});
5151

5252
const windowsTestCases = [
5353
// Lowercase ascii alpha
@@ -95,6 +95,7 @@ const windowsTestCases = [
9595
// UNC path (see https://docs.microsoft.com/en-us/archive/blogs/ie/file-uris-in-windows)
9696
{ path: '\\\\nas\\My Docs\\File.doc', fileURL: 'file://nas/My%20Docs/File.doc' },
9797
];
98+
9899
const posixTestCases = [
99100
// Lowercase ascii alpha
100101
{ path: '/foo', fileURL: 'file:///foo' },
@@ -140,29 +141,37 @@ const posixTestCases = [
140141
{ path: '/🚀', fileURL: 'file:///%F0%9F%9A%80' },
141142
];
142143

143-
for (const { path, fileURL } of windowsTestCases) {
144-
const fromString = url.fileURLToPath(fileURL, { windows: true });
145-
assert.strictEqual(fromString, path);
146-
const fromURL = url.fileURLToPath(new URL(fileURL), { windows: true });
147-
assert.strictEqual(fromURL, path);
148-
}
144+
test('fileURLToPath with windows path', { skip: !isWindows }, () => {
145+
146+
for (const { path, fileURL } of windowsTestCases) {
147+
const fromString = url.fileURLToPath(fileURL, { windows: true });
148+
assert.strictEqual(fromString, path);
149+
const fromURL = url.fileURLToPath(new URL(fileURL), { windows: true });
150+
assert.strictEqual(fromURL, path);
151+
}
152+
});
149153

150-
for (const { path, fileURL } of posixTestCases) {
151-
const fromString = url.fileURLToPath(fileURL, { windows: false });
152-
assert.strictEqual(fromString, path);
153-
const fromURL = url.fileURLToPath(new URL(fileURL), { windows: false });
154-
assert.strictEqual(fromURL, path);
155-
}
154+
test('fileURLToPath with posix path', { skip: isWindows }, () => {
155+
for (const { path, fileURL } of posixTestCases) {
156+
const fromString = url.fileURLToPath(fileURL, { windows: false });
157+
assert.strictEqual(fromString, path);
158+
const fromURL = url.fileURLToPath(new URL(fileURL), { windows: false });
159+
assert.strictEqual(fromURL, path);
160+
}
161+
});
156162

157163
const defaultTestCases = isWindows ? windowsTestCases : posixTestCases;
158164

159-
// Test when `options` is null
160-
const whenNullActual = url.fileURLToPath(new URL(defaultTestCases[0].fileURL), null);
161-
assert.strictEqual(whenNullActual, defaultTestCases[0].path);
165+
test('options is null', () => {
166+
const whenNullActual = url.fileURLToPath(new URL(defaultTestCases[0].fileURL), null);
167+
assert.strictEqual(whenNullActual, defaultTestCases[0].path);
168+
});
162169

163-
for (const { path, fileURL } of defaultTestCases) {
164-
const fromString = url.fileURLToPath(fileURL);
165-
assert.strictEqual(fromString, path);
166-
const fromURL = url.fileURLToPath(new URL(fileURL));
167-
assert.strictEqual(fromURL, path);
168-
}
170+
test('defaultTestCases', () => {
171+
for (const { path, fileURL } of defaultTestCases) {
172+
const fromString = url.fileURLToPath(fileURL);
173+
assert.strictEqual(fromString, path);
174+
const fromURL = url.fileURLToPath(new URL(fileURL));
175+
assert.strictEqual(fromURL, path);
176+
}
177+
});
+27-24
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,30 @@
11
'use strict';
2-
const common = require('../common');
3-
const assert = require('assert');
4-
const url = require('url');
52

6-
const throwsObjsAndReportTypes = [
7-
undefined,
8-
null,
9-
true,
10-
false,
11-
0,
12-
function() {},
13-
Symbol('foo'),
14-
];
3+
require('../common');
154

16-
for (const urlObject of throwsObjsAndReportTypes) {
17-
assert.throws(() => {
18-
url.format(urlObject);
19-
}, {
20-
code: 'ERR_INVALID_ARG_TYPE',
21-
name: 'TypeError',
22-
message: 'The "urlObject" argument must be one of type object or string.' +
23-
common.invalidArgTypeHelper(urlObject)
24-
});
25-
}
26-
assert.strictEqual(url.format(''), '');
27-
assert.strictEqual(url.format({}), '');
5+
const assert = require('node:assert');
6+
const url = require('node:url');
7+
const { test } = require('node:test');
8+
9+
test('format invalid input', () => {
10+
const throwsObjsAndReportTypes = [
11+
undefined,
12+
null,
13+
true,
14+
false,
15+
0,
16+
function() {},
17+
Symbol('foo'),
18+
];
19+
20+
for (const urlObject of throwsObjsAndReportTypes) {
21+
assert.throws(() => {
22+
url.format(urlObject);
23+
}, {
24+
code: 'ERR_INVALID_ARG_TYPE',
25+
name: 'TypeError',
26+
});
27+
}
28+
assert.strictEqual(url.format(''), '');
29+
assert.strictEqual(url.format({}), '');
30+
});

0 commit comments

Comments
 (0)