remove various tests from encoder and parser
This commit is contained in:
parent
dfbda5a9d3
commit
50133ffefd
2 changed files with 2 additions and 381 deletions
|
@ -23,7 +23,7 @@
|
|||
|
||||
-module(jsx_encoder).
|
||||
|
||||
-export([encoder/3]).
|
||||
-export([encoder/3, pre_encode/2]).
|
||||
|
||||
-spec encoder(Handler::module(), State::any(), Opts::jsx:opts()) -> jsx:encoder().
|
||||
|
||||
|
@ -120,7 +120,7 @@ list([], {Handler, State}, _Opts) -> Handler:handle_event(end_array, State);
|
|||
list(Term, Handler, Opts) -> ?error([Term, Handler, Opts]).
|
||||
|
||||
|
||||
pre_encode(Value, #opts{pre_encode=false}) -> Value;
|
||||
pre_encode(Value, #opts{pre_encode=false}) -> io:format("~p~n", [Value]), Value;
|
||||
pre_encode(Value, Opts) -> (Opts#opts.pre_encode)(Value).
|
||||
|
||||
|
||||
|
@ -136,211 +136,4 @@ clean_string(Bin, Opts) -> jsx_utils:clean_string(Bin, Opts).
|
|||
-include_lib("eunit/include/eunit.hrl").
|
||||
|
||||
|
||||
encode(Term) -> encode(Term, []).
|
||||
|
||||
encode(Term, Opts) ->
|
||||
try (encoder(jsx, [], Opts))(Term)
|
||||
catch _:_ -> {error, badarg}
|
||||
end.
|
||||
|
||||
|
||||
encode_test_() ->
|
||||
[
|
||||
{"naked string", ?_assertEqual(encode(<<"a string\n">>), [{string, <<"a string\n">>}, end_json])},
|
||||
{"escaped naked string", ?_assertEqual(encode(<<"a string\n">>, [escaped_strings]), [{string, <<"a string\\n">>}, end_json])},
|
||||
{"naked integer", ?_assertEqual(encode(123), [{integer, 123}, end_json])},
|
||||
{"naked float", ?_assertEqual(encode(1.23), [{float, 1.23}, end_json])},
|
||||
{"naked literal", ?_assertEqual(encode(null), [{literal, null}, end_json])},
|
||||
{"empty object", ?_assertEqual(encode([{}]), [start_object, end_object, end_json])},
|
||||
{"empty list", ?_assertEqual(encode([]), [start_array, end_array, end_json])},
|
||||
{"simple list", ?_assertEqual(
|
||||
encode([1,2,3,true,false]),
|
||||
[
|
||||
start_array,
|
||||
{integer, 1},
|
||||
{integer, 2},
|
||||
{integer, 3},
|
||||
{literal, true},
|
||||
{literal, false},
|
||||
end_array,
|
||||
end_json
|
||||
]
|
||||
)
|
||||
},
|
||||
{"simple object", ?_assertEqual(
|
||||
encode([{<<"a">>, true}, {<<"b">>, false}]),
|
||||
[
|
||||
start_object,
|
||||
{key, <<"a">>},
|
||||
{literal, true},
|
||||
{key, <<"b">>},
|
||||
{literal, false},
|
||||
end_object,
|
||||
end_json
|
||||
]
|
||||
)
|
||||
},
|
||||
{"complex term", ?_assertEqual(
|
||||
encode([
|
||||
{<<"a">>, true},
|
||||
{<<"b">>, false},
|
||||
{<<"c">>, [1,2,3]},
|
||||
{<<"d">>, [{<<"key">>, <<"value">>}]}
|
||||
]),
|
||||
[
|
||||
start_object,
|
||||
{key, <<"a">>},
|
||||
{literal, true},
|
||||
{key, <<"b">>},
|
||||
{literal, false},
|
||||
{key, <<"c">>},
|
||||
start_array,
|
||||
{integer, 1},
|
||||
{integer, 2},
|
||||
{integer, 3},
|
||||
end_array,
|
||||
{key, <<"d">>},
|
||||
start_object,
|
||||
{key, <<"key">>},
|
||||
{string, <<"value">>},
|
||||
end_object,
|
||||
end_object,
|
||||
end_json
|
||||
]
|
||||
)
|
||||
},
|
||||
{"atom keys", ?_assertEqual(
|
||||
encode([{key, <<"value">>}]),
|
||||
[start_object, {key, <<"key">>}, {string, <<"value">>}, end_object, end_json]
|
||||
)
|
||||
}
|
||||
].
|
||||
|
||||
|
||||
pre_encoders_test_() ->
|
||||
Term = [
|
||||
{<<"object">>, [
|
||||
{<<"literals">>, [true, false, null]},
|
||||
{<<"strings">>, [<<"foo">>, <<"bar">>, <<"baz">>]},
|
||||
{<<"numbers">>, [1, 1.0, 1.0e0]}
|
||||
]}
|
||||
],
|
||||
[
|
||||
{"no pre encode", ?_assertEqual(
|
||||
encode(Term, []),
|
||||
[
|
||||
start_object,
|
||||
{key, <<"object">>}, start_object,
|
||||
{key, <<"literals">>}, start_array,
|
||||
{literal, true}, {literal, false}, {literal, null},
|
||||
end_array,
|
||||
{key, <<"strings">>}, start_array,
|
||||
{string, <<"foo">>}, {string, <<"bar">>}, {string, <<"baz">>},
|
||||
end_array,
|
||||
{key, <<"numbers">>}, start_array,
|
||||
{integer, 1}, {float, 1.0}, {float, 1.0},
|
||||
end_array,
|
||||
end_object,
|
||||
end_object,
|
||||
end_json
|
||||
]
|
||||
)},
|
||||
{"replace lists with empty lists", ?_assertEqual(
|
||||
encode(Term, [{pre_encode, fun(V) -> case V of [{_,_}|_] -> V; [{}] -> V; V when is_list(V) -> []; _ -> V end end}]),
|
||||
[
|
||||
start_object,
|
||||
{key, <<"object">>}, start_object,
|
||||
{key, <<"literals">>}, start_array, end_array,
|
||||
{key, <<"strings">>}, start_array, end_array,
|
||||
{key, <<"numbers">>}, start_array, end_array,
|
||||
end_object,
|
||||
end_object,
|
||||
end_json
|
||||
]
|
||||
)},
|
||||
{"replace objects with empty objects", ?_assertEqual(
|
||||
encode(Term, [{pre_encode, fun(V) -> case V of [{_,_}|_] -> [{}]; _ -> V end end}]),
|
||||
[
|
||||
start_object,
|
||||
end_object,
|
||||
end_json
|
||||
]
|
||||
)},
|
||||
{"replace all non-list and non_tuple values with false", ?_assertEqual(
|
||||
encode(Term, [{pre_encode, fun(V) when is_list(V); is_tuple(V) -> V; (_) -> false end}]),
|
||||
[
|
||||
start_object,
|
||||
{key, <<"object">>}, start_object,
|
||||
{key, <<"literals">>}, start_array,
|
||||
{literal, false}, {literal, false}, {literal, false},
|
||||
end_array,
|
||||
{key, <<"strings">>}, start_array,
|
||||
{literal, false}, {literal, false}, {literal, false},
|
||||
end_array,
|
||||
{key, <<"numbers">>}, start_array,
|
||||
{literal, false}, {literal, false}, {literal, false},
|
||||
end_array,
|
||||
end_object,
|
||||
end_object,
|
||||
end_json
|
||||
]
|
||||
)},
|
||||
{"replace all atoms with atom_to_list", ?_assertEqual(
|
||||
encode(Term, [{pre_encode, fun(V) when is_atom(V) -> unicode:characters_to_binary(atom_to_list(V)); (V) -> V end}]),
|
||||
[
|
||||
start_object,
|
||||
{key, <<"object">>}, start_object,
|
||||
{key, <<"literals">>}, start_array,
|
||||
{string, <<"true">>}, {string, <<"false">>}, {string, <<"null">>},
|
||||
end_array,
|
||||
{key, <<"strings">>}, start_array,
|
||||
{string, <<"foo">>}, {string, <<"bar">>}, {string, <<"baz">>},
|
||||
end_array,
|
||||
{key, <<"numbers">>}, start_array,
|
||||
{integer, 1}, {float, 1.0}, {float, 1.0},
|
||||
end_array,
|
||||
end_object,
|
||||
end_object,
|
||||
end_json
|
||||
]
|
||||
)},
|
||||
{"pre_encode tuple", ?_assertEqual(
|
||||
encode({1, 2, 3}, [{pre_encode, fun(Tuple) when is_tuple(Tuple) -> tuple_to_list(Tuple); (V) -> V end}]),
|
||||
[
|
||||
start_array,
|
||||
{integer, 1}, {integer, 2}, {integer, 3},
|
||||
end_array,
|
||||
end_json
|
||||
]
|
||||
)},
|
||||
{"pre_encode 2-tuples", ?_assertEqual(
|
||||
encode([{two, 1}, {three, 2}], [{pre_encode, fun({K, V}) -> {K, V + 1}; (V) -> V end}]),
|
||||
[
|
||||
start_object,
|
||||
{key, <<"two">>}, {integer, 2}, {key, <<"three">>}, {integer, 3},
|
||||
end_object,
|
||||
end_json
|
||||
]
|
||||
)},
|
||||
{"pre_encode one field record", ?_assertEqual(
|
||||
encode([{foo, bar}], [{pre_encode, fun({foo, V}) -> {V, undefined}; (undefined) -> false; (V) -> V end}]),
|
||||
[
|
||||
start_object,
|
||||
{key, <<"bar">>}, {literal, false},
|
||||
end_object,
|
||||
end_json
|
||||
]
|
||||
)},
|
||||
{"pre_encode list", ?_assertEqual(
|
||||
encode([1,2,3], [{pre_encode, fun(X) when is_integer(X) -> X + 1; (V) -> V end}]),
|
||||
[
|
||||
start_array,
|
||||
{integer, 2}, {integer, 3}, {integer, 4},
|
||||
end_array,
|
||||
end_json
|
||||
]
|
||||
)}
|
||||
].
|
||||
|
||||
|
||||
-endif.
|
Loading…
Add table
Add a link
Reference in a new issue