File: test_warnings.py

package info (click to toggle)
python-testtools 2.8.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,244 kB
  • sloc: python: 15,086; makefile: 127; sh: 3
file content (220 lines) | stat: -rw-r--r-- 6,893 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
# Copyright (c) 2008-2016 testtools developers. See LICENSE for details.

import warnings
from typing import ClassVar

from testtools import TestCase
from testtools.matchers import (
    AfterPreprocessing,
    Contains,
    Equals,
    HasLength,
    MatchesListwise,
    MatchesStructure,
)
from testtools.matchers._warnings import IsDeprecated, WarningMessage, Warnings

from ..helpers import FullStackRunTest
from ..matchers.helpers import TestMatchersInterface


def make_warning(warning_type, message):
    warnings.warn(message, warning_type, 2)


def make_warning_message(message, category, filename=None, lineno=None, line=None):
    return warnings.WarningMessage(
        message=message, category=category, filename=filename, lineno=lineno, line=line
    )


class TestWarningMessageCategoryTypeInterface(TestCase, TestMatchersInterface):
    """Tests for `testtools.matchers._warnings.WarningMessage`.

    In particular matching the ``category_type``.
    """

    matches_matcher: ClassVar = WarningMessage(category_type=DeprecationWarning)
    warning_foo = make_warning_message("foo", DeprecationWarning)
    warning_bar = make_warning_message("bar", SyntaxWarning)
    warning_base = make_warning_message("base", Warning)
    matches_matches: ClassVar = [warning_foo]
    matches_mismatches: ClassVar = [warning_bar, warning_base]

    str_examples: ClassVar[list] = []
    describe_examples: ClassVar[list] = []


class TestWarningMessageMessageInterface(TestCase, TestMatchersInterface):
    """Tests for `testtools.matchers._warnings.WarningMessage`.

    In particular matching the ``message``.
    """

    matches_matcher: ClassVar = WarningMessage(
        category_type=DeprecationWarning, message=Equals("foo")
    )
    warning_foo = make_warning_message("foo", DeprecationWarning)
    warning_bar = make_warning_message("bar", DeprecationWarning)
    matches_matches: ClassVar = [warning_foo]
    matches_mismatches: ClassVar = [warning_bar]

    str_examples: ClassVar[list] = []
    describe_examples: ClassVar[list] = []


class TestWarningMessageFilenameInterface(TestCase, TestMatchersInterface):
    """Tests for `testtools.matchers._warnings.WarningMessage`.

    In particular matching the ``filename``.
    """

    matches_matcher: ClassVar = WarningMessage(
        category_type=DeprecationWarning, filename=Equals("a")
    )
    warning_foo = make_warning_message("foo", DeprecationWarning, filename="a")
    warning_bar = make_warning_message("bar", DeprecationWarning, filename="b")
    matches_matches: ClassVar = [warning_foo]
    matches_mismatches: ClassVar = [warning_bar]

    str_examples: ClassVar[list] = []
    describe_examples: ClassVar[list] = []


class TestWarningMessageLineNumberInterface(TestCase, TestMatchersInterface):
    """Tests for `testtools.matchers._warnings.WarningMessage`.

    In particular matching the ``lineno``.
    """

    matches_matcher: ClassVar = WarningMessage(
        category_type=DeprecationWarning, lineno=Equals(42)
    )
    warning_foo = make_warning_message("foo", DeprecationWarning, lineno=42)
    warning_bar = make_warning_message("bar", DeprecationWarning, lineno=21)
    matches_matches: ClassVar = [warning_foo]
    matches_mismatches: ClassVar = [warning_bar]

    str_examples: ClassVar[list] = []
    describe_examples: ClassVar[list] = []


class TestWarningMessageLineInterface(TestCase, TestMatchersInterface):
    """Tests for `testtools.matchers._warnings.WarningMessage`.

    In particular matching the ``line``.
    """

    matches_matcher: ClassVar = WarningMessage(
        category_type=DeprecationWarning, line=Equals("x")
    )
    warning_foo = make_warning_message("foo", DeprecationWarning, line="x")
    warning_bar = make_warning_message("bar", DeprecationWarning, line="y")
    matches_matches: ClassVar = [warning_foo]
    matches_mismatches: ClassVar = [warning_bar]

    str_examples: ClassVar[list] = []
    describe_examples: ClassVar[list] = []


class TestWarningsInterface(TestCase, TestMatchersInterface):
    """Tests for `testtools.matchers._warnings.Warnings`.

    Specifically without the optional argument.
    """

    matches_matcher: ClassVar = Warnings()

    @staticmethod
    def old_func():
        warnings.warn("old_func is deprecated", DeprecationWarning, 2)

    matches_matches: ClassVar = [old_func]
    matches_mismatches: ClassVar = [lambda: None]

    # Tricky to get function objects to render constantly, and the interfaces
    # helper uses assertEqual rather than (for instance) DocTestMatches.
    str_examples: ClassVar[list] = []

    describe_examples: ClassVar[list] = []


class TestWarningsMatcherInterface(TestCase, TestMatchersInterface):
    """Tests for `testtools.matchers._warnings.Warnings`.

    Specifically with the optional matcher argument.
    """

    matches_matcher: ClassVar = Warnings(
        warnings_matcher=MatchesListwise(
            [MatchesStructure(message=AfterPreprocessing(str, Contains("old_func")))]
        )
    )

    @staticmethod
    def old_func():
        warnings.warn("old_func is deprecated", DeprecationWarning, 2)

    @staticmethod
    def older_func():
        warnings.warn("older_func is deprecated", DeprecationWarning, 2)

    matches_matches: ClassVar = [old_func]
    matches_mismatches: ClassVar = [lambda: None, older_func]

    str_examples: ClassVar[list] = []
    describe_examples: ClassVar[list] = []


class TestWarningsMatcherNoWarningsInterface(TestCase, TestMatchersInterface):
    """Tests for `testtools.matchers._warnings.Warnings`.

    Specifically with the optional matcher argument matching that there were no
    warnings.
    """

    matches_matcher: ClassVar = Warnings(warnings_matcher=HasLength(0))

    @staticmethod
    def nowarning_func():
        pass

    @staticmethod
    def warning_func():
        warnings.warn("warning_func is deprecated", DeprecationWarning, 2)

    matches_matches: ClassVar = [nowarning_func]
    matches_mismatches: ClassVar = [warning_func]

    str_examples: ClassVar[list] = []
    describe_examples: ClassVar[list] = []


class TestWarningMessage(TestCase):
    """Tests for `testtools.matchers._warnings.WarningMessage`."""

    run_tests_with = FullStackRunTest

    def test_category(self):
        def old_func():
            warnings.warn("old_func is deprecated", DeprecationWarning, 2)

        self.assertThat(old_func, IsDeprecated(Contains("old_func")))


class TestIsDeprecated(TestCase):
    """Tests for `testtools.matchers._warnings.IsDeprecated`."""

    run_tests_with = FullStackRunTest

    def test_warning(self):
        def old_func():
            warnings.warn("old_func is deprecated", DeprecationWarning, 2)

        self.assertThat(old_func, IsDeprecated(Contains("old_func")))


def test_suite():
    from unittest import TestLoader

    return TestLoader().loadTestsFromName(__name__)