Erlang basics · Lesson 17 (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 · FOUNDATION · LESSON 1725 minutes

Put one tested module in Rebar3

Create an Erlang application and run a focused EUnit test.

01 · Start with the whole example

Run this first

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

Erlang
%% src/score_card.erl
-module(score_card).
-export([pass/1]).

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

pass(Score) when is_integer(Score), Score >= 60, Score =< 100 -> true;
pass(_Score) -> false.

%% EUnit discovers functions whose names end in _test
pass_test() ->
  ?assertEqual(true, pass(70)).
Check the result first
  • After rebar3 new app score_card, save the module and run rebar3 eunit; the focused test passes.
02 · Take the code apart

Read from the first line down

  1. Read rebar3 new app: Create an OTP application project.
  2. Read rebar3 compile: Compile project modules.
  3. Read rebar3 eunit: Run EUnit tests.
03 · Meet the new symbols

Symbols are not secret signs

rebar3 new app

Create an OTP application project.

rebar3 compile

Compile project modules.

rebar3 eunit

Run EUnit tests.

04 · Ideas inside the code

Match each name to its meaning

01

Rebar3

Rebar3 creates Erlang projects, resolves dependencies, compiles code, and runs tests.

02

EUnit

EUnit discovers small test functions and assertion macros.

03

profile

Rebar3 profiles keep test or development dependencies out of production builds.

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.

Rebar3 creates Erlang projects, resolves dependencies, compiles code, and runs tests.

EUnit discovers small test functions and assertion macros.

06 · Change it yourself

Close the answer and try

Write an EUnit assertion that 70 passes.

Practice starting point
pass_test() ->
  ?assertEqual(____, score_card:pass(70)).

Target result: Expect true.

Stuck? Read one hint

EUnit macros come from eunit.hrl.

After you run it, see one answer
One answer
pass_test() ->
  ?assertEqual(true, score_card:pass(70)).
Think about it: Should generated `_build/` files be committed?

No. Rebar3 regenerates build output from source and configuration.

Take with you

Remember these three lines

  1. 1

    Use Rebar3 for repeatable projects.

  2. 2

    Keep source under src/.

  3. 3

    Run EUnit from the project root.

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