Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: Gianfrancoalongi/TDD-Exercises
base: c107b71803
...
head fork: Gianfrancoalongi/TDD-Exercises
compare: 3af123d328
Checking mergeability… Don't worry, you can still create the pull request.
  • 6 commits
  • 6 files changed
  • 0 commit comments
  • 1 contributor
View
43 Two/Solution/src/port.erl
@@ -1,18 +1,47 @@
-module(port).
--export([open/2,
+-export([open/3,
close/1
]).
--record(port,{type :: echo,
+-record(port,{type :: echo | string(),
socket :: gen_tcp:socket(),
number :: integer()
}).
--spec(open(integer(),atom() | string()) -> {ok,gen_tcp:socket()}).
-open(Port,echo) ->
+-spec(open(string(),integer(),atom() | string()) -> {ok,gen_tcp:socket()} |
+ {error,no_such_module}|
+ {error,compile_error} |
+ {error,no_handle_function_exported}).
+open(_,Port,echo) ->
{ok,Sock} = gen_tcp:listen(Port,[{active,false}]),
- {ok,#port{ type = echo,
- socket = Sock,
- number = Port}}.
+ {ok,#port{type = echo,
+ socket = Sock,
+ number = Port}};
+open(FileDir,Port,ModuleName) ->
+ {ok,Files} = file:list_dir(FileDir),
+ ErlSource = ModuleName++".erl",
+ case lists:member(ErlSource,Files) of
+ false ->
+ {error,no_such_module};
+ true ->
+ case compile:file(filename:join(FileDir,ErlSource),[report_warnings,
+ report_errors,
+ binary]) of
+ error ->
+ {error,compile_error};
+ {ok,Module,Binary} ->
+ {module,Module} = code:load_binary(Module,ErlSource,Binary),
+ 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.
+
-spec(close(#port{}) -> ok).
close(Port) ->
View
39 Two/Solution/test/port_tests.erl
@@ -4,7 +4,44 @@
echo_port_open_close_test() ->
Port = 50001,
Type = echo,
- {ok,Openport} = port:open(Port,Type),
+ FileDir = "./test/test_files/",
+ {ok,Openport} = port:open(FileDir,Port,Type),
+ assert_port_open(Port),
+ port:close(Openport),
+ assert_port_closed(Port).
+
+module_does_not_exist_port_failure_test() ->
+ Port = 50002,
+ Type = "non_existent_module",
+ FileDir = "./test/test_files/",
+ ?assertEqual({error,no_such_module},port:open(FileDir,Port,Type)).
+
+module_does_exist_but_can_not_compile_test() ->
+ Port = 50003,
+ Type = "compilation_error_module",
+ FileDir = "./test/test_files/",
+ ?assertMatch({error,compile_error},port:open(FileDir,Port,Type)).
+
+module_does_exist_and_compilation_warning_open_test() ->
+ Port = 50004,
+ Type = "compilation_warning_module",
+ FileDir = "./test/test_files/",
+ {ok,Openport} = port:open(FileDir,Port,Type),
+ 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)).
+
+module_exists_and_is_clean_open_test() ->
+ Port = 50006,
+ Type = "compilation_clean_module",
+ FileDir = "./test/test_files/",
+ {ok,Openport} = port:open(FileDir,Port,Type),
assert_port_open(Port),
port:close(Openport),
assert_port_closed(Port).
View
4 Two/Solution/test/test_files/compilation_clean_module.erl
@@ -0,0 +1,4 @@
+-module(compilation_clean_module).
+-export([handle/1]).
+handle(X) ->
+ X.
View
4 Two/Solution/test/test_files/compilation_error_module.erl
@@ -0,0 +1,4 @@
+-module(compilation_error_module).
+-export([handle/1]).
+handle(_) ->
+ Unboundvariable.
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.
View
4 Two/Solution/test/test_files/compilation_warning_module.erl
@@ -0,0 +1,4 @@
+-module(compilation_warning_module).
+-export([handle/1]).
+handle(UnusedVariable) ->
+ ok.

No commit comments for this range

Something went wrong with that request. Please try again.