Skip to content

Commit

Permalink
#14 #53 #86 Add unit tests for palettes and palette maps.
Browse files Browse the repository at this point in the history
  • Loading branch information
azurepolarbear committed May 26, 2024
1 parent 8708cd7 commit 18e3883
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 18 deletions.
1 change: 1 addition & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const config: JestConfigWithTsJest = {
'^map$': '<rootDir>/src/main/map',
'^math$': '<rootDir>/src/main/math',
'^palette$': '<rootDir>/src/main/color/palette',
'^palettes$': '<rootDir>/src/main/color/palette/palettes',
'^palette-colors$': '<rootDir>/src/main/color/palette/palette-colors',
'^random$': '<rootDir>/src/main/random',
'^unit-test/shared$': '<rootDir>/src/test/shared'
Expand Down
28 changes: 15 additions & 13 deletions src/test/color/color.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ import P5Lib from 'p5';

import {Color} from 'color';
import {SketchContext} from 'context';
import {_0437F2, _0FFF4F, _7A00F5, _FF6BB5} from 'palette-colors';

import {
checkForValidHexColorString, checkForValidHexColorStringWithAlpha,
ColorComponents,
checkForValidHexColorString,
checkForValidHexColorStringWithAlpha,
colorToColorComponents,
p5ColorToColorComponents
} from '../shared/color';
import {PaletteColor} from "palette";
import {_0437F2, _0FFF4F, _7A00F5, _FF6BB5} from "palette-colors";
} from 'unit-test/shared';

const p5: P5Lib = SketchContext.p5;

Expand Down Expand Up @@ -80,19 +80,21 @@ describe('color tests', (): void => {

test.each(
[
_0437F2,
_0FFF4F,
_7A00F5,
_FF6BB5
{hex: _0437F2.HEX, paletteColor: _0437F2},
{hex: _0FFF4F.HEX, paletteColor: _0FFF4F},
{hex: _7A00F5.HEX, paletteColor: _7A00F5},
{hex: _FF6BB5.HEX, paletteColor: _FF6BB5}
]
)('color built with PaletteColor object',
(c: PaletteColor): void => {
)('$# color built with PaletteColor object: $hex',
({hex, paletteColor}): void => {
expect(hex).toBeTruthy();
const expected: ColorComponents = {
r: c.RGB.R, g: c.RGB.G, b: c.RGB.B, a: 255
r: paletteColor.RGB.R, g: paletteColor.RGB.G, b: paletteColor.RGB.B, a: 255
}
const expectedName: string = c.NAME;

const color: Color = new Color(c);
const expectedName: string = paletteColor.NAME;

const color: Color = new Color(paletteColor);
expect(colorToColorComponents(color)).toEqual(expected);
expect(p5ColorToColorComponents(color.color)).toEqual(expected);
expect(color.name).toBe(expectedName);
Expand Down
21 changes: 18 additions & 3 deletions src/test/color/palette/palettes/miscellaneous/brittni.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,22 @@
* See the GNU Affero General Public License for more details.
*/

// import {BRITTNI} from 'palettes';
import {PaletteColor} from 'palette';
import {_0437F2, _0FFF4F, _121212, _7A00F5, _FF6BB5} from 'palette-colors';
import {BRITTNI} from 'palettes';

// TODO - brittni unit tests
// TODO - misc map unit tests
import {checkForValidPalette} from 'unit-test/shared';

describe('BRITTNI palette tests', (): void => {
const colors: PaletteColor[] = [
_121212,
_0437F2,
_0FFF4F,
_7A00F5,
_FF6BB5
]

test('BRITTNI palette is valid', (): void => {
checkForValidPalette(BRITTNI, colors);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* 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 {Palette} from 'palette';
import {BRITTNI, MISCELLANEOUS_PALETTES} from 'palettes';

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

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

test('valid map: MISCELLANEOUS_PALETTES', (): void => {
checkForValidStringMap(MISCELLANEOUS_PALETTES, ALL_MISC_PALETTES.length);
});

test.each(
ALL_MISC_PALETTES
)('$# palette successfully added to map: $name',
({name, palette}): void => {
expect(name).toBeTruthy();
checkForPaletteInMap(palette, MISCELLANEOUS_PALETTES);
}
);
});
6 changes: 5 additions & 1 deletion src/test/shared/map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import {StringMap} from 'map';

export function checkForValidStringMap<Type>(map: StringMap<Type>): void {
export function checkForValidStringMap<Type>(map: StringMap<Type>, expectedSize?: number): void {
expect(map).toBeTruthy();
expect(map.size).toBeGreaterThan(0);

Expand All @@ -28,4 +28,8 @@ export function checkForValidStringMap<Type>(map: StringMap<Type>): void {
expect(values.length).toBe(keys.length);

expect(keys.length).toBe(map.size);

if (expectedSize) {
expect(map.size).toBe(expectedSize);
}
}
24 changes: 23 additions & 1 deletion src/test/shared/palette.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,35 @@
* See the GNU Affero General Public License for more details.
*/

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

import {ColorComponents} from './color';
import {checkForValidStringMap} from './map';
import {checkNumberWithinAmount} from './math';

export function checkComponents(actual: ColorComponents, expected: PaletteColor): void {
checkNumberWithinAmount(actual.r, expected.RGB.R, 1);
checkNumberWithinAmount(actual.g, expected.RGB.G, 1);
checkNumberWithinAmount(actual.b, expected.RGB.B, 1);
}

export function checkForValidPalette(palette: Palette, expectedColors?: PaletteColor[]): void {
expect(palette).toBeTruthy();
expect(palette.NAME).toBeTruthy();
expect(palette.SOURCE).toBeTruthy();
expect(palette.COLORS).toBeTruthy();
expect(palette.COLORS.length).toBeGreaterThanOrEqual(2);

if (expectedColors) {
expect(palette.COLORS).toEqual(expectedColors);
}
}

export function checkForPaletteInMap(palette: Palette, map: StringMap<Palette>): void {
checkForValidStringMap(map);
checkForValidPalette(palette);
const actualPalette: Palette | undefined = map.get(palette.NAME);
expect(actualPalette).toBeTruthy();
expect(actualPalette).toEqual(palette);
}

0 comments on commit 18e3883

Please sign in to comment.