2019-01-21 14:03:30 -05:00
|
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
2019-03-06 20:48:46 -05:00
|
|
|
|
import { test, assert, assertEquals } from "./test_util.ts";
|
2018-09-20 18:53:29 -04:00
|
|
|
|
|
2019-04-28 12:14:57 -04:00
|
|
|
|
test(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
|
|
|
|
});
|
|
|
|
|
|
2019-04-28 12:14:57 -04:00
|
|
|
|
test(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
|
|
|
|
});
|
|
|
|
|
|
2019-04-30 14:25:37 -04:00
|
|
|
|
test(function atobWithAsciiWhitespace(): void {
|
|
|
|
|
const encodedList = [
|
|
|
|
|
" aGVsbG8gd29ybGQ=",
|
|
|
|
|
" aGVsbG8gd29ybGQ=",
|
|
|
|
|
"aGVsbG8gd29ybGQ= ",
|
|
|
|
|
"aGVsbG8gd29ybGQ=\n",
|
|
|
|
|
"aGVsbG\t8gd29ybGQ=",
|
|
|
|
|
`aGVsbG\t8g
|
|
|
|
|
d29ybGQ=`
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
for (let encoded of encodedList) {
|
|
|
|
|
let decoded = atob(encoded);
|
|
|
|
|
assertEquals(decoded, "hello world");
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test(function atobThrows(): void {
|
|
|
|
|
let threw = false;
|
|
|
|
|
try {
|
|
|
|
|
atob("aGVsbG8gd29ybGQ==");
|
|
|
|
|
} catch (e) {
|
|
|
|
|
threw = true;
|
|
|
|
|
}
|
|
|
|
|
assert(threw);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test(function atobThrows2(): void {
|
|
|
|
|
let threw = false;
|
|
|
|
|
try {
|
|
|
|
|
atob("aGVsbG8gd29ybGQ===");
|
|
|
|
|
} catch (e) {
|
|
|
|
|
threw = true;
|
|
|
|
|
}
|
|
|
|
|
assert(threw);
|
|
|
|
|
});
|
|
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
|
test(function btoaFailed(): void {
|
2018-09-20 18:53:29 -04:00
|
|
|
|
const text = "你好";
|
|
|
|
|
let err;
|
|
|
|
|
try {
|
|
|
|
|
btoa(text);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
err = e;
|
|
|
|
|
}
|
|
|
|
|
assert(!!err);
|
2019-03-06 20:48:46 -05:00
|
|
|
|
assertEquals(err.name, "InvalidInput");
|
2018-09-20 18:53:29 -04:00
|
|
|
|
});
|
2018-12-06 13:01:15 -05:00
|
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
|
test(function textDecoder2(): void {
|
2018-12-06 13:01:15 -05:00
|
|
|
|
// prettier-ignore
|
|
|
|
|
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
|
|
|
|
});
|
|
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
|
test(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
|
|
|
|
});
|
|
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
|
test(function textDecoderErrorEncoding(): void {
|
2018-12-06 18:23:29 -05:00
|
|
|
|
let didThrow = false;
|
|
|
|
|
try {
|
2019-03-09 12:30:38 -05:00
|
|
|
|
new TextDecoder("foo");
|
2018-12-06 18:23:29 -05:00
|
|
|
|
} catch (e) {
|
|
|
|
|
didThrow = true;
|
2019-03-06 20:48:46 -05:00
|
|
|
|
assertEquals(e.message, "The encoding label provided ('foo') is invalid.");
|
2018-12-06 18:23:29 -05:00
|
|
|
|
}
|
|
|
|
|
assert(didThrow);
|
|
|
|
|
});
|
|
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
|
test(function textEncoder2(): void {
|
2018-12-06 13:01:15 -05:00
|
|
|
|
const fixture = "𝓽𝓮𝔁𝓽";
|
|
|
|
|
const encoder = new TextEncoder();
|
|
|
|
|
// prettier-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
|
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
|
test(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");
|
|
|
|
|
});
|
|
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
|
test(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
|
|
|
|
|
2019-04-21 16:40:10 -04:00
|
|
|
|
test(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]");
|
|
|
|
|
});
|