added makefile, fixed problems with tests

This commit is contained in:
Jeena Paradies 2011-02-22 03:11:35 +01:00
parent e4753b2c07
commit 6ee945379f
8 changed files with 58 additions and 36 deletions

28
Makefile Normal file
View file

@ -0,0 +1,28 @@
ERLC=erlc
ERLCFLAGS=-o
SRCDIR=src
TESTDIR=tests
BEAMDIR=./ebin
all: compile erlang_js
compile:
@ mkdir -p $(BEAMDIR) ;
@ $(ERLC) $(ERLCFLAGS) $(BEAMDIR) $(SRCDIR)/*.erl ;
erlang_js:
$(MAKE) -C erlang_js/
test: compile erlang_js
@ mkdir -p $(BEAMDIR) ;
@ $(ERLC) $(ERLCFLAGS) $(BEAMDIR) $(TESTDIR)/*.erl ;
@ cd $(BEAMDIR) ; erl -noinput -eval 'eunit:test({dir, "."}, [verbose]), init:stop()' ;
clean:
@ rm -rf $(BEAMDIR)/*.beam ;
@ rm -rf erl_crush.dump ;
@ echo "==> clean ggs" ;
@ $(MAKE) -C erlang_js/ clean
run:
@ erl -sname ggs -mnesia -boot start_sasl -pa erlang_js/ebin/ -pa ebin -pa src -s start_ggs

View file

@ -65,7 +65,7 @@ handle_call({join_table, Table}, From, State) ->
{FromPlayer, _Ref} = From, {FromPlayer, _Ref} = From,
Tables = State#co_state.tables, Tables = State#co_state.tables,
case lists:keyfind(Table, 1, Tables) of case lists:keyfind(Table, 1, Tables) of
{TableID, TablePID} -> {_TableID, TablePID} ->
ggs_table:add_player(TablePID, FromPlayer), ggs_table:add_player(TablePID, FromPlayer),
{reply, {ok, TablePID}, State}; {reply, {ok, TablePID}, State};
false -> false ->
@ -84,7 +84,7 @@ handle_call({create_table, {force, TableID}}, From, State) ->
handle_call(_Message, _From, State) -> handle_call(_Message, _From, State) ->
{noreply, State}. {noreply, State}.
handle_cast({stop, Reason}, State) -> handle_cast({stop, _Reason}, _State) ->
{stop, normal, state}; {stop, normal, state};
handle_cast(_Message, State) -> handle_cast(_Message, State) ->

View file

@ -45,7 +45,7 @@ handle_call(_Message, _From, State) ->
handle_cast(_Message, State) -> handle_cast(_Message, State) ->
{noreply, State}. {noreply, State}.
handle_info({tcp, _Socket, RawData}, State) -> handle_info({tcp, _Socket, _RawData}, State) ->
io:format("Got connect request!~n"), io:format("Got connect request!~n"),
{noreply, State}; {noreply, State};

View file

@ -32,7 +32,7 @@ user_command(GameVM, Player, Command, Args) ->
loop(Table) -> loop(Table) ->
receive receive
{define, SourceCode} -> {define, _SourceCode} ->
loop(Table); loop(Table);
{user_command, _User, Command, _Args, _From, _Ref} -> {user_command, _User, Command, _Args, _From, _Ref} ->
io:format("GameVM received a message~n"), io:format("GameVM received a message~n"),

View file

@ -60,7 +60,7 @@ loop(#pl_state{token = Token, socket = Socket, table = Table} = State) ->
io:format("Notifying table..~n"), io:format("Notifying table..~n"),
ggs_table:notify_game(Table, Token, Data), ggs_table:notify_game(Table, Token, Data),
loop(State); loop(State);
{notify, From, Message} -> {notify, _From, Message} ->
gen_tcp:send(Socket, Message), gen_tcp:send(Socket, Message),
loop(State) loop(State)
end. end.

View file

@ -5,17 +5,18 @@
%% gen_server callbacks %% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, -export([init/1, handle_call/3, handle_cast/2, handle_info/2,
terminate/2, code_change/3, notify_all_players/2, notify_game/3, terminate/2, code_change/3]).
add_player/2]).
-record(state, { players, game_vm } ). -record(state, { players, game_vm } ).
%% API %% API
-export([start_link/0, -export([start_link/0,
add_player/2, add_player/2,
remove_player/2, remove_player/2,
stop/1, stop/1,
notify/3]). notify/3,
notify_all_players/2,
notify_game/3]).
%% ---------------------------------------------------------------------- %% ----------------------------------------------------------------------
@ -24,6 +25,7 @@
% @doc returns a new table % @doc returns a new table
start_link() -> start_link() ->
{ok, Pid} = gen_server:start_link(?MODULE, [], []), {ok, Pid} = gen_server:start_link(?MODULE, [], []),
Pid.
%% @private %% @private
call(Pid, Msg) -> call(Pid, Msg) ->

View file

@ -1,6 +1,5 @@
-module(ggs_player_test). -module(ggs_player_test).
-include_lib("eunit/include/eunit.hrl"). -include_lib("eunit/include/eunit.hrl").
-import(ggs_player).
%% @doc start_link should always return ok for any valid socket. A valid socket %% @doc start_link should always return ok for any valid socket. A valid socket
%% should always return {ok, Pid} and {error, Reason} otherwise. %% should always return {ok, Pid} and {error, Reason} otherwise.
@ -10,9 +9,9 @@ start_link_test() ->
%% @doc Given that start_link returned {ok, Player}. Notify shall always return ok and %% @doc Given that start_link returned {ok, Player}. Notify shall always return ok and
%% deliver a specified message through the socket. %% deliver a specified message through the socket.
notify_test() -> notify_test() ->
Player = start_link("bad arg"), Player = ggs_player:start_link("bad arg"),
Message = {"something", ""}, Message = {"something", ""},
Ret = ggs_player:notify(Player, self(), Message) Ret = ggs_player:notify(Player, self(), Message),
?assertNot(ok =:= Ret). ?assertNot(ok =:= Ret).
%% @doc Given that start_link returned {ok, Player}. get_token shall always return a valid %% @doc Given that start_link returned {ok, Player}. get_token shall always return a valid
@ -23,6 +22,6 @@ get_token_test() ->
%% @doc Given that start_link returned {ok, Pid}. There shouldn't be possible to %% @doc Given that start_link returned {ok, Pid}. There shouldn't be possible to
%% execute this function with the same Player and Table arguments twice. %% execute this function with the same Player and Table arguments twice.
stop_test() -> stop_test() ->
Player = start_link(something), Player = ggs_player:start_link(something),
Table = test, Table = test,
ok = stop(Player, Table). ok = ggs_player:stop(Player, Table).

View file

@ -1,45 +1,38 @@
-module(ggs_table_test). -module(ggs_table_test).
-include_lib("eunit/include/eunit.hrl"). -include_lib("eunit/include/eunit.hrl").
-import(ggs_table).
% @private
start_link_test() -> start_link_test() ->
Table = start_link(), Table = ggs_table:start_link(),
?assertNot(Table =:= undefined). ?assertNot(Table =:= undefined).
% @private
add_player_test() -> add_player_test() ->
Table = start_link(), Table = ggs_table:start_link(),
Player = test_player, Player = test_player,
add_player(Table, Player), ggs_table:add_player(Table, Player),
{ok, [Player]} = gen_server:call(Table, get_player_list). {ok, [Player]} = gen_server:call(Table, get_player_list).
% @private
remove_player_test() -> remove_player_test() ->
Table = start_link(), Table = ggs_table:start_link(),
Player = test_player, Player = test_player,
Player2 = test_player2, Player2 = test_player2,
add_player(Table, Player), ggs_table:add_player(Table, Player),
{ok, [Player]} = gen_server:call(Table, get_player_list), {ok, [Player]} = gen_server:call(Table, get_player_list),
add_player(Table, Player2), ggs_table:add_player(Table, Player2),
{ok, [Player2, Player]} = gen_server:call(Table, get_player_list), {ok, [Player2, Player]} = gen_server:call(Table, get_player_list),
remove_player(Table, Player), ggs_table:remove_player(Table, Player),
{ok, [Player2]} = gen_server:call(Table, get_player_list), {ok, [Player2]} = gen_server:call(Table, get_player_list),
remove_player(Table, Player2), ggs_table:remove_player(Table, Player2),
{ok, []} = gen_server:call(Table, get_player_list). {ok, []} = gen_server:call(Table, get_player_list).
% @private
stop_test() -> stop_test() ->
Table = start_link(), Table = ggs_table:start_link(),
ok = stop(Table). ok = ggs_table:stop(Table).
% @private
notify_test() -> notify_test() ->
Table = start_link(), Table = ggs_table:start_link(),
Player = test_player, Player = test_player,
Message = {server, define, "function helloWorld(x) { }"}, Message = {server, define, "function helloWorld(x) { }"},
ok = notify(Table, Player, Message). ok = ggs_table:notify(Table, Player, Message),
Message2 = {game, "helloWorld", "test"}, Message2 = {game, "helloWorld", "test"},
ok = notify(Table, Player, Message2). ok = ggs_table:notify(Table, Player, Message2).