• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1import unittest
2from test import test_support
3import __future__
4
5GOOD_SERIALS = ("alpha", "beta", "candidate", "final")
6
7features = __future__.all_feature_names
8
9class FutureTest(unittest.TestCase):
10
11    def test_names(self):
12        # Verify that all_feature_names appears correct.
13        given_feature_names = features[:]
14        for name in dir(__future__):
15            obj = getattr(__future__, name, None)
16            if obj is not None and isinstance(obj, __future__._Feature):
17                self.assertTrue(
18                    name in given_feature_names,
19                    "%r should have been in all_feature_names" % name
20                )
21                given_feature_names.remove(name)
22        self.assertEqual(len(given_feature_names), 0,
23               "all_feature_names has too much: %r" % given_feature_names)
24
25    def test_attributes(self):
26        for feature in features:
27            value = getattr(__future__, feature)
28
29            optional = value.getOptionalRelease()
30            mandatory = value.getMandatoryRelease()
31
32            a = self.assertTrue
33            e = self.assertEqual
34            def check(t, name):
35                a(isinstance(t, tuple), "%s isn't tuple" % name)
36                e(len(t), 5, "%s isn't 5-tuple" % name)
37                (major, minor, micro, level, serial) = t
38                a(isinstance(major, int), "%s major isn't int"  % name)
39                a(isinstance(minor, int), "%s minor isn't int" % name)
40                a(isinstance(micro, int), "%s micro isn't int" % name)
41                a(isinstance(level, basestring),
42                    "%s level isn't string" % name)
43                a(level in GOOD_SERIALS,
44                       "%s level string has unknown value" % name)
45                a(isinstance(serial, int), "%s serial isn't int" % name)
46
47            check(optional, "optional")
48            if mandatory is not None:
49                check(mandatory, "mandatory")
50                a(optional < mandatory,
51                       "optional not less than mandatory, and mandatory not None")
52
53            a(hasattr(value, "compiler_flag"),
54                   "feature is missing a .compiler_flag attr")
55            # Make sure the compile accepts the flag.
56            compile("", "<test>", "exec", value.compiler_flag)
57            a(isinstance(getattr(value, "compiler_flag"), int),
58                   ".compiler_flag isn't int")
59
60
61def test_main():
62    test_support.run_unittest(FutureTest)
63
64if __name__ == "__main__":
65    test_main()
66