diff --git a/packages/dbgpt-core/src/dbgpt/util/string_utils.py b/packages/dbgpt-core/src/dbgpt/util/string_utils.py index 41e60b97c7..0593378a5c 100644 --- a/packages/dbgpt-core/src/dbgpt/util/string_utils.py +++ b/packages/dbgpt-core/src/dbgpt/util/string_utils.py @@ -94,12 +94,10 @@ def extract_content_open_ending(long_string, s1, s2, is_include: bool = False): def str_to_bool(s): if s.lower() in ("true", "t", "1", "yes", "y"): return True - elif s.lower().startswith("true"): - return True elif s.lower() in ("false", "f", "0", "no", "n"): return False else: - return False + raise ValueError(f"Could not convert string to bool: '{s}'") def _to_str(x, charset="utf8", errors="strict"): diff --git a/tests/unit_tests/test_string_utils.py b/tests/unit_tests/test_string_utils.py new file mode 100644 index 0000000000..354b09b8b4 --- /dev/null +++ b/tests/unit_tests/test_string_utils.py @@ -0,0 +1,31 @@ +import pytest +from dbgpt.util.string_utils import str_to_bool + + +def test_str_to_bool_positive(): + assert str_to_bool("true") is True + assert str_to_bool("t") is True + assert str_to_bool("1") is True + assert str_to_bool("yes") is True + assert str_to_bool("y") is True + + +def test_str_to_bool_negative(): + assert str_to_bool("false") is False + assert str_to_bool("f") is False + assert str_to_bool("0") is False + assert str_to_bool("no") is False + assert str_to_bool("n") is False + + +def test_str_to_bool_edge_cases(): + with pytest.raises(ValueError): + str_to_bool("true_dat") + with pytest.raises(ValueError): + str_to_bool("false_dat") + with pytest.raises(ValueError): + str_to_bool("t_rue") + with pytest.raises(ValueError): + str_to_bool("f_alse") + with pytest.raises(ValueError): + str_to_bool("")