2

I have written this simple function and I want to stub out the last functions so I don't actually launch a process.

(defun dc-test-stub-process (command &rest params)
  (interactive)
  (let ((commands (append (list dc-buffer-name dc-docker-cmd command) params)))
    (apply #'dc-process commands)))

I have been playing around with this expecting it to fail because it returns "test" how ever it just passes and it looks like dc-process is actually called and not stubbed out.

(ert-deftest pp-test-process-function ()
  (setq dc-current-buffer "/")
  (cl-letf (((symbol-function 'dc-process)
             (lambda (&rest _) "test"))
    (should (equal (dc-test-stub-process "a" "d" "c") "test2")))))

I have also tried doing the same but stubbing out apply with the same results.

Anyone able to spot what's wrong? There is not much info on using cl-letf but most likely I am doing something wrong as I am still learning lisp.

  • Hint: try indenting your code with C-M-\ – npostavs Oct 16 '17 at 11:15
  • I did not know about that shortcut although i says indented my highlighted region and nothing actually changes, so i guess it likes the indent ? it does tend to auto indent as I hit return. – Oly Oct 16 '17 at 11:24
  • 1
    Did you highlight the whole thing? You have a paren in the wrong place, indenting should definitely make changes that will it more obvious. – npostavs Oct 16 '17 at 11:48
  • not sure whats happening, if i change the indent level and the do C-M-\ it restores the indent to how it was previous so my emacs thinks its correct for some reason :/ – Oly Oct 16 '17 at 12:02
  • your exactly right on the paren, i have it working but now i want to know why my indent does not work or if there is some kind of reformat my code function i can use. – Oly Oct 16 '17 at 12:15
2

If you indent your code, you should end up with the following:

(ert-deftest pp-test-process-function ()
  (setq dc-current-buffer "/")
  (cl-letf
      (((symbol-function 'dc-process)
        (lambda (&rest params) "test"))
       (should (equal (dc-test-stub-process "a" "d" "c") "test2")))))

Which should make clear that you are actually binding a variable called should to the result of (equal (dc-test-stub-process "a" "d" "c") "test2"). Since you use cl-letf and not cl-letf*, the binding for (symbol-function 'dc-process) is not in effect during the next binding.

It can be even more obvious if you put the put first binding on the same line as the cl-letf:

(ert-deftest pp-test-process-function ()
  (setq dc-current-buffer "/")
  (cl-letf (((symbol-function 'dc-process)
             (lambda (&rest params) "test"))
            (should (equal (dc-test-stub-process "a" "d" "c") "test2")))))

Correcting the parentheses so that you end up with your original indentation should give the expected behaviour, i.e., that (should (equal ...)) is the body of the cl-letf so it runs with the dc-process binding in effect:

(ert-deftest pp-test-process-function ()
  (setq dc-current-buffer "/")
  (cl-letf (((symbol-function 'dc-process)
             (lambda (&rest params) "test")))
    (should (equal (dc-test-stub-process "a" "d" "c") "test2"))))
| improve this answer | |
  • thanks for the assistance I will reformat with your suggestions :) – Oly Oct 16 '17 at 15:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.