2022-01-20 16:10:16 +09:00
|
|
|
|
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
2021-11-23 17:45:18 +01:00
|
|
|
|
import { assert, assertEquals, assertThrows } from "./test_util.ts";
|
2018-09-20 15:53:29 -07:00
|
|
|
|
|
2021-11-23 17:45:18 +01:00
|
|
|
|
Deno.test(function btoaSuccess() {
|
2018-09-20 15:53:29 -07:00
|
|
|
|
const text = "hello world";
|
|
|
|
|
const encoded = btoa(text);
|
2019-03-06 20:48:46 -05:00
|
|
|
|
assertEquals(encoded, "aGVsbG8gd29ybGQ=");
|
2018-09-20 15:53:29 -07:00
|
|
|
|
});
|
|
|
|
|
|
2021-11-23 17:45:18 +01:00
|
|
|
|
Deno.test(function atobSuccess() {
|
2018-09-20 15:53:29 -07:00
|
|
|
|
const encoded = "aGVsbG8gd29ybGQ=";
|
|
|
|
|
const decoded = atob(encoded);
|
2019-03-06 20:48:46 -05:00
|
|
|
|
assertEquals(decoded, "hello world");
|
2018-09-20 15:53:29 -07:00
|
|
|
|
});
|
|
|
|
|
|
2021-11-23 17:45:18 +01:00
|
|
|
|
Deno.test(function atobWithAsciiWhitespace() {
|
2019-05-01 02:25:37 +08:00
|
|
|
|
const encodedList = [
|
|
|
|
|
" aGVsbG8gd29ybGQ=",
|
|
|
|
|
" aGVsbG8gd29ybGQ=",
|
|
|
|
|
"aGVsbG8gd29ybGQ= ",
|
|
|
|
|
"aGVsbG8gd29ybGQ=\n",
|
|
|
|
|
"aGVsbG\t8gd29ybGQ=",
|
|
|
|
|
`aGVsbG\t8g
|
2020-03-29 04:03:49 +11:00
|
|
|
|
d29ybGQ=`,
|
2019-05-01 02:25:37 +08:00
|
|
|
|
];
|
|
|
|
|
|
2019-09-08 01:27:18 +09:00
|
|
|
|
for (const encoded of encodedList) {
|
|
|
|
|
const decoded = atob(encoded);
|
2019-05-01 02:25:37 +08:00
|
|
|
|
assertEquals(decoded, "hello world");
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2021-11-23 17:45:18 +01:00
|
|
|
|
Deno.test(function atobThrows() {
|
2019-05-01 02:25:37 +08:00
|
|
|
|
let threw = false;
|
|
|
|
|
try {
|
|
|
|
|
atob("aGVsbG8gd29ybGQ==");
|
2021-04-09 23:35:29 +02:00
|
|
|
|
} catch (_e) {
|
2019-05-01 02:25:37 +08:00
|
|
|
|
threw = true;
|
|
|
|
|
}
|
|
|
|
|
assert(threw);
|
|
|
|
|
});
|
|
|
|
|
|
2021-11-23 17:45:18 +01:00
|
|
|
|
Deno.test(function atobThrows2() {
|
2019-05-01 02:25:37 +08:00
|
|
|
|
let threw = false;
|
|
|
|
|
try {
|
|
|
|
|
atob("aGVsbG8gd29ybGQ===");
|
2021-04-09 23:35:29 +02:00
|
|
|
|
} catch (_e) {
|
2019-05-01 02:25:37 +08:00
|
|
|
|
threw = true;
|
|
|
|
|
}
|
|
|
|
|
assert(threw);
|
|
|
|
|
});
|
|
|
|
|
|
2021-11-23 17:45:18 +01:00
|
|
|
|
Deno.test(function atobThrows3() {
|
2021-01-11 07:15:32 +09:00
|
|
|
|
let threw = false;
|
|
|
|
|
try {
|
|
|
|
|
atob("foobar!!");
|
|
|
|
|
} catch (e) {
|
|
|
|
|
if (
|
|
|
|
|
e instanceof DOMException &&
|
|
|
|
|
e.toString().startsWith("InvalidCharacterError:")
|
|
|
|
|
) {
|
|
|
|
|
threw = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
assert(threw);
|
|
|
|
|
});
|
|
|
|
|
|
2021-11-23 17:45:18 +01:00
|
|
|
|
Deno.test(function btoaFailed() {
|
2018-09-20 15:53:29 -07:00
|
|
|
|
const text = "你好";
|
2020-06-25 06:57:08 +08:00
|
|
|
|
assertThrows(() => {
|
2018-09-20 15:53:29 -07:00
|
|
|
|
btoa(text);
|
2021-02-16 09:10:59 +08:00
|
|
|
|
}, DOMException);
|
2018-09-20 15:53:29 -07:00
|
|
|
|
});
|
2018-12-07 05:01:15 +11:00
|
|
|
|
|
2021-11-23 17:45:18 +01:00
|
|
|
|
Deno.test(function textDecoder2() {
|
2020-07-14 15:24:17 -04:00
|
|
|
|
// deno-fmt-ignore
|
2018-12-07 05:01:15 +11:00
|
|
|
|
const fixture = new Uint8Array([
|
|
|
|
|
0xf0, 0x9d, 0x93, 0xbd,
|
|
|
|
|
0xf0, 0x9d, 0x93, 0xae,
|
|
|
|
|
0xf0, 0x9d, 0x94, 0x81,
|
|
|
|
|
0xf0, 0x9d, 0x93, 0xbd
|
|
|
|
|
]);
|
|
|
|
|
const decoder = new TextDecoder();
|
2019-03-06 20:48:46 -05:00
|
|
|
|
assertEquals(decoder.decode(fixture), "𝓽𝓮𝔁𝓽");
|
2018-12-07 05:01:15 +11:00
|
|
|
|
});
|
|
|
|
|
|
2021-01-05 19:50:40 +01:00
|
|
|
|
// ignoreBOM is tested through WPT
|
2019-10-02 09:08:51 +09:00
|
|
|
|
|
2021-11-23 17:45:18 +01:00
|
|
|
|
Deno.test(function textDecoderASCII() {
|
2018-12-07 10:23:29 +11:00
|
|
|
|
const fixture = new Uint8Array([0x89, 0x95, 0x9f, 0xbf]);
|
|
|
|
|
const decoder = new TextDecoder("ascii");
|
2019-03-06 20:48:46 -05:00
|
|
|
|
assertEquals(decoder.decode(fixture), "‰•Ÿ¿");
|
2018-12-07 10:23:29 +11:00
|
|
|
|
});
|
|
|
|
|
|
2021-11-23 17:45:18 +01:00
|
|
|
|
Deno.test(function textDecoderErrorEncoding() {
|
2018-12-07 10:23:29 +11:00
|
|
|
|
let didThrow = false;
|
|
|
|
|
try {
|
2020-10-20 20:47:38 +09:00
|
|
|
|
new TextDecoder("Foo");
|
2018-12-07 10:23:29 +11:00
|
|
|
|
} catch (e) {
|
|
|
|
|
didThrow = true;
|
2021-10-28 08:43:40 +11:00
|
|
|
|
assert(e instanceof Error);
|
2020-10-20 20:47:38 +09:00
|
|
|
|
assertEquals(e.message, "The encoding label provided ('Foo') is invalid.");
|
2018-12-07 10:23:29 +11:00
|
|
|
|
}
|
|
|
|
|
assert(didThrow);
|
|
|
|
|
});
|
|
|
|
|
|
2021-11-23 17:45:18 +01:00
|
|
|
|
Deno.test(function textEncoder() {
|
2018-12-07 05:01:15 +11:00
|
|
|
|
const fixture = "𝓽𝓮𝔁𝓽";
|
|
|
|
|
const encoder = new TextEncoder();
|
2020-07-14 15:24:17 -04:00
|
|
|
|
// deno-fmt-ignore
|
2019-03-06 20:48:46 -05:00
|
|
|
|
assertEquals(Array.from(encoder.encode(fixture)), [
|
2018-12-07 05:01:15 +11:00
|
|
|
|
0xf0, 0x9d, 0x93, 0xbd,
|
|
|
|
|
0xf0, 0x9d, 0x93, 0xae,
|
|
|
|
|
0xf0, 0x9d, 0x94, 0x81,
|
|
|
|
|
0xf0, 0x9d, 0x93, 0xbd
|
|
|
|
|
]);
|
|
|
|
|
});
|
2019-03-15 16:29:54 -04:00
|
|
|
|
|
2021-11-23 17:45:18 +01:00
|
|
|
|
Deno.test(function textEncodeInto() {
|
2019-06-21 18:32:14 -04:00
|
|
|
|
const fixture = "text";
|
|
|
|
|
const encoder = new TextEncoder();
|
|
|
|
|
const bytes = new Uint8Array(5);
|
|
|
|
|
const result = encoder.encodeInto(fixture, bytes);
|
|
|
|
|
assertEquals(result.read, 4);
|
|
|
|
|
assertEquals(result.written, 4);
|
2020-07-14 15:24:17 -04:00
|
|
|
|
// deno-fmt-ignore
|
2019-06-21 18:32:14 -04:00
|
|
|
|
assertEquals(Array.from(bytes), [
|
|
|
|
|
0x74, 0x65, 0x78, 0x74, 0x00,
|
|
|
|
|
]);
|
|
|
|
|
});
|
|
|
|
|
|
2021-11-23 17:45:18 +01:00
|
|
|
|
Deno.test(function textEncodeInto2() {
|
2019-06-21 18:32:14 -04:00
|
|
|
|
const fixture = "𝓽𝓮𝔁𝓽";
|
|
|
|
|
const encoder = new TextEncoder();
|
|
|
|
|
const bytes = new Uint8Array(17);
|
|
|
|
|
const result = encoder.encodeInto(fixture, bytes);
|
|
|
|
|
assertEquals(result.read, 8);
|
|
|
|
|
assertEquals(result.written, 16);
|
2020-07-14 15:24:17 -04:00
|
|
|
|
// deno-fmt-ignore
|
2019-06-21 18:32:14 -04:00
|
|
|
|
assertEquals(Array.from(bytes), [
|
|
|
|
|
0xf0, 0x9d, 0x93, 0xbd,
|
|
|
|
|
0xf0, 0x9d, 0x93, 0xae,
|
|
|
|
|
0xf0, 0x9d, 0x94, 0x81,
|
|
|
|
|
0xf0, 0x9d, 0x93, 0xbd, 0x00,
|
|
|
|
|
]);
|
|
|
|
|
});
|
|
|
|
|
|
2021-11-23 17:45:18 +01:00
|
|
|
|
Deno.test(function textEncodeInto3() {
|
2020-05-06 20:10:15 +03:00
|
|
|
|
const fixture = "𝓽𝓮𝔁𝓽";
|
|
|
|
|
const encoder = new TextEncoder();
|
|
|
|
|
const bytes = new Uint8Array(5);
|
|
|
|
|
const result = encoder.encodeInto(fixture, bytes);
|
|
|
|
|
assertEquals(result.read, 2);
|
|
|
|
|
assertEquals(result.written, 4);
|
2020-07-14 15:24:17 -04:00
|
|
|
|
// deno-fmt-ignore
|
2020-05-06 20:10:15 +03:00
|
|
|
|
assertEquals(Array.from(bytes), [
|
|
|
|
|
0xf0, 0x9d, 0x93, 0xbd, 0x00,
|
|
|
|
|
]);
|
|
|
|
|
});
|
|
|
|
|
|
2021-11-23 17:45:18 +01:00
|
|
|
|
Deno.test(function loneSurrogateEncodeInto() {
|
2021-05-08 18:31:40 -03:00
|
|
|
|
const fixture = "lone𝄞\ud888surrogate";
|
|
|
|
|
const encoder = new TextEncoder();
|
|
|
|
|
const bytes = new Uint8Array(20);
|
|
|
|
|
const result = encoder.encodeInto(fixture, bytes);
|
|
|
|
|
assertEquals(result.read, 16);
|
|
|
|
|
assertEquals(result.written, 20);
|
|
|
|
|
// deno-fmt-ignore
|
|
|
|
|
assertEquals(Array.from(bytes), [
|
|
|
|
|
0x6c, 0x6f, 0x6e, 0x65,
|
|
|
|
|
0xf0, 0x9d, 0x84, 0x9e,
|
|
|
|
|
0xef, 0xbf, 0xbd, 0x73,
|
|
|
|
|
0x75, 0x72, 0x72, 0x6f,
|
|
|
|
|
0x67, 0x61, 0x74, 0x65
|
|
|
|
|
]);
|
|
|
|
|
});
|
|
|
|
|
|
2021-11-23 17:45:18 +01:00
|
|
|
|
Deno.test(function loneSurrogateEncodeInto2() {
|
2021-05-08 18:31:40 -03:00
|
|
|
|
const fixture = "\ud800";
|
|
|
|
|
const encoder = new TextEncoder();
|
|
|
|
|
const bytes = new Uint8Array(3);
|
|
|
|
|
const result = encoder.encodeInto(fixture, bytes);
|
|
|
|
|
assertEquals(result.read, 1);
|
|
|
|
|
assertEquals(result.written, 3);
|
|
|
|
|
// deno-fmt-ignore
|
|
|
|
|
assertEquals(Array.from(bytes), [
|
|
|
|
|
0xef, 0xbf, 0xbd
|
|
|
|
|
]);
|
|
|
|
|
});
|
|
|
|
|
|
2021-11-23 17:45:18 +01:00
|
|
|
|
Deno.test(function loneSurrogateEncodeInto3() {
|
2021-05-08 18:31:40 -03:00
|
|
|
|
const fixture = "\udc00";
|
|
|
|
|
const encoder = new TextEncoder();
|
|
|
|
|
const bytes = new Uint8Array(3);
|
|
|
|
|
const result = encoder.encodeInto(fixture, bytes);
|
|
|
|
|
assertEquals(result.read, 1);
|
|
|
|
|
assertEquals(result.written, 3);
|
|
|
|
|
// deno-fmt-ignore
|
|
|
|
|
assertEquals(Array.from(bytes), [
|
|
|
|
|
0xef, 0xbf, 0xbd
|
|
|
|
|
]);
|
|
|
|
|
});
|
|
|
|
|
|
2021-11-23 17:45:18 +01:00
|
|
|
|
Deno.test(function swappedSurrogatePairEncodeInto4() {
|
2021-05-08 18:31:40 -03:00
|
|
|
|
const fixture = "\udc00\ud800";
|
|
|
|
|
const encoder = new TextEncoder();
|
|
|
|
|
const bytes = new Uint8Array(8);
|
|
|
|
|
const result = encoder.encodeInto(fixture, bytes);
|
|
|
|
|
assertEquals(result.read, 2);
|
|
|
|
|
assertEquals(result.written, 6);
|
|
|
|
|
// deno-fmt-ignore
|
|
|
|
|
assertEquals(Array.from(bytes), [
|
|
|
|
|
0xef, 0xbf, 0xbd, 0xef, 0xbf, 0xbd, 0x00, 0x00
|
|
|
|
|
]);
|
|
|
|
|
});
|
|
|
|
|
|
2021-11-23 17:45:18 +01:00
|
|
|
|
Deno.test(function textDecoderSharedUint8Array() {
|
2019-03-15 16:29:54 -04:00
|
|
|
|
const ab = new SharedArrayBuffer(6);
|
|
|
|
|
const dataView = new DataView(ab);
|
|
|
|
|
const charCodeA = "A".charCodeAt(0);
|
|
|
|
|
for (let i = 0; i < ab.byteLength; i++) {
|
|
|
|
|
dataView.setUint8(i, charCodeA + i);
|
|
|
|
|
}
|
|
|
|
|
const ui8 = new Uint8Array(ab);
|
|
|
|
|
const decoder = new TextDecoder();
|
|
|
|
|
const actual = decoder.decode(ui8);
|
|
|
|
|
assertEquals(actual, "ABCDEF");
|
|
|
|
|
});
|
|
|
|
|
|
2021-11-23 17:45:18 +01:00
|
|
|
|
Deno.test(function textDecoderSharedInt32Array() {
|
2019-03-15 16:29:54 -04:00
|
|
|
|
const ab = new SharedArrayBuffer(8);
|
|
|
|
|
const dataView = new DataView(ab);
|
|
|
|
|
const charCodeA = "A".charCodeAt(0);
|
|
|
|
|
for (let i = 0; i < ab.byteLength; i++) {
|
|
|
|
|
dataView.setUint8(i, charCodeA + i);
|
|
|
|
|
}
|
|
|
|
|
const i32 = new Int32Array(ab);
|
|
|
|
|
const decoder = new TextDecoder();
|
|
|
|
|
const actual = decoder.decode(i32);
|
|
|
|
|
assertEquals(actual, "ABCDEFGH");
|
|
|
|
|
});
|
2019-04-02 01:05:19 +08:00
|
|
|
|
|
2021-11-23 17:45:18 +01:00
|
|
|
|
Deno.test(function toStringShouldBeWebCompatibility() {
|
2019-04-02 01:05:19 +08:00
|
|
|
|
const encoder = new TextEncoder();
|
|
|
|
|
assertEquals(encoder.toString(), "[object TextEncoder]");
|
|
|
|
|
|
|
|
|
|
const decoder = new TextDecoder();
|
|
|
|
|
assertEquals(decoder.toString(), "[object TextDecoder]");
|
|
|
|
|
});
|
2021-11-23 17:45:18 +01:00
|
|
|
|
Deno.test(function textEncoderShouldCoerceToString() {
|
2020-11-02 01:57:18 +02:00
|
|
|
|
const encoder = new TextEncoder();
|
|
|
|
|
const fixutreText = "text";
|
|
|
|
|
const fixture = {
|
|
|
|
|
toString() {
|
|
|
|
|
return fixutreText;
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const bytes = encoder.encode(fixture as unknown as string);
|
|
|
|
|
const decoder = new TextDecoder();
|
|
|
|
|
const decoded = decoder.decode(bytes);
|
|
|
|
|
assertEquals(decoded, fixutreText);
|
|
|
|
|
});
|