blob: cc1e9f4d50916165c34911f2dc24b8c49cc387e0 [file] [log] [blame]
Steven Bethardae42f332008-03-18 17:26:10 +00001import unittest
2from test.test_support import catch_warning, TestSkipped, run_unittest
3import warnings
4
5# TODO: This is a hack to raise TestSkipped if -3 is not enabled. Instead
6# of relying on callable to have a warning, we should expose the -3 flag
7# to Python code somehow
8with catch_warning() as w:
9 callable(int)
10 if w.message is None:
11 raise TestSkipped('%s must be run with the -3 flag' % __name__)
12
13class TestPy3KWarnings(unittest.TestCase):
14
15 def test_type_inequality_comparisons(self):
16 expected = 'type inequality comparisons not supported in 3.x.'
17 with catch_warning() as w:
18 self.assertWarning(int < str, w, expected)
19 with catch_warning() as w:
20 self.assertWarning(type < object, w, expected)
21
22 def test_object_inequality_comparisons(self):
23 expected = 'comparing unequal types not supported in 3.x.'
24 with catch_warning() as w:
25 self.assertWarning(str < [], w, expected)
26 with catch_warning() as w:
27 self.assertWarning(object() < (1, 2), w, expected)
28
29 def test_dict_inequality_comparisons(self):
30 expected = 'dict inequality comparisons not supported in 3.x.'
31 with catch_warning() as w:
32 self.assertWarning({} < {2:3}, w, expected)
33 with catch_warning() as w:
34 self.assertWarning({} <= {}, w, expected)
35 with catch_warning() as w:
36 self.assertWarning({} > {2:3}, w, expected)
37 with catch_warning() as w:
38 self.assertWarning({2:3} >= {}, w, expected)
39
40 def test_cell_inequality_comparisons(self):
41 expected = 'cell comparisons not supported in 3.x.'
42 def f(x):
43 def g():
44 return x
45 return g
46 cell0, = f(0).func_closure
47 cell1, = f(1).func_closure
48 with catch_warning() as w:
49 self.assertWarning(cell0 == cell1, w, expected)
50 with catch_warning() as w:
51 self.assertWarning(cell0 < cell1, w, expected)
52
David Wolever2724ab92008-03-19 02:35:45 +000053 def test_filter(self):
54 from itertools import ifilter
55 from future_builtins import filter
56 expected = 'ifilter with None as a first argument is not supported '\
57 'in 3.x. Use a list comprehension instead.'
58
59 with catch_warning() as w:
60 self.assertWarning(ifilter(None, []), w, expected)
61 with catch_warning() as w:
62 self.assertWarning(filter(None, []), w, expected)
63
Steven Bethard6a644f92008-03-18 22:08:20 +000064 def test_code_inequality_comparisons(self):
65 expected = 'code inequality comparisons not supported in 3.x.'
66 def f(x):
67 pass
68 def g(x):
69 pass
70 with catch_warning() as w:
71 self.assertWarning(f.func_code < g.func_code, w, expected)
72 with catch_warning() as w:
73 self.assertWarning(f.func_code <= g.func_code, w, expected)
74 with catch_warning() as w:
75 self.assertWarning(f.func_code >= g.func_code, w, expected)
76 with catch_warning() as w:
77 self.assertWarning(f.func_code > g.func_code, w, expected)
78
79 def test_builtin_function_or_method_comparisons(self):
80 expected = ('builtin_function_or_method '
81 'inequality comparisons not supported in 3.x.')
82 func = eval
83 meth = {}.get
84 with catch_warning() as w:
85 self.assertWarning(func < meth, w, expected)
86 with catch_warning() as w:
87 self.assertWarning(func > meth, w, expected)
88 with catch_warning() as w:
89 self.assertWarning(meth <= func, w, expected)
90 with catch_warning() as w:
91 self.assertWarning(meth >= func, w, expected)
92
Steven Bethardae42f332008-03-18 17:26:10 +000093 def assertWarning(self, _, warning, expected_message):
94 self.assertEqual(str(warning.message), expected_message)
95
96def test_main():
97 run_unittest(TestPy3KWarnings)
98
99if __name__ == '__main__':
100 test_main()