Skip to content

Commit d4fc02f

Browse files
committed
Corrected parsing
1 parent 24338c8 commit d4fc02f

11 files changed

Lines changed: 37 additions & 210 deletions

README.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,11 @@ And then use it to parse a dice notation expression::
100100
parser = DiceParser()
101101
dice = parser.parse('1d6+2')
102102

103-
The result can be accessed just by calling the 'roll' method as many times as
103+
The result can be accessed just by calling the 'value' method as many times as
104104
needed, which will generate a new random value each time it is called::
105105

106-
print(dice.roll())
107-
print(dice.roll())
106+
print(dice.value())
107+
print(dice.value())
108108

109109
Testing
110110
-------

dice_notation/algebra.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# -*- coding: utf-8 -*-
22

3-
from abc import ABCMeta, abstractmethod
4-
from random import randint
3+
from dice_notation.dice import Rollable
54

65
"""
76
Algebraic classes.
@@ -13,7 +12,7 @@
1312
__license__ = 'MIT'
1413

1514

16-
class BinaryOperation(object):
15+
class BinaryOperation(Rollable):
1716
"""
1817
A binary operation. Matching an operator with two operands.
1918
"""
@@ -32,8 +31,8 @@ def __repr__(self):
3231
return '<class %s>(left=%r, right=%r, operator=%r)' % \
3332
(self.__class__.__name__, self._left, self._right, self._operator)
3433

35-
def value(self):
36-
return self._operation(self._left.value(), self._right.value())
34+
def roll(self):
35+
return self._operation(self._left.roll(), self._right.roll())
3736

3837
@property
3938
def left(self):
@@ -75,7 +74,7 @@ def operator(self, operator):
7574
self._operator = operator
7675

7776

78-
class Number(object):
77+
class Number(Rollable):
7978
"""
8079
A numeric constant
8180
"""
@@ -91,5 +90,5 @@ def __repr__(self):
9190
return '<class %s>(value=%r)' % \
9291
(self.__class__.__name__, self._value)
9392

94-
def value(self):
93+
def roll(self):
9594
return self._value

dice_notation/parser/dice.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,4 @@ def parse(self, input):
2727

2828
expression = listener.expression()
2929
self._logger.debug("Parsed expression %s", expression)
30-
return expression.value()
30+
return expression

tests/parser/dice/test_dice_parser_dice_rollable.py

Lines changed: 0 additions & 49 deletions
This file was deleted.

tests/parser/number/test_dice_parser_number.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,22 @@ def test_positive(self):
2727
"""
2828
Tests that positive numbers can be parsed.
2929
"""
30-
result = self.parser.parse("5")
30+
result = self.parser.parse("5").roll()
3131

3232
self.assertEqual(5, result)
3333

3434
def test_zero(self):
3535
"""
3636
Tests that the zero value can be parsed.
3737
"""
38-
result = self.parser.parse("0")
38+
result = self.parser.parse("0").roll()
3939

4040
self.assertEqual(0, result)
4141

4242
def test_negative(self):
4343
"""
4444
Tests that negative numbers can be parsed.
4545
"""
46-
result = self.parser.parse("-5")
46+
result = self.parser.parse("-5").roll()
4747

48-
# TODO: currently not supported
49-
# self.assertEqual(-5, result)
48+
self.assertEqual(-5, result)

tests/parser/number/test_dice_parser_number_add.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,15 @@ def test_add(self):
2727
"""
2828
Tests that additions can be parsed, and the result is the expected one.
2929
"""
30-
result = self.parser.parse("1+2")
30+
result = self.parser.parse("1+2").roll()
3131

3232
self.assertEqual(3, result)
3333

3434
def test_add_toNegative(self):
3535
"""
3636
Tests that additions to a negative value can be parsed, and the result is the expected one.
3737
"""
38-
result = self.parser.parse("-1+2")
38+
result = self.parser.parse("-1+2").roll()
3939

4040
self.assertEqual(1, result)
4141

@@ -55,14 +55,14 @@ def test_longAdd(self):
5555
"""
5656
Tests that long additions can be parsed, and the result is the expected one.
5757
"""
58-
result = self.parser.parse("1+2+3")
58+
result = self.parser.parse("1+2+3").roll()
5959

6060
self.assertEqual(6, result)
6161

6262
def test_longerAdd(self):
6363
"""
6464
Tests that longer additions can be parsed, and the result is the expected one.
6565
"""
66-
result = self.parser.parse("1+2+3+4+5")
66+
result = self.parser.parse("1+2+3+4+5").roll()
6767

6868
self.assertEqual(15, result)

tests/parser/number/test_dice_parser_number_binary_op_mixed.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@ def test_addAndSub(self):
2929
"""
3030
Tests that additions followed by subtractions can be parsed, and the result is the expected one.
3131
"""
32-
result = self.parser.parse("1+2-3")
32+
result = self.parser.parse("1+2-3").roll()
3333

3434
self.assertEqual(0, result)
3535

3636
def test_subAndAdd(self):
3737
"""
3838
Tests that subtractions followed by additions can be parsed, and the result is the expected one.
3939
"""
40-
result = self.parser.parse("3-1+2")
40+
result = self.parser.parse("3-1+2").roll()
4141

4242
self.assertEqual(4, result)

tests/parser/number/test_dice_parser_number_rollable.py

Lines changed: 6 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -13,42 +13,6 @@
1313
__license__ = 'MIT'
1414

1515

16-
class TestRollable(unittest.TestCase):
17-
"""
18-
Tests that numeric expressions return instances of Rollable.
19-
"""
20-
21-
def setUp(self):
22-
"""
23-
Initializes parser.
24-
"""
25-
self.parser = DiceParser()
26-
27-
def test_number_rollable(self):
28-
"""
29-
Tests that parsing numbers returns a Rollable.
30-
"""
31-
result = self.parser.parse("1")
32-
33-
self.assertTrue(isinstance(result, Rollable))
34-
35-
def test_add_rollable(self):
36-
"""
37-
Tests that parsing numeric additions returns a Rollable.
38-
"""
39-
result = self.parser.parse("1+2")
40-
41-
self.assertTrue(isinstance(result, Rollable))
42-
43-
def test_sub_rollable(self):
44-
"""
45-
Tests that parsing numeric subtractions returns a Rollable.
46-
"""
47-
result = self.parser.parse("1-2")
48-
49-
self.assertTrue(isinstance(result, Rollable))
50-
51-
5216
class TestRoll(unittest.TestCase):
5317
"""
5418
Tests that rolling the result from parsing numeric expressions returns the expected value.
@@ -64,22 +28,22 @@ def test_number_roll(self):
6428
"""
6529
Tests that rolling a parsed number returns the expected value.
6630
"""
67-
result = self.parser.parse("1")
31+
result = self.parser.parse("1").roll()
6832

