|
6 | 6 | CPython's _warnings module. http://docs.python.org/library/warnings.html |
7 | 7 | ''' |
8 | 8 |
|
9 | | -import unittest |
10 | 9 | import _warnings |
11 | | -from iptest import IronPythonTestCase, run_test, stderr_trapper as stderr_trapper |
| 10 | +from iptest import IronPythonTestCase, stderr_trapper, run_test |
12 | 11 |
|
13 | 12 | #--GLOBALS--------------------------------------------------------------------- |
14 | | -EXPECTED = [] # expected output (ignoring filename, lineno, and line) |
15 | 13 | WARN_TYPES = [Warning, UserWarning, PendingDeprecationWarning, SyntaxWarning, |
16 | 14 | RuntimeWarning, FutureWarning, ImportWarning, UnicodeWarning, |
17 | 15 | BytesWarning] |
18 | 16 |
|
19 | | -def cleanup(): |
20 | | - '''Clean up after possible incomplete test runs.''' |
21 | | - global EXPECTED |
22 | | - EXPECTED = [] |
23 | | - |
24 | | - |
25 | | -def expect(warn_type, message): |
26 | | - '''Helper for test output''' |
27 | | - for filter in _warnings.filters: |
28 | | - if filter[0] == "ignore" and issubclass(warn_type, filter[2]): |
29 | | - return |
30 | | - EXPECTED.append(": " + warn_type.__name__ + ": " + message + "\n") |
31 | | - |
32 | | - |
33 | 17 | class _WarningsTest(IronPythonTestCase): |
34 | 18 | #TODO: @skip("multiple_execute") |
35 | 19 | def test_sanity(self): |
36 | | - global EXPECTED |
37 | | - try: |
38 | | - with stderr_trapper() as output: |
39 | | - # generate test output |
40 | | - _warnings.warn("Warning Message!") |
41 | | - expect(UserWarning, "Warning Message!") |
42 | | - for warn_type in WARN_TYPES: |
43 | | - _warnings.warn(warn_type("Type-overriding message!"), UnicodeWarning) |
44 | | - expect(warn_type, "Type-overriding message!") |
45 | | - _warnings.warn("Another Warning Message!", warn_type) |
46 | | - expect(warn_type, "Another Warning Message!") |
47 | | - _warnings.warn_explicit("Explicit Warning!", warn_type, "nonexistent_file.py", 12) |
48 | | - expect(warn_type, "Explicit Warning!") |
49 | | - _warnings.warn_explicit("Explicit Warning!", warn_type, "test_python26.py", 34) |
50 | | - expect(warn_type, "Explicit Warning!") |
51 | | - _warnings.warn_explicit("Explicit Warning!", warn_type, "nonexistent_file.py", 56, "module.py") |
52 | | - expect(warn_type, "Explicit Warning!") |
53 | | - _warnings.warn_explicit("Explicit Warning!", warn_type, "test_python26.py", 78, "module.py") |
54 | | - expect(warn_type, "Explicit Warning!") |
55 | | - |
56 | | - temp_messages = output.messages |
57 | | - |
58 | | - #No point in going further if the number of lines is not what we expect |
59 | | - nlines = len([x for x in temp_messages if not x.startswith(" ")]) |
60 | | - self.assertEqual(nlines, len(EXPECTED)) |
61 | | - |
62 | | - # match lines |
63 | | - for line in temp_messages: |
64 | | - if line.startswith(" "): |
65 | | - continue |
66 | | - temp = EXPECTED.pop(0).rstrip() |
67 | | - self.assertTrue(line.endswith(temp), str(line) + " does not end with " + temp) |
68 | | - |
69 | | - finally: |
70 | | - # remove generated files |
71 | | - cleanup() |
| 20 | + EXPECTED = [] # expected output (ignoring filename, lineno, and line) |
| 21 | + |
| 22 | + def expect(warn_type, message): |
| 23 | + '''Helper for test output''' |
| 24 | + # unittest will reset the warning filters so the base filters do nothing |
| 25 | + #for filter in _warnings.filters: |
| 26 | + # if filter[0] == "ignore" and issubclass(warn_type, filter[2]): |
| 27 | + # return |
| 28 | + EXPECTED.append(": " + warn_type.__name__ + ": " + message + "\n") |
| 29 | + |
| 30 | + with stderr_trapper() as output: |
| 31 | + # generate test output |
| 32 | + _warnings.warn("Warning Message!") |
| 33 | + expect(UserWarning, "Warning Message!") |
| 34 | + for warn_type in WARN_TYPES: |
| 35 | + _warnings.warn(warn_type("Type-overriding message!"), UnicodeWarning) |
| 36 | + expect(warn_type, "Type-overriding message!") |
| 37 | + _warnings.warn("Another Warning Message!", warn_type) |
| 38 | + expect(warn_type, "Another Warning Message!") |
| 39 | + _warnings.warn_explicit("Explicit Warning!", warn_type, "nonexistent_file.py", 12) |
| 40 | + expect(warn_type, "Explicit Warning!") |
| 41 | + _warnings.warn_explicit("Explicit Warning!", warn_type, "test_python26.py", 34) |
| 42 | + expect(warn_type, "Explicit Warning!") |
| 43 | + _warnings.warn_explicit("Explicit Warning!", warn_type, "nonexistent_file.py", 56, "module.py") |
| 44 | + expect(warn_type, "Explicit Warning!") |
| 45 | + _warnings.warn_explicit("Explicit Warning!", warn_type, "test_python26.py", 78, "module.py") |
| 46 | + expect(warn_type, "Explicit Warning!") |
| 47 | + |
| 48 | + temp_messages = output.messages |
| 49 | + |
| 50 | + #No point in going further if the number of lines is not what we expect |
| 51 | + nlines = len([x for x in temp_messages if not x.startswith(" ")]) |
| 52 | + self.assertEqual(nlines, len(EXPECTED)) |
| 53 | + |
| 54 | + # match lines |
| 55 | + for line in temp_messages: |
| 56 | + if line.startswith(" "): |
| 57 | + continue |
| 58 | + temp = EXPECTED.pop(0).rstrip() |
| 59 | + self.assertTrue(line.endswith(temp), str(line) + " does not end with " + temp) |
72 | 60 |
|
73 | 61 | def test_ipy2_gh23(self): |
74 | 62 | def test(): |
|
0 commit comments