Skip to content

Commit

Permalink
#14 #53 #78 Add new tests to palette color selector unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
azurepolarbear committed Jun 29, 2024
1 parent a3c11df commit 9d37ab8
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 7 deletions.
11 changes: 5 additions & 6 deletions src/main/color/palette/palette-color-selector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -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<string> = new Set<string>();

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<PaletteColor> = new RandomSelector<PaletteColor>(palette.COLORS);
Expand All @@ -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));
}
}
39 changes: 38 additions & 1 deletion src/test/color/palette/palette-color-selector.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
checkForValidRandomSelector,
getColorsArray
} from 'unit-test/shared';
import {ColorSelectorType} from "color";

const TEST_PALETTE_A: Palette = {
NAME: 'test A palette',
Expand Down Expand Up @@ -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');
Expand All @@ -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();
Expand Down

0 comments on commit 9d37ab8

Please sign in to comment.