Skip to content

Commit

Permalink
normalize validation result; breaking changes!
Browse files Browse the repository at this point in the history
  • Loading branch information
andreineculau committed Dec 28, 2014
1 parent 33c17dd commit 57cfe50
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 19 deletions.
19 changes: 8 additions & 11 deletions src/katt_callbacks.erl
Original file line number Diff line number Diff line change
Expand Up @@ -183,12 +183,8 @@ validate( Expected = #katt_response{}
validate_headers(Expected, Actual, Callbacks)),
{AddParams2, Failures2} = get_params_and_failures(
validate_body(Expected, Actual, Callbacks)),
AddParams = lists:flatten([ AddParams0
, AddParams1
, AddParams2]),
Failures = lists:flatten([ Failures0
, Failures1
, Failures2]),
AddParams = AddParams0 ++ AddParams1 ++ AddParams2,
Failures = Failures0 ++ Failures1 ++ Failures2,
case Failures of
[] -> {pass, AddParams};
_ -> {fail, Failures}
Expand All @@ -206,16 +202,17 @@ progress(_Step, _Detail) ->

%%%_* Internal =================================================================

get_params_and_failures(Result) when not is_list(Result) ->
get_params_and_failures([Result]);
get_params_and_failures(Result) ->
lists:foldl(
fun(pass, Acc) -> Acc;
({pass, AddParam}, {AddParams0, Failures0}) ->
{[AddParam | AddParams0], Failures0};
(Failure, {AddParams0, Failures0}) ->
fun({pass, AddParams}, {AddParams0, Failures0}) ->
{AddParams ++ AddParams0, Failures0};
(Failure, {AddParams0, Failures0}) ->
{AddParams0, [Failure | Failures0]}
end,
{[],[]},
lists:flatten([Result])
lists:flatten(Result)
).

http_request(R = #katt_request{}, Params) ->
Expand Down
16 changes: 8 additions & 8 deletions src/katt_util.erl
Original file line number Diff line number Diff line change
Expand Up @@ -196,10 +196,10 @@ validate(ParentKey, E, A) ->

%% Expected actual
validate(_ParentKey, _E, _E, _Unexpected, _Callbacks) ->
pass;
{pass, []};
%% Expected anything
validate(_ParentKey, ?MATCH_ANY = _E, _A, _Unexpected, _Callbacks) ->
pass;
{pass, []};
%% Expected struct, got struct
validate( ParentKey
, {struct, EItems} = _E
Expand Down Expand Up @@ -251,12 +251,12 @@ validate(ParentKey, E, A, Unexpected, _Callbacks) ->
%% Validate when unexpected values show up
%% Expected anything
validate_simple(_Key, undefined = _E, _A, ?MATCH_ANY) ->
pass;
{pass, []};
%% validate_simple(_Key, [] = _E, _A, ?MATCH_ANY) ->
%% pass;
%% {pass, []};
%% Not expected and undefined
validate_simple(_Key, ?UNEXPECTED = _E, undefined = _A, _Unexpected) ->
pass;
{pass, []};
%% Not expected
validate_simple(Key, undefined = E, A, ?UNEXPECTED) ->
{unexpected, {Key, E, A}};
Expand All @@ -272,13 +272,13 @@ validate_simple(Key, E, A, _Unexpected) ->

%% Validate JSON primitive types or empty structured types
validate_primitive(_Key, E, E) ->
pass;
{pass, []};
validate_primitive(Key, E, A) when is_binary(A) ->
validate_primitive(Key, E, from_utf8(A));
validate_primitive(Key, E, A) when is_binary(E) ->
validate_primitive(Key, from_utf8(E), A);
validate_primitive(_Key, ?MATCH_ANY, _A) ->
pass;
{pass, []};
validate_primitive(Key, E, A) when is_list(E) ->
case re:run( E
, "(" ++ ?STORE_BEGIN_TAG ++ "[^}]+" ++ ?STORE_END_TAG ++ ")"
Expand All @@ -289,7 +289,7 @@ validate_primitive(Key, E, A) when is_list(E) ->
nomatch ->
{not_equal, {Key, E, A}};
{match, [[E]]} ->
{pass, {store_tag2param(E), A}};
{pass, [{store_tag2param(E), A}]};
{match, Params0} ->
Type = if
is_list(A) ->
Expand Down

0 comments on commit 57cfe50

Please sign in to comment.