This repository was archived by the owner on Nov 2, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtest_exceptiongroup.py
84 lines (68 loc) · 2.6 KB
/
test_exceptiongroup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import copy
import pytest
from exceptiongroup import ExceptionGroup
def raise_group():
try:
1 / 0
except Exception as e:
raise ExceptionGroup("ManyError", [e], [str(e)]) from e
def test_exception_group_init():
memberA = ValueError("A")
memberB = RuntimeError("B")
group = ExceptionGroup(
"many error.", [memberA, memberB], [str(memberA), str(memberB)]
)
assert group.exceptions == [memberA, memberB]
assert group.message == "many error."
assert group.sources == [str(memberA), str(memberB)]
assert group.args == (
"many error.",
[memberA, memberB],
[str(memberA), str(memberB)],
)
def test_exception_group_when_members_are_not_exceptions():
with pytest.raises(TypeError):
ExceptionGroup(
"error",
[RuntimeError("RuntimeError"), "error2"],
["RuntimeError", "error2"],
)
def test_exception_group_init_when_exceptions_messages_not_equal():
with pytest.raises(ValueError):
ExceptionGroup(
"many error.", [ValueError("A"), RuntimeError("B")], ["A"]
)
def test_exception_group_str():
memberA = ValueError("memberA")
memberB = ValueError("memberB")
group = ExceptionGroup(
"many error.", [memberA, memberB], [str(memberA), str(memberB)]
)
assert "memberA" in str(group)
assert "memberB" in str(group)
assert "ExceptionGroup: " in repr(group)
assert "memberA" in repr(group)
assert "memberB" in repr(group)
def test_exception_group_copy():
try:
raise_group() # the exception is raise by `raise...from..`
except ExceptionGroup as e:
group = e
another_group = copy.copy(group)
assert another_group.message == group.message
assert another_group.exceptions == group.exceptions
assert another_group.sources == group.sources
assert another_group.__traceback__ is group.__traceback__
assert another_group.__cause__ is group.__cause__
assert another_group.__context__ is group.__context__
assert another_group.__suppress_context__ is group.__suppress_context__
assert another_group.__cause__ is not None
assert another_group.__context__ is not None
assert another_group.__suppress_context__ is True
# doing copy when __suppress_context__ is False
group.__suppress_context__ = False
another_group = copy.copy(group)
assert another_group.__cause__ is group.__cause__
assert another_group.__context__ is group.__context__
assert another_group.__suppress_context__ is group.__suppress_context__
assert another_group.__suppress_context__ is False