Skip to content

Commit 2e241b1

Browse files
committed
more tests
1 parent e2f4b7a commit 2e241b1

File tree

1 file changed

+61
-14
lines changed

1 file changed

+61
-14
lines changed

tests/whatwg.test.js

Lines changed: 61 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,67 @@
11
import '@exodus/bytes/encoding.js'
22
import { percentEncodeAfterEncoding } from '@exodus/bytes/whatwg.js'
3-
import { test } from 'node:test'
3+
import { describe, test } from 'node:test'
4+
import { labels } from './encoding/fixtures/encodings.cjs'
5+
6+
const userinfo = ' "#/:;<=>?@[\\]^`{|}' // https://url.spec.whatwg.org/#userinfo-percent-encode-set
7+
const jsuri = ' "%<>[\\]^`{|}' // https://tc39.es/ecma262/#sec-encodeuri-uri
8+
const jsuricomponent = ' "#$%&+,/:;<=>?@[\\]^`{|}' // https://tc39.es/ecma262/#sec-encodeuricomponent-uricomponent
9+
10+
const slowEngine =
11+
process.env.EXODUS_TEST_PLATFORM === 'quickjs' ||
12+
process.env.EXODUS_TEST_PLATFORM === 'xs' ||
13+
process.env.EXODUS_TEST_PLATFORM === 'engine262'
414

515
// https://url.spec.whatwg.org/#example-percent-encode-operations
6-
test('percent-encode after encoding', (t) => {
7-
const userinfo = ' "#/:;<=>?@[\\]^`{|}' // https://url.spec.whatwg.org/#userinfo-percent-encode-set
16+
describe('percent-encode after encoding', () => {
817
const f = percentEncodeAfterEncoding
9-
t.assert.strictEqual(f('Shift_JIS', ' ', userinfo), '%20')
10-
t.assert.strictEqual(f('Shift_JIS', '≡', userinfo), '%81%DF')
11-
t.assert.strictEqual(f('Shift_JIS', '‽', userinfo), '%26%238253%3B')
12-
// t.assert.strictEqual(f('ISO-2022-JP', '¥', userinfo), "%1B(J\\%1B(B") // https://github.com/whatwg/url/issues/895
13-
t.assert.strictEqual(
14-
f('Shift_JIS', '1+1 ≡ 2%20‽', userinfo, true),
15-
'1+1+%81%DF+2%20%26%238253%3B'
16-
)
17-
t.assert.strictEqual(f('UTF-8', '≡', userinfo), '%E2%89%A1')
18-
t.assert.strictEqual(f('UTF-8', '‽', userinfo), '%E2%80%BD')
19-
t.assert.strictEqual(f('UTF-8', 'Say what‽', userinfo), 'Say%20what%E2%80%BD')
18+
19+
test('examples from spec', (t) => {
20+
t.assert.strictEqual(f('Shift_JIS', ' ', userinfo), '%20')
21+
t.assert.strictEqual(f('Shift_JIS', '≡', userinfo), '%81%DF')
22+
t.assert.strictEqual(f('Shift_JIS', '‽', userinfo), '%26%238253%3B')
23+
// t.assert.strictEqual(f('ISO-2022-JP', '¥', userinfo), "%1B(J\\%1B(B") // https://github.com/whatwg/url/issues/895
24+
t.assert.strictEqual(
25+
f('Shift_JIS', '1+1 ≡ 2%20‽', userinfo, true),
26+
'1+1+%81%DF+2%20%26%238253%3B'
27+
)
28+
t.assert.strictEqual(f('UTF-8', '≡', userinfo), '%E2%89%A1')
29+
t.assert.strictEqual(f('UTF-8', '‽', userinfo), '%E2%80%BD')
30+
t.assert.strictEqual(f('UTF-8', 'Say what‽', userinfo), 'Say%20what%E2%80%BD')
31+
})
32+
33+
describe('throws on unkown, utf-16 and replacement', () => {
34+
// https://encoding.spec.whatwg.org/#get-an-encoder
35+
for (const encoding of ['what', 'UTF-16', 'utf16-le', 'utf16-be', 'replacement', 'unicode']) {
36+
test(encoding, (t) => {
37+
t.assert.throws(() => f(encoding, '', userinfo), /encoding/)
38+
t.assert.throws(() => f(encoding, '', jsuri), /encoding/)
39+
})
40+
}
41+
})
42+
43+
describe('throws on non-scalarvalue', () => {
44+
for (const encoding of labels) {
45+
test(encoding, (t) => {
46+
for (let cp = 0xd8_00; cp < 0xe0_00; cp++) {
47+
const s = String.fromCodePoint(cp)
48+
t.assert.throws(() => f(encoding, s, userinfo))
49+
t.assert.throws(() => f(encoding, s, jsuri))
50+
}
51+
})
52+
}
53+
})
54+
55+
test('encodeURI / encodeURIComponent', (t) => {
56+
// https://tc39.es/ecma262/#sec-encodeuri-uri step 2, coherence check
57+
t.assert.deepStrictEqual([...(jsuri + ';/?:@&=+$,#')].sort(), [...jsuricomponent].sort())
58+
59+
const MAX = slowEngine ? 0x1_ff_ff : 0x10_ff_ff // Max Unicode codepoint
60+
for (let cp = 0; cp <= MAX; cp++) {
61+
if (cp >= 0xd8_00 && cp < 0xe0_00) continue
62+
const s = String.fromCodePoint(cp)
63+
t.assert.strictEqual(f('utf8', s, jsuricomponent), encodeURIComponent(s))
64+
t.assert.strictEqual(f('utf8', s, jsuri), encodeURI(s))
65+
}
66+
})
2067
})

0 commit comments

Comments
 (0)