Skip to content

Commit

Permalink
add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanseifert committed May 9, 2024
1 parent be86d6a commit fde6992
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 3 deletions.
8 changes: 7 additions & 1 deletion src/util/random/randomEnumMultiDifferentValue.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import randomEnumDifferentValue from "./randomEnumDifferentValue"

/**
* Gets multiple random enum values from all available enums.
* @param anEnum Enum type
* @param count Number of random values to return
* @returns Random enum values
*/
export default function randomEnumMultiDifferentValue<T extends Record<string, unknown>>(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
}
2 changes: 1 addition & 1 deletion tests/unit/util/random/randomEnumDifferentValue.spec.ts
Original file line number Diff line number Diff line change
@@ -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)

Expand Down
38 changes: 38 additions & 0 deletions tests/unit/util/random/randomEnumMultiDifferentValue.spec.ts
Original file line number Diff line number Diff line change
@@ -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'
}
2 changes: 1 addition & 1 deletion tests/unit/util/random/rollDiceDifferentValue.spec.ts
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
14 changes: 14 additions & 0 deletions tests/unit/util/random/rollDiceMultiDifferentValue.spec.ts
Original file line number Diff line number Diff line change
@@ -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
})
})
})

0 comments on commit fde6992

Please sign in to comment.