File: __init__.py

package info (click to toggle)
python-gvm 26.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 5,132 kB
  • sloc: python: 44,662; makefile: 18
file content (52 lines) | stat: -rw-r--r-- 1,397 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
# SPDX-FileCopyrightText: 2018-2024 Greenbone AG
#
# SPDX-License-Identifier: GPL-3.0-or-later
#


class CallableMock:
    def __init__(self, name):
        self.name = name
        self.calls = []
        self.result = None

    def __call__(self, *args, **kwargs):
        self.calls.append({"args": args, "kwargs": kwargs})

        return self.result

    def return_value(self, value):
        self.result = value

    def has_not_been_called(self):
        assert len(self.calls) == 0, f"{self.name} has been called."

    def has_been_called(self):
        assert len(self.calls) > 0, f"{self.name} has not been called."

    def has_been_called_times(self, times):
        assert (
            len(self.calls) == times
        ), f"{self.name} has not been called {times} times."

    def has_been_called_with(self, *args, **kwargs):
        if len(self.calls) == 0:
            assert False

        lastcall = self.calls[-1]

        resp = (
            "Expected arguments {eargs} {ekwargs} of {name} do not match. "
            "Received: {rargs} {rkwargs}"
        )

        # not sure if this is correct
        assert (
            lastcall["args"] == args and lastcall["kwargs"] == kwargs
        ), resp.format(
            name=self.name,
            eargs=args,
            ekwargs=kwargs,
            rargs=lastcall["args"],
            rkwargs=lastcall["kwargs"],
        )