more test data generators

This commit is contained in:
alisdair sullivan 2013-02-08 10:43:52 -08:00
parent 7855fef0f3
commit 18f36ddb0a
2 changed files with 20 additions and 24 deletions

View file

@ -1,7 +1,8 @@
%% data and helper functions for tests
-export([init/1, handle_event/2]).
-export([empty_array/0, empty_object/0]).
-export([empty_array/0, deep_array/0, really_deep_array/0]).
-export([empty_object/0]).
-export([literals/0, naked_literals/0]).
-export([integers/0, naked_integers/0]).
@ -17,6 +18,15 @@ handle_event(Event, State) -> [Event] ++ State.
empty_array() -> [{"[]", <<"[]">>, [], [start_array, end_array]}].
deep_array() ->
[Object] = empty_array(),
[repeat(fun wrap_with_array/1, Object, 10)].
really_deep_array() ->
[Object] = empty_array(),
[repeat(fun wrap_with_array/1, Object, 1000)].
empty_object() -> [{"{}", <<"{}">>, [{}], [start_object, end_object]}].
@ -65,7 +75,7 @@ wrap_with_array({Title, JSON, Term, Events}) ->
"[" ++ Title ++ "]",
<<"[", JSON/binary, "]">>,
[Term],
[start_array, Events, end_array]
[start_array] ++ Events ++ [end_array]
}.
@ -74,5 +84,9 @@ wrap_with_object({Title, JSON, Term, Events}) ->
"{\"key\":" ++ Title ++ "}",
<<"{\"key\":", JSON/binary, "}">>,
[{<<"key">>, Term}],
[start_object, {key, <<"key">>}, Events, end_object]
}.
[start_object, {key, <<"key">>}] ++ Events ++ [end_object]
}.
repeat(_, Object, 0) -> Object;
repeat(Fun, Object, Times) -> repeat(Fun, Fun(Object), Times - 1).

View file

@ -114,28 +114,10 @@ opts_test_() ->
].
valid_json_test_() ->
Data = jsx:empty_array()
++ jsx:empty_object()
++ jsx:literals()
++ jsx:naked_literals()
++ jsx:integers()
++ jsx:naked_integers(),
[ {Title, ?_assertEqual(true, is_json(JSON, []))} || {Title, JSON, _, _} <- Data ].
valid_term_test_() ->
Data = jsx:empty_array()
++ jsx:empty_object()
++ jsx:literals()
++ jsx:naked_literals()
++ jsx:integers()
++ jsx:naked_integers(),
[ {Title, ?_assertEqual(true, is_term(Term, []))} || {Title, _, Term, _} <- Data ].
handle_event_test_() ->
Data = jsx:empty_array()
++ jsx:deep_array()
++ jsx:really_deep_array()
++ jsx:empty_object()
++ jsx:literals()
++ jsx:naked_literals()