Ease dice mocking.

This commit is contained in:
Oliver Rümpelein 2021-01-02 13:06:32 +01:00
parent 88f821c7aa
commit 8ca93fc9c9

View file

@ -9,11 +9,21 @@ import { RollProvider } from "../../src/module/rolls/roll-provider";
import "jasmine"; 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<number>): 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.", () => { describe("DS4 Rolls with one die and no modifications.", () => {
it("Should do a regular success roll.", () => { it("Should do a regular success roll.", () => {
const rollProvider: RollProvider = jasmine.createSpyObj("rollProvider", ["getNextRoll"]); const rollProvider = mockSingleThrow(4);
rollProvider.getNextRoll = jasmine.createSpy("getNextRoll").and.returnValue(4);
expect(rollCheckSingleDie(12, new RollOptions(), rollProvider)).toEqual( expect(rollCheckSingleDie(12, new RollOptions(), rollProvider)).toEqual(
new RollResult(4, RollResultStatus.SUCCESS, [4]), 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.", () => { it("Should do a single success roll on success upper edge case.", () => {
const rollProvider: RollProvider = jasmine.createSpyObj("rollProvider", ["getNextRoll"]); const rollProvider = mockSingleThrow(4);
rollProvider.getNextRoll = jasmine.createSpy("getNextRoll").and.returnValue(4);
expect(rollCheckSingleDie(4, new RollOptions(), rollProvider)).toEqual( expect(rollCheckSingleDie(4, new RollOptions(), rollProvider)).toEqual(
new RollResult(4, RollResultStatus.SUCCESS, [4]), 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.", () => { it("Should do a single failure roll on lower edge case.", () => {
const rollProvider: RollProvider = jasmine.createSpyObj("rollProvider", ["getNextRoll"]); const rollProvider = mockSingleThrow(5);
rollProvider.getNextRoll = jasmine.createSpy("getNextRoll").and.returnValue(5);
expect(rollCheckSingleDie(4, new RollOptions(), rollProvider)).toEqual( expect(rollCheckSingleDie(4, new RollOptions(), rollProvider)).toEqual(
new RollResult(0, RollResultStatus.FAILURE, [5]), 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'.", () => { it("Should do a single failure roll on upper edge case '19'.", () => {
const rollProvider: RollProvider = jasmine.createSpyObj("rollProvider", ["getNextRoll"]); const rollProvider = mockSingleThrow(19);
rollProvider.getNextRoll = jasmine.createSpy("getNextRoll").and.returnValue(19);
expect(rollCheckSingleDie(4, new RollOptions(), rollProvider)).toEqual( expect(rollCheckSingleDie(4, new RollOptions(), rollProvider)).toEqual(
new RollResult(0, RollResultStatus.FAILURE, [19]), 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'.", () => { it("Should do a single crit success roll on '1'.", () => {
const rollProvider: RollProvider = jasmine.createSpyObj("rollProvider", ["getNextRoll"]); const rollProvider = mockSingleThrow(1);
rollProvider.getNextRoll = jasmine.createSpy("getNextRoll").and.returnValue(1);
expect(rollCheckSingleDie(4, new RollOptions(), rollProvider)).toEqual( expect(rollCheckSingleDie(4, new RollOptions(), rollProvider)).toEqual(
new RollResult(4, RollResultStatus.CRITICAL_SUCCESS, [1]), 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'.", () => { it("Should do a single crit failure roll on '20'.", () => {
const rollProvider: RollProvider = jasmine.createSpyObj("rollProvider", ["getNextRoll"]); const rollProvider = mockSingleThrow(20);
rollProvider.getNextRoll = jasmine.createSpy("getNextRoll").and.returnValue(20);
expect(rollCheckSingleDie(4, new RollOptions(), rollProvider)).toEqual( expect(rollCheckSingleDie(4, new RollOptions(), rollProvider)).toEqual(
new RollResult(0, RollResultStatus.CRITICAL_FAILURE, [20]), 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.", () => { describe("DS4 Rolls with one die and crit roll modifications.", () => {
it("Should do a crit success on `1`.", () => { it("Should do a crit success on `1`.", () => {
const rollProvider: RollProvider = jasmine.createSpyObj("rollProvider", ["getNextRoll"]); const rollProvider = mockSingleThrow(1);
rollProvider.getNextRoll = jasmine.createSpy("getNextRoll").and.returnValue(1);
expect(rollCheckSingleDie(4, { maxCritSucc: 2, minCritFail: 19 }, rollProvider)).toEqual( expect(rollCheckSingleDie(4, { maxCritSucc: 2, minCritFail: 19 }, rollProvider)).toEqual(
new RollResult(4, RollResultStatus.CRITICAL_SUCCESS, [1]), 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`.", () => { it("Should do a crit success on `maxCritSucc`.", () => {
const rollProvider: RollProvider = jasmine.createSpyObj("rollProvider", ["getNextRoll"]); const rollProvider = mockSingleThrow(2);
rollProvider.getNextRoll = jasmine.createSpy("getNextRoll").and.returnValue(2);
expect(rollCheckSingleDie(4, { maxCritSucc: 2, minCritFail: 19 }, rollProvider)).toEqual( expect(rollCheckSingleDie(4, { maxCritSucc: 2, minCritFail: 19 }, rollProvider)).toEqual(
new RollResult(4, RollResultStatus.CRITICAL_SUCCESS, [2]), 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`.", () => { it("Should do a success on lower edge case `3`.", () => {
const rollProvider: RollProvider = jasmine.createSpyObj("rollProvider", ["getNextRoll"]); const rollProvider = mockSingleThrow(3);
rollProvider.getNextRoll = jasmine.createSpy("getNextRoll").and.returnValue(3);
expect(rollCheckSingleDie(4, { maxCritSucc: 2, minCritFail: 19 }, rollProvider)).toEqual( expect(rollCheckSingleDie(4, { maxCritSucc: 2, minCritFail: 19 }, rollProvider)).toEqual(
new RollResult(3, RollResultStatus.SUCCESS, [3]), 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`.", () => { it("Should do a success on upper edge case `18`.", () => {
const rollProvider: RollProvider = jasmine.createSpyObj("rollProvider", ["getNextRoll"]); const rollProvider = mockSingleThrow(18);
rollProvider.getNextRoll = jasmine.createSpy("getNextRoll").and.returnValue(18);
expect(rollCheckSingleDie(4, { maxCritSucc: 2, minCritFail: 19 }, rollProvider)).toEqual( expect(rollCheckSingleDie(4, { maxCritSucc: 2, minCritFail: 19 }, rollProvider)).toEqual(
new RollResult(0, RollResultStatus.FAILURE, [18]), 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`.", () => { it("Should do a crit fail on `minCritFail`.", () => {
const rollProvider: RollProvider = jasmine.createSpyObj("rollProvider", ["getNextRoll"]); const rollProvider = mockSingleThrow(19);
rollProvider.getNextRoll = jasmine.createSpy("getNextRoll").and.returnValue(19);
expect(rollCheckSingleDie(4, { maxCritSucc: 2, minCritFail: 19 }, rollProvider)).toEqual( expect(rollCheckSingleDie(4, { maxCritSucc: 2, minCritFail: 19 }, rollProvider)).toEqual(
new RollResult(0, RollResultStatus.CRITICAL_FAILURE, [19]), 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`", () => { it("Should do a crit fail on `20`", () => {
const rollProvider: RollProvider = jasmine.createSpyObj("rollProvider", ["getNextRoll"]); const rollProvider = mockSingleThrow(20);
rollProvider.getNextRoll = jasmine.createSpy("getNextRoll").and.returnValue(20);
expect(rollCheckSingleDie(4, { maxCritSucc: 2, minCritFail: 19 }, rollProvider)).toEqual( expect(rollCheckSingleDie(4, { maxCritSucc: 2, minCritFail: 19 }, rollProvider)).toEqual(
new RollResult(0, RollResultStatus.CRITICAL_FAILURE, [20]), 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.", () => { describe("DS4 Rools with multiple dice and no modifiers.", () => {
it("Should do a crit fail on `20` for first roll.", () => { it("Should do a crit fail on `20` for first roll.", () => {
const rollProvider: RollProvider = jasmine.createSpyObj("rollProvider", ["getNextRolls"]); const rollProvider = mockMultipleThrows([20, 15, 6]);
rollProvider.getNextRolls = jasmine.createSpy("getNextRolls").and.returnValue([20, 15, 6]);
expect(rollCheckMultipleDice(48, new RollOptions(), rollProvider)).toEqual( expect(rollCheckMultipleDice(48, new RollOptions(), rollProvider)).toEqual(
new RollResult(0, RollResultStatus.CRITICAL_FAILURE, [20, 15, 6]), 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.", () => { it("Should succeed with all rolls crit successes.", () => {
const rollProvider: RollProvider = jasmine.createSpyObj("rollProvider", ["getNextRolls"]); const rollProvider = mockMultipleThrows([1, 1, 1]);
rollProvider.getNextRolls = jasmine.createSpy("getNextRolls").and.returnValue([1, 1, 1]);
expect(rollCheckMultipleDice(48, new RollOptions(), rollProvider)).toEqual( expect(rollCheckMultipleDice(48, new RollOptions(), rollProvider)).toEqual(
new RollResult(48, RollResultStatus.SUCCESS, [1, 1, 1]), 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.", () => { it("Should succeed with the last roll not being suficient.", () => {
const rollProvider: RollProvider = jasmine.createSpyObj("rollProvider", ["getNextRolls"]); const rollProvider = mockMultipleThrows([15, 15, 15]);
rollProvider.getNextRolls = jasmine.createSpy("getNextRolls").and.returnValue([15, 15, 15]);
expect(rollCheckMultipleDice(48, new RollOptions(), rollProvider)).toEqual( expect(rollCheckMultipleDice(48, new RollOptions(), rollProvider)).toEqual(
new RollResult(30, RollResultStatus.SUCCESS, [15, 15, 15]), 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.", () => { it("Should succeed with the last roll a crit success.", () => {
const rollProvider: RollProvider = jasmine.createSpyObj("rollProvider", ["getNextRolls"]); const rollProvider = mockMultipleThrows([15, 15, 1]);
rollProvider.getNextRolls = jasmine.createSpy("getNextRolls").and.returnValue([15, 15, 1]);
expect(rollCheckMultipleDice(48, new RollOptions(), rollProvider)).toEqual( expect(rollCheckMultipleDice(48, new RollOptions(), rollProvider)).toEqual(
new RollResult(35, RollResultStatus.SUCCESS, [1, 15, 15]), 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.", () => { it("Should succeed with the last roll being 20 and one crit success.", () => {
const rollProvider: RollProvider = jasmine.createSpyObj("rollProvider", ["getNextRolls"]); const rollProvider = mockMultipleThrows([15, 1, 20]);
rollProvider.getNextRolls = jasmine.createSpy("getNextRolls").and.returnValue([15, 1, 20]);
expect(rollCheckMultipleDice(48, new RollOptions(), rollProvider)).toEqual( expect(rollCheckMultipleDice(48, new RollOptions(), rollProvider)).toEqual(
new RollResult(40, RollResultStatus.SUCCESS, [1, 20, 15]), 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.", () => { describe("DS4 Rools with multiple dice and min/max modifiers.", () => {
it("Should do a crit fail on `19` for first roll.", () => { it("Should do a crit fail on `19` for first roll.", () => {
const rollProvider: RollProvider = jasmine.createSpyObj("rollProvider", ["getNextRolls"]); const rollProvider = mockMultipleThrows([19, 15, 6]);
rollProvider.getNextRolls = jasmine.createSpy("getNextRolls").and.returnValue([19, 15, 6]);
expect(rollCheckMultipleDice(48, { maxCritSucc: 2, minCritFail: 19 } as RollOptions, rollProvider)).toEqual( expect(rollCheckMultipleDice(48, { maxCritSucc: 2, minCritFail: 19 } as RollOptions, rollProvider)).toEqual(
new RollResult(0, RollResultStatus.CRITICAL_FAILURE, [19, 15, 6]), 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).", () => { it("Should succeed with all rolls crit successes (1 and 2).", () => {
const rollProvider: RollProvider = jasmine.createSpyObj("rollProvider", ["getNextRolls"]); const rollProvider = mockMultipleThrows([2, 1, 2]);
rollProvider.getNextRolls = jasmine.createSpy("getNextRolls").and.returnValue([2, 1, 2]);
expect(rollCheckMultipleDice(48, { maxCritSucc: 2, minCritFail: 19 } as RollOptions, rollProvider)).toEqual( expect(rollCheckMultipleDice(48, { maxCritSucc: 2, minCritFail: 19 } as RollOptions, rollProvider)).toEqual(
new RollResult(48, RollResultStatus.SUCCESS, [2, 2, 1]), 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.", () => { it("Should succeed with the last roll not being suficient.", () => {
const rollProvider: RollProvider = jasmine.createSpyObj("rollProvider", ["getNextRolls"]); const rollProvider = mockMultipleThrows([15, 15, 15]);
rollProvider.getNextRolls = jasmine.createSpy("getNextRolls").and.returnValue([15, 15, 15]);
expect(rollCheckMultipleDice(48, { maxCritSucc: 2, minCritFail: 19 } as RollOptions, rollProvider)).toEqual( expect(rollCheckMultipleDice(48, { maxCritSucc: 2, minCritFail: 19 } as RollOptions, rollProvider)).toEqual(
new RollResult(30, RollResultStatus.SUCCESS, [15, 15, 15]), 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`.", () => { it("Should succeed with the last roll a crit success `2`.", () => {
const rollProvider: RollProvider = jasmine.createSpyObj("rollProvider", ["getNextRolls"]); const rollProvider = mockMultipleThrows([15, 15, 2]);
rollProvider.getNextRolls = jasmine.createSpy("getNextRolls").and.returnValue([15, 15, 2]);
expect(rollCheckMultipleDice(48, { maxCritSucc: 2, minCritFail: 19 } as RollOptions, rollProvider)).toEqual( expect(rollCheckMultipleDice(48, { maxCritSucc: 2, minCritFail: 19 } as RollOptions, rollProvider)).toEqual(
new RollResult(35, RollResultStatus.SUCCESS, [2, 15, 15]), 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'.", () => { it("Should succeed with the last roll being `20` and one crit success '2'.", () => {
const rollProvider: RollProvider = jasmine.createSpyObj("rollProvider", ["getNextRolls"]); const rollProvider = mockMultipleThrows([15, 2, 20]);
rollProvider.getNextRolls = jasmine.createSpy("getNextRolls").and.returnValue([15, 2, 20]);
expect(rollCheckMultipleDice(48, { maxCritSucc: 2, minCritFail: 19 } as RollOptions, rollProvider)).toEqual( expect(rollCheckMultipleDice(48, { maxCritSucc: 2, minCritFail: 19 } as RollOptions, rollProvider)).toEqual(
new RollResult(40, RollResultStatus.SUCCESS, [2, 20, 15]), 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'.", () => { it("Should succeed with the last roll being `19` and one crit success '2'.", () => {
const rollProvider: RollProvider = jasmine.createSpyObj("rollProvider", ["getNextRolls"]); const rollProvider = mockMultipleThrows([15, 2, 19]);
rollProvider.getNextRolls = jasmine.createSpy("getNextRolls").and.returnValue([15, 2, 19]);
expect(rollCheckMultipleDice(48, { maxCritSucc: 2, minCritFail: 19 } as RollOptions, rollProvider)).toEqual( expect(rollCheckMultipleDice(48, { maxCritSucc: 2, minCritFail: 19 } as RollOptions, rollProvider)).toEqual(
new RollResult(39, RollResultStatus.SUCCESS, [2, 19, 15]), 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.", () => { describe("DS4 Rools with multiple dice and fail modifiers.", () => {
it("Should do a crit fail on `19` for first roll.", () => { it("Should do a crit fail on `19` for first roll.", () => {
const rollProvider: RollProvider = jasmine.createSpyObj("rollProvider", ["getNextRolls"]); const rollProvider = mockMultipleThrows([19, 15, 6]);
rollProvider.getNextRolls = jasmine.createSpy("getNextRolls").and.returnValue([19, 15, 6]);
expect(rollCheckMultipleDice(48, { minCritFail: 19 } as RollOptions, rollProvider)).toEqual( expect(rollCheckMultipleDice(48, { minCritFail: 19 } as RollOptions, rollProvider)).toEqual(
new RollResult(0, RollResultStatus.CRITICAL_FAILURE, [19, 15, 6]), 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.", () => { describe("DS4 Rools with multiple dice and success modifiers.", () => {
it("Should succeed with all rolls crit successes (1 and 2).", () => { it("Should succeed with all rolls crit successes (1 and 2).", () => {
const rollProvider: RollProvider = jasmine.createSpyObj("rollProvider", ["getNextRolls"]); const rollProvider = mockMultipleThrows([2, 1, 2]);
rollProvider.getNextRolls = jasmine.createSpy("getNextRolls").and.returnValue([2, 1, 2]);
expect(rollCheckMultipleDice(48, { maxCritSucc: 2 } as RollOptions, rollProvider)).toEqual( expect(rollCheckMultipleDice(48, { maxCritSucc: 2 } as RollOptions, rollProvider)).toEqual(
new RollResult(48, RollResultStatus.SUCCESS, [2, 2, 1]), new RollResult(48, RollResultStatus.SUCCESS, [2, 2, 1]),