Skip to content

Commit d02993e

Browse files
committed
test: compare single-byte against WHATWG non-normative indexes.json
1 parent 142166b commit d02993e

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
lines changed

fallback/single-byte.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export const assertEncoding = (encoding) => {
1313

1414
const r = 0xff_fd
1515

16-
function getEncoding(encoding) {
16+
export function getEncoding(encoding) {
1717
assertEncoding(encoding)
1818
if (encoding === xUserDefined) return Array.from({ length: 128 }, (_, i) => 0xf7_80 + i)
1919
if (encoding === iso8i) encoding = 'iso-8859-8'
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const raw = require('./indexes.json')
2+
3+
const {
4+
big5,
5+
'euc-kr': eucKr,
6+
gb18030,
7+
jis0208,
8+
jis0212,
9+
'gb18030-ranges': gb18030ranges,
10+
'iso-2022-jp-katakana': katakana,
11+
...singleByte
12+
} = raw
13+
const multiByte = { big5, 'euc-kr': eucKr, gb18030, jis0208, jis0212 }
14+
15+
module.exports = { raw, gb18030ranges, katakana, multiByte, singleByte }

tests/single-byte.test.js

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ import { readFileSync } from 'node:fs'
22
import { join } from 'node:path'
33
import { test, describe } from 'node:test'
44
import { createSinglebyteDecoder, createSinglebyteEncoder } from '@exodus/bytes/single-byte.js'
5-
import { encodingDecoder } from '../fallback/single-byte.js'
5+
import { encodingDecoder, getEncoding } from '../fallback/single-byte.js'
66
import encodingsObject from '../fallback/single-byte.encodings.js'
7+
import { singleByte } from './encoding/fixtures/indexes.cjs'
78

89
const encodings = Object.keys(encodingsObject)
910
const nonWhatwg = new Set(['iso-8859-1', 'iso-8859-9', 'iso-8859-11'])
@@ -214,6 +215,42 @@ describe('single-byte encodings index: WHATWG', () => {
214215
}
215216
})
216217

218+
describe('single-byte encodings index: WHATWG non-normative indexes.json', () => {
219+
for (const [encoding, data] of Object.entries(singleByte)) {
220+
test(encoding, (t) => {
221+
t.assert.ok(Object.hasOwn(encodingsObject, encoding))
222+
const high = getEncoding(encoding)
223+
while (high.length < 128) high.push(128 + high.length)
224+
t.assert.deepStrictEqual(
225+
high,
226+
data.map((x) => (x === null ? 65_533 : x))
227+
)
228+
const decoder = createSinglebyteDecoder(encoding)
229+
const decoderLoose = createSinglebyteDecoder(encoding, true)
230+
const encoder = createSinglebyteEncoder(encoding)
231+
232+
t.assert.strictEqual(data.length, 128)
233+
for (let i = 0; i < data.length; i++) {
234+
const byte = i + 128
235+
t.assert.notEqual(data[i], 0)
236+
t.assert.notEqual(data[i], 0xff_fd)
237+
const str = data[i] === null ? null : String.fromCodePoint(data[i])
238+
239+
if (str) {
240+
t.assert.ok(data[i] > 0)
241+
t.assert.ok(data[i] <= 0xff_ff)
242+
t.assert.strictEqual(decoder(Uint8Array.of(byte)), str)
243+
t.assert.strictEqual(decoderLoose(Uint8Array.of(byte)), str)
244+
t.assert.deepStrictEqual(encoder(str), Uint8Array.of(byte))
245+
} else {
246+
t.assert.throws(() => decoder(Uint8Array.of(byte)))
247+
t.assert.strictEqual(decoderLoose(Uint8Array.of(byte)), '\uFFFD')
248+
}
249+
}
250+
})
251+
}
252+
})
253+
217254
// https://encoding.spec.whatwg.org/#x-user-defined-decoder
218255
describe('x-user-defined', () => {
219256
const encoding = 'x-user-defined'

0 commit comments

Comments
 (0)