Make the promise executable: full examples and further reading
The original lesson material is preserved here for reference. The main notebook focuses on this lesson's core question.
The original English project below is a log cleaner. The current notebooks use the same file-statistics project as the Chinese route. These preserved examples show another application of the concepts; their inputs and expected outputs belong to the log cleaner.
Add docs, specs, examples, and boundary tests to the project API.
Write one spec, one documented example, and tests for both success and failure.
Run this first
Run it once, change one input, and compare the new result.
# test/cleaner_test.exs
defmodule CleanerTest do
use ExUnit.Case, async: true
describe "parse_line/1" do
test "accepts a known level" do
assert Cleaner.parse_line("INFO ready") == {:ok, {:info, "ready"}}
end
test "rejects a malformed line" do
assert Cleaner.parse_line("BROKEN") == {:error, :invalid_line}
end
end
endmix test should report two passing contract tests.
Steps for the full example
- Read
@doc: Explain behavior and include a small example. - Read
@spec: State accepted and returned term shapes. - Read
assert / refute: Check positive and negative behavior.
Original lesson notes
Run the example first. Predict one result, then change one input and run it again.
Docs, specs, and tests describe the same externally visible behavior.
A doctest turns a short documented IEx example into a regression check.
Symbols and terms
@doc- Explain behavior and include a small example.
@spec- State accepted and returned term shapes.
assert / refute- Check positive and negative behavior.
- public contract
- Docs, specs, and tests describe the same externally visible behavior.
- doctest
- A doctest turns a short documented IEx example into a regression check.
- fixture
- A small fixed input makes edge cases repeatable.
Original full exercise
Add a test for an empty line.
test "skips empty input" do
assert Cleaner.parse_line("") == ____
endExpect :skip.
Show hint
Use the brief's exact result.
Show original answer
test "skips empty input" do
assert Cleaner.parse_line("") == :skip
endWhich artifact wins when docs and tests disagree?
Fix the disagreement against the agreed acceptance examples; all contract artifacts should say the same thing.
Original lesson takeaways
- Keep docs, specs, and tests aligned.
- Test failures as carefully as success.
- Use tiny fixtures that show one rule.