Erlang basics · Lesson 29 (open contents)
01 · Open erl02 · Meet common terms03 · Tell two kinds of text apart04 · Bind a variable once05 · Split and filter a list06 · Parse a number safely07 · Choose one path with case08 · Let a function keep going09 · Put code in a module10 · Compare terms and Boolean results11 · Read and update nested maps12 · Build and inspect UTF-8 binaries13 · Let clauses choose the function path14 · Solve a list twice15 · Choose with patterns, guards, and errors16 · Describe a module and one record17 · Put one tested module in Rebar318 · Return success or failure as data19 · Read and write one file safely20 · Build a data pipeline with functions21 · Keep records behind a module boundary22 · Define a module contract23 · Describe the data contract24 · Test small rules and whole flows25 · Shape builds and make a command26 · Write the project promise27 · Parse one real line28 · Keep the public API small29 · Make the promise executable30 · Package the edge and leave clues31 · Prove the project is done
ERLANG · PROJECT · LESSON 2935 minutes

Make the promise executable

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

01 · Start with the whole example

Run this first

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

Erlang
%% 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">>))].
Check the result first
  • rebar3 eunit reports two passing contract checks.
02 · Take the code apart

Read from the first line down

  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.
03 · Meet the new symbols

Symbols are not secret signs

%% @doc

Describe one public function for EDoc readers.

-spec

State the accepted and returned term shapes.

?assertEqual

Check exact public behavior in EUnit.

04 · Ideas inside the code

Match each name to its meaning

01

public contract

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

02

EDoc

EDoc turns structured module and function comments into browsable documentation.

03

fixture

A small fixed input makes an edge case repeatable.

05 · Make the idea clear

Why these forms are useful

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.

06 · Change it yourself

Close the answer and try

Add an empty-line check.

Practice starting point
?_assertEqual(____, cleaner:parse_line(<<>>))

Target result: Expect skip.

Stuck? Read one hint

Use the brief's exact atom.

After you run it, see one answer
One answer
?_assertEqual(skip, cleaner:parse_line(<<>>))
Think about it: What should happen when docs and tests disagree?

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

Take with you

Remember these three lines

  1. 1

    Align docs, specs, and tests.

  2. 2

    Test failures as carefully as success.

  3. 3

    Use tiny fixtures that reveal one rule.

Lesson completeRun the code once, then mark this lesson as done.