Erlang basics · Lesson 29 (open contents)
Make the promise executable
Align specs, EDoc comments, and tests with the acceptance examples.
Run this first
Run it once, change one input, and compare the new result.
%% 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 eunitreports two passing contract checks.
Read from the first line down
- Read
%% @doc: Describe one public function for EDoc readers. - Read
-spec: State the accepted and returned term shapes. - Read
?assertEqual: Check exact public behavior in EUnit.
Symbols are not secret signs
%% @docDescribe one public function for EDoc readers.
-specState the accepted and returned term shapes.
?assertEqualCheck exact public behavior in EUnit.
Match each name to its meaning
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.
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.
Close the answer and try
Add an empty-line check.
?_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
?_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.
Remember these three lines
- 1
Align docs, specs, and tests.
- 2
Test failures as carefully as success.
- 3
Use tiny fixtures that reveal one rule.