tests.hamcrest_unit_test.core.CallingTest   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 27
Duplicated Lines 0 %
Metric Value
wmc 6
dl 0
loc 27
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testCallingObjectCallsProvidedFunction() 0 4 1
A testCallingWithFunctionReturnsObject() 0 6 1
A testCallingWithFunctionSetsArgumentList() 0 6 1
A testCallingDoesNotImmediatelyExecuteFunction() 0 7 3
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
class RaisesTest(MatcherTest):
26
    def testMatchesIfFunctionRaisesTheExactExceptionExpected(self):
27
        self.assert_matches('Right exception',
28
                            raises(AssertionError),
29
                            calling(raise_exception))
30
31
    def testDoesNotMatchTypeErrorIfActualIsNotCallable(self):
32
        self.assert_does_not_match('Not callable',
33
                                   raises(TypeError),
34
                                   23)
35
36
    def testMatchesIfFunctionRaisesASubclassOfTheExpectedException(self):
37
        self.assert_matches('Subclassed Exception',
38
                            raises(Exception),
39
                            calling(raise_exception))
40
41
    def testDoesNotMatchIfFunctionDoesNotRaiseException(self):
42
        self.assert_does_not_match('No exception',
43
                            raises(ValueError),
44
                            calling(no_exception))
45
46
    def testDoesNotMatchExceptionIfRegularExpressionDoesNotMatch(self):
47
        self.assert_does_not_match('Bad regex',
48
                                   raises(AssertionError, "Phrase not found"),
49
                                   calling(raise_exception))
50
51
    def testMatchesRegularExpressionToStringifiedException(self):
52
        self.assert_matches('Regex',
53
                            raises(AssertionError, "(3, 1, 4)"),
54
                            calling(raise_exception).with_args(3,1,4))
55
56
        self.assert_matches('Regex',
57
                            raises(AssertionError, "([\d, ]+)"),
58
                            calling(raise_exception).with_args(3,1,4))
59
60
    def testDescribeMismatchWillCallItemIfNotTheOriginalMatch(self):
61
        function = Callable()
62
        matcher = raises(AssertionError)
63
        matcher.describe_mismatch(function, object())
64
        self.assertTrue(function.called)
65
66
class CallingTest(unittest.TestCase):
67
    def testCallingDoesNotImmediatelyExecuteFunction(self):
68
        try:
69
            calling(raise_exception)
70
        except AssertionError:
71
            self.fail()
72
        else:
73
            pass
74
75
    def testCallingObjectCallsProvidedFunction(self):
76
        method = Callable()
77
        calling(method)()
78
        self.assertTrue(method.called)
79
80
    def testCallingWithFunctionReturnsObject(self):
81
        method = Callable()
82
        callable = calling(method)
83
        returned = callable.with_args(3, 1, 4, keyword1="arg1")
84
85
        self.assertEqual(returned, callable)
86
87
    def testCallingWithFunctionSetsArgumentList(self):
88
        method = Callable()
89
        calling(method).with_args(3, 1, 4, keyword1="arg1")()
90
91
        self.assertEqual(method.args, (3, 1, 4))
92
        self.assertEqual(method.kwargs, {"keyword1": "arg1"})
93
94
95
class Callable(object):
96
    def __init__(self):
97
        self.called = False
98
99
    def __call__(self, *args, **kwargs):
100
        self.called = True
101
        self.args = args
102
        self.kwargs = kwargs
103