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.
%% 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
- 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.
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.
?_assertEqual(____, cleaner:parse_line(<<>>))Expect skip.
Show hint
Use the brief's exact atom.
Show original 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
- Align docs, specs, and tests.
- Test failures as carefully as success.
- Use tiny fixtures that reveal one rule.