diff --git a/makefile b/makefile index d6d2f8c..086e5b2 100644 --- a/makefile +++ b/makefile @@ -1,8 +1,20 @@ -compile: +compile: clean get-deps ./rebar --jobs=1 compile +compile-test: get-test-deps + ./rebar --config rebar.test.config --jobs 1 compile + clean: ./rebar clean -test: clean - ./rebar --jobs=1 eunit \ No newline at end of file +clean-test: + ./rebar --config rebar.test.config clean + +get-deps: + ./rebar get-deps + +get-test-deps: + ./rebar --config rebar.test.config get-deps + +test: clean-test compile-test + ./rebar --config rebar.test.config --jobs 1 skip_deps=true eunit \ No newline at end of file diff --git a/rebar.config b/rebar.config index 6303376..303b263 100644 --- a/rebar.config +++ b/rebar.config @@ -1,10 +1,3 @@ -%% edit eunit_test_path if you want to run your own tests, use "../" not "./" as -%% rebar changes to working dir to .eunit when running tests -{eunit_compile_opts, [{d, eunit_test_path, "../test/cases"}]}. - -%% uncomment to get verbose output from test suite -%% {eunit_opts, [verbose]}. - {erl_opts, [{i, "src"}, {src_dirs, ["src"]}, warn_unused_vars, warn_export_all, diff --git a/rebar.test.config b/rebar.test.config new file mode 100644 index 0000000..ae6cc33 --- /dev/null +++ b/rebar.test.config @@ -0,0 +1,30 @@ +%% edit eunit_test_path if you want to run your own tests, use "../" not "./" as +%% rebar changes to working dir to .eunit when running tests +{eunit_compile_opts, [{d, eunit_test_path, "../test/cases"}]}. + +%% uncomment to get verbose output from test suite +%% {eunit_opts, [verbose]}. + +{erl_opts, [{i, "src"}, {src_dirs, ["src"]}, + warn_unused_vars, + warn_export_all, + warn_shadow_vars, + warn_unused_import, + warn_unused_function, + warn_bif_clash, + warn_unused_record, + warn_deprecated_function, + warn_obsolete_guard, + strict_validation, + warn_export_vars, + warn_exported_vars]}. +{dialyzer_opts, [{warnings, [unmatched_returns, error_handling, race_conditions, behaviours]}]}. +{xref_checks, [undefined_function_calls]}. +{cover_enabled, true}. + +{deps, [ + {'meck', + ".*", + {git, "git://github.com/eproxus/meck.git"} + } +]}. \ No newline at end of file diff --git a/src/jsx_to_json.erl b/src/jsx_to_json.erl index bcba3c6..6d012f5 100644 --- a/src/jsx_to_json.erl +++ b/src/jsx_to_json.erl @@ -176,12 +176,24 @@ 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", ?_assert(format(<<"{}">>, []) =:= <<"{}">>)}, {"empty array", ?_assert(format(<<"[]">>, []) =:= <<"[]">>)}, {"naked integer", ?_assert(format(<<"123">>, []) =:= <<"123">>)}, - {"naked float", ?_assert(format(<<"1.23">>, []) =:= <<"1.23">>)}, + {foreach, + fun() -> setup_nicedecimal_meck(<<"1.23">>) end, + fun(R) -> teardown_nicedecimal_meck(R) end, + [{"naked float", ?_assert(format(<<"1.23">>, []) =:= <<"1.23">>)}] + }, {"naked string", ?_assert(format(<<"\"hi\"">>, []) =:= <<"\"hi\"">>)}, {"naked literal", ?_assert(format(<<"true">>, []) =:= <<"true">>)}, {"simple object", @@ -229,7 +241,11 @@ basic_to_json_test_() -> {"empty object", ?_assert(to_json([{}], []) =:= <<"{}">>)}, {"empty array", ?_assert(to_json([], []) =:= <<"[]">>)}, {"naked integer", ?_assert(to_json(123, []) =:= <<"123">>)}, - {"naked float", ?_assert(to_json(1.23, []) =:= <<"1.23">>)}, + {foreach, + fun() -> setup_nicedecimal_meck(<<"1.23">>) end, + fun(R) -> teardown_nicedecimal_meck(R) end, + [{"naked float", ?_assert(to_json(1.23, []) =:= <<"1.23">>)}] + }, {"naked string", ?_assert(to_json(<<"hi">>, []) =:= <<"\"hi\"">>)}, {"naked literal", ?_assert(to_json(true, []) =:= <<"true">>)}, {"simple object", @@ -316,11 +332,16 @@ opts_test_() -> ) =:= <<"{\"a\": true, \"b\": true, \"c\": true}">> ) }, - {"array indent", - ?_assert(format(<<"[1.0, 2.0, 3.0]">>, - [{indent, 2}] - ) =:= <<"[\n 1.0,\n 2.0,\n 3.0\n]">> - ) + {foreach, + fun() -> setup_nicedecimal_meck(<<"1.23">>) end, + fun(R) -> teardown_nicedecimal_meck(R) end, + [{ + "array indent", + ?_assert(format(<<"[1.23, 1.23, 1.23]">>, + [{indent, 2}] + ) =:= <<"[\n 1.23,\n 1.23,\n 1.23\n]">> + ) + }] }, {"object indent", ?_assert(format(<<"{\"a\":true,\"b\":true,\"c\":true}">>,