break special decodeable forms out to own test data

This commit is contained in:
alisdair sullivan 2013-02-10 19:39:42 -08:00
parent 086803cc95
commit e6380bc3a3
4 changed files with 69 additions and 13 deletions

View file

@ -6,6 +6,7 @@
-export([literals/0, naked_literals/0]). -export([literals/0, naked_literals/0]).
-export([integers/0, naked_integers/0]). -export([integers/0, naked_integers/0]).
-export([floats/0, naked_floats/0]). -export([floats/0, naked_floats/0]).
-export([decodeables/0]).
-include_lib("eunit/include/eunit.hrl"). -include_lib("eunit/include/eunit.hrl").
@ -49,7 +50,7 @@ naked_integers() ->
[{integer, X}] [{integer, X}]
} }
|| X <- Raw ++ [ -1 * Y || Y <- Raw ] ++ [0] || X <- Raw ++ [ -1 * Y || Y <- Raw ] ++ [0]
] ++ [{"-0", <<"-0">>, 0, [{integer, 0}]}]. ].
integers() -> integers() ->
[ wrap_with_array(Test) || Test <- naked_integers() ] [ wrap_with_array(Test) || Test <- naked_integers() ]
@ -80,13 +81,7 @@ naked_floats() ->
[{float, X}] [{float, X}]
} }
|| X <- Raw ++ [ -1 * Y || Y <- Raw ] || X <- Raw ++ [ -1 * Y || Y <- Raw ]
] ].
++ [{"-0.0", <<"-0.0">>, 0.0, [{float, 0.0}]}]
++ [{"1e0", <<"1e0">>, 1.0, [{float, 1.0}]}]
++ [{"0e0", <<"0e0">>, 0.0, [{float, 0.0}]}]
++ [{"1e4", <<"1e4">>, 1.0e4, [{float, 1.0e4}]}]
++ [{"0e4", <<"0e4">>, 0.0, [{float, 0.0}]}]
++ [{"-1e0", <<"-1e0">>, -1.0, [{float, -1.0}]}].
floats() -> floats() ->
@ -95,6 +90,22 @@ floats() ->
++ [listify(naked_floats())] ++ [listify(naked_floats())]
++ [objectify(naked_floats())]. ++ [objectify(naked_floats())].
decodeables() ->
Tests = [
{"-0.0", <<"-0.0">>, 0.0, [{float, 0.0}]},
{"1e0", <<"1e0">>, 1.0, [{float, 1.0}]},
{"0e0", <<"0e0">>, 0.0, [{float, 0.0}]},
{"1e4", <<"1e4">>, 1.0e4, [{float, 1.0e4}]},
{"0e4", <<"0e4">>, 0.0, [{float, 0.0}]},
{"-1e0", <<"-1e0">>, -1.0, [{float, -1.0}]},
{"-0", <<"-0">>, 0, [{integer, 0}]}
],
[ wrap_with_array(Test) || Test <- Tests ]
++ [ wrap_with_object(Test) || Test <- Tests ]
++ [listify(Tests)]
++ [objectify(Tests)].
sane_float_to_list(X) -> sane_float_to_list(X) ->
[Output] = io_lib:format("~p", [X]), [Output] = io_lib:format("~p", [X]),
Output. Output.
@ -147,18 +158,18 @@ listify([], {_, JSON, Term, Events}) ->
binary_to_list(<<"["/utf8, JSON/binary, "]"/utf8>>), binary_to_list(<<"["/utf8, JSON/binary, "]"/utf8>>),
<<"["/utf8, JSON/binary, "]"/utf8>>, <<"["/utf8, JSON/binary, "]"/utf8>>,
Term, Term,
[start_array, Events, end_array] [start_array] ++ Events ++ [end_array]
}; };
listify([Test|Rest], Acc) -> listify([Test|Rest], Acc) ->
{_, A, M, X} = Acc, {_, A, M, X} = Acc,
{_, B, N, Y} = Test, {_, B, N, Y} = Test,
listify(Rest, {null, <<A/binary, ", "/utf8, B/binary>>, M ++ [N], X ++ Y}). listify(Rest, {null, <<A/binary, ","/utf8, B/binary>>, M ++ [N], X ++ Y}).
objectify([{_, JSON, Term, Events}|Rest]) -> objectify([{_, JSON, Term, Events}|Rest]) ->
objectify( objectify(
Rest, Rest,
{null, <<"\"", JSON/binary, "\": ", JSON/binary>>, [{JSON, Term}], [{key, JSON}] ++ Events} {null, <<"\"", JSON/binary, "\":", JSON/binary>>, [{JSON, Term}], [{key, JSON}] ++ Events}
). ).
objectify([], {_, JSON, Term, Events}) -> objectify([], {_, JSON, Term, Events}) ->
@ -173,7 +184,7 @@ objectify([Test|Rest], Acc) ->
{_, B, N, [Y]} = Test, {_, B, N, [Y]} = Test,
objectify(Rest, { objectify(Rest, {
null, null,
<<A/binary, ", \"", B/binary, "\": "/utf8, B/binary>>, <<A/binary, ",\"", B/binary, "\":"/utf8, B/binary>>,
M ++ [{B, N}], M ++ [{B, N}],
X ++ [{key, B}, Y] X ++ [{key, B}, Y]
}). }).

View file

@ -291,4 +291,25 @@ format_test_() ->
]. ].
handle_event_test_() ->
Data = jsx:empty_array()
++ jsx:deep_array()
++ jsx:really_deep_array()
++ jsx:empty_object()
++ jsx:literals()
++ jsx:naked_literals()
++ jsx:integers()
++ jsx:naked_integers()
++ jsx:floats()
++ jsx:naked_floats(),
[
{
Title, ?_assertEqual(
JSON,
lists:foldl(fun handle_event/2, {start, [], #opts{}}, Events ++ [end_json])
)
} || {Title, JSON, _, Events} <- Data
].
-endif. -endif.

View file

@ -265,4 +265,27 @@ post_decoders_test_() ->
)} )}
]. ].
handle_event_test_() ->
Data = jsx:empty_array()
++ jsx:deep_array()
++ jsx:really_deep_array()
++ jsx:empty_object()
++ jsx:literals()
++ jsx:naked_literals()
++ jsx:integers()
++ jsx:naked_integers()
++ jsx:floats()
++ jsx:naked_floats()
++ jsx:decodeables(),
[
{
Title, ?_assertEqual(
Term,
lists:foldl(fun handle_event/2, {[[]], #opts{}}, Events ++ [end_json])
)
} || {Title, _, Term, Events} <- Data
].
-endif. -endif.

View file

@ -124,7 +124,8 @@ handle_event_test_() ->
++ jsx:integers() ++ jsx:integers()
++ jsx:naked_integers() ++ jsx:naked_integers()
++ jsx:floats() ++ jsx:floats()
++ jsx:naked_floats(), ++ jsx:naked_floats()
++ jsx:decodeables(),
[ [
{ {
Title, ?_assertEqual( Title, ?_assertEqual(