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