From 14f32e812f79e029413ec82319696a556f6b91e0 Mon Sep 17 00:00:00 2001 From: bertieb Date: Thu, 11 Feb 2021 10:57:07 +0000 Subject: [PATCH] Add tests using Mocha+Chai --- test/test-dice.spec.ts | 97 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 test/test-dice.spec.ts diff --git a/test/test-dice.spec.ts b/test/test-dice.spec.ts new file mode 100644 index 0000000..39361ae --- /dev/null +++ b/test/test-dice.spec.ts @@ -0,0 +1,97 @@ +import { Asphodice } from "../dice"; +import { expect } from 'chai'; +import 'mocha'; + +let asphodice = new Asphodice; + +const testCandidates = [ + { dice: [ 6, 6, 6 ], aboveCutOff: true, belowCutOff: false, + reroll: false, cancelled: [6,6,6] }, + { dice: [ 2, 2, 2 ], aboveCutOff: false, belowCutOff: true, + reroll: false, cacncelled: [2,2,2] }, + { dice: [ 1 ], aboveCutOff: false, belowCutOff: true, + reroll: true, cacncelled: [1] }, + ]; + + + +describe("allAboveCutOff() says are all above cutoff correctly", function() { + const tests = [ + { dice: [ 6, 6, 6 ], expected: true }, + { dice: [ 2, 2, 2 ], expected: false }, + { dice: [ 1 ], expected: false }, + { dice: [ 10 ], expected: true }, + { dice: [ 6, 10, 6, 9 ], expected: true} + ]; + + tests.forEach(({dice, expected}) => { + it(`correctly determines ${dice} are above cutoff`, function () { + const result = asphodice.allAboveCutOff(dice); + expect(result).to.equal(expected); + }); + }); + testCandidates.forEach( ({dice, aboveCutOff}) => { + it(`allAboveCutOff() says ${dice} → ${aboveCutOff}`, function() { + const result = asphodice.allAboveCutOff(dice); + expect(result).to.equal(aboveCutOff); + }); + + }); +}); + +describe("Are all below cutoff?", function() { + const tests = [ + { dice: [ 6, 6, 6 ], expected: false }, + { dice: [ 2, 2, 2 ], expected: true }, + { dice: [ 1 ], expected: true }, + { dice: [ 10 ], expected: false }, + { dice: [ 6, 10, 6, 9 ], expected: false }, + { dice: [ 6, 1, 6, 9 ], expected: false } + ]; + + tests.forEach(({dice, expected}) => { + it(`correctly determines ${dice} are above cutoff`, function () { + const result = asphodice.allBelowCutOff(dice); + expect(result).to.equal(expected); + }); + }); +}); + +// TODO: single dice behaviour +describe("Does it need a re-roll?", function() { + const tests = [ + { dice: [ 6, 6, 6 ], expected: false }, + { dice: [ 2, 2, 2 ], expected: false }, + { dice: [ 6, 10, 6, 9 ], expected: false }, + { dice: [ 6, 1, 6, 9 ], expected: true }, + { dice: [ 10, 1, 2], expected: false }, + { dice: [ 10, 10, 1, 1, 1, 6], expected: true }, + { dice: [ 10, 10, 1, 1, 1, 2], expected: false }, + ]; + + tests.forEach(({dice, expected}) => { + it(`correctly determines ${dice} needs a re-roll`, function () { + const result = asphodice.rerollNeeded(dice); + expect(result).to.equal(expected); + }); + }); +}); + +describe("cancelRerollDice() filters correctly", function() { + const tests = [ + { dice: [ 6, 6, 6 ], out: [6,6,6] }, + { dice: [ 2, 2, 2 ], out: [2,2,2] }, + { dice: [ 6, 10, 6, 9 ], out: [6, 10, 6, 9] }, + { dice: [ 6, 1, 6, 9 ], out: [ 6, 1, 6, 9 ] }, + { dice: [ 10, 1, 2], out: [2] }, + { dice: [ 10, 10, 1, 1, 1, 6], out: [1,6] }, + { dice: [ 10, 10, 1, 1, 1, 2], out: [1,2] }, + ]; + + tests.forEach(({dice, out}) => { + it(`correctly filters ${dice} to ${out}`, function () { + const result = asphodice.cancelRerollDice(dice); + expect(result.sort()).to.deep.equal(out.sort()); + }); + }); +});