add tests for repeated keys in jsx_verify

This commit is contained in:
alisdair sullivan 2013-03-04 20:34:04 -08:00
parent e625c625e0
commit 8eb302b45f

View file

@ -52,29 +52,30 @@ is_term(Source, Config) when is_list(Config) ->
parse_config(Config) -> parse_config(Config, #config{}).
parse_config([no_repeated_keys|Rest], Config) ->
parse_config(Rest, Config#config{repeated_keys=false});
%% deprecated, use `no_repeated_keys`
parse_config([{repeated_keys, Val}|Rest], Config) when Val == true; Val == false ->
parse_config(Rest, Config#config{repeated_keys = Val});
parse_config(Rest, Config#config{repeated_keys=Val});
parse_config([repeated_keys|Rest], Config) ->
parse_config(Rest, Config#config{repeated_keys = true});
parse_config(Rest, Config#config{repeated_keys=true});
parse_config([{K, _}|Rest] = Options, Config) ->
case lists:member(K, jsx_utils:valid_flags()) of
true -> parse_config(Rest, Config)
; false -> erlang:error(badarg, [Options, Config])
true -> parse_config(Rest, Config);
false -> erlang:error(badarg, [Options, Config])
end;
parse_config([K|Rest] = Options, Config) ->
case lists:member(K, jsx_utils:valid_flags()) of
true -> parse_config(Rest, Config)
; false -> erlang:error(badarg, [Options, Config])
true -> parse_config(Rest, Config);
false -> erlang:error(badarg, [Options, Config])
end;
parse_config([], Config) ->
Config.
init(Config) -> {parse_config(Config), []}.
handle_event(end_json, _) -> true;
handle_event(_, {Config, _} = State) when Config#config.repeated_keys == true -> State;
@ -84,8 +85,8 @@ handle_event(end_object, {Config, [_|Keys]}) -> {Config, Keys};
handle_event({key, Key}, {Config, [CurrentKeys|Keys]}) ->
case dict:is_key(Key, CurrentKeys) of
true -> erlang:error(badarg)
; false -> {Config, [dict:store(Key, blah, CurrentKeys)|Keys]}
true -> erlang:error(badarg);
false -> {Config, [dict:store(Key, blah, CurrentKeys)|Keys]}
end;
handle_event(_, State) -> State.
@ -100,6 +101,7 @@ handle_event(_, State) -> State.
config_test_() ->
[
{"empty config", ?_assertEqual(#config{}, parse_config([]))},
{"no repeat keys", ?_assertEqual(#config{repeated_keys=false}, parse_config([no_repeated_keys]))},
{"bare repeated keys", ?_assertEqual(#config{}, parse_config([repeated_keys]))},
{"repeated keys true", ?_assertEqual(
#config{},
@ -114,6 +116,43 @@ config_test_() ->
].
repeated_keys_test_() ->
RepeatedKey = [
start_object,
{key, <<"alpha">>},
{literal, true},
{key, <<"alpha">>},
{literal, false},
end_object,
end_json
],
NestedKey = [
start_object,
{key, <<"alpha">>},
start_object,
{key, <<"alpha">>},
start_object,
{key, <<"alpha">>},
{literal, true},
end_object,
end_object,
end_object,
end_json
],
[
{"repeated key", ?_assert(
lists:foldl(fun handle_event/2, {#config{}, []}, RepeatedKey)
)},
{"no repeated key", ?_assertError(
badarg,
lists:foldl(fun handle_event/2, {#config{repeated_keys=false}, []}, RepeatedKey)
)},
{"nested key", ?_assert(
lists:foldl(fun handle_event/2, {#config{repeated_keys=false}, []}, NestedKey)
)}
].
handle_event_test_() ->
Data = jsx:test_cases(),
[