Skip to content

Commit

Permalink
Adds tests, linting.
Browse files Browse the repository at this point in the history
  • Loading branch information
jvanderaa committed Nov 4, 2023
1 parent 67f433a commit 429acd9
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
11 changes: 8 additions & 3 deletions tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,17 @@ def strtobool(val: str) -> bool:
bool: True or False
"""
val = val.lower()

# Check for valid truth values
if val in ("y", "yes", "t", "true", "on", "1"):
return True
elif val in ("n", "no", "f", "false", "off", "0"):

# Check for valid false values
if val in ("n", "no", "f", "false", "off", "0"):
return False
else:
raise ValueError("invalid truth value %r" % (val,))

# Raise error if not valid truth value
raise ValueError("invalid truth value %r" % (val,))


def is_truthy(arg):
Expand Down
31 changes: 31 additions & 0 deletions tests/test_tasks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"""Test functions for tasks.py."""
import pytest

from tasks import strtobool

@pytest.mark.parametrize(
"test_case, expected",
[
("y", True),
("yes", True),
("true", True),
("t", True),
("on", True),
("1", True),
("n", False),
("no", False),
("false", False),
("f", False),
("off", False),
("0", False),
],
)
def test_strtobool(test_case, expected):
"""Test strtobool function."""
assert strtobool(test_case) == expected

def test_strtobool_error():
"""Test generation of the error message."""
with pytest.raises(ValueError) as excinfo:
strtobool("invalid")
assert "Invalid truth value invalid" in str(excinfo.value)

0 comments on commit 429acd9

Please sign in to comment.