Comparison tests

This commit is contained in:
Bambus Control 2023-11-18 19:28:40 +01:00
parent 3af2581e28
commit 8fedd767f0
9 changed files with 151 additions and 4 deletions

2
.gitignore vendored
View file

@ -20,3 +20,5 @@
.DS_Store
/.ignore/
/coverage/

View file

@ -6,7 +6,8 @@
"scripts": {
"dev": "node esbuild.config.mjs",
"build": "tsc -noEmit -skipLibCheck && node esbuild.config.mjs production",
"export-schema": "typescript-json-schema --required --strictNullChecks --validationKeywords --ignoreErrors --refs --id https://raw.githubusercontent.com/BambusControl/obsidian-unicode-search/0.5.0-NEXT/resources/save-data-schema.json --out ./resources/save-data-schema.json tsconfig.json SaveData"
"export-schema": "typescript-json-schema --required --strictNullChecks --validationKeywords --ignoreErrors --refs --id https://raw.githubusercontent.com/BambusControl/obsidian-unicode-search/0.5.0-NEXT/resources/save-data-schema.json --out ./resources/save-data-schema.json tsconfig.json SaveData",
"test": "jest"
},
"keywords": [
"obsidian"

View file

@ -0,0 +1,31 @@
import {compareUsageTrackedCharacters} from "./compare-usage-tracked.characters";
test(
"later use is before sooner use",
() => {
expect(compareUsageTrackedCharacters(
{
lastUsed: 1,
useCount: 1,
},
{
lastUsed: 0,
useCount: 1,
}
)).toBe(-1)
}
)
test(
"one use is before zero uses",
() => {
expect(compareUsageTrackedCharacters(
{
useCount: 1,
},
{
useCount: 0,
}
)).toBe(-1)
}
)

View file

@ -4,7 +4,7 @@ import {inverse} from "../order/inverse";
import {compareNullable} from "./compare.nullable";
import {compareNumbers} from "./compare.numbers";
export function compareStatTrackedCharacters(left: UsageInfo, right: UsageInfo): Order {
export function compareUsageTrackedCharacters(left: UsageInfo, right: UsageInfo): Order {
// We want the most recently used to be first.
const lastUsedComparison = compareNullable(
left.lastUsed,

View file

@ -0,0 +1,55 @@
import {compareCharacters} from "./compare.characters";
test(
"character with name `a` is before character with name `b`",
() => {
expect(compareCharacters(
{
char: " ",
name: "a"
},
{
char: " ",
name: "b"
}
)).toBe(-1)
}
)
test(
"character with `use` is before character without",
() => {
expect(compareCharacters(
{
char: " ",
name: "b",
lastUsed: 1,
useCount: 1
},
{
char: " ",
name: "a",
}
)).toBe(-1)
}
)
test(
"characters with same `name` and `use` are equal",
() => {
expect(compareCharacters(
{
char: " ",
name: "name",
lastUsed: 1,
useCount: 1
},
{
char: " ",
name: "name",
lastUsed: 1,
useCount: 1
}
)).toBe(0)
}
)

View file

@ -1,7 +1,7 @@
import {Order} from "../order/order";
import {UsageInfo} from "../types/usage-info";
import {compareNullable} from "./compare.nullable";
import {compareStatTrackedCharacters} from "./compare-stat-tracked.characters";
import {compareUsageTrackedCharacters} from "./compare-usage-tracked.characters";
import {compareUnicodeNames} from "./compare-unicode.names";
import {Character} from "../types/character";
@ -9,7 +9,7 @@ export function compareCharacters(left: Character, right: Character): Order {
const order = compareNullable(
left as UsageInfo,
right as UsageInfo,
(l, r) => compareStatTrackedCharacters(l, r),
(l, r) => compareUsageTrackedCharacters(l, r),
);
if (order != Order.Equal) {

View file

@ -0,0 +1,24 @@
import {expect, test} from "@jest/globals";
import {compareNullable} from "./compare.nullable";
import {compareNumbers} from "./compare.numbers";
test(
"null equals null",
() => {
expect(compareNullable(null, null, compareNumbers)).toBe(0);
}
)
test(
"non-null is less than null",
() => {
expect(compareNullable(0, null, compareNumbers)).toBe(-1);
}
)
test(
"null is more than non-null",
() => {
expect(compareNullable(null, 0, compareNumbers)).toBe(1);
}
)

View file

@ -0,0 +1,23 @@
import {expect, test} from "@jest/globals";
import {compareNumbers} from "./compare.numbers";
test(
"one equals one",
() => {
expect(compareNumbers(1, 1)).toBe(0);
}
)
test(
"zero is less than one",
() => {
expect(compareNumbers(0, 1)).toBe(-1);
}
)
test(
"two is more than one",
() => {
expect(compareNumbers(2, 1)).toBe(1);
}
)

View file

@ -0,0 +1,11 @@
import {toHexadecimal} from "./hexadecimal.characters";
test(
"character `b` is `0062`",
() => {
expect(toHexadecimal({
"char": "b",
"name": "latin small letter b"
})).toBe("0062")
}
)