diff options
author | Marcin Chrzanowski <marcin.j.chrzanowski@gmail.com> | 2017-07-14 23:34:55 -0400 |
---|---|---|
committer | Marcin Chrzanowski <marcin.j.chrzanowski@gmail.com> | 2017-07-14 23:34:55 -0400 |
commit | c5b9b7d03a37b807b212c29a9bdf7afc837c5423 (patch) | |
tree | 010ff6b701a22204cdec8fd277e7a8129ae988e2 /__tests__ | |
parent | d3132a5463908bcabd0578ad48a457b2bec30a9a (diff) |
Lex and parse parentheses
Diffstat (limited to '__tests__')
-rw-r--r-- | __tests__/lexer.test.js | 28 | ||||
-rw-r--r-- | __tests__/parser.test.js | 30 |
2 files changed, 58 insertions, 0 deletions
diff --git a/__tests__/lexer.test.js b/__tests__/lexer.test.js index e9115a1..38c880e 100644 --- a/__tests__/lexer.test.js +++ b/__tests__/lexer.test.js @@ -88,4 +88,32 @@ describe('lex', () => { ]) }) }) + + describe('lexes parentheses', () => { + it('(1d6)d6', () => { + expect(lex('(1d6)d6')).toEqual([ + { type: '(' }, + { type: 'constant', value: 1 }, + { type: 'd' }, + { type: 'constant', value: 6 }, + { type: ')' }, + { type: 'd' }, + { type: 'constant', value: 6 } + ]) + }) + + it('2d(6+3)d4', () => { + expect(lex('2d(6+3)d4')).toEqual([ + { type: 'constant', value: 2 }, + { type: 'd' }, + { type: '(' }, + { type: 'constant', value: 6 }, + { type: '+' }, + { type: 'constant', value: 3 }, + { type: ')' }, + { type: 'd' }, + { type: 'constant', value: 4 } + ]) + }) + }) }) diff --git a/__tests__/parser.test.js b/__tests__/parser.test.js index 6fe296c..a741007 100644 --- a/__tests__/parser.test.js +++ b/__tests__/parser.test.js @@ -72,4 +72,34 @@ describe('parse', () => { } }) }) + + describe('parsing parentheses', () => { + test('(1d6)d6', () => { + expect(parse('(1d6)d6')).toEqual({ + type: 'd', + left: { + type: 'd', + left: { type: 'constant', value: 1 }, + right: { type: 'constant', value: 6 } + }, + right: { type: 'constant', value: 6 }, + }) + }) + + test('2d(6+3)d4', () => { + expect(parse('2d(6+3)d4')).toEqual({ + type: 'd', + left: { type: 'constant', value: 2 }, + right: { + type: 'd', + left: { + type: 'add', + left: { type: 'constant', value: 6 }, + right: { type: 'constant', value: 3 } + }, + right: { type: 'constant', value: 4 } + }, + }) + }) + }) }) |