Skip to content

Commit

Permalink
feat: fix: duplicate test output due to run triggered by function exp…
Browse files Browse the repository at this point in the history
…ort from another test
  • Loading branch information
mineejo committed Nov 23, 2023
1 parent 883f558 commit 3181a26
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 31 deletions.
35 changes: 15 additions & 20 deletions advanced_color_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,7 @@
import { assertEquals } from "./dev_deps.ts";
import { AdvancedColor } from "./advanced_color.ts";
import { Rgb } from "./color/mod.ts";
import { Color } from "./color.ts";

export function truncComponents(color: AdvancedColor | Color): Rgb {
const [_red, _green, _blue]: Rgb = color.components;
return [Math.trunc(_red), Math.trunc(_green), Math.trunc(_blue)];
}
import { getTruncComponents } from "./util.ts";

Deno.test("class contains correct fields", async (t) => {
const red = 31;
Expand All @@ -31,13 +26,13 @@ Deno.test("class contains correct fields", async (t) => {

await t.step("dependent field contains correct values", () => {
assertEquals(
truncComponents(color.rgbColor),
getTruncComponents(color.rgbColor),
[red, green, blue],
`"color.rgbColor"`,
);

assertEquals(
truncComponents(color),
getTruncComponents(color),
[red, green, blue],
`"color"`,
);
Expand All @@ -50,52 +45,52 @@ Deno.test("HSL conversion of red, green and blue correct", async (t) => {
const blue: Rgb = [0, 0, 255];

const hue: AdvancedColor = new AdvancedColor(0);
assertEquals(truncComponents(hue), red, `"hue"`);
assertEquals(getTruncComponents(hue), red, `"hue"`);

await t.step("no lightness or saturation values do not change hue", () => {
const hueSaturation: AdvancedColor = new AdvancedColor(0, 100);
assertEquals(truncComponents(hueSaturation), red, `"hueSaturation"`);
assertEquals(getTruncComponents(hueSaturation), red, `"hueSaturation"`);

const hueLightness: AdvancedColor = new AdvancedColor(0, undefined, 50);
assertEquals(truncComponents(hueLightness), red, `"hueLightness"`);
assertEquals(getTruncComponents(hueLightness), red, `"hueLightness"`);
});

await t.step("default values do not change hue", () => {
const hueSaturationLightness: AdvancedColor = new AdvancedColor(0, 100, 50);
assertEquals(
truncComponents(hueSaturationLightness),
getTruncComponents(hueSaturationLightness),
red,
`"hueSaturationLightness"`,
);
});

await t.step("hue range correct", () => {
const startRedColor: AdvancedColor = new AdvancedColor(0);
assertEquals(truncComponents(startRedColor), red, `"startRedColor"`);
assertEquals(getTruncComponents(startRedColor), red, `"startRedColor"`);

const greenColor: AdvancedColor = new AdvancedColor(33.33);
assertEquals(truncComponents(greenColor), green, `"greenColor"`);
assertEquals(getTruncComponents(greenColor), green, `"greenColor"`);

const blueColor: AdvancedColor = new AdvancedColor(66.66);
assertEquals(truncComponents(blueColor), blue, `"blueColor"`);
assertEquals(getTruncComponents(blueColor), blue, `"blueColor"`);

const endRedColor: AdvancedColor = new AdvancedColor(100);
assertEquals(truncComponents(endRedColor), red, `"endRedColor"`);
assertEquals(getTruncComponents(endRedColor), red, `"endRedColor"`);
});

await t.step("saturation range correct", () => {
const redSaturated: Rgb = [255, 0, 0];
const saturatedColor: AdvancedColor = new AdvancedColor(0, 100);
assertEquals(
truncComponents(saturatedColor),
getTruncComponents(saturatedColor),
redSaturated,
`"saturatedColor"`,
);

const redUnsaturated: Rgb = [63, 63, 63];
const unsaturatedColor: AdvancedColor = new AdvancedColor(0, 0);
assertEquals(
truncComponents(unsaturatedColor),
getTruncComponents(unsaturatedColor),
redUnsaturated,
`"unsaturatedColor"`,
);
Expand All @@ -105,15 +100,15 @@ Deno.test("HSL conversion of red, green and blue correct", async (t) => {
const black: Rgb = [0, 0, 0];
const blackColor: AdvancedColor = new AdvancedColor(0, undefined, 0);
assertEquals(
truncComponents(blackColor),
getTruncComponents(blackColor),
black,
`"blackColor"`,
);

const white: Rgb = [255, 255, 255];
const whiteColor: AdvancedColor = new AdvancedColor(0, undefined, 100);
assertEquals(
truncComponents(whiteColor),
getTruncComponents(whiteColor),
white,
`"whiteColor"`,
);
Expand Down
12 changes: 6 additions & 6 deletions color_effects/color_blind/mod_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { Color } from "../../color.ts";
import * as originalFilters from "./mod.ts";
import { assertEquals } from "../../dev_deps.ts";
import { truncComponents } from "../../advanced_color_test.ts";
import { getTruncComponents } from "../../util.ts";

// deno-lint-ignore no-explicit-any
const filters = originalFilters as any;
Expand Down Expand Up @@ -59,31 +59,31 @@ Deno.test("color blindness functions correct", async (t) => {

await t.step(`${func.name} function correct`, () => {
assertEquals(
truncComponents(func(red)),
getTruncComponents(func(red)),
functionValues?.[func.name]?.[0],
`"truncComponents(func(red))"`,
);

assertEquals(
truncComponents(func(green)),
getTruncComponents(func(green)),
functionValues?.[func.name]?.[1],
`"truncComponents(func(green))"`,
);

assertEquals(
truncComponents(func(blue)),
getTruncComponents(func(blue)),
functionValues?.[func.name]?.[2],
`"truncComponents(func(blue))"`,
);

assertEquals(
truncComponents(func(white)),
getTruncComponents(func(white)),
[255, 255, 255],
`"truncComponents(func(white))"`,
);

assertEquals(
truncComponents(func(black)),
getTruncComponents(func(black)),
[0, 0, 0],
`"truncComponents(func(black))"`,
);
Expand Down
10 changes: 5 additions & 5 deletions color_effects/mix_colors_test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Copyright 2023 mineejo. All rights reserved. MIT license.

import { assertEquals } from "../dev_deps.ts";
import { truncComponents } from "../advanced_color_test.ts";
import { Color } from "../color.ts";
import { mixColors } from "./mix_colors.ts";
import { getTruncComponents } from "../util.ts";

Deno.test("mix colors function correct", async (t) => {
const purple: Color = mixColors(
Expand All @@ -12,7 +12,7 @@ Deno.test("mix colors function correct", async (t) => {
50,
);

assertEquals(truncComponents(purple), [127, 0, 127], `"purple"`);
assertEquals(getTruncComponents(purple), [127, 0, 127], `"purple"`);

await t.step("percentage correct", () => {
const blue: Color = mixColors(
Expand All @@ -21,22 +21,22 @@ Deno.test("mix colors function correct", async (t) => {
100,
);

assertEquals(truncComponents(blue), [0, 0, 255], `"blue"`);
assertEquals(getTruncComponents(blue), [0, 0, 255], `"blue"`);

const red: Color = mixColors(
new Color(false, 255, 0, 0),
new Color(false, 0, 0, 255),
0,
);

assertEquals(truncComponents(red), [255, 0, 0], `"red"`);
assertEquals(getTruncComponents(red), [255, 0, 0], `"red"`);

const black: Color = mixColors(
new Color(false, 0, 0, 0),
new Color(false, 0, 0, 0),
50,
);

assertEquals(truncComponents(black), [0, 0, 0], `"black"`);
assertEquals(getTruncComponents(black), [0, 0, 0], `"black"`);
});
});
10 changes: 10 additions & 0 deletions util.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
// Copyright 2023 mineejo. All rights reserved. MIT license.

import { AdvancedColor } from "./advanced_color.ts";
import { Color } from "./color.ts";
import { Rgb } from "./color/rgb.ts";

export function percent(value: number): number {
return Math.min(Math.max(value, 0), 100);
}

export function getTruncComponents(color: AdvancedColor | Color): Rgb {
return color.components.map((component: number) =>
Math.trunc(component)
) as Rgb;
}

0 comments on commit 3181a26

Please sign in to comment.