/usr/lib/python2.7/dist-packages/testtools/tests/matchers/test_warnings.py is in python-testtools 2.3.0-3ubuntu2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | # Copyright (c) 2008-2016 testtools developers. See LICENSE for details.
import warnings
from testtools import TestCase
from testtools.matchers import (
AfterPreprocessing,
Equals,
MatchesStructure,
MatchesListwise,
Contains,
HasLength,
)
from testtools.matchers._warnings import Warnings, IsDeprecated, WarningMessage
from testtools.tests.helpers import FullStackRunTest
from testtools.tests.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 = 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 = [warning_foo]
matches_mismatches = [warning_bar, warning_base]
str_examples = []
describe_examples = []
class TestWarningMessageMessageInterface(TestCase, TestMatchersInterface):
"""
Tests for `testtools.matchers._warnings.WarningMessage`.
In particular matching the ``message``.
"""
matches_matcher = WarningMessage(category_type=DeprecationWarning,
message=Equals('foo'))
warning_foo = make_warning_message('foo', DeprecationWarning)
warning_bar = make_warning_message('bar', DeprecationWarning)
matches_matches = [warning_foo]
matches_mismatches = [warning_bar]
str_examples = []
describe_examples = []
class TestWarningMessageFilenameInterface(TestCase, TestMatchersInterface):
"""
Tests for `testtools.matchers._warnings.WarningMessage`.
In particular matching the ``filename``.
"""
matches_matcher = 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 = [warning_foo]
matches_mismatches = [warning_bar]
str_examples = []
describe_examples = []
class TestWarningMessageLineNumberInterface(TestCase, TestMatchersInterface):
"""
Tests for `testtools.matchers._warnings.WarningMessage`.
In particular matching the ``lineno``.
"""
matches_matcher = 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 = [warning_foo]
matches_mismatches = [warning_bar]
str_examples = []
describe_examples = []
class TestWarningMessageLineInterface(TestCase, TestMatchersInterface):
"""
Tests for `testtools.matchers._warnings.WarningMessage`.
In particular matching the ``line``.
"""
matches_matcher = 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 = [warning_foo]
matches_mismatches = [warning_bar]
str_examples = []
describe_examples = []
class TestWarningsInterface(TestCase, TestMatchersInterface):
"""
Tests for `testtools.matchers._warnings.Warnings`.
Specifically without the optional argument.
"""
matches_matcher = Warnings()
def old_func():
warnings.warn('old_func is deprecated', DeprecationWarning, 2)
matches_matches = [old_func]
matches_mismatches = [lambda: None]
# Tricky to get function objects to render constantly, and the interfaces
# helper uses assertEqual rather than (for instance) DocTestMatches.
str_examples = []
describe_examples = []
class TestWarningsMatcherInterface(TestCase, TestMatchersInterface):
"""
Tests for `testtools.matchers._warnings.Warnings`.
Specifically with the optional matcher argument.
"""
matches_matcher = Warnings(
warnings_matcher=MatchesListwise([
MatchesStructure(
message=AfterPreprocessing(
str, Contains('old_func')))]))
def old_func():
warnings.warn('old_func is deprecated', DeprecationWarning, 2)
def older_func():
warnings.warn('older_func is deprecated', DeprecationWarning, 2)
matches_matches = [old_func]
matches_mismatches = [lambda:None, older_func]
str_examples = []
describe_examples = []
class TestWarningsMatcherNoWarningsInterface(TestCase, TestMatchersInterface):
"""
Tests for `testtools.matchers._warnings.Warnings`.
Specifically with the optional matcher argument matching that there were no
warnings.
"""
matches_matcher = Warnings(warnings_matcher=HasLength(0))
def nowarning_func():
pass
def warning_func():
warnings.warn('warning_func is deprecated', DeprecationWarning, 2)
matches_matches = [nowarning_func]
matches_mismatches = [warning_func]
str_examples = []
describe_examples = []
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__)
|