2010-05-25 21:26:55 -07:00
|
|
|
%% The MIT License
|
|
|
|
|
2010-05-25 21:48:36 -07:00
|
|
|
%% Copyright (c) 2010 Alisdair Sullivan <alisdairsullivan@yahoo.ca>
|
2010-05-25 21:26:55 -07:00
|
|
|
|
|
|
|
%% Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
%% of this software and associated documentation files (the "Software"), to deal
|
|
|
|
%% in the Software without restriction, including without limitation the rights
|
|
|
|
%% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
%% copies of the Software, and to permit persons to whom the Software is
|
|
|
|
%% furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
%% The above copyright notice and this permission notice shall be included in
|
|
|
|
%% all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
%% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
%% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
%% THE SOFTWARE.
|
|
|
|
|
|
|
|
|
2010-05-18 14:07:16 -07:00
|
|
|
-module(jsx_test).
|
2010-05-25 22:23:06 -07:00
|
|
|
-author("alisdairsullivan@yahoo.ca").
|
2010-05-18 14:07:16 -07:00
|
|
|
|
2010-06-01 23:33:56 -07:00
|
|
|
-export([test/0, test/1, test_event/2]).
|
2010-05-18 17:22:31 -07:00
|
|
|
|
2010-06-01 23:33:56 -07:00
|
|
|
test() ->
|
|
|
|
F = decoder([]),
|
|
|
|
incremental_decode(F, unicode:characters_to_binary(<<"0">>, utf8, utf16)).
|
2010-05-18 17:22:31 -07:00
|
|
|
|
2010-05-23 22:40:35 -07:00
|
|
|
test(Dir) ->
|
2010-06-01 23:33:56 -07:00
|
|
|
ValidJSONTests = load_tests(Dir),
|
|
|
|
|
|
|
|
etap:plan(length(ValidJSONTests) * 10),
|
|
|
|
run_tests(ValidJSONTests),
|
|
|
|
etap:end_tests().
|
2010-05-23 22:40:35 -07:00
|
|
|
|
2010-06-01 19:43:21 -07:00
|
|
|
|
|
|
|
decoder(Flags) ->
|
|
|
|
jsx:decoder({jsx_test, test_event, []}, Flags).
|
|
|
|
|
|
|
|
test_event(end_of_stream, Acc) ->
|
|
|
|
lists:reverse(Acc);
|
|
|
|
test_event(Event, Acc) ->
|
|
|
|
[Event] ++ Acc.
|
|
|
|
|
|
|
|
|
2010-06-01 23:33:56 -07:00
|
|
|
load_tests(Dir) ->
|
2010-05-23 22:40:35 -07:00
|
|
|
TestSpecs = filelib:wildcard("*.test", Dir),
|
2010-06-01 23:33:56 -07:00
|
|
|
load_tests(TestSpecs, Dir, []).
|
2010-05-23 22:40:35 -07:00
|
|
|
|
2010-06-01 23:33:56 -07:00
|
|
|
load_tests([], _Dir, Acc) ->
|
|
|
|
lists:reverse(Acc);
|
|
|
|
load_tests([Test|Rest], Dir, Acc) ->
|
2010-05-23 22:40:35 -07:00
|
|
|
try
|
2010-06-01 23:33:56 -07:00
|
|
|
TestName = filename:basename(Test, ".test"),
|
2010-05-23 22:40:35 -07:00
|
|
|
{ok, JSON} = file:read_file(Dir ++ "/" ++ TestName ++ ".json"),
|
2010-06-01 23:33:56 -07:00
|
|
|
case file:consult(Dir ++ "/" ++ Test) of
|
2010-05-23 22:40:35 -07:00
|
|
|
{ok, [Events]} ->
|
2010-06-01 23:33:56 -07:00
|
|
|
load_tests(Rest, Dir, [{TestName, JSON, Events, []}] ++ Acc)
|
2010-05-23 22:40:35 -07:00
|
|
|
; {ok, [Events, Flags]} ->
|
2010-06-01 23:33:56 -07:00
|
|
|
load_tests(Rest, Dir, [{TestName, JSON, Events, Flags}] ++ Acc)
|
2010-05-23 22:40:35 -07:00
|
|
|
end
|
2010-06-01 23:33:56 -07:00
|
|
|
catch _:_ -> load_tests(Rest, Dir, Acc) end.
|
|
|
|
|
|
|
|
run_tests([]) ->
|
|
|
|
ok;
|
|
|
|
run_tests([{TestName, JSON, Events, Flags}|Rest]) ->
|
|
|
|
F = decoder(Flags),
|
|
|
|
etap:is(decode(F, JSON), Events, TestName ++ ": utf8"),
|
|
|
|
etap:is(incremental_decode(F, JSON), Events, TestName ++ ": incremental utf8"),
|
|
|
|
etap:is(decode(F, to_utf16(JSON)), Events, TestName ++ ": utf16"),
|
|
|
|
etap:is(incremental_decode(F, to_utf16(JSON)), Events, TestName ++ ": incremental utf16"),
|
|
|
|
etap:is(decode(F, to_utf16le(JSON)), Events, TestName ++ ": utf16le"),
|
|
|
|
etap:is(incremental_decode(F, to_utf16le(JSON)), Events, TestName ++ ": incremental utf16le"),
|
|
|
|
etap:is(decode(F, to_utf32(JSON)), Events, TestName ++ ": utf32"),
|
|
|
|
etap:is(incremental_decode(F, to_utf32(JSON)), Events, TestName ++ ": incremental utf32"),
|
|
|
|
etap:is(decode(F, to_utf32le(JSON)), Events, TestName ++ ": utf32le"),
|
|
|
|
etap:is(incremental_decode(F, to_utf32le(JSON)), Events, TestName ++ ": incremental utf32le"),
|
|
|
|
run_tests(Rest).
|
|
|
|
|
2010-05-24 15:47:29 -07:00
|
|
|
|
2010-05-25 21:04:11 -07:00
|
|
|
incremental_decode(F, <<>>) ->
|
2010-05-29 17:50:27 -07:00
|
|
|
case F(<<>>) of
|
|
|
|
{incomplete, G} -> G
|
|
|
|
; {Result, _} -> Result
|
|
|
|
end;
|
2010-06-01 19:43:21 -07:00
|
|
|
incremental_decode(F, <<A, Rest/binary>>) ->
|
2010-05-29 17:50:27 -07:00
|
|
|
{_, G} = F(<<A>>),
|
|
|
|
incremental_decode(G, Rest).
|
2010-05-25 21:04:11 -07:00
|
|
|
|
|
|
|
decode(F, JSON) ->
|
|
|
|
case F(JSON) of
|
2010-05-29 17:50:27 -07:00
|
|
|
{incomplete, G} when is_function(G) ->
|
|
|
|
throw(badjson)
|
|
|
|
; {Result, _} ->
|
2010-05-25 21:04:11 -07:00
|
|
|
Result
|
|
|
|
end.
|
2010-06-01 23:33:56 -07:00
|
|
|
|
|
|
|
to_utf16(Bin) -> unicode:characters_to_binary(Bin, utf8, utf16).
|
|
|
|
to_utf16le(Bin) -> unicode:characters_to_binary(Bin, utf8, {utf16,little}).
|
|
|
|
to_utf32(Bin) -> unicode:characters_to_binary(Bin, utf8, utf32).
|
|
|
|
to_utf32le(Bin) -> unicode:characters_to_binary(Bin, utf8, {utf32,little}).
|
|
|
|
|
2010-05-24 15:47:29 -07:00
|
|
|
|