Skip to content

Commit

Permalink
move tests to test/kvc_tests.erl
Browse files Browse the repository at this point in the history
  • Loading branch information
etrepum committed Jun 11, 2011
1 parent 67e0dc4 commit 2a9ae2d
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 144 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
/.eunit
/doc
/ebin
/deps
TEST-*.xml
*.beam
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
REBAR=`which rebar || ./rebar`
.PHONY: all edoc test clean

all:
@$(REBAR) get-deps compile
Expand Down
144 changes: 0 additions & 144 deletions src/kvc.erl
Original file line number Diff line number Diff line change
Expand Up @@ -159,147 +159,3 @@ normalize(K, string) when is_atom(K) ->
atom_to_list(K);
normalize(K, undefined) ->
K.

-ifdef(TEST).
-include_lib("eunit/include/eunit.hrl").
path_aggregate_test() ->
?assertEqual(
[taco, taco, grande],
kvc:path(foo, [{foo, [taco, taco, grande]}])),
?assertEqual(
3,
kvc:path(<<"foo.@count">>, [{foo, [taco, taco, grande]}])),
?assertEqual(
6,
kvc:path('foo.@sum', [{foo, [1, 2, 3]}])),
?assertEqual(
2.0,
kvc:path("foo.@avg", [{foo, [1, 2, 3]}])),
?assertEqual(
1,
kvc:path([foo, '@min'], [{foo, [1, 2, 3]}])),
?assertEqual(
3,
kvc:path("foo.@max", [{foo, [1, 2, 3]}])),
?assertEqual(
[taco, taco, grande],
kvc:path(<<"foo.@unionOfObjects">>,
[{foo, [taco, taco, grande]}])),
?assertEqual(
[taco, taco, grande],
kvc:path(<<"foo.@unionOfArrays">>,
[{foo, [[taco], [taco], [grande]]}])),
?assertEqual(
lists:sort([taco, grande]),
lists:sort(kvc:path(<<"foo.@distinctUnionOfObjects">>,
[{foo, [taco, taco, grande]}]))),
?assertEqual(
lists:sort([taco, grande]),
lists:sort(kvc:path(<<"foo.@distinctUnionOfArrays">>,
[{foo, [[taco], [taco], [grande]]}]))),
ok.

pairwise_combinations(L) ->
pairwise_combinations(L, []).

pairwise_combinations([], Acc) ->
lists:append(lists:reverse(Acc));
pairwise_combinations(L=[H | T], Acc) ->
pairwise_combinations(T, [[{H, E} || E <- L] | Acc]).

value_coercion_test() ->
lists:foreach(fun ({K0, K1}) ->
?assertEqual(bar, kvc:value(K0, [{K1, bar}], []))
end,
pairwise_combinations([a, <<"a">>, "a"])).

value_aggregate_test() ->
?assertEqual(
6,
kvc:value('@sum', [1, 2, 3], [])),
?assertEqual(
6,
kvc:value("@sum", [1, 2, 3], [])),
?assertEqual(
6,
kvc:value(<<"@sum">>, [1, 2, 3], [])),
?assertEqual(
2.0,
kvc:value(<<"@avg">>, [1, 2, 3], [])),
?assertEqual(
[],
kvc:value(<<"@avg">>, [], [])),
ok.

path_edge_test() ->
?assertEqual(
[bar],
kvc:path(foo, [[{foo, bar}], [{bar, baz}]])),
?assertEqual(
[bar],
kvc:path(foo, [[{foo, bar}], [{bar, baz}]])),
ok.

value_edge_test() ->
?assertEqual(
[],
kvc:value(foo, [{1, 2}], [])),
?assertEqual(
[],
kvc:value(<<255>>, [{foo, ok}], [])),
?assertEqual(
[],
kvc:value([256], [{foo, ok}], [])),
ok.

path_plist_test() ->
lists:foreach(
fun (F) ->
?assertEqual(
baz,
kvc:path(foo.bar, F([{foo, [{bar, baz}]}]))),
?assertEqual(
[],
kvc:path(foo.bar, F([{foo, [{baz, baz}]}]))),
?assertEqual(
[],
kvc:path(foo.bar, F([{not_foo, ok}]))),
?assertEqual(
[],
kvc:path(foo.bar, F([])))
end,
[fun gb_trees:from_orddict/1, fun dict:from_list/1]),
?assertEqual(
wibble,
kvc:path(foo.bar.baz, [{foo, [{bar, [{baz, wibble}]}]}])),
?assertEqual(
[],
kvc:path(foo.bar.baz.invalid_proplist, [{foo, [{bar, [{baz, wibble}]}]}])),
?assertEqual(
[],
kvc:path(foo.bar.baz, [{foo, [{bar, [{bar, wibble}]}]}])),
?assertEqual(
<<"wibble">>,
kvc:path(foo.bar.baz,
{struct,
[{<<"foo">>,
{struct,
[{<<"bar">>,
{struct, [{<<"baz">>, <<"wibble">>}]}}]}}]})),
?assertEqual(
"wibble",
kvc:path(foo.bar.baz,
{struct,
[{"foo",
{struct,
[{"bar",
{struct, [{"baz", "wibble"}]}}]}}]})),
?assertEqual(
ok,
kvc:value("foo", [{foo, ok}], [])),
?assertEqual(
ok,
kvc:value("foo", [{<<"foo">>, ok}], [])),
ok.

