選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

132 行
2.6 KiB

  1. % This file is part of Jiffy released under the MIT license.
  2. % See the LICENSE file for more information.
  3. -module(jiffy_tests).
  4. -include_lib("proper/include/proper.hrl").
  5. -include_lib("eunit/include/eunit.hrl").
  6. proper_test_() ->
  7. PropErOpts = [
  8. {to_file, user},
  9. {max_size, 15},
  10. {numtests, 1000}
  11. ],
  12. {timeout, 3600, ?_assertEqual([], proper:module(jiffy_tests, PropErOpts))}.
  13. prop_encode_decode() ->
  14. ?FORALL(Data, json(),
  15. begin
  16. %io:format(standard_error, "Data: ~p~n", [Data]),
  17. Data == jiffy:decode(jiffy:encode(Data))
  18. end
  19. ).
  20. prop_encode_decode_pretty() ->
  21. ?FORALL(Data, json(),
  22. begin
  23. Data == jiffy:decode(jiffy:encode(Data, [pretty]))
  24. end
  25. ).
  26. prop_encode_not_crash() ->
  27. ?FORALL(Data, any(), begin catch jiffy:encode(Data), true end).
  28. prop_decode_not_crash_bin() ->
  29. ?FORALL(Data, binary(), begin catch jiffy:decode(Data), true end).
  30. prop_decode_not_crash_any() ->
  31. ?FORALL(Data, any(), begin catch jiffy:decode(Data), true end).
  32. % JSON Generation
  33. json_null() ->
  34. null.
  35. json_boolean() ->
  36. oneof([true, false]).
  37. json_number() ->
  38. oneof([integer(), float()]).
  39. json_string() ->
  40. escaped_utf8_bin().
  41. json_list(S) when S =< 0 ->
  42. [];
  43. json_list(S) ->
  44. ?LETSHRINK(
  45. [ListSize],
  46. [integer(0, S)],
  47. vector(ListSize, json_text(S - ListSize))
  48. ).
  49. json_object(S) when S =< 0 ->
  50. {[]};
  51. json_object(S) ->
  52. ?LETSHRINK(
  53. [ObjectSize],
  54. [integer(0, S)],
  55. {vector(ObjectSize, {json_string(), json_text(S - ObjectSize)})}
  56. ).
  57. json_value() ->
  58. oneof([
  59. json_null(),
  60. json_boolean(),
  61. json_string(),
  62. json_number()
  63. ]).
  64. json_text(S) when S > 0 ->
  65. ?LAZY(oneof([
  66. json_list(S),
  67. json_object(S)
  68. ]));
  69. json_text(_) ->
  70. json_value().
  71. json() ->
  72. ?SIZED(S, json_text(S)).
  73. %% XXX: Add generators
  74. %
  75. % We should add generators that generate JSON binaries directly
  76. % so we can test things that aren't produced by the encoder.
  77. %
  78. % We should also have a version of the JSON generator that inserts
  79. % errors into the JSON that we can test for.
  80. escaped_utf8_bin() ->
  81. ?SUCHTHAT(Bin,
  82. ?LET(S, ?SUCHTHAT(L, list(escaped_char()), L /= []),
  83. unicode:characters_to_binary(S, unicode, utf8)),
  84. is_binary(Bin)
  85. ).
  86. escaped_char() ->
  87. ?LET(C, char(),
  88. case C of
  89. $" -> "\\\"";
  90. C when C == 65534 -> 65533;
  91. C when C == 65535 -> 65533;
  92. C when C > 1114111 -> 1114111;
  93. C -> C
  94. end
  95. ).