Skip to content

Commit

Permalink
Add tests for pickling and exception wrapping
Browse files Browse the repository at this point in the history
Also raise `NotImplementedError` when `DiceBaseException` wrap is used
incorrectly.
  • Loading branch information
calebj committed Apr 7, 2021
1 parent 94fd26c commit 159c806
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
4 changes: 3 additions & 1 deletion dice/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ def from_other(cls, other):
return DiceException(*other.args)
elif isinstance(other, ParseFatalException):
return DiceFatalException(*other.args)
return cls(*other.args)
raise NotImplementedError(
'DiceBaseException can only wrap ParseException or ParseFatalException'
)

def pretty_print(self):
string, location, description = self.args
Expand Down
25 changes: 24 additions & 1 deletion dice/tests/test_elements.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from __future__ import absolute_import

from dice.constants import DiceExtreme
from dice.exceptions import DiceFatalException
from dice.exceptions import DiceException, DiceFatalException
import pickle
from pytest import raises
import random

Expand Down Expand Up @@ -135,6 +136,13 @@ def test_roll_error(self):
rolled = roll("6d6")
rolled.do_roll_single(4, 3)

def test_invalid_wrap(self):
with raises(NotImplementedError):
try:
raise RuntimeError('blah')
except Exception as e:
raise DiceException.from_other(e)


class TestEvaluate(object):
def test_cache(self):
Expand Down Expand Up @@ -191,3 +199,18 @@ def test_sysrandom_op(self):

def test_sysrandom_roll(self):
roll("6d6", random=self.sysrandom)


class TestPickle(object):
for expr in [
'-d20',
'4d6t',
'+-(1,2,3)',
'2d20h',
'4d6h3s',
'4dF - 2',
'4*d%'
]:
value = roll(expr, raw=True, single=False)
pickled = pickle.dumps(value)
clone = pickle.loads(pickled)

0 comments on commit 159c806

Please sign in to comment.