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

83 行
2.1 KiB

  1. -module(proper_tests).
  2. -include_lib("proper/include/proper.hrl").
  3. -include_lib("eunit/include/eunit.hrl").
  4. -export([proper_test_/0]).
  5. all() -> proper_ct:testcases(?MODULE).
  6. init_per_testcase(tc_prop_foo, Config) ->
  7. [{proper, [{numtests, 1000}]} | Config].
  8. %% Helper funs
  9. escaped_char() ->
  10. ?LET(C, char(),
  11. case C == $" of
  12. true -> "\\\"";
  13. false -> C
  14. end).
  15. escaped_utf8_bin() ->
  16. ?SUCHTHAT(Bin,
  17. ?LET(S, ?SUCHTHAT(L, list(escaped_char()), L /= []),
  18. unicode:characters_to_binary(S, unicode, utf8)),
  19. is_binary(Bin)).
  20. %% Atomic types
  21. json_null() ->
  22. null.
  23. json_string() ->
  24. escaped_utf8_bin().
  25. json_number() ->
  26. oneof([integer(), float()]).
  27. json_boolean() ->
  28. oneof([true, false]).
  29. json_atomic() ->
  30. oneof([json_null(),
  31. json_string(),
  32. json_number(),
  33. json_boolean()]).
  34. %% Compound types
  35. json_object() ->
  36. ?SIZED(S, json_object(S)).
  37. json_object(S) when S =< 0 ->
  38. json_atomic();
  39. json_object(S) ->
  40. frequency([{1, json_object(0)},
  41. {3, ?LAZY(json_list(S))},
  42. {3, ?LAZY(
  43. ?LETSHRINK(
  44. [ObjectSize],
  45. [integer(1, S)],
  46. ?LETSHRINK(
  47. [Object],
  48. [{vector(ObjectSize,
  49. {json_string(),
  50. json_object(S - ObjectSize)})}],
  51. Object
  52. )))}]).
  53. json_list(S) ->
  54. ?LETSHRINK([ListSize],
  55. [integer(1, S)],
  56. vector(ListSize, json_object(S - ListSize))).
  57. json_list() ->
  58. list(json_object()).
  59. prop_encode_decode() ->
  60. ?FORALL(Data, json_object(),
  61. begin
  62. %% io:format(user, "Data: ~p~n", [Data]),
  63. Data == jiffy:decode(jiffy:encode(Data))
  64. end).
  65. proper_test_() ->
  66. {timeout, 3600,
  67. ?_assertEqual([], proper:module(proper_tests, [{to_file, user},
  68. {numtests, 1000}]))}.