Skip to content

Latest commit

 

History

History
124 lines (92 loc) · 3.3 KB

eunit.adoc

File metadata and controls

124 lines (92 loc) · 3.3 KB

eunit.hrl header file

Useful macros for use in EUnit tests.

Usage

Pair this with the standard EUnit header file:

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

?output(Expression[, Columns, Rows]) macro

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 begin …​ end to group multiple expressions. For example:

?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)).

?outputEqual(Expect, Expression[, 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))).

?_outputEqual(Expect, 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))).

?outputEqualToFile(App, File, Test[, Columns, Rows], [Comment])

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 over File.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.

Licence

See LICENSE for licensing information.