From fde699298ca0093561ab8582890ec77713c2cf63 Mon Sep 17 00:00:00 2001 From: Stefan Seifert Date: Thu, 9 May 2024 13:55:10 +0200 Subject: [PATCH] add unit tests --- .../random/randomEnumMultiDifferentValue.ts | 8 +++- .../random/randomEnumDifferentValue.spec.ts | 2 +- .../randomEnumMultiDifferentValue.spec.ts | 38 +++++++++++++++++++ .../random/rollDiceDifferentValue.spec.ts | 2 +- .../rollDiceMultiDifferentValue.spec.ts | 14 +++++++ 5 files changed, 61 insertions(+), 3 deletions(-) create mode 100644 tests/unit/util/random/randomEnumMultiDifferentValue.spec.ts create mode 100644 tests/unit/util/random/rollDiceMultiDifferentValue.spec.ts diff --git a/src/util/random/randomEnumMultiDifferentValue.ts b/src/util/random/randomEnumMultiDifferentValue.ts index cd5f1f1..07fa470 100644 --- a/src/util/random/randomEnumMultiDifferentValue.ts +++ b/src/util/random/randomEnumMultiDifferentValue.ts @@ -1,3 +1,5 @@ +import randomEnumDifferentValue from "./randomEnumDifferentValue" + /** * Gets multiple random enum values from all available enums. * @param anEnum Enum type @@ -5,5 +7,9 @@ * @returns Random enum values */ export default function randomEnumMultiDifferentValue>(anEnum: T, count: number): T[keyof T][] { - return [] + const result : T[keyof T][] = [] + for (let i = 0; i < count; i++) { + result.push(randomEnumDifferentValue(anEnum, ...result)) + } + return result } diff --git a/tests/unit/util/random/randomEnumDifferentValue.spec.ts b/tests/unit/util/random/randomEnumDifferentValue.spec.ts index fef8427..71bdbae 100644 --- a/tests/unit/util/random/randomEnumDifferentValue.spec.ts +++ b/tests/unit/util/random/randomEnumDifferentValue.spec.ts @@ -1,7 +1,7 @@ import { expect } from 'chai' import randomEnumDifferentValue from '@/util/random/randomEnumDifferentValue' -describe('util/random/randomEnumDifferentValue.spec', () => { +describe('util/random/randomEnumDifferentValue', () => { it('int enum', () => { const value = randomEnumDifferentValue(IntEnum, IntEnum.TWO) diff --git a/tests/unit/util/random/randomEnumMultiDifferentValue.spec.ts b/tests/unit/util/random/randomEnumMultiDifferentValue.spec.ts new file mode 100644 index 0000000..f51745b --- /dev/null +++ b/tests/unit/util/random/randomEnumMultiDifferentValue.spec.ts @@ -0,0 +1,38 @@ +import { expect } from 'chai' +import randomEnumMultiDifferentValue from '@/util/random/randomEnumMultiDifferentValue' + +describe('util/random/randomEnumMultiDifferentValue', () => { + it('int enum', () => { + const values = randomEnumMultiDifferentValue(IntEnum, 2) + + expect(values.length).to.eq(2) + values.forEach((value,index) => { + expect([1,2,3,4].includes(value)).to.true + expect(values.toSpliced(index, 1).includes(value)).to.false + }) + }) + + it('string enum', () => { + const values = randomEnumMultiDifferentValue(StringEnum, 3) + + expect(values.length).to.eq(3) + values.forEach((value,index) => { + expect(['one','two','three','four'].includes(value)).to.true + expect(values.toSpliced(index, 1).includes(value)).to.false + }) + }) +}) + +enum IntEnum { + ONE = 1, + TWO = 2, + THREE = 3, + FOUR = 4 +} + +enum StringEnum { + ONE = 'one', + TWO = 'two', + THREE = 'three', + FOUR = 'four' +} diff --git a/tests/unit/util/random/rollDiceDifferentValue.spec.ts b/tests/unit/util/random/rollDiceDifferentValue.spec.ts index ce7e3e8..37a311a 100644 --- a/tests/unit/util/random/rollDiceDifferentValue.spec.ts +++ b/tests/unit/util/random/rollDiceDifferentValue.spec.ts @@ -1,7 +1,7 @@ import { expect } from 'chai' import rollDiceDifferentValue from '@/util/random/rollDiceDifferentValue' -describe('util/random/rollDice', () => { +describe('util/random/rollDiceDifferentValue', () => { it('D6', () => { for (let currentValue=1; currentValue<=6; currentValue++) { const value = rollDiceDifferentValue(6, currentValue) diff --git a/tests/unit/util/random/rollDiceMultiDifferentValue.spec.ts b/tests/unit/util/random/rollDiceMultiDifferentValue.spec.ts new file mode 100644 index 0000000..2e3d963 --- /dev/null +++ b/tests/unit/util/random/rollDiceMultiDifferentValue.spec.ts @@ -0,0 +1,14 @@ +import { expect } from 'chai' +import rollDiceMultiDifferentValue from '@/util/random/rollDiceMultiDifferentValue' + +describe('util/random/rollDiceMultiDifferentValue', () => { + it('D6', () => { + const values = rollDiceMultiDifferentValue(6, 3) + + expect(values.length).to.eq(3) + values.forEach((value,index) => { + expect([1,2,3,4,5,6].includes(value)).to.true + expect(values.toSpliced(index, 1).includes(value)).to.false + }) + }) +})