Skip to content

Commit 762a4e5

Browse files
committed
Parsing the grammar generates a rollable dice
1 parent 6c97e9d commit 762a4e5

2 files changed

Lines changed: 35 additions & 2 deletions

File tree

dice_notation/parser/DiceNotationListener.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from antlr4 import *
33

44
import logging
5-
from dice_notation.dice import Dice
5+
from dice_notation.dice import RollableDice
66
from dice_notation.algebra import BinaryOperation, Number
77

88
# This class defines a complete listener for a parse tree produced by DiceNotationParser.
@@ -125,7 +125,7 @@ def exitDice(self, ctx):
125125

126126
sides = int(next(digits).getText())
127127

128-
dice = Dice(quantity, sides)
128+
dice = RollableDice(quantity, sides)
129129
self._nodes.append(dice)
130130

131131

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# -*- coding: utf-8 -*-
2+
3+
import sys
4+
import unittest
5+
6+
from dice_notation.parser import DiceParser
7+
8+
"""
9+
Dice parser tests for expressions only containing dice.
10+
"""
11+
12+
__author__ = 'Bernardo Martínez Garrido'
13+
__license__ = 'MIT'
14+
15+
16+
class TestSimpleDice(unittest.TestCase):
17+
"""
18+
Tests that simple dice expressions can be parsed.
19+
"""
20+
21+
def setUp(self):
22+
"""
23+
Initializes parser.
24+
"""
25+
self.parser = DiceParser()
26+
27+
def test_minimalDice(self):
28+
"""
29+
Tests that the minimal dice can be rolled.
30+
"""
31+
result = self.parser.parse("1d1").roll()
32+
33+
self.assertEqual(1, result)

0 commit comments

Comments
 (0)