From 1cc6b80cfd4875f289837421cdbe81e3dfcf0ed1 Mon Sep 17 00:00:00 2001 From: Marcin Chrzanowski Date: Sat, 2 Sep 2017 00:47:46 -0400 Subject: Implement repeat --- __tests__/dice.test.js | 45 ++++++++++++++++++++++++++++++++++++++++++++- __tests__/lexer.test.js | 12 ++++++++++++ __tests__/parser.test.js | 16 ++++++++++++++++ 3 files changed, 72 insertions(+), 1 deletion(-) (limited to '__tests__') diff --git a/__tests__/dice.test.js b/__tests__/dice.test.js index d397d4c..6a9ba46 100644 --- a/__tests__/dice.test.js +++ b/__tests__/dice.test.js @@ -16,7 +16,8 @@ const { againAbove, againUnder, thresholdHigh, - thresholdLow + thresholdLow, + repeat } = require('../src/dice.js') const defaultNumberRolls = 500 @@ -692,3 +693,45 @@ describe('threshold', () => { }) }) }) + +describe('repeat', () => { + describe('1d6 x 2', () => { + const die = repeat(d(constant(1), constant(6)), constant(2)) + + testDie(die, { + diceCount: 2, + average: { + average: 7 + }, + variance: { + variance: 5.83 + }, + bounds: { + low: 2, + high: 12, + expectLow: true, + expectHigh: true + } + }) + }) + + describe('1d6 x 1d4', () => { + const die = repeat(d(constant(1), constant(6)), d(constant(1), constant(4))) + + testDie(die, { + variableDiceCount: { + min: 1, + max: 4 + }, + average: { + average: 8.75 + }, + bounds: { + low: 1, + high: 24, + expectLow: true, + expectHigh: false + } + }) + }) +}) diff --git a/__tests__/lexer.test.js b/__tests__/lexer.test.js index 0c615d5..6745715 100644 --- a/__tests__/lexer.test.js +++ b/__tests__/lexer.test.js @@ -284,4 +284,16 @@ describe('lex', () => { ]) }) }) + + describe('repeat', () => { + test('1d6 x 3', () => { + expect(lex('1d6 x 3')).toEqual([ + { type: 'constant', value: 1 }, + { type: 'd' }, + { type: 'constant', value: 6 }, + { type: ' x ' }, + { type: 'constant', value: 3 } + ]) + }) + }) }) diff --git a/__tests__/parser.test.js b/__tests__/parser.test.js index bef11c5..d696b3e 100644 --- a/__tests__/parser.test.js +++ b/__tests__/parser.test.js @@ -285,6 +285,22 @@ describe('parse', () => { }) }) + it('parses dice with repeat', () => { + expect(parse('1d6 x 1d4')).toEqual({ + type: 'repeat', + left: { + type: 'd', + left: { type: 'constant', value: 1 }, + right: { type: 'constant', value: 6 } + }, + right: { + type: 'd', + left: { type: 'constant', value: 1 }, + right: { type: 'constant', value: 4 } + } + }) + }) + describe('parsing parentheses', () => { test('(1d6)d6', () => { expect(parse('(1d6)d6')).toEqual({ -- cgit v1.2.3