How to make unit test functions that take an active area as input?

For example, here are two versions of a function for counting instances of β€œa” in a region or row:

(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)) 

This is a test for checking the function foo1 :

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

but how can I test a function foo that accepts a region as input using the ert unit testing framework?

+4
source share
2 answers

I would prefer a second @sds sentence with the addition of a suggestion to do something like:

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

I would do something like this:

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

Source: https://habr.com/ru/post/1495629/


All Articles