Skip to content

Commit

Permalink
chore(encoding): improve test coverage (#3971)
Browse files Browse the repository at this point in the history
* chore(encoding): improve test coverage

* fmt

* tweak

---------

Co-authored-by: Asher Gomez <[email protected]>
  • Loading branch information
babiabeo and iuioiua authored Dec 17, 2023
1 parent 66e8a50 commit 60f7b20
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 3 deletions.
7 changes: 7 additions & 0 deletions encoding/_util_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,11 @@ Deno.test("validateBinaryLike with invalid inputs", () => {
TypeError,
"The input must be a Uint8Array, a string, or an ArrayBuffer. Received a value of the type MyClass.",
);
assertThrows(
() => {
validateBinaryLike(Object.create(null));
},
TypeError,
"The input must be a Uint8Array, a string, or an ArrayBuffer. Received a value of the type object.",
);
});
18 changes: 18 additions & 0 deletions encoding/ascii85_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,21 @@ Deno.test({
assertEquals(encoded2, "F)YQ)");
},
});

Deno.test({
name: `[encoding/ascii85] decode with default standard`,
fn() {
const tests = [
["<~FCfN8Bl7P~>", "testing"],
["<~A7]XsCgh3l~>", "denoland"],
["<~@<5pmBfIsm@:X:cAH~>", "ascii85 adobe"],
];

for (const [input, expect] of tests) {
assertEquals(
decodeAscii85(input),
utf8encoder.encode(expect),
);
}
},
});
28 changes: 26 additions & 2 deletions encoding/base32_test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Test cases copied from https://github.com/LinusU/base32-encode/blob/master/test.js
// Copyright (c) 2016-2017 Linus Unnebäck. MIT license.
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
import { assert, assertEquals } from "../assert/mod.ts";
import { decodeBase32, encodeBase32 } from "./base32.ts";
import { assert, assertEquals, assertExists } from "../assert/mod.ts";
import { byteLength, decodeBase32, encodeBase32 } from "./base32.ts";

// Lifted from https://stackoverflow.com/questions/38987784
const fromHexString = (hexString: string): Uint8Array =>
Expand Down Expand Up @@ -135,3 +135,27 @@ Deno.test({
assert(errorCaught);
},
});

Deno.test({
name: "[encoding.base32] byteLength",
fn() {
const tests: [string, number][] = [
["JBSWY3DPEBLW64TMMQ======", 11],
["3X4A5PRBX4NR4EVGJROMNJ2LLWJN2===", 18],
["WB2K5C467XQPC7ZXXTFN3YAG2A4ZS62ZZDX3AWW5", 25],
["6L6CGGN5FFCXZTIB5DQZJ3U327UXFGFWMEG7JKYPHVN2UCZNPTHWTAU63N2O33Y=", 39],
];

for (const [input, expect] of tests) {
assertEquals(byteLength(input), expect);
}
},
});

Deno.test({
name: "[encoding.base32] encode very long text",
fn() {
const data = "a".repeat(16400);
assertExists(encodeBase32(data));
},
});
17 changes: 17 additions & 0 deletions encoding/base64url_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,20 @@ Deno.test("[decoding/base64url] base64url.decode throws on invalid input", () =>
);
}
});

Deno.test("[decoding/base64url] base64url.decode throws on illegal base64url string", () => {
const testsetIllegalBase64url = [
"w58De",
"Zm9vYmFyy",
"DPj8-ZD_DnwEg",
"SGVsbG8gV29ybGQ-_",
];

for (const illegalBase64url of testsetIllegalBase64url) {
assertThrows(
() => decodeBase64Url(illegalBase64url),
TypeError,
"Illegal base64url string!",
);
}
});
5 changes: 4 additions & 1 deletion encoding/varint_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,11 @@ Deno.test("VarInt encode manual", () => {
[Uint8Array.of(255, 255, 255, 255, 255, 255, 255, 255, 255, 1), 10],
);
});
Deno.test("VarInt encode overflow uint64", () => {
assertThrows(() => encode(1e+30), RangeError, "overflows uint64");
});
Deno.test("VarInt encode overflow with negative", () => {
assertThrows(() => encode(-1), RangeError);
assertThrows(() => encode(-1), RangeError, "signed input given");
});
Deno.test("VarInt encode with offset", () => {
let uint = new Uint8Array(3);
Expand Down

0 comments on commit 60f7b20

Please sign in to comment.