Skip to content

Commit

Permalink
Pulled out response module to handle formatting of responses to clients,
Browse files Browse the repository at this point in the history
where necessary.

Oliver
  • Loading branch information
oferrigni committed Jun 13, 2011
1 parent 8481647 commit f1a7bc0
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
12 changes: 3 additions & 9 deletions src/cucumber.erl
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ execute_json(Json, AllStepModules) ->
Matches = discovery:step_matches(AllStepModules, Name),
case (Matches) of
ok ->
ToBeEncoded = [success, [{struct,[{id, utils:bitstring_to_atom(Name)}, {args, Args}]}]],
End = mochijson2:encode(ToBeEncoded),
End = response:matches(Name, Args),
{ok, End};
_ -> %io:format("Name not found: ~s~n", [Name]),
{ok, not_found}
Expand All @@ -22,14 +21,9 @@ execute_json(Json, AllStepModules) ->
{ok, end_scenario} -> {ok, noreply};
{ok, end_scenario, _Tags} -> {ok, noreply};
{ok, snippet_text, _Keyword, Name} ->
ListName = binary:bin_to_list(Name),
CucumberAtoms = utils:string_to_atoms(ListName),
%CompleteList = "For " ++ ListKeyword ++ " Please implement step(" ++ ListName ++ ")\n and step("++ ListName++ ", matches)",
JoinedAtoms = string:join(lists:map(fun(Elem) -> atom_to_list(Elem) end, CucumberAtoms), ","),
CompleteList = "step(" ++ JoinedAtoms ++ ")\nstep("++ JoinedAtoms ++ ", matches)->ok;",
End = mochijson2:encode([success, list_to_atom(CompleteList)]),
End = reponse:snippet(Name),
{ok, End};
{ok, invoke, Name, _Args} -> StepAtoms = utils:string_to_atoms(binary:bin_to_list(Name)),
{ok, invoke, Name, _Args} -> StepAtoms = utils:bitstring_to_atoms(Name),
discovery:run_steps(AllStepModules, StepAtoms);
{ok, new_line} -> {ok, new_line};
_ -> undefined
Expand Down
15 changes: 15 additions & 0 deletions src/response.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
-module(response).


-export([matches/2, snippet/1]).

matches(Name, Args) ->
ToBeEncoded = [success, [{struct,[{id, utils:bitstring_to_atom(Name)}, {args, Args}]}]],
mochijson2:encode(ToBeEncoded).

snippet(Name) ->
ListName = binary:bin_to_list(Name),
CucumberAtoms = utils:string_to_atoms(ListName),
JoinedAtoms = string:join(lists:map(fun(Elem) -> atom_to_list(Elem) end, CucumberAtoms), ","),
CompleteList = "step(" ++ JoinedAtoms ++ ")\nstep("++ JoinedAtoms ++ ", matches)->ok;",
mochijson2:encode([success, list_to_atom(CompleteList)]).
4 changes: 2 additions & 2 deletions test/cucumber_test.erl
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ execute_json_when_invoke_test() ->
meck:new(utils),
meck:new(discovery),
try
meck:expect(parsing, parse_json, fun("stub") -> {ok, invoke, "foo", "baz"} end),
meck:expect(utils, string_to_atoms, fun("foo") -> ['foo'] end),
meck:expect(parsing, parse_json, fun("stub") -> {ok, invoke, <<"foo">>, "baz"} end),
meck:expect(utils, bitstring_to_atoms, fun(<<"foo">>) -> ['foo'] end),
meck:expect(discovery, run_steps, fun([], ['foo']) -> {ok} end),
{ok} = cucumber:execute_json("stub", []),
?assert(meck:validate(parsing)),
Expand Down

0 comments on commit f1a7bc0

Please sign in to comment.