Skip to content
Closed
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
6 changes: 3 additions & 3 deletions checks/generators/generators1.py
Original file line number Diff line number Diff line change
@@ -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 ✓")
4 changes: 2 additions & 2 deletions checks/generators/generators3.py
Original file line number Diff line number Diff line change
@@ -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 ✓")
4 changes: 2 additions & 2 deletions checks/generators/generators6.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 ✓")
6 changes: 3 additions & 3 deletions checks/generators/generators7.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
12 changes: 6 additions & 6 deletions checks/generators/generators8.py
Original file line number Diff line number Diff line change
@@ -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")
26 changes: 26 additions & 0 deletions tests/test_generators_assertion_messages.py
Original file line number Diff line number Diff line change
@@ -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}"
Loading