← Back to lesson 29: 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.

Align specs, EDoc comments, and tests with the acceptance examples.

Write one spec and tests for both a valid and malformed line.

Run this first

Run it once, change one input, and compare the new result.

Original full example
%% test/cleaner_tests.erl
-module(cleaner_tests).

-include_lib("eunit/include/eunit.hrl").

%% Test both sides of the parser contract
parse_line_test_() ->
  [?_assertEqual({ok, {info, <<"ready">>}}, cleaner:parse_line(<<"INFO ready">>)),
   ?_assertEqual({error, invalid_line}, cleaner:parse_line(<<"BROKEN">>))].

rebar3 eunit reports two passing contract checks.

Steps for the full example

  1. Read %% @doc: Describe one public function for EDoc readers.
  2. Read -spec: State the accepted and returned term shapes.
  3. Read ?assertEqual: Check exact public behavior in EUnit.

Original lesson notes

Run the example first. Predict one result, then change one input and run it again.

Documentation, specs, and tests should describe the same visible behavior.

EDoc turns structured module and function comments into browsable documentation.

Symbols and terms
%% @doc
Describe one public function for EDoc readers.
-spec
State the accepted and returned term shapes.
?assertEqual
Check exact public behavior in EUnit.
public contract
Documentation, specs, and tests should describe the same visible behavior.
EDoc
EDoc turns structured module and function comments into browsable documentation.
fixture
A small fixed input makes an edge case repeatable.

Original full exercise

Add an empty-line check.

Exercise starter
?_assertEqual(____, cleaner:parse_line(<<>>))

Expect skip.

Show hint

Use the brief's exact atom.

Show original answer
Reference answer
?_assertEqual(skip, cleaner:parse_line(<<>>))
What should happen when docs and tests disagree?

Resolve the disagreement against the accepted examples, then make every contract artifact agree.

Original lesson takeaways
Design trade-offs and language comparisons
Original course references