test___future__.py 2.4 KB

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