From ba13007dce63d211eb6a168f502f58af01a6ca1f Mon Sep 17 00:00:00 2001 From: EuAndreh Date: Wed, 28 Feb 2024 11:30:33 -0300 Subject: src/utils.mjs: Define strSortFn() and use it on all files --- tests/js/accretion.mjs | 3 ++- tests/js/utils.mjs | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) (limited to 'tests') diff --git a/tests/js/accretion.mjs b/tests/js/accretion.mjs index 73c93c8..14c86d3 100644 --- a/tests/js/accretion.mjs +++ b/tests/js/accretion.mjs @@ -5,6 +5,7 @@ import sqlite from "../../src/sqlite.cjs"; import * as runner from "../runner.mjs"; import * as db from "../../src/db.mjs"; +import * as u from "../../src/utils.mjs"; import { MIGRATIONS_DIR, runMigrations, @@ -35,7 +36,7 @@ const test_runMigrations = t => { const migrationLogs = fs .readdirSync(MIGRATIONS_DIR) - .sort((a, b) => a.localeCompare(b, "POSIX")) + .sort(u.strSortFn) .map(filename => ({ log: "exec-migration", filename })); assert.deepEqual(contents, migrationLogs); }); diff --git a/tests/js/utils.mjs b/tests/js/utils.mjs index 004af95..178f6c8 100644 --- a/tests/js/utils.mjs +++ b/tests/js/utils.mjs @@ -10,6 +10,7 @@ import { first, promisify, partial, + strSortFn, } from "../../src/utils.mjs"; const test_eq = t => { @@ -344,6 +345,22 @@ const test_partial = t => { }); }; +const test_strSortFn = t => { + t.start("strSortFn()"); + + t.test("empty value", () => { + assert.equal(strSortFn("", ""), 0); + }); + + t.test("default sort", () => { + const arr = [ "a", "Z" ]; + assert.deepEqual( + [...arr].sort(strSortFn), + [...arr].sort().reverse(), + ); + }); +}; + await runner.runTests([ test_eq, @@ -354,4 +371,5 @@ await runner.runTests([ test_first, test_promisify, test_partial, + test_strSortFn, ]); -- cgit v1.2.3