1: -module(safely_SUITE).
    2: -compile([export_all, nowarn_export_all]).
    3: -include_lib("eunit/include/eunit.hrl").
    4: 
    5: all() ->
    6:     [ handles_errors_similar_to_catch,
    7:       handles_exits_similar_to_errors,
    8:       handles_throws_unlike_catch,
    9:       handles_success_like_catch
   10:     ].
   11: 
   12: handles_errors_similar_to_catch(_) ->
   13:     {SafeRes, CatchRes} =
   14:         %% These two must be on the same line for the stacktraces to be equal.
   15:         {safely:apply(lists, min, [[]]),(catch apply(lists, min, [[]]))},
   16: 
   17:     {exception, #{class := error, reason := function_clause, stacktrace := SafeST}} = SafeRes,
   18:     {'EXIT', {function_clause, CatchST}} = CatchRes,
   19: 
   20:     true = (hd(CatchST) == hd(SafeST)),
   21: 
   22:     {safely,apply,3,[{file,_},{line,_}]} = hd(tl(SafeST)), % this is extra in SafeStackTrace
   23: 
   24:     true = (tl(CatchST) == tl(tl(SafeST))).
   25: 
   26: handles_exits_similar_to_errors(_) ->
   27:     ExitF = fun() -> exit(i_quit) end,
   28:     {exception, #{class := exit, reason := i_quit, stacktrace := _S}} = safely:apply(ExitF,[]),
   29:     {'EXIT', i_quit} = (catch apply(ExitF,[])),
   30:     ok.
   31: 
   32: handles_throws_unlike_catch(_) ->
   33:     ThrowF = fun() -> throw(up) end,
   34:     {exception, #{class := throw, reason := up}} = safely:apply(ThrowF,[]),
   35:     up = (catch apply(ThrowF,[])),
   36:     ok.
   37: 
   38: handles_success_like_catch(_) ->
   39:     1 = safely:apply(lists, min, [[1,2,3]]),
   40:     1 = (catch apply(lists, min, [[1,2,3]])).