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.

48 lines
1.3 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({E}, Options) ->
  18. J = jiffy:encode(E),
  19. etap:is(jiffy:decode(J), E, ok_dec(J, E)),
  20. etap:is(do_encode(E, Options), J, ok_enc(E, J));
  21. check_good({J, E}, Options) ->
  22. etap:is(jiffy:decode(J), E, ok_dec(J, E)),
  23. etap:is(do_encode(E, Options), J, ok_enc(E, J));
  24. check_good({J, E, J2}, Options) ->
  25. etap:is(jiffy:decode(J), E, ok_dec(J, E)),
  26. etap:is(do_encode(E, Options), J2, ok_enc(E, J2)).
  27. check_error({J, E}) ->
  28. etap:fun_is(
  29. fun({error, E1}) when E1 == E -> true; (E1) -> E1 end,
  30. (catch jiffy:decode(J)),
  31. error_mesg(J)
  32. );
  33. check_error(J) ->
  34. etap:fun_is(
  35. fun({error, _}) -> true; (Else) -> Else end,
  36. (catch jiffy:decode(J)),
  37. error_mesg(J)
  38. ).