File: exceptions.py

package info (click to toggle)
python-pyforge 1.3.0-7
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 576 kB
  • sloc: python: 3,729; makefile: 13; sh: 7
file content (131 lines) | stat: -rw-r--r-- 3,519 bytes parent folder | download | duplicates (5)
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
from difflib import Differ
from .queued_object import QueuedObject


class ForgeException(Exception):
    pass


class SignatureException(ForgeException):
    pass


class InvalidKeywordArgument(SignatureException):
    pass


class FunctionCannotBeBound(SignatureException):
    pass


class ConflictingActions(ForgeException):
    pass


class MockObjectUnhashable(ForgeException):
    pass


class CannotMockException(ForgeException):
    pass


class CannotMockFunctions(CannotMockException):
    pass

UNEXPECTED_FUNCTION_CALLED_STR = "Unexpected function called!"
UNEXPECTED_SETATTR_STR = "Unexpected attribute set!"
DIFF_DESCRIPTION_STR = "(Expected: +, Got: -)"


class UnexpectedEvent(ForgeException, AssertionError):
    def __init__(self, expected, got):
        super(UnexpectedEvent, self).__init__()
        self.expected = expected
        self.got = got

    def __str__(self):
        returned = self._getTitle()
        returned += " %s\n" % DIFF_DESCRIPTION_STR
        debug_info = self._get_debug_info()
        if debug_info:
            returned += debug_info
        returned += self._get_diff_string()
        return returned

    def _get_debug_info(self):
        returned = ""
        returned += self._get_expected_caller_infos()
        returned += self._get_got_caller_infos()
        return returned

    def _get_expected_caller_infos(self):
        return self._get_caller_infos("Recorded", self.expected)

    def _get_got_caller_infos(self):
        return self._get_caller_infos("Replayed", self.got)

    def _get_caller_infos(self, verb, list_or_single):
        if not isinstance(list_or_single, list):
            list_or_single = [list_or_single]
        returned = ""
        for option in list_or_single:
            if option and option.caller_info:
                returned += "%s from %s\n" % (verb, option.caller_info)
        return returned

    def _get_diff_string(self):
        got_string = self._get_got_string()
        expected_string = self._get_expected_string()
        if got_string == expected_string:
            return "- %s\n+ %s" % (expected_string, got_string)
        diff = Differ().compare(
            [self._get_got_string()],
            [self._get_expected_string()],
            )
        return "\n".join(line.strip() for line in diff)

    def _get_expected_string(self):
        return self._get_description_string(self.expected)

    def _get_got_string(self):
        return self._get_description_string(self.got)

    def _get_description_string(self, x):
        if isinstance(x, list):
            if len(x) == 1:
                x = x[0]
            elif len(x) == 0:
                x = None
        if x is None:
            return "<< None >>"
        if not isinstance(x, QueuedObject):
            return str(x)
        return x.describe()


class UnexpectedCall(UnexpectedEvent):
    @classmethod
    def _getTitle(cls):
        return UNEXPECTED_FUNCTION_CALLED_STR


class UnexpectedSetattr(UnexpectedEvent):
    @classmethod
    def _getTitle(cls):
        return UNEXPECTED_SETATTR_STR


class ExpectedEventsNotFound(ForgeException, AssertionError):
    def __init__(self, events):
        super(ExpectedEventsNotFound, self).__init__()
        if type(events) is not list:
            raise ValueError("Expected events must be a list")
        self.events = events

    def __str__(self):
        return "Expected events not found:\n%s" % "\n".join(map(str, self.events))


class InvalidEntryPoint(ForgeException):
    pass