From 4a52670951b6922d83778e71df639a96b49a7574 Mon Sep 17 00:00:00 2001 From: Abhik Sarkar Date: Thu, 5 Nov 2026 13:15:22 +0530 Subject: [PATCH] fix(curriculum): complete functional assertion messages (batch 1) Add expected-value messages to nine assertions in functional1 through functional6 without changing their predicates or execution order. Avoid re-running learner functions in new messages, with four regression cases. Complete the contribution from #151 on a branch from current dev. Co-authored-by: Sweta Kumari --- checks/functional/functional1.py | 3 ++- checks/functional/functional2.py | 5 ++-- checks/functional/functional3.py | 9 +++++-- checks/functional/functional4.py | 13 +++++++--- checks/functional/functional5.py | 12 ++++++--- checks/functional/functional6.py | 5 +++- tests/unit/test_check_namespaces.py | 40 +++++++++++++++++++++++++++++ 7 files changed, 75 insertions(+), 12 deletions(-) diff --git a/checks/functional/functional1.py b/checks/functional/functional1.py index 09cd559..a9690ca 100644 --- a/checks/functional/functional1.py +++ b/checks/functional/functional1.py @@ -1,4 +1,5 @@ +# checks/functional/functional1.py assert square(3) == 9, f"square(3) should be 9, got {square(3)}" assert square(5) == 25, f"square(5) should be 25, got {square(5)}" -assert square(0) == 0 +assert square(0) == 0, "square(0) should be 0" print("functional1 ✓") diff --git a/checks/functional/functional2.py b/checks/functional/functional2.py index 2dbece0..ddb88df 100644 --- a/checks/functional/functional2.py +++ b/checks/functional/functional2.py @@ -1,5 +1,6 @@ +# checks/functional/functional2.py assert absolute(5) == 5, f"absolute(5) should be 5, got {absolute(5)}" assert absolute(-3) == 3, f"absolute(-3) should be 3, got {absolute(-3)}" -assert absolute(0) == 0 -assert absolute(-100) == 100 +assert absolute(0) == 0, "absolute(0) should be 0" +assert absolute(-100) == 100, "absolute(-100) should be 100" print("functional2 ✓") diff --git a/checks/functional/functional3.py b/checks/functional/functional3.py index 2fc411d..f65f86d 100644 --- a/checks/functional/functional3.py +++ b/checks/functional/functional3.py @@ -1,3 +1,8 @@ -assert doubled == [2, 4, 6, 8, 10], f"doubled should be [2, 4, 6, 8, 10], got {doubled}" -assert isinstance(doubled, list) +# checks/functional/functional3.py +assert doubled == [2, 4, 6, 8, 10], ( + f"doubled should be [2, 4, 6, 8, 10], got {doubled}" +) +assert isinstance(doubled, list), ( + f"doubled should be a list, got {type(doubled).__name__}" +) print("functional3 ✓") diff --git a/checks/functional/functional4.py b/checks/functional/functional4.py index a905400..9f15519 100644 --- a/checks/functional/functional4.py +++ b/checks/functional/functional4.py @@ -1,4 +1,11 @@ -assert evens == [2, 4, 6, 8, 10], f"evens should be [2, 4, 6, 8, 10], got {evens}" -assert isinstance(evens, list) -assert all(x % 2 == 0 for x in evens) +# checks/functional/functional4.py +assert evens == [2, 4, 6, 8, 10], ( + f"evens should be [2, 4, 6, 8, 10], got {evens}" +) +assert isinstance(evens, list), ( + f"evens should be a list, got {type(evens).__name__}" +) +assert all(x % 2 == 0 for x in evens), ( + f"evens should contain only even numbers, got {evens}" +) print("functional4 ✓") diff --git a/checks/functional/functional5.py b/checks/functional/functional5.py index 6a07007..0c30dc8 100644 --- a/checks/functional/functional5.py +++ b/checks/functional/functional5.py @@ -1,6 +1,12 @@ +# checks/functional/functional5.py assert by_length == ["fig", "kiwi", "date", "apple", "banana"], ( - f"by_length should be ['fig', 'kiwi', 'date', 'apple', 'banana'], got {by_length}" + "by_length should be ['fig', 'kiwi', 'date', 'apple', 'banana'], " + f"got {by_length}" +) +assert by_length[0] == "fig", ( + f"by_length[0] should be 'fig', got {by_length[0]}" +) +assert by_length[-1] == "banana", ( + f"by_length[-1] should be 'banana', got {by_length[-1]}" ) -assert by_length[0] == "fig" -assert by_length[-1] == "banana" print("functional5 ✓") diff --git a/checks/functional/functional6.py b/checks/functional/functional6.py index 5207c6c..179fc00 100644 --- a/checks/functional/functional6.py +++ b/checks/functional/functional6.py @@ -1,8 +1,11 @@ +# checks/functional/functional6.py assert apply_twice(triple, 2) == 18, ( f"apply_twice(triple, 2) should be 18, got {apply_twice(triple, 2)}" ) assert apply_twice(add_ten, 5) == 25, ( f"apply_twice(add_ten, 5) should be 25, got {apply_twice(add_ten, 5)}" ) -assert apply_twice(lambda x: x + 1, 0) == 2 +assert apply_twice(lambda x: x + 1, 0) == 2, ( + "apply_twice(lambda x: x + 1, 0) should be 2" +) print("functional6 ✓") diff --git a/tests/unit/test_check_namespaces.py b/tests/unit/test_check_namespaces.py index bf09e6d..47222a7 100644 --- a/tests/unit/test_check_namespaces.py +++ b/tests/unit/test_check_namespaces.py @@ -1,6 +1,8 @@ # tests/unit/test_check_namespaces.py from pathlib import Path +import pytest + from solutions._answers import ANSWERS @@ -19,3 +21,41 @@ def test_area_check_preserves_learner_globals() -> None: ) assert namespace["area"] == 0 + + +@pytest.mark.parametrize( + ("name", "function", "argument"), + [ + ("functional1", "square", 0), + ("functional2", "absolute", 0), + ("functional2", "absolute", -100), + ("functional6", "apply_twice", 0), + ], +) +def test_functional_messages_do_not_repeat_failed_calls( + name: str, function: str, argument: int +) -> None: + namespace = {"__name__": "__main__"} + exec(ANSWERS[name], namespace) + original = namespace[function] + calls = [] + + def wrong_once(*args): + if args[-1] != argument: + return original(*args) + calls.append(args) + if len(calls) > 1: + raise RuntimeError("Failure message called learner code again") + return -1 + + namespace[function] = wrong_once + check = ( + Path(__file__).resolve().parents[2] + / "checks" / "functional" / f"{name}.py" + ) + with pytest.raises(AssertionError, match="should be"): + exec( + compile(check.read_text(encoding="utf-8"), str(check), "exec"), + namespace, + ) + assert len(calls) == 1