Skip to content

Instantly share code, notes, and snippets.

@shaefer
Created June 5, 2018 21:13
Show Gist options
  • Save shaefer/250ff254bc7e6d5a41b7bfb271f88d75 to your computer and use it in GitHub Desktop.
Save shaefer/250ff254bc7e6d5a41b7bfb271f88d75 to your computer and use it in GitHub Desktop.
Dicebag.test.js Phase 3
import {DiceBag, DiceBagWithSeed} from './Dice'
import seedrandom from 'seedrandom'
it('rolls a basic die and returns results', () => {
const diceBag = DiceBag();
for (let i = 0; i<100; i++) {
const result = diceBag.d6().total;
expect(result).toBeGreaterThanOrEqual(1);
expect(result).toBeLessThanOrEqual(6);
}
});
it ('rolls multiple dice with any number of sides', () => {
const numOfDice = 2;
const numOfSides = 10;
for (let i = 0; i<1000; i++) {
const result = DiceBag().rollDice(numOfDice, numOfSides).total;
expect(result).toBeGreaterThanOrEqual(2);
expect(result).toBeLessThanOrEqual(20);
}
});
it('rolls multiple dice with any number of sides has array of individual results', () => {
const numOfDice = 2;
const numOfSides = 10;
const outcome = DiceBag().rollDice(numOfDice, numOfSides);
expect(outcome.individualResults.length).toEqual(2);
});
it('throws exception when more than 1000000 dice are attempted', () => {
const numOfDice = 1400000;
const numOfSides = 8;
const outcome =
expect(() => {
DiceBag().rollDice(numOfDice, numOfSides);
}).toThrow();
});
it('throws exception when more than 1000000 sides are attempted', () => {
const numOfDice = 8;
const numOfSides = 1400000;
const outcome =
expect(() => {
DiceBag().rollDice(numOfDice, numOfSides);
}).toThrow();
});
it('specified seed always returns the same result', () => {
const seed = "someRandomSeed!";
const diceBag = DiceBagWithSeed(seed);
const outcome = diceBag.rollDice(2, 6);
expect(outcome.individualResults).toEqual(expect.arrayContaining([6, 6]));
});
it('generator is reused resulting in same results for 2 subsequent rolls as a single call with 2 dice.', () => {
const seed = "someRandomSeed!";
const diceBag = DiceBagWithSeed(seed);
const outcome = diceBag.rollDice(2, 6);
expect(outcome.individualResults).toEqual(expect.arrayContaining([6, 6]));
const diceBag2 = DiceBagWithSeed(seed);
const db2Outcome1 = diceBag2.rollDice(1, 6);
const db2Outcome2 = diceBag2.rollDice(1, 6);
expect(db2Outcome1.total).toEqual(6);
expect(db2Outcome2.total).toEqual(6);
});
//Sum (This should actually be in the ArrayUtils.js test)
it('should add all numbers together', () => {
var result = DiceBag().sum([1,2,3,4]);
expect(result).toEqual(10);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment