UNPKG

owlbear

Version:

A simple dice notation parser.

165 lines (128 loc) 3.23 kB
should = require 'should' Owlbear = require '../src' describe 'Parser', ()-> defaultParser = null dumbParser = null beforeEach ()-> defaultParser = new Owlbear() dumbParser = new Owlbear operators: [] describe 'parsing', ()-> it 'should parse and compose complex notation', ()-> notation = '1d4 * 1d10k.5>>>' a = defaultParser.parse notation a.should.have.length 3 b = [ { count: 1 chain: 0 keep: 1 die: sides: 4 reroll: [] explode: [] }, { operator: '*' }, { count: 1 chain: 3 keep: .5 die: sides: 10 reroll: [] explode: [] } ] a.should.eql b x = defaultParser.compose b x.should.eql '1d4*1d10k0.5>>>' it 'should parse single dice terms', ()-> notation = '5d10' a = defaultParser.parseTerm notation a.should.eql count: 5 chain: 0 keep: 5 die: sides: 10 reroll: [] explode: [] it 'should parse simple notation', ()-> notation = '5d10' a = defaultParser.parse notation a.should.have.length 1 a[0].should.eql count: 5 chain: 0 keep: 5 die: sides: 10 reroll: [] explode: [] it 'should parse single, complex dice terms', ()-> notation = '5d10R!' a = defaultParser.parseTerm notation a.should.eql count: 5 chain: 0 keep: 5 die: sides: 10 reroll: [1] explode: [10] it 'should parse single, more complex dice terms', ()-> notation = '5d10R!x5.5,2.2<r1.5k2.4>>><' a = defaultParser.parseTerm notation a.should.eql count: 5 chain: 1 keep: 2.4 die: sides: 10 reroll: [1, 1.5] explode: [10, 5.5, 2.2] it 'should parse single dice terms with "keep"', ()-> notation = '5d10k1' a = defaultParser.parseTerm notation a.should.eql count: 5 chain: 0 keep: 1 die: sides: 10 reroll: [] explode: [] it 'should parse single wacky, floating point dice terms', ()-> notation = '5.25d10.5' a = defaultParser.parseTerm notation a.should.eql count: 5.25 chain: 0 keep: 5.25 die: sides: 10.5 reroll: [] explode: [] it 'should parse single wacky, floating point dice terms with keep', ()-> notation = '5.25d10.5k5.1' a = defaultParser.parseTerm notation a.should.eql count: 5.25 chain: 0 keep: 5.1 die: sides: 10.5 reroll: [] explode: [] it 'should parse constants', ()-> notation = '4' a = defaultParser.parseTerm notation a.should.eql constant: 4 it 'should parse floating point constants', ()-> notation = '4.1' a = defaultParser.parseTerm notation a.should.eql constant: 4.1