use io_lib:format for float formatting
This commit is contained in:
parent
ad3e870615
commit
9763ff189f
1 changed files with 7 additions and 27 deletions
|
@ -142,7 +142,7 @@ encode(literal, Literal, _Opts) ->
|
|||
encode(integer, Integer, _Opts) ->
|
||||
erlang:integer_to_list(Integer);
|
||||
encode(float, Float, _Opts) ->
|
||||
nicedecimal:format(Float).
|
||||
lists:flatten(io_lib:format("~p", [Float])).
|
||||
|
||||
|
||||
space(Opts) ->
|
||||
|
@ -176,24 +176,12 @@ indent_or_space(Opts) ->
|
|||
-ifdef(TEST).
|
||||
-include_lib("eunit/include/eunit.hrl").
|
||||
|
||||
setup_nicedecimal_meck(Return) ->
|
||||
ok = meck:new(nicedecimal),
|
||||
ok = meck:expect(nicedecimal, format, fun(1.23) -> Return end).
|
||||
|
||||
teardown_nicedecimal_meck(_) ->
|
||||
?assert(meck:validate(nicedecimal)),
|
||||
ok = meck:unload(nicedecimal).
|
||||
|
||||
basic_format_test_() ->
|
||||
[
|
||||
{"empty object", ?_assertEqual(format(<<"{}">>, []), <<"{}">>)},
|
||||
{"empty array", ?_assertEqual(format(<<"[]">>, []), <<"[]">>)},
|
||||
{"naked integer", ?_assertEqual(format(<<"123">>, []), <<"123">>)},
|
||||
{foreach,
|
||||
fun() -> setup_nicedecimal_meck(<<"1.23">>) end,
|
||||
fun(R) -> teardown_nicedecimal_meck(R) end,
|
||||
[{"naked float", ?_assertEqual(format(<<"1.23">>, []), <<"1.23">>)}]
|
||||
},
|
||||
{"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\"">>
|
||||
|
@ -238,11 +226,7 @@ basic_to_json_test_() ->
|
|||
{"empty object", ?_assertEqual(to_json([{}], []), <<"{}">>)},
|
||||
{"empty array", ?_assertEqual(to_json([], []), <<"[]">>)},
|
||||
{"naked integer", ?_assertEqual(to_json(123, []), <<"123">>)},
|
||||
{foreach,
|
||||
fun() -> setup_nicedecimal_meck(<<"1.23">>) end,
|
||||
fun(R) -> teardown_nicedecimal_meck(R) end,
|
||||
[{"naked float", ?_assertEqual(to_json(1.23, []) , <<"1.23">>)}]
|
||||
},
|
||||
{"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\"">>
|
||||
|
@ -316,14 +300,10 @@ opts_test_() ->
|
|||
format(<<"{\"a\":true,\"b\":true,\"c\":true}">>, [{space, 2}]),
|
||||
<<"{\"a\": true, \"b\": true, \"c\": true}">>
|
||||
)},
|
||||
{foreach,
|
||||
fun() -> setup_nicedecimal_meck(<<"1.23">>) end,
|
||||
fun(R) -> teardown_nicedecimal_meck(R) end,
|
||||
[{"array indent", ?_assertEqual(
|
||||
format(<<"[1.23, 1.23, 1.23]">>, [{indent, 2}]),
|
||||
<<"[\n 1.23,\n 1.23,\n 1.23\n]">>
|
||||
)}]
|
||||
},
|
||||
{"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