unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
blob 65577ad9f43aeae302325c58746774cea54e5181 8350 bytes (raw)
name: test/automated/advice-tests.el 	 # note: path name is non-authoritative(*)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
 
;;; advice-tests.el --- Test suite for the new advice thingy.

;; Copyright (C) 2012-2013 Free Software Foundation, Inc.

;; This file is part of GNU Emacs.

;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.

;;; Commentary:

;;; Code:

(require 'ert)

(defun clear-advice (symbol)
  "Reset SYMBOL's function to its original unadvised definition."
  (let ((func (symbol-function symbol)))
    (while (advice--p func)
      (setq func (advice--cdr func)))
    (fset symbol func)))

(defmacro post-restore-func (func &rest body)
  (let ((fdef (symbol-function func)))
    `(unwind-protect
	 (progn ,@body)
       (fset ',func ,fdef))))
(put 'post-restore-func 'lisp-indent-function
     (get 'prog1 'lisp-indent-function))

(ert-deftest advice-tests-nadvice ()
  "Test nadvice code."
  (defun sm-test1 (x) (+ x 4))
  (should (equal (sm-test1 6) 10))
  (advice-add 'sm-test1 :around (lambda (f y) (* (funcall f y) 5)))
  (should (equal (sm-test1 6) 50))
  (defun sm-test1 (x) (+ x 14))
  (should (equal (sm-test1 6) 100))
  (should (equal (null (get 'sm-test1 'defalias-fset-function)) nil))
  (advice-remove 'sm-test1 (lambda (f y) (* (funcall f y) 5)))
  (should (equal (sm-test1 6) 20))
  (should (equal (get 'sm-test1 'defalias-fset-function) nil))

  (advice-add 'sm-test3 :around
              (lambda (f &rest args) `(toto ,(apply f args)))
              '((name . wrap-with-toto)))
  (defmacro sm-test3 (x) `(call-test3 ,x))
  (should (equal (macroexpand '(sm-test3 56)) '(toto (call-test3 56)))))

(ert-deftest advice-tests-advice ()
  "Test advice code."
  (defun sm-test2 (x) (+ x 4))
  (should (equal (sm-test2 6) 10))
  (defadvice sm-test2 (around sm-test activate)
    ad-do-it (setq ad-return-value (* ad-return-value 5)))
  (should (equal (sm-test2 6) 50))
  (ad-deactivate 'sm-test2)
  (should (equal (sm-test2 6) 10))
  (ad-activate 'sm-test2)
  (should (equal (sm-test2 6) 50))
  (defun sm-test2 (x) (+ x 14))
  (should (equal (sm-test2 6) 100))
  (should (equal (null (get 'sm-test2 'defalias-fset-function)) nil))
  (ad-remove-advice 'sm-test2 'around 'sm-test)
  (should (equal (sm-test2 6) 100))
  (ad-activate 'sm-test2)
  (should (equal (sm-test2 6) 20))
  (should (equal (null (get 'sm-test2 'defalias-fset-function)) t))

  (defadvice sm-test4 (around wrap-with-toto activate)
    ad-do-it (setq ad-return-value `(toto ,ad-return-value)))
  (defmacro sm-test4 (x) `(call-test4 ,x))
  (should (equal (macroexpand '(sm-test4 56)) '(toto (call-test4 56))))
  (defmacro sm-test4 (x) `(call-testq ,x))
  (should (equal (macroexpand '(sm-test4 56)) '(toto (call-testq 56))))

  ;; This used to signal an error (bug#12858).
  (autoload 'sm-test6 "foo")
  (defadvice sm-test6 (around test activate)
    ad-do-it))

(ert-deftest advice-tests-combination ()
  "Combining old style and new style advices."
  (defun sm-test5 (x) (+ x 4))
  (should (equal (sm-test5 6) 10))
  (advice-add 'sm-test5 :around (lambda (f y) (* (funcall f y) 5)))
  (should (equal (sm-test5 6) 50))
  (defadvice sm-test5 (around test activate)
    ad-do-it (setq ad-return-value (+ ad-return-value 0.1)))
  (should (equal (sm-test5 5) 45.1))
  (ad-deactivate 'sm-test5)
  (should (equal (sm-test5 6) 50))
  (ad-activate 'sm-test5)
  (should (equal (sm-test5 6) 50.1))
  (defun sm-test5 (x) (+ x 14))
  (should (equal (sm-test5 6) 100.1))
  (advice-remove 'sm-test5 (lambda (f y) (* (funcall f y) 5)))
  (should (equal (sm-test5 6) 20.1)))

(ert-deftest advice-test-called-interactively-p ()
  "Check interaction between advice and called-interactively-p."
  (defun sm-test7 (&optional x) (interactive) (+ (or x 7) 4))
  (advice-add 'sm-test7 :around
              (lambda (f &rest args)
                (list (cons 1 (called-interactively-p)) (apply f args))))
  (should (equal (sm-test7) '((1 . nil) 11)))
  (should (equal (call-interactively 'sm-test7) '((1 . t) 11)))
  (let ((smi 7))
    (advice-add 'sm-test7 :before
                (lambda (&rest args)
                  (setq smi (called-interactively-p))))
    (should (equal (list (sm-test7) smi)
                   '(((1 . nil) 11) nil)))
    (should (equal (list (call-interactively 'sm-test7) smi)
                   '(((1 . t) 11) t))))
  (advice-add 'sm-test7 :around
              (lambda (f &rest args)
                (cons (cons 2 (called-interactively-p)) (apply f args))))
  (should (equal (call-interactively 'sm-test7) '((2 . t) (1 . t) 11))))

(ert-deftest advice-test-called-interactively-p-2 ()
  "Check interaction between around advice and called-interactively-p.

This tests the currently broken case of the innermost advice to a
function being an around advice."
  :expected-result :failed
  (defun sm-test7.2 () (interactive) (cons 1 (called-interactively-p)))
  (clear-advice 'sm-test7.2)
  (advice-add 'sm-test7.2 :around
              (lambda (f &rest args)
                (list (cons 1 (called-interactively-p)) (apply f args))))
  (advice-add 'sm-test7.2 :before #'ignore)
  (advice-add 'sm-test7.2 :after #'ignore)
  ;(advice-add 'sm-test7.2 :filter-args #'list)
  ;(advice-add 'sm-test7.2 :filter-return #'identity)
  (should (equal (sm-test7.2) '((1 . nil) (1 . nil))))
  (should (equal (call-interactively 'sm-test7.2) '((1 . t) (1 . t)))))

(ert-deftest advice-test-called-interactively-p-3 ()
  "Check interaction between before advice and called-interactively-p.

This tests the case of the innermost advice being before"
  (defun sm-test7.3 () (interactive) (cons 1 (called-interactively-p)))
  (advice-add 'sm-test7.3 :before #'ignore)
  ;(advice-add 'sm-test7.3 :filter-args #'list)
  ;(advice-add 'sm-test7.3 :filter-return #'identity)
  (should (equal (sm-test7.3) '(1 . nil)))
  (should (equal (call-interactively 'sm-test7.3) '(1 . t))))

(ert-deftest advice-test-called-interactively-p-4 ()
  "Check interaction between advice on call-interactively and called-interactively-p.

This tests the case where call-interactively itself is advised,
which is currently broken."
  :expected-result :failed
  (defun sm-test7.4 () (interactive) (cons 1 (called-interactively-p)))
  (post-restore-func call-interactively
    (advice-add 'call-interactively :before #'ignore)
    (should (equal (sm-test7.4) '(1 . nil)))
    (should (equal (call-interactively 'sm-test7.4) '(1 . t)))))

(ert-deftest advice-test-called-interactively-p-5 ()
  "Check interaction between non-innermost around advice and called-interactively-p.

This tests the case where a function has around advice, but it is
not the innermost advice."
  (defun sm-test7.5 () (interactive) (cons 1 (called-interactively-p)))
  (advice-add 'sm-test7.5 :before #'ignore)
  (advice-add 'sm-test7.5 :around #'funcall)
  ;(advice-add 'sm-test7.5 :filter-args #'list)
  ;(advice-add 'sm-test7.5 :filter-return #'identity)
  (should (equal (sm-test7.5) '(1 . nil)))
  (should (equal (call-interactively 'sm-test7.5) '(1 . t))))

(ert-deftest advice-test-interactive ()
  "Check handling of interactive spec."
  (defun sm-test8 (a) (interactive "p") a)
  (defadvice sm-test8 (before adv1 activate) nil)
  (defadvice sm-test8 (before adv2 activate) (interactive "P") nil)
  (should (equal (interactive-form 'sm-test8) '(interactive "P"))))

(ert-deftest advice-test-preactivate ()
  (should (equal (null (get 'sm-test9 'defalias-fset-function)) t))
  (defun sm-test9 (a) (interactive "p") a)
  (should (equal (null (get 'sm-test9 'defalias-fset-function)) t))
  (defadvice sm-test9 (before adv1 pre act protect compile) nil)
  (should (equal (null (get 'sm-test9 'defalias-fset-function)) nil))
  (defadvice sm-test9 (before adv2 pre act protect compile)
    (interactive "P") nil)
  (should (equal (interactive-form 'sm-test9) '(interactive "P"))))

;; Local Variables:
;; no-byte-compile: t
;; End:

;;; advice-tests.el ends here.

debug log:

solving 65577ad ...
found 65577ad in https://yhetil.org/emacs-bugs/523B73CF.4080105@thompsonclan.org/
found 69c15e3 in https://git.savannah.gnu.org/cgit/emacs.git
preparing index
index prepared:
100644 69c15e34ed00af74a0ceb43d002d63451bde9c0c	test/automated/advice-tests.el

applying [1/1] https://yhetil.org/emacs-bugs/523B73CF.4080105@thompsonclan.org/
diff --git a/test/automated/advice-tests.el b/test/automated/advice-tests.el
index 69c15e3..65577ad 100644

Checking patch test/automated/advice-tests.el...
Applied patch test/automated/advice-tests.el cleanly.

index at:
100644 65577ad9f43aeae302325c58746774cea54e5181	test/automated/advice-tests.el

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).