File: arg_matching.py

package info (click to toggle)
python-flexmock 0.12.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 460 kB
  • sloc: python: 3,802; makefile: 17; sh: 14
file content (256 lines) | stat: -rw-r--r-- 10,909 bytes parent folder | download
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
"""Tests for argument matching."""

# pylint: disable=missing-docstring,no-member
import re

from flexmock import exceptions, flexmock
from flexmock._api import flexmock_teardown
from tests import some_module
from tests.some_module import SomeClass
from tests.utils import assert_raises


class ArgumentMatchingTestCase:
    def test_arg_matching_works_with_regexp(self):
        class Foo:
            def method(self, arg1, arg2):
                pass

        instance = Foo()
        flexmock(instance).should_receive("method").with_args(
            re.compile("^arg1.*asdf$"), arg2=re.compile("f")
        ).and_return("mocked")
        assert instance.method("arg1somejunkasdf", arg2="aadsfdas") == "mocked"

    def test_arg_matching_with_regexp_fails_when_regexp_doesnt_match_karg(self):
        class Foo:
            def method(self, arg1, arg2):
                pass

        instance = Foo()
        flexmock(instance).should_receive("method").with_args(
            re.compile("^arg1.*asdf$"), arg2=re.compile("a")
        ).and_return("mocked")
        with assert_raises(
            exceptions.MethodSignatureError,
            (
                "Arguments for call method did not match expectations:\n"
                '  Received call:\tmethod("arg1somejunkasdfa", arg2="a")\n'
                "  Expected call[1]:\tmethod(arg2=/a/, arg1=/^arg1.*asdf$/)"
            ),
        ):
            instance.method("arg1somejunkasdfa", arg2="a")

    def test_arg_matching_with_regexp_fails_when_regexp_doesnt_match_kwarg(self):
        class Foo:
            def method(self, arg1, arg2):
                pass

        instance = Foo()
        flexmock(instance).should_receive("method").with_args(
            re.compile("^arg1.*asdf$"), arg2=re.compile("a")
        ).and_return("mocked")
        with assert_raises(
            exceptions.MethodSignatureError,
            (
                "Arguments for call method did not match expectations:\n"
                '  Received call:\tmethod("arg1somejunkasdf", arg2="b")\n'
                "  Expected call[1]:\tmethod(arg2=/a/, arg1=/^arg1.*asdf$/)"
            ),
        ):
            instance.method("arg1somejunkasdf", arg2="b")

    def test_module_level_function_with_kwargs(self):
        flexmock(some_module).should_receive("module_function").with_args(1, y="expected")
        with assert_raises(
            exceptions.FlexmockError,
            (
                "Arguments for call module_function did not match expectations:\n"
                '  Received call:\tmodule_function(1, y="not expected")\n'
                '  Expected call[1]:\tmodule_function(y="expected", x=1)'
            ),
        ):
            some_module.module_function(1, y="not expected")

    def test_flexmock_should_match_types_on_multiple_arguments(self):
        class Foo:
            def method(self, arg1, arg2):
                pass

        instance = Foo()
        flexmock(instance).should_receive("method").with_args(str, int).and_return("ok")
        assert instance.method("some string", 12) == "ok"
        with assert_raises(
            exceptions.MethodSignatureError,
            (
                "Arguments for call method did not match expectations:\n"
                "  Received call:\tmethod(12, 32)\n"
                "  Expected call[1]:\tmethod(arg1=<class 'str'>, arg2=<class 'int'>)"
            ),
        ):
            instance.method(12, 32)
        flexmock(instance).should_receive("method").with_args(str, int).and_return("ok")
        with assert_raises(
            exceptions.MethodSignatureError,
            (
                "Arguments for call method did not match expectations:\n"
                '  Received call:\tmethod(12, "some string")\n'
                "  Expected call[1]:\tmethod(arg1=<class 'str'>, arg2=<class 'int'>)\n"
                "  Expected call[2]:\tmethod(arg1=<class 'str'>, arg2=<class 'int'>)"
            ),
        ):
            instance.method(12, "some string")
        flexmock(instance).should_receive("method").with_args(str, int).and_return("ok")
        with assert_raises(
            exceptions.MethodSignatureError,
            (
                "Arguments for call method did not match expectations:\n"
                '  Received call:\tmethod("string", 12, 14)\n'
                "  Expected call[1]:\tmethod(arg1=<class 'str'>, arg2=<class 'int'>)\n"
                "  Expected call[2]:\tmethod(arg1=<class 'str'>, arg2=<class 'int'>)\n"
                "  Expected call[3]:\tmethod(arg1=<class 'str'>, arg2=<class 'int'>)"
            ),
        ):
            instance.method("string", 12, 14)

    def test_flexmock_should_match_types_on_multiple_arguments_generic(self):
        class Foo:
            def method(self, a, b, c):  # pylint: disable=invalid-name
                pass

        instance = Foo()
        flexmock(instance).should_receive("method").with_args(object, object, object).and_return(
            "ok"
        )
        assert instance.method("some string", None, 12) == "ok"
        assert instance.method((1,), None, 12) == "ok"
        assert instance.method(12, 14, []) == "ok"
        assert instance.method("some string", "another one", False) == "ok"
        with assert_raises(
            exceptions.MethodSignatureError,
            (
                "Arguments for call method did not match expectations:\n"
                '  Received call:\tmethod("string", 12)\n'
                "  Expected call[1]:\tmethod(a=<class 'object'>, "
                "b=<class 'object'>, c=<class 'object'>)"
            ),
        ):
            instance.method("string", 12)  # pylint: disable=no-value-for-parameter
        flexmock_teardown()
        flexmock(instance).should_receive("method").with_args(object, object, object).and_return(
            "ok"
        )
        with assert_raises(
            exceptions.MethodSignatureError,
            (
                "Arguments for call method did not match expectations:\n"
                '  Received call:\tmethod("string", 12, 13, 14)\n'
                "  Expected call[1]:\tmethod(a=<class 'object'>, "
                "b=<class 'object'>, c=<class 'object'>)"
            ),
        ):
            instance.method("string", 12, 13, 14)

    def test_flexmock_should_match_types_on_multiple_arguments_classes(self):
        class Foo:
            def method(self, a, b):  # pylint: disable=invalid-name
                pass

        class Bar:
            pass

        foo_instance = Foo()
        bar_instance = Bar()
        flexmock(foo_instance).should_receive("method").with_args(object, Bar).and_return("ok")
        assert foo_instance.method("some string", bar_instance) == "ok"
        with assert_raises(
            exceptions.MethodSignatureError,
            re.compile(
                "Arguments for call method did not match expectations:\n"
                r'  Received call:\tmethod\(.+\.<locals>\.Bar object at 0x.+>, "some string"\)\n'
                r"  Expected call\[1\]:\tmethod\(a=<class 'object'>, b=<class.+\.<locals>\.Bar'>\)"
            ),
        ):
            foo_instance.method(bar_instance, "some string")
        flexmock_teardown()
        flexmock(foo_instance).should_receive("method").with_args(object, Bar).and_return("ok")
        with assert_raises(
            exceptions.MethodSignatureError,
            re.compile(
                "Arguments for call method did not match expectations:\n"
                r'  Received call:\tmethod\(12, "some string"\)\n'
                r"  Expected call\[1\]:\tmethod\(a=<class 'object'>, b=<class.+\.<locals>\.Bar'>\)"
            ),
        ):
            foo_instance.method(12, "some string")

    def test_flexmock_should_match_keyword_arguments(self):
        class Foo:
            def method(self, arg1, **kwargs):
                pass

        instance = Foo()
        flexmock(instance).should_receive("method").with_args(1, arg3=3, arg2=2).twice()
        instance.method(1, arg2=2, arg3=3)
        instance.method(1, arg3=3, arg2=2)
        flexmock_teardown()
        flexmock(instance).should_receive("method").with_args(1, arg3=3, arg2=2)
        with assert_raises(
            exceptions.MethodSignatureError,
            (
                "Arguments for call method did not match expectations:\n"
                "  Received call:\tmethod(arg2=2, arg3=3)\n"
                "  Expected call[1]:\tmethod(arg3=3, arg2=2, arg1=1)"
            ),
        ):
            instance.method(arg2=2, arg3=3)  # pylint: disable=no-value-for-parameter
        flexmock_teardown()
        flexmock(instance).should_receive("method").with_args(1, arg3=3, arg2=2)
        with assert_raises(
            exceptions.MethodSignatureError,
            (
                "Arguments for call method did not match expectations:\n"
                "  Received call:\tmethod(1, arg2=2, arg3=4)\n"
                "  Expected call[1]:\tmethod(arg3=3, arg2=2, arg1=1)"
            ),
        ):
            instance.method(1, arg2=2, arg3=4)
        flexmock_teardown()
        flexmock(instance).should_receive("method").with_args(1, arg3=3, arg2=2)
        with assert_raises(
            exceptions.MethodSignatureError,
            (
                "Arguments for call method did not match expectations:\n"
                "  Received call:\tmethod(1)\n"
                "  Expected call[1]:\tmethod(arg3=3, arg2=2, arg1=1)"
            ),
        ):
            instance.method(1)

    def test_flexmock_should_call_should_match_keyword_arguments(self):
        class Foo:
            def method(self, arg1, arg2=None, arg3=None):
                return f"{arg1}{arg2}{arg3}"

        instance = Foo()
        flexmock(instance).should_call("method").with_args(1, arg3=3, arg2=2).once()
        assert instance.method(1, arg2=2, arg3=3) == "123"

    def test_with_args_with_instance_method(self):
        flexmock(SomeClass).should_receive("instance_method_with_args").with_args("red").once()
        flexmock(SomeClass).should_receive("instance_method_with_args").with_args("blue").once()
        instance = SomeClass()
        instance.instance_method_with_args("red")
        instance.instance_method_with_args("blue")

    def test_with_args_with_class_method(self):
        flexmock(SomeClass).should_receive("class_method_with_args").with_args("red").once()
        flexmock(SomeClass).should_receive("class_method_with_args").with_args("blue").once()
        SomeClass.class_method_with_args("red")
        SomeClass.class_method_with_args("blue")

    def test_with_args_with_static_method(self):
        flexmock(SomeClass).should_receive("static_method_with_args").with_args("red").once()
        flexmock(SomeClass).should_receive("static_method_with_args").with_args("blue").once()
        SomeClass.static_method_with_args("red")
        SomeClass.static_method_with_args("blue")