Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 1 | from contextlib import contextmanager |
Christian Heimes | 5fb7c2a | 2007-12-24 08:52:31 +0000 | [diff] [blame] | 2 | import linecache |
Raymond Hettinger | dc9dcf1 | 2003-07-13 06:15:11 +0000 | [diff] [blame] | 3 | import os |
Christian Heimes | 5fb7c2a | 2007-12-24 08:52:31 +0000 | [diff] [blame] | 4 | from io import StringIO |
Guido van Rossum | 61e21b5 | 2007-08-20 19:06:03 +0000 | [diff] [blame] | 5 | import sys |
Raymond Hettinger | d6f6e50 | 2003-07-13 08:37:40 +0000 | [diff] [blame] | 6 | import unittest |
| 7 | from test import test_support |
Jeremy Hylton | 8501466 | 2003-07-11 15:37:59 +0000 | [diff] [blame] | 8 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 9 | from test import warning_tests |
Jeremy Hylton | 8501466 | 2003-07-11 15:37:59 +0000 | [diff] [blame] | 10 | |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 11 | import warnings as original_warnings |
Jeremy Hylton | 8501466 | 2003-07-11 15:37:59 +0000 | [diff] [blame] | 12 | |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 13 | sys.modules['_warnings'] = 0 |
| 14 | del sys.modules['warnings'] |
| 15 | |
| 16 | import warnings as py_warnings |
| 17 | |
| 18 | del sys.modules['_warnings'] |
| 19 | del sys.modules['warnings'] |
| 20 | |
| 21 | import warnings as c_warnings |
| 22 | |
| 23 | sys.modules['warnings'] = original_warnings |
| 24 | |
| 25 | |
| 26 | @contextmanager |
| 27 | def warnings_state(module): |
| 28 | """Use a specific warnings implementation in warning_tests.""" |
| 29 | global __warningregistry__ |
| 30 | for to_clear in (sys, warning_tests): |
| 31 | try: |
| 32 | to_clear.__warningregistry__.clear() |
| 33 | except AttributeError: |
| 34 | pass |
| 35 | try: |
| 36 | __warningregistry__.clear() |
| 37 | except NameError: |
| 38 | pass |
| 39 | original_warnings = warning_tests.warnings |
| 40 | try: |
| 41 | warning_tests.warnings = module |
| 42 | yield |
| 43 | finally: |
| 44 | warning_tests.warnings = original_warnings |
| 45 | |
| 46 | |
| 47 | class BaseTest(unittest.TestCase): |
| 48 | |
| 49 | """Basic bookkeeping required for testing.""" |
| 50 | |
| 51 | def setUp(self): |
| 52 | # The __warningregistry__ needs to be in a pristine state for tests |
| 53 | # to work properly. |
| 54 | if '__warningregistry__' in globals(): |
| 55 | del globals()['__warningregistry__'] |
| 56 | if hasattr(warning_tests, '__warningregistry__'): |
| 57 | del warning_tests.__warningregistry__ |
| 58 | if hasattr(sys, '__warningregistry__'): |
| 59 | del sys.__warningregistry__ |
| 60 | # The 'warnings' module must be explicitly set so that the proper |
| 61 | # interaction between _warnings and 'warnings' can be controlled. |
| 62 | sys.modules['warnings'] = self.module |
| 63 | super(BaseTest, self).setUp() |
| 64 | |
| 65 | def tearDown(self): |
| 66 | sys.modules['warnings'] = original_warnings |
| 67 | super(BaseTest, self).tearDown() |
| 68 | |
| 69 | |
| 70 | class FilterTests(object): |
| 71 | |
| 72 | """Testing the filtering functionality.""" |
| 73 | |
| 74 | def test_error(self): |
| 75 | with test_support.catch_warning(self.module) as w: |
| 76 | self.module.resetwarnings() |
| 77 | self.module.filterwarnings("error", category=UserWarning) |
| 78 | self.assertRaises(UserWarning, self.module.warn, |
| 79 | "FilterTests.test_error") |
| 80 | |
| 81 | def test_ignore(self): |
| 82 | with test_support.catch_warning(self.module) as w: |
| 83 | self.module.resetwarnings() |
| 84 | self.module.filterwarnings("ignore", category=UserWarning) |
| 85 | self.module.warn("FilterTests.test_ignore", UserWarning) |
| 86 | self.assert_(not w.message) |
| 87 | |
| 88 | def test_always(self): |
| 89 | with test_support.catch_warning(self.module) as w: |
| 90 | self.module.resetwarnings() |
| 91 | self.module.filterwarnings("always", category=UserWarning) |
| 92 | message = "FilterTests.test_always" |
| 93 | self.module.warn(message, UserWarning) |
| 94 | self.assert_(message, w.message) |
| 95 | w.message = None # Reset. |
| 96 | self.module.warn(message, UserWarning) |
| 97 | self.assert_(w.message, message) |
| 98 | |
| 99 | def test_default(self): |
| 100 | with test_support.catch_warning(self.module) as w: |
| 101 | self.module.resetwarnings() |
| 102 | self.module.filterwarnings("default", category=UserWarning) |
| 103 | message = UserWarning("FilterTests.test_default") |
| 104 | for x in range(2): |
| 105 | self.module.warn(message, UserWarning) |
| 106 | if x == 0: |
| 107 | self.assertEquals(w.message, message) |
| 108 | w.reset() |
| 109 | elif x == 1: |
| 110 | self.assert_(not w.message, "unexpected warning: " + str(w)) |
| 111 | else: |
| 112 | raise ValueError("loop variant unhandled") |
| 113 | |
| 114 | def test_module(self): |
| 115 | with test_support.catch_warning(self.module) as w: |
| 116 | self.module.resetwarnings() |
| 117 | self.module.filterwarnings("module", category=UserWarning) |
| 118 | message = UserWarning("FilterTests.test_module") |
| 119 | self.module.warn(message, UserWarning) |
| 120 | self.assertEquals(w.message, message) |
| 121 | w.reset() |
| 122 | self.module.warn(message, UserWarning) |
| 123 | self.assert_(not w.message, "unexpected message: " + str(w)) |
| 124 | |
| 125 | def test_once(self): |
| 126 | with test_support.catch_warning(self.module) as w: |
| 127 | self.module.resetwarnings() |
| 128 | self.module.filterwarnings("once", category=UserWarning) |
| 129 | message = UserWarning("FilterTests.test_once") |
| 130 | self.module.warn_explicit(message, UserWarning, "test_warnings.py", |
| 131 | 42) |
| 132 | self.assertEquals(w.message, message) |
| 133 | w.reset() |
| 134 | self.module.warn_explicit(message, UserWarning, "test_warnings.py", |
| 135 | 13) |
| 136 | self.assert_(not w.message) |
| 137 | self.module.warn_explicit(message, UserWarning, "test_warnings2.py", |
| 138 | 42) |
| 139 | self.assert_(not w.message) |
| 140 | |
| 141 | def test_inheritance(self): |
| 142 | with test_support.catch_warning(self.module) as w: |
| 143 | self.module.resetwarnings() |
| 144 | self.module.filterwarnings("error", category=Warning) |
| 145 | self.assertRaises(UserWarning, self.module.warn, |
| 146 | "FilterTests.test_inheritance", UserWarning) |
| 147 | |
| 148 | def test_ordering(self): |
| 149 | with test_support.catch_warning(self.module) as w: |
| 150 | self.module.resetwarnings() |
| 151 | self.module.filterwarnings("ignore", category=UserWarning) |
| 152 | self.module.filterwarnings("error", category=UserWarning, |
| 153 | append=True) |
| 154 | w.reset() |
| 155 | try: |
| 156 | self.module.warn("FilterTests.test_ordering", UserWarning) |
| 157 | except UserWarning: |
| 158 | self.fail("order handling for actions failed") |
| 159 | self.assert_(not w.message) |
| 160 | |
| 161 | def test_filterwarnings(self): |
| 162 | # Test filterwarnings(). |
| 163 | # Implicitly also tests resetwarnings(). |
| 164 | with test_support.catch_warning(self.module) as w: |
| 165 | self.module.filterwarnings("error", "", Warning, "", 0) |
| 166 | self.assertRaises(UserWarning, self.module.warn, 'convert to error') |
| 167 | |
| 168 | self.module.resetwarnings() |
| 169 | text = 'handle normally' |
| 170 | self.module.warn(text) |
| 171 | self.assertEqual(str(w.message), text) |
| 172 | self.assert_(w.category is UserWarning) |
| 173 | |
| 174 | self.module.filterwarnings("ignore", "", Warning, "", 0) |
| 175 | text = 'filtered out' |
| 176 | self.module.warn(text) |
| 177 | self.assertNotEqual(str(w.message), text) |
| 178 | |
| 179 | self.module.resetwarnings() |
| 180 | self.module.filterwarnings("error", "hex*", Warning, "", 0) |
| 181 | self.assertRaises(UserWarning, self.module.warn, 'hex/oct') |
| 182 | text = 'nonmatching text' |
| 183 | self.module.warn(text) |
| 184 | self.assertEqual(str(w.message), text) |
| 185 | self.assert_(w.category is UserWarning) |
| 186 | |
| 187 | class CFilterTests(BaseTest, FilterTests): |
| 188 | module = c_warnings |
| 189 | |
| 190 | class PyFilterTests(BaseTest, FilterTests): |
| 191 | module = py_warnings |
| 192 | |
| 193 | |
| 194 | class WarnTests(unittest.TestCase): |
| 195 | |
| 196 | """Test warnings.warn() and warnings.warn_explicit().""" |
| 197 | |
| 198 | def test_message(self): |
| 199 | with test_support.catch_warning(self.module) as w: |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 200 | for i in range(4): |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 201 | text = 'multi %d' %i # Different text on each call. |
| 202 | self.module.warn(text) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 203 | self.assertEqual(str(w.message), text) |
| 204 | self.assert_(w.category is UserWarning) |
Raymond Hettinger | d6f6e50 | 2003-07-13 08:37:40 +0000 | [diff] [blame] | 205 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 206 | def test_filename(self): |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 207 | with warnings_state(self.module): |
| 208 | with test_support.catch_warning(self.module) as w: |
| 209 | warning_tests.inner("spam1") |
| 210 | self.assertEqual(os.path.basename(w.filename), "warning_tests.py") |
| 211 | warning_tests.outer("spam2") |
| 212 | self.assertEqual(os.path.basename(w.filename), "warning_tests.py") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 213 | |
| 214 | def test_stacklevel(self): |
| 215 | # Test stacklevel argument |
| 216 | # make sure all messages are different, so the warning won't be skipped |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 217 | with warnings_state(self.module): |
| 218 | with test_support.catch_warning(self.module) as w: |
| 219 | warning_tests.inner("spam3", stacklevel=1) |
| 220 | self.assertEqual(os.path.basename(w.filename), "warning_tests.py") |
| 221 | warning_tests.outer("spam4", stacklevel=1) |
| 222 | self.assertEqual(os.path.basename(w.filename), "warning_tests.py") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 223 | |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 224 | warning_tests.inner("spam5", stacklevel=2) |
| 225 | self.assertEqual(os.path.basename(w.filename), "test_warnings.py") |
| 226 | warning_tests.outer("spam6", stacklevel=2) |
| 227 | self.assertEqual(os.path.basename(w.filename), "warning_tests.py") |
Christian Heimes | 5d8da20 | 2008-05-06 13:58:24 +0000 | [diff] [blame] | 228 | warning_tests.outer("spam6.5", stacklevel=3) |
| 229 | self.assertEqual(os.path.basename(w.filename), "test_warnings.py") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 230 | |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 231 | warning_tests.inner("spam7", stacklevel=9999) |
| 232 | self.assertEqual(os.path.basename(w.filename), "sys") |
| 233 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 234 | def test_missing_filename_not_main(self): |
| 235 | # If __file__ is not specified and __main__ is not the module name, |
| 236 | # then __file__ should be set to the module name. |
| 237 | filename = warning_tests.__file__ |
| 238 | try: |
| 239 | del warning_tests.__file__ |
| 240 | with warnings_state(self.module): |
| 241 | with test_support.catch_warning(self.module) as w: |
| 242 | warning_tests.inner("spam8", stacklevel=1) |
| 243 | self.assertEqual(w.filename, warning_tests.__name__) |
| 244 | finally: |
| 245 | warning_tests.__file__ = filename |
| 246 | |
| 247 | def test_missing_filename_main_with_argv(self): |
| 248 | # If __file__ is not specified and the caller is __main__ and sys.argv |
| 249 | # exists, then use sys.argv[0] as the file. |
| 250 | if not hasattr(sys, 'argv'): |
| 251 | return |
| 252 | filename = warning_tests.__file__ |
| 253 | module_name = warning_tests.__name__ |
| 254 | try: |
| 255 | del warning_tests.__file__ |
| 256 | warning_tests.__name__ = '__main__' |
| 257 | with warnings_state(self.module): |
| 258 | with test_support.catch_warning(self.module) as w: |
| 259 | warning_tests.inner('spam9', stacklevel=1) |
| 260 | self.assertEqual(w.filename, sys.argv[0]) |
| 261 | finally: |
| 262 | warning_tests.__file__ = filename |
| 263 | warning_tests.__name__ = module_name |
| 264 | |
| 265 | def test_missing_filename_main_without_argv(self): |
| 266 | # If __file__ is not specified, the caller is __main__, and sys.argv |
| 267 | # is not set, then '__main__' is the file name. |
| 268 | filename = warning_tests.__file__ |
| 269 | module_name = warning_tests.__name__ |
| 270 | argv = sys.argv |
| 271 | try: |
| 272 | del warning_tests.__file__ |
| 273 | warning_tests.__name__ = '__main__' |
| 274 | del sys.argv |
| 275 | with warnings_state(self.module): |
| 276 | with test_support.catch_warning(self.module) as w: |
| 277 | warning_tests.inner('spam10', stacklevel=1) |
| 278 | self.assertEqual(w.filename, '__main__') |
| 279 | finally: |
| 280 | warning_tests.__file__ = filename |
| 281 | warning_tests.__name__ = module_name |
| 282 | sys.argv = argv |
| 283 | |
Christian Heimes | daaf8ee | 2008-05-04 23:58:41 +0000 | [diff] [blame] | 284 | def test_missing_filename_main_with_argv_empty_string(self): |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 285 | # If __file__ is not specified, the caller is __main__, and sys.argv[0] |
| 286 | # is the empty string, then '__main__ is the file name. |
| 287 | # Tests issue 2743. |
| 288 | file_name = warning_tests.__file__ |
| 289 | module_name = warning_tests.__name__ |
| 290 | argv = sys.argv |
| 291 | try: |
| 292 | del warning_tests.__file__ |
| 293 | warning_tests.__name__ = '__main__' |
| 294 | sys.argv = [''] |
| 295 | with warnings_state(self.module): |
| 296 | with test_support.catch_warning(self.module) as w: |
| 297 | warning_tests.inner('spam11', stacklevel=1) |
| 298 | self.assertEqual(w.filename, '__main__') |
| 299 | finally: |
| 300 | warning_tests.__file__ = file_name |
| 301 | warning_tests.__name__ = module_name |
| 302 | sys.argv = argv |
| 303 | |
| 304 | |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 305 | |
| 306 | class CWarnTests(BaseTest, WarnTests): |
| 307 | module = c_warnings |
| 308 | |
| 309 | class PyWarnTests(BaseTest, WarnTests): |
| 310 | module = py_warnings |
| 311 | |
| 312 | |
| 313 | class WCmdLineTests(unittest.TestCase): |
| 314 | |
| 315 | def test_improper_input(self): |
| 316 | # Uses the private _setoption() function to test the parsing |
| 317 | # of command-line warning arguments |
| 318 | with test_support.catch_warning(self.module): |
| 319 | self.assertRaises(self.module._OptionError, |
| 320 | self.module._setoption, '1:2:3:4:5:6') |
| 321 | self.assertRaises(self.module._OptionError, |
| 322 | self.module._setoption, 'bogus::Warning') |
| 323 | self.assertRaises(self.module._OptionError, |
| 324 | self.module._setoption, 'ignore:2::4:-5') |
| 325 | self.module._setoption('error::Warning::0') |
| 326 | self.assertRaises(UserWarning, self.module.warn, 'convert to error') |
| 327 | |
| 328 | class CWCmdLineTests(BaseTest, WCmdLineTests): |
| 329 | module = c_warnings |
| 330 | |
| 331 | class PyWCmdLineTests(BaseTest, WCmdLineTests): |
| 332 | module = py_warnings |
| 333 | |
| 334 | |
| 335 | class _WarningsTests(BaseTest): |
| 336 | |
| 337 | """Tests specific to the _warnings module.""" |
| 338 | |
| 339 | module = c_warnings |
| 340 | |
| 341 | def test_filter(self): |
| 342 | # Everything should function even if 'filters' is not in warnings. |
| 343 | with test_support.catch_warning(self.module) as w: |
| 344 | self.module.filterwarnings("error", "", Warning, "", 0) |
| 345 | self.assertRaises(UserWarning, self.module.warn, |
| 346 | 'convert to error') |
| 347 | del self.module.filters |
| 348 | self.assertRaises(UserWarning, self.module.warn, |
| 349 | 'convert to error') |
| 350 | |
| 351 | def test_onceregistry(self): |
| 352 | # Replacing or removing the onceregistry should be okay. |
| 353 | global __warningregistry__ |
| 354 | message = UserWarning('onceregistry test') |
| 355 | try: |
| 356 | original_registry = self.module.onceregistry |
| 357 | __warningregistry__ = {} |
| 358 | with test_support.catch_warning(self.module) as w: |
| 359 | self.module.resetwarnings() |
| 360 | self.module.filterwarnings("once", category=UserWarning) |
| 361 | self.module.warn_explicit(message, UserWarning, "file", 42) |
| 362 | self.failUnlessEqual(w.message, message) |
| 363 | w.reset() |
| 364 | self.module.warn_explicit(message, UserWarning, "file", 42) |
| 365 | self.assert_(not w.message) |
| 366 | # Test the resetting of onceregistry. |
| 367 | self.module.onceregistry = {} |
| 368 | __warningregistry__ = {} |
| 369 | self.module.warn('onceregistry test') |
| 370 | self.failUnlessEqual(w.message.args, message.args) |
| 371 | # Removal of onceregistry is okay. |
| 372 | w.reset() |
| 373 | del self.module.onceregistry |
| 374 | __warningregistry__ = {} |
| 375 | self.module.warn_explicit(message, UserWarning, "file", 42) |
| 376 | self.failUnless(not w.message) |
| 377 | finally: |
| 378 | self.module.onceregistry = original_registry |
| 379 | |
| 380 | def test_showwarning_missing(self): |
| 381 | # Test that showwarning() missing is okay. |
| 382 | text = 'del showwarning test' |
| 383 | with test_support.catch_warning(self.module): |
| 384 | self.module.filterwarnings("always", category=UserWarning) |
| 385 | del self.module.showwarning |
| 386 | with test_support.captured_output('stderr') as stream: |
| 387 | self.module.warn(text) |
| 388 | result = stream.getvalue() |
| 389 | self.failUnless(text in result) |
| 390 | |
| 391 | def test_show_warning_output(self): |
| 392 | # With showarning() missing, make sure that output is okay. |
| 393 | text = 'test show_warning' |
| 394 | with test_support.catch_warning(self.module): |
| 395 | self.module.filterwarnings("always", category=UserWarning) |
| 396 | del self.module.showwarning |
| 397 | with test_support.captured_output('stderr') as stream: |
| 398 | warning_tests.inner(text) |
| 399 | result = stream.getvalue() |
| 400 | self.failUnlessEqual(result.count('\n'), 2, |
| 401 | "Too many newlines in %r" % result) |
| 402 | first_line, second_line = result.split('\n', 1) |
| 403 | expected_file = os.path.splitext(warning_tests.__file__)[0] + '.py' |
Neal Norwitz | 32dde22 | 2008-04-15 06:43:13 +0000 | [diff] [blame] | 404 | first_line_parts = first_line.rsplit(':', 3) |
| 405 | path, line, warning_class, message = first_line_parts |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 406 | line = int(line) |
| 407 | self.failUnlessEqual(expected_file, path) |
| 408 | self.failUnlessEqual(warning_class, ' ' + UserWarning.__name__) |
| 409 | self.failUnlessEqual(message, ' ' + text) |
| 410 | expected_line = ' ' + linecache.getline(path, line).strip() + '\n' |
| 411 | assert expected_line |
| 412 | self.failUnlessEqual(second_line, expected_line) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 413 | |
| 414 | |
Christian Heimes | 5fb7c2a | 2007-12-24 08:52:31 +0000 | [diff] [blame] | 415 | class WarningsDisplayTests(unittest.TestCase): |
| 416 | |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 417 | """Test the displaying of warnings and the ability to overload functions |
| 418 | related to displaying warnings.""" |
| 419 | |
Christian Heimes | 5fb7c2a | 2007-12-24 08:52:31 +0000 | [diff] [blame] | 420 | def test_formatwarning(self): |
| 421 | message = "msg" |
| 422 | category = Warning |
| 423 | file_name = os.path.splitext(warning_tests.__file__)[0] + '.py' |
| 424 | line_num = 3 |
| 425 | file_line = linecache.getline(file_name, line_num).strip() |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 426 | format = "%s:%s: %s: %s\n %s\n" |
| 427 | expect = format % (file_name, line_num, category.__name__, message, |
| 428 | file_line) |
| 429 | self.failUnlessEqual(expect, self.module.formatwarning(message, |
| 430 | category, file_name, line_num)) |
| 431 | # Test the 'line' argument. |
| 432 | file_line += " for the win!" |
| 433 | expect = format % (file_name, line_num, category.__name__, message, |
| 434 | file_line) |
| 435 | self.failUnlessEqual(expect, self.module.formatwarning(message, |
| 436 | category, file_name, line_num, file_line)) |
Christian Heimes | 5fb7c2a | 2007-12-24 08:52:31 +0000 | [diff] [blame] | 437 | |
| 438 | def test_showwarning(self): |
| 439 | file_name = os.path.splitext(warning_tests.__file__)[0] + '.py' |
| 440 | line_num = 3 |
| 441 | expected_file_line = linecache.getline(file_name, line_num).strip() |
| 442 | message = 'msg' |
| 443 | category = Warning |
| 444 | file_object = StringIO() |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 445 | expect = self.module.formatwarning(message, category, file_name, |
| 446 | line_num) |
| 447 | self.module.showwarning(message, category, file_name, line_num, |
Christian Heimes | 5fb7c2a | 2007-12-24 08:52:31 +0000 | [diff] [blame] | 448 | file_object) |
| 449 | self.failUnlessEqual(file_object.getvalue(), expect) |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 450 | # Test 'line' argument. |
| 451 | expected_file_line += "for the win!" |
| 452 | expect = self.module.formatwarning(message, category, file_name, |
| 453 | line_num, expected_file_line) |
| 454 | file_object = StringIO() |
| 455 | self.module.showwarning(message, category, file_name, line_num, |
| 456 | file_object, expected_file_line) |
| 457 | self.failUnlessEqual(expect, file_object.getvalue()) |
| 458 | |
| 459 | class CWarningsDisplayTests(BaseTest, WarningsDisplayTests): |
| 460 | module = c_warnings |
| 461 | |
| 462 | class PyWarningsDisplayTests(BaseTest, WarningsDisplayTests): |
| 463 | module = py_warnings |
Christian Heimes | 5fb7c2a | 2007-12-24 08:52:31 +0000 | [diff] [blame] | 464 | |
| 465 | |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 466 | def test_main(): |
Christian Heimes | dae2a89 | 2008-04-19 00:55:37 +0000 | [diff] [blame] | 467 | py_warnings.onceregistry.clear() |
| 468 | c_warnings.onceregistry.clear() |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 469 | test_support.run_unittest(CFilterTests, |
| 470 | PyFilterTests, |
| 471 | CWarnTests, |
| 472 | PyWarnTests, |
| 473 | CWCmdLineTests, PyWCmdLineTests, |
| 474 | _WarningsTests, |
| 475 | CWarningsDisplayTests, PyWarningsDisplayTests, |
| 476 | ) |
| 477 | |
Raymond Hettinger | d6f6e50 | 2003-07-13 08:37:40 +0000 | [diff] [blame] | 478 | |
| 479 | if __name__ == "__main__": |
Christian Heimes | 33fe809 | 2008-04-13 13:53:33 +0000 | [diff] [blame] | 480 | test_main() |