diff --git a/README.md b/README.md index de31d76..84e6889 100644 --- a/README.md +++ b/README.md @@ -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. ---------- diff --git a/src/bddr.erl b/src/bddr.erl index 915332c..ca50a62 100644 --- a/src/bddr.erl +++ b/src/bddr.erl @@ -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. diff --git a/test/bddr_SUITE.erl b/test/bddr_SUITE.erl index 3db3365..b91126c 100644 --- a/test/bddr_SUITE.erl +++ b/test/bddr_SUITE.erl @@ -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(_) -> @@ -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)