Skip to content

Commit 159c806

Browse files
committed
Add tests for pickling and exception wrapping
Also raise `NotImplementedError` when `DiceBaseException` wrap is used incorrectly.
1 parent 94fd26c commit 159c806

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

dice/exceptions.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ def from_other(cls, other):
88
return DiceException(*other.args)
99
elif isinstance(other, ParseFatalException):
1010
return DiceFatalException(*other.args)
11-
return cls(*other.args)
11+
raise NotImplementedError(
12+
'DiceBaseException can only wrap ParseException or ParseFatalException'
13+
)
1214

1315
def pretty_print(self):
1416
string, location, description = self.args

dice/tests/test_elements.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
from __future__ import absolute_import
22

33
from dice.constants import DiceExtreme
4-
from dice.exceptions import DiceFatalException
4+
from dice.exceptions import DiceException, DiceFatalException
5+
import pickle
56
from pytest import raises
67
import random
78

@@ -135,6 +136,13 @@ def test_roll_error(self):
135136
rolled = roll("6d6")
136137
rolled.do_roll_single(4, 3)
137138

139+
def test_invalid_wrap(self):
140+
with raises(NotImplementedError):
141+
try:
142+
raise RuntimeError('blah')
143+
except Exception as e:
144+
raise DiceException.from_other(e)
145+
138146

139147
class TestEvaluate(object):
140148
def test_cache(self):
@@ -191,3 +199,18 @@ def test_sysrandom_op(self):
191199

192200
def test_sysrandom_roll(self):
193201
roll("6d6", random=self.sysrandom)
202+
203+
204+
class TestPickle(object):
205+
for expr in [
206+
'-d20',
207+
'4d6t',
208+
'+-(1,2,3)',
209+
'2d20h',
210+
'4d6h3s',
211+
'4dF - 2',
212+
'4*d%'
213+
]:
214+
value = roll(expr, raw=True, single=False)
215+
pickled = pickle.dumps(value)
216+
clone = pickle.loads(pickled)

0 commit comments

Comments
 (0)