From 9368b1c33db290d7c80c93db5c867cdb7fcaf14d Mon Sep 17 00:00:00 2001 From: Marcin Chrzanowski Date: Mon, 11 Sep 2017 15:47:50 -0400 Subject: Implement collecting --- __tests__/dice.test.js | 43 ++++++++++++++++++++++++++++++++++++++++++- __tests__/lexer.test.js | 12 ++++++++++++ __tests__/parser.test.js | 19 +++++++++++++++++++ 3 files changed, 73 insertions(+), 1 deletion(-) (limited to '__tests__') diff --git a/__tests__/dice.test.js b/__tests__/dice.test.js index 9c03a48..56572d2 100644 --- a/__tests__/dice.test.js +++ b/__tests__/dice.test.js @@ -19,7 +19,8 @@ const { againUnder, thresholdHigh, thresholdLow, - repeat + repeat, + collect } = require('../src/dice.js') const defaultNumberRolls = 500 @@ -817,3 +818,43 @@ describe('repeat', () => { }) }) }) + +describe('collect', () => { + describe('[2d6]', () => { + const die = collect(d(constant(2), constant(6))) + + testDie(die, { + diceCount: 1, + average: { + average: 7 + }, + variance: { + variance: 5.83 + }, + bounds: { + low: 2, + high: 12, + expectLow: true, + expectHigh: true + } + }) + }) + + describe('[1d6 x 1d4]', () => { + const die = + collect(repeat(d(constant(1), constant(6)), d(constant(1), constant(4)))) + + testDie(die, { + diceCount: 1, + 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 7124746..b2cb77e 100644 --- a/__tests__/lexer.test.js +++ b/__tests__/lexer.test.js @@ -171,6 +171,18 @@ describe('lex', () => { { type: 'constant', value: 4 } ]) }) + + it('[2d8]+3', () => { + expect(lex('[2d8]+3')).toEqual([ + { type: '[' }, + { type: 'constant', value: 2 }, + { type: 'd' }, + { type: 'constant', value: 8 }, + { type: ']' }, + { type: 'plus' }, + { type: 'constant', value: 3 } + ]) + }) }) describe('lexes negatives', () => { diff --git a/__tests__/parser.test.js b/__tests__/parser.test.js index 3c9f6ec..cb89ad5 100644 --- a/__tests__/parser.test.js +++ b/__tests__/parser.test.js @@ -325,6 +325,25 @@ describe('parse', () => { }) }) + it('parses dice with collecting', () => { + expect(parse('[2d6] x 1d4')).toEqual({ + type: 'repeat', + left: { + type: 'collect', + value: { + type: 'd', + left: { type: 'constant', value: 2 }, + 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