m-chrzan.xyz
aboutsummaryrefslogtreecommitdiff
path: root/__tests__
diff options
context:
space:
mode:
Diffstat (limited to '__tests__')
-rw-r--r--__tests__/dice.test.js21
1 files changed, 21 insertions, 0 deletions
diff --git a/__tests__/dice.test.js b/__tests__/dice.test.js
index 489750f..45c859b 100644
--- a/__tests__/dice.test.js
+++ b/__tests__/dice.test.js
@@ -37,6 +37,19 @@ expect.extend({
`expected all to${pass ? ' not ' : ' '}be equal to ${expected}`
)
}
+ },
+ toHaveVariance(received, expected, margin = 0.5) {
+ const average = received.reduce(plus) / received.length
+ const variance = received.map((value) => (Math.pow(value - average, 2)))
+ .reduce(plus) / (received.length - 1)
+ const pass = variance > expected - margin && variance < expected + margin
+
+ return {
+ pass,
+ message: () => (
+ `expected variance to${pass ? ' not ' : ' '}be around ${expected} (margin: ${margin}), got ${variance}`
+ )
+ }
}
})
@@ -72,6 +85,14 @@ const testDie = (die, testSpecs, numberRolls = defaultNumberRolls) => {
})
}
+ if ('variance' in testSpecs) {
+ let { variance, margin } = testSpecs.variance
+
+ it(`has a variance of ${variance}`, () => {
+ expect(rolls).toHaveVariance(variance, margin)
+ })
+ }
+
if ('bounds' in testSpecs) {
let { low, high, expectExtrema } = testSpecs.bounds