remove integration tests and add unit tests to jsx_to_json.erl
This commit is contained in:
parent
81f0321e32
commit
822a668f8e
1 changed files with 14 additions and 124 deletions
|
@ -184,138 +184,28 @@ indent_or_space(Opts) ->
|
||||||
-ifdef(TEST).
|
-ifdef(TEST).
|
||||||
-include_lib("eunit/include/eunit.hrl").
|
-include_lib("eunit/include/eunit.hrl").
|
||||||
|
|
||||||
basic_format_test_() ->
|
|
||||||
[
|
|
||||||
{"empty object", ?_assertEqual(format(<<"{}">>, []), <<"{}">>)},
|
|
||||||
{"empty array", ?_assertEqual(format(<<"[]">>, []), <<"[]">>)},
|
|
||||||
{"naked integer", ?_assertEqual(format(<<"123">>, []), <<"123">>)},
|
|
||||||
{"naked float", ?_assertEqual(format(<<"1.23">>, []), <<"1.23">>)},
|
|
||||||
{"naked string", ?_assertEqual(format(<<"\"hi\"">>, []), <<"\"hi\"">>)},
|
|
||||||
{"naked string with control character", ?_assertEqual(
|
|
||||||
format(<<"\"hi\\n\"">>, []), <<"\"hi\\n\"">>
|
|
||||||
)},
|
|
||||||
{"naked literal", ?_assertEqual(format(<<"true">>, []), <<"true">>)},
|
|
||||||
{"simple object", ?_assertEqual(
|
|
||||||
format(<<" { \"key\" :\n\t \"value\"\r\r\r\n } ">>, []),
|
|
||||||
<<"{\"key\":\"value\"}">>
|
|
||||||
)},
|
|
||||||
{"really simple object", ?_assertEqual(format(<<"{\"k\":\"v\"}">>, []) , <<"{\"k\":\"v\"}">>)},
|
|
||||||
{"nested object", ?_assertEqual(
|
|
||||||
format(<<"{\"k\":{\"k\":\"v\"}, \"j\":{}}">>, []),
|
|
||||||
<<"{\"k\":{\"k\":\"v\"},\"j\":{}}">>
|
|
||||||
)},
|
|
||||||
{"simple array", ?_assertEqual(
|
|
||||||
format(<<" [\n\ttrue,\n\tfalse , \n \tnull\n] ">>, []),
|
|
||||||
<<"[true,false,null]">>
|
|
||||||
)},
|
|
||||||
{"really simple array", ?_assertEqual(format(<<"[1]">>, []), <<"[1]">>)},
|
|
||||||
{"nested array", ?_assertEqual(format(<<"[[[]]]">>, []), <<"[[[]]]">>)},
|
|
||||||
{"nested structures", ?_assertEqual(
|
|
||||||
format(<<"[
|
|
||||||
{
|
|
||||||
\"key\":\"value\",
|
|
||||||
\"another key\": \"another value\",
|
|
||||||
\"a list\": [true, false]
|
|
||||||
},
|
|
||||||
[[{}]]
|
|
||||||
]">>, []),
|
|
||||||
<<"[{\"key\":\"value\",\"another key\":\"another value\",\"a list\":[true,false]},[[{}]]]">>
|
|
||||||
)},
|
|
||||||
{"simple nested structure",
|
|
||||||
?_assertEqual(
|
|
||||||
format(<<"[[],{\"k\":[[],{}],\"j\":{}},[]]">>, []),
|
|
||||||
<<"[[],{\"k\":[[],{}],\"j\":{}},[]]">>
|
|
||||||
)
|
|
||||||
}
|
|
||||||
].
|
|
||||||
|
|
||||||
basic_to_json_test_() ->
|
|
||||||
[
|
|
||||||
{"empty object", ?_assertEqual(to_json([{}], []), <<"{}">>)},
|
|
||||||
{"empty array", ?_assertEqual(to_json([], []), <<"[]">>)},
|
|
||||||
{"naked integer", ?_assertEqual(to_json(123, []), <<"123">>)},
|
|
||||||
{"naked float", ?_assertEqual(to_json(1.23, []) , <<"1.23">>)},
|
|
||||||
{"naked string", ?_assertEqual(to_json(<<"hi">>, []), <<"\"hi\"">>)},
|
|
||||||
{"naked string with control character", ?_assertEqual(
|
|
||||||
to_json(<<"hi\n">>, []), <<"\"hi\\n\"">>
|
|
||||||
)},
|
|
||||||
{"naked literal", ?_assertEqual(to_json(true, []), <<"true">>)},
|
|
||||||
{"simple object", ?_assertEqual(
|
|
||||||
to_json(
|
|
||||||
[{<<"key">>, <<"value">>}],
|
|
||||||
[]
|
|
||||||
),
|
|
||||||
<<"{\"key\":\"value\"}">>
|
|
||||||
)},
|
|
||||||
{"nested object", ?_assertEqual(
|
|
||||||
to_json(
|
|
||||||
[{<<"k">>,[{<<"k">>,<<"v">>}]},{<<"j">>,[{}]}],
|
|
||||||
[]
|
|
||||||
),
|
|
||||||
<<"{\"k\":{\"k\":\"v\"},\"j\":{}}">>
|
|
||||||
)},
|
|
||||||
{"simple array", ?_assertEqual(to_json([true, false, null], []), <<"[true,false,null]">>)},
|
|
||||||
{"really simple array", ?_assertEqual(to_json([1], []), <<"[1]">>)},
|
|
||||||
{"nested array", ?_assertEqual(to_json([[[]]], []), <<"[[[]]]">>)},
|
|
||||||
{"nested structures", ?_assertEqual(
|
|
||||||
to_json(
|
|
||||||
[
|
|
||||||
[
|
|
||||||
{<<"key">>, <<"value">>},
|
|
||||||
{<<"another key">>, <<"another value">>},
|
|
||||||
{<<"a list">>, [true, false]}
|
|
||||||
],
|
|
||||||
[[[{}]]]
|
|
||||||
],
|
|
||||||
[]
|
|
||||||
),
|
|
||||||
<<"[{\"key\":\"value\",\"another key\":\"another value\",\"a list\":[true,false]},[[{}]]]">>
|
|
||||||
)},
|
|
||||||
{"simple nested structure", ?_assertEqual(
|
|
||||||
to_json(
|
|
||||||
[[], [{<<"k">>, [[], [{}]]}, {<<"j">>, [{}]}], []],
|
|
||||||
[]
|
|
||||||
),
|
|
||||||
<<"[[],{\"k\":[[],{}],\"j\":{}},[]]">>
|
|
||||||
)}
|
|
||||||
].
|
|
||||||
|
|
||||||
opts_test_() ->
|
opts_test_() ->
|
||||||
[
|
[
|
||||||
|
{"empty opts", ?_assertEqual(#opts{}, parse_opts([]))},
|
||||||
{"unspecified indent/space", ?_assertEqual(
|
{"unspecified indent/space", ?_assertEqual(
|
||||||
format(<<" [\n\ttrue,\n\tfalse,\n\tnull\n] ">>, [space, indent]),
|
#opts{space=1, indent=1},
|
||||||
<<"[\n true,\n false,\n null\n]">>
|
parse_opts([space, indent])
|
||||||
)},
|
)},
|
||||||
{"specific indent/space", ?_assertEqual(
|
{"specific indent", ?_assertEqual(
|
||||||
format(
|
#opts{indent=4},
|
||||||
<<"\n{\n\"key\" : [],\n\"another key\" : true\n}\n">>,
|
parse_opts([{indent, 4}])
|
||||||
[{space, 2}, {indent, 3}]
|
|
||||||
),
|
|
||||||
<<"{\n \"key\": [],\n \"another key\": true\n}">>
|
|
||||||
)},
|
)},
|
||||||
{"nested structures", ?_assertEqual(
|
{"specific space", ?_assertEqual(
|
||||||
format(
|
#opts{space=2},
|
||||||
<<"[{\"key\":\"value\", \"another key\": \"another value\"}, [[true, false, null]]]">>,
|
parse_opts([{space, 2}])
|
||||||
[{space, 2}, {indent, 2}]
|
|
||||||
),
|
|
||||||
<<"[\n {\n \"key\": \"value\",\n \"another key\": \"another value\"\n },\n [\n [\n true,\n false,\n null\n ]\n ]\n]">>
|
|
||||||
)},
|
)},
|
||||||
{"array spaces", ?_assertEqual(
|
{"specific space and indent", ?_assertEqual(
|
||||||
format(<<"[1,2,3]">>, [{space, 2}]),
|
#opts{space=2, indent=2},
|
||||||
<<"[1, 2, 3]">>
|
parse_opts([{space, 2}, {indent, 2}])
|
||||||
)},
|
)},
|
||||||
{"object spaces", ?_assertEqual(
|
{"invalid opt flag", ?_assertError(badarg, parse_opts([error]))},
|
||||||
format(<<"{\"a\":true,\"b\":true,\"c\":true}">>, [{space, 2}]),
|
{"invalid opt tuple", ?_assertError(badarg, parse_opts([{error, true}]))}
|
||||||
<<"{\"a\": true, \"b\": true, \"c\": true}">>
|
|
||||||
)},
|
|
||||||
{"array indent", ?_assertEqual(
|
|
||||||
format(<<"[1.23, 1.23, 1.23]">>, [{indent, 2}]),
|
|
||||||
<<"[\n 1.23,\n 1.23,\n 1.23\n]">>
|
|
||||||
)},
|
|
||||||
{"object indent", ?_assertEqual(
|
|
||||||
format(<<"{\"a\":true,\"b\":true,\"c\":true}">>, [{indent, 2}]),
|
|
||||||
<<"{\n \"a\":true,\n \"b\":true,\n \"c\":true\n}">>
|
|
||||||
)}
|
|
||||||
].
|
].
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue