From 9d37ab8c1b1d0577d28c92ee6dcedddf84c03b4f Mon Sep 17 00:00:00 2001 From: azurepolarbear Date: Fri, 28 Jun 2024 19:47:46 -0500 Subject: [PATCH] #14 #53 #78 Add new tests to palette color selector unit tests. --- .../color/palette/palette-color-selector.ts | 11 +++--- .../palette/palette-color-selector.test.ts | 39 ++++++++++++++++++- 2 files changed, 43 insertions(+), 7 deletions(-) diff --git a/src/main/color/palette/palette-color-selector.ts b/src/main/color/palette/palette-color-selector.ts index 27bbb2c..0db2abf 100644 --- a/src/main/color/palette/palette-color-selector.ts +++ b/src/main/color/palette/palette-color-selector.ts @@ -51,25 +51,21 @@ export class PaletteColorSelector extends ColorSelector { } // TODO - documentation - // TODO - unit test public override get colorNames(): string[] { return this._COLOR_NAMES; } // TODO - documentation - // TODO - unit test public override get name(): string { return this._NAME; } // TODO - documentation - // TODO - unit test public override get type(): ColorSelectorType { return ColorSelectorType.PALETTE; } // TODO - documentation - // TODO - unit test public override getColor(): Color { return this.selectColorFromChoices(); } @@ -94,13 +90,14 @@ export class PaletteColorSelector extends ColorSelector { // TODO - documentation private choosePaletteColors(palette: Palette, buildWithPaletteOrder: boolean, colorCount: number): void { colorCount = p5.constrain(colorCount, PaletteColorSelector.MIN_COLOR_COUNT, palette.COLORS.length); + const colorNames: Set = new Set(); if (palette.COLORS.length > 0) { if (buildWithPaletteOrder) { for (let i: number = 0; i < colorCount; i++) { const pc: PaletteColor = palette.COLORS[i]; this.addColorChoice(new Color(pc)); - this._COLOR_NAMES.push(pc.NAME); + colorNames.add(pc.NAME); } } else { const selector: RandomSelector = new RandomSelector(palette.COLORS); @@ -110,10 +107,12 @@ export class PaletteColorSelector extends ColorSelector { if (pc) { this.addColorChoice(new Color(pc)); - this._COLOR_NAMES.push(pc.NAME); + colorNames.add(pc.NAME); } } } } + + this._COLOR_NAMES.push(...Array.from(colorNames)); } } diff --git a/src/test/color/palette/palette-color-selector.test.ts b/src/test/color/palette/palette-color-selector.test.ts index 6502bec..094ceee 100644 --- a/src/test/color/palette/palette-color-selector.test.ts +++ b/src/test/color/palette/palette-color-selector.test.ts @@ -25,6 +25,7 @@ import { checkForValidRandomSelector, getColorsArray } from 'unit-test/shared'; +import {ColorSelectorType} from "color"; const TEST_PALETTE_A: Palette = { NAME: 'test A palette', @@ -57,7 +58,7 @@ const TEST_PALETTE_B: Palette = { }; describe('palette color selector tests', (): void => { - test('test palette color selector names', (): void => { + test('test palette color selector get name', (): void => { const selectorA: PaletteColorSelector = new PaletteColorSelector(TEST_PALETTE_A); expect(selectorA).toBeTruthy(); expect(selectorA.name).toBe('test a palette color selector'); @@ -67,6 +68,42 @@ describe('palette color selector tests', (): void => { expect(selectorB.name).toBe('test b palette color selector'); }); + test('test palette color selector get color', (): void => { + const selectorA: PaletteColorSelector = new PaletteColorSelector(TEST_PALETTE_A); + expect(selectorA).toBeTruthy(); + + for (let i: number = 0; i < 10; i++) { + expect(selectorA.getColor()).toBeTruthy(); + } + + const selectorB: PaletteColorSelector = new PaletteColorSelector(TEST_PALETTE_B); + expect(selectorB).toBeTruthy(); + + for (let i: number = 0; i < 10; i++) { + expect(selectorB.getColor()).toBeTruthy(); + } + }); + + test('test palette color selector get type', (): void => { + const selectorA: PaletteColorSelector = new PaletteColorSelector(TEST_PALETTE_A); + expect(selectorA).toBeTruthy(); + expect(selectorA.type).toBe(ColorSelectorType.PALETTE); + + const selectorB: PaletteColorSelector = new PaletteColorSelector(TEST_PALETTE_B); + expect(selectorB).toBeTruthy(); + expect(selectorB.type).toBe(ColorSelectorType.PALETTE); + }); + + test('test palette color selector get color names', (): void => { + const selectorA: PaletteColorSelector = new PaletteColorSelector(TEST_PALETTE_A); + expect(selectorA).toBeTruthy(); + expect(selectorA.colorNames.length).toBeGreaterThan(0); + + const selectorB: PaletteColorSelector = new PaletteColorSelector(TEST_PALETTE_B); + expect(selectorB).toBeTruthy(); + expect(selectorB.colorNames.length).toBe(0); + }); + test('test palette color selector: palette only', (): void => { const selector: PaletteColorSelector = new PaletteColorSelector(TEST_PALETTE_A); expect(selector).toBeTruthy();