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.

55 lines
1.5 KiB

преди 11 години
преди 11 години
преди 11 години
преди 14 години
преди 14 години
преди 14 години
преди 14 години
  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. check_map({J, J2}, Options),
  23. etap:is(do_encode(E, Options), J2, ok_enc(E, J2)).
  24. check_map({J, J2}, Options) ->
  25. try jiffy:decode(J, [map]) of
  26. E2 ->
  27. % etap function breaks all tests because of etap:plan, so stop
  28. % if map test failed
  29. J2 = do_encode(E2, Options)
  30. catch throw:{error, {1, map_unavailable}} ->
  31. ok
  32. end.
  33. check_error({J, E}) ->
  34. etap:fun_is(
  35. fun({error, E1}) when E1 == E -> true; (E1) -> E1 end,
  36. (catch jiffy:decode(J)),
  37. error_mesg(J)
  38. );
  39. check_error(J) ->
  40. etap:fun_is(
  41. fun({error, _}) -> true; (Else) -> Else end,
  42. (catch jiffy:decode(J)),
  43. error_mesg(J)
  44. ).