From d28bd019928918c8a60f3309b81658127465f356 Mon Sep 17 00:00:00 2001 From: Skillz4Killz <23035000+Skillz4Killz@users.noreply.github.com> Date: Wed, 11 May 2022 13:33:54 +0000 Subject: [PATCH] fix: add more utils unit tests --- tests/util/bigint.ts | 20 ------- tests/util/hash.ts | 30 ---------- tests/util/utils.ts | 10 +--- tests/util/validateLength.ts | 44 -------------- testss/delay.test.ts | 12 ++++ testss/utils.test.ts | 107 ++++++++++++++++++++++++++++++++++- 6 files changed, 119 insertions(+), 104 deletions(-) delete mode 100644 tests/util/bigint.ts delete mode 100644 tests/util/hash.ts delete mode 100644 tests/util/validateLength.ts create mode 100644 testss/delay.test.ts diff --git a/tests/util/bigint.ts b/tests/util/bigint.ts deleted file mode 100644 index f59613219..000000000 --- a/tests/util/bigint.ts +++ /dev/null @@ -1,20 +0,0 @@ -import { bigintToSnowflake, snowflakeToBigint } from "../../util/bigint.ts"; -import { assertEquals, assertNotEquals } from "../deps.ts"; - -Deno.test("[Local Test] - snowflakeToBigint ", () => { - const text = "130136895395987456"; - const big = 130136895395987456n; - const result = snowflakeToBigint(text); - - assertEquals(big, result); - assertNotEquals(text, result); -}); - -Deno.test("[Local Test] - bigIntToSnowflake", () => { - const text = "130136895395987456"; - const big = 130136895395987456n; - const result = bigintToSnowflake(big); - - assertEquals(text, result); - assertNotEquals(big, result); -}); diff --git a/tests/util/hash.ts b/tests/util/hash.ts deleted file mode 100644 index e7069a337..000000000 --- a/tests/util/hash.ts +++ /dev/null @@ -1,30 +0,0 @@ -import { iconBigintToHash, iconHashToBigInt } from "../../util/hash.ts"; -import { assertEquals } from "../deps.ts"; -const iconHash = "4bbb271a13f7195031adcc06a2d867ce"; -const iconBigInt = 3843769888406823508519992434416504301518n; -const a_iconHash = "a_4bbb271a13f7195031adcc06a2d867ce"; -const a_iconBigInt = 3503487521485885045056617826984736090062n; -Deno.test({ - name: "[utils] icon hash to bigint", - fn() { - assertEquals(iconHashToBigInt(iconHash), iconBigInt); - }, -}); -Deno.test({ - name: "[utils] icon bigint to hash", - fn() { - assertEquals(iconBigintToHash(iconBigInt), iconHash); - }, -}); -Deno.test({ - name: "[utils] icon hash to bigint a_ (animated)", - fn() { - assertEquals(iconHashToBigInt(a_iconHash), a_iconBigInt); - }, -}); -Deno.test({ - name: "[utils] icon bigint to hash a_ (animated)", - fn() { - assertEquals(iconBigintToHash(a_iconBigInt), a_iconHash); - }, -}); diff --git a/tests/util/utils.ts b/tests/util/utils.ts index 11f82a204..622e9f810 100644 --- a/tests/util/utils.ts +++ b/tests/util/utils.ts @@ -19,12 +19,4 @@ Deno.test({ // delay -Deno.test({ - name: "[utils] delay 2000 ms", - async fn() { - const before = Date.now(); - await delay(2000); - const after = Date.now(); - assertEquals(after - before >= 2000, true); - }, -}); + diff --git a/tests/util/validateLength.ts b/tests/util/validateLength.ts deleted file mode 100644 index 33bdf2e59..000000000 --- a/tests/util/validateLength.ts +++ /dev/null @@ -1,44 +0,0 @@ -import { validateLength } from "../../util/validateLength.ts"; -import { assertEquals } from "../deps.ts"; - -Deno.test({ - name: "[utils] Validate length is too low", - fn() { - assertEquals(validateLength("test", { min: 5 }), false); - }, -}); - -Deno.test({ - name: "[utils] Validate length is too high", - fn() { - assertEquals(validateLength("test", { max: 3 }), false); - }, -}); - -Deno.test({ - name: "[utils] Validate length is NOT just right in between.", - fn() { - assertEquals(validateLength("test", { min: 5, max: 3 }), false); - }, -}); - -Deno.test({ - name: "[utils] Validate length is NOT too low", - fn() { - assertEquals(validateLength("test", { min: 3 }), true); - }, -}); - -Deno.test({ - name: "[utils] Validate length is NOT too high", - fn() { - assertEquals(validateLength("test", { max: 5 }), true); - }, -}); - -Deno.test({ - name: "[utils] Validate length is just right in between.", - fn() { - assertEquals(validateLength("test", { min: 3, max: 6 }), true); - }, -}); diff --git a/testss/delay.test.ts b/testss/delay.test.ts new file mode 100644 index 000000000..4a37c8c4d --- /dev/null +++ b/testss/delay.test.ts @@ -0,0 +1,12 @@ +import { delay } from "../mod.ts"; +import { assertEquals } from "./deps.ts"; + +Deno.test({ + name: "[utils] delay 2000 ms", + async fn() { + const before = Date.now(); + await delay(2000); + const after = Date.now(); + assertEquals(after - before >= 2000, true); + }, +}); diff --git a/testss/utils.test.ts b/testss/utils.test.ts index e9878b8a7..87afcf46d 100644 --- a/testss/utils.test.ts +++ b/testss/utils.test.ts @@ -1,5 +1,7 @@ +import { formatImageURL, iconBigintToHash, iconHashToBigInt, validateLength } from "../mod.ts"; +import { bigintToSnowflake, snowflakeToBigint } from "../util/bigint.ts"; import { removeTokenPrefix } from "../util/token.ts"; -import { assertEquals } from "./deps.ts"; +import { assertEquals, assertNotEquals } from "./deps.ts"; Deno.test({ name: "[token] Remove token prefix when Bot is prefixed.", @@ -14,3 +16,106 @@ Deno.test({ assertEquals("discordeno is best lib", removeTokenPrefix("discordeno is best lib")); }, }); + +Deno.test("[bigint] - Transform a snowflake string to bigint", () => { + const text = "130136895395987456"; + const big = 130136895395987456n; + const result = snowflakeToBigint(text); + + assertEquals(big, result); + assertNotEquals(text, result); +}); + +Deno.test("[bigint] - Transform a bigint to a string", () => { + const text = "130136895395987456"; + const big = 130136895395987456n; + const result = bigintToSnowflake(big); + + assertEquals(text, result); + assertNotEquals(big, result); +}); + +Deno.test({ + name: "[utils] format image url", + fn() { + assertEquals(formatImageURL(`https://skillz.is.pro`), "https://skillz.is.pro.jpg?size=128"); + assertEquals(formatImageURL(`https://skillz.is.pro`, 1024), "https://skillz.is.pro.jpg?size=1024"); + assertEquals(formatImageURL(`https://skillz.is.pro`, 1024, "gif"), "https://skillz.is.pro.gif?size=1024"); + assertEquals(formatImageURL(`https://skillz.is.pro`, undefined, "gif"), "https://skillz.is.pro.gif?size=128"); + }, +}); + +const iconHash = "4bbb271a13f7195031adcc06a2d867ce"; +const iconBigInt = 3843769888406823508519992434416504301518n; +const a_iconHash = "a_4bbb271a13f7195031adcc06a2d867ce"; +const a_iconBigInt = 3503487521485885045056617826984736090062n; + +Deno.test({ + name: "[utils] icon hash to bigint", + fn() { + assertEquals(iconHashToBigInt(iconHash), iconBigInt); + }, +}); + +Deno.test({ + name: "[utils] icon bigint to hash", + fn() { + assertEquals(iconBigintToHash(iconBigInt), iconHash); + }, +}); + +Deno.test({ + name: "[utils] icon hash to bigint a_ (animated)", + fn() { + assertEquals(iconHashToBigInt(a_iconHash), a_iconBigInt); + }, +}); + +Deno.test({ + name: "[utils] icon bigint to hash a_ (animated)", + fn() { + assertEquals(iconBigintToHash(a_iconBigInt), a_iconHash); + }, +}); + +Deno.test({ + name: "[utils] Validate length is too low", + fn() { + assertEquals(validateLength("test", { min: 5 }), false); + }, +}); + +Deno.test({ + name: "[utils] Validate length is too high", + fn() { + assertEquals(validateLength("test", { max: 3 }), false); + }, +}); + +Deno.test({ + name: "[utils] Validate length is NOT just right in between.", + fn() { + assertEquals(validateLength("test", { min: 5, max: 3 }), false); + }, +}); + +Deno.test({ + name: "[utils] Validate length is NOT too low", + fn() { + assertEquals(validateLength("test", { min: 3 }), true); + }, +}); + +Deno.test({ + name: "[utils] Validate length is NOT too high", + fn() { + assertEquals(validateLength("test", { max: 5 }), true); + }, +}); + +Deno.test({ + name: "[utils] Validate length is just right in between.", + fn() { + assertEquals(validateLength("test", { min: 3, max: 6 }), true); + }, +});