diff --git a/checks/decorators/decorators2.py b/checks/decorators/decorators2.py index 3792dc6..1338291 100644 --- a/checks/decorators/decorators2.py +++ b/checks/decorators/decorators2.py @@ -1,5 +1,7 @@ assert loud_greet() == "HELLO", ( f"loud_greet() should return 'HELLO', got {loud_greet()!r}" ) -assert shout(lambda: "world")() == "WORLD" +assert shout(lambda: "world")() == "WORLD", ( + f"shout(lambda: 'world')() should return 'WORLD', got {shout(lambda: 'world')()!r}" +) print("decorators2 ✓") diff --git a/checks/decorators/decorators5.py b/checks/decorators/decorators5.py index facfe56..997a53d 100644 --- a/checks/decorators/decorators5.py +++ b/checks/decorators/decorators5.py @@ -1,5 +1,9 @@ assert add(3, 4) == 7, f"add(3, 4) should return 7, got {add(3, 4)!r}" -assert add(10, -2) == 8 -assert greet("Alice") == "Hello, Alice!" -assert greet("Bob", greeting="Hi") == "Hi, Bob!" +assert add(10, -2) == 8, f"add(10, -2) should return 8, got {add(10, -2)!r}" +assert greet("Alice") == "Hello, Alice!", ( + f"greet('Alice') should return 'Hello, Alice!', got {greet('Alice')!r}" +) +assert greet("Bob", greeting="Hi") == "Hi, Bob!", ( + f"greet('Bob', greeting='Hi') should return 'Hi, Bob!', got {greet('Bob', greeting='Hi')!r}" +) print("decorators5 ✓") diff --git a/tests/test_decorators_assertion_messages.py b/tests/test_decorators_assertion_messages.py new file mode 100644 index 0000000..c68f242 --- /dev/null +++ b/tests/test_decorators_assertion_messages.py @@ -0,0 +1,22 @@ +import ast +from pathlib import Path + + +CHECKS = Path(__file__).resolve().parent.parent / "checks" / "decorators" + + +def _asserts_without_msg(path): + tree = ast.parse(path.read_text()) + 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_decorators2_asserts_have_messages(): + assert _asserts_without_msg(CHECKS / "decorators2.py") == [] + + +def test_decorators5_asserts_have_messages(): + assert _asserts_without_msg(CHECKS / "decorators5.py") == []