m-chrzan.xyz
aboutsummaryrefslogtreecommitdiff
path: root/__tests__
diff options
context:
space:
mode:
authorMarcin Chrzanowski <marcin.j.chrzanowski@gmail.com>2017-08-30 17:46:07 -0400
committerMarcin Chrzanowski <marcin.j.chrzanowski@gmail.com>2017-08-30 17:46:07 -0400
commit5087cde4825d91112cad565c68d81296359cf4d8 (patch)
treead9dd27b079b1f41aa07c66f4f401c645241e25c /__tests__
parent94bbae437d98c103d1dae09be9949bcf26e8bcf9 (diff)
Implement multiplication
Diffstat (limited to '__tests__')
-rw-r--r--__tests__/dice.test.js23
-rw-r--r--__tests__/lexer.test.js24
-rw-r--r--__tests__/parser.test.js42
3 files changed, 89 insertions, 0 deletions
diff --git a/__tests__/dice.test.js b/__tests__/dice.test.js
index caba0a9..4e89031 100644
--- a/__tests__/dice.test.js
+++ b/__tests__/dice.test.js
@@ -4,6 +4,7 @@ const {
d,
add,
subtract,
+ multiply,
bonusAdd,
bonusSubtract,
negative,
@@ -316,6 +317,28 @@ describe('subtract', () => {
})
})
+describe('multiply', () => {
+ describe('1d6 * 3', () => {
+ const die = multiply(d(constant(1), constant(6)), constant(3))
+ testDie(die, {
+ diceCount: 1,
+ average: {
+ average: 10.5
+ },
+ variance: {
+ variance: 26.25
+ },
+ bounds: {
+ low: 3,
+ high: 18,
+ expectLow: true,
+ expectHigh: true
+ }
+ })
+ })
+})
+
+
describe('bonusAdd', () => {
describe('1d20+3', () => {
const die = bonusAdd(d(constant(1), constant(20)), constant(3))
diff --git a/__tests__/lexer.test.js b/__tests__/lexer.test.js
index b51c0f9..db7ab50 100644
--- a/__tests__/lexer.test.js
+++ b/__tests__/lexer.test.js
@@ -97,6 +97,30 @@ describe('lex', () => {
})
})
+ describe('lexes multiplication', () => {
+ it('1d6 * 1d4', () => {
+ expect(lex('1d6 * 1d4')).toEqual([
+ { type: 'constant', value: 1 },
+ { type: 'd' },
+ { type: 'constant', value: 6 },
+ { type: 'bigTimes' },
+ { type: 'constant', value: 1 },
+ { type: 'd' },
+ { type: 'constant', value: 4 }
+ ])
+ })
+
+ it('2d17 * 4', () => {
+ expect(lex('2d17 * 4')).toEqual([
+ { type: 'constant', value: 2 },
+ { type: 'd' },
+ { type: 'constant', value: 17 },
+ { type: 'bigTimes' },
+ { type: 'constant', value: 4 }
+ ])
+ })
+ })
+
describe('lexes parentheses', () => {
it('(1d6)d6', () => {
expect(lex('(1d6)d6')).toEqual([
diff --git a/__tests__/parser.test.js b/__tests__/parser.test.js
index 6e575b6..5d4288d 100644
--- a/__tests__/parser.test.js
+++ b/__tests__/parser.test.js
@@ -97,6 +97,22 @@ describe('parse', () => {
})
})
+ it('parses dice multiplication', () => {
+ expect(parse('1d6 * 2d8')).toEqual({
+ type: 'multiply',
+ left: {
+ type: 'd',
+ left: { type: 'constant', value: 1 },
+ right: { type: 'constant', value: 6 }
+ },
+ right: {
+ type: 'd',
+ left: { type: 'constant', value: 2 },
+ right: { type: 'constant', value: 8 }
+ }
+ })
+ })
+
it('parses additive bonuses', () => {
expect(parse('3d4+1')).toEqual({
type: 'bonusAdd',
@@ -282,4 +298,30 @@ describe('parse', () => {
})
})
})
+
+ describe('order of operations', () => {
+ test('2 * 3 + 4', () => {
+ expect(parse('2 * 3 + 4')).toEqual({
+ type: 'add',
+ left: {
+ type: 'multiply',
+ left: { type: 'constant', value: 2 },
+ right: { type: 'constant', value: 3 },
+ },
+ right: { type: 'constant', value: 4 },
+ })
+ })
+
+ test('2 + 3 * 4', () => {
+ expect(parse('2 + 3 * 4')).toEqual({
+ type: 'add',
+ left: { type: 'constant', value: 2 },
+ right: {
+ type: 'multiply',
+ left: { type: 'constant', value: 3 },
+ right: { type: 'constant', value: 4 },
+ }
+ })
+ })
+ })
})