Completed
Pull Request — master (#80)
by
unknown
40s
created

CallingTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 27
rs 10
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testCallingObjectCallsProvidedFunction() 0 4 1
A testCallingDoesNotImmediatelyExecuteFunction() 0 7 3
A testCallingWithFunctionSetsArgumentList() 0 6 1
A testCallingWithFunctionReturnsObject() 0 6 1
1
if __name__ == '__main__':
2
    import sys
3
    sys.path.insert(0, '..')
4
    sys.path.insert(0, '../..')
5
6
from hamcrest.core.core.raises import *
7
8
from hamcrest.core.core.isequal import equal_to
9
from hamcrest_unit_test.matcher_test import MatcherTest
10
import unittest
11
12
__author__ = "Per Fagrell"
13
__copyright__ = "Copyright 2013 hamcrest.org"
14
__license__ = "BSD, see License.txt"
15
16
17
def no_exception(*args, **kwargs):
18
    return
19
20
21
def raise_exception(*args, **kwargs):
22
    raise AssertionError(str(args) + str(kwargs))
23
24
25
def raise_baseException(*args, **kwargs):
26
    raise SystemExit(str(args) + str(kwargs))
27
28
29
class RaisesTest(MatcherTest):
30
    def testMatchesIfFunctionRaisesTheExactExceptionExpected(self):
31
        self.assert_matches('Right exception',
32
                            raises(AssertionError),
33
                            calling(raise_exception))
34
35
    def testDoesNotMatchTypeErrorIfActualIsNotCallable(self):
36
        self.assert_does_not_match('Not callable',
37
                                   raises(TypeError),
38
                                   23)
39
40
    def testMatchesIfFunctionRaisesASubclassOfTheExpectedException(self):
41
        self.assert_matches('Subclassed Exception',
42
                            raises(Exception),
43
                            calling(raise_exception))
44
45
    def testMatchesIfFunctionRaisesASubclassOfTheExpectedBaseException(self):
46
        self.assert_matches('Subclassed BasedException',
47
                            raises(BaseException),
48
                            calling(raise_baseException))
49
50
    def testDoesNotMatchIfFunctionDoesNotRaiseException(self):
51
        self.assert_does_not_match('No exception',
52
                            raises(ValueError),
53
                            calling(no_exception))
54
55
    def testDoesNotMatchExceptionIfRegularExpressionDoesNotMatch(self):
56
        self.assert_does_not_match('Bad regex',
57
                                   raises(AssertionError, "Phrase not found"),
58
                                   calling(raise_exception))
59
60
    def testMatchesRegularExpressionToStringifiedException(self):
61
        self.assert_matches('Regex',
62
                            raises(AssertionError, "(3, 1, 4)"),
63
                            calling(raise_exception).with_args(3,1,4))
64
65
        self.assert_matches('Regex',
66
                            raises(AssertionError, "([\d, ]+)"),
67
                            calling(raise_exception).with_args(3,1,4))
68
69
    def testDescribeMismatchWillCallItemIfNotTheOriginalMatch(self):
70
        function = Callable()
71
        matcher = raises(AssertionError)
72
        matcher.describe_mismatch(function, object())
73
        self.assertTrue(function.called)
74
75
class CallingTest(unittest.TestCase):
76
    def testCallingDoesNotImmediatelyExecuteFunction(self):
77
        try:
78
            calling(raise_exception)
79
        except AssertionError:
80
            self.fail()
81
        else:
82
            pass
83
84
    def testCallingObjectCallsProvidedFunction(self):
85
        method = Callable()
86
        calling(method)()
87
        self.assertTrue(method.called)
88
89
    def testCallingWithFunctionReturnsObject(self):
90
        method = Callable()
91
        callable = calling(method)
92
        returned = callable.with_args(3, 1, 4, keyword1="arg1")
93
94
        self.assertEqual(returned, callable)
95
96
    def testCallingWithFunctionSetsArgumentList(self):
97
        method = Callable()
98
        calling(method).with_args(3, 1, 4, keyword1="arg1")()
99
100
        self.assertEqual(method.args, (3, 1, 4))
101
        self.assertEqual(method.kwargs, {"keyword1": "arg1"})
102
103
104
class Callable(object):
105
    def __init__(self):
106
        self.called = False
107
108
    def __call__(self, *args, **kwargs):
109
        self.called = True
110
        self.args = args
111
        self.kwargs = kwargs
112