Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

185 linhas
3.9 KiB

  1. % This file is part of Jiffy released under the MIT license.
  2. % See the LICENSE file for more information.
  3. -module(jiffy_11_proper_tests).
  4. -ifdef(JIFFY_DEV).
  5. -include_lib("proper/include/proper.hrl").
  6. -include_lib("eunit/include/eunit.hrl").
  7. -include("jiffy_util.hrl").
  8. opts() ->
  9. [
  10. {max_size, 15},
  11. {numtests, 1000}
  12. ].
  13. run(Name) ->
  14. {msg("~s", [Name]), [
  15. {timeout, 300, ?_assert(proper:quickcheck(?MODULE:Name(), opts()))}
  16. ]}.
  17. proper_encode_decode_test_() ->
  18. [
  19. run(prop_enc_dec),
  20. run(prop_enc_dec_pretty),
  21. run(prop_dec_trailer),
  22. run(prop_enc_no_crash),
  23. run(prop_dec_no_crash_bin),
  24. run(prop_dec_no_crash_any)
  25. ].
  26. prop_enc_dec() ->
  27. ?FORALL(Data, json(),
  28. begin
  29. %io:format(standard_error, "Data: ~p~n", [Data]),
  30. Data == jiffy:decode(jiffy:encode(Data))
  31. end
  32. ).
  33. prop_dec_trailer() ->
  34. ?FORALL({T1, T2}, {json(), json()},
  35. begin
  36. B1 = jiffy:encode(T1),
  37. B2 = jiffy:encode(T2),
  38. Combiners = [
  39. <<" ">>,
  40. <<"\r\t">>,
  41. <<"\n \t">>,
  42. <<" ">>
  43. ],
  44. lists:foreach(fun(Comb) ->
  45. Bin = <<B1/binary, Comb/binary, B2/binary>>,
  46. {has_trailer, T1, Rest} = jiffy:decode(Bin, [return_trailer]),
  47. T2 = jiffy:decode(Rest)
  48. end, Combiners),
  49. true
  50. end
  51. ).
  52. -ifndef(JIFFY_NO_MAPS).
  53. to_map_ejson({Props}) ->
  54. NewProps = [{K, to_map_ejson(V)} || {K, V} <- Props],
  55. maps:from_list(NewProps);
  56. to_map_ejson(Vals) when is_list(Vals) ->
  57. [to_map_ejson(V) || V <- Vals];
  58. to_map_ejson(Val) ->
  59. Val.
  60. prop_map_enc_dec() ->
  61. ?FORALL(Data, json(),
  62. begin
  63. MapData = to_map_ejson(Data),
  64. MapData == jiffy:decode(jiffy:encode(MapData), [return_maps])
  65. end
  66. ).
  67. -endif.
  68. prop_enc_dec_pretty() ->
  69. ?FORALL(Data, json(),
  70. begin
  71. Data == jiffy:decode(jiffy:encode(Data, [pretty]))
  72. end
  73. ).
  74. prop_enc_no_crash() ->
  75. ?FORALL(Data, any(), begin catch jiffy:encode(Data), true end).
  76. prop_dec_no_crash_bin() ->
  77. ?FORALL(Data, binary(), begin catch jiffy:decode(Data), true end).
  78. prop_dec_no_crash_any() ->
  79. ?FORALL(Data, any(), begin catch jiffy:decode(Data), true end).
  80. % JSON Generation
  81. json_null() ->
  82. null.
  83. json_boolean() ->
  84. oneof([true, false]).
  85. json_number() ->
  86. oneof([integer(), float()]).
  87. json_string() ->
  88. escaped_utf8_bin().
  89. json_list(S) when S =< 0 ->
  90. [];
  91. json_list(S) ->
  92. ?LETSHRINK(
  93. [ListSize],
  94. [integer(0, S)],
  95. vector(ListSize, json_text(S - ListSize))
  96. ).
  97. json_object(S) when S =< 0 ->
  98. {[]};
  99. json_object(S) ->
  100. ?LETSHRINK(
  101. [ObjectSize],
  102. [integer(0, S)],
  103. {vector(ObjectSize, {json_string(), json_text(S - ObjectSize)})}
  104. ).
  105. json_value() ->
  106. oneof([
  107. json_null(),
  108. json_boolean(),
  109. json_string(),
  110. json_number()
  111. ]).
  112. json_text(S) when S > 0 ->
  113. ?LAZY(oneof([
  114. json_list(S),
  115. json_object(S)
  116. ]));
  117. json_text(_) ->
  118. json_value().
  119. json() ->
  120. ?SIZED(S, json_text(S)).
  121. %% XXX: Add generators
  122. %
  123. % We should add generators that generate JSON binaries directly
  124. % so we can test things that aren't produced by the encoder.
  125. %
  126. % We should also have a version of the JSON generator that inserts
  127. % errors into the JSON that we can test for.
  128. escaped_utf8_bin() ->
  129. ?SUCHTHAT(Bin,
  130. ?LET(S, ?SUCHTHAT(L, list(escaped_char()), L /= []),
  131. unicode:characters_to_binary(S, unicode, utf8)),
  132. is_binary(Bin)
  133. ).
  134. escaped_char() ->
  135. ?LET(C, char(),
  136. case C of
  137. $" -> "\\\"";
  138. C when C == 65534 -> 65533;
  139. C when C == 65535 -> 65533;
  140. C when C > 1114111 -> 1114111;
  141. C -> C
  142. end
  143. ).
  144. -endif.