Skip to content
Browse files

Some test refactoring using fixture

  • Loading branch information...
1 parent 24b3b1b commit 0b82cfb7ff2d0afe5de6536ddb7bb0df4d1f9292 Gianfranco Alongi committed Mar 12, 2012
Showing with 32 additions and 30 deletions.
  1. +32 −30 Two/Solution/test/server_tests.erl
View
62 Two/Solution/test/server_tests.erl
@@ -1,6 +1,6 @@
-module(server_tests).
-include_lib("eunit/include/eunit.hrl").
-
+-define(COMMAND_PORT,50001).
server_start_test() ->
CommandPort = 50001,
@@ -11,49 +11,51 @@ server_start_test() ->
?assertEqual(ok,server:stop(CommandPort)),
assert_port_closed(CommandPort).
-server_list_bind_command_test() ->
- CommandPort = 50001,
- Options = [{files,"./test/test_files/"},
- {command_port,CommandPort}],
- server:start(Options),
- ?assertEqual("bindings: none",send_receive_command(CommandPort,"list bind")),
- server:stop(CommandPort).
+command_test_() ->
+ {foreach,
+ fun start/0,
+ fun stop/1,
+ [fun list_bind/0,
+ fun bind_type/0,
+ fun unbind_type/0
+ ]}.
-server_bind_type_file_test() ->
- CommandPort = 50001,
- Options = [{files,"./test/test_files/"},
- {command_port,CommandPort}],
- server:start(Options),
- BindRes = send_receive_command(CommandPort,"bind testtype compilation_clean_module"),
- ?assertEqual("binding created",BindRes),
- ListRes = send_receive_command(CommandPort,"list bind"),
+list_bind() ->
+ ?assertEqual("bindings: none",send_receive_command("list bind")).
+
+bind_type() ->
+ ?assertEqual("binding created",
+ send_receive_command("bind testtype compilation_clean_module")),
?assertEqual("bindings:\n"
- " testtype, compilation_clean_module",ListRes),
- server:stop(CommandPort).
+ " testtype, compilation_clean_module",
+ send_receive_command("list bind")).
-server_unbind_type_test() ->
- CommandPort = 50001,
- Options = [{files,"./test/test_files/"},
- {command_port,CommandPort}],
- server:start(Options),
- send_receive_command(CommandPort,"bind testtype compilation_clean_module"),
- ?assertEqual("binding undone",send_receive_command(CommandPort,"unbind testtype")),
- ListRes = send_receive_command(CommandPort,"list bind"),
- ?assertEqual("bindings: none",ListRes),
- server:stop(CommandPort).
+unbind_type() ->
+ send_receive_command("bind testtype compilation_clean_module"),
+ ?assertEqual("binding undone",send_receive_command("unbind testtype")),
+ ?assertEqual("bindings: none",send_receive_command("list bind")).
%% --------------------------------------------------
+start() ->
+ Options = [{files,"./test/test_files/"},
+ {command_port,?COMMAND_PORT}],
+ server:start(Options),
+ ok.
+
+stop(ok) ->
+ server:stop(?COMMAND_PORT).
+
assert_port_open(Port) ->
{ok,Sock} = gen_tcp:connect("localhost",Port,[]),
gen_tcp:close(Sock).
assert_port_closed(Port) ->
{error,econnrefused} = gen_tcp:connect("localhost",Port,[]).
-send_receive_command(CommandPort,Command) ->
- {ok,Sock} = gen_tcp:connect("localhost",CommandPort,[{active,false}]),
+send_receive_command(Command) ->
+ {ok,Sock} = gen_tcp:connect("localhost",?COMMAND_PORT,[{active,false}]),
gen_tcp:send(Sock,Command),
{ok,Socket} = gen_tcp:recv(Sock,0),
Socket.

0 comments on commit 0b82cfb

Please sign in to comment.
Something went wrong with that request. Please try again.