Skip to content

Commit

Permalink
Added test for compilation error on files and file that causes error
Browse files Browse the repository at this point in the history
  • Loading branch information
Gianfranco Alongi committed Mar 8, 2012
1 parent ed5a401 commit ece66f4
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
14 changes: 11 additions & 3 deletions Two/Solution/src/port.erl
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -7,17 +7,25 @@
number :: integer() number :: integer()
}). }).


-spec(open(string(),integer(),atom() | string()) -> {ok,gen_tcp:socket()} | {error,no_such_module}). -spec(open(string(),integer(),atom() | string()) -> {ok,gen_tcp:socket()} |
{error,no_such_module}|
{error,compile_error}).
open(_,Port,echo) -> open(_,Port,echo) ->
{ok,Sock} = gen_tcp:listen(Port,[{active,false}]), {ok,Sock} = gen_tcp:listen(Port,[{active,false}]),
{ok,#port{type = echo, {ok,#port{type = echo,
socket = Sock, socket = Sock,
number = Port}}; number = Port}};
open(FileDir,Port,ModuleName) -> open(FileDir,Port,ModuleName) ->
{ok,Files} = file:list_dir(FileDir), {ok,Files} = file:list_dir(FileDir),
case lists:member(ModuleName++".erl",Files) of ErlSource = ModuleName++".erl",
case lists:member(ErlSource,Files) of
false -> false ->
{error,no_such_module} {error,no_such_module};
true ->
case compile:file(filename:join(FileDir,ErlSource)) of
error ->
{error,compile_error}
end
end. end.




Expand Down
8 changes: 8 additions & 0 deletions Two/Solution/test/port_tests.erl
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ module_does_not_exist_port_failure_test() ->
FileDir = "./test/test_files/", FileDir = "./test/test_files/",
?assertEqual({error,no_such_module},port:open(FileDir,Port,Type)). ?assertEqual({error,no_such_module},port:open(FileDir,Port,Type)).


module_does_exist_but_can_not_compile_test() ->
Port = 50001,
Type = "compilation_error_module",
FileDir = "./test/test_files/",
?assertMatch({error,compile_error},port:open(FileDir,Port,Type)).



%% ------------------------------------------------------------ %% ------------------------------------------------------------
assert_port_open(Port) -> assert_port_open(Port) ->
{ok,Sock} = gen_tcp:connect("localhost",Port,[]), {ok,Sock} = gen_tcp:connect("localhost",Port,[]),
Expand Down
4 changes: 4 additions & 0 deletions Two/Solution/test/test_files/compilation_error_module.erl
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,4 @@
-module(compilation_error_module).
-export([handle/1]).
handle(_) ->
Unboundvariable.

0 comments on commit ece66f4

Please sign in to comment.