File: matchers.py

package info (click to toggle)
python-mockito 1.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 528 kB
  • sloc: python: 4,102; makefile: 206
file content (413 lines) | stat: -rw-r--r-- 10,059 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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
# Copyright (c) 2008-2016 Szczepan Faber, Serhiy Oplakanets, Herr Kaste
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

"""Argument matchers for stubbing and verifications.

In general the call signature you specify when stubbing or verifying in mockito
is as concrete as possible: it consists of values only::

    when(os.path).exists('/foo/bar.txt').thenReturn(True)

This is for a reason. In controlled test environments, for the scope of a
single test, you should usually know exactly how you use a function, and what
you expect its outcome to be. In mockito usually (in `strict` mode) all
invocations you did not specify upfront will throw at call time.

If you reason about your code, the above `when` tirade turns - for the time
of the test - the specific stubbed function into a constant.

You can use so called argument matchers below if you can't or don't
want to specify a single concrete value for an argument, but a type or class of
possible values. E.g.::

    when(os.path).exists(...).thenReturn(True)
    when(os.path).exists(ANY).thenReturn(True)
    when(os.path).exists(ANY(str)).thenReturn(True)

    when(requests).get(ANY(str), **kwargs)
    when(requests).get('https://example.com', ...)

    when(math).sqrt(not_(_or(ANY(float), ANY(int)))).thenRaise(TypeError)

Now what you get each time is a function that up to a degree takes various
arguments and responds with the same outcome each time. Now that's a weird
thing. So use the matchers for a reason, they're powerful.

The one usage you should not care about is a loose signature when using
:func:`verify`. Since mockito will throw for unexpected calls, a very loose
`verify` should be ok::

    verify(requests, times=1).get(...)


"""

import re
builtin_any = any

__all__ = [
    'and_', 'or_', 'not_',
    'eq', 'neq',
    'lt', 'lte',
    'gt', 'gte',
    'any', 'any_', 'ANY',
    'arg_that',
    'contains',
    'matches',
    'captor',
    'times',
    'args', 'ARGS',
    'kwargs', 'KWARGS'
]


class _ArgsSentinel(object):
    def __repr__(self):
        return '*args'


ARGS_SENTINEL = _ArgsSentinel()
ARGS = args = [ARGS_SENTINEL]
# ARGS.__doc__ = """Matches multiple positional arguments.

# Note: `args` must match at least one argument.

# Example::

#     when(manager).add_tasks(1, 2, *args)

# """

KWARGS_SENTINEL = '**'
KWARGS = kwargs = {KWARGS_SENTINEL: '_'}
# KWARGS.__doc__ = """Matches multiple keyword arguments.

# Note that `kwargs` must match at least one remaining keyword argument.

# Example::

#     when(requests).get('http://myapi/', **KWARGS)

# """


class MatcherError(RuntimeError):
    '''Indicates generic runtime error raised by mockito-python matchers
    '''
    pass


class Matcher:
    def matches(self, arg):
        pass

class Capturing:
    def capture_value(self, value):
        pass


class Any(Matcher):
    def __init__(self, wanted_type=None):
        self.wanted_type = wanted_type

    def matches(self, arg):
        if self.wanted_type:
            return isinstance(arg, self.wanted_type)
        else:
            return True

    def __repr__(self):
        return "<Any: %s>" % self.wanted_type


class ValueMatcher(Matcher):
    def __init__(self, value):
        self.value = value

    def __repr__(self):
        return "<%s: %s>" % (self.__class__.__name__, self.value)


class Eq(ValueMatcher):
    def matches(self, arg):
        return arg == self.value


class Neq(ValueMatcher):
    def matches(self, arg):
        return arg != self.value


class Lt(ValueMatcher):
    def matches(self, arg):
        return arg < self.value


class Lte(ValueMatcher):
    def matches(self, arg):
        return arg <= self.value


class Gt(ValueMatcher):
    def matches(self, arg):
        return arg > self.value


class Gte(ValueMatcher):
    def matches(self, arg):
        return arg >= self.value


class And(Matcher):
    def __init__(self, matchers):
        self.matchers = [
            matcher if isinstance(matcher, Matcher) else Eq(matcher)
            for matcher in matchers]

    def matches(self, arg):
        return all(matcher.matches(arg) for matcher in self.matchers)

    def __repr__(self):
        return "<And: %s>" % self.matchers


