Skip to content
Browse files

Added test to handle modules that do not export handle/1 function

  • Loading branch information...
1 parent 65632a9 commit b2607c4c0bdf972e673c10617d84319631318cd0 Gianfranco Alongi committed
View
16 Two/Solution/src/port.erl
@@ -9,7 +9,8 @@
-spec(open(string(),integer(),atom() | string()) -> {ok,gen_tcp:socket()} |
{error,no_such_module}|
- {error,compile_error}).
+ {error,compile_error} |
+ {error,no_handle_function_exported}).
open(_,Port,echo) ->
{ok,Sock} = gen_tcp:listen(Port,[{active,false}]),
{ok,#port{type = echo,
@@ -29,10 +30,15 @@ open(FileDir,Port,ModuleName) ->
{error,compile_error};
{ok,Module,Binary} ->
{module,Module} = code:load_binary(Module,ErlSource,Binary),
- {ok,Sock} = gen_tcp:listen(Port,[{active,false}]),
- {ok,#port{type = ModuleName,
- socket = Sock,
- number = Port}}
+ case erlang:function_exported(Module,handle,1) of
+ false ->
+ {error,no_handle_function_exported};
+ true ->
+ {ok,Sock} = gen_tcp:listen(Port,[{active,false}]),
+ {ok,#port{type = ModuleName,
+ socket = Sock,
+ number = Port}}
+ end
end
end.
View
6 Two/Solution/test/port_tests.erl
@@ -30,6 +30,12 @@ module_does_exist_and_compilation_warning_open_test() ->
assert_port_open(Port),
port:close(Openport),
assert_port_closed(Port).
+
+module_does_exist_but_does_not_export_handle_function_test() ->
+ Port = 50005,
+ Type = "compilation_no_exported_handle_function_module",
+ FileDir = "./test/test_files/",
+ ?assertMatch({error,no_handle_function_exported},port:open(FileDir,Port,Type)).
%% ------------------------------------------------------------
View
4 Two/Solution/test/test_files/compilation_no_exported_handle_function_module.erl
@@ -0,0 +1,4 @@
+-module(compilation_no_exported_handle_function_module).
+-export([not_handle/1]).
+not_handle(X) ->
+ X.

0 comments on commit b2607c4

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