reduce test cases to saner set

This commit is contained in:
alisdair sullivan 2013-02-13 19:13:50 -08:00
parent 5c96222cbf
commit 9e01e3a704
7 changed files with 41 additions and 159 deletions

View file

@ -1629,8 +1629,7 @@ to_fake_utf(N, utf8) ->
decode_test_() -> decode_test_() ->
Data = jsx:universals() Data = jsx:test_cases(),
++ jsx:decodeables(),
[ [
{ {
Title, ?_assertEqual( Title, ?_assertEqual(

View file

@ -137,7 +137,7 @@ clean_string(Bin, Config) -> jsx_utils:clean_string(Bin, Config).
encode_test_() -> encode_test_() ->
Data = jsx:universals(), Data = jsx:test_cases(),
[ [
{ {
Title, ?_assertEqual( Title, ?_assertEqual(

View file

@ -170,8 +170,7 @@ clean_string(Bin, Config) -> jsx_utils:clean_string(Bin, Config).
decode_test_() -> decode_test_() ->
Data = jsx:universals() Data = jsx:test_cases(),
++ jsx:decodeables(),
[ [
{ {
Title, ?_assertEqual( Title, ?_assertEqual(

View file

@ -1,8 +1,7 @@
%% data and helper functions for tests %% data and helper functions for tests
-export([init/1, handle_event/2]). -export([init/1, handle_event/2]).
-export([universals/0]). -export([test_cases/0]).
-export([decodeables/0]).
-include_lib("eunit/include/eunit.hrl"). -include_lib("eunit/include/eunit.hrl").
@ -15,30 +14,47 @@ handle_event(end_json, State) -> lists:reverse([end_json] ++ State);
handle_event(Event, State) -> [Event] ++ State. handle_event(Event, State) -> [Event] ++ State.
universals() -> test_cases() ->
empty_array() empty_array()
++ deep_array() ++ nested_array()
++ really_deep_array()
++ empty_object() ++ empty_object()
++ nested_object()
++ strings()
++ literals() ++ literals()
++ integers() ++ integers()
++ floats() ++ floats().
++ strings().
empty_array() -> [{"[]", <<"[]">>, [], [start_array, end_array]}]. empty_array() -> [{"[]", <<"[]">>, [], [start_array, end_array]}].
deep_array() -> nested_array() ->
[Test] = empty_array(), [{
[repeat(fun wrap_with_array/1, Test, 10)]. "[[[]]]",
<<"[[[]]]">>,
really_deep_array() -> [[[]]],
[Test] = empty_array(), [start_array, start_array, start_array, end_array, end_array, end_array]
[repeat(fun wrap_with_array/1, Test, 1000)]. }].
empty_object() -> [{"{}", <<"{}">>, [{}], [start_object, end_object]}]. empty_object() -> [{"{}", <<"{}">>, [{}], [start_object, end_object]}].
nested_object() ->
[{
"{\"key\":{\"key\":{}}}",
<<"{\"key\":{\"key\":{}}}">>,
[{<<"key">>, [{<<"key">>, [{}]}]}],
[
start_object,
{key, <<"key">>},
start_object,
{key, <<"key">>},
start_object,
end_object,
end_object,
end_object
]
}].
naked_strings() -> naked_strings() ->
Raw = [ Raw = [
@ -58,16 +74,7 @@ naked_strings() ->
strings() -> strings() ->
naked_strings() naked_strings()
++ [ wrap_with_array(Test) || Test <- naked_strings() ] ++ [ wrap_with_array(Test) || Test <- naked_strings() ]
++ [ wrap_with_object(Test) || Test <- naked_strings() ] ++ [ wrap_with_object(Test) || Test <- naked_strings() ].
++ [listify("naked strings", naked_strings())]
++ [
{
"naked strings",
<<"{\"\":\"\",\"hello world\":\"hello world\"}">>,
[{<<>>, <<>>}, {<<"hello world">>, <<"hello world">>}],
[start_object, {key, <<>>}, {string, <<>>}, {key, <<"hello world">>}, {string, <<"hello world">>}, end_object]
}
].
naked_integers() -> naked_integers() ->
@ -92,9 +99,7 @@ naked_integers() ->
integers() -> integers() ->
naked_integers() naked_integers()
++ [ wrap_with_array(Test) || Test <- naked_integers() ] ++ [ wrap_with_array(Test) || Test <- naked_integers() ]
++ [ wrap_with_object(Test) || Test <- naked_integers() ] ++ [ wrap_with_object(Test) || Test <- naked_integers() ].
++ [listify("naked integers", naked_integers())]
++ [objectify("naked integers", naked_integers())].
naked_floats() -> naked_floats() ->
@ -124,9 +129,7 @@ naked_floats() ->
floats() -> floats() ->
naked_floats() naked_floats()
++ [ wrap_with_array(Test) || Test <- naked_floats() ] ++ [ wrap_with_array(Test) || Test <- naked_floats() ]
++ [ wrap_with_object(Test) || Test <- naked_floats() ] ++ [ wrap_with_object(Test) || Test <- naked_floats() ].
++ [listify("naked floats", naked_floats())]
++ [objectify("naked floats", naked_floats())].
naked_literals() -> naked_literals() ->
@ -143,27 +146,7 @@ naked_literals() ->
literals() -> literals() ->
naked_literals() naked_literals()
++ [ wrap_with_array(Test) || Test <- naked_literals() ] ++ [ wrap_with_array(Test) || Test <- naked_literals() ]
++ [ wrap_with_object(Test) || Test <- naked_literals() ] ++ [ wrap_with_object(Test) || Test <- naked_literals() ].
++ [listify("naked literals", naked_literals())]
++ [objectify("naked literals", naked_literals())].
%% special tests used only for things that don't round trip when decoded and re-encoded
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("naked decodeables", Tests)]
++ [objectify("naked decodeables", Tests)].
wrap_with_array({Title, JSON, Term, Events}) -> wrap_with_array({Title, JSON, Term, Events}) ->
@ -184,103 +167,6 @@ wrap_with_object({Title, JSON, Term, Events}) ->
}. }.
repeat(_, Test, 0) -> Test;
repeat(Fun, Test, Times) -> repeat(Fun, Fun(Test), Times - 1).
sane_float_to_list(X) -> sane_float_to_list(X) ->
[Output] = io_lib:format("~p", [X]), [Output] = io_lib:format("~p", [X]),
Output. Output.
listify(Title, [{_, JSON, Term, Events}|Rest]) -> do_listify(Rest, {Title, JSON, [Term], Events}).
do_listify([], {Title, JSON, Term, Events}) ->
{
Title,
<<"["/utf8, JSON/binary, "]"/utf8>>,
Term,
[start_array] ++ Events ++ [end_array]
};
do_listify([Test|Rest], Acc) ->
{Title, A, M, X} = Acc,
{_, B, N, Y} = Test,
do_listify(Rest, {Title, <<A/binary, ","/utf8, B/binary>>, M ++ [N], X ++ Y}).
objectify(Title, [{_, JSON, Term, Events}|Rest]) ->
do_objectify(
Rest,
{Title, <<"\"", JSON/binary, "\":", JSON/binary>>, [{JSON, Term}], [{key, JSON}] ++ Events}
).
do_objectify([], {Title, JSON, Term, Events}) ->
{
Title,
<<"{"/utf8, JSON/binary, "}"/utf8>>,
Term,
[start_object] ++ Events ++ [end_object]
};
do_objectify([Test|Rest], Acc) ->
{Title, A, M, X} = Acc,
{_, B, N, Y} = Test,
do_objectify(Rest, {
Title,
<<A/binary, ",\"", B/binary, "\":"/utf8, B/binary>>,
M ++ [{B, N}],
X ++ [{key, B}] ++ Y
}).
listify_test_() ->
{"listify test", ?_assertEqual(
{
"listify test",
<<"[true,1,\"hello world\",{}]">>,
[true, 1, <<"hello world">>, [{}]],
[
start_array,
{literal, true},
{integer, 1},
{string, <<"hello world">>},
start_object,
end_object,
end_array
]
},
listify("listify test", [
{"true", <<"true">>, true, [{literal, true}]},
{"1", <<"1">>, 1, [{integer, 1}]},
{"hello world", <<"\"hello world\"">>, <<"hello world">>, [{string, <<"hello world">>}]},
{"{}", <<"{}">>, [{}], [start_object, end_object]}
])
)}.
objectify_test_() ->
{"objectify test", ?_assertEqual(
{
"objectify test",
<<"{\"true\":true,\"1\":1,\"\"hello world\"\":\"hello world\",\"[]\":[]}">>,
[{<<"true">>, true}, {<<"1">>, 1}, {<<"\"hello world\"">>, <<"hello world">>}, {<<"[]">>, []}],
[
start_object,
{key, <<"true">>},
{literal, true},
{key, <<"1">>},
{integer, 1},
{key, <<"\"hello world\"">>},
{string, <<"hello world">>},
{key, <<"[]">>},
start_array,
end_array,
end_object
]
},
objectify("objectify test", [
{"true", <<"true">>, true, [{literal, true}]},
{"1", <<"1">>, 1, [{integer, 1}]},
{"hello world", <<"\"hello world\"">>, <<"hello world">>, [{string, <<"hello world">>}]},
{"[]", <<"[]">>, [], [start_array, end_array]}
])
)}.

View file

@ -292,7 +292,7 @@ format_test_() ->
handle_event_test_() -> handle_event_test_() ->
Data = jsx:universals(), Data = jsx:test_cases(),
[ [
{ {
Title, ?_assertEqual( Title, ?_assertEqual(

View file

@ -267,8 +267,7 @@ post_decoders_test_() ->
handle_event_test_() -> handle_event_test_() ->
Data = jsx:universals() Data = jsx:test_cases(),
++ jsx:decodeables(),
[ [
{ {
Title, ?_assertEqual( Title, ?_assertEqual(

View file

@ -115,8 +115,7 @@ config_test_() ->
handle_event_test_() -> handle_event_test_() ->
Data = jsx:universals() Data = jsx:test_cases(),
++ jsx:decodeables(),
[ [
{ {
Title, ?_assertEqual( Title, ?_assertEqual(