← Back to lesson 30: Make the promise executable

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.

Original full example
# 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
end

mix test should report two passing contract tests.

Steps for the full example

  1. Read @doc: Explain behavior and include a small example.
  2. Read @spec: State accepted and returned term shapes.
  3. 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.

Exercise starter
test "skips empty input" do
  assert Cleaner.parse_line("") == ____
end

Expect :skip.

Show hint

Use the brief's exact result.

Show original answer
Reference answer
test "skips empty input" do
  assert Cleaner.parse_line("") == :skip
end
Which 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
Design trade-offs and language comparisons
Original course references