Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions packages/dbgpt-core/src/dbgpt/util/string_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"):
Expand Down
31 changes: 31 additions & 0 deletions tests/unit_tests/test_string_utils.py
Original file line number Diff line number Diff line change
@@ -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("")