Skip to content
Merged
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
3 changes: 2 additions & 1 deletion checks/functional/functional1.py
Original file line number Diff line number Diff line change
@@ -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 ✓")
5 changes: 3 additions & 2 deletions checks/functional/functional2.py
Original file line number Diff line number Diff line change
@@ -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 ✓")
9 changes: 7 additions & 2 deletions checks/functional/functional3.py
Original file line number Diff line number Diff line change
@@ -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 ✓")
13 changes: 10 additions & 3 deletions checks/functional/functional4.py
Original file line number Diff line number Diff line change
@@ -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 ✓")
12 changes: 9 additions & 3 deletions checks/functional/functional5.py
Original file line number Diff line number Diff line change
@@ -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 ✓")
5 changes: 4 additions & 1 deletion checks/functional/functional6.py
Original file line number Diff line number Diff line change
@@ -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 ✓")
40 changes: 40 additions & 0 deletions tests/unit/test_check_namespaces.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# tests/unit/test_check_namespaces.py
from pathlib import Path

import pytest

from solutions._answers import ANSWERS


Expand All @@ -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
Loading