Skip to content

Commit

Permalink
Refactor PR #128
Browse files Browse the repository at this point in the history
  • Loading branch information
henriquebastos committed Feb 2, 2022
1 parent 7e90cd3 commit a4b104d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 26 deletions.
20 changes: 12 additions & 8 deletions decouple.py
Expand Up @@ -26,15 +26,19 @@
DEFAULT_ENCODING = 'UTF-8'


# Python 3.10 don't have strtobool anymore. So we move it here.
TRUE_VALUES = {"y", "yes", "t", "true", "on", "1"}
FALSE_VALUES = {"n", "no", "f", "false", "off", "0"}

def strtobool(value):
_value = value.lower()
if _value in {"y", "yes", "t", "true", "on", "1"}:
result = True
elif _value in {"n", "no", "f", "false", "off", "0"}:
result = False
else:
raise ValueError(" ".join(("invalid truth value", value)))
return result
value = value.lower()

if value in TRUE_VALUES:
return True
elif value in FALSE_VALUES:
return False

raise ValueError("Invalid truth value: " + value)


class UndefinedValueError(Exception):
Expand Down
26 changes: 8 additions & 18 deletions tests/test_strtobool.py
Expand Up @@ -3,26 +3,16 @@


def test_true_values():
true_list = ["y", "yes", "t", "true", "on", "1"]
for item in true_list:
assert strtobool(item) == 1
for item in ("Y", "YES", "T", "TRUE", "ON", "1"):
assert strtobool(item)


def test_false_values():
false_list = ["n", "no", "f", "false", "off", "0"]
for item in false_list:
assert strtobool(item) == 0
for item in ("N", "NO", "F", "FALSE", "OFF", "0"):
assert strtobool(item) is False


@pytest.mark.parametrize(
"test_input,expected",
[
("Invalid_Value_1", "invalid truth value Invalid_Value_1"),
("1nv4l1d_V4lu3_2", "invalid truth value 1nv4l1d_V4lu3_2"),
("invalid_value_3", "invalid truth value invalid_value_3"),
],
)
def test_eval(test_input, expected):
with pytest.raises(ValueError) as execinfo:
strtobool(test_input)
assert str(execinfo.value) == expected
def test_invalid():
with pytest.raises(ValueError, match="Invalid truth value"):
strtobool("MAYBE")

0 comments on commit a4b104d

Please sign in to comment.