From 831e19699a8b604c81df999b743ac0ce4853f502 Mon Sep 17 00:00:00 2001 From: Marcin Chrzanowski Date: Tue, 22 Aug 2017 18:48:45 -0400 Subject: Refactor --- __tests__/dice.test.js | 218 +++++++++++++++++++++++------------------------ __tests__/parser.test.js | 96 ++++++++++----------- 2 files changed, 157 insertions(+), 157 deletions(-) (limited to '__tests__') diff --git a/__tests__/dice.test.js b/__tests__/dice.test.js index 6ff86e7..15941cb 100644 --- a/__tests__/dice.test.js +++ b/__tests__/dice.test.js @@ -4,12 +4,12 @@ const { d, add, subtract, + bonusAdd, + bonusSubtract, negative, explode, keepHigh, - keepLow, - bonusAdd, - bonusSubtract + keepLow } = require('../src/dice.js') const defaultNumberRolls = 500 @@ -293,12 +293,6 @@ describe('add', () => { ]) }) -describe('negative', () => { - describeBasicDie(1, 6, defaultNumberRolls, true) - describeBasicDie(0, 6, defaultNumberRolls, true) - describeBasicDie(2, 8, defaultNumberRolls, true) -}) - describe('subtract', () => { describeCompoundDice([ { number: 1, sides: 6 }, @@ -317,6 +311,112 @@ describe('subtract', () => { }) }) +describe('bonusAdd', () => { + describe('1d20+3', () => { + const die = bonusAdd(d(constant(1), constant(20)), constant(3)) + testDie(die, { + diceCount: 1, + average: { + average: 13.5 + }, + variance: { + variance: 33.25 + }, + bounds: { + low: 4, + high: 23, + expectLow: true, + expectHigh: true + } + }) + }) + + describe('3d4+1', () => { + const die = bonusAdd(d(constant(3), constant(4)), constant(1)) + testDie(die, { + diceCount: 3, + average: { + average: 10.5 + }, + variance: { + variance: 3.75 + }, + bounds: { + low: 6, + high: 15, + expectLow: true, + expectHigh: true + } + }) + }) + + describe('2d6+1d6', () => { + const die = bonusAdd(d(constant(2), constant(6)), + d(constant(1), constant(6))) + testDie(die, { + diceCount: 2, + average: { + average: 14 + }, + variance: { + variance: 11.67 + }, + bounds: { + low: 4, + high: 24, + expectLow: false, + expectHigh: false + } + }) + }) +}) + +describe('bonusSubtract', () => { + describe('1d20-3', () => { + const die = bonusSubtract(d(constant(1), constant(20)), constant(3)) + testDie(die, { + diceCount: 1, + average: { + average: 7.5 + }, + variance: { + variance: 33.25 + }, + bounds: { + low: -2, + high: 17, + expectLow: true, + expectHigh: true + } + }) + }) + + describe('3d4-1', () => { + const die = bonusSubtract(d(constant(3), constant(4)), constant(1)) + testDie(die, { + diceCount: 3, + average: { + average: 4.5 + }, + variance: { + variance: 3.75 + }, + bounds: { + low: 0, + high: 9, + expectLow: true, + expectHigh: true + } + }) + }) +}) + +describe('negative', () => { + describeBasicDie(1, 6, defaultNumberRolls, true) + describeBasicDie(0, 6, defaultNumberRolls, true) + describeBasicDie(2, 8, defaultNumberRolls, true) +}) + describe('compound dice', () => { describe('(1d4)d(1d6)', () => { const die = d(d(constant(1), constant(4)), d(constant(1), constant(6))) @@ -434,103 +534,3 @@ describe('keep', () => { testDie(die, basicDieTestSpecs(1, 20)) }) }) - -describe('bonusAdd', () => { - describe('1d20+3', () => { - const die = bonusAdd(d(constant(1), constant(20)), constant(3)) - testDie(die, { - diceCount: 1, - average: { - average: 13.5 - }, - variance: { - variance: 33.25 - }, - bounds: { - low: 4, - high: 23, - expectLow: true, - expectHigh: true - } - }) - }) - - describe('3d4+1', () => { - const die = bonusAdd(d(constant(3), constant(4)), constant(1)) - testDie(die, { - diceCount: 3, - average: { - average: 10.5 - }, - variance: { - variance: 3.75 - }, - bounds: { - low: 6, - high: 15, - expectLow: true, - expectHigh: true - } - }) - }) - - describe('2d6+1d6', () => { - const die = bonusAdd(d(constant(2), constant(6)), - d(constant(1), constant(6))) - testDie(die, { - diceCount: 2, - average: { - average: 14 - }, - variance: { - variance: 11.67 - }, - bounds: { - low: 4, - high: 24, - expectLow: false, - expectHigh: false - } - }) - }) -}) - -describe('bonusSubtract', () => { - describe('1d20-3', () => { - const die = bonusSubtract(d(constant(1), constant(20)), constant(3)) - testDie(die, { - diceCount: 1, - average: { - average: 7.5 - }, - variance: { - variance: 33.25 - }, - bounds: { - low: -2, - high: 17, - expectLow: true, - expectHigh: true - } - }) - }) - - describe('3d4-1', () => { - const die = bonusSubtract(d(constant(3), constant(4)), constant(1)) - testDie(die, { - diceCount: 3, - average: { - average: 4.5 - }, - variance: { - variance: 3.75 - }, - bounds: { - low: 0, - high: 9, - expectLow: true, - expectHigh: true - } - }) - }) -}) diff --git a/__tests__/parser.test.js b/__tests__/parser.test.js index fc47576..25edbfc 100644 --- a/__tests__/parser.test.js +++ b/__tests__/parser.test.js @@ -97,6 +97,54 @@ describe('parse', () => { }) }) + it('parses additive bonuses', () => { + expect(parse('3d4+1')).toEqual({ + type: 'bonusAdd', + left: { + type: 'd', + left: { type: 'constant', value: 3 }, + right: { type: 'constant', value: 4 } + }, + right: { type: 'constant', value: 1 } + }) + }) + + it('parses negative bonuses', () => { + expect(parse('3d4-1')).toEqual({ + type: 'bonusSubtract', + left: { + type: 'd', + left: { type: 'constant', value: 3 }, + right: { type: 'constant', value: 4 } + }, + right: { type: 'constant', value: 1 } + }) + }) + + test('bonus binds stronger than addition', () => { + expect(parse('2d6 + 2d6+2d6')).toEqual({ + type: 'add', + left: { + type: 'd', + left: { type: 'constant', value: 2 }, + right: { type: 'constant', value: 6 } + }, + right: { + type: 'bonusAdd', + left: { + type: 'd', + left: { type: 'constant', value: 2 }, + right: { type: 'constant', value: 6 } + }, + right: { + type: 'd', + left: { type: 'constant', value: 2 }, + right: { type: 'constant', value: 6 } + } + } + }) + }) + it('parses negatives', () => { expect(parse('-1 + (-(2d6))')).toEqual({ type: 'add', @@ -151,54 +199,6 @@ describe('parse', () => { }) }) - it('parses additive bonuses', () => { - expect(parse('3d4+1')).toEqual({ - type: 'bonusAdd', - left: { - type: 'd', - left: { type: 'constant', value: 3 }, - right: { type: 'constant', value: 4 } - }, - right: { type: 'constant', value: 1 } - }) - }) - - it('parses negative bonuses', () => { - expect(parse('3d4-1')).toEqual({ - type: 'bonusSubtract', - left: { - type: 'd', - left: { type: 'constant', value: 3 }, - right: { type: 'constant', value: 4 } - }, - right: { type: 'constant', value: 1 } - }) - }) - - test('bonus binds stronger than addition', () => { - expect(parse('2d6 + 2d6+2d6')).toEqual({ - type: 'add', - left: { - type: 'd', - left: { type: 'constant', value: 2 }, - right: { type: 'constant', value: 6 } - }, - right: { - type: 'bonusAdd', - left: { - type: 'd', - left: { type: 'constant', value: 2 }, - right: { type: 'constant', value: 6 } - }, - right: { - type: 'd', - left: { type: 'constant', value: 2 }, - right: { type: 'constant', value: 6 } - } - } - }) - }) - describe('parsing parentheses', () => { test('(1d6)d6', () => { expect(parse('(1d6)d6')).toEqual({ -- cgit v1.2.3