]> Lady’s Gitweb - Pisces/blob - binary.test.js
161f8db2736ced1ac332c0f5ee3d8d105fa42a70
[Pisces] / binary.test.js
1 // ♓🌟 Piscēs ∷ binary.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 {
19 base64Binary,
20 base64String,
21 filenameSafeBase64Binary,
22 filenameSafeBase64String,
23 isBase64,
24 isFilenameSafeBase64,
25 } from "./binary.js";
26
27 const base64s = {
28 "AGIAYQBzAGUANgA0": "base64",
29 "R/Q=": new Uint16Array([62535]),
30 "S0lCSQ==": new Uint8ClampedArray([75, 73, 66, 73]).buffer,
31 YmFzZTY0: new DataView(
32 new Uint8Array([98, 97, 115, 101, 54, 52]).buffer,
33 ),
34 };
35
36 describe("base64Binary", () => {
37 it("[[Call]] returns the correct data", () => {
38 assertEquals(
39 base64Binary("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 "AGIAYQBzAGUANgA0",
47 );
48 assertEquals(
49 base64Binary("R/Q="),
50 new Uint16Array([62535]).buffer,
51 "R/Q=",
52 );
53 assertEquals(
54 base64Binary("S0lCSQ=="),
55 new Uint8ClampedArray([75, 73, 66, 73]).buffer,
56 "S0lCSQ==",
57 );
58 assertEquals(
59 base64Binary("YmFzZTY0"),
60 new Uint8Array([98, 97, 115, 101, 54, 52]).buffer,
61 "YmFzZTY0",
62 );
63 });
64
65 it("[[Call]] throws when provided with an invalid character", () => {
66 assertThrows(() => base64Binary("abc_"));
67 });
68
69 it("[[Call]] throws when provided with an invalid equals", () => {
70 assertThrows(() => base64Binary("abc=="));
71 });
72
73 it("[[Call]] throws when provided with a length with a remainder of 1 when divided by 4", () => {
74 assertThrows(() => base64Binary("abcde"));
75 assertThrows(() => base64Binary("abcde==="));
76 });
77 });
78
79 describe("base64String", () => {
80 it("[[Call]] returns the correct string", () => {
81 Object.entries(base64s).forEach(([key, value]) =>
82 assertStrictEquals(base64String(value), key)
83 );
84 });
85 });
86
87 describe("filenameSafeBase64Binary", () => {
88 it("[[Call]] returns the correct data", () => {
89 assertEquals(
90 filenameSafeBase64Binary("AGIAYQBzAGUANgA0"),
91 new Uint8Array(
92 Array.prototype.map.call(
93 "\u{0}b\u{0}a\u{0}s\u{0}e\u{0}6\u{0}4",
94 ($) => $.charCodeAt(0),
95 ),
96 ).buffer,
97 "AGIAYQBzAGUANgA0",
98 );
99 assertEquals(
100 filenameSafeBase64Binary("R_Q="),
101 new Uint16Array([62535]).buffer,
102 "R/Q=",
103 );
104 assertEquals(
105 filenameSafeBase64Binary("S0lCSQ=="),
106 new Uint8ClampedArray([75, 73, 66, 73]).buffer,
107 "S0lCSQ==",
108 );
109 assertEquals(
110 filenameSafeBase64Binary("YmFzZTY0"),
111 new Uint8Array([98, 97, 115, 101, 54, 52]).buffer,
112 "YmFzZTY0",
113 );
114 });
115
116 it("[[Call]] throws when provided with an invalid character", () => {
117 assertThrows(() => filenameSafeBase64Binary("abc/"));
118 });
119
120 it("[[Call]] throws when provided with an invalid equals", () => {
121 assertThrows(() => filenameSafeBase64Binary("abc=="));
122 });
123
124 it("[[Call]] throws when provided with a length with a remainder of 1 when divided by 4", () => {
125 assertThrows(() => filenameSafeBase64Binary("abcde"));
126 assertThrows(() => filenameSafeBase64Binary("abcde==="));
127 });
128 });
129
130 describe("filenameSafeBase64String", () => {
131 it("[[Call]] returns the correct string", () => {
132 Object.entries(base64s).forEach(([key, value]) =>
133 assertStrictEquals(
134 filenameSafeBase64String(value),
135 key.replace("+", "-").replace("/", "_"),
136 )
137 );
138 });
139 });
140
141 describe("isBase64", () => {
142 it("[[Call]] returns true for base64 strings", () => {
143 Object.keys(base64s).forEach((key) => assert(isBase64(key)));
144 });
145
146 it("[[Call]] returns false for others", () => {
147 [
148 undefined,
149 null,
150 true,
151 Symbol(),
152 27,
153 98n,
154 {},
155 [],
156 () => {},
157 new Proxy({}, {}),
158 "abc_",
159 "a",
160 "abc==",
161 ].forEach((value) => assert(!isBase64(value)));
162 });
163 });
164
165 describe("isFilenameSafeBase64", () => {
166 it("[[Call]] returns true for filename‐safe base64 strings", () => {
167 Object.keys(base64s).forEach((key) =>
168 assert(
169 isFilenameSafeBase64(key.replace("+", "-").replace("/", "_")),
170 )
171 );
172 });
173
174 it("[[Call]] returns false for others", () => {
175 [
176 undefined,
177 null,
178 true,
179 Symbol(),
180 27,
181 98n,
182 {},
183 [],
184 () => {},
185 new Proxy({}, {}),
186 "abc/",
187 "a",
188 "abc==",
189 ].forEach((value) => assert(!isFilenameSafeBase64(value)));
190 });
191 });
This page took 0.054659 seconds and 3 git commands to generate.