]> Lady’s Gitweb - Pisces/blob - base64.test.js
e6361a45e037c6f68bc5efbcdcac441bcaed6f35
[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 { a2b, b2a, isBase64 } from "./base64.js";
11 import {
12 assert,
13 assertEquals,
14 assertStrictEquals,
15 assertThrows,
16 } from "./dev-deps.js";
17
18 const base64s = {
19 "AGIAYQBzAGUANgA0": "base64",
20 "R/Q=": new Uint16Array([62535]),
21 "S0lCSQ==": new Uint8ClampedArray([75, 73, 66, 73]).buffer,
22 YmFzZTY0: new DataView(
23 new Uint8Array([98, 97, 115, 101, 54, 52]).buffer,
24 ),
25 };
26
27 Deno.test({
28 name: "Calling b2a returns the correct string.",
29 fn: () =>
30 Object.entries(base64s).forEach(([key, value]) =>
31 assertStrictEquals(b2a(value), key)
32 ),
33 });
34
35 Deno.test({
36 name: "Calling a2b returns the correct data.",
37 fn: () => {
38 assertEquals(
39 a2b("AGIAYQBzAGUANgA0"),
40 new Uint8Array(
41 Array.prototype.map.call(
42 "\u{0}b\u{0}a\u{0}s\u{0}e\u{0}6\u{0}4",
43 ($) => $.charCodeAt(0),
44 ),
45 ).buffer,
46 );
47 assertEquals(
48 a2b("R/Q="),
49 new Uint16Array([62535]).buffer,
50 );
51 assertEquals(
52 a2b("S0lCSQ=="),
53 new Uint8ClampedArray([75, 73, 66, 73]).buffer,
54 );
55 assertEquals(
56 a2b("YmFzZTY0"),
57 new Uint8Array([98, 97, 115, 101, 54, 52]).buffer,
58 );
59 },
60 });
61
62 Deno.test({
63 name: "Calling a2b throws when provided with an invalid character.",
64 fn: () => assertThrows(() => a2b("abc_")),
65 });
66
67 Deno.test({
68 name: "Calling a2b throws when provided with an invalid equals.",
69 fn: () => assertThrows(() => a2b("abc==")),
70 });
71
72 Deno.test({
73 name:
74 "Calling a2b throws when provided with a length with a remainder of 1 when divided by 4.",
75 fn: () => {
76 assertThrows(() => a2b("abcde"));
77 assertThrows(() => a2b("abcde==="));
78 },
79 });
80
81 Deno.test({
82 name: "isBase64 returns true for base64 strings.",
83 fn: () =>
84 Object.keys(base64s).forEach((key) => assert(isBase64(key))),
85 });
86
87 Deno.test({
88 name: "isBase64 returns false for others.",
89 fn: () =>
90 [
91 undefined,
92 null,
93 true,
94 Symbol(),
95 27,
96 98n,
97 {},
98 [],
99 () => {},
100 new Proxy({}, {}),
101 "abc_",
102 "a",
103 "abc==",
104 ].forEach((value) => assert(!isBase64(value))),
105 });
This page took 0.053921 seconds and 3 git commands to generate.