Permalink
Browse files

fix eunit to print stacktrace correctly if test cases throw any excep…

…tion

R15 adds file and lineno in the result of erlang:get_stacktrace/0, so
eunit should be updated accordingly to accomodate the new stacktrace format.
Fix eunit_lib:print_exception when erlang:get_stacktrace returns {M,F,A,[]}
  • Loading branch information...
1 parent 9a6ad15 commit 73b94a990bb91fd263dace4ccbaef6ff727a9637 @weicao weicao committed with proxyles Nov 2, 2011
Showing with 20 additions and 9 deletions.
  1. +18 −7 lib/eunit/src/eunit_lib.erl
  2. +2 −2 lib/eunit/src/eunit_test.erl
@@ -86,20 +86,26 @@ analyze_exit_term(Term) ->
is_stacktrace([]) ->
true;
-is_stacktrace([{M,F,A}|Fs]) when is_atom(M), is_atom(F), is_integer(A) ->
+is_stacktrace([{M,F,A,Fl}|Fs]) when is_atom(M), is_atom(F), is_integer(A), is_list(Fl) ->
is_stacktrace(Fs);
-is_stacktrace([{M,F,As}|Fs]) when is_atom(M), is_atom(F), is_list(As) ->
+is_stacktrace([{M,F,As,Fl}|Fs]) when is_atom(M), is_atom(F), is_list(As), is_list(Fl) ->
is_stacktrace(Fs);
is_stacktrace(_) ->
false.
format_stacktrace(Trace) ->
format_stacktrace(Trace, "in function", "in call from").
-format_stacktrace([{M,F,A}|Fs], Pre, Pre1) when is_integer(A) ->
- [io_lib:fwrite(" ~s ~w:~w/~w\n", [Pre, M, F, A])
+
+format_stacktrace([{M,F,A,Fl}|Fs], Pre, Pre1) when is_integer(A) ->
+ S = case Fl of
+ [{file, File},{line, Line}]->
+ io_lib:fwrite(" [~s:~w]", [File, Line]);
+ _-> ""
+ end,
+ [io_lib:fwrite(" ~s ~w:~w/~w~s\n", [Pre, M, F, A, S])
| format_stacktrace(Fs, Pre1, Pre1)];
-format_stacktrace([{M,F,As}|Fs], Pre, Pre1) when is_list(As) ->
+format_stacktrace([{M,F,As,Fl}|Fs], Pre, Pre1) when is_list(As) ->
A = length(As),
C = case is_op(M,F,A) of
true when A =:= 1 ->
@@ -112,8 +118,13 @@ format_stacktrace([{M,F,As}|Fs], Pre, Pre1) when is_list(As) ->
false ->
io_lib:fwrite("~w(~s)", [F,format_arglist(As)])
end,
- [io_lib:fwrite(" ~s ~w:~w/~w\n called as ~s\n",
- [Pre,M,F,A,C])
+ S = case Fl of
+ [{file, File},{line, Line}]->
+ io_lib:fwrite(" [~s:~w]", [File, Line]);
+ _-> ""
+ end,
+ [io_lib:fwrite(" ~s ~w:~w/~w~s\n called as ~s\n",
+ [Pre,M,F,A,S,C])
| format_stacktrace(Fs,Pre1,Pre1)];
format_stacktrace([],_Pre,_Pre1) ->
"".
@@ -41,9 +41,9 @@ get_stacktrace() ->
get_stacktrace(Ts) ->
eunit_lib:uniq(prune_trace(erlang:get_stacktrace(), Ts)).
-prune_trace([{eunit_data, _, _} | Rest], Tail) ->
+prune_trace([{eunit_data, _, _, _} | Rest], Tail) ->
prune_trace(Rest, Tail);
-prune_trace([{?MODULE, _, _} | _Rest], Tail) ->
+prune_trace([{?MODULE, _, _, _} | _Rest], Tail) ->
Tail;
prune_trace([T | Ts], Tail) ->
[T | prune_trace(Ts, Tail)];

0 comments on commit 73b94a9

Please sign in to comment.