|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# |
| 3 | +# Copyright (C) 2021 - 2025 ANSYS, Inc. and/or its affiliates. |
| 4 | +# SPDX-License-Identifier: MIT |
| 5 | +# |
| 6 | +# |
| 7 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | +# of this software and associated documentation files (the "Software"), to deal |
| 9 | +# in the Software without restriction, including without limitation the rights |
| 10 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | +# copies of the Software, and to permit persons to whom the Software is |
| 12 | +# furnished to do so, subject to the following conditions: |
| 13 | +# |
| 14 | +# The above copyright notice and this permission notice shall be included in all |
| 15 | +# copies or substantial portions of the Software. |
| 16 | +# |
| 17 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 | +# SOFTWARE. |
| 24 | + |
| 25 | +import pytest |
| 26 | + |
| 27 | +from ansys.aedt.core.generic.general_methods import _is_version_format_valid |
| 28 | +from ansys.aedt.core.generic.general_methods import _normalize_version_to_string |
| 29 | +from ansys.aedt.core.generic.general_methods import number_aware_string_key |
| 30 | + |
| 31 | + |
| 32 | +@pytest.fixture(scope="module", autouse=True) |
| 33 | +def desktop(): |
| 34 | + """Override the desktop fixture to DO NOT open the Desktop when running this test class""" |
| 35 | + return |
| 36 | + |
| 37 | + |
| 38 | +class TestGeneralMethods: |
| 39 | + def test_00_number_aware_string_key(self): |
| 40 | + assert number_aware_string_key("C1") == ("C", 1) |
| 41 | + assert number_aware_string_key("1234asdf") == (1234, "asdf") |
| 42 | + assert number_aware_string_key("U100") == ("U", 100) |
| 43 | + assert number_aware_string_key("U100X0") == ("U", 100, "X", 0) |
| 44 | + |
| 45 | + def test_01_number_aware_string_key(self): |
| 46 | + component_names = ["U10", "U2", "C1", "Y1000", "Y200"] |
| 47 | + expected_sort_order = ["C1", "U2", "U10", "Y200", "Y1000"] |
| 48 | + assert sorted(component_names, key=number_aware_string_key) == expected_sort_order |
| 49 | + assert sorted(component_names + [""], key=number_aware_string_key) == [""] + expected_sort_order |
| 50 | + |
| 51 | + def test_valid_full_year_float(self): |
| 52 | + assert _normalize_version_to_string(2023.2) == "2023.2" |
| 53 | + assert _normalize_version_to_string("2024.5") == "2024.5" |
| 54 | + |
| 55 | + def test_valid_short_year_float(self): |
| 56 | + assert _normalize_version_to_string(23.4) == "2023.4" |
| 57 | + assert _normalize_version_to_string("25.9") == "2025.9" |
| 58 | + |
| 59 | + def test_valid_int_formats(self): |
| 60 | + assert _normalize_version_to_string(232) == "2023.2" |
| 61 | + assert _normalize_version_to_string("245") == "2024.5" |
| 62 | + |
| 63 | + def test_valid_string_R_formats(self): |
| 64 | + assert _normalize_version_to_string("2025R2") == "2025.2" |
| 65 | + assert _normalize_version_to_string("2025 R2") == "2025.2" |
| 66 | + assert _normalize_version_to_string("25R2") == "2025.2" |
| 67 | + assert _normalize_version_to_string("25 R2") == "2025.2" |
| 68 | + |
| 69 | + def test_none_value_as_version(self): |
| 70 | + assert _normalize_version_to_string(None) is None |
| 71 | + |
| 72 | + def test_invalid_types(self): |
| 73 | + with pytest.raises(ValueError): |
| 74 | + _normalize_version_to_string([2023.2]) # list not allowed |
| 75 | + with pytest.raises(ValueError): |
| 76 | + _normalize_version_to_string({"year": 2023.2}) # dict not allowed |
| 77 | + |
| 78 | + def test_invalid_formats(self): |
| 79 | + with pytest.raises(ValueError): |
| 80 | + _normalize_version_to_string("20232") # too many digits |
| 81 | + with pytest.raises(ValueError): |
| 82 | + _normalize_version_to_string("2023.23") # too many decimals |
| 83 | + with pytest.raises(ValueError): |
| 84 | + _normalize_version_to_string("23") # too short |
| 85 | + with pytest.raises(ValueError): |
| 86 | + _normalize_version_to_string("1999.2") # not 20XX |
| 87 | + with pytest.raises(ValueError): |
| 88 | + _normalize_version_to_string("20A3.2") # invalid chars |
| 89 | + with pytest.raises(ValueError): |
| 90 | + _normalize_version_to_string("2025 R") # missing digit after R |
| 91 | + |
| 92 | + def test_edge_cases(self): |
| 93 | + assert _normalize_version_to_string("2000.0") == "2000.0" # earliest year |
| 94 | + assert _normalize_version_to_string("2099.9") == "2099.9" # latest year |
| 95 | + assert _normalize_version_to_string("00.1") == "2000.1" # short float lowest |
| 96 | + assert _normalize_version_to_string("99.9") == "2099.9" # short float highest |
| 97 | + assert _normalize_version_to_string("000") == "2000.0" # int lowest |
| 98 | + assert _normalize_version_to_string("999") == "2099.9" # int highest |
| 99 | + assert _normalize_version_to_string("00R1") == "2000.1" # short R format |
| 100 | + assert _normalize_version_to_string("99R9") == "2099.9" # short R format max |
| 101 | + assert _normalize_version_to_string("2000R1") == "2000.1" # long R format |
| 102 | + assert _normalize_version_to_string("2099R9") == "2099.9" # long R format max |
| 103 | + |
| 104 | + @pytest.mark.parametrize( |
| 105 | + "version", |
| 106 | + [ |
| 107 | + "2023.2", |
| 108 | + "2023.12", |
| 109 | + "1999.1", |
| 110 | + "2023.2SV", |
| 111 | + "2023.12SV", |
| 112 | + ], |
| 113 | + ) |
| 114 | + def test_valid_versions(self, version): |
| 115 | + assert _is_version_format_valid(version) |
| 116 | + |
| 117 | + @pytest.mark.parametrize( |
| 118 | + "version", |
| 119 | + [ |
| 120 | + "23.2", # too short year |
| 121 | + "202.2", # too short year |
| 122 | + "20234.2", # too long year |
| 123 | + "2023.02", # leading zero |
| 124 | + "2023", # missing minor version |
| 125 | + "2023.", # dangling dot |
| 126 | + "2023.2Extra", # extra text |
| 127 | + "2023.2 SV", # space before SV |
| 128 | + "", # empty |
| 129 | + None, # None value |
| 130 | + 2023.2, # float type |
| 131 | + ], |
| 132 | + ) |
| 133 | + def test_invalid_versions(self, version): |
| 134 | + assert not _is_version_format_valid(version) |
0 commit comments