~ubuntu-branches/ubuntu/utopic/dogtail/utopic

« back to all changes in this revision

Viewing changes to dogtail/version.py

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Holbach
  • Date: 2006-12-21 13:33:47 UTC
  • mfrom: (1.2.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 5.
  • Revision ID: james.westby@ubuntu.com-20061221133347-xo9jg11afp5plcka
Tags: upstream-0.6.1
ImportĀ upstreamĀ versionĀ 0.6.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
7
7
import unittest
8
8
 
9
9
class Version:
10
 
        """
11
 
        Class representing a version of a software package.
12
 
        Stored internally as a list of subversions, from major to minor.
13
 
        Overloaded comparison operators ought to work sanely.
14
 
        """
15
 
        def __init__(self, versionList):
16
 
                self.versionList = versionList
17
 
 
18
 
        def fromString(versionString):
19
 
                """
20
 
                Parse a string of the form number.number.number
21
 
                """
22
 
                return Version(map(int, versionString.split(".")))
23
 
        fromString = staticmethod(fromString)
24
 
 
25
 
        def __str__(self):
26
 
                return ".".join(map(str, self.versionList))
27
 
 
28
 
        def __getNum(self):
29
 
                tmpList = list(self.versionList)
30
 
 
31
 
                while len(tmpList)<5:
32
 
                        tmpList += [0]
33
 
 
34
 
                num = 0
35
 
                for i in range(len(tmpList)):
36
 
                        num *=1000
37
 
                        num += tmpList[i]
38
 
                return num
39
 
                
40
 
        def __lt__(self, other):
41
 
                return self.__getNum()<other.__getNum()
42
 
        def __le__(self, other):
43
 
                return self.__getNum()<=other.__getNum()
44
 
        def __eq__(self, other):
45
 
                return self.__getNum()==other.__getNum()
46
 
        def __ne__(self, other):
47
 
                return self.__getNum()!=other.__getNum()
48
 
        def __gt__(self, other):
49
 
                return self.__getNum()>other.__getNum()
50
 
        def __ge__(self, other):
51
 
                return self.__getNum()>=other.__getNum()
 
10
    """
 
11
    Class representing a version of a software package.
 
12
    Stored internally as a list of subversions, from major to minor.
 
13
    Overloaded comparison operators ought to work sanely.
 
14
    """
 
15
    def __init__(self, versionList):
 
16
        self.versionList = versionList
 
17
 
 
18
    def fromString(versionString):
 
19
        """
 
20
        Parse a string of the form number.number.number
 
21
        """
 
22
        return Version(map(int, versionString.split(".")))
 
23
    fromString = staticmethod(fromString)
 
24
 
 
25
    def __str__(self):
 
26
        return ".".join(map(str, self.versionList))
 
27
 
 
28
    def __getNum(self):
 
29
        tmpList = list(self.versionList)
 
30
 
 
31
        while len(tmpList)<5:
 
32
            tmpList += [0]
 
33
 
 
34
        num = 0
 
35
        for i in range(len(tmpList)):
 
36
            num *=1000
 
37
            num += tmpList[i]
 
38
        return num
 
39
 
 
40
    def __lt__(self, other):
 
41
        return self.__getNum()<other.__getNum()
 
42
    def __le__(self, other):
 
43
        return self.__getNum()<=other.__getNum()
 
44
    def __eq__(self, other):
 
45
        return self.__getNum()==other.__getNum()
 
46
    def __ne__(self, other):
 
47
        return self.__getNum()!=other.__getNum()
 
48
    def __gt__(self, other):
 
49
        return self.__getNum()>other.__getNum()
 
50
    def __ge__(self, other):
 
51
        return self.__getNum()>=other.__getNum()
52
52
 
53
53
class VersionTests(unittest.TestCase):
54
 
        def testLt(self):
55
 
                assert Version.fromString("2.0.1") < Version.fromString("2.1.0")
56
 
                assert not Version.fromString("1.4.0") < Version.fromString("1.4.0")
57
 
 
58
 
        def testLe(self):
59
 
                assert Version.fromString("2.0.1") <= Version.fromString("2.1.0")
60
 
                assert Version.fromString("1.4.0") <= Version.fromString("1.4.0")
61
 
 
62
 
        def testEq(self):
63
 
                assert not Version.fromString("2.0.1") == Version.fromString("2.1.0")
64
 
                assert Version.fromString("1.4.0") == Version.fromString("1.4.0")
65
 
 
66
 
        def testNe(self):
67
 
                assert Version.fromString("2.0.1") != Version.fromString("2.1.0")
68
 
                assert not Version.fromString("1.4.0") != Version.fromString("1.4.0")
69
 
 
70
 
        def testGt(self):
71
 
                assert Version.fromString("2.1.0") > Version.fromString("2.0.1")
72
 
                assert not Version.fromString("1.4.0") > Version.fromString("1.4.0")
73
 
 
74
 
        def testGe(self):
75
 
                assert Version.fromString("2.1.0") >= Version.fromString("2.0.1")
76
 
                assert Version.fromString("1.4.0") >= Version.fromString("1.4.0")
77
 
 
78
 
        def testStr(self):
79
 
                assert "2.0.4"==str(Version.fromString("2.0.4"))
80
 
 
81
 
        def testParsing(self):
82
 
                assert Version.fromString("0") == Version([0])
83
 
                assert Version.fromString("0.1") == Version([0, 1])
84
 
                assert Version.fromString("1.4.0") == Version([1, 4, 0])
 
54
    def testLt(self):
 
55
        assert Version.fromString("2.0.1") < Version.fromString("2.1.0")
 
56
        assert not Version.fromString("1.4.0") < Version.fromString("1.4.0")
 
57
 
 
58
    def testLe(self):
 
59
        assert Version.fromString("2.0.1") <= Version.fromString("2.1.0")
 
60
        assert Version.fromString("1.4.0") <= Version.fromString("1.4.0")
 
61
 
 
62
    def testEq(self):
 
63
        assert not Version.fromString("2.0.1") == Version.fromString("2.1.0")
 
64
        assert Version.fromString("1.4.0") == Version.fromString("1.4.0")
 
65
 
 
66
    def testNe(self):
 
67
        assert Version.fromString("2.0.1") != Version.fromString("2.1.0")
 
68
        assert not Version.fromString("1.4.0") != Version.fromString("1.4.0")
 
69
 
 
70
    def testGt(self):
 
71
        assert Version.fromString("2.1.0") > Version.fromString("2.0.1")
 
72
        assert not Version.fromString("1.4.0") > Version.fromString("1.4.0")
 
73
 
 
74
    def testGe(self):
 
75
        assert Version.fromString("2.1.0") >= Version.fromString("2.0.1")
 
76
        assert Version.fromString("1.4.0") >= Version.fromString("1.4.0")
 
77
 
 
78
    def testStr(self):
 
79
        assert "2.0.4"==str(Version.fromString("2.0.4"))
 
80
 
 
81
    def testParsing(self):
 
82
        assert Version.fromString("0") == Version([0])
 
83
        assert Version.fromString("0.1") == Version([0, 1])
 
84
        assert Version.fromString("1.4.0") == Version([1, 4, 0])
85
85
 
86
86
if __name__ == "__main__":
87
 
        unittest.main()
 
87
    unittest.main()