rewrite from lager
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

63 line
2.1 KiB

4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
  1. -module(pr_stacktrace_test).
  2. -compile([{parse_transform, lager_transform}]).
  3. -ifdef(OTP_RELEASE). %% this implies 21 or higher
  4. -define(EXCEPTION(Class, Reason, Stacktrace), Class:Reason:Stacktrace).
  5. -define(GET_STACK(Stacktrace), Stacktrace).
  6. -else.
  7. -define(EXCEPTION(Class, Reason, _), Class:Reason).
  8. -define(GET_STACK(_), erlang:get_stacktrace()).
  9. -endif.
  10. -include_lib("eunit/include/eunit.hrl").
  11. make_throw() ->
  12. throw({test, exception}).
  13. bad_arity() ->
  14. lists:concat([], []).
  15. bad_arg() ->
  16. integer_to_list(1.0).
  17. pr_stacktrace_throw_test() ->
  18. Got = try
  19. make_throw()
  20. catch
  21. ?EXCEPTION(Class, Reason, Stacktrace) ->
  22. eRum:pr_stacktrace(?GET_STACK(Stacktrace), {Class, Reason})
  23. end,
  24. Want = "pr_stacktrace_test:pr_stacktrace_throw_test/0 line 26\n pr_stacktrace_test:make_throw/0 line 16\nthrow:{test,exception}",
  25. ?assertNotEqual(nomatch, string:find(Got, Want)).
  26. pr_stacktrace_bad_arg_test() ->
  27. Got = try
  28. bad_arg()
  29. catch
  30. ?EXCEPTION(Class, Reason, Stacktrace) ->
  31. eRum:pr_stacktrace(?GET_STACK(Stacktrace), {Class, Reason})
  32. end,
  33. Want = "pr_stacktrace_test:pr_stacktrace_bad_arg_test/0 line 36\n pr_stacktrace_test:bad_arg/0 line 22\nerror:badarg",
  34. ?assertNotEqual(nomatch, string:find(Got, Want)).
  35. pr_stacktrace_bad_arity_test() ->
  36. Got = try
  37. bad_arity()
  38. catch
  39. ?EXCEPTION(Class, Reason, Stacktrace) ->
  40. eRum:pr_stacktrace(?GET_STACK(Stacktrace), {Class, Reason})
  41. end,
  42. Want = "pr_stacktrace_test:pr_stacktrace_bad_arity_test/0 line 46\n lists:concat([], [])\nerror:undef",
  43. ?assertNotEqual(nomatch, string:find(Got, Want)).
  44. pr_stacktrace_no_reverse_test() ->
  45. application:set_env(lager, reverse_pretty_stacktrace, false),
  46. Got = try
  47. bad_arity()
  48. catch
  49. ?EXCEPTION(Class, Reason, Stacktrace) ->
  50. eRum:pr_stacktrace(?GET_STACK(Stacktrace), {Class, Reason})
  51. end,
  52. Want = "error:undef\n lists:concat([], [])\n pr_stacktrace_test:pr_stacktrace_bad_arity_test/0 line 57",
  53. ?assertEqual(nomatch, string:find(Got, Want)).