test_future.py 4.05 KB
Newer Older
1 2
# Test various flavors of legal and illegal future statements

3
import unittest
4
from test import support
5 6 7 8
import re

rx = re.compile('\((\S+).py, line (\d+)')

9 10 11
def get_error_location(msg):
    mo = rx.search(str(msg))
    return mo.group(1, 2)
12

13
class FutureTest(unittest.TestCase):
14

15
    def test_future1(self):
16 17 18
        with support.CleanImport('future_test1'):
            from test import future_test1
            self.assertEqual(future_test1.result, 6)
19

20
    def test_future2(self):
21 22 23
        with support.CleanImport('future_test2'):
            from test import future_test2
            self.assertEqual(future_test2.result, 6)
24

25
    def test_future3(self):
26 27
        with support.CleanImport('test_future3'):
            from test import test_future3
28

29 30 31
    def test_badfuture3(self):
        try:
            from test import badsyntax_future3
32
        except SyntaxError as msg:
33 34 35
            self.assertEqual(get_error_location(msg), ("badsyntax_future3", '3'))
        else:
            self.fail("expected exception didn't occur")
36

37 38 39
    def test_badfuture4(self):
        try:
            from test import badsyntax_future4
40
        except SyntaxError as msg:
41 42 43
            self.assertEqual(get_error_location(msg), ("badsyntax_future4", '3'))
        else:
            self.fail("expected exception didn't occur")
44

45 46 47
    def test_badfuture5(self):
        try:
            from test import badsyntax_future5
48
        except SyntaxError as msg:
49 50 51
            self.assertEqual(get_error_location(msg), ("badsyntax_future5", '4'))
        else:
            self.fail("expected exception didn't occur")
52

53 54 55
    def test_badfuture6(self):
        try:
            from test import badsyntax_future6
56
        except SyntaxError as msg:
57 58 59
            self.assertEqual(get_error_location(msg), ("badsyntax_future6", '3'))
        else:
            self.fail("expected exception didn't occur")
60

61 62 63
    def test_badfuture7(self):
        try:
            from test import badsyntax_future7
64
        except SyntaxError as msg:
65 66 67 68 69 70 71
            self.assertEqual(get_error_location(msg), ("badsyntax_future7", '3'))
        else:
            self.fail("expected exception didn't occur")

    def test_badfuture8(self):
        try:
            from test import badsyntax_future8
72
        except SyntaxError as msg:
73 74 75 76 77 78 79
            self.assertEqual(get_error_location(msg), ("badsyntax_future8", '3'))
        else:
            self.fail("expected exception didn't occur")

    def test_badfuture9(self):
        try:
            from test import badsyntax_future9
80
        except SyntaxError as msg:
81 82 83 84
            self.assertEqual(get_error_location(msg), ("badsyntax_future9", '3'))
        else:
            self.fail("expected exception didn't occur")

85 86 87 88 89 90 91 92
    def test_badfuture10(self):
        try:
            from test import badsyntax_future10
        except SyntaxError as msg:
            self.assertEqual(get_error_location(msg), ("badsyntax_future10", '3'))
        else:
            self.fail("expected exception didn't occur")

93 94 95 96 97 98 99
    def test_parserhack(self):
        # test that the parser.c::future_hack function works as expected
        # Note: although this test must pass, it's not testing the original
        #       bug as of 2.6 since the with statement is not optional and
        #       the parser hack disabled. If a new keyword is introduced in
        #       2.6, change this to refer to the new future import.
        try:
100
            exec("from __future__ import print_function; print 0")
101 102 103 104 105 106
        except SyntaxError:
            pass
        else:
            self.fail("syntax error didn't occur")

        try:
107
            exec("from __future__ import (print_function); print 0")
108 109 110 111 112
        except SyntaxError:
            pass
        else:
            self.fail("syntax error didn't occur")

113
    def test_multiple_features(self):
114 115
        with support.CleanImport("test.test_future5"):
            from test import test_future5
116

117 118 119
    def test_unicode_literals_exec(self):
        scope = {}
        exec("from __future__ import unicode_literals; x = ''", {}, scope)
120
        self.assertIsInstance(scope["x"], str)
121

122

123 124

if __name__ == "__main__":
125
    unittest.main()