Skip to content

Commit

Permalink
Cleanup hsluv tests
Browse files Browse the repository at this point in the history
Fix crash due to a missing file.

Simplify tests by only dealing with coords and not
creating PlainColorObjects.

Delete util.mjs and move the readTestData function to
the hsluv.js test module.
  • Loading branch information
lloydk committed Jan 5, 2025
1 parent 168b96b commit db0479d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 36 deletions.
14 changes: 7 additions & 7 deletions test/hsluv/hpluv.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import { to, sRGB, HPLuv } from "../../src/index-fn.js";
import { check } from "../util.mjs";
import { readTestData, normalizeCoords } from "./util.mjs";
import * as check from "../../node_modules/htest.dev/src/check.js";
import { readTestData } from "./hsluv.js";

let json = readTestData();
let srgbToHpluv = [];
let hpluvToSrgb = [];

Object.entries(json).forEach(([rgbHex, value]) => {
let coords = normalizeCoords(value.hpluv);
let coords = value.hpluv;
let rgb = value.rgb;
srgbToHpluv.push({ args: {space: sRGB, coords: rgb, alpha: 1}, expect: coords });
hpluvToSrgb.push({ args: {space: HPLuv, coords: coords, alpha: 1}, expect: rgb });
srgbToHpluv.push({ args: {space: sRGB, coords: rgb}, expect: coords });
hpluvToSrgb.push({ args: {space: HPLuv, coords: coords}, expect: rgb });
});

const tests = {
name: "HPLuv Conversion Tests",
description: "These tests compare sRGB values against the HSLuv reference implementation snapshot data.",
run (color, spaceId = this.data.toSpace) {
return to(color, spaceId).coords;
run (color, space = this.data.toSpace) {
return space.from(color.space, color.coords);
},
check: check.deep(function (actual, expect) {
if (expect === null || Number.isNaN(expect)) {
Expand Down
27 changes: 20 additions & 7 deletions test/hsluv/hsluv.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,36 @@
import { to, sRGB, HSLuv } from "../../src/index-fn.js";
import { check } from "../util.mjs";
import { readTestData, normalizeCoords } from "./util.mjs";
import { sRGB, HSLuv } from "../../src/index-fn.js";
import * as check from "../../node_modules/htest.dev/src/check.js";
import fs from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";

export function readTestData () {
try {
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const filePath = path.resolve(__dirname, "snapshot-rev4.json");
return JSON.parse(fs.readFileSync(filePath, "utf8"));
}
catch (err) {
console.error(err);
}
}

let json = readTestData();
let srgbToHsluv = [];
let hsluvToSrgb = [];

Object.entries(json).forEach(([rgbHex, value]) => {
let coords = normalizeCoords(value.hsluv);
let coords = value.hsluv;
let rgb = value.rgb;
srgbToHsluv.push({ args: {space: sRGB, coords: rgb, alpha: 1}, expect: coords });
hsluvToSrgb.push({ args: {space: HSLuv, coords: coords, alpha: 1}, expect: rgb });
srgbToHsluv.push({ args: {space: sRGB, coords: rgb}, expect: coords });
hsluvToSrgb.push({ args: {space: HSLuv, coords: coords}, expect: rgb });
});

const tests = {
name: "HSLuv Conversion Tests",
description: "These tests compare sRGB values against the HSLuv reference implementation snapshot data.",
run (color, space = this.data.toSpace) {
return space.from(color);
return space.from(color.space, color.coords);
},
check: check.deep(function (actual, expect) {
if (expect === null || Number.isNaN(expect)) {
Expand Down
22 changes: 0 additions & 22 deletions test/hsluv/util.mjs

This file was deleted.

0 comments on commit db0479d

Please sign in to comment.