Useful macros for use in EUnit tests.
Pair this with the standard EUnit header file:
-include_lib("eunit/include/eunit.hrl").
-include_lib("solarized/include/eunit.hrl").
When the expression you are capturing output from is simple,
the solarized_capture:output/1
call can be similified also.
The following two expressions are equivilent:
?assertEqual(<<$\n>>, ?output(solarized:nl()))
?assertEqual(<<$\n>>, solarized_capture:output(fun () -> solarized:nl() end))
Note
|
Use ?assertEqual(<<$\n>>,
?output(begin
solarized:nl(),
solarized:nl()
end)) |
Defined as:
-define(output(Expression),
solarized_capture:output(fun () -> Expression end)).
-define(output(Expression, Columns, Rows),
solarized_capture:output(fun () -> Expression end, Columns, Rows)).
Similar to ?assertEqual(Expect, Expression)
but compares the out put of
Expression
rather than the value.
Defined as:
-define(outputEqual(Expect, Expression),
?assertEqual(Expect, ?output(Expression))).
-define(outputEqual(Expect, Expression, Columns, Rows),
?assertEqual(Expect, ?output(Expression, Columns, Rows))).
Similar to ?_assertEqual(Expect, Expression)
.
Defined as:
-define(_outputEqual(Expect, Expression),
?_test(?outputEqual(Expect, Expression))).
-define(_outputEqual(Expect, Expression, Columns, Rows),
?_test(?outputEqual(Expect, Expression, Columns, Rows))).
When the expected value is large and complex and you are more concerned about how it looks rather than how it is encoded, save it as a file in:
-
AppDir/test/File.expect
You gain the following benifits:
-
You can
cat File.expect
to see it visably rendered in your terminal, -
When your expectations change and you are happy with the new
File.output
, copy it overFile.expect
The ?outputEqualToFile
macro supports such a work flow.
For example:
a_test() ->
Test = fun () ->
...
end,
?outputEqualToFile(App, File, Test).
Where App
is your application atom()
to be used
to lookup AppDir
by code:lib_dir(App, test)
.
Where File
is the base of the following two files:
-
AppDir/test/File.expect
-
AppDir/test/File.output
The test logic is as follows:
-
If no
.expect
file exists then the test fails and the captured output is saved to the.output
file. -
If the captured output does not match the
.expect
file then the test fails and the captured output is saved to the.output
file. -
If the captured output matches the
.expect
file then the test passes and the.output
file is removed.
Note
|
If not specified, Columns and Rows will default to 80 and 25 respectively.
|
See LICENSE for licensing information.