2022-01-07 22:09:52 -05:00
|
|
|
|
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
2021-02-04 17:18:32 -05:00
|
|
|
|
"use strict";
|
2020-11-03 10:19:29 -05:00
|
|
|
|
function assert(cond) {
|
|
|
|
|
if (!cond) {
|
|
|
|
|
throw Error("assert");
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-03-15 10:31:55 -04:00
|
|
|
|
|
|
|
|
|
function assertArrayEquals(a1, a2) {
|
|
|
|
|
if (a1.length !== a2.length) throw Error("assert");
|
|
|
|
|
|
|
|
|
|
for (const index in a1) {
|
|
|
|
|
if (a1[index] !== a2[index]) {
|
|
|
|
|
throw Error("assert");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function main() {
|
2020-07-14 15:24:17 -04:00
|
|
|
|
// deno-fmt-ignore
|
2020-03-15 10:31:55 -04:00
|
|
|
|
const fixture1 = [
|
|
|
|
|
0xf0, 0x9d, 0x93, 0xbd,
|
|
|
|
|
0xf0, 0x9d, 0x93, 0xae,
|
|
|
|
|
0xf0, 0x9d, 0x94, 0x81,
|
|
|
|
|
0xf0, 0x9d, 0x93, 0xbd
|
|
|
|
|
];
|
2020-07-14 15:24:17 -04:00
|
|
|
|
// deno-fmt-ignore
|
|
|
|
|
const fixture2 = [
|
|
|
|
|
72, 101, 108, 108,
|
|
|
|
|
111, 32, 239, 191,
|
|
|
|
|
189, 239, 191, 189,
|
|
|
|
|
32, 87, 111, 114,
|
|
|
|
|
108, 100
|
2020-03-15 10:31:55 -04:00
|
|
|
|
];
|
|
|
|
|
|
2020-03-19 16:31:56 -04:00
|
|
|
|
const empty = Deno.core.encode("");
|
|
|
|
|
if (empty.length !== 0) throw new Error("assert");
|
|
|
|
|
|
2020-03-15 10:31:55 -04:00
|
|
|
|
assertArrayEquals(Array.from(Deno.core.encode("𝓽𝓮𝔁𝓽")), fixture1);
|
|
|
|
|
assertArrayEquals(
|
|
|
|
|
Array.from(Deno.core.encode("Hello \udc12\ud834 World")),
|
2020-07-14 15:24:17 -04:00
|
|
|
|
fixture2,
|
2020-03-15 10:31:55 -04:00
|
|
|
|
);
|
|
|
|
|
|
2020-03-19 16:31:56 -04:00
|
|
|
|
const emptyBuf = Deno.core.decode(new Uint8Array(0));
|
|
|
|
|
if (emptyBuf !== "") throw new Error("assert");
|
|
|
|
|
|
2020-03-15 10:31:55 -04:00
|
|
|
|
assert(Deno.core.decode(new Uint8Array(fixture1)) === "𝓽𝓮𝔁𝓽");
|
|
|
|
|
assert(Deno.core.decode(new Uint8Array(fixture2)) === "Hello <20><> World");
|
2020-09-09 14:52:11 -04:00
|
|
|
|
|
|
|
|
|
// See https://github.com/denoland/deno/issues/6649
|
|
|
|
|
let thrown = false;
|
|
|
|
|
try {
|
|
|
|
|
Deno.core.decode(new Uint8Array(2 ** 29));
|
|
|
|
|
} catch (e) {
|
|
|
|
|
thrown = true;
|
|
|
|
|
assert(e instanceof RangeError);
|
|
|
|
|
assert(e.message === "string too long");
|
|
|
|
|
}
|
|
|
|
|
assert(thrown);
|
2020-03-15 10:31:55 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
main();
|