#!/usr/bin/python # Copyright (c) 2010 Benjamin Drung # # 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()