adds test for {labels, atom} option

This commit is contained in:
alisdair sullivan 2011-11-28 15:24:44 -08:00
parent adc453b226
commit c9b590735e

View file

@ -92,7 +92,7 @@ handle_event(end_array, {[List, {key, Key}, Last|Terms], Opts}) ->
handle_event(end_array, {[Current, Last|Terms], Opts}) ->
{[[lists:reverse(Current)] ++ Last] ++ Terms, Opts};
handle_event({key, Key}, {Terms, Opts}) -> {[{key, Key}] ++ Terms, Opts};
handle_event({key, Key}, {Terms, Opts}) -> {[{key, format_key(Key, Opts)}] ++ Terms, Opts};
handle_event({_, Event}, {[{key, Key}, Last|Terms], Opts}) ->
{[[{Key, Event}] ++ Last] ++ Terms, Opts};
@ -101,6 +101,15 @@ handle_event({_, Event}, {[Last|Terms], Opts}) ->
format_key(Key, Opts) ->
case Opts#opts.labels of
binary -> Key
; atom -> binary_to_atom(Key, utf8)
; existing_atom -> binary_to_existing_atom(Key, utf8)
end.
%% eunit tests
-ifdef(TEST).
@ -162,4 +171,19 @@ comp_term() ->
[{<<"key">>, []}, {<<"another key">>, [{}]}]
].
atom_labels_test_() ->
{"atom labels test", ?_assert(to_term(comp_json(), [{labels, atom}]) =:= atom_term())}.
atom_term() ->
[
[{'a key', [{'a key', -17.346}, {'another key', 3.0e152}, {'last key', 14}]}],
[0,1,2,3,4,5],
[[{a, <<"a">>}, {b, <<"b">>}], [{'c', <<"c">>}, {'d', <<"d">>}]],
[true, false, null],
[{}],
[],
[[{}], [{}]],
[{key, []}, {'another key', [{}]}]
].
-endif.