rewrote test harness to use eunit and make it easier to add future tests

This commit is contained in:
alisdair sullivan 2010-05-23 22:40:35 -07:00
parent f5f1f588d4
commit ab67abd01a
37 changed files with 121 additions and 81 deletions

View file

@ -1,17 +1,26 @@
-module(jsx).
-export([decoder/0, decoder/1]).
-export([decoder/0, decoder/2]).
-include("jsx_common.hrl").
decoder() ->
decoder([]).
decoder(none, []).
decoder(OptsList) ->
OptsRec = parse_opts(OptsList),
case OptsRec#opts.encoding of
decoder(Callbacks, OptsList) ->
Opts = parse_opts(OptsList),
case Opts#opts.encoding of
utf8 ->
fun(Stream) -> jsx_utf8:start(Stream, [], [], OptsRec) end
fun(Stream) -> jsx_utf8:start(Stream, [], init_callbacks(Callbacks), Opts) end
; utf16-big ->
fun(Stream) -> jsx_utf16b:start(Stream, [], init_callbacks(Callbacks), Opts) end
; utf16-little ->
fun(Stream) -> jsx_utf16l:start(Stream, [], init_callbacks(Callbacks), Opts) end
; utf32-big ->
fun(Stream) -> jsx_utf32b:start(Stream, [], init_callbacks(Callbacks), Opts) end
; utf32-little ->
fun(Stream) -> jsx_utf32l:start(Stream, [], init_callbacks(Callbacks), Opts) end
;
end.
@ -30,5 +39,13 @@ parse_opts([{naked_values, Value}|Rest], Opts) ->
true = lists:member(Value, [true, false]),
parse_opts(Rest, Opts#opts{naked_values = Value});
parse_opts([{encoding, Value}|Rest], Opts) ->
true = lists:member(Value, [utf8]),
parse_opts(Rest, Opts#opts{encoding = Value}).
true = lists:member(Value, [auto, utf8, utf16-big, utf16-little, utf32-big, utf32-little]),
parse_opts(Rest, Opts#opts{encoding = Value}).
init_callbacks(none) ->
{none, []};
init_callbacks({M, S}) when is_atom(M) ->
{M, S};
init_callbacks({F, S}) when is_function(F) ->
{F, S}.

View file

@ -3,21 +3,15 @@
-export([start/4]).
-include("jsx_common.hrl").
callback(eof, Callbacks) ->
lists:reverse(Callbacks);
callback(Event, Callbacks) ->
[Event] ++ Callbacks.
%% this code is mostly autogenerated and mostly ugly. apologies. for more insight on
%% Callbacks or Opts, see the comments accompanying callback/2 (in this file) and
%% parse_opts/1 (in jsx.erl). Stack is a stack of flags used to track depth and to
%% keep track of whether we are returning from a value or a key inside objects. all
%% pops, peeks and pushes are inlined. the code that handles naked values and comments
%% is not optimized by the compiler for efficient matching, but you shouldn't be using
%% naked values or comments anyways, they are horrible and contrary to the spec.
%% Callbacks or Opts, see the comments accompanying decoder/2 (in jsx.erl). Stack
%% is a stack of flags used to track depth and to keep track of whether we are
%% returning from a value or a key inside objects. all pops, peeks and pushes are
%% inlined. the code that handles naked values and comments is not optimized by the
%% compiler for efficient matching, but you shouldn't be using naked values or comments
%% anyways, they are horrible and contrary to the spec.
start(<<?start_object/utf8, Rest/binary>>, Stack, Callbacks, Opts) ->
object(Rest, [key|Stack], callback(start_object, Callbacks), Opts);
@ -204,7 +198,7 @@ escaped_unicode(<<D/utf8, Rest/binary>>, Stack, Callbacks, Opts, String, [C, B,
string(Rest, Stack, Callbacks, Opts, [X] ++ String)
; codepoint ->
string(Rest, Stack, Callbacks, Opts, [X] ++ String)
; none ->
; _ ->
string(Rest, Stack, Callbacks, Opts, [?rsolidus, $u, A, B, C, D] ++ String)
end;
escaped_unicode(<<S/utf8, Rest/binary>>, Stack, Callbacks, Opts, String, Acc) when ?is_hex(S) ->
@ -422,6 +416,18 @@ maybe_comment_done(<<?solidus/utf8, Rest/binary>>, Resume) ->
Resume(Rest);
maybe_comment_done(<<>>, Resume) ->
fun(Stream) -> maybe_comment_done(Stream, Resume) end.
%% helper function for dispatching of parser events
callback(eof, {none, Callbacks}) ->
lists:reverse(Callbacks);
callback(Event, {none, Callbacks}) ->
{none, [Event] ++ Callbacks};
callback(Event, {Mod, State}) when is_atom(Mod) ->
{Mod, Mod:jsx_event(Event, State)};
callback(Event, {F, State}) when is_function(F) ->
{F, F(Event, State)}.