simplify exportation of tests, add strings to test suite
This commit is contained in:
parent
eaf19cde9c
commit
2f47fdd642
8 changed files with 79 additions and 90 deletions
|
@ -22,6 +22,7 @@
|
|||
|
||||
|
||||
-module(jsx).
|
||||
-compile(export_all).
|
||||
|
||||
-export([encode/1, encode/2, decode/1, decode/2]).
|
||||
-export([is_json/1, is_json/2, is_term/1, is_term/2]).
|
||||
|
|
|
@ -1613,16 +1613,7 @@ to_fake_utf(N, utf8) ->
|
|||
|
||||
|
||||
decode_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()
|
||||
Data = jsx:universals()
|
||||
++ jsx:decodeables(),
|
||||
[
|
||||
{
|
||||
|
|
|
@ -137,16 +137,7 @@ clean_string(Bin, Opts) -> jsx_utils:clean_string(Bin, Opts).
|
|||
|
||||
|
||||
encode_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(),
|
||||
Data = jsx:universals(),
|
||||
[
|
||||
{
|
||||
Title, ?_assertEqual(
|
||||
|
|
|
@ -170,16 +170,7 @@ clean_string(Bin, Opts) -> jsx_utils:clean_string(Bin, Opts).
|
|||
|
||||
|
||||
decode_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()
|
||||
Data = jsx:universals()
|
||||
++ jsx:decodeables(),
|
||||
[
|
||||
{
|
||||
|
|
|
@ -1,11 +1,7 @@
|
|||
%% data and helper functions for tests
|
||||
|
||||
-export([init/1, handle_event/2]).
|
||||
-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]).
|
||||
-export([floats/0, naked_floats/0]).
|
||||
-export([universals/0]).
|
||||
-export([decodeables/0]).
|
||||
|
||||
|
||||
|
@ -19,6 +15,17 @@ handle_event(end_json, State) -> lists:reverse([end_json] ++ State);
|
|||
handle_event(Event, State) -> [Event] ++ State.
|
||||
|
||||
|
||||
universals() ->
|
||||
empty_array()
|
||||
++ deep_array()
|
||||
++ really_deep_array()
|
||||
++ empty_object()
|
||||
++ literals()
|
||||
++ integers()
|
||||
++ floats()
|
||||
++ strings().
|
||||
|
||||
|
||||
empty_array() -> [{"[]", <<"[]">>, [], [start_array, end_array]}].
|
||||
|
||||
deep_array() ->
|
||||
|
@ -33,6 +40,36 @@ really_deep_array() ->
|
|||
empty_object() -> [{"{}", <<"{}">>, [{}], [start_object, end_object]}].
|
||||
|
||||
|
||||
naked_strings() ->
|
||||
Raw = [
|
||||
"",
|
||||
"hello world"
|
||||
],
|
||||
[
|
||||
{
|
||||
String,
|
||||
<<"\"", (list_to_binary(String))/binary, "\"">>,
|
||||
list_to_binary(String),
|
||||
[{string, list_to_binary(String)}]
|
||||
}
|
||||
|| String <- Raw
|
||||
].
|
||||
|
||||
strings() ->
|
||||
naked_strings()
|
||||
++ [ wrap_with_array(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() ->
|
||||
Raw = [
|
||||
1, 2, 3,
|
||||
|
@ -53,7 +90,8 @@ naked_integers() ->
|
|||
].
|
||||
|
||||
integers() ->
|
||||
[ wrap_with_array(Test) || Test <- naked_integers() ]
|
||||
naked_integers()
|
||||
++ [ wrap_with_array(Test) || Test <- naked_integers() ]
|
||||
++ [ wrap_with_object(Test) || Test <- naked_integers() ]
|
||||
++ [listify("naked integers", naked_integers())]
|
||||
++ [objectify("naked integers", naked_integers())].
|
||||
|
@ -84,12 +122,34 @@ naked_floats() ->
|
|||
].
|
||||
|
||||
floats() ->
|
||||
[ wrap_with_array(Test) || Test <- naked_floats() ]
|
||||
naked_floats()
|
||||
++ [ wrap_with_array(Test) || Test <- naked_floats() ]
|
||||
++ [ wrap_with_object(Test) || Test <- naked_floats() ]
|
||||
++ [listify("naked floats", naked_floats())]
|
||||
++ [objectify("naked floats", naked_floats())].
|
||||
|
||||
|
||||
naked_literals() ->
|
||||
[
|
||||
{
|
||||
atom_to_list(Literal),
|
||||
atom_to_binary(Literal, unicode),
|
||||
Literal,
|
||||
[{literal, Literal}]
|
||||
}
|
||||
|| Literal <- [true, false, null]
|
||||
].
|
||||
|
||||
literals() ->
|
||||
naked_literals()
|
||||
++ [ wrap_with_array(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}]},
|
||||
|
@ -106,29 +166,6 @@ decodeables() ->
|
|||
++ [objectify("naked decodeables", Tests)].
|
||||
|
||||
|
||||
sane_float_to_list(X) ->
|
||||
[Output] = io_lib:format("~p", [X]),
|
||||
Output.
|
||||
|
||||
|
||||
naked_literals() ->
|
||||
[
|
||||
{
|
||||
atom_to_list(Literal),
|
||||
atom_to_binary(Literal, unicode),
|
||||
Literal,
|
||||
[{literal, Literal}]
|
||||
}
|
||||
|| Literal <- [true, false, null]
|
||||
].
|
||||
|
||||
literals() ->
|
||||
[ wrap_with_array(Test) || Test <- naked_literals() ]
|
||||
++ [ wrap_with_object(Test) || Test <- naked_literals() ]
|
||||
++ [listify("naked literals", naked_literals())]
|
||||
++ [objectify("naked literals", naked_literals())].
|
||||
|
||||
|
||||
wrap_with_array({Title, JSON, Term, Events}) ->
|
||||
{
|
||||
"[" ++ Title ++ "]",
|
||||
|
@ -151,6 +188,11 @@ repeat(_, Test, 0) -> Test;
|
|||
repeat(Fun, Test, Times) -> repeat(Fun, Fun(Test), Times - 1).
|
||||
|
||||
|
||||
sane_float_to_list(X) ->
|
||||
[Output] = io_lib:format("~p", [X]),
|
||||
Output.
|
||||
|
||||
|
||||
listify(Title, [{_, JSON, Term, Events}|Rest]) -> do_listify(Rest, {Title, JSON, [Term], Events}).
|
||||
|
||||
do_listify([], {Title, JSON, Term, Events}) ->
|
||||
|
|
|
@ -292,16 +292,7 @@ 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(),
|
||||
Data = jsx:universals(),
|
||||
[
|
||||
{
|
||||
Title, ?_assertEqual(
|
||||
|
|
|
@ -267,16 +267,7 @@ 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()
|
||||
Data = jsx:universals()
|
||||
++ jsx:decodeables(),
|
||||
[
|
||||
{
|
||||
|
|
|
@ -115,16 +115,7 @@ opts_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()
|
||||
Data = jsx:universals()
|
||||
++ jsx:decodeables(),
|
||||
[
|
||||
{
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue