Skip to content

Commit

Permalink
Add tests. Make build pipeline work in both Deno 1 and Deno 2.
Browse files Browse the repository at this point in the history
  • Loading branch information
Hexagon committed Oct 18, 2024
1 parent 06375f9 commit 2b58565
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 48 deletions.
2 changes: 1 addition & 1 deletion build/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,4 @@ if (Deno.args[1] === "clean") {
2,
)),
);
}
}
88 changes: 44 additions & 44 deletions build/package.template.json
Original file line number Diff line number Diff line change
@@ -1,45 +1,45 @@
{
"description": "Base64 and base64url to string or arraybuffer, and back. Node, Deno or browser.",
"author": "Hexagon <github.com/hexagon>",
"contributors": [
{
"name": "Niklas von Hertzen",
"email": "niklasvh@gmail.com",
"url": "https://hertzen.com"
}
],
"homepage": "https://base64.56k.guru",
"repository": {
"type": "git",
"url": "https://github.com/hexagon/base64"
},
"bugs": {
"url": "https://github.com/hexagon/base64/issues"
},
"files": [
"dist/*",
"SECURITY.md"
],
"keywords": [
"base64",
"base64url",
"parser",
"isomorphic",
"arraybuffer",
"string"
],
"type": "module",
"main": "./dist/base64.cjs",
"browser": "./dist/base64.umd.js",
"module": "./dist/base64.js",
"types": "./dist/base64.d.ts",
"exports": {
"./package.json": "./package.json",
".": {
"import": { "default": "./dist/base64.js", "types": "./dist/base64.d.ts" },
"require": { "default": "./dist/base64.cjs", "types": "./dist/base64.d.cts" },
"browser": "./dist/base64.umd.js"
}
},
"license": "MIT"
}
"description": "Base64 and base64url to string or arraybuffer, and back. Node, Deno or browser.",
"author": "Hexagon <github.com/hexagon>",
"contributors": [
{
"name": "Niklas von Hertzen",
"email": "niklasvh@gmail.com",
"url": "https://hertzen.com"
}
],
"homepage": "https://base64.56k.guru",
"repository": {
"type": "git",
"url": "https://github.com/hexagon/base64"
},
"bugs": {
"url": "https://github.com/hexagon/base64/issues"
},
"files": [
"dist/*",
"SECURITY.md"
],
"keywords": [
"base64",
"base64url",
"parser",
"isomorphic",
"arraybuffer",
"string"
],
"type": "module",
"main": "./dist/base64.cjs",
"browser": "./dist/base64.umd.js",
"module": "./dist/base64.js",
"types": "./dist/base64.d.ts",
"exports": {
"./package.json": "./package.json",
".": {
"import": { "default": "./dist/base64.js", "types": "./dist/base64.d.ts" },
"require": { "default": "./dist/base64.cjs", "types": "./dist/base64.d.cts" },
"browser": "./dist/base64.umd.js"
}
},
"license": "MIT"
}
4 changes: 2 additions & 2 deletions deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
"exclude": ["dist", "build"]
},
"tasks": {
"build:prep": "deno cache --allow-scripts=npm:esbuild build/build.ts",
"build:prep": "deno install && deno cache --allow-scripts=npm:esbuild build/build.ts",
"build:clean": "deno run --allow-read --allow-write --allow-env build/build.ts -- clean",
"build:npm": "deno run --allow-read --allow-write --allow-env build/build.ts -- package",
"build:esbuild": "deno run --allow-read --allow-write --allow-env --allow-run build/build.ts -- build",
"build": "deno test && deno task build:prep && deno task build:clean && deno task build:esbuild && deno task build:npm",
"build": "deno task build:prep && deno test && deno task build:clean && deno task build:esbuild && deno task build:npm",
"check-deps": "deno run -A jsr:@check/deps"
},
"imports": {
Expand Down
46 changes: 45 additions & 1 deletion tests/base64.test.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
import { assertEquals } from "@std/assert";
import { test } from "@cross/test";

import { base64 } from "../src/base64.ts";
import { base64, fromString, toString, fromArrayBuffer, toArrayBuffer, validate } from "../src/base64.ts";

test("Encode 'Hello world'", function () {
const result = base64.fromString("Hello world");
assertEquals(result, "SGVsbG8gd29ybGQ=");
});

test("Encode 'Hello world' without going through namespace", function () {
const result = fromString("Hello world");
assertEquals(result, "SGVsbG8gd29ybGQ=");
});

test("Decode 'Hello world'", function () {
const result = base64.toString("SGVsbG8gd29ybGQ=");
assertEquals(result, "Hello world");
});

test("Decode 'Hello world' without going through namespace", function () {
const result = toString("SGVsbG8gd29ybGQ=");
assertEquals(result, "Hello world");
});

// Basic string base64url
test("Encode 'Hello world' in base64url mode", function () {
const result = base64.fromString("Hello world", true);
Expand Down Expand Up @@ -54,6 +64,13 @@ test("Encode array buffer", function () {
assertEquals(result, "AAECAwQFBgcI");
});

test("Encode array buffer without going through namespace", function () {
const result = fromArrayBuffer(
Uint8Array.from([0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8]).buffer,
);
assertEquals(result, "AAECAwQFBgcI");
});

test("Decode array buffer", function () {
const result = base64.toArrayBuffer("AAECAwQFBgcI");
assertEquals(
Expand All @@ -62,6 +79,15 @@ test("Decode array buffer", function () {
);
});

test("Decode array buffer without going through namespace", function () {
const result = toArrayBuffer("AAECAwQFBgcI");
assertEquals(
result,
Uint8Array.from([0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8]).buffer,
);
});


// Validate
test("AAECAwQFBgcI is valid base64 and base64url", function () {
const resultNormal = base64.validate("AAECAwQFBgcI"),
Expand All @@ -71,6 +97,14 @@ test("AAECAwQFBgcI is valid base64 and base64url", function () {
assertEquals(resultUrl, true);
});

test("AAECAwQFBgcI is valid base64 and base64url withouth going through namespace", function () {
const resultNormal = validate("AAECAwQFBgcI"),
resultUrl = base64.validate("AAECAwQFBgcI", true);

assertEquals(resultNormal, true);
assertEquals(resultUrl, true);
});

test("SGVsbG8gd29ybGQ= is valid base64 but not base64url", function () {
const resultNormal = base64.validate("SGVsbG8gd29ybGQ="),
resultUrl = base64.validate("SGVsbG8gd29ybGQ=", true);
Expand Down Expand Up @@ -101,3 +135,13 @@ test("c3ViamVjdHM_+X2Q9MQ is neither base64 nor base64url", function () {
assertEquals(resultNormal, false);
assertEquals(resultUrl, false);
});

test("Standard tests for base64", function () {
assertEquals(base64.fromString(""),"");
assertEquals(base64.fromString("f"),"Zg==");
assertEquals(base64.fromString("fo"),"Zm8=");
assertEquals(base64.fromString("foo"),"Zm9v");
assertEquals(base64.fromString("foob"),"Zm9vYg==");
assertEquals(base64.fromString("fooba"),"Zm9vYmE=");
assertEquals(base64.fromString("foobar"),"Zm9vYmFy");
});

0 comments on commit 2b58565

Please sign in to comment.