+++ /dev/null
-// ♓🌟 Piscēs ∷ base64.test.js
-// ====================================================================
-//
-// Copyright © 2020–2022 Lady [@ Lady’s Computer].
-//
-// This Source Code Form is subject to the terms of the Mozilla Public
-// License, v. 2.0. If a copy of the MPL was not distributed with this
-// file, You can obtain one at <https://mozilla.org/MPL/2.0/>.
-
-import {
- assert,
- assertEquals,
- assertStrictEquals,
- assertThrows,
- describe,
- it,
-} from "./dev-deps.js";
-import { a2b, b2a, isBase64 } from "./base64.js";
-
-const base64s = {
- "AGIAYQBzAGUANgA0": "base64",
- "R/Q=": new Uint16Array([62535]),
- "S0lCSQ==": new Uint8ClampedArray([75, 73, 66, 73]).buffer,
- YmFzZTY0: new DataView(
- new Uint8Array([98, 97, 115, 101, 54, 52]).buffer,
- ),
-};
-
-describe("a2b", () => {
- it("[[Call]] returns the correct data", () => {
- assertEquals(
- a2b("AGIAYQBzAGUANgA0"),
- new Uint8Array(
- Array.prototype.map.call(
- "\u{0}b\u{0}a\u{0}s\u{0}e\u{0}6\u{0}4",
- ($) => $.charCodeAt(0),
- ),
- ).buffer,
- "AGIAYQBzAGUANgA0",
- );
- assertEquals(
- a2b("R/Q="),
- new Uint16Array([62535]).buffer,
- "R/Q=",
- );
- assertEquals(
- a2b("S0lCSQ=="),
- new Uint8ClampedArray([75, 73, 66, 73]).buffer,
- "S0lCSQ==",
- );
- assertEquals(
- a2b("YmFzZTY0"),
- new Uint8Array([98, 97, 115, 101, 54, 52]).buffer,
- "YmFzZTY0",
- );
- });
-
- it("[[Call]] throws when provided with an invalid character", () => {
- assertThrows(() => a2b("abc_"));
- });
-
- it("[[Call]] throws when provided with an invalid equals", () => {
- assertThrows(() => a2b("abc=="));
- });
-
- it("[[Call]] throws when provided with a length with a remainder of 1 when divided by 4", () => {
- assertThrows(() => a2b("abcde"));
- assertThrows(() => a2b("abcde==="));
- });
-});
-
-describe("b2a", () => {
- it("[[Call]] returns the correct string", () => {
- Object.entries(base64s).forEach(([key, value]) =>
- assertStrictEquals(b2a(value), key)
- );
- });
-});
-
-describe("isBase64", () => {
- it("[[Call]] returns true for base64 strings", () => {
- Object.keys(base64s).forEach((key) => assert(isBase64(key)));
- });
-
- it("[[Call]] returns false for others", () => {
- [
- undefined,
- null,
- true,
- Symbol(),
- 27,
- 98n,
- {},
- [],
- () => {},
- new Proxy({}, {}),
- "abc_",
- "a",
- "abc==",
- ].forEach((value) => assert(!isBase64(value)));
- });
-});