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.

51 lines
1.3 KiB

преди 13 години
преди 13 години
преди 11 години
преди 11 години
  1. #! /usr/bin/env escript
  2. % This file is part of Jiffy released under the MIT license.
  3. % See the LICENSE file for more information.
  4. main([]) ->
  5. code:add_pathz("ebin"),
  6. code:add_pathz("test"),
  7. etap:plan(15),
  8. util:test_good(good()),
  9. test_encode(),
  10. util:test_errors(errors()),
  11. etap:end_tests().
  12. good() ->
  13. [
  14. {<<"{}">>, {[]}},
  15. {<<"{\"foo\": \"bar\"}">>,
  16. {[{<<"foo">>, <<"bar">>}]},
  17. <<"{\"foo\":\"bar\"}">>},
  18. {<<"\n\n{\"foo\":\r \"bar\",\n \"baz\"\t: 123 }">>,
  19. {[{<<"foo">>, <<"bar">>}, {<<"baz">>, 123}]},
  20. <<"{\"foo\":\"bar\",\"baz\":123}">>}
  21. ].
  22. test_encode() ->
  23. % Its ok to use iolist() for keys
  24. Cases = [
  25. {<<"{\"foo\":true}">>, {[{"foo",true}]}},
  26. {<<"{\"foo\":true}">>, {[{["f","o",<<"o">>],true}]}},
  27. {<<"{\"foo\":[98,97,114]}">>, {[{"foo","bar"}]}}
  28. ],
  29. lists:foreach(fun({J, E}) ->
  30. Msg = lists:flatten(io_lib:format("Encoded ~p", [E])),
  31. etap:is(jiffy:encode(E), J, Msg)
  32. end, Cases).
  33. errors() ->
  34. [
  35. <<"{">>,
  36. <<"{,}">>,
  37. <<"{123:true}">>,
  38. <<"{false:123}">>,
  39. <<"{:\"stuff\"}">>,
  40. <<"{\"key\":}">>,
  41. <<"{\"key\": 123">>,
  42. <<"{\"key\": 123 true">>,
  43. <<"{\"key\": 123,}">>
  44. ].