From 98deee1856e5072ea999729fe74063459dde246a Mon Sep 17 00:00:00 2001 From: Johannes Loher Date: Sat, 13 Mar 2021 18:43:23 +0100 Subject: [PATCH] Add tests for evaluateCheck --- spec/rolls/check-evaluation.spec.ts | 252 +++++++++++++++------------ src/module/rolls/check-evaluation.ts | 12 +- 2 files changed, 142 insertions(+), 122 deletions(-) diff --git a/spec/rolls/check-evaluation.spec.ts b/spec/rolls/check-evaluation.spec.ts index 539a89fd..f240b1f9 100644 --- a/spec/rolls/check-evaluation.spec.ts +++ b/spec/rolls/check-evaluation.spec.ts @@ -1,241 +1,261 @@ -import { assignSubChecksToDice } from "../../src/module/rolls/check-evaluation"; +import evaluateCheck from "../../src/module/rolls/check-evaluation"; Object.defineProperty(globalThis, "game", { value: { i18n: { localize: (key: string) => key } } }); -describe("assignSubChecksToDice with no dice", () => { +describe("evaluateCheck with no dice", () => { it("should throw an error.", () => { - expect(() => assignSubChecksToDice([], 10)).toThrow("DS4.ErrorInvalidNumberOfDice"); + expect(() => evaluateCheck([], 10)).toThrow("DS4.ErrorInvalidNumberOfDice"); }); }); -describe("assignSubChecksToDice with more dice than required by the checkTargetNumber", () => { +describe("evaluateCheck with more dice than required by the checkTargetNumber", () => { it("should throw an error.", () => { - expect(() => assignSubChecksToDice([10, 10], 10)).toThrow("DS4.ErrorInvalidNumberOfDice"); + expect(() => evaluateCheck([10, 10], 10)).toThrow("DS4.ErrorInvalidNumberOfDice"); }); }); -describe("assignSubChecksToDice with less dice than required by the checkTargetNumber", () => { +describe("evaluateCheck with less dice than required by the checkTargetNumber", () => { it("should throw an error.", () => { - expect(() => assignSubChecksToDice([10], 21)).toThrow("DS4.ErrorInvalidNumberOfDice"); + expect(() => evaluateCheck([10], 21)).toThrow("DS4.ErrorInvalidNumberOfDice"); }); }); -describe("assignSubChecksToDice with a single die", () => { - it("should assign the checkTargetNumber to the single die.", () => { - expect(assignSubChecksToDice([4], 12)).toEqual([{ result: 4, checkTargetNumber: 12 }]); +describe("evaluateCheck with a single die", () => { + it("should assign the checkTargetNumber to the single die and be successful.", () => { + expect(evaluateCheck([4], 12)).toEqual([{ result: 4, checkTargetNumber: 12, active: true, discarded: false }]); }); - it("should assign the checkTargetNumber to the single die on upper edge case.", () => { - expect(assignSubChecksToDice([4], 4)).toEqual([{ result: 4, checkTargetNumber: 4 }]); + it("should assign the checkTargetNumber to the single die on upper edge case and be successful.", () => { + expect(evaluateCheck([4], 4)).toEqual([{ result: 4, checkTargetNumber: 4, active: true, discarded: false }]); }); - it("should assign the checkTargetNumber to the single die on lower edge case.", () => { - expect(assignSubChecksToDice([5], 4)).toEqual([{ result: 5, checkTargetNumber: 4 }]); + it("should assign the checkTargetNumber to the single die on lower edge case not be successful.", () => { + expect(evaluateCheck([5], 4)).toEqual([{ result: 5, checkTargetNumber: 4, active: false, discarded: true }]); }); - it("should assign the checkTargetNumber to the single die on upper edge case '19'", () => { - expect(assignSubChecksToDice([19], 4)).toEqual([{ result: 19, checkTargetNumber: 4 }]); + it("should assign the checkTargetNumber to the single die and not be successful on upper edge case '19'", () => { + expect(evaluateCheck([19], 4)).toEqual([{ result: 19, checkTargetNumber: 4, active: false, discarded: true }]); }); - it("should assign the checkTargetNumber to the single die on '1'", () => { - expect(assignSubChecksToDice([1], 4)).toEqual([{ result: 1, checkTargetNumber: 4 }]); + it("should assign the checkTargetNumber to the single die and coup on '1'", () => { + expect(evaluateCheck([1], 4)).toEqual([ + { result: 1, checkTargetNumber: 4, active: true, discarded: false, success: true, count: 4 }, + ]); }); - it("should assign the checkTargetNumber to the single die on '20'", () => { - expect(assignSubChecksToDice([20], 4)).toEqual([{ result: 20, checkTargetNumber: 4 }]); + it("should assign the checkTargetNumber to the single die and fumble on '20'", () => { + expect(evaluateCheck([20], 4)).toEqual([ + { result: 20, checkTargetNumber: 4, active: false, discarded: true, failure: true }, + ]); }); }); -describe("assignSubChecksToDice with a single die and coup modification", () => { - it("should assign the checkTargetNumber to the single die on 'maximumCoupResult'", () => { - expect(assignSubChecksToDice([2], 4, { maximumCoupResult: 2 })).toEqual([{ result: 2, checkTargetNumber: 4 }]); +describe("evaluateCheck with a single die and coup / fumble modification", () => { + it("should assign the checkTargetNumber to the single die and coup on 'maximumCoupResult'", () => { + expect(evaluateCheck([2], 4, { maximumCoupResult: 2 })).toEqual([ + { result: 2, checkTargetNumber: 4, active: true, discarded: false, success: true, count: 4 }, + ]); }); - it("should assign the checkTargetNumber to the single die on lower edge case '3'", () => { - expect(assignSubChecksToDice([3], 4, { maximumCoupResult: 2 })).toEqual([{ result: 3, checkTargetNumber: 4 }]); + it("should assign the checkTargetNumber to the single die and not coup on lower edge case '3'", () => { + expect(evaluateCheck([3], 4, { maximumCoupResult: 2 })).toEqual([ + { result: 3, checkTargetNumber: 4, active: true, discarded: false }, + ]); + }); + + it("should assign the checkTargetNumber to the single die and fumble on 'minimumFUmbleResultResult'", () => { + expect(evaluateCheck([19], 20, { minimumFumbleResult: 19 })).toEqual([ + { result: 19, checkTargetNumber: 20, active: true, discarded: false, failure: true }, + ]); + }); + + it("should assign the checkTargetNumber to the single die and not fumble on upper edge case '18'", () => { + expect(evaluateCheck([18], 20, { minimumFumbleResult: 19 })).toEqual([ + { result: 18, checkTargetNumber: 20, active: true, discarded: false }, + ]); }); }); -describe("assignSubChecksToDice with multiple dice", () => { +describe("evaluateCheck with multiple dice", () => { it("should assign the checkTargetNumber for the last sub check to the lowest non coup, even if the first is '20'.", () => { - expect(assignSubChecksToDice([20, 6, 15], 48)).toEqual([ - { result: 20, checkTargetNumber: 20 }, - { result: 6, checkTargetNumber: 8 }, - { result: 15, checkTargetNumber: 20 }, + expect(evaluateCheck([20, 6, 15], 48)).toEqual([ + { result: 20, checkTargetNumber: 20, active: true, discarded: false, failure: true }, + { result: 6, checkTargetNumber: 8, active: true, discarded: false }, + { result: 15, checkTargetNumber: 20, active: true, discarded: false }, ]); }); it("should assign the checkTargetNumber for the last sub check to the first coup if there are only coups.", () => { - expect(assignSubChecksToDice([1, 1, 1], 48)).toEqual([ - { result: 1, checkTargetNumber: 8 }, - { result: 1, checkTargetNumber: 20 }, - { result: 1, checkTargetNumber: 20 }, + expect(evaluateCheck([1, 1, 1], 48)).toEqual([ + { result: 1, checkTargetNumber: 8, active: true, discarded: false, success: true, count: 8 }, + { result: 1, checkTargetNumber: 20, active: true, discarded: false, success: true, count: 20 }, + { result: 1, checkTargetNumber: 20, active: true, discarded: false, success: true, count: 20 }, ]); }); it("should assign the checkTargetNumber for the last sub check to the first lowest die, even if it is higher than that value.", () => { - expect(assignSubChecksToDice([15, 15, 15], 48)).toEqual([ - { result: 15, checkTargetNumber: 8 }, - { result: 15, checkTargetNumber: 20 }, - { result: 15, checkTargetNumber: 20 }, + expect(evaluateCheck([15, 15, 15], 48)).toEqual([ + { result: 15, checkTargetNumber: 8, active: false, discarded: true }, + { result: 15, checkTargetNumber: 20, active: true, discarded: false }, + { result: 15, checkTargetNumber: 20, active: true, discarded: false }, ]); }); it("should assign the checkTargetNumber for the last sub check to the first coup if its sum with the lowest non coup is high enough.", () => { - expect(assignSubChecksToDice([15, 15, 1], 48)).toEqual([ - { result: 15, checkTargetNumber: 20 }, - { result: 15, checkTargetNumber: 20 }, - { result: 1, checkTargetNumber: 8 }, + expect(evaluateCheck([15, 15, 1], 48)).toEqual([ + { result: 15, checkTargetNumber: 20, active: true, discarded: false }, + { result: 15, checkTargetNumber: 20, active: true, discarded: false }, + { result: 1, checkTargetNumber: 8, active: true, discarded: false, success: true, count: 8 }, ]); }); it("should assign the checkTargetNumber for the last sub check to the first coup if its sum with the lowest non coup is high enough, even if the last die is '20'.", () => { - expect(assignSubChecksToDice([15, 1, 20], 48)).toEqual([ - { result: 15, checkTargetNumber: 20 }, - { result: 1, checkTargetNumber: 8 }, - { result: 20, checkTargetNumber: 20 }, + expect(evaluateCheck([15, 1, 20], 48)).toEqual([ + { result: 15, checkTargetNumber: 20, active: true, discarded: false }, + { result: 1, checkTargetNumber: 8, active: true, discarded: false, success: true, count: 8 }, + { result: 20, checkTargetNumber: 20, active: true, discarded: false }, ]); }); it("should assign the checkTargetNumber for the last sub check to properly maximize the result when all dice are successes.", () => { - expect(assignSubChecksToDice([15, 4, 12], 46)).toEqual([ - { result: 15, checkTargetNumber: 20 }, - { result: 4, checkTargetNumber: 6 }, - { result: 12, checkTargetNumber: 20 }, + expect(evaluateCheck([15, 4, 12], 46)).toEqual([ + { result: 15, checkTargetNumber: 20, active: true, discarded: false }, + { result: 4, checkTargetNumber: 6, active: true, discarded: false }, + { result: 12, checkTargetNumber: 20, active: true, discarded: false }, ]); }); it("should assign the checkTargetNumber for the last sub check to properly maximize the result when one dice is a failure.", () => { - expect(assignSubChecksToDice([15, 8, 12], 46)).toEqual([ - { result: 15, checkTargetNumber: 20 }, - { result: 8, checkTargetNumber: 6 }, - { result: 12, checkTargetNumber: 20 }, + expect(evaluateCheck([15, 8, 12], 46)).toEqual([ + { result: 15, checkTargetNumber: 20, active: true, discarded: false }, + { result: 8, checkTargetNumber: 6, active: false, discarded: true }, + { result: 12, checkTargetNumber: 20, active: true, discarded: false }, ]); }); it("should assign the checkTargetNumber for the last sub check to properly maximize the result on 'lowest dice higher than last check target number and coups thrown'-edge case, coup not used for last sub CTN.", () => { - expect(assignSubChecksToDice([15, 1, 8], 46)).toEqual([ - { result: 15, checkTargetNumber: 20 }, - { result: 1, checkTargetNumber: 20 }, - { result: 8, checkTargetNumber: 6 }, + expect(evaluateCheck([15, 1, 8], 46)).toEqual([ + { result: 15, checkTargetNumber: 20, active: true, discarded: false }, + { result: 1, checkTargetNumber: 20, active: true, discarded: false, success: true, count: 20 }, + { result: 8, checkTargetNumber: 6, active: false, discarded: true }, ]); }); it("should assign the checkTargetNumber for the last sub check to properly maximize the result on 2-dice 'lowest dice higher than last check target number and coups thrown'-edge case, coup not used for last sub CTN.", () => { - expect(assignSubChecksToDice([1, 8], 24)).toEqual([ - { result: 1, checkTargetNumber: 20 }, - { result: 8, checkTargetNumber: 4 }, + expect(evaluateCheck([1, 8], 24)).toEqual([ + { result: 1, checkTargetNumber: 20, active: true, discarded: false, success: true, count: 20 }, + { result: 8, checkTargetNumber: 4, active: false, discarded: true }, ]); }); it("should assign the checkTargetNumber for the last sub check to properly maximize the result on 2-dice 'lowest dice higher than last check target number and coups thrown'-edge case, coup used for last sub CTN.", () => { - expect(assignSubChecksToDice([1, 19], 38)).toEqual([ - { result: 1, checkTargetNumber: 18 }, - { result: 19, checkTargetNumber: 20 }, + expect(evaluateCheck([1, 19], 38)).toEqual([ + { result: 1, checkTargetNumber: 18, active: true, discarded: false, success: true, count: 18 }, + { result: 19, checkTargetNumber: 20, active: true, discarded: false }, ]); }); it("should assign the checkTargetNumber for the last sub check to properly maximize the result when there is more than one coup and a coup is used for the last sub CTN", () => { - expect(assignSubChecksToDice([1, 1, 15], 48)).toEqual([ - { result: 1, checkTargetNumber: 8 }, - { result: 1, checkTargetNumber: 20 }, - { result: 15, checkTargetNumber: 20 }, + expect(evaluateCheck([1, 1, 15], 48)).toEqual([ + { result: 1, checkTargetNumber: 8, active: true, discarded: false, success: true, count: 8 }, + { result: 1, checkTargetNumber: 20, active: true, discarded: false, success: true, count: 20 }, + { result: 15, checkTargetNumber: 20, active: true, discarded: false }, ]); }); }); -describe("assignSubChecksToDice with multiple dice and coup modification", () => { - it("should assign the checkTargetNumber for the last sub check to the lowest non coup, even if the first is '19'.", () => { - expect(assignSubChecksToDice([19, 15, 6], 48, { maximumCoupResult: 2 })).toEqual([ - { result: 19, checkTargetNumber: 20 }, - { result: 15, checkTargetNumber: 20 }, - { result: 6, checkTargetNumber: 8 }, +describe("evaluateCheck with multiple dice and coup / fumble modification", () => { + it("should assign the checkTargetNumber for the last sub check to the lowest non coup and fumble if the first is '19'.", () => { + expect(evaluateCheck([19, 15, 6], 48, { maximumCoupResult: 2, minimumFumbleResult: 19 })).toEqual([ + { result: 19, checkTargetNumber: 20, active: true, discarded: false, failure: true }, + { result: 15, checkTargetNumber: 20, active: true, discarded: false }, + { result: 6, checkTargetNumber: 8, active: true, discarded: false }, ]); }); it("should assign the checkTargetNumber for the last sub check to the first coup if there are only coups ('1' and '2').", () => { - expect(assignSubChecksToDice([2, 1, 2], 48, { maximumCoupResult: 2 })).toEqual([ - { result: 2, checkTargetNumber: 8 }, - { result: 1, checkTargetNumber: 20 }, - { result: 2, checkTargetNumber: 20 }, + expect(evaluateCheck([2, 1, 2], 48, { maximumCoupResult: 2, minimumFumbleResult: 19 })).toEqual([ + { result: 2, checkTargetNumber: 8, active: true, discarded: false, success: true, count: 8 }, + { result: 1, checkTargetNumber: 20, active: true, discarded: false, success: true, count: 20 }, + { result: 2, checkTargetNumber: 20, active: true, discarded: false, success: true, count: 20 }, ]); }); it("should assign the checkTargetNumber for the last sub check to the first lowest die, even if it is higher than that value.", () => { - expect(assignSubChecksToDice([15, 15, 15], 48, { maximumCoupResult: 2 })).toEqual([ - { result: 15, checkTargetNumber: 8 }, - { result: 15, checkTargetNumber: 20 }, - { result: 15, checkTargetNumber: 20 }, + expect(evaluateCheck([15, 15, 15], 48, { maximumCoupResult: 2, minimumFumbleResult: 19 })).toEqual([ + { result: 15, checkTargetNumber: 8, active: false, discarded: true }, + { result: 15, checkTargetNumber: 20, active: true, discarded: false }, + { result: 15, checkTargetNumber: 20, active: true, discarded: false }, ]); }); it("should assign the checkTargetNumber for the last sub check to the first coup ('2') if its sum with the lowest non coup is high enough.", () => { - expect(assignSubChecksToDice([15, 15, 2], 48, { maximumCoupResult: 2 })).toEqual([ - { result: 15, checkTargetNumber: 20 }, - { result: 15, checkTargetNumber: 20 }, - { result: 2, checkTargetNumber: 8 }, + expect(evaluateCheck([15, 15, 2], 48, { maximumCoupResult: 2, minimumFumbleResult: 19 })).toEqual([ + { result: 15, checkTargetNumber: 20, active: true, discarded: false }, + { result: 15, checkTargetNumber: 20, active: true, discarded: false }, + { result: 2, checkTargetNumber: 8, active: true, discarded: false, success: true, count: 8 }, ]); }); it("should assign the checkTargetNumber for the last sub check to the first coup ('2') if its sum with the lowest non coup is high enough, even if the last die is '20'.", () => { - expect(assignSubChecksToDice([15, 2, 20], 48, { maximumCoupResult: 2 })).toEqual([ - { result: 15, checkTargetNumber: 20 }, - { result: 2, checkTargetNumber: 8 }, - { result: 20, checkTargetNumber: 20 }, + expect(evaluateCheck([15, 2, 20], 48, { maximumCoupResult: 2, minimumFumbleResult: 19 })).toEqual([ + { result: 15, checkTargetNumber: 20, active: true, discarded: false }, + { result: 2, checkTargetNumber: 8, active: true, discarded: false, success: true, count: 8 }, + { result: 20, checkTargetNumber: 20, active: true, discarded: false }, ]); }); it("should assign the checkTargetNumber for the last sub check to the first coup ('2') if its sum with the lowest non coup is high enough, even if the last die is '19'.", () => { - expect(assignSubChecksToDice([15, 2, 19], 48, { maximumCoupResult: 2 })).toEqual([ - { result: 15, checkTargetNumber: 20 }, - { result: 2, checkTargetNumber: 8 }, - { result: 19, checkTargetNumber: 20 }, + expect(evaluateCheck([15, 2, 19], 48, { maximumCoupResult: 2, minimumFumbleResult: 19 })).toEqual([ + { result: 15, checkTargetNumber: 20, active: true, discarded: false }, + { result: 2, checkTargetNumber: 8, active: true, discarded: false, success: true, count: 8 }, + { result: 19, checkTargetNumber: 20, active: true, discarded: false }, ]); }); it("should assign the checkTargetNumber for the last sub check to properly maximize the result when all dice are successes.", () => { - expect(assignSubChecksToDice([15, 4, 12], 46, { maximumCoupResult: 2 })).toEqual([ - { result: 15, checkTargetNumber: 20 }, - { result: 4, checkTargetNumber: 6 }, - { result: 12, checkTargetNumber: 20 }, + expect(evaluateCheck([15, 4, 12], 46, { maximumCoupResult: 2, minimumFumbleResult: 19 })).toEqual([ + { result: 15, checkTargetNumber: 20, active: true, discarded: false }, + { result: 4, checkTargetNumber: 6, active: true, discarded: false }, + { result: 12, checkTargetNumber: 20, active: true, discarded: false }, ]); }); it("should assign the checkTargetNumber for the last sub check to properly maximize the result when one dice is a failure.", () => { - expect(assignSubChecksToDice([15, 8, 12], 46, { maximumCoupResult: 2 })).toEqual([ - { result: 15, checkTargetNumber: 20 }, - { result: 8, checkTargetNumber: 6 }, - { result: 12, checkTargetNumber: 20 }, + expect(evaluateCheck([15, 8, 12], 46, { maximumCoupResult: 2, minimumFumbleResult: 19 })).toEqual([ + { result: 15, checkTargetNumber: 20, active: true, discarded: false }, + { result: 8, checkTargetNumber: 6, active: false, discarded: true }, + { result: 12, checkTargetNumber: 20, active: true, discarded: false }, ]); }); it("should assign the checkTargetNumber for the last sub check to properly maximize the result on 'lowest dice higher than last check target number and coups ('2') thrown'-edge case, coup not used for last sub CTN.", () => { - expect(assignSubChecksToDice([15, 2, 8], 46, { maximumCoupResult: 2 })).toEqual([ - { result: 15, checkTargetNumber: 20 }, - { result: 2, checkTargetNumber: 20 }, - { result: 8, checkTargetNumber: 6 }, + expect(evaluateCheck([15, 2, 8], 46, { maximumCoupResult: 2 })).toEqual([ + { result: 15, checkTargetNumber: 20, active: true, discarded: false }, + { result: 2, checkTargetNumber: 20, active: true, discarded: false, success: true, count: 20 }, + { result: 8, checkTargetNumber: 6, active: false, discarded: true }, ]); }); it("should assign the checkTargetNumber for the last sub check to properly maximize the result on 2-dice 'lowest dice higher than last check target number and coups ('2') thrown'-edge case, coup not used for last sub CTN.", () => { - expect(assignSubChecksToDice([2, 8], 24, { maximumCoupResult: 2 })).toEqual([ - { result: 2, checkTargetNumber: 20 }, - { result: 8, checkTargetNumber: 4 }, + expect(evaluateCheck([2, 8], 24, { maximumCoupResult: 2 })).toEqual([ + { result: 2, checkTargetNumber: 20, active: true, discarded: false, success: true, count: 20 }, + { result: 8, checkTargetNumber: 4, active: false, discarded: true }, ]); }); it("should assign the checkTargetNumber for the last sub check to properly maximize the result on 2-dice 'lowest dice higher than last check target number and coups ('2') thrown'-edge case, coup used for last sub CTN.", () => { - expect(assignSubChecksToDice([2, 19], 38, { maximumCoupResult: 2 })).toEqual([ - { result: 2, checkTargetNumber: 18 }, - { result: 19, checkTargetNumber: 20 }, + expect(evaluateCheck([2, 19], 38, { maximumCoupResult: 2 })).toEqual([ + { result: 2, checkTargetNumber: 18, active: true, discarded: false, success: true, count: 18 }, + { result: 19, checkTargetNumber: 20, active: true, discarded: false }, ]); }); it("should assign the checkTargetNumber for the last sub check to properly maximize the result when there is more than one coup ('1' and '2') and a coup is used for the last sub CTN", () => { - expect(assignSubChecksToDice([1, 2, 15], 48, { maximumCoupResult: 2 })).toEqual([ - { result: 1, checkTargetNumber: 8 }, - { result: 2, checkTargetNumber: 20 }, - { result: 15, checkTargetNumber: 20 }, + expect(evaluateCheck([1, 2, 15], 48, { maximumCoupResult: 2 })).toEqual([ + { result: 1, checkTargetNumber: 8, active: true, discarded: false, success: true, count: 8 }, + { result: 2, checkTargetNumber: 20, active: true, discarded: false, success: true, count: 20 }, + { result: 15, checkTargetNumber: 20, active: true, discarded: false }, ]); }); }); diff --git a/src/module/rolls/check-evaluation.ts b/src/module/rolls/check-evaluation.ts index dcc0d5cb..df3f427e 100644 --- a/src/module/rolls/check-evaluation.ts +++ b/src/module/rolls/check-evaluation.ts @@ -17,7 +17,12 @@ export default function evaluateCheck( }); } -export function assignSubChecksToDice( +interface DieWithSubCheck { + result: number; + checkTargetNumber: number; +} + +function assignSubChecksToDice( dice: number[], checkTargetNumber: number, { @@ -76,11 +81,6 @@ function shouldUseCoupForLastSubCheck( ); } -interface DieWithSubCheck { - result: number; - checkTargetNumber: number; -} - interface DS4SubCheckResult extends DieWithSubCheck, DiceTerm.Result { success?: boolean; failure?: boolean;