# Copyright (C) 2023 Intel Corporation
# SPDX-License-Identifier: GPL-3.0-or-later

import pytest

from cve_bin_tool.version_compare import UnknownVersion, Version


class TestVersionCompare:
    """Test the cve_bin_tool.version_compare functionality"""

    def test_eq(self):
        """Make sure == works between versions"""
        assert Version("1.2") == Version("1.2")
        assert Version("1.1a") == Version("1.1A")
        assert Version("4.4.A") == Version("4.4.a")
        assert Version("5.6   ") == Version("5.6")

    def test_lt(self):
        """Make sure < works between versions, including some with unusual version schemes"""
        assert Version("1.2") < Version("1.3")
        assert Version("1.2a") < Version("1.3")
        assert Version("1_2a") < Version("5a")
        assert Version("5.6.dev1") < Version("5.6")
        assert Version("5.6.dev1") < Version("5.6.dev2")
        assert Version("5.4") < Version("5.4.1")
        assert Version("5.4.dev5") < Version("5.4.1")
        assert Version("1.2.pre4") < Version("1.2.1")
        assert Version("1.2.post8") < Version("1.2.1")
        assert Version("rc5") < Version("rc10")
        assert Version("9.10") < Version("9.10.post")
        assert Version("5.3.9") < Version("5.4")

    def test_gt(self):
        """Make sure > works between versions, including some with unusual version schemes"""
        assert Version("1.1.1a") > Version("1.0.1z")
        assert Version("2-kdc") > Version("2-a")
        assert Version("7.34.0") > Version("7.3.0")
        assert Version("5.6.1") > Version("5.6.dev2")
        assert Version("5.4.6") > Version("5.4")
        assert Version("10.2.3.rc1") > Version("10.2.3.rc0")
        assert Version("10.2.3.rc10") > Version("10.2.3.rc2")
        assert Version("9.10.post") > Version("9.10")
        assert Version("5.5") > Version("5.4.1")

    def test_error(self):
        """Make sure 'unknown' and blank strings raise appropriate errors"""
        with pytest.raises(UnknownVersion):
            Version("6") > Version("unknown")
            Version("") > Version("6")
