您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

44 行
1.2 KiB

  1. -module(util).
  2. -export([test_good/1, test_good/2, test_errors/1]).
  3. test_good(Cases) ->
  4. test_good(Cases, []).
  5. test_good(Cases, Options) ->
  6. lists:foreach(fun(Case) -> check_good(Case, Options) end, Cases).
  7. test_errors(Cases) ->
  8. lists:foreach(fun(Case) -> check_error(Case) end, Cases).
  9. ok_dec(J, _E) ->
  10. lists:flatten(io_lib:format("Decoded ~p.", [J])).
  11. ok_enc(E, _J) ->
  12. lists:flatten(io_lib:format("Encoded ~p", [E])).
  13. do_encode(E, Options) ->
  14. iolist_to_binary(jiffy:encode(E, Options)).
  15. error_mesg(J) ->
  16. lists:flatten(io_lib:format("Decoding ~p returns an error.", [J])).
  17. check_good({J, E}, Options) ->
  18. etap:is(jiffy:decode(J), E, ok_dec(J, E)),
  19. etap:is(do_encode(E, Options), J, ok_enc(E, J));
  20. check_good({J, E, J2}, Options) ->
  21. etap:is(jiffy:decode(J), E, ok_dec(J, E)),
  22. etap:is(do_encode(E, Options), J2, ok_enc(E, J2)).
  23. check_error({J, E}) ->
  24. etap:fun_is(
  25. fun({error, E1}) when E1 == E -> true; (E1) -> E1 end,
  26. (catch jiffy:decode(J)),
  27. error_mesg(J)
  28. );
  29. check_error(J) ->
  30. etap:fun_is(
  31. fun({error, _}) -> true; (Else) -> Else end,
  32. (catch jiffy:decode(J)),
  33. error_mesg(J)
  34. ).