X-Git-Url: http://j8takagi.net/cgi-bin/gitweb.cgi?p=YACASL2.git;a=blobdiff_plain;f=test%2Funit%2FTest.mk;h=ac0fbb75c9660e693fcdd739d1f7536291ff46a0;hp=ada87aee33ffe4cbe875d0afcdd1e85aa08b0e43;hb=9927edbc8aa5f1add65502eca27401d5d5d098f2;hpb=73d1617a1a5b766d1a1e960d3af8f87789d49e5c diff --git a/test/unit/Test.mk b/test/unit/Test.mk index ada87ae..ac0fbb7 100644 --- a/test/unit/Test.mk +++ b/test/unit/Test.mk @@ -12,16 +12,97 @@ # make cleantime: "make time" で作成されたファイルをクリア # make clean : "make" で作成されたファイルをクリア # make cleanall: "make" と "make set" で作成されたファイルをクリア - SHELL = /bin/bash +###################################################################### +# マクロ +###################################################################### + +# 指定されたファイルをチェックし、空の場合は削除 +# 引数は、対象ファイル +# 用例: $(call rm_null,file_target) +define rm_null + if test ! -s $1; then $(RM) $1; fi +endef + +# 説明ファイルの内容を、指定されたファイルに出力 +# 引数は、出力ファイル +# 用例: $(call desc_log,file_out) +define desc_log + if test -s $(DESC_FILE); then $(CAT) $(DESC_FILE) >>$1; fi +endef + +# テスト実行の経過時間を、指定されたファイルに出力して表示 +# 引数は、テスト名、コマンドファイル、出力ファイル +# 用例: $(call time_cmd,file_cmd,file_out) +define time_cmd + if test ! -x $1; then $(CHMOD) u+x $1; fi + ($(TIME) ./$1 1>$(DEV_NULL) 2>$(DEV_NULL)) 2>&1 | $(GREP) '^real' >$2 +endef + +# テスト実行コマンド。 +# コマンドファイルを実行し、標準出力を指定されたファイルに保存。 +# エラー発生時は、エラー出力を出力ファイルとエラーファイルに保存。 +# 引数は、コマンドファイル、出力ファイル、エラーファイル +# 用例: $(call exec_cmd,file_cmd,file_out,file_err) +define exec_cmd + if test ! -x $1; then $(CHMOD) u+x $1; fi + ./$1 >>$2 2>$3 + if test -s $3; then $(CAT) $3 >>$2; fi + $(CP) $2 $2.tmp && $(SED) -e "s%$(CURRDIR)%\$$PWD%g" $2.tmp >$2 && $(RM) $2.tmp + $(call rm_null,$3) +endef + +# 2つのファイルを比較し、差分ファイルを作成 +# 引数は、2ファイルのリスト、差分ファイル +# 用例: $(call diff_files,files,file_out) +define diff_files + $(DIFF) $1 >$2 2>&1 + $(call rm_null,$2) +endef + +# 差分ファイルの内容をログファイルに出力 +# 引数は、テスト名、差分ファイル、ログファイル +# 用例: $(call test_log,name,file_diff,file_log) +define test_log + $(call desc_log,$3) + if test ! -s $2; then RES=Success; else RES=Failure; fi; $(ECHO) "$1: Test $$RES $(DATE)" >>$3 + $(ECHO) "Details in $(CURRDIR)/$(DETAIL_FILE)" >>$3 +endef + +# NODISPが設定されていない時は、ログファイルを表示 +# 引数は、ログファイル +# 用例: $(call disp_test_log,file_log) +define disp_test_log + $(if $(NODISP),,$(CAT) $1) +endef + +# ファイル群から、ファイル名とファイルの内容を指定されたファイルに出力 +# 引数は、対象ファイル群、出力ファイル +# 用例: $(call report_files,list_file_target,file_out) +define report_files + $(foreach tfile,$1,$(call report_file,$(tfile),$2)) +endef + +# ファイル名とファイルの内容を指定されたファイルに出力 +# 引数は、対象ファイル、出力ファイル +# 用例: $(call report_file,file_target,file_out) +define report_file + $(call chk_var_null,$1) + if test -s $1; then $(ECHO) "== $1 ==" >>$2; $(call echo_hr,$2); cat $1 >>$2; $(call echo_hr,$2); $(ECHO) >>$2; fi +endef + +define echo_hr + $(ECHO) "----------------------------------------------------------------------" >>$1 +endef + # テスト名。カレントディレクトリー名から取得 TEST = $(notdir $(CURRDIR)) # コマンドファイルのソース -CMDSRC_FILE = $(CMD_FILE).c +CMDSRC_FILE ?= $(CMD_FILE) -.PHONY: check set reset time cleantime clean cleanall +.PHONY: check set reset clean cleanall time cleantime check: clean $(DETAIL_FILE) @$(call disp_test_log,$(LOG_FILE)) @@ -31,15 +112,13 @@ checkall: clean $(DETAIL_FILE) $(TIME_FILE) @$(call disp_test_log,$(LOG_FILE)) set: $(CMD_FILE) + @$(call chk_file_ext,$(TEST0_FILE)) @-$(call exec_cmd,$(CMD_FILE),$(TEST0_FILE),$(ERR_FILE)) @$(CAT) $(TEST0_FILE) -reset: cleanall set - -time: cleantime $(TIME_FILE) - -cleantime: - @$(RM) $(TIME_FILE) +reset: cleanall $(CMD_FILE) + @-$(call exec_cmd,$(CMD_FILE),$(TEST0_FILE),$(ERR_FILE)) + @$(CAT) $(TEST0_FILE) clean: @$(RM) $(TEST_RES_FILES) @@ -47,6 +126,12 @@ clean: cleanall: clean @$(RM) $(TEST0_FILE) +time: cleantime $(TIME_FILE) + @$(CAT) $(TIME_FILE) + +cleantime: + @$(RM) $(TIME_FILE) + $(TEST1_FILE): $(CMD_FILE) @-$(call exec_cmd,$^,$@,$(ERR_FILE)) @@ -60,4 +145,4 @@ $(DETAIL_FILE): $(LOG_FILE) @$(call report_files,$(LOG_FILE) $(CMDSRC_FILE) $(TEST0_FILE) $(ERR_FILE) $(DIFF_FILE) $(TEST1_FILE),$@) $(TIME_FILE): $(CMD_FILE) - $(call time_cmd,$^,$@) + @$(call time_cmd,$^,$@)