add whitespace formatting to rep manipulation functions
This commit is contained in:
parent
7b9170b32d
commit
f58ac5566e
1 changed files with 77 additions and 31 deletions
|
@ -210,20 +210,63 @@ insert(Value, {[], Config}) when is_binary(Value) ->
|
||||||
insert(Key, {[{object, Object}|Rest], Config}) when is_binary(Key) ->
|
insert(Key, {[{object, Object}|Rest], Config}) when is_binary(Key) ->
|
||||||
{[{object, Key, Object}] ++ Rest, Config};
|
{[{object, Key, Object}] ++ Rest, Config};
|
||||||
insert(Value, {[{object, Key, ?start_object}|Rest], Config}) when is_binary(Value) ->
|
insert(Value, {[{object, Key, ?start_object}|Rest], Config}) when is_binary(Value) ->
|
||||||
{[{object, <<?start_object/binary, Key/binary, ?colon/binary, Value/binary>>}] ++ Rest, Config};
|
{
|
||||||
|
[{object, <<?start_object/binary,
|
||||||
|
Key/binary,
|
||||||
|
?colon/binary,
|
||||||
|
(space(Config))/binary,
|
||||||
|
Value/binary
|
||||||
|
>>}] ++ Rest,
|
||||||
|
Config
|
||||||
|
};
|
||||||
insert(Value, {[{object, Key, Object}|Rest], Config}) when is_binary(Value) ->
|
insert(Value, {[{object, Key, Object}|Rest], Config}) when is_binary(Value) ->
|
||||||
{[{object, <<Object/binary, ?comma/binary, Key/binary, ?colon/binary, Value/binary>>}] ++ Rest, Config};
|
{
|
||||||
|
[{object, <<Object/binary,
|
||||||
|
?comma/binary,
|
||||||
|
(indent_or_space(Config))/binary,
|
||||||
|
Key/binary,
|
||||||
|
?colon/binary,
|
||||||
|
(space(Config))/binary,
|
||||||
|
Value/binary
|
||||||
|
>>}] ++ Rest,
|
||||||
|
Config
|
||||||
|
};
|
||||||
insert(Value, {[{array, ?start_array}|Rest], Config}) when is_binary(Value) ->
|
insert(Value, {[{array, ?start_array}|Rest], Config}) when is_binary(Value) ->
|
||||||
{[{array, <<?start_array/binary, Value/binary>>}] ++ Rest, Config};
|
{[{array, <<?start_array/binary, Value/binary>>}] ++ Rest, Config};
|
||||||
insert(Value, {[{array, Array}|Rest], Config}) when is_binary(Value) ->
|
insert(Value, {[{array, Array}|Rest], Config}) when is_binary(Value) ->
|
||||||
{[{array, <<Array/binary, ?comma/binary, Value/binary>>}] ++ Rest, Config};
|
{
|
||||||
|
[{array, <<Array/binary,
|
||||||
|
?comma/binary,
|
||||||
|
(indent_or_space(Config))/binary,
|
||||||
|
Value/binary
|
||||||
|
>>}] ++ Rest,
|
||||||
|
Config
|
||||||
|
};
|
||||||
insert(_, _) -> erlang:error(badarg).
|
insert(_, _) -> erlang:error(badarg).
|
||||||
|
|
||||||
%% insert a key/value pair into an object
|
%% insert a key/value pair into an object
|
||||||
insert(Key, Value, {[{object, ?start_object}|Rest], Config}) when is_binary(Key), is_binary(Value) ->
|
insert(Key, Value, {[{object, ?start_object}|Rest], Config}) when is_binary(Key), is_binary(Value) ->
|
||||||
{[{object, <<?start_object/binary, Key/binary, ?colon/binary, Value/binary>>}] ++ Rest, Config};
|
{
|
||||||
|
[{object, <<?start_object/binary,
|
||||||
|
Key/binary,
|
||||||
|
?colon/binary,
|
||||||
|
(space(Config))/binary,
|
||||||
|
Value/binary
|
||||||
|
>>}] ++ Rest,
|
||||||
|
Config
|
||||||
|
};
|
||||||
insert(Key, Value, {[{object, Object}|Rest], Config}) when is_binary(Key), is_binary(Value) ->
|
insert(Key, Value, {[{object, Object}|Rest], Config}) when is_binary(Key), is_binary(Value) ->
|
||||||
{[{object, <<Object/binary, ?comma/binary, Key/binary, ?colon/binary, Value/binary>>}] ++ Rest, Config};
|
{
|
||||||
|
[{object, <<Object/binary,
|
||||||
|
?comma/binary,
|
||||||
|
(indent_or_space(Config))/binary,
|
||||||
|
Key/binary,
|
||||||
|
?colon/binary,
|
||||||
|
(space(Config))/binary,
|
||||||
|
Value/binary
|
||||||
|
>>}] ++ Rest,
|
||||||
|
Config
|
||||||
|
};
|
||||||
insert(_, _, _) -> erlang:error(badarg).
|
insert(_, _, _) -> erlang:error(badarg).
|
||||||
|
|
||||||
|
|
||||||
|
@ -342,56 +385,59 @@ format_test_() ->
|
||||||
rep_manipulation_test_() ->
|
rep_manipulation_test_() ->
|
||||||
[
|
[
|
||||||
{"allocate a new object on an empty stack", ?_assertEqual(
|
{"allocate a new object on an empty stack", ?_assertEqual(
|
||||||
{[{object, <<"{">>}], []},
|
{[{object, <<"{">>}], #config{}},
|
||||||
start_object({[], []})
|
start_object({[], #config{}})
|
||||||
)},
|
)},
|
||||||
{"allocate a new object on a stack", ?_assertEqual(
|
{"allocate a new object on a stack", ?_assertEqual(
|
||||||
{[{object, <<"{">>}, {object, <<"{">>}], []},
|
{[{object, <<"{">>}, {object, <<"{">>}], #config{}},
|
||||||
start_object({[{object, <<"{">>}], []})
|
start_object({[{object, <<"{">>}], #config{}})
|
||||||
)},
|
)},
|
||||||
{"allocate a new array on an empty stack", ?_assertEqual(
|
{"allocate a new array on an empty stack", ?_assertEqual(
|
||||||
{[{array, <<"[">>}], []},
|
{[{array, <<"[">>}], #config{}},
|
||||||
start_array({[], []})
|
start_array({[], #config{}})
|
||||||
)},
|
)},
|
||||||
{"allocate a new array on a stack", ?_assertEqual(
|
{"allocate a new array on a stack", ?_assertEqual(
|
||||||
{[{array, <<"[">>}, {object, <<"{">>}], []},
|
{[{array, <<"[">>}, {object, <<"{">>}], #config{}},
|
||||||
start_array({[{object, <<"{">>}], []})
|
start_array({[{object, <<"{">>}], #config{}})
|
||||||
)},
|
)},
|
||||||
{"insert a key into an object", ?_assertEqual(
|
{"insert a key into an object", ?_assertEqual(
|
||||||
{[{object, <<"\"key\"">>, <<"{">>}], []},
|
{[{object, <<"\"key\"">>, <<"{">>}], #config{}},
|
||||||
insert(<<"\"key\"">>, {[{object, <<"{">>}], []})
|
insert(<<"\"key\"">>, {[{object, <<"{">>}], #config{}})
|
||||||
)},
|
)},
|
||||||
{"insert a value into an object", ?_assertEqual(
|
{"insert a value into an object", ?_assertEqual(
|
||||||
{[{object, <<"{\"key\":true">>}], []},
|
{[{object, <<"{\"key\":true">>}], #config{}},
|
||||||
insert(<<"true">>, {[{object, <<"\"key\"">>, <<"{">>}], []})
|
insert(<<"true">>, {[{object, <<"\"key\"">>, <<"{">>}], #config{}})
|
||||||
)},
|
)},
|
||||||
{"insert a value into an array", ?_assertEqual(
|
{"insert a value into an array", ?_assertEqual(
|
||||||
{[{array, <<"[true">>}], []},
|
{[{array, <<"[true">>}], #config{}},
|
||||||
insert(<<"true">>, {[{array, <<"[">>}], []})
|
insert(<<"true">>, {[{array, <<"[">>}], #config{}})
|
||||||
)},
|
)},
|
||||||
{"insert a key/value pair into an object", ?_assertEqual(
|
{"insert a key/value pair into an object", ?_assertEqual(
|
||||||
{[{object, <<"{\"x\":true,\"y\":false">>}], []},
|
{[{object, <<"{\"x\":true,\"y\":false">>}], #config{}},
|
||||||
insert(<<"\"y\"">>, <<"false">>, {[{object, <<"{\"x\":true">>}], []})
|
insert(<<"\"y\"">>, <<"false">>, {[{object, <<"{\"x\":true">>}], #config{}})
|
||||||
)},
|
)},
|
||||||
{"finish an object with no ancestor", ?_assertEqual(
|
{"finish an object with no ancestor", ?_assertEqual(
|
||||||
{<<"{\"x\":true,\"y\":false}">>, []},
|
{<<"{\"x\":true,\"y\":false}">>, #config{}},
|
||||||
finish({[{object, <<"{\"x\":true,\"y\":false">>}], []})
|
finish({[{object, <<"{\"x\":true,\"y\":false">>}], #config{}})
|
||||||
)},
|
)},
|
||||||
{"finish an empty object", ?_assertEqual(
|
{"finish an empty object", ?_assertEqual(
|
||||||
{<<"{}">>, []},
|
{<<"{}">>, #config{}},
|
||||||
finish({[{object, <<"{">>}], []})
|
finish({[{object, <<"{">>}], #config{}})
|
||||||
)},
|
)},
|
||||||
{"finish an object with an ancestor", ?_assertEqual(
|
{"finish an object with an ancestor", ?_assertEqual(
|
||||||
{[{object, <<"{\"a\":[],\"b\":{\"x\":true,\"y\":false}">>}], []},
|
{[{object, <<"{\"a\":[],\"b\":{\"x\":true,\"y\":false}">>}], #config{}},
|
||||||
finish({[{object, <<"{\"x\":true,\"y\":false">>}, {object, <<"\"b\"">>, <<"{\"a\":[]">>}], []})
|
finish({
|
||||||
|
[{object, <<"{\"x\":true,\"y\":false">>}, {object, <<"\"b\"">>, <<"{\"a\":[]">>}],
|
||||||
|
#config{}
|
||||||
|
})
|
||||||
)},
|
)},
|
||||||
{"finish an array with no ancestor", ?_assertEqual(
|
{"finish an array with no ancestor", ?_assertEqual(
|
||||||
{<<"[true,false,null]">>, []},
|
{<<"[true,false,null]">>, #config{}},
|
||||||
finish({[{array, <<"[true,false,null">>}], []})
|
finish({[{array, <<"[true,false,null">>}], #config{}})
|
||||||
)},
|
)},
|
||||||
{"finish an array with an ancestor", ?_assertEqual(
|
{"finish an array with an ancestor", ?_assertEqual(
|
||||||
{[{array, <<"[1,2,3,[true,false,null]">>}], []},
|
{[{array, <<"[1,2,3,[true,false,null]">>}], #config{}},
|
||||||
finish({[{array, <<"[true,false,null">>}, {array, <<"[1,2,3">>}], []})
|
finish({[{array, <<"[true,false,null">>}, {array, <<"[1,2,3">>}], #config{}})
|
||||||
)}
|
)}
|
||||||
].
|
].
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue