如何对以活动区域为输入的函数进行单元测试?

时间:2013-08-06 22:58:24

标签: unit-testing emacs elisp

例如,这里有两个版本的函数来计算区域或字符串中“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

2 个答案:

答案 0 :(得分:3)

我会做这样的事情:

(with-temp-buffer
  (insert ....) ; prepare the buffer
  (should (equal ... (foo (point-min) (point-max)))))

答案 1 :(得分:3)

我会提出第二个@sds的建议,并附加建议:

(with-temp-buffer
  (insert <text>)
  (set-mark <markpos>)
  (goto-char <otherend>)
  (should (equal (call-interactively 'foo) <n>)))