1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-28 10:09:20 -05:00
denoland-deno/js/text_encoding_test.ts

68 lines
1.6 KiB
TypeScript
Raw Normal View History

2019-01-21 14:03:30 -05:00
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import { test, assert, assertEquals } from "./test_util.ts";
2018-09-20 18:53:29 -04:00
test(function atobSuccess() {
const text = "hello world";
const encoded = btoa(text);
assertEquals(encoded, "aGVsbG8gd29ybGQ=");
2018-09-20 18:53:29 -04:00
});
test(function btoaSuccess() {
const encoded = "aGVsbG8gd29ybGQ=";
const decoded = atob(encoded);
assertEquals(decoded, "hello world");
2018-09-20 18:53:29 -04:00
});
test(function btoaFailed() {
const text = "你好";
let err;
try {
btoa(text);
} catch (e) {
err = e;
}
assert(!!err);
assertEquals(err.name, "InvalidInput");
2018-09-20 18:53:29 -04:00
});
test(function textDecoder2() {
// 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();
assertEquals(decoder.decode(fixture), "𝓽𝓮𝔁𝓽");
});
2018-12-06 18:23:29 -05:00
test(function textDecoderASCII() {
const fixture = new Uint8Array([0x89, 0x95, 0x9f, 0xbf]);
const decoder = new TextDecoder("ascii");
assertEquals(decoder.decode(fixture), "‰•Ÿ¿");
2018-12-06 18:23:29 -05:00
});
test(function textDecoderErrorEncoding() {
let didThrow = false;
try {
new TextDecoder("foo");
2018-12-06 18:23:29 -05:00
} catch (e) {
didThrow = true;
assertEquals(e.message, "The encoding label provided ('foo') is invalid.");
2018-12-06 18:23:29 -05:00
}
assert(didThrow);
});
test(function textEncoder2() {
const fixture = "𝓽𝓮𝔁𝓽";
const encoder = new TextEncoder();
// prettier-ignore
assertEquals(Array.from(encoder.encode(fixture)), [
0xf0, 0x9d, 0x93, 0xbd,
0xf0, 0x9d, 0x93, 0xae,
0xf0, 0x9d, 0x94, 0x81,
0xf0, 0x9d, 0x93, 0xbd
]);
});