summaryrefslogtreecommitdiff
path: root/tests/test-moz-version
blob: db6c282885c4da46444e02fb8aefba2943684b6c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/usr/bin/python

# Copyright (c) 2010 Benjamin Drung <bdrung@ubuntu.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

import getopt
import os
import sys

sys.path.insert(1, os.path.abspath(os.path.dirname(sys.argv[0]) + "/../src"))
from moz_version import compare_versions

# error codes
COMMAND_LINE_SYNTAX_ERROR = 1
TEST_FAILED_ERROR = 2

TEST_PATTERN = (
    ("1.-1", "lt", "1", True),
    ("1", "eq", "1.", True),
    ("1", "eq", "1.0", True),
    ("1", "eq", "1.0.0", True),
    ("1.0.0", "lt", "1.1a", True),
    ("1.1a", "lt", "1.1aa", True),
    ("1.1aa", "lt", "1.1ab", True),
    ("1.1ab", "lt", "1.1b", True),
    ("1.1b", "lt", "1.1c", True),
    ("1.1pre", "eq", "1.1pre0", True),
    ("1.1pre0", "eq", "1.0+", True),
    ("1.0+", "lt", "1.1pre1a", True),
    ("1.1pre1a", "lt", "1.1pre1aa", True),
    ("1.1pre1aa", "lt", "1.1pre1b", True),
    ("1.1pre1b", "lt", "1.1pre1", True),
    ("1.1pre1", "lt", "1.1pre2", True),
    ("1.1pre2", "lt", "1.1pre10", True),
    ("1.1pre10", "lt", "1.1.-1", True),
    ("1.1.-1", "lt", "1.1", True),
    ("1.1", "eq", "1.1.0", True),
    ("1.1.0", "eq", "1.1.00", True),
    ("1.1.00", "lt", "1.10", True),
    ("1.10", "lt", "1.*", True),
    ("1.*", "lt", "1.*.1", True),
    ("1.*.1", "lt", "2.0", True),
    ("1.*.1", "gt", "2.0", False),
)

def fail(message):
    print >> sys.stderr, "E: " + message
    sys.exit(TEST_FAILED_ERROR)

def test_compare(verbose=False):
    for pattern in TEST_PATTERN:
        compare_result = compare_versions(pattern[0], pattern[2], verbose)
        if pattern[1] == "lt":
            if (compare_result < 0) != pattern[3]:
                fail('Test pattern "%s %s %s" failed (result should be %s).' % \
                     pattern)
        elif pattern[1] == "eq":
            if (compare_result == 0) != pattern[3]:
                fail('Test pattern "%s %s %s" failed (result should be %s).' % \
                     pattern)
        elif pattern[1] == "gt":
            if (compare_result > 0) != pattern[3]:
                fail('Test pattern "%s %s %s" failed (result should be %s).' % \
                     pattern)
        else:
            fail('Unknown pattern %s.' % (pattern[1]))

def main():
    try:
        long_opts = ["verbose"]
        opts = getopt.gnu_getopt(sys.argv[1:], "v", long_opts)[0]
    except getopt.GetoptError, e:
        # print help information and exit:
        print >> sys.stderr, str(e)
        sys.exit(COMMAND_LINE_SYNTAX_ERROR)

    verbose = False

    for o, _ in opts:
        if o in ("-v", "--verbose"):
            verbose = True
        else:
            assert False, "unhandled option"

    test_compare(verbose)

if __name__ == "__main__":
    main()