diff --git a/checks/generators/generators1.py b/checks/generators/generators1.py index 4f3822e..b2153c0 100644 --- a/checks/generators/generators1.py +++ b/checks/generators/generators1.py @@ -1,4 +1,4 @@ -assert list(count_up_to(4)) == [1, 2, 3, 4] -assert list(count_up_to(1)) == [1] -assert list(count_up_to(0)) == [] +assert list(count_up_to(4)) == [1, 2, 3, 4], f"expected [1, 2, 3, 4], got {list(count_up_to(4))}" +assert list(count_up_to(1)) == [1], f"expected [1], got {list(count_up_to(1))}" +assert list(count_up_to(0)) == [], f"expected [], got {list(count_up_to(0))}" print("generators1 ✓") diff --git a/checks/generators/generators3.py b/checks/generators/generators3.py index 8dc4615..1f8477c 100644 --- a/checks/generators/generators3.py +++ b/checks/generators/generators3.py @@ -1,4 +1,4 @@ assert collected == [2, 4, 6, 8], f"expected [2, 4, 6, 8], got {collected}" -assert list(evens_up_to(5)) == [2, 4] -assert list(evens_up_to(1)) == [] +assert list(evens_up_to(5)) == [2, 4], f"expected [2, 4], got {list(evens_up_to(5))}" +assert list(evens_up_to(1)) == [], f"expected [], got {list(evens_up_to(1))}" print("generators3 ✓") diff --git a/checks/generators/generators6.py b/checks/generators/generators6.py index 8aabc65..58965f5 100644 --- a/checks/generators/generators6.py +++ b/checks/generators/generators6.py @@ -3,6 +3,6 @@ ) # Confirm the generator really is infinite: pull 1000 values. big = list(itertools.islice(natural_numbers(), 1000)) -assert len(big) == 1000 -assert big[-1] == 1000 +assert len(big) == 1000, f"expected 1000 values, got {len(big)}" +assert big[-1] == 1000, f"expected last value 1000, got {big[-1]}" print("generators6 ✓") diff --git a/checks/generators/generators7.py b/checks/generators/generators7.py index a043998..88341e0 100644 --- a/checks/generators/generators7.py +++ b/checks/generators/generators7.py @@ -6,6 +6,6 @@ ) # Confirm more terms keep coming. first15 = list(itertools.islice(fibonacci(), 15)) -assert len(first15) == 15 -assert first15[-1] == 377 -print("generators7 ✓") +assert len(first15) == 15, f"expected 15 terms in the sequence, got {len(first15)}" +assert first15[-1] == 377, f"expected the 15th term to be 377, got {first15[-1]}" +print("generators7 \u2713") diff --git a/checks/generators/generators8.py b/checks/generators/generators8.py index d1e027d..854780f 100644 --- a/checks/generators/generators8.py +++ b/checks/generators/generators8.py @@ -1,8 +1,8 @@ -assert list(Countdown(3)) == [3, 2, 1] -assert list(Countdown(1)) == [1] -assert list(Countdown(5)) == [5, 4, 3, 2, 1] +assert list(Countdown(3)) == [3, 2, 1], f"expected [3, 2, 1], got {list(Countdown(3))}" +assert list(Countdown(1)) == [1], f"expected [1], got {list(Countdown(1))}" +assert list(Countdown(5)) == [5, 4, 3, 2, 1], f"expected [5, 4, 3, 2, 1], got {list(Countdown(5))}" # Should be re-iterable: a fresh iteration each time. cd = Countdown(3) -assert list(cd) == [3, 2, 1] -assert list(cd) == [3, 2, 1] -print("generators8 ✓") +assert list(cd) == [3, 2, 1], f"expected a fresh iteration [3, 2, 1], got {list(cd)}" +assert list(cd) == [3, 2, 1], f"expected the second iteration to replay [3, 2, 1], got {list(cd)}" +print("generators8 \u2713") diff --git a/tests/test_generators_assertion_messages.py b/tests/test_generators_assertion_messages.py new file mode 100644 index 0000000..ad3d590 --- /dev/null +++ b/tests/test_generators_assertion_messages.py @@ -0,0 +1,26 @@ +import ast +import pathlib + + +CHECKS = [ + "checks/generators/generators1.py", + "checks/generators/generators3.py", + "checks/generators/generators6.py", + "checks/generators/generators7.py", + "checks/generators/generators8.py", +] + + +def _asserts_without_message(path): + tree = ast.parse(pathlib.Path(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_generators_checks_have_actionable_assertion_messages(): + for path in CHECKS: + missing = _asserts_without_message(path) + assert missing == [], f"{path} has asserts without messages at lines {missing}"