Roll dice (eg Asphodice) and show outcomes https://rpg.bertieb.org/dice-roller/
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

test-dice.spec.ts 4.2 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import { Asphodice } from "../dice";
  2. import { Outcomes } from "../dice";
  3. import { expect } from 'chai';
  4. import 'mocha';
  5. let asphodice = new Asphodice;
  6. interface TestCandidate {
  7. dice: Array<number>;
  8. aboveCutOff?: boolean;
  9. belowCutOff?: boolean;
  10. reroll?: boolean;
  11. cancelled?: Array<number>;
  12. outcomeBalance?: number,
  13. outcome?: Outcomes,
  14. }
  15. const testCandidates: Array<TestCandidate> = [
  16. { dice: [ 6, 6, 6 ], aboveCutOff: true, belowCutOff: false,
  17. reroll: false, cancelled: [6,6,6], outcome: Outcomes.Success, outcomeBalance: 3 },
  18. { dice: [ 2, 2, 2 ], aboveCutOff: false, belowCutOff: true,
  19. reroll: false, cancelled: [2,2,2], outcome: Outcomes.Fail, outcomeBalance: -3 },
  20. { dice: [ 1 ], aboveCutOff: false, belowCutOff: true,
  21. reroll: false, cancelled: [1], outcomeBalance: -1 }, // TODO: temporary behaviour until we handle crits
  22. { dice: [ 10 ], aboveCutOff: true, belowCutOff: false,
  23. reroll: false, cancelled: [10], outcomeBalance: 1 },// TODO: temporary behaviour until we handle crits
  24. { dice: [ 6, 10, 6, 9 ], aboveCutOff: true, belowCutOff: false,
  25. reroll: false, cancelled: [6, 10, 6, 9], outcomeBalance: 4 },
  26. { dice: [ 4, 1, 6, 9 ], aboveCutOff: false, belowCutOff: false,
  27. reroll: true, cancelled: [4, 1, 6, 9], outcomeBalance: 0 },
  28. { dice: [ 10, 10, 1, 1, 1, 6], reroll: true, cancelled: [1,6], outcomeBalance: 0 },
  29. { dice: [ 10, 10, 1, 1, 1, 2], reroll: false, cancelled: [1,2], outcomeBalance: -2},
  30. { dice: [ 10, 1, 2], reroll: false, cancelled: [2], outcome: Outcomes.Success, outcomeBalance: -1 },
  31. ];
  32. describe("allAboveCutOff() says are all above cutoff correctly", function() {
  33. testCandidates.filter(candidate => candidate.aboveCutOff !== undefined)
  34. .forEach( ({dice, aboveCutOff}) => {
  35. it(`allAboveCutOff() says ${dice} → ${aboveCutOff}`, function() {
  36. const result = asphodice.allAboveCutOff(dice);
  37. expect(result).to.equal(aboveCutOff);
  38. });
  39. });
  40. });
  41. describe("allBelowCutOff() says are all above cutoff correctly", function() {
  42. testCandidates.filter(candidate => candidate.belowCutOff !== undefined)
  43. .forEach( ({dice, belowCutOff}) => {
  44. it(`allBelowAboveCutOff() says ${dice} → ${belowCutOff}`, function() {
  45. const result = asphodice.allBelowCutOff(dice);
  46. expect(result).to.equal(belowCutOff);
  47. });
  48. });
  49. });
  50. // TODO: single dice behaviour
  51. describe("rerollNeeded() determines re-roll correctly", function() {
  52. testCandidates.filter(candidate => candidate.reroll!== undefined)
  53. .forEach( ({dice, reroll}) => {
  54. it(`rerollNeeded() says ${dice} → ${reroll}`, function() {
  55. const result = asphodice.rerollNeeded(dice);
  56. expect(result).to.equal(reroll);
  57. });
  58. });
  59. });
  60. describe("cancelRerollDice() filters correctly", function() {
  61. const tests = [
  62. { dice: [ 6, 6, 6 ], out: [6,6,6] },
  63. { dice: [ 2, 2, 2 ], out: [2,2,2] },
  64. { dice: [ 6, 10, 6, 9 ], out: [6, 10, 6, 9] },
  65. { dice: [ 6, 1, 6, 9 ], out: [ 6, 1, 6, 9 ] },
  66. { dice: [ 10, 1, 2], out: [2] },
  67. { dice: [ 10, 10, 1, 1, 1, 6], out: [1,6] },
  68. { dice: [ 10, 10, 1, 1, 1, 2], out: [1,2] },
  69. ];
  70. tests.forEach(({dice, out}) => {
  71. it(`correctly filters ${dice} to ${out}`, function () {
  72. const result = asphodice.cancelRerollDice(dice);
  73. expect(result.sort()).to.deep.equal(out.sort());
  74. });
  75. });
  76. testCandidates.filter(candidate => candidate.cancelled!== undefined)
  77. .forEach( ({dice, cancelled}) => {
  78. it(`cancelRerollDice() says ${dice} → ${cancelled}`, function() {
  79. const result = asphodice.cancelRerollDice(dice).sort();
  80. cancelled?.sort();
  81. expect(result).to.deep.equal(cancelled);
  82. });
  83. });
  84. });
  85. describe("countOutComeBalance() gets the right total", function() {
  86. testCandidates.filter(candidate => candidate.outcomeBalance!== undefined)
  87. .forEach( ({dice, outcomeBalance}) => {
  88. it(`countOutcomeBalance() says ${dice} → ${outcomeBalance}`, function() {
  89. const result = asphodice.countOutcomeBalance(dice);
  90. expect(result).to.equal(outcomeBalance);
  91. });
  92. });
  93. });
  94. describe("checkOutcome() gets the right outcome", function() {
  95. testCandidates.filter(candidate => candidate.outcome!== undefined)
  96. .forEach( ({dice, outcome}) => {
  97. it(`checkOutcome() says ${dice} → ${outcome}`, function() {
  98. const result = asphodice.checkOutcome(dice);
  99. expect(result).to.equal(outcome);
  100. });
  101. });
  102. });