class Or(Matcher):
    def __init__(self, matchers):
        self.matchers = [
            matcher if isinstance(matcher, Matcher) else Eq(matcher)
            for matcher in matchers]

    def matches(self, arg):
        return builtin_any([matcher.matches(arg) for matcher in self.matchers])

    def __repr__(self):
        return "<Or: %s>" % self.matchers


class Not(Matcher):
    def __init__(self, matcher):
        self.matcher = matcher if isinstance(matcher, Matcher) else Eq(matcher)

    def matches(self, arg):
        return not self.matcher.matches(arg)

    def __repr__(self):
        return "<Not: %s>" % self.matcher


class ArgThat(Matcher):
    def __init__(self, predicate):
        self.predicate = predicate

    def matches(self, arg):
        return self.predicate(arg)

    def __repr__(self):
        return "<ArgThat>"


class Contains(Matcher):
    def __init__(self, sub):
        self.sub = sub

    def matches(self, arg):
        if not hasattr(arg, 'find'):
            return
        return self.sub and len(self.sub) > 0 and arg.find(self.sub) > -1

    def __repr__(self):
        return "<Contains: '%s'>" % self.sub


class Matches(Matcher):
    def __init__(self, regex, flags=0):
        self.regex = re.compile(regex, flags)

    def matches(self, arg):
        if not isinstance(arg, str):
            return
        return self.regex.match(arg) is not None

    def __repr__(self):
        if self.regex.flags:
            return "<Matches: %s flags=%d>" % (self.regex.pattern,
                                               self.regex.flags)
        else:
            return "<Matches: %s>" % self.regex.pattern


class ArgumentCaptor(Matcher, Capturing):
    def __init__(self, matcher=None):
        self.matcher = matcher or Any()
        self.all_values = []

    def matches(self, arg):
        result = self.matcher.matches(arg)
        if not result:
            return
        return True

    @property
    def value(self):
        if not self.all_values:
            raise MatcherError("No argument value was captured!")
        return self.all_values[-1]

    def capture_value(self, value):
        self.all_values.append(value)

    def __repr__(self):
        return "<ArgumentCaptor: matcher=%s values=%s>" % (
            repr(self.matcher), self.all_values,
        )


def any(wanted_type=None):
    """Matches against type of argument (`isinstance`).

    If you want to match *any* type, use either `ANY` or `ANY()`.

    Examples::

        when(mock).foo(any).thenReturn(1)
        verify(mock).foo(any(int))

    """
    return Any(wanted_type)


ANY = any_ = any


def eq(value):
    """Matches particular value (`==`)"""
    return Eq(value)


def neq(value):
    """Matches any but given value (`!=`)"""
    return Neq(value)


def lt(value):
    """Matches any value that is less than given value (`<`)"""
    return Lt(value)


def lte(value):
    """Matches any value that is less than or equal to given value (`<=`)"""
    return Lte(value)


def gt(value):
    """Matches any value that is greater than given value (`>`)"""
    return Gt(value)


def gte(value):
    """Matches any value that is greater than or equal to given value (`>=`)"""
    return Gte(value)


def and_(*matchers):
    """Matches if all given matchers match

    Example::

        when(mock).foo(and_(ANY(str), contains('foo')))

    """
    return And(matchers)


def or_(*matchers):
    """Matches if any given matcher match

    Example::

        when(mock).foo(or_(ANY(int), ANY(float)))

    """
    return Or(matchers)


def not_(matcher):
    """Matches if given matcher does not match

    Example::

        when(mock).foo(not_(ANY(str))).thenRaise(TypeError)

    """
    return Not(matcher)


def arg_that(predicate):
    """Matches any argument for which predicate returns True

    Example::

        verify(mock).foo(arg_that(lambda arg: arg > 3 and arg < 7))

    """
    return ArgThat(predicate)


def contains(sub):
    """Matches any string containing given substring

    Example::

        mock.foo([120, 121, 122, 123])
        verify(mock).foo(contains(123))

    """
    return Contains(sub)


def matches(regex, flags=0):
    """Matches any string that matches given regex"""
    return Matches(regex, flags)


def captor(matcher=None):
    """Returns argument captor that captures values for further assertions

    Example::

        arg = captor()
        mock.do_something(123)
        mock.do_something(456)
        verify(mock).do_something(arg)
        assert arg.value == 456
        assert arg.all_values == [123, 456]

    You can restrict what the captor captures using the other matchers
    shown herein::

        arg = captor(any(str))
        arg = captor(contains("foo"))

    """
    return ArgumentCaptor(matcher)


def times(count):
    return count