Elixir basics · Lesson 29 (open contents)
Make the promise executable
Add docs, specs, examples, and boundary tests to the project API.
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 testshould report two passing contract tests.
Read from the first line down
- 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.
Symbols are not secret signs
@docExplain behavior and include a small example.
@specState accepted and returned term shapes.
assert / refuteCheck positive and negative behavior.
Match each name to its meaning
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.
Why these forms are useful
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.
Close the answer and try
Add a test for an empty line.
test "skips empty input" do
assert Cleaner.parse_line("") == ____
endTarget result: Expect :skip.
Stuck? Read one hint
Use the brief's exact result.
After you run it, see one answer
test "skips empty input" do
assert Cleaner.parse_line("") == :skip
endThink about it: Which artifact wins when docs and tests disagree?
Fix the disagreement against the agreed acceptance examples; all contract artifacts should say the same thing.
Remember these three lines
- 1
Keep docs, specs, and tests aligned.
- 2
Test failures as carefully as success.
- 3
Use tiny fixtures that show one rule.