Steven Bethard | ae42f33 | 2008-03-18 17:26:10 +0000 | [diff] [blame] | 1 | import unittest |
| 2 | from test.test_support import catch_warning, TestSkipped, run_unittest |
| 3 | import 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 |
| 8 | with 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 | |
| 13 | class 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 | |
Steven Bethard | 6a644f9 | 2008-03-18 22:08:20 +0000 | [diff] [blame] | 53 | def test_code_inequality_comparisons(self): |
| 54 | expected = 'code inequality comparisons not supported in 3.x.' |
| 55 | def f(x): |
| 56 | pass |
| 57 | def g(x): |
| 58 | pass |
| 59 | with catch_warning() as w: |
| 60 | self.assertWarning(f.func_code < g.func_code, w, expected) |
| 61 | with catch_warning() as w: |
| 62 | self.assertWarning(f.func_code <= g.func_code, w, expected) |
| 63 | with catch_warning() as w: |
| 64 | self.assertWarning(f.func_code >= g.func_code, w, expected) |
| 65 | with catch_warning() as w: |
| 66 | self.assertWarning(f.func_code > g.func_code, w, expected) |
| 67 | |
| 68 | def test_builtin_function_or_method_comparisons(self): |
| 69 | expected = ('builtin_function_or_method ' |
| 70 | 'inequality comparisons not supported in 3.x.') |
| 71 | func = eval |
| 72 | meth = {}.get |
| 73 | with catch_warning() as w: |
| 74 | self.assertWarning(func < meth, w, expected) |
| 75 | with catch_warning() as w: |
| 76 | self.assertWarning(func > meth, w, expected) |
| 77 | with catch_warning() as w: |
| 78 | self.assertWarning(meth <= func, w, expected) |
| 79 | with catch_warning() as w: |
| 80 | self.assertWarning(meth >= func, w, expected) |
| 81 | |
Steven Bethard | ae42f33 | 2008-03-18 17:26:10 +0000 | [diff] [blame] | 82 | def assertWarning(self, _, warning, expected_message): |
| 83 | self.assertEqual(str(warning.message), expected_message) |
| 84 | |
| 85 | def test_main(): |
| 86 | run_unittest(TestPy3KWarnings) |
| 87 | |
| 88 | if __name__ == '__main__': |
| 89 | test_main() |