m-chrzan.xyz
aboutsummaryrefslogtreecommitdiff
path: root/__tests__
diff options
context:
space:
mode:
authorMarcin Chrzanowski <marcin.j.chrzanowski@gmail.com>2017-07-10 14:03:41 -0400
committerMarcin Chrzanowski <marcin.j.chrzanowski@gmail.com>2017-07-10 14:04:57 -0400
commitc09ab019efddfd6f072500bb8f9d862cca6b3a67 (patch)
tree421cbaf7bafbec8b2e4328b566b9debbd27a670b /__tests__
parentaeb4e619a60c5f39cf70d6e29aee0bb226c35a11 (diff)
Refactor basic die tests
Diffstat (limited to '__tests__')
-rw-r--r--__tests__/dice.test.js63
1 files changed, 34 insertions, 29 deletions
diff --git a/__tests__/dice.test.js b/__tests__/dice.test.js
index 9df156f..489750f 100644
--- a/__tests__/dice.test.js
+++ b/__tests__/dice.test.js
@@ -1,9 +1,9 @@
const { d } = require('../src/dice.js')
-const defaultNumberRolls = 100
+const defaultNumberRolls = 500
expect.extend({
- toBeOnAverage(received, expected, margin = 0.3) {
+ toBeOnAverage(received, expected, margin = 0.5) {
const average = received.reduce(plus) / received.length
const pass = average > expected - margin && average < expected + margin
@@ -40,7 +40,7 @@ expect.extend({
}
})
-const plus = (a, b) => (a + b)
+const plus = (a, b) => a + b
const rollForTest = (die, numberRolls) => {
let pools = []
@@ -55,7 +55,7 @@ const rollForTest = (die, numberRolls) => {
return { pools, rolls }
}
-const testDie = (die, numberRolls, testSpecs) => {
+const testDie = (die, testSpecs, numberRolls = defaultNumberRolls) => {
let { pools, rolls } = rollForTest(die, numberRolls)
if ('diceCount' in testSpecs) {
@@ -73,47 +73,52 @@ const testDie = (die, numberRolls, testSpecs) => {
}
if ('bounds' in testSpecs) {
- let { low, high } = testSpecs.bounds
+ let { low, high, expectExtrema } = testSpecs.bounds
it(`rolls between ${low} and ${high}`, () => {
expect(rolls).toBeBetween(low, high)
})
- it('attains its minimum', () => {
- expect(rolls).toContain(low)
- })
+ if (expectExtrema) {
+ it('attains its minimum', () => {
+ expect(rolls).toContain(low)
+ })
- it('attains its maximum', () => {
- expect(rolls).toContain(high)
- })
+ it('attains its maximum', () => {
+ expect(rolls).toContain(high)
+ })
+ }
+ }
+}
+
+const basicDieTestSpecs = (number, sides) => {
+ return {
+ diceCount: number,
+ average: {
+ average: (number * (sides + 1)) / 2
+ },
+ variance: {
+ variance: (number * (sides * sides - 1)) / 12
+ },
+ bounds: {
+ low: number,
+ high: number * sides,
+ expectExtrema: true
+ }
+ }
+}
}
}
const describeBasicDie = (number, sides, numberRolls = defaultNumberRolls) => {
describe(`${number}d${sides}`, () => {
const die = d(number, sides)
- const min = number
- const max = number * sides
-
- const testSpecs = {
- diceCount: number,
- average: {
- average: (min + max) / 2,
- },
- bounds: {
- low: min,
- high: max,
- expectLow: true,
- expectHigh: true
- }
- }
-
- testDie(die, numberRolls, testSpecs)
+ testDie(die, basicDieTestSpecs(number, sides), numberRolls)
})
}
describe('basic dice', () => {
describeBasicDie(1, 6)
- describeBasicDie(2, 8)
+ describeBasicDie(2, 8, 500)
describeBasicDie(20, 1)
})