test_version.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. """Tests for distutils.version."""
  2. import unittest
  3. from distutils.version import LooseVersion
  4. from distutils.version import StrictVersion
  5. from test.support import run_unittest
  6. class VersionTestCase(unittest.TestCase):
  7. def test_prerelease(self):
  8. version = StrictVersion('1.2.3a1')
  9. self.assertEqual(version.version, (1, 2, 3))
  10. self.assertEqual(version.prerelease, ('a', 1))
  11. self.assertEqual(str(version), '1.2.3a1')
  12. version = StrictVersion('1.2.0')
  13. self.assertEqual(str(version), '1.2')
  14. def test_cmp_strict(self):
  15. versions = (('1.5.1', '1.5.2b2', -1),
  16. ('161', '3.10a', ValueError),
  17. ('8.02', '8.02', 0),
  18. ('3.4j', '1996.07.12', ValueError),
  19. ('3.2.pl0', '3.1.1.6', ValueError),
  20. ('2g6', '11g', ValueError),
  21. ('0.9', '2.2', -1),
  22. ('1.2.1', '1.2', 1),
  23. ('1.1', '1.2.2', -1),
  24. ('1.2', '1.1', 1),
  25. ('1.2.1', '1.2.2', -1),
  26. ('1.2.2', '1.2', 1),
  27. ('1.2', '1.2.2', -1),
  28. ('0.4.0', '0.4', 0),
  29. ('1.13++', '5.5.kw', ValueError))
  30. for v1, v2, wanted in versions:
  31. try:
  32. res = StrictVersion(v1)._cmp(StrictVersion(v2))
  33. except ValueError:
  34. if wanted is ValueError:
  35. continue
  36. else:
  37. raise AssertionError(("cmp(%s, %s) "
  38. "shouldn't raise ValueError")
  39. % (v1, v2))
  40. self.assertEqual(res, wanted,
  41. 'cmp(%s, %s) should be %s, got %s' %
  42. (v1, v2, wanted, res))
  43. res = StrictVersion(v1)._cmp(v2)
  44. self.assertEqual(res, wanted,
  45. 'cmp(%s, %s) should be %s, got %s' %
  46. (v1, v2, wanted, res))
  47. res = StrictVersion(v1)._cmp(object())
  48. self.assertIs(res, NotImplemented,
  49. 'cmp(%s, %s) should be NotImplemented, got %s' %
  50. (v1, v2, res))
  51. def test_cmp(self):
  52. versions = (('1.5.1', '1.5.2b2', -1),
  53. ('161', '3.10a', 1),
  54. ('8.02', '8.02', 0),
  55. ('3.4j', '1996.07.12', -1),
  56. ('3.2.pl0', '3.1.1.6', 1),
  57. ('2g6', '11g', -1),
  58. ('0.960923', '2.2beta29', -1),
  59. ('1.13++', '5.5.kw', -1))
  60. for v1, v2, wanted in versions:
  61. res = LooseVersion(v1)._cmp(LooseVersion(v2))
  62. self.assertEqual(res, wanted,
  63. 'cmp(%s, %s) should be %s, got %s' %
  64. (v1, v2, wanted, res))
  65. res = LooseVersion(v1)._cmp(v2)
  66. self.assertEqual(res, wanted,
  67. 'cmp(%s, %s) should be %s, got %s' %
  68. (v1, v2, wanted, res))
  69. res = LooseVersion(v1)._cmp(object())
  70. self.assertIs(res, NotImplemented,
  71. 'cmp(%s, %s) should be NotImplemented, got %s' %
  72. (v1, v2, res))
  73. def test_suite():
  74. return unittest.makeSuite(VersionTestCase)
  75. if __name__ == "__main__":
  76. run_unittest(test_suite())