|
1 | | -# Copyright (c) 2015-2018 Cisco Systems, Inc. # noqa: D100 |
| 1 | +# Copyright (c) 2015-2018 Cisco Systems, Inc. |
2 | 2 | # |
3 | 3 | # Permission is hereby granted, free of charge, to any person obtaining a copy |
4 | 4 | # of this software and associated documentation files (the "Software"), to |
|
17 | 17 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
18 | 18 | # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
19 | 19 | # DEALINGS IN THE SOFTWARE. |
| 20 | +"""Test the util module.""" |
| 21 | + |
| 22 | +# pylint: disable=too-many-lines |
| 23 | + |
20 | 24 | from __future__ import annotations |
21 | 25 |
|
22 | 26 | import binascii |
@@ -938,3 +942,75 @@ def test_lookup_config_file_collection_no_extensions_dir( |
938 | 942 |
|
939 | 943 | # Should fall back to home directory since collection extensions dir doesn't exist |
940 | 944 | assert result == str(home_config) |
| 945 | + |
| 946 | + |
| 947 | +@pytest.mark.parametrize( |
| 948 | + ("input_value", "expected"), |
| 949 | + ( |
| 950 | + (True, True), |
| 951 | + (False, False), |
| 952 | + ("yes", True), |
| 953 | + ("YES", True), |
| 954 | + ("on", True), |
| 955 | + ("ON", True), |
| 956 | + ("1", True), |
| 957 | + ("true", True), |
| 958 | + ("TRUE", True), |
| 959 | + ("no", False), |
| 960 | + ("off", False), |
| 961 | + ("0", False), |
| 962 | + ("false", False), |
| 963 | + (1, True), |
| 964 | + (0, False), |
| 965 | + ("", False), |
| 966 | + ), |
| 967 | +) |
| 968 | +def test_boolean_valid_inputs(input_value: object, expected: bool) -> None: # noqa: FBT001 |
| 969 | + """Test boolean function with valid inputs. |
| 970 | +
|
| 971 | + Args: |
| 972 | + input_value: The input value to test. |
| 973 | + expected: The expected boolean result. |
| 974 | + """ |
| 975 | + assert util.boolean(input_value) is expected |
| 976 | + |
| 977 | + |
| 978 | +@pytest.mark.parametrize( |
| 979 | + "input_value", |
| 980 | + ( |
| 981 | + None, |
| 982 | + "random", |
| 983 | + 42, |
| 984 | + "invalid", |
| 985 | + ), |
| 986 | +) |
| 987 | +def test_boolean_invalid_values(input_value: object) -> None: |
| 988 | + """Test boolean function raises TypeError for invalid inputs. |
| 989 | +
|
| 990 | + Args: |
| 991 | + input_value: The invalid input value to test. |
| 992 | + """ |
| 993 | + with pytest.raises(TypeError): |
| 994 | + util.boolean(input_value) |
| 995 | + |
| 996 | + |
| 997 | +@pytest.mark.parametrize( |
| 998 | + ("input_value", "default_value", "expected"), |
| 999 | + ( |
| 1000 | + (None, False, False), |
| 1001 | + ("random", True, True), |
| 1002 | + (42, False, False), |
| 1003 | + ("invalid", True, True), |
| 1004 | + ("yes", False, True), # Valid input should ignore default |
| 1005 | + ("no", True, False), # Valid input should ignore default |
| 1006 | + ), |
| 1007 | +) |
| 1008 | +def test_boolean_with_default(input_value: object, default_value: bool, expected: bool) -> None: # noqa: FBT001 |
| 1009 | + """Test boolean function with default parameter. |
| 1010 | +
|
| 1011 | + Args: |
| 1012 | + input_value: The input value to test. |
| 1013 | + default_value: The default value to use for invalid inputs. |
| 1014 | + expected: The expected boolean result. |
| 1015 | + """ |
| 1016 | + assert util.boolean(input_value, default=default_value) is expected |
0 commit comments