Skip to content

Commit 40ae6a1

Browse files
committed
add new test_utitilies.py file
1 parent a444429 commit 40ae6a1

File tree

1 file changed

+93
-0
lines changed

1 file changed

+93
-0
lines changed

symengine/test_utilities.py

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
try:
2+
import py
3+
from py.test import skip, raises
4+
USE_PYTEST = getattr(sys, '_running_pytest', False)
5+
except ImportError:
6+
USE_PYTEST = False
7+
8+
if not USE_PYTEST:
9+
def raises(expectedException, code=None):
10+
"""
11+
Tests that ``code`` raises the exception ``expectedException``.
12+
13+
``code`` may be a callable, such as a lambda expression or function
14+
name.
15+
16+
If ``code`` is not given or None, ``raises`` will return a context
17+
manager for use in ``with`` statements; the code to execute then
18+
comes from the scope of the ``with``.
19+
20+
``raises()`` does nothing if the callable raises the expected
21+
exception, otherwise it raises an AssertionError.
22+
23+
Examples
24+
========
25+
26+
>>> from symengine.pytest import raises
27+
28+
>>> raises(ZeroDivisionError, lambda: 1/0)
29+
>>> raises(ZeroDivisionError, lambda: 1/2)
30+
Traceback (most recent call last):
31+
...
32+
AssertionError: DID NOT RAISE
33+
34+
>>> with raises(ZeroDivisionError):
35+
... n = 1/0
36+
>>> with raises(ZeroDivisionError):
37+
... n = 1/2
38+
Traceback (most recent call last):
39+
...
40+
AssertionError: DID NOT RAISE
41+
42+
Note that you cannot test multiple statements via
43+
``with raises``:
44+
45+
>>> with raises(ZeroDivisionError):
46+
... n = 1/0 # will execute and raise, aborting the ``with``
47+
... n = 9999/0 # never executed
48+
49+
This is just what ``with`` is supposed to do: abort the
50+
contained statement sequence at the first exception and let
51+
the context manager deal with the exception.
52+
53+
To test multiple statements, you'll need a separate ``with``
54+
for each:
55+
56+
>>> with raises(ZeroDivisionError):
57+
... n = 1/0 # will execute and raise
58+
>>> with raises(ZeroDivisionError):
59+
... n = 9999/0 # will also execute and raise
60+
61+
"""
62+
if code is None:
63+
return RaisesContext(expectedException)
64+
elif callable(code):
65+
try:
66+
code()
67+
except expectedException:
68+
return
69+
raise AssertionError("DID NOT RAISE")
70+
elif isinstance(code, str):
71+
raise TypeError(
72+
'\'raises(xxx, "code")\' has been phased out; '
73+
'change \'raises(xxx, "expression")\' '
74+
'to \'raises(xxx, lambda: expression)\', '
75+
'\'raises(xxx, "statement")\' '
76+
'to \'with raises(xxx): statement\'')
77+
else:
78+
raise TypeError(
79+
'raises() expects a callable for the 2nd argument.')
80+
81+
class RaisesContext(object):
82+
def __init__(self, expectedException):
83+
self.expectedException = expectedException
84+
85+
def __enter__(self):
86+
return None
87+
88+
def __exit__(self, exc_type, exc_value, traceback):
89+
if exc_type is None:
90+
raise AssertionError("DID NOT RAISE")
91+
return issubclass(exc_type, self.expectedException)
92+
93+

0 commit comments

Comments
 (0)