Skip to content

Commit 2da71f2

Browse files
Jay Brownleeevanlucas
Jay Brownlee
authored andcommitted
url, test: fix typo in inspect output, add test
In the string returned from URL.inspect there was an extra semicolon at the end when showHidden === true. The semicolon has been removed and a test for the inspect function has been added. The test parses the returned string, validates all of the contained keys/values and tests logic related to the showHidden option. PR-URL: #10231 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: Evan Lucas <[email protected]>
1 parent a97f264 commit 2da71f2

File tree

2 files changed

+69
-1
lines changed

2 files changed

+69
-1
lines changed

lib/internal/url.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ class URL {
440440
ret += ` hash: ${this.hash}\n`;
441441
if (opts.showHidden) {
442442
ret += ` cannot-be-base: ${this[cannotBeBase]}\n`;
443-
ret += ` special: ${this[special]}\n;`;
443+
ret += ` special: ${this[special]}\n`;
444444
}
445445
ret += '}';
446446
return ret;

test/parallel/test-whatwg-url-parsing.js

+68
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,71 @@ additional_tests.forEach((test) => {
127127
if (test.search) assert.strictEqual(test.search, u.search);
128128
if (test.hash) assert.strictEqual(test.hash, u.hash);
129129
});
130+
131+
// test inspect
132+
const allTests = additional_tests.slice();
133+
for (const test of tests) {
134+
if (test.failure || typeof test === 'string') continue;
135+
allTests.push(test);
136+
}
137+
138+
for (const test of allTests) {
139+
const url = test.url
140+
? new URL(test.url)
141+
: new URL(test.input, test.base);
142+
143+
for (const showHidden of [true, false]) {
144+
const res = url.inspect(null, {
145+
showHidden: showHidden
146+
});
147+
148+
const lines = res.split('\n');
149+
150+
const firstLine = lines[0];
151+
assert.strictEqual(firstLine, 'URL {');
152+
153+
const lastLine = lines[lines.length - 1];
154+
assert.strictEqual(lastLine, '}');
155+
156+
const innerLines = lines.slice(1, lines.length - 1);
157+
const keys = new Set();
158+
for (const line of innerLines) {
159+
const i = line.indexOf(': ');
160+
const k = line.slice(0, i).trim();
161+
const v = line.slice(i + 2);
162+
assert.strictEqual(keys.has(k), false, 'duplicate key found: ' + k);
163+
keys.add(k);
164+
165+
const hidden = new Set([
166+
'password',
167+
'cannot-be-base',
168+
'special'
169+
]);
170+
if (showHidden) {
171+
if (!hidden.has(k)) {
172+
assert.strictEqual(v, url[k], k);
173+
continue;
174+
}
175+
176+
if (k === 'password') {
177+
assert.strictEqual(v, url[k], k);
178+
}
179+
if (k === 'cannot-be-base') {
180+
assert.ok(v.match(/^true$|^false$/), k + ' is Boolean');
181+
}
182+
if (k === 'special') {
183+
assert.ok(v.match(/^true$|^false$/), k + ' is Boolean');
184+
}
185+
continue;
186+
}
187+
188+
// showHidden is false
189+
if (k === 'password') {
190+
assert.strictEqual(v, '--------', k);
191+
continue;
192+
}
193+
assert.strictEqual(hidden.has(k), false, 'no hidden keys: ' + k);
194+
assert.strictEqual(v, url[k], k);
195+
}
196+
}
197+
}

0 commit comments

Comments
 (0)