2020-01-02 15:13:47 -05:00
|
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-09-27 06:22:32 -04:00
|
|
|
|
import { assert, assertEquals, assertThrows, unitTest } from "./test_util.ts";
|
2018-09-20 18:53:29 -04:00
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
|
unitTest(function btoaSuccess(): void {
|
2018-09-20 18:53:29 -04:00
|
|
|
|
const text = "hello world";
|
|
|
|
|
const encoded = btoa(text);
|
2019-03-06 20:48:46 -05:00
|
|
|
|
assertEquals(encoded, "aGVsbG8gd29ybGQ=");
|
2018-09-20 18:53:29 -04:00
|
|
|
|
});
|
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
|
unitTest(function atobSuccess(): void {
|
2018-09-20 18:53:29 -04:00
|
|
|
|
const encoded = "aGVsbG8gd29ybGQ=";
|
|
|
|
|
const decoded = atob(encoded);
|
2019-03-06 20:48:46 -05:00
|
|
|
|
assertEquals(decoded, "hello world");
|
2018-09-20 18:53:29 -04:00
|
|
|
|
});
|
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
|
unitTest(function atobWithAsciiWhitespace(): void {
|
2019-04-30 14:25:37 -04:00
|
|
|
|
const encodedList = [
|
|
|
|
|
" aGVsbG8gd29ybGQ=",
|
|
|
|
|
" aGVsbG8gd29ybGQ=",
|
|
|
|
|
"aGVsbG8gd29ybGQ= ",
|
|
|
|
|
"aGVsbG8gd29ybGQ=\n",
|
|
|
|
|
"aGVsbG\t8gd29ybGQ=",
|
|
|
|
|
`aGVsbG\t8g
|
2020-03-28 13:03:49 -04:00
|
|
|
|
d29ybGQ=`,
|
2019-04-30 14:25:37 -04:00
|
|
|
|
];
|
|
|
|
|
|
2019-09-07 12:27:18 -04:00
|
|
|
|
for (const encoded of encodedList) {
|
|
|
|
|
const decoded = atob(encoded);
|
2019-04-30 14:25:37 -04:00
|
|
|
|
assertEquals(decoded, "hello world");
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
|
unitTest(function atobThrows(): void {
|
2019-04-30 14:25:37 -04:00
|
|
|
|
let threw = false;
|
|
|
|
|
try {
|
|
|
|
|
atob("aGVsbG8gd29ybGQ==");
|
|
|
|
|
} catch (e) {
|
|
|
|
|
threw = true;
|
|
|
|
|
}
|
|
|
|
|
assert(threw);
|
|
|
|
|
});
|
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
|
unitTest(function atobThrows2(): void {
|
2019-04-30 14:25:37 -04:00
|
|
|
|
let threw = false;
|
|
|
|
|
try {
|
|
|
|
|
atob("aGVsbG8gd29ybGQ===");
|
|
|
|
|
} catch (e) {
|
|
|
|
|
threw = true;
|
|
|
|
|
}
|
|
|
|
|
assert(threw);
|
|
|
|
|
});
|
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
|
unitTest(function btoaFailed(): void {
|
2018-09-20 18:53:29 -04:00
|
|
|
|
const text = "你好";
|
2020-06-24 18:57:08 -04:00
|
|
|
|
assertThrows(() => {
|
2018-09-20 18:53:29 -04:00
|
|
|
|
btoa(text);
|
2020-06-24 18:57:08 -04:00
|
|
|
|
}, TypeError);
|
2018-09-20 18:53:29 -04:00
|
|
|
|
});
|
2018-12-06 13:01:15 -05:00
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
|
unitTest(function textDecoder2(): void {
|
2020-07-14 15:24:17 -04:00
|
|
|
|
// deno-fmt-ignore
|
2018-12-06 13:01:15 -05: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-06 13:01:15 -05:00
|
|
|
|
});
|
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
|
unitTest(function textDecoderIgnoreBOM(): void {
|
2020-07-14 15:24:17 -04:00
|
|
|
|
// deno-fmt-ignore
|
2019-10-01 20:08:51 -04:00
|
|
|
|
const fixture = new Uint8Array([
|
|
|
|
|
0xef, 0xbb, 0xbf,
|
|
|
|
|
0xf0, 0x9d, 0x93, 0xbd,
|
|
|
|
|
0xf0, 0x9d, 0x93, 0xae,
|
|
|
|
|
0xf0, 0x9d, 0x94, 0x81,
|
|
|
|
|
0xf0, 0x9d, 0x93, 0xbd
|
|
|
|
|
]);
|
|
|
|
|
const decoder = new TextDecoder("utf-8", { ignoreBOM: true });
|
|
|
|
|
assertEquals(decoder.decode(fixture), "𝓽𝓮𝔁𝓽");
|
|
|
|
|
});
|
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
|
unitTest(function textDecoderNotBOM(): void {
|
2020-07-14 15:24:17 -04:00
|
|
|
|
// deno-fmt-ignore
|
2019-10-01 20:08:51 -04:00
|
|
|
|
const fixture = new Uint8Array([
|
|
|
|
|
0xef, 0xbb, 0x89,
|
|
|
|
|
0xf0, 0x9d, 0x93, 0xbd,
|
|
|
|
|
0xf0, 0x9d, 0x93, 0xae,
|
|
|
|
|
0xf0, 0x9d, 0x94, 0x81,
|
|
|
|
|
0xf0, 0x9d, 0x93, 0xbd
|
|
|
|
|
]);
|
|
|
|
|
const decoder = new TextDecoder("utf-8", { ignoreBOM: true });
|
|
|
|
|
assertEquals(decoder.decode(fixture), "ﻉ𝓽𝓮𝔁𝓽");
|
|
|
|
|
});
|
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
|
unitTest(function textDecoderASCII(): void {
|
2018-12-06 18:23:29 -05: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-06 18:23:29 -05:00
|
|
|
|
});
|
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
|
unitTest(function textDecoderErrorEncoding(): void {
|
2018-12-06 18:23:29 -05:00
|
|
|
|
let didThrow = false;
|
|
|
|
|
try {
|
2020-10-20 07:47:38 -04:00
|
|
|
|
new TextDecoder("Foo");
|
2018-12-06 18:23:29 -05:00
|
|
|
|
} catch (e) {
|
|
|
|
|
didThrow = true;
|
2020-10-20 07:47:38 -04:00
|
|
|
|
assertEquals(e.message, "The encoding label provided ('Foo') is invalid.");
|
2018-12-06 18:23:29 -05:00
|
|
|
|
}
|
|
|
|
|
assert(didThrow);
|
|
|
|
|
});
|
|
|
|
|
|
2020-10-19 17:56:29 -04:00
|
|
|
|
unitTest(function textDecoderHandlesNotFoundInternalDecoder() {
|
|
|
|
|
let didThrow = false;
|
|
|
|
|
try {
|
|
|
|
|
new TextDecoder("gbk");
|
|
|
|
|
} catch (e) {
|
|
|
|
|
didThrow = true;
|
|
|
|
|
assert(e instanceof RangeError);
|
|
|
|
|
}
|
|
|
|
|
assert(didThrow);
|
|
|
|
|
});
|
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
|
unitTest(function textEncoder(): void {
|
2018-12-06 13:01:15 -05: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-06 13:01:15 -05: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
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
|
unitTest(function textEncodeInto(): void {
|
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,
|
|
|
|
|
]);
|
|
|
|
|
});
|
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
|
unitTest(function textEncodeInto2(): void {
|
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,
|
|
|
|
|
]);
|
|
|
|
|
});
|
|
|
|
|
|
2020-05-06 13:10:15 -04:00
|
|
|
|
unitTest(function textEncodeInto3(): void {
|
|
|
|
|
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 13:10:15 -04:00
|
|
|
|
assertEquals(Array.from(bytes), [
|
|
|
|
|
0xf0, 0x9d, 0x93, 0xbd, 0x00,
|
|
|
|
|
]);
|
|
|
|
|
});
|
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
|
unitTest(function textDecoderSharedUint8Array(): void {
|
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");
|
|
|
|
|
});
|
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
|
unitTest(function textDecoderSharedInt32Array(): void {
|
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-01 13:05:19 -04:00
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
|
unitTest(function toStringShouldBeWebCompatibility(): void {
|
2019-04-01 13:05:19 -04:00
|
|
|
|
const encoder = new TextEncoder();
|
|
|
|
|
assertEquals(encoder.toString(), "[object TextEncoder]");
|
|
|
|
|
|
|
|
|
|
const decoder = new TextDecoder();
|
|
|
|
|
assertEquals(decoder.toString(), "[object TextDecoder]");
|
|
|
|
|
});
|