diff --git a/checks/functional/functional7.py b/checks/functional/functional7.py index 1d88fa5..35a1794 100644 --- a/checks/functional/functional7.py +++ b/checks/functional/functional7.py @@ -1,8 +1,9 @@ +# checks/functional/functional7.py triple = make_multiplier(3) double = make_multiplier(2) assert triple(4) == 12, f"triple(4) should be 12, got {triple(4)}" assert double(7) == 14, f"double(7) should be 14, got {double(7)}" -assert make_multiplier(10)(5) == 50 -assert callable(make_multiplier(1)) +assert make_multiplier(10)(5) == 50, "make_multiplier(10)(5) should be 50" +assert callable(make_multiplier(1)), "make_multiplier(1) should be a function" print("functional7 ✓") diff --git a/checks/functional/functional8.py b/checks/functional/functional8.py index 110f1be..5dc3fb7 100644 --- a/checks/functional/functional8.py +++ b/checks/functional/functional8.py @@ -1,7 +1,12 @@ +# checks/functional/functional8.py assert product == 120, f"product should be 120, got {product}" from functools import reduce -assert reduce(lambda a, b: a * b, [2, 3, 4]) == 24 -assert reduce(lambda a, b: a + b, [1, 2, 3, 4]) == 10 +assert reduce(lambda a, b: a * b, [2, 3, 4]) == 24, ( + "product of [2, 3, 4] should be 24" +) +assert reduce(lambda a, b: a + b, [1, 2, 3, 4]) == 10, ( + "sum of [1, 2, 3, 4] should be 10" +) print("functional8 ✓") diff --git a/checks/functional/functional9.py b/checks/functional/functional9.py index 1f7b3cd..917060b 100644 --- a/checks/functional/functional9.py +++ b/checks/functional/functional9.py @@ -1,8 +1,17 @@ +# checks/functional/functional9.py assert has_negative is True, f"has_negative should be True, got {has_negative}" assert all_positive is True, f"all_positive should be True, got {all_positive}" -assert any(x < 0 for x in [1, 2, -3]) is True -assert any(x < 0 for x in [1, 2, 3]) is False -assert all(x > 0 for x in [1, 2, 3]) is True -assert all(x > 0 for x in [1, -2, 3]) is False +assert any(x < 0 for x in [1, 2, -3]) is True, ( + "any(x < 0 for x in [1, 2, -3]) should be True: -3 is negative" +) +assert any(x < 0 for x in [1, 2, 3]) is False, ( + "any(x < 0 for x in [1, 2, 3]) should be False: all are positive" +) +assert all(x > 0 for x in [1, 2, 3]) is True, ( + "all(x > 0 for x in [1, 2, 3]) should be True: all are positive" +) +assert all(x > 0 for x in [1, -2, 3]) is False, ( + "all(x > 0 for x in [1, -2, 3]) should be False: -2 is not > 0" +) print("functional9 ✓") diff --git a/tests/test_functional_assertion_messages.py b/tests/test_functional_assertion_messages.py new file mode 100644 index 0000000..70ed7d1 --- /dev/null +++ b/tests/test_functional_assertion_messages.py @@ -0,0 +1,44 @@ +# tests/test_functional_assertion_messages.py +import ast +from pathlib import Path + + +CHECKS_DIR = Path(__file__).resolve().parent.parent / "checks" / "functional" + +BATCH = ("functional7.py", "functional8.py", "functional9.py") + + +def _asserts_without_message(path): + tree = ast.parse(path.read_text(encoding="utf-8")) + missing = [] + for node in ast.walk(tree): + if isinstance(node, ast.Assert) and node.msg is None: + missing.append(node.lineno) + return missing + + +def test_functional7_asserts_have_messages(): + path = CHECKS_DIR / "functional7.py" + assert _asserts_without_message(path) == [] + + +def test_functional8_asserts_have_messages(): + path = CHECKS_DIR / "functional8.py" + assert _asserts_without_message(path) == [] + + +def test_functional9_asserts_have_messages(): + path = CHECKS_DIR / "functional9.py" + assert _asserts_without_message(path) == [] + + +def test_batch_lines_fit_eighty_columns(): + too_long = [ + f"{name}:{number} ({len(line)} columns)" + for name in BATCH + for number, line in enumerate( + (CHECKS_DIR / name).read_text(encoding="utf-8").splitlines(), 1 + ) + if len(line) > 80 + ] + assert too_long == []