]> Lady’s Gitweb - Pisces/blob - base64.test.js
84d9057e11282cf35d3c19ee6edca9a1844ded24
[Pisces] / base64.test.js
1 // ♓🌟 Piscēs ∷ base64.test.js
2 // ====================================================================
3 //
4 // Copyright © 2020–2022 Lady [@ Lady’s Computer].
5 //
6 // This Source Code Form is subject to the terms of the Mozilla Public
7 // License, v. 2.0. If a copy of the MPL was not distributed with this
8 // file, You can obtain one at <https://mozilla.org/MPL/2.0/>.
9
10 import {
11 assert,
12 assertEquals,
13 assertStrictEquals,
14 assertThrows,
15 describe,
16 it,
17 } from "./dev-deps.js";
18 import { a2b, b2a, isBase64 } from "./base64.js";
19
20 const base64s = {
21 "AGIAYQBzAGUANgA0": "base64",
22 "R/Q=": new Uint16Array([62535]),
23 "S0lCSQ==": new Uint8ClampedArray([75, 73, 66, 73]).buffer,
24 YmFzZTY0: new DataView(
25 new Uint8Array([98, 97, 115, 101, 54, 52]).buffer,
26 ),
27 };
28
29 describe("a2b", () => {
30 it("[[Call]] returns the correct data", () => {
31 assertEquals(
32 a2b("AGIAYQBzAGUANgA0"),
33 new Uint8Array(
34 Array.prototype.map.call(
35 "\u{0}b\u{0}a\u{0}s\u{0}e\u{0}6\u{0}4",
36 ($) => $.charCodeAt(0),
37 ),
38 ).buffer,
39 "AGIAYQBzAGUANgA0",
40 );
41 assertEquals(
42 a2b("R/Q="),
43 new Uint16Array([62535]).buffer,
44 "R/Q=",
45 );
46 assertEquals(
47 a2b("S0lCSQ=="),
48 new Uint8ClampedArray([75, 73, 66, 73]).buffer,
49 "S0lCSQ==",
50 );
51 assertEquals(
52 a2b("YmFzZTY0"),
53 new Uint8Array([98, 97, 115, 101, 54, 52]).buffer,
54 "YmFzZTY0",
55 );
56 });
57
58 it("[[Call]] throws when provided with an invalid character", () => {
59 assertThrows(() => a2b("abc_"));
60 });
61
62 it("[[Call]] throws when provided with an invalid equals", () => {
63 assertThrows(() => a2b("abc=="));
64 });
65
66 it("[[Call]] throws when provided with a length with a remainder of 1 when divided by 4", () => {
67 assertThrows(() => a2b("abcde"));
68 assertThrows(() => a2b("abcde==="));
69 });
70 });
71
72 describe("b2a", () => {
73 it("[[Call]] returns the correct string", () => {
74 Object.entries(base64s).forEach(([key, value]) =>
75 assertStrictEquals(b2a(value), key)
76 );
77 });
78 });
79
80 describe("isBase64", () => {
81 it("[[Call]] returns true for base64 strings", () => {
82 Object.keys(base64s).forEach((key) => assert(isBase64(key)));
83 });
84
85 it("[[Call]] returns false for others", () => {
86 [
87 undefined,
88 null,
89 true,
90 Symbol(),
91 27,
92 98n,
93 {},
94 [],
95 () => {},
96 new Proxy({}, {}),
97 "abc_",
98 "a",
99 "abc==",
100 ].forEach((value) => assert(!isBase64(value)));
101 });
102 });
This page took 0.051391 seconds and 3 git commands to generate.