-endif.
147 changes: 147 additions & 0 deletions test/kvc_tests.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
%% @copyright 2011 Bob Ippolito
%% @author Bob Ippolito <bob@redivi.com>

%% @doc Tests for KVC.
-module(kvc_tests).
-include_lib("proper/include/proper.hrl").
-include_lib("eunit/include/eunit.hrl").

path_aggregate_test() ->
?assertEqual(
[taco, taco, grande],
kvc:path(foo, [{foo, [taco, taco, grande]}])),
?assertEqual(
3,
kvc:path(<<"foo.@count">>, [{foo, [taco, taco, grande]}])),
?assertEqual(
6,
kvc:path('foo.@sum', [{foo, [1, 2, 3]}])),
?assertEqual(
2.0,
kvc:path("foo.@avg", [{foo, [1, 2, 3]}])),
?assertEqual(
1,
kvc:path([foo, '@min'], [{foo, [1, 2, 3]}])),
?assertEqual(
3,
kvc:path("foo.@max", [{foo, [1, 2, 3]}])),
?assertEqual(
[taco, taco, grande],
kvc:path(<<"foo.@unionOfObjects">>,
[{foo, [taco, taco, grande]}])),
?assertEqual(
[taco, taco, grande],
kvc:path(<<"foo.@unionOfArrays">>,
[{foo, [[taco], [taco], [grande]]}])),
?assertEqual(
lists:sort([taco, grande]),
lists:sort(kvc:path(<<"foo.@distinctUnionOfObjects">>,
[{foo, [taco, taco, grande]}]))),
?assertEqual(
lists:sort([taco, grande]),
lists:sort(kvc:path(<<"foo.@distinctUnionOfArrays">>,
[{foo, [[taco], [taco], [grande]]}]))),
ok.

pairwise_combinations(L) ->
pairwise_combinations(L, []).

pairwise_combinations([], Acc) ->
lists:append(lists:reverse(Acc));
pairwise_combinations(L=[H | T], Acc) ->
pairwise_combinations(T, [[{H, E} || E <- L] | Acc]).

value_coercion_test() ->
lists:foreach(fun ({K0, K1}) ->
?assertEqual(bar, kvc:value(K0, [{K1, bar}], []))
end,
pairwise_combinations([a, <<"a">>, "a"])).

value_aggregate_test() ->
?assertEqual(
6,
kvc:value('@sum', [1, 2, 3], [])),
?assertEqual(
6,
kvc:value("@sum", [1, 2, 3], [])),
?assertEqual(
6,
kvc:value(<<"@sum">>, [1, 2, 3], [])),
?assertEqual(
2.0,
kvc:value(<<"@avg">>, [1, 2, 3], [])),
?assertEqual(
[],
kvc:value(<<"@avg">>, [], [])),
ok.

path_edge_test() ->
?assertEqual(
[bar],
kvc:path(foo, [[{foo, bar}], [{bar, baz}]])),
?assertEqual(
[bar],
kvc:path(foo, [[{foo, bar}], [{bar, baz}]])),
ok.

value_edge_test() ->
?assertEqual(
[],
kvc:value(foo, [{1, 2}], [])),
?assertEqual(
[],
kvc:value(<<255>>, [{foo, ok}], [])),
?assertEqual(
[],
kvc:value([256], [{foo, ok}], [])),
ok.

path_plist_test() ->
lists:foreach(
fun (F) ->
?assertEqual(
baz,
kvc:path(foo.bar, F([{foo, [{bar, baz}]}]))),
?assertEqual(
[],
kvc:path(foo.bar, F([{foo, [{baz, baz}]}]))),
?assertEqual(
[],
kvc:path(foo.bar, F([{not_foo, ok}]))),
?assertEqual(
[],
kvc:path(foo.bar, F([])))
end,
[fun gb_trees:from_orddict/1, fun dict:from_list/1]),
?assertEqual(
wibble,
kvc:path(foo.bar.baz, [{foo, [{bar, [{baz, wibble}]}]}])),
?assertEqual(
[],
kvc:path(foo.bar.baz.invalid_proplist, [{foo, [{bar, [{baz, wibble}]}]}])),
?assertEqual(
[],
kvc:path(foo.bar.baz, [{foo, [{bar, [{bar, wibble}]}]}])),
?assertEqual(
<<"wibble">>,
kvc:path(foo.bar.baz,
{struct,
[{<<"foo">>,
{struct,
[{<<"bar">>,
{struct, [{<<"baz">>, <<"wibble">>}]}}]}}]})),
?assertEqual(
"wibble",
kvc:path(foo.bar.baz,
{struct,
[{"foo",
{struct,
[{"bar",
{struct, [{"baz", "wibble"}]}}]}}]})),
?assertEqual(
ok,
kvc:value("foo", [{foo, ok}], [])),
?assertEqual(
ok,
kvc:value("foo", [{<<"foo">>, ok}], [])),
ok.

0 comments on commit 2a9ae2d

Please sign in to comment.