change `get_key/1' functions to return bare value or throw error

add `start_term' and `start_json' functions
This commit is contained in:
alisdair sullivan 2014-01-12 20:36:19 +00:00
parent 0c7517468c
commit 00469ba9c6
2 changed files with 40 additions and 14 deletions

View file

@ -25,6 +25,7 @@
-export([to_json/2, format/2]). -export([to_json/2, format/2]).
-export([init/1, handle_event/2]). -export([init/1, handle_event/2]).
-export([start_json/0, start_json/1]).
-export([start_object/1, start_array/1, finish/1, insert/2, insert/3, get_key/1]). -export([start_object/1, start_array/1, finish/1, insert/2, insert/3, get_key/1]).
@ -150,6 +151,10 @@ indent_or_space(Config) ->
%% `{array, Array}` %% `{array, Array}`
%% `Object` and `Array` are utf8 encoded binaries %% `Object` and `Array` are utf8 encoded binaries
start_json() -> {[], #config{}}.
start_json(Config) when is_list(Config) -> {[], parse_config(Config)}.
%% allocate a new object on top of the stack %% allocate a new object on top of the stack
start_object({Stack, Config}) -> {[{object, ?start_object}] ++ Stack, Config}. start_object({Stack, Config}) -> {[{object, ?start_object}] ++ Stack, Config}.
@ -234,8 +239,8 @@ insert(Key, Value, {[{object, Object}|Rest], Config}) when is_binary(Key), is_bi
insert(_, _, _) -> erlang:error(badarg). insert(_, _, _) -> erlang:error(badarg).
get_key({[{object, Key, _}|_], _}) -> {ok, Key}; get_key({[{object, Key, _}|_], _}) -> Key;
get_key(_) -> {error, nokey}. get_key(_) -> erlang:error(badarg).
@ -360,6 +365,14 @@ format_test_() ->
rep_manipulation_test_() -> rep_manipulation_test_() ->
[ [
{"allocate a new context", ?_assertEqual(
{[], #config{}},
start_json()
)},
{"allocate a new context with config", ?_assertEqual(
{[], #config{space=1, indent=2}},
start_json([{space, 1}, {indent, 2}])
)},
{"allocate a new object on an empty stack", ?_assertEqual( {"allocate a new object on an empty stack", ?_assertEqual(
{[{object, <<"{">>}], #config{}}, {[{object, <<"{">>}], #config{}},
start_object({[], #config{}}) start_object({[], #config{}})
@ -381,15 +394,15 @@ rep_manipulation_test_() ->
insert(<<"\"key\"">>, {[{object, <<"{">>}], #config{}}) insert(<<"\"key\"">>, {[{object, <<"{">>}], #config{}})
)}, )},
{"get current key", ?_assertEqual( {"get current key", ?_assertEqual(
{ok, key}, key,
get_key({[{object, key, <<"{">>}], #config{}}) get_key({[{object, key, <<"{">>}], #config{}})
)}, )},
{"try to get non-key from object", ?_assertEqual( {"try to get non-key from object", ?_assertError(
{error, nokey}, badarg,
get_key({[{object, <<"{">>}], #config{}}) get_key({[{object, <<"{">>}], #config{}})
)}, )},
{"try to get key from array", ?_assertEqual( {"try to get key from array", ?_assertError(
{error, nokey}, badarg,
get_key({[{array, <<"[">>}], #config{}}) get_key({[{array, <<"[">>}], #config{}})
)}, )},
{"insert a value into an object", ?_assertEqual( {"insert a value into an object", ?_assertEqual(

View file

@ -25,6 +25,7 @@
-export([to_term/2]). -export([to_term/2]).
-export([init/1, handle_event/2]). -export([init/1, handle_event/2]).
-export([start_term/0, start_term/1]).
-export([start_object/1, start_array/1, finish/1, insert/2, insert/3, get_key/1]). -export([start_object/1, start_array/1, finish/1, insert/2, insert/3, get_key/1]).
@ -117,6 +118,10 @@ format_key(Key, Config) ->
%% an array looks like %% an array looks like
%% `{array, [NthValue, NthMinus1Value,...FirstValue]}` %% `{array, [NthValue, NthMinus1Value,...FirstValue]}`
start_term() -> {[], #config{}}.
start_term(Config) when is_list(Config) -> {[], parse_config(Config)}.
%% allocate a new object on top of the stack %% allocate a new object on top of the stack
start_object({Stack, Config}) -> {[{object, []}] ++ Stack, Config}. start_object({Stack, Config}) -> {[{object, []}] ++ Stack, Config}.
@ -149,8 +154,8 @@ insert(Key, Value, {[{object, Pairs}|Rest], Config}) ->
insert(_, _, _) -> erlang:error(badarg). insert(_, _, _) -> erlang:error(badarg).
get_key({[{object, Key, _}|_], _}) -> {ok, Key}; get_key({[{object, Key, _}|_], _}) -> Key;
get_key(_) -> {error, nokey}. get_key(_) -> erlang:error(badarg).
@ -200,6 +205,14 @@ format_key_test_() ->
rep_manipulation_test_() -> rep_manipulation_test_() ->
[ [
{"allocate a new context", ?_assertEqual(
{[], #config{}},
start_term()
)},
{"allocate a new context with option", ?_assertEqual(
{[], #config{labels=atom}},
start_term([{labels, atom}])
)},
{"allocate a new object on an empty stack", ?_assertEqual( {"allocate a new object on an empty stack", ?_assertEqual(
{[{object, []}], #config{}}, {[{object, []}], #config{}},
start_object({[], #config{}}) start_object({[], #config{}})
@ -221,15 +234,15 @@ rep_manipulation_test_() ->
insert(key, {[{object, []}, junk], #config{}}) insert(key, {[{object, []}, junk], #config{}})
)}, )},
{"get current key", ?_assertEqual( {"get current key", ?_assertEqual(
{ok, key}, key,
get_key({[{object, key, []}], #config{}}) get_key({[{object, key, []}], #config{}})
)}, )},
{"try to get non-key from object", ?_assertEqual( {"try to get non-key from object", ?_assertError(
{error, nokey}, badarg,
get_key({[{object, []}], #config{}}) get_key({[{object, []}], #config{}})
)}, )},
{"try to get key from array", ?_assertEqual( {"try to get key from array", ?_assertError(
{error, nokey}, badarg,
get_key({[{array, []}], #config{}}) get_key({[{array, []}], #config{}})
)}, )},
{"insert a value into an object", ?_assertEqual( {"insert a value into an object", ?_assertEqual(