remove integration tests from jsx module

This commit is contained in:
alisdair sullivan 2013-01-31 06:18:29 -08:00
parent b711bc9ef9
commit bd3a64f457

View file

@ -159,232 +159,11 @@ parser(Handler, State, Opts) -> jsx_parser:parser(Handler, State, Opts).
-include_lib("eunit/include/eunit.hrl").
jsx_decoder_test_() ->
jsx_decoder_gen(load_tests(code:lib_dir(jsx, priv) ++ "/test_cases/")).
encoder_decoder_equiv_test_() ->
[
{"encoder/decoder equivalency",
?_assert((jsx_decoder:decoder(?MODULE, [], []))(
<<"[\"a\", 17, 3.14, true, {\"k\":false}, []]">>
) =:= (jsx_encoder:encoder(?MODULE, [], []))([<<"a">>, 17, 3.14, true, [{<<"k">>, false}], []])
)
}
].
partial_numbers_test_() ->
[
{"partial integer",
?_assert(begin
{incomplete, F} = jsx:decode(<<"{\"integer\": 12345">>),
F(<<"67890}">>)
end =:= [{<<"integer">>, 1234567890}]
)
},
{"partial integer",
?_assert(begin
{incomplete, F} = jsx:decode(<<"{\"integer\": 1234567890">>),
F(<<"}">>)
end =:= [{<<"integer">>, 1234567890}]
)
},
{"partial float",
?_assert(begin
{incomplete, F} = jsx:decode(<<"{\"float\": 1.">>),
F(<<"23}">>)
end =:= [{<<"float">>, 1.23}]
)
},
{"partial float",
?_assert(begin
{incomplete, F} = jsx:decode(<<"{\"float\": 1.2">>),
F(<<"3}">>)
end =:= [{<<"float">>, 1.23}]
)
},
{"partial float",
?_assert(begin
{incomplete, F} = jsx:decode(<<"{\"float\": 1.23">>),
F(<<"}">>)
end =:= [{<<"float">>, 1.23}]
)
},
{"partial exp",
?_assert(begin
{incomplete, F} = jsx:decode(<<"{\"exp\": 1.0e">>),
F(<<"1}">>)
end =:= [{<<"exp">>, 10.0}]
)
},
{"partial exp",
?_assert(begin
{incomplete, F} = jsx:decode(<<"{\"exp\": 1.0e1">>),
F(<<"2}">>)
end =:= [{<<"exp">>, 1.0e12}]
)
},
{"partial exp",
?_assert(begin
{incomplete, F} = jsx:decode(<<"{\"exp\": 1.0e1">>),
F(<<"}">>)
end =:= [{<<"exp">>, 10.0}]
)
},
{"partial zero",
?_assert(begin
{incomplete, F} = jsx:decode(<<"{\"zero\": 0">>),
F(<<".0}">>)
end =:= [{<<"zero">>, 0.0}]
)
},
{"partial zero",
?_assert(begin
{incomplete, F} = jsx:decode(<<"{\"zero\": 0">>),
F(<<"}">>)
end =:= [{<<"zero">>, 0}]
)
}
].
single_quoted_strings_test_() ->
[
{"single quoted keys",
?_assertEqual(
to_term(<<"{'key':true}">>, [single_quoted_strings]),
[{<<"key">>, true}]
)
},
{"multiple single quoted keys",
?_assertEqual(
to_term(<<"{'key':true, 'another key':true}">>, [single_quoted_strings]),
[{<<"key">>, true}, {<<"another key">>, true}]
)
},
{"nested single quoted keys",
?_assertEqual(
to_term(<<"{'key': {'key':true, 'another key':true}}">>, [single_quoted_strings]),
[{<<"key">>, [{<<"key">>, true}, {<<"another key">>, true}]}]
)
},
{"single quoted string",
?_assertEqual(
to_term(<<"['string']">>, [single_quoted_strings]),
[<<"string">>]
)
},
{"single quote in double quoted string",
?_assertEqual(
to_term(<<"[\"a single quote: '\"]">>, [single_quoted_strings]),
[<<"a single quote: '">>]
)
},
{"escaped single quote in single quoted string",
?_assertEqual(
to_term(<<"['a single quote: \\'']">>, [single_quoted_strings]),
[<<"a single quote: '">>]
)
},
{"escaped single quote when single quotes are disallowed",
?_assertError(
badarg,
to_term(<<"[\"a single quote: \\'\"]">>)
)
},
{"mismatched quotes",
?_assertError(
badarg,
to_term(<<"['mismatched\"]">>, [single_quoted_strings])
)
}
].
%% test handler
%% stub test handler
init([]) -> [].
handle_event(end_json, State) -> lists:reverse([end_json] ++ State);
handle_event(Event, State) -> [Event] ++ State.
jsx_decoder_gen([]) -> [];
jsx_decoder_gen([Test|Rest]) ->
Name = proplists:get_value(name, Test),
JSON = proplists:get_value(json, Test),
JSX = proplists:get_value(jsx, Test),
Flags = proplists:get_value(jsx_flags, Test, []),
{generator, fun() ->
[{Name, ?_assertEqual(test_decode(JSON, Flags), JSX)},
{Name ++ " (incremental)",
?_assertEqual(incremental_decode(JSON, Flags), JSX)
}
| jsx_decoder_gen(Rest)
]
end}.
load_tests(Path) ->
%% search the specified directory for any files with the .test ending
TestSpecs = filelib:wildcard("*.test", Path),
load_tests(TestSpecs, Path, []).
load_tests([], _Dir, Acc) ->
lists:reverse(Acc);
load_tests([Test|Rest], Dir, Acc) ->
case file:consult(Dir ++ "/" ++ Test) of
{ok, TestSpec} ->
ParsedTest = parse_tests(TestSpec, Dir),
load_tests(Rest, Dir, [ParsedTest] ++ Acc)
; {error, _Reason} ->
erlang:error(badarg, [Test|Rest], Dir, Acc)
end.
parse_tests(TestSpec, Dir) ->
parse_tests(TestSpec, Dir, []).
parse_tests([{json, Path}|Rest], Dir, Acc) when is_list(Path) ->
case file:read_file(Dir ++ "/" ++ Path) of
{ok, Bin} -> parse_tests(Rest, Dir, [{json, Bin}] ++ Acc)
; _ -> erlang:error(badarg, [[{json, Path}|Rest], Dir, Acc])
end;
parse_tests([KV|Rest], Dir, Acc) ->
parse_tests(Rest, Dir, [KV] ++ Acc);
parse_tests([], _Dir, Acc) ->
Acc.
test_decode(JSON, Flags) ->
try
case (jsx_decoder:decoder(?MODULE, [], Flags))(JSON) of
{incomplete, More} ->
case More(<<" ">>) of
{incomplete, _} -> {error, badarg}
; Events -> Events
end
; Events -> Events
end
catch
error:badarg -> {error, badarg}
end.
incremental_decode(<<C:1/binary, Rest/binary>>, Flags) ->
P = jsx_decoder:decoder(?MODULE, [], Flags ++ [explicit_end]),
try incremental_decode_loop(P(C), Rest)
catch error:badarg -> {error, badarg}
end.
incremental_decode_loop({incomplete, More}, <<>>) ->
case More(end_stream) of
{incomplete, _} -> {error, badarg}
; X -> X
end;
incremental_decode_loop({incomplete, More}, <<C:1/binary, Rest/binary>>) ->
incremental_decode_loop(More(C), Rest).
-endif.