ProgrammingErlang/ct.erl
author Eung-ju PARK <eungju@gmail.com>
Tue May 15 23:13:11 2007 +0900 (16 months ago)
changeset 33 22e2fa9932a4
parent 26870db81eceeb
permissions -rw-r--r--
Better naming.
     1 %% TODOs
     2 %% * Callback을 프로세스로 돌리기
     3 %% * 돌고 있는 콜백이 있으면 죽이고 시작하기
     4 %% * LAST_SNAPSHOT의 변경 시각을 현재 파일의 변경 시각과 비교하기
     5 -module(ct).
     6 -include_lib("eunit/include/eunit.hrl").
     7 -compile(export_all).
     8 
     9 -define(LATEST_SNAPSHOT, ".last").
    10 -define(MTIME_PRECISION, 1000).
    11 -define(TEST_FILE, "dut.txt").
    12 -define(TEST_MODULE, dut).
    13 
    14 is_modified(FileName, LastModified) ->
    15     case filelib:last_modified(FileName) of
    16 	LastModified ->
    17 	    false;
    18 	_ -> true
    19     end.
    20 
    21 touch(FileName) ->
    22     {ok, S} = file:open(FileName, write),
    23     io:format(S, "touch", []),
    24     file:close(S).
    25 
    26 is_modified_test() ->
    27     touch(?TEST_FILE),
    28     Modified = filelib:last_modified("dut.txt"),
    29     ?assertMatch(false, is_modified("dut.txt", Modified)),
    30     timer:sleep(?MTIME_PRECISION),
    31     touch(?TEST_FILE),
    32     ?assertMatch(true, is_modified("dut.txt", Modified)).
    33 
    34 watch_loop(ModuleName, Callback) ->
    35     watch_loop(ModuleName, Callback, 0).
    36 
    37 watch_loop(ModuleName, Callback, LastModified) ->    
    38     receive
    39 	stop ->
    40 	    void
    41     after ?MTIME_PRECISION ->
    42 	    case is_modified(file_name(ModuleName), LastModified) of
    43 		true ->
    44 		    Callback(ModuleName);
    45 		false ->
    46 		    void
    47 	    end,
    48 	    ?MODULE:watch_loop(ModuleName, Callback,
    49 		       filelib:last_modified(file_name(ModuleName)))
    50     end.
    51 
    52 callback_test() ->
    53     Me = self(),
    54     Callback = fun(_) -> Me ! modified end,
    55     Pid = spawn(fun() -> watch_loop(?TEST_MODULE, Callback) end),
    56     touch(?TEST_FILE),
    57     Invoked = receive
    58 		 modified ->
    59 		     true
    60 	     after 6000 ->
    61 		     false
    62 	     end,
    63     stop(Pid),
    64     ?assertMatch(true, Invoked).
    65 
    66 show_diff(Old, New) ->
    67     io:put_chars(os:cmd("diff -u " ++ Old ++ " " ++ New)).
    68 
    69 snapshot(FileName) ->
    70     file:copy(FileName, ?LATEST_SNAPSHOT).
    71 
    72 test_module(ModuleName) ->
    73     case c:c(ModuleName) of
    74         {ok, ModuleName} ->
    75             apply(ModuleName, test, []);
    76 	error ->
    77 	    void
    78     end.
    79 
    80 file_name(ModuleName) ->
    81     atom_to_list(ModuleName) ++ ".erl".
    82 
    83 continuous_testing(ModuleName) ->
    84     show_diff(?LATEST_SNAPSHOT, file_name(ModuleName)),
    85     snapshot(file_name(ModuleName)),
    86     test_module(ModuleName).
    87 
    88 start(ModuleName) ->
    89     spawn(?MODULE, watch_loop, [ModuleName, fun continuous_testing/1]).
    90 
    91 stop(Pid) ->
    92     Pid ! stop.