Skip to content

Commit 3f31a2c

Browse files
committed
Cleanup and minor fixes
1 parent 091eff3 commit 3f31a2c

File tree

12 files changed

+52
-27
lines changed

12 files changed

+52
-27
lines changed

dice/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010

1111
__all__ = ['roll', 'ParseException']
1212
__author__ = "Sam Clements <[email protected]>"
13-
__version__ = '0.3.1'
13+
__version__ = '0.3.2'
14+
1415

1516
def roll(string, single=True, verbose=False):
1617
"""Parses and evaluates a dice expression"""

dice/command.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
__version__ = "dice v{0} by {1}".format(dice.__version__, dice.__author__)
1818

19+
1920
def main(argv=None):
2021
"""Run roll() from a command line interface"""
2122
args = docopt.docopt(__doc__, argv=argv, version=__version__)

dice/elements.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
from dice.utilities import classname
99

10+
1011
class Element(object):
1112
def evaluate(self, verbose=False):
1213
"""Evaluate the current object - a no-op by default"""

dice/grammar.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,16 @@
88

99
from __future__ import absolute_import, print_function, unicode_literals
1010

11-
from pyparsing import (CaselessLiteral, Forward, Literal, OneOrMore,
12-
StringStart, StringEnd, Suppress, Word, nums, opAssoc)
11+
from pyparsing import (
12+
CaselessLiteral, Forward, Literal, OneOrMore, StringStart, StringEnd,
13+
Suppress, Word, nums, opAssoc)
1314

1415
from dice.elements import Integer, Dice, Total, Mul, Div, Sub, Add
1516
from dice.utilities import patch_pyparsing
1617

1718
patch_pyparsing()
1819

20+
1921
def operatorPrecedence(base, operators):
2022
"""
2123
This re-implements pyparsing's operatorPrecedence function.

dice/tests/conftest.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from termcolor import colored
44

5+
56
def pytest_report_teststatus(report):
67
if report.when != "call":
78
return None, None, None

dice/tests/test_command.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11
from __future__ import absolute_import
22

3+
import itertools
4+
5+
from dice import roll
36
from dice.command import main
47
from dice.elements import Integer, Roll
58

69

10+
def test_roll():
11+
for single, verbose in itertools.product((True, False), (True, False)):
12+
assert roll('6d6', single=single, verbose=verbose)
13+
14+
715
def test_main():
816
assert isinstance(main(['2d6']), (Integer, Roll, list))
917

18+
1019
def test_main_verbose():
1120
assert main(['1d6', '--verbose'])

dice/tests/test_elements.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,25 @@
22

33
from dice.elements import Integer, Roll, Dice
44

5+
56
def test_integer():
67
assert isinstance(Integer(1), int)
78

9+
810
def test_dice_from_iterable():
911
d = Dice.from_iterable((2, 6))
1012
assert d.amount == 2 and d.sides == 6
1113

14+
1215
def test_dice_from_string():
13-
d = Dice.from_string("2d6")
16+
d = Dice.from_string('2d6')
1417
assert d.amount == 2 and d.sides == 6
1518

19+
20+
def test_dice_roll():
21+
assert isinstance(Dice(2, 6).roll(), Roll)
22+
23+
1624
def test_roll():
1725
amount, sides = 6, 6
1826
assert len(Roll.roll(amount, sides)) == amount

dice/tests/test_grammar.py

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
from dice.elements import Integer, Roll
44
from dice import roll
55

6+
67
class TestInteger(object):
78
def test_value(self):
8-
assert roll("1337") == 1337
9+
assert roll('1337') == 1337
910

1011
def test_type(self):
11-
assert isinstance(roll("1"), int)
12-
assert isinstance(roll("1"), Integer)
12+
assert isinstance(roll('1'), int)
13+
assert isinstance(roll('1'), Integer)
1314

1415

1516
class TestDice(object):
@@ -22,44 +23,44 @@ def test_dice_type(self):
2223
assert isinstance(roll('1d6'), Roll)
2324

2425
def test_dice_values(self):
25-
for die in roll("6d6"):
26+
for die in roll('6d6'):
2627
assert 0 < die <= 6
2728

2829

2930
class TestFunctionOperators(object):
3031
def test_add(self):
31-
assert roll("2 + 2") == 4
32+
assert roll('2 + 2') == 4
3233

3334
def test_sub(self):
34-
assert roll("2 - 2") == 0
35+
assert roll('2 - 2') == 0
3536

3637
def test_mul(self):
37-
assert roll("2 * 2") == 4
38+
assert roll('2 * 2') == 4
3839

3940
def test_div(self):
40-
assert roll("2 / 2") == 1
41+
assert roll('2 / 2') == 1
4142

4243
def test_total(self):
43-
assert (6 * 1) <= roll("6d6t") <= (6 * 6)
44+
assert (6 * 1) <= roll('6d6t') <= (6 * 6)
4445

4546

46-
def TestOperatorPrecedence(object):
47+
class TestOperatorPrecedence(object):
4748
def test_operator_precedence_1(self):
48-
assert roll("16 / 8 * 4 + 2 - 1") == 9
49+
assert roll('16 / 8 * 4 + 2 - 1') == 9
4950

5051
def test_operator_precedence_2(self):
51-
assert roll("16 - 8 + 4 * 2 / 1") == 16
52+
assert roll('16 - 8 + 4 * 2 / 1') == 16
5253

5354
def test_operator_precedence_3(self):
54-
assert roll("10 - 3 + 2") == 9
55+
assert roll('10 - 3 + 2') == 9
5556

5657
def test_operator_precedence_4(self):
57-
assert roll("1 + 2 * 3") == 7
58+
assert roll('1 + 2 * 3') == 7
5859

5960

6061
class TestExpression(object):
6162
def test_expression(self):
62-
assert isinstance(roll("2d6"), Roll)
63+
assert isinstance(roll('2d6'), Roll)
6364

6465
def test_sub_expression(self):
65-
assert isinstance(roll("(2d6)d(2d6)"), Roll)
66+
assert isinstance(roll('(2d6)d(2d6)'), Roll)

dice/tests/test_utilities.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from py.test import raises
66

7+
78
def test_enable_pyparsing_packrat_parsing():
89
"""Test that packrat parsing was enabled"""
910
import pyparsing
@@ -21,6 +22,6 @@ def test_disable_pyparsing_arity_trimming_works():
2122
"""Tests that arity trimming has been disabled and parse actions with
2223
the wrong number of arguments will raise TypeErrors"""
2324
for func in [lambda a: None, lambda a, b: None, lambda a, b, c, d: None]:
24-
element = Literal("test").setParseAction(func)
25+
element = Literal('test').setParseAction(func)
2526
with raises(TypeError):
26-
element.parseString("test")
27+
element.parseString('test')

dice/utilities.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import pyparsing
66

7+
78
def classname(obj):
89
"""Returns the name of an objects class"""
910
return obj.__class__.__name__

0 commit comments

Comments
 (0)