mirror of
https://github.com/nodejs/node.git
synced 2024-11-29 23:16:30 +01:00
7535a94c8a
Adds a new `../common/fixtures' module to begin normalizing `test/fixtures` use. Our test code is a bit inconsistent with regards to use of the fixtures directory. Some code uses `path.join()`, some code uses string concats, some other code uses template strings, etc. In mnay cases, significant duplication of code is seen when accessing fixture files, etc. This updates many (but by no means all) of the tests in the test suite to use the new consistent API. There are still many more to update, which would make an excelent Code-n-Learn exercise. PR-URL: https://github.com/nodejs/node/pull/14332 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Gibson Fahnestock <gibfahn@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de> Reviewed-By: Michaël Zasso <targos@protonmail.com>
110 lines
3.0 KiB
JavaScript
110 lines
3.0 KiB
JavaScript
'use strict';
|
|
|
|
const common = require('../common');
|
|
const assert = require('assert');
|
|
const { URL, URLSearchParams } = require('url');
|
|
const fixtures = require('../common/fixtures');
|
|
|
|
// Tests below are not from WPT.
|
|
const serialized = 'a=a&a=1&a=true&a=undefined&a=null&a=%EF%BF%BD' +
|
|
'&a=%EF%BF%BD&a=%F0%9F%98%80&a=%EF%BF%BD%EF%BF%BD' +
|
|
'&a=%5Bobject+Object%5D';
|
|
const values = ['a', 1, true, undefined, null, '\uD83D', '\uDE00',
|
|
'\uD83D\uDE00', '\uDE00\uD83D', {}];
|
|
const normalizedValues = ['a', '1', 'true', 'undefined', 'null', '\uFFFD',
|
|
'\uFFFD', '\uD83D\uDE00', '\uFFFD\uFFFD',
|
|
'[object Object]'];
|
|
|
|
const m = new URL('http://example.org');
|
|
const sp = m.searchParams;
|
|
|
|
assert(sp);
|
|
assert.strictEqual(sp.toString(), '');
|
|
assert.strictEqual(m.search, '');
|
|
|
|
assert(!sp.has('a'));
|
|
values.forEach((i) => sp.set('a', i));
|
|
assert(sp.has('a'));
|
|
assert.strictEqual(sp.get('a'), '[object Object]');
|
|
sp.delete('a');
|
|
assert(!sp.has('a'));
|
|
|
|
m.search = '';
|
|
assert.strictEqual(sp.toString(), '');
|
|
|
|
values.forEach((i) => sp.append('a', i));
|
|
assert(sp.has('a'));
|
|
assert.strictEqual(sp.getAll('a').length, values.length);
|
|
assert.strictEqual(sp.get('a'), 'a');
|
|
|
|
assert.strictEqual(sp.toString(), serialized);
|
|
|
|
assert.strictEqual(m.search, `?${serialized}`);
|
|
|
|
assert.strictEqual(sp[Symbol.iterator], sp.entries);
|
|
|
|
let key, val;
|
|
let n = 0;
|
|
for ([key, val] of sp) {
|
|
assert.strictEqual(key, 'a', n);
|
|
assert.strictEqual(val, normalizedValues[n], n);
|
|
n++;
|
|
}
|
|
n = 0;
|
|
for (key of sp.keys()) {
|
|
assert.strictEqual(key, 'a', n);
|
|
n++;
|
|
}
|
|
n = 0;
|
|
for (val of sp.values()) {
|
|
assert.strictEqual(val, normalizedValues[n], n);
|
|
n++;
|
|
}
|
|
n = 0;
|
|
sp.forEach(function(val, key, obj) {
|
|
assert.strictEqual(this, undefined, n);
|
|
assert.strictEqual(key, 'a', n);
|
|
assert.strictEqual(val, normalizedValues[n], n);
|
|
assert.strictEqual(obj, sp, n);
|
|
n++;
|
|
});
|
|
sp.forEach(function() {
|
|
assert.strictEqual(this, m);
|
|
}, m);
|
|
|
|
{
|
|
const callbackErr = common.expectsError({
|
|
code: 'ERR_INVALID_CALLBACK',
|
|
type: TypeError
|
|
}, 2);
|
|
assert.throws(() => sp.forEach(), callbackErr);
|
|
assert.throws(() => sp.forEach(1), callbackErr);
|
|
}
|
|
|
|
m.search = '?a=a&b=b';
|
|
assert.strictEqual(sp.toString(), 'a=a&b=b');
|
|
|
|
const tests = require(fixtures.path('url-searchparams.js'));
|
|
|
|
for (const [input, expected, parsed] of tests) {
|
|
if (input[0] !== '?') {
|
|
const sp = new URLSearchParams(input);
|
|
assert.strictEqual(String(sp), expected);
|
|
assert.deepStrictEqual(Array.from(sp), parsed);
|
|
|
|
m.search = input;
|
|
assert.strictEqual(String(m.searchParams), expected);
|
|
assert.deepStrictEqual(Array.from(m.searchParams), parsed);
|
|
}
|
|
|
|
{
|
|
const sp = new URLSearchParams(`?${input}`);
|
|
assert.strictEqual(String(sp), expected);
|
|
assert.deepStrictEqual(Array.from(sp), parsed);
|
|
|
|
m.search = `?${input}`;
|
|
assert.strictEqual(String(m.searchParams), expected);
|
|
assert.deepStrictEqual(Array.from(m.searchParams), parsed);
|
|
}
|
|
}
|