4

例如,这里有两个版本的函数来计算区域或字符串中“a”的实例:

(defun foo (beg end)
  (interactive "r")
  (let ((count 0))
    (save-excursion
      (while (/= (point) end)
        (if (equal (char-after) ?a)
          (setq count (1+ count)))
        (forward-char)))
  count))

(defun foo1 (str)
  (let ((count 0))
    (mapcar #'(lambda (x) (if (equal x ?a) (setq count (1+ count))))
            str)
  count))

这是检查功能的测试foo1

(require 'ert)
(ert-deftest foo-test ()
  (should (equal (foo1 "aba") 2)))

但是如何使用单元测试框架测试foo将区域作为输入的功能?ert

4

2 回答 2

3

我会做这样的事情:

(with-temp-buffer
  (insert ....) ; prepare the buffer
  (should (equal ... (foo (point-min) (point-max)))))
于 2013-08-07T04:41:04.087 回答
3

我会支持@sds 的建议,并添加以下建议:

(with-temp-buffer
  (insert <text>)
  (set-mark <markpos>)
  (goto-char <otherend>)
  (should (equal (call-interactively 'foo) <n>)))
于 2013-08-07T14:58:42.507 回答