Skip to content

Commit

Permalink
Provide test/4 with teardown clause
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Zelazny committed Jan 10, 2015
1 parent 59167b9 commit ca27da3
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 10 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ bddr:test([given_app_started(),

## ...it doesn't do XYZ?

No, the implementation is four lines of code. No parse transforms, no
macros. It's the developer's responsibility to provide his/her abstractions for
the tested components. Remember, your tests are just Erlang code. Make it
readable and extensible for others. Be humane.
No, the implementation is minimal. No parse transforms, no macros. It's the
developer's responsibility to provide his/her abstractions for the tested
components. Remember, your tests are just Erlang code. Make it readable and
extensible for others. Be humane.

----------

Expand Down
29 changes: 25 additions & 4 deletions src/bddr.erl
Original file line number Diff line number Diff line change
@@ -1,7 +1,28 @@
-module(bddr).
-export([test/3]).
-export([test/3, test/4]).

test(Given, When, Then) when is_function(Given, 0) ->
Then(When(Given()));
test(Given, When, Then) ->
Then(When(Given)).
test(Given, When, Then, fun null_teardown/1).

test(Given, When, Then, Teardown) ->
Environment = run_given(Given),
Result = run_test(Environment, When, Then),
_ = (catch Teardown(Environment)),
return(Result).

%% Internal

null_teardown(_) -> ok.

run_given(G) when is_function(G, 0) -> G();
run_given(G) -> G.

run_test(E, W, T) ->
try T(W(E))
catch E:R -> {bddr_failure, {E, R}}
end.

return({bddr_failure, {exit, E}}) -> exit(E);
return({bddr_failure, {throw, T}}) -> throw(T);
return({bddr_failure, {error, E}}) -> error(E);
return(Result) -> Result.
16 changes: 14 additions & 2 deletions test/bddr_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ all() -> [givens_can_be_a_lambda,

then_clause_can_access_action_result,
then_clause_matches_on_action_result,
then_with_unexpected_result_raises_function_clause
then_with_unexpected_result_raises_function_clause,

test_provides_teardown_option
].

givens_can_be_a_lambda(_) ->
Expand Down Expand Up @@ -80,10 +82,20 @@ then_with_unexpected_result_raises_function_clause(_) ->
fun(_) -> nay end,
fun(yay) -> this_will_fail end)).

test_provides_teardown_option(_) ->
bddr:test(spawn(fun echo/0),
fun(_) -> some_user_action end,
fun(_) -> ok end,

%% Non-test functions
fun(Pid) -> Pid ! self(),
receive ok -> ok end,
false = is_process_alive(Pid) end).


%% Non-test functions
echo() ->
receive From -> From ! ok end.

compiles(Source) ->
try {ModName, Bin} = dynamic_compile:from_string(Source),
is_atom(ModName) andalso is_binary(Bin)
Expand Down

0 comments on commit ca27da3

Please sign in to comment.