【发布时间】:2017-03-25 04:47:03
【问题描述】:
In Practical Common Lisp Ch. 9,Peter Seibel 提供了一个基本的单元测试平台,用于比较评估 S 表达式的预期结果和实际结果。例如,将测试定义为 (deftest plus-test () (check (= (+ 1 2) 3))) 并评估 (plus-test) 将打印结果 pass ... (PLUS-TEST): (= (+ 1 2) 3)。然而,像(deftest cdr-test () (check (equal (cdr '(a |a| "a" #\a)) '(|a| "a" #\a) 这样稍微复杂一点的例子会产生pass ... (CDR-TEST): (equal (cdr '(A a a a)) '(a a a)) 而不是pass ... (CDR-TEST): (equal (cdr '(a |a| "a" #\a)) '(|a| "a" #\a))。我无法成功修改他的代码以打印所需的结果,希望能得到一些帮助。这是他来自 Ch 的代码。 9:
(defmacro with-gensyms ((&rest names) &body body)
`(let ,(loop for n in names collect `(,n (make-symbol ,(string n))))
,@body))
(defvar *test-name* nil)
(defmacro deftest (name parameters &body body)
"Define a test function. Within a test function we can call other
test functions or use `check' to run individual test cases."
`(defun ,name ,parameters
(let ((*test-name* (append *test-name* (list ',name))))
,@body)))
(defmacro check (&body forms)
"Run each expression in `forms' as a test case."
`(combine-results
,@(loop for f in forms collect `(report-result ,f ',f))))
(defmacro combine-results (&body forms)
"Combine the results (as booleans) of evaluating `forms' in order."
(with-gensyms (result)
`(let ((,result t))
,@(loop for f in forms collect `(unless ,f (setf ,result nil)))
,result)))
(defun report-result (result form)
"Report the results of a single test case. Called by `check'."
(format t "~:[FAIL~;pass~] ... ~a: ~a~%" result *test-name* form)
result)
【问题讨论】:
-
需要将FORMAT中的
~a改为~s。
标签: format common-lisp