Skip to content

Allowed dice rolls with upper case D #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions spec/dice.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,11 @@ describe('Dice', () => {
const exp = dice.roll('2d10kl');
expect(exp.total).toBe(4);
});
it('correctly handles dice expressions with an upper case D (4D6)', () => {
const mock = new MockListRandomProvider([2, 3, 4, 5]);
const dice = new Dice(null, mock);
const exp = dice.roll('4D6');
expect(exp.total).toBe(14);
});
});
});
18 changes: 18 additions & 0 deletions spec/parser/dice-parser.parseDice.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,24 @@ describe('DiceParser', () => {
expect(dice.getChild(1).type).toBe(NodeType.DiceSides);
expect(dice.getChild(1).getAttribute('value')).toBe('fate');
});
it('can correctly parse a dice roll with an upper case D.', () => {
const lexer = new MockLexer([
new Token(TokenType.Number, 0, '4'),
new Token(TokenType.Identifier, 0, 'D'),
new Token(TokenType.Number, 1, '6')
]);
const parser = new Parser.DiceParser(lexer);
const result = new ParseResult();
const dice = parser.parseDice(result);
expect(result.errors.length).toBe(0);

expect(dice.type).toBe(NodeType.Dice);
expect(dice.getChildCount()).toBe(2);
expect(dice.getChild(0).type).toBe(NodeType.Number);
expect(dice.getChild(0).getAttribute('value')).toBe(4);
expect(dice.getChild(1).type).toBe(NodeType.DiceSides);
expect(dice.getChild(1).getAttribute('value')).toBe(6);
});
it('can correctly parse a simple dice roll with pre-parsed number.', () => {
const lexer = new MockLexer([
new Token(TokenType.Number, 0, '10'),
Expand Down
3 changes: 2 additions & 1 deletion src/parser/dice-parser.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export class DiceParser extends BasicParser {
const token = this.lexer.peekNextToken();
switch (token.type) {
case TokenType.Identifier:
if (token.value === 'd' || token.value === 'dF') {
if (token.value === 'd' || token.value === 'D' || token.value === 'dF') {
root = this.parseDice(result);
} else {
root = this.parseFunction(result);
Expand Down Expand Up @@ -215,6 +215,7 @@ export class DiceParser extends BasicParser {

switch (token.value) {
case 'd':
case 'D':
const sidesToken = this.expectAndConsume(result, TokenType.Number);
root.addChild(Ast.Factory.create(Ast.NodeType.DiceSides))
.setAttribute('value', Number(sidesToken.value));
Expand Down