summaryrefslogtreecommitdiffhomepage
path: root/erlang.html.markdown
diff options
context:
space:
mode:
authorwillianjusten <willianjustenqui@gmail.com>2015-10-03 21:00:47 -0300
committerwillianjusten <willianjustenqui@gmail.com>2015-10-03 21:00:47 -0300
commit5578db0705e0fe05c40e5cad404fae47767eafa9 (patch)
treeb2588868f4ae66387dd78572a451f0230b03c561 /erlang.html.markdown
parent46d509077f10fc9b04aaf69829526d4b8297798d (diff)
parent9b46fe7e57bcf63785b67cdaffc49ff0d47d7476 (diff)
Merge remote-tracking branch 'upstream/master'
* upstream/master: Add section on eunit in erlang doc Add clarification on bind / match with = op in erlang. Fixes #1139 Removed random "r" Add to contributors Misc. typos and formatting More explanation on virtual destructors Clarify character literals Fixing typo in julia-es.html.markdown [xml/id] Translated JSON to Indonesian (json-id) [xml/id] Translated XML to Indonesian (xml-id) Aligned the comment block [javascript] Fix for issue 1248 Fixed some grammatical issues/typos Typos fixed in julia-es.html.markdown [c++/pt-br] Fix translate on editing object as parameters Update c++.html.markdown Add lang: to header of Python3 cs-cz file. All class constants can be accessed statically Add `kbd` tag Fixed the description of attributes and elements.
Diffstat (limited to 'erlang.html.markdown')
-rw-r--r--erlang.html.markdown42
1 files changed, 39 insertions, 3 deletions
diff --git a/erlang.html.markdown b/erlang.html.markdown
index 8b67a76a..64330867 100644
--- a/erlang.html.markdown
+++ b/erlang.html.markdown
@@ -25,6 +25,7 @@ filename: learnerlang.erl
%% 1. Variables and pattern matching.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+% In Erlang new variables are bound with an `=` statement.
Num = 42. % All variable names must start with an uppercase letter.
% Erlang has single-assignment variables; if you try to assign a different
@@ -32,9 +33,11 @@ Num = 42. % All variable names must start with an uppercase letter.
Num = 43. % ** exception error: no match of right hand side value 43
% In most languages, `=` denotes an assignment statement. In Erlang, however,
-% `=` denotes a pattern-matching operation. `Lhs = Rhs` really means this:
-% evaluate the right side (`Rhs`), and then match the result against the
-% pattern on the left side (`Lhs`).
+% `=` denotes a pattern-matching operation. When an empty variable is used on the
+% left hand side of the `=` operator to is bound (assigned), but when a bound
+% varaible is used on the left hand side the following behaviour is observed.
+% `Lhs = Rhs` really means this: evaluate the right side (`Rhs`), and then
+% match the result against the pattern on the left side (`Lhs`).
Num = 7 * 6.
% Floating-point number.
@@ -299,6 +302,39 @@ CalculateArea ! {circle, 2}. % 12.56000000000000049738
% The shell is also a process; you can use `self` to get the current pid.
self(). % <0.41.0>
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%% 5. Testing with EUnit
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+% Unit tests can be written using EUnits's test generators and assert macros
+-module(fib).
+ -export([fib/1]).
+ -include_lib("eunit/include/eunit.hrl").
+
+ fib(0) -> 1;
+ fib(1) -> 1;
+ fib(N) when N > 1 -> fib(N-1) + fib(N-2).
+
+ fib_test_() ->
+ [?_assert(fib(0) =:= 1),
+ ?_assert(fib(1) =:= 1),
+ ?_assert(fib(2) =:= 2),
+ ?_assert(fib(3) =:= 3),
+ ?_assert(fib(4) =:= 5),
+ ?_assert(fib(5) =:= 8),
+ ?_assertException(error, function_clause, fib(-1)),
+ ?_assert(fib(31) =:= 2178309)
+ ].
+
+% EUnit will automatically export to a test() fucntion to allo running the tests
+% in the erlang shell
+fib:test()
+
+% The popular erlang build tool Rebar is also compatible with EUnit
+% ```
+% rebar eunit
+% ```
+
```
## References