m-chrzan.xyz
aboutsummaryrefslogtreecommitdiff
path: root/__tests__/parser.test.js
diff options
context:
space:
mode:
authorMarcin Chrzanowski <marcin.j.chrzanowski@gmail.com>2017-07-15 22:48:10 -0400
committerMarcin Chrzanowski <marcin.j.chrzanowski@gmail.com>2017-07-15 23:09:22 -0400
commite0ab43bcdc050ca3cac2adea927817d99700737e (patch)
treee11746830954eba9ec7a030f5588325c53a258ef /__tests__/parser.test.js
parent522654d5bf68636a39477f67fa4fd04961f5a6d0 (diff)
Throw Error on syntax errors
Diffstat (limited to '__tests__/parser.test.js')
-rw-r--r--__tests__/parser.test.js17
1 files changed, 17 insertions, 0 deletions
diff --git a/__tests__/parser.test.js b/__tests__/parser.test.js
index 74ca442..f3c6e97 100644
--- a/__tests__/parser.test.js
+++ b/__tests__/parser.test.js
@@ -1,6 +1,23 @@
const { parse } = require('../src/parser.js')
describe('parse', () => {
+ describe('error handling', () => {
+ it('throws when a binary operation lacks an argument', () => {
+ expect(() => { parse('1d') }).toThrow(/Syntax error/)
+ expect(() => { parse('2+3+') }).toThrow(/Syntax error/)
+ })
+
+ it('throws when two binary operations follow each other', () => {
+ expect(() => { parse('1dd2') }).toThrow(/Syntax error/)
+ expect(() => { parse('1+d2') }).toThrow(/Syntax error/)
+ })
+
+ it('throws when two dice not combined with a binary operation', () => {
+ expect(() => { parse('(1d4)(1d6)') }).toThrow(/Syntax error/)
+ expect(() => { parse('1 2') }).toThrow(/Syntax error/)
+ })
+ })
+
it('parses a constant', () => {
expect(parse('5')).toEqual({ type: 'constant', value: 5 })
})