From 8ca93fc9c9505c7c29298526d60cdab97958138f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20R=C3=BCmpelein?= Date: Sat, 2 Jan 2021 13:06:32 +0100 Subject: [PATCH] Ease dice mocking. --- spec/support/ds4rolls.spec.ts | 112 +++++++++++----------------------- 1 file changed, 37 insertions(+), 75 deletions(-) diff --git a/spec/support/ds4rolls.spec.ts b/spec/support/ds4rolls.spec.ts index f56fa46c..054f6818 100644 --- a/spec/support/ds4rolls.spec.ts +++ b/spec/support/ds4rolls.spec.ts @@ -9,11 +9,21 @@ import { RollProvider } from "../../src/module/rolls/roll-provider"; import "jasmine"; +function mockSingleThrow(value: number): RollProvider { + const rollProvider = jasmine.createSpyObj("rollProvider", ["getNextRoll"]); + rollProvider.getNextRoll = jasmine.createSpy("getNextRoll").and.returnValue(value); + return rollProvider; +} + +function mockMultipleThrows(values: Array): RollProvider { + const rollProvider = jasmine.createSpyObj("rollProvider", ["getNextRolls"]); + rollProvider.getNextRolls = jasmine.createSpy("getNextRolls").and.returnValue(values); + return rollProvider; +} + describe("DS4 Rolls with one die and no modifications.", () => { it("Should do a regular success roll.", () => { - const rollProvider: RollProvider = jasmine.createSpyObj("rollProvider", ["getNextRoll"]); - - rollProvider.getNextRoll = jasmine.createSpy("getNextRoll").and.returnValue(4); + const rollProvider = mockSingleThrow(4); expect(rollCheckSingleDie(12, new RollOptions(), rollProvider)).toEqual( new RollResult(4, RollResultStatus.SUCCESS, [4]), @@ -21,9 +31,7 @@ describe("DS4 Rolls with one die and no modifications.", () => { }); it("Should do a single success roll on success upper edge case.", () => { - const rollProvider: RollProvider = jasmine.createSpyObj("rollProvider", ["getNextRoll"]); - - rollProvider.getNextRoll = jasmine.createSpy("getNextRoll").and.returnValue(4); + const rollProvider = mockSingleThrow(4); expect(rollCheckSingleDie(4, new RollOptions(), rollProvider)).toEqual( new RollResult(4, RollResultStatus.SUCCESS, [4]), @@ -31,9 +39,7 @@ describe("DS4 Rolls with one die and no modifications.", () => { }); it("Should do a single failure roll on lower edge case.", () => { - const rollProvider: RollProvider = jasmine.createSpyObj("rollProvider", ["getNextRoll"]); - - rollProvider.getNextRoll = jasmine.createSpy("getNextRoll").and.returnValue(5); + const rollProvider = mockSingleThrow(5); expect(rollCheckSingleDie(4, new RollOptions(), rollProvider)).toEqual( new RollResult(0, RollResultStatus.FAILURE, [5]), @@ -41,9 +47,7 @@ describe("DS4 Rolls with one die and no modifications.", () => { }); it("Should do a single failure roll on upper edge case '19'.", () => { - const rollProvider: RollProvider = jasmine.createSpyObj("rollProvider", ["getNextRoll"]); - - rollProvider.getNextRoll = jasmine.createSpy("getNextRoll").and.returnValue(19); + const rollProvider = mockSingleThrow(19); expect(rollCheckSingleDie(4, new RollOptions(), rollProvider)).toEqual( new RollResult(0, RollResultStatus.FAILURE, [19]), @@ -51,9 +55,7 @@ describe("DS4 Rolls with one die and no modifications.", () => { }); it("Should do a single crit success roll on '1'.", () => { - const rollProvider: RollProvider = jasmine.createSpyObj("rollProvider", ["getNextRoll"]); - - rollProvider.getNextRoll = jasmine.createSpy("getNextRoll").and.returnValue(1); + const rollProvider = mockSingleThrow(1); expect(rollCheckSingleDie(4, new RollOptions(), rollProvider)).toEqual( new RollResult(4, RollResultStatus.CRITICAL_SUCCESS, [1]), @@ -61,9 +63,7 @@ describe("DS4 Rolls with one die and no modifications.", () => { }); it("Should do a single crit failure roll on '20'.", () => { - const rollProvider: RollProvider = jasmine.createSpyObj("rollProvider", ["getNextRoll"]); - - rollProvider.getNextRoll = jasmine.createSpy("getNextRoll").and.returnValue(20); + const rollProvider = mockSingleThrow(20); expect(rollCheckSingleDie(4, new RollOptions(), rollProvider)).toEqual( new RollResult(0, RollResultStatus.CRITICAL_FAILURE, [20]), @@ -73,9 +73,7 @@ describe("DS4 Rolls with one die and no modifications.", () => { describe("DS4 Rolls with one die and crit roll modifications.", () => { it("Should do a crit success on `1`.", () => { - const rollProvider: RollProvider = jasmine.createSpyObj("rollProvider", ["getNextRoll"]); - - rollProvider.getNextRoll = jasmine.createSpy("getNextRoll").and.returnValue(1); + const rollProvider = mockSingleThrow(1); expect(rollCheckSingleDie(4, { maxCritSucc: 2, minCritFail: 19 }, rollProvider)).toEqual( new RollResult(4, RollResultStatus.CRITICAL_SUCCESS, [1]), @@ -83,9 +81,7 @@ describe("DS4 Rolls with one die and crit roll modifications.", () => { }); it("Should do a crit success on `maxCritSucc`.", () => { - const rollProvider: RollProvider = jasmine.createSpyObj("rollProvider", ["getNextRoll"]); - - rollProvider.getNextRoll = jasmine.createSpy("getNextRoll").and.returnValue(2); + const rollProvider = mockSingleThrow(2); expect(rollCheckSingleDie(4, { maxCritSucc: 2, minCritFail: 19 }, rollProvider)).toEqual( new RollResult(4, RollResultStatus.CRITICAL_SUCCESS, [2]), @@ -93,9 +89,7 @@ describe("DS4 Rolls with one die and crit roll modifications.", () => { }); it("Should do a success on lower edge case `3`.", () => { - const rollProvider: RollProvider = jasmine.createSpyObj("rollProvider", ["getNextRoll"]); - - rollProvider.getNextRoll = jasmine.createSpy("getNextRoll").and.returnValue(3); + const rollProvider = mockSingleThrow(3); expect(rollCheckSingleDie(4, { maxCritSucc: 2, minCritFail: 19 }, rollProvider)).toEqual( new RollResult(3, RollResultStatus.SUCCESS, [3]), @@ -103,9 +97,7 @@ describe("DS4 Rolls with one die and crit roll modifications.", () => { }); it("Should do a success on upper edge case `18`.", () => { - const rollProvider: RollProvider = jasmine.createSpyObj("rollProvider", ["getNextRoll"]); - - rollProvider.getNextRoll = jasmine.createSpy("getNextRoll").and.returnValue(18); + const rollProvider = mockSingleThrow(18); expect(rollCheckSingleDie(4, { maxCritSucc: 2, minCritFail: 19 }, rollProvider)).toEqual( new RollResult(0, RollResultStatus.FAILURE, [18]), @@ -113,9 +105,7 @@ describe("DS4 Rolls with one die and crit roll modifications.", () => { }); it("Should do a crit fail on `minCritFail`.", () => { - const rollProvider: RollProvider = jasmine.createSpyObj("rollProvider", ["getNextRoll"]); - - rollProvider.getNextRoll = jasmine.createSpy("getNextRoll").and.returnValue(19); + const rollProvider = mockSingleThrow(19); expect(rollCheckSingleDie(4, { maxCritSucc: 2, minCritFail: 19 }, rollProvider)).toEqual( new RollResult(0, RollResultStatus.CRITICAL_FAILURE, [19]), @@ -123,9 +113,7 @@ describe("DS4 Rolls with one die and crit roll modifications.", () => { }); it("Should do a crit fail on `20`", () => { - const rollProvider: RollProvider = jasmine.createSpyObj("rollProvider", ["getNextRoll"]); - - rollProvider.getNextRoll = jasmine.createSpy("getNextRoll").and.returnValue(20); + const rollProvider = mockSingleThrow(20); expect(rollCheckSingleDie(4, { maxCritSucc: 2, minCritFail: 19 }, rollProvider)).toEqual( new RollResult(0, RollResultStatus.CRITICAL_FAILURE, [20]), @@ -135,9 +123,7 @@ describe("DS4 Rolls with one die and crit roll modifications.", () => { describe("DS4 Rools with multiple dice and no modifiers.", () => { it("Should do a crit fail on `20` for first roll.", () => { - const rollProvider: RollProvider = jasmine.createSpyObj("rollProvider", ["getNextRolls"]); - - rollProvider.getNextRolls = jasmine.createSpy("getNextRolls").and.returnValue([20, 15, 6]); + const rollProvider = mockMultipleThrows([20, 15, 6]); expect(rollCheckMultipleDice(48, new RollOptions(), rollProvider)).toEqual( new RollResult(0, RollResultStatus.CRITICAL_FAILURE, [20, 15, 6]), @@ -145,9 +131,7 @@ describe("DS4 Rools with multiple dice and no modifiers.", () => { }); it("Should succeed with all rolls crit successes.", () => { - const rollProvider: RollProvider = jasmine.createSpyObj("rollProvider", ["getNextRolls"]); - - rollProvider.getNextRolls = jasmine.createSpy("getNextRolls").and.returnValue([1, 1, 1]); + const rollProvider = mockMultipleThrows([1, 1, 1]); expect(rollCheckMultipleDice(48, new RollOptions(), rollProvider)).toEqual( new RollResult(48, RollResultStatus.SUCCESS, [1, 1, 1]), @@ -155,9 +139,7 @@ describe("DS4 Rools with multiple dice and no modifiers.", () => { }); it("Should succeed with the last roll not being suficient.", () => { - const rollProvider: RollProvider = jasmine.createSpyObj("rollProvider", ["getNextRolls"]); - - rollProvider.getNextRolls = jasmine.createSpy("getNextRolls").and.returnValue([15, 15, 15]); + const rollProvider = mockMultipleThrows([15, 15, 15]); expect(rollCheckMultipleDice(48, new RollOptions(), rollProvider)).toEqual( new RollResult(30, RollResultStatus.SUCCESS, [15, 15, 15]), @@ -165,9 +147,7 @@ describe("DS4 Rools with multiple dice and no modifiers.", () => { }); it("Should succeed with the last roll a crit success.", () => { - const rollProvider: RollProvider = jasmine.createSpyObj("rollProvider", ["getNextRolls"]); - - rollProvider.getNextRolls = jasmine.createSpy("getNextRolls").and.returnValue([15, 15, 1]); + const rollProvider = mockMultipleThrows([15, 15, 1]); expect(rollCheckMultipleDice(48, new RollOptions(), rollProvider)).toEqual( new RollResult(35, RollResultStatus.SUCCESS, [1, 15, 15]), @@ -175,9 +155,7 @@ describe("DS4 Rools with multiple dice and no modifiers.", () => { }); it("Should succeed with the last roll being 20 and one crit success.", () => { - const rollProvider: RollProvider = jasmine.createSpyObj("rollProvider", ["getNextRolls"]); - - rollProvider.getNextRolls = jasmine.createSpy("getNextRolls").and.returnValue([15, 1, 20]); + const rollProvider = mockMultipleThrows([15, 1, 20]); expect(rollCheckMultipleDice(48, new RollOptions(), rollProvider)).toEqual( new RollResult(40, RollResultStatus.SUCCESS, [1, 20, 15]), @@ -187,9 +165,7 @@ describe("DS4 Rools with multiple dice and no modifiers.", () => { describe("DS4 Rools with multiple dice and min/max modifiers.", () => { it("Should do a crit fail on `19` for first roll.", () => { - const rollProvider: RollProvider = jasmine.createSpyObj("rollProvider", ["getNextRolls"]); - - rollProvider.getNextRolls = jasmine.createSpy("getNextRolls").and.returnValue([19, 15, 6]); + const rollProvider = mockMultipleThrows([19, 15, 6]); expect(rollCheckMultipleDice(48, { maxCritSucc: 2, minCritFail: 19 } as RollOptions, rollProvider)).toEqual( new RollResult(0, RollResultStatus.CRITICAL_FAILURE, [19, 15, 6]), @@ -197,9 +173,7 @@ describe("DS4 Rools with multiple dice and min/max modifiers.", () => { }); it("Should succeed with all rolls crit successes (1 and 2).", () => { - const rollProvider: RollProvider = jasmine.createSpyObj("rollProvider", ["getNextRolls"]); - - rollProvider.getNextRolls = jasmine.createSpy("getNextRolls").and.returnValue([2, 1, 2]); + const rollProvider = mockMultipleThrows([2, 1, 2]); expect(rollCheckMultipleDice(48, { maxCritSucc: 2, minCritFail: 19 } as RollOptions, rollProvider)).toEqual( new RollResult(48, RollResultStatus.SUCCESS, [2, 2, 1]), @@ -207,9 +181,7 @@ describe("DS4 Rools with multiple dice and min/max modifiers.", () => { }); it("Should succeed with the last roll not being suficient.", () => { - const rollProvider: RollProvider = jasmine.createSpyObj("rollProvider", ["getNextRolls"]); - - rollProvider.getNextRolls = jasmine.createSpy("getNextRolls").and.returnValue([15, 15, 15]); + const rollProvider = mockMultipleThrows([15, 15, 15]); expect(rollCheckMultipleDice(48, { maxCritSucc: 2, minCritFail: 19 } as RollOptions, rollProvider)).toEqual( new RollResult(30, RollResultStatus.SUCCESS, [15, 15, 15]), @@ -217,9 +189,7 @@ describe("DS4 Rools with multiple dice and min/max modifiers.", () => { }); it("Should succeed with the last roll a crit success `2`.", () => { - const rollProvider: RollProvider = jasmine.createSpyObj("rollProvider", ["getNextRolls"]); - - rollProvider.getNextRolls = jasmine.createSpy("getNextRolls").and.returnValue([15, 15, 2]); + const rollProvider = mockMultipleThrows([15, 15, 2]); expect(rollCheckMultipleDice(48, { maxCritSucc: 2, minCritFail: 19 } as RollOptions, rollProvider)).toEqual( new RollResult(35, RollResultStatus.SUCCESS, [2, 15, 15]), @@ -227,9 +197,7 @@ describe("DS4 Rools with multiple dice and min/max modifiers.", () => { }); it("Should succeed with the last roll being `20` and one crit success '2'.", () => { - const rollProvider: RollProvider = jasmine.createSpyObj("rollProvider", ["getNextRolls"]); - - rollProvider.getNextRolls = jasmine.createSpy("getNextRolls").and.returnValue([15, 2, 20]); + const rollProvider = mockMultipleThrows([15, 2, 20]); expect(rollCheckMultipleDice(48, { maxCritSucc: 2, minCritFail: 19 } as RollOptions, rollProvider)).toEqual( new RollResult(40, RollResultStatus.SUCCESS, [2, 20, 15]), @@ -237,9 +205,7 @@ describe("DS4 Rools with multiple dice and min/max modifiers.", () => { }); it("Should succeed with the last roll being `19` and one crit success '2'.", () => { - const rollProvider: RollProvider = jasmine.createSpyObj("rollProvider", ["getNextRolls"]); - - rollProvider.getNextRolls = jasmine.createSpy("getNextRolls").and.returnValue([15, 2, 19]); + const rollProvider = mockMultipleThrows([15, 2, 19]); expect(rollCheckMultipleDice(48, { maxCritSucc: 2, minCritFail: 19 } as RollOptions, rollProvider)).toEqual( new RollResult(39, RollResultStatus.SUCCESS, [2, 19, 15]), @@ -249,9 +215,7 @@ describe("DS4 Rools with multiple dice and min/max modifiers.", () => { describe("DS4 Rools with multiple dice and fail modifiers.", () => { it("Should do a crit fail on `19` for first roll.", () => { - const rollProvider: RollProvider = jasmine.createSpyObj("rollProvider", ["getNextRolls"]); - - rollProvider.getNextRolls = jasmine.createSpy("getNextRolls").and.returnValue([19, 15, 6]); + const rollProvider = mockMultipleThrows([19, 15, 6]); expect(rollCheckMultipleDice(48, { minCritFail: 19 } as RollOptions, rollProvider)).toEqual( new RollResult(0, RollResultStatus.CRITICAL_FAILURE, [19, 15, 6]), @@ -261,9 +225,7 @@ describe("DS4 Rools with multiple dice and fail modifiers.", () => { describe("DS4 Rools with multiple dice and success modifiers.", () => { it("Should succeed with all rolls crit successes (1 and 2).", () => { - const rollProvider: RollProvider = jasmine.createSpyObj("rollProvider", ["getNextRolls"]); - - rollProvider.getNextRolls = jasmine.createSpy("getNextRolls").and.returnValue([2, 1, 2]); + const rollProvider = mockMultipleThrows([2, 1, 2]); expect(rollCheckMultipleDice(48, { maxCritSucc: 2 } as RollOptions, rollProvider)).toEqual( new RollResult(48, RollResultStatus.SUCCESS, [2, 2, 1]),