Writing Meck testcases for gen_tcp function

295 views Asked by At

Here is a simple IRC bot module written by Erlang: IRC Bot

Could someone helps me write the testcase for the function connect and parse_line with MECK

connect(Host, Port) ->
        {ok, Sock} = gen_tcp:connect(Host, Port, [{packet, line}]),
        % According to RFC1459, we need to tell the server our nickname and username
        gen_tcp:send(Sock, "NICK " ++ ?nickname ++ "\r\n"),
        gen_tcp:send(Sock, "USER " ++ ?nickname ++ " blah blah blah blah\r\n"),
        loop(Sock).

parse_line(Sock, [User,"PRIVMSG",Channel,?nickname|_]) ->
        Nick = lists:nth(1, string:tokens(User, "!")),
        irc_privmsg(Sock, Channel, "You talkin to me, " ++ Nick ++ "?");            

parse_line(Sock, [_,"376"|_]) ->
        gen_tcp:send(Sock, "JOIN :" ++ ?channel ++ "\r\n");

parse_line(Sock, ["PING"|Rest]) ->
        gen_tcp:send(Sock, "PONG " ++ Rest ++ "\r\n");

parse_line(_, _) ->
        0.

Thanks you very much, I have already know how to use MECK to write some simple Erlang testcases about input/ otput, lists...but this IRC bot seems tobe quite beyond my current ability.

1

There are 1 answers

0
I GIVE CRAP ANSWERS On

I would suggest splitting your parsing code and output code from the rest of your logic. There is hardly any reason to test such low-level functionality, but if you added functions "in between" the low-level interface and your code, you could easily write test cases without even using Meck.