69-
self.assertEqual(1, result.roll())
33+
self.assertEqual(1, result)
7034

7135
def test_add_roll(self):
7236
"""
7337
Tests that rolling a parsed numeric addition returns the expected value.
7438
"""
75-
result = self.parser.parse("1+2")
39+
result = self.parser.parse("1+2").roll()
7640

77-
self.assertEqual(3, result.roll())
41+
self.assertEqual(3, result)
7842

7943
def test_sub_roll(self):
8044
"""
8145
Tests that rolling a parsed numeric subtraction returns the expected value.
8246
"""
83-
result = self.parser.parse("1-2")
47+
result = self.parser.parse("1-2").roll()
8448

85-
self.assertEqual(-1, result.roll())
49+
self.assertEqual(-1, result)

tests/parser/number/test_dice_parser_number_rollable_mixed.py

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -13,34 +13,6 @@
1313
__license__ = 'MIT'
1414

1515

16-
class TestRollableMixedBinaryOp(unittest.TestCase):
17-
"""
18-
Tests that mixed numeric binary operation expressions return instances of Rollable.
19-
"""
20-
21-
def setUp(self):
22-
"""
23-
Initializes parser.
24-
"""
25-
self.parser = DiceParser()
26-
27-
def test_addAndSub_rollable(self):
28-
"""
29-
Tests that parsing numeric additions followed by subtractions returns a Rollable.
30-
"""
31-
result = self.parser.parse("1+2-3")
32-
33-
self.assertTrue(isinstance(result, Rollable))
34-
35-
def test_subAndAdd_rollable(self):
36-
"""
37-
Tests that parsing numeric subtractions followed by additions returns a Rollable.
38-
"""
39-
result = self.parser.parse("3-1+2")
40-
41-
self.assertTrue(isinstance(result, Rollable))
42-
43-
4416
class TestRollMixedBinaryOp(unittest.TestCase):
4517
"""
4618
Tests that rolling the result from parsing mixed numeric binary operation expressions returns the expected value.
@@ -56,14 +28,14 @@ def test_addAndSub_roll(self):
5628
"""
5729
Tests that rolling a parsed numeric additions followed by subtractions returns the expected value.
5830
"""
59-
result = self.parser.parse("1+2-3")
31+
result = self.parser.parse("1+2-3").roll()
6032

61-
self.assertEqual(0, result.roll())
33+
self.assertEqual(0, result)
6234

6335
def test_subAndAdd_roll(self):
6436
"""
6537
Tests that rolling a parsed numeric subtractions followed by additions returns the expected value.
6638
"""
67-
result = self.parser.parse("3-1+2")
39+
result = self.parser.parse("3-1+2").roll()
6840

69-
self.assertEqual(4, result.roll())
41+
self.assertEqual(4, result)

tests/parser/number/test_dice_parser_number_sub.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,34 +27,33 @@ def test_sub_positive(self):
2727
"""
2828
Tests that subtractions can be parsed, and the result is the expected one.
2929
"""
30-
result = self.parser.parse("2-1")
30+
result = self.parser.parse("2-1").roll()
3131

3232
self.assertEqual(1, result)
3333

3434
def test_sub_negative(self):
3535
"""
3636
Tests that subtractions ending in a negative value can be parsed, and the result is the expected one.
3737
"""
38-
result = self.parser.parse("1-2")
38+
result = self.parser.parse("1-2").roll()
3939

4040
self.assertEqual(-1, result)
4141

4242
def test_sub_zero(self):
4343
"""
4444
Tests that subtractions ending in zero can be parsed, and the result is the expected one.
4545
"""
46-
result = self.parser.parse("1-1")
46+
result = self.parser.parse("1-1").roll()
4747

4848
self.assertEqual(0, result)
4949

5050
def test_sub_negatives(self):
5151
"""
5252
Tests that subtractions of negative values can be parsed, and the result is the expected one.
5353
"""
54-
result = self.parser.parse("-1-1")
54+
result = self.parser.parse("-1-1").roll()
5555

56-
# TODO: Currently not supported
57-
# self.assertEqual(-2, result)
56+
self.assertEqual(-2, result)
5857

5958

6059
class TestSubLong(unittest.TestCase):
@@ -72,7 +71,7 @@ def test_longSub(self):
7271
"""
7372
Tests that long subtractions can be parsed, and the result is the expected one.
7473
"""
75-
result = self.parser.parse("1-2-3")
74+
result = self.parser.parse("1-2-3").roll()
7675

7776
# TODO: Maybe it should be "-1-2-3"
7877

@@ -82,7 +81,7 @@ def test_longerSub(self):
8281
"""
8382
Tests that longer subtractions can be parsed, and the result is the expected one.
8483
"""
85-
result = self.parser.parse("1-2-3-4-5")
84+
result = self.parser.parse("1-2-3-4-5").roll()
8685

8786
# TODO: Maybe it should be "-1-2-3-4-5"
8887

0 commit comments

Comments
 (0)