Skip to content

Commit

Permalink
#14 #88 Add unit tests for all palettes map and contrast map.
Browse files Browse the repository at this point in the history
  • Loading branch information
azurepolarbear committed Jun 2, 2024
1 parent ed960d0 commit 83746ee
Show file tree
Hide file tree
Showing 13 changed files with 173 additions and 17 deletions.
4 changes: 0 additions & 4 deletions src/main/color/palette/palettes/miscellaneous/brittni.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,6 @@ export const BRITTNI: Palette = {
_FF6BB5
],

// TODO - Verify '#000000' does not contain '#FFFFFF'
// TODO - verify '#FFFFFF' does not contain '#000000'
// TODO - verify only keys values are palette hexes, '#000000, and '#FFFFFF'
// TODO - Unit Test - Verify AA.NORMAL Pass for all pairs
CONTRAST_MAP: {
'#000000': ['#0FFF4F', '#FF6BB5'],
'#FFFFFF': ['#121212', '#0437F2', '#7A00F5'],
Expand Down
13 changes: 10 additions & 3 deletions src/test/color/palette/palette-colors/all-palette-colors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import {
checkComponents,
checkForValidStringMap,
p5ColorToColorComponents,
ColorComponents, checkForValidHexColorString
ColorComponents, checkForValidHexColorString, checkForEquivalentComponents
} from 'unit-test/shared';

const p5: P5Lib = SketchContext.p5;
Expand Down Expand Up @@ -73,8 +73,7 @@ function makeRGBKey(RGB: {R: number, G: number, B: number}): string {

describe('all palette colors', (): void => {
test('valid string map: ALL_PALETTE_COLORS', (): void => {
checkForValidStringMap(ALL_PALETTE_COLORS);
expect(ALL_PALETTE_COLORS.size).toBe(ALL_HEXES.length);
checkForValidStringMap(ALL_PALETTE_COLORS, ALL_HEXES.length);
});

test('all colors are unique', (): void => {
Expand Down Expand Up @@ -117,6 +116,10 @@ describe('all palette colors', (): void => {
const rgb: P5Lib.Color = p5.color(c.RGB.R, c.RGB.G, c.RGB.B);
const rgbComponents: ColorComponents = p5ColorToColorComponents(rgb);
checkComponents(rgbComponents, c);

checkForEquivalentComponents(hslComponents, hexComponents);
checkForEquivalentComponents(hslComponents, rgbComponents);
checkForEquivalentComponents(rgbComponents, hexComponents);
}
);

Expand All @@ -136,4 +139,8 @@ describe('all palette colors', (): void => {
expect(new Set<string>(ALL_PALETTE_COLORS.keys)).toContain(hexString);
}
);

test.todo('all color objects are in the map');

test.todo('all keys match palette color hex');
});
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,8 @@ describe('black palette colors', (): void => {
expect(new Set<string>(BLACK_PALETTE_COLORS.keys)).toContain(hexString);
}
);

test.todo('all color objects are in the map');

test.todo('all keys match palette color hex');
});
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,8 @@ describe('blue palette colors', (): void => {
expect(new Set<string>(BLUE_PALETTE_COLORS.keys)).toContain(hexString);
}
);

test.todo('all color objects are in the map');

test.todo('all keys match palette color hex');
});
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,8 @@ describe('green palette colors', (): void => {
expect(new Set<string>(GREEN_PALETTE_COLORS.keys)).toContain(hexString);
}
);

test.todo('all color objects are in the map');

test.todo('all keys match palette color hex');
});
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,8 @@ describe('pink palette colors', (): void => {
expect(new Set<string>(PINK_PALETTE_COLORS.keys)).toContain(hexString);
}
);

test.todo('all color objects are in the map');

test.todo('all keys match palette color hex');
});
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,8 @@ describe('purple palette colors', (): void => {
expect(new Set<string>(PURPLE_PALETTE_COLORS.keys)).toContain(hexString);
}
);

test.todo('all color objects are in the map');

test.todo('all keys match palette color hex');
});
79 changes: 79 additions & 0 deletions src/test/color/palette/palettes/all-palettes.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Copyright (C) 2024 brittni and the polar bear LLC.
*
* This file is a part of brittni and the polar bear's Generative Art Library,
* which is released under the GNU Affero General Public License, Version 3.0.
* You may not use this file except in compliance with the license.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. See LICENSE or go to
* https://www.gnu.org/licenses/agpl-3.0.en.html for full license details.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Affero General Public License for more details.
*/

import {StringMap} from 'map';
import {Palette, PaletteColor} from 'palette';
import {ALL_PALETTES, MISCELLANEOUS_PALETTES} from 'palettes';

import {
checkForPaletteInMap,
checkForPaletteNameKeyMatch,
checkForValidStringMap,
checkForValidContrastMap
} from 'unit-test/shared';

function getPaletteArray(map: StringMap<Palette>): {palette: Palette}[] {
const palettes: Palette[] = Array.from(map.values);
return palettes.map((p: Palette): {palette: Palette} => {
return {palette: p}
});
}

const EXPECTED_PALETTES: {palette: Palette}[] = [];
EXPECTED_PALETTES.push(
...(getPaletteArray(MISCELLANEOUS_PALETTES))
);

describe('all palettes tests', (): void => {
test('valid string map: ALL_PALETTES', (): void => {
checkForValidStringMap(ALL_PALETTES, EXPECTED_PALETTES.length);
});

test.each(
EXPECTED_PALETTES
)('$# palette successfully added to map: $palette.NAME',
({palette}): void => {
checkForPaletteInMap(palette, ALL_PALETTES);
}
);

test('all keys match associated palette name', (): void => {
checkForPaletteNameKeyMatch(ALL_PALETTES);
});

test.each(
EXPECTED_PALETTES
)('$# palette has valid contrast map: $palette.NAME',
({palette}): void => {
checkForValidContrastMap(palette);
}
);

test('all palettes are unique', (): void => {
const colorSets: Set<Set<string>> = new Set<Set<string>>();

for (const palette of ALL_PALETTES.values) {
const hexStrings: string[] = palette.COLORS.map((color: PaletteColor): string => {
return color.HEX;
});
const hexSet: Set<string> = new Set<string>(hexStrings);

expect(colorSets).not.toContainEqual(hexSet);
colorSets.add(hexSet);
}
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,26 @@
import {Palette} from 'palette';
import {BRITTNI, MISCELLANEOUS_PALETTES} from 'palettes';

import {checkForPaletteInMap, checkForValidStringMap} from 'unit-test/shared';
import {checkForPaletteInMap, checkForPaletteNameKeyMatch, checkForValidStringMap} from 'unit-test/shared';

describe('miscellaneous palettes map test', (): void => {
const ALL_MISC_PALETTES: {palette: Palette}[] = [
{palette: BRITTNI}
]
const EXPECTED_PALETTES: {palette: Palette}[] = [
{palette: BRITTNI}
]

test('valid map: MISCELLANEOUS_PALETTES', (): void => {
checkForValidStringMap(MISCELLANEOUS_PALETTES, ALL_MISC_PALETTES.length);
describe('miscellaneous palettes map test', (): void => {
test('valid string map: MISCELLANEOUS_PALETTES', (): void => {
checkForValidStringMap(MISCELLANEOUS_PALETTES, EXPECTED_PALETTES.length);
});

test.each(
ALL_MISC_PALETTES
EXPECTED_PALETTES
)('$# palette successfully added to map: $palette.NAME',
({palette}): void => {
checkForPaletteInMap(palette, MISCELLANEOUS_PALETTES);
}
);

test('all keys match associated palette name', (): void => {
checkForPaletteNameKeyMatch(MISCELLANEOUS_PALETTES);
});
});
7 changes: 7 additions & 0 deletions src/test/shared/color.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import P5Lib from 'p5';

import {Color, ColorSelector, ColorSelectorType} from 'color';
import {SketchContext} from 'context';
import {checkNumberWithinAmount} from "./math";

const p5: P5Lib = SketchContext.p5;

Expand Down Expand Up @@ -86,3 +87,9 @@ export function checkForValidHexColorStringWithAlpha(hex: string): void {
const isValid: boolean = hexWithAlphaPattern.test(hex);
expect(isValid).toBeTruthy();
}

export function checkForEquivalentComponents(actual: ColorComponents, expected: ColorComponents): void {
checkNumberWithinAmount(actual.r, expected.r, 1);
checkNumberWithinAmount(actual.g, expected.g, 1);
checkNumberWithinAmount(actual.b, expected.b, 1);
}
2 changes: 1 addition & 1 deletion src/test/shared/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ export * from './map';
export * from './math';
export * from './palette';
export * from './random';
export * from './palette-color';
export * from './palette-colors';
File renamed without changes.
45 changes: 44 additions & 1 deletion src/test/shared/palette.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@
import {StringMap} from 'map';
import {Palette, PaletteColor} from 'palette';

import {ColorComponents} from './color';
import {checkForValidHexColorString, ColorComponents} from './color';
import {checkForValidStringMap} from './map';
import {checkNumberWithinAmount} from './math';
import {ColorContrastAssessor, ContrastFontSize, ContrastStandard} from "color-contrast";

export function checkComponents(actual: ColorComponents, expected: PaletteColor): void {
checkNumberWithinAmount(actual.r, expected.RGB.R, 1);
Expand All @@ -47,3 +48,45 @@ export function checkForPaletteInMap(palette: Palette, map: StringMap<Palette>):
expect(actualPalette).toBeTruthy();
expect(actualPalette).toEqual(palette);
}

export function checkForPaletteNameKeyMatch(map: StringMap<Palette>): void {
for (const key of map.keys) {
const palette: Palette | undefined = map.get(key);
expect(palette).toBeTruthy();

if (palette) {
expect(palette.NAME).toBe(key);
}
}
}

export function checkForValidContrastMap(palette: Palette): void {
checkForValidPalette(palette);
expect(palette.CONTRAST_MAP['#000000']).not.toContain('#FFFFFF');
expect(palette.CONTRAST_MAP['#FFFFFF']).not.toContain('#000000');

const validHexes: string[] = palette.COLORS.map((color: PaletteColor): string => {
return color.HEX;
});
validHexes.push('#000000');
validHexes.push('#FFFFFF');

for (const key in palette.CONTRAST_MAP) {
checkForValidHexColorString(key);
expect(validHexes).toContain(key);

for (const hex of palette.CONTRAST_MAP[key]) {
checkForValidHexColorString(hex);
expect(validHexes).toContain(hex);

const meetsRatio: boolean =
ColorContrastAssessor.meetsContrastStandard(
key,
hex,
ContrastStandard.AA,
ContrastFontSize.NORMAL
);
expect(meetsRatio).toBeTruthy();
}
}
}

0 comments on commit 83746ee

Please sign in to comment.