Roll dice (eg Asphodice) and show outcomes https://rpg.bertieb.org/dice-roller/
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 

96 行
3.0 KiB

  1. import { Asphodice } from "../dice";
  2. import { expect } from 'chai';
  3. import 'mocha';
  4. let asphodice = new Asphodice;
  5. interface TestCandidate {
  6. dice: Array<number>;
  7. aboveCutOff?: boolean;
  8. belowCutOff?: boolean;
  9. reroll?: boolean;
  10. cancelled?: Array<number>;
  11. }
  12. const testCandidates: Array<TestCandidate> = [
  13. { dice: [ 6, 6, 6 ], aboveCutOff: true, belowCutOff: false,
  14. reroll: false, cancelled: [6,6,6] },
  15. { dice: [ 2, 2, 2 ], aboveCutOff: false, belowCutOff: true,
  16. reroll: false, cancelled: [2,2,2] },
  17. { dice: [ 1 ], aboveCutOff: false, belowCutOff: true,
  18. reroll: true, cancelled: [1] },
  19. { dice: [ 10 ], aboveCutOff: true, belowCutOff: false,
  20. reroll: true, cancelled: [10] },
  21. { dice: [ 6, 10, 6, 9 ], aboveCutOff: true, belowCutOff: false,
  22. reroll: false, cancelled: [6, 10, 6, 9] },
  23. { dice: [ 4, 1, 6, 9 ], aboveCutOff: false, belowCutOff: false,
  24. reroll: true, cancelled: [4, 1, 6, 9] },
  25. { dice: [ 10, 10, 1, 1, 1, 6], reroll: true, cancelled: [1,6] },
  26. { dice: [ 10, 10, 1, 1, 1, 2], reroll: false, cancelled: [1,2] },
  27. { dice: [ 10, 1, 2], reroll: false, cancelled: [2] },
  28. ];
  29. describe("allAboveCutOff() says are all above cutoff correctly", function() {
  30. testCandidates.filter(candidate => candidate.aboveCutOff !== undefined)
  31. .forEach( ({dice, aboveCutOff}) => {
  32. it(`allAboveCutOff() says ${dice} → ${aboveCutOff}`, function() {
  33. const result = asphodice.allAboveCutOff(dice);
  34. expect(result).to.equal(aboveCutOff);
  35. });
  36. });
  37. });
  38. describe("allBelowCutOff() says are all above cutoff correctly", function() {
  39. testCandidates.filter(candidate => candidate.belowCutOff !== undefined)
  40. .forEach( ({dice, belowCutOff}) => {
  41. it(`allBelowAboveCutOff() says ${dice} → ${belowCutOff}`, function() {
  42. const result = asphodice.allBelowCutOff(dice);
  43. expect(result).to.equal(belowCutOff);
  44. });
  45. });
  46. });
  47. // TODO: single dice behaviour
  48. describe("rerollNeeded() determines re-roll correctly", function() {
  49. testCandidates.filter(candidate => candidate.reroll!== undefined)
  50. .forEach( ({dice, reroll}) => {
  51. it(`rerollNeeded() says ${dice} → ${reroll}`, function() {
  52. const result = asphodice.rerollNeeded(dice);
  53. expect(result).to.equal(reroll);
  54. });
  55. });
  56. });
  57. describe("cancelRerollDice() filters correctly", function() {
  58. const tests = [
  59. { dice: [ 6, 6, 6 ], out: [6,6,6] },
  60. { dice: [ 2, 2, 2 ], out: [2,2,2] },
  61. { dice: [ 6, 10, 6, 9 ], out: [6, 10, 6, 9] },
  62. { dice: [ 6, 1, 6, 9 ], out: [ 6, 1, 6, 9 ] },
  63. { dice: [ 10, 1, 2], out: [2] },
  64. { dice: [ 10, 10, 1, 1, 1, 6], out: [1,6] },
  65. { dice: [ 10, 10, 1, 1, 1, 2], out: [1,2] },
  66. ];
  67. tests.forEach(({dice, out}) => {
  68. it(`correctly filters ${dice} to ${out}`, function () {
  69. const result = asphodice.cancelRerollDice(dice);
  70. expect(result.sort()).to.deep.equal(out.sort());
  71. });
  72. });
  73. testCandidates.filter(candidate => candidate.cancelled!== undefined)
  74. .forEach( ({dice, cancelled}) => {
  75. it(`rerollNeeded() says ${dice} → ${cancelled}`, function() {
  76. const result = asphodice.cancelRerollDice(dice).sort();
  77. cancelled?.sort();
  78. expect(result).to.deep.equal(cancelled);
  79. });
  80. });
  81. });