【问题标题】:Using test bench code from Practical Common Lisp使用来自 Practical Common Lisp 的测试台代码
【发布时间】: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


【解决方案1】:

这只是一个关于format 控制的问题,因为函数format 会在您使用的代码中进行输出。

相应的 Common Lisp Hyperspec 文档位于 FORMAT Printer Operations

* (format t "~a" ''(a #\a))
'(A a)
NIL

* (format t "~s" ''(a #\a))
'(A #\a)
NIL

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-03
    • 1970-01-01
    • 2011-09-05
    • 2011-02-04
    • 2011-06-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多