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.

phils
  • 48,657
  • 3
  • 76
  • 115
Oly
  • 583
  • 1
  • 5
  • 15
  • 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
  • What emacs version do you have? If you do `emacs -Q` and then paste the code in `*scratch*` does it also indent the same way? – npostavs Oct 16 '17 at 13:37
  • same behavior, i have just noticed it does adjust by a single space which i had not noticed I am using emacs 25.1.1 perhaps thats all its supposed to do i was expecting something more drastic :) – Oly Oct 16 '17 at 14:34

1 Answers1

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"))))
npostavs
  • 9,033
  • 1
  • 21
  • 53