-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathtest_common.py
More file actions
116 lines (90 loc) Β· 3.59 KB
/
Copy pathtest_common.py
File metadata and controls
116 lines (90 loc) Β· 3.59 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
"""
Tests for hyperlink.test.common
"""
from typing import Any
from unittest import TestCase
from .common import HyperlinkTestCase
class _ExpectedException(Exception):
"""An exception used to test HyperlinkTestCase.assertRaises."""
class _UnexpectedException(Exception):
"""An exception used to test HyperlinkTestCase.assertRaises."""
class TestHyperlink(TestCase):
"""Tests for HyperlinkTestCase"""
def setUp(self):
# type: () -> None
self.hyperlink_test = HyperlinkTestCase("run")
def test_assertRaisesWithCallable(self):
# type: () -> None
"""HyperlinkTestCase.assertRaises does not raise an AssertionError
when given a callable that, when called with the provided
arguments, raises the expected exception.
"""
called_with = []
def raisesExpected(*args, **kwargs):
# type: (Any, Any) -> None
called_with.append((args, kwargs))
raise _ExpectedException
self.hyperlink_test.assertRaises(
_ExpectedException, raisesExpected, 1, keyword=True
)
self.assertEqual(called_with, [((1,), {"keyword": True})])
def test_assertRaisesWithCallableUnexpectedException(self):
# type: () -> None
"""When given a callable that raises an unexpected exception,
HyperlinkTestCase.assertRaises raises that exception.
"""
def doesNotRaiseExpected(*args, **kwargs):
# type: (Any, Any) -> None
raise _UnexpectedException
try:
self.hyperlink_test.assertRaises(
_ExpectedException, doesNotRaiseExpected
)
except _UnexpectedException:
pass
def test_assertRaisesWithCallableDoesNotRaise(self):
# type: () -> None
"""HyperlinkTestCase.assertRaises raises an AssertionError when given
a callable that, when called, does not raise any exception.
"""
def doesNotRaise(*args, **kwargs):
# type: (Any, Any) -> None
pass
try:
self.hyperlink_test.assertRaises(_ExpectedException, doesNotRaise)
except AssertionError:
pass
def test_assertRaisesContextManager(self):
# type: () -> None
"""HyperlinkTestCase.assertRaises does not raise an AssertionError
when used as a context manager with a suite that raises the
expected exception. The context manager stores the exception
instance under its `exception` instance variable.
"""
with self.hyperlink_test.assertRaises(_ExpectedException) as cm:
raise _ExpectedException
self.assertTrue( # type: ignore[unreachable]
isinstance(cm.exception, _ExpectedException)
)
def test_assertRaisesContextManagerUnexpectedException(self):
# type: () -> None
"""When used as a context manager with a block that raises an
unexpected exception, HyperlinkTestCase.assertRaises raises
that unexpected exception.
"""
try:
with self.hyperlink_test.assertRaises(_ExpectedException):
raise _UnexpectedException
except _UnexpectedException:
pass
def test_assertRaisesContextManagerDoesNotRaise(self):
# type: () -> None
"""HyperlinkTestcase.assertRaises raises an AssertionError when used
as a context manager with a block that does not raise any
exception.
"""
try:
with self.hyperlink_test.assertRaises(_ExpectedException):
pass
except AssertionError:
pass