* lisp/emacs-lisp/advice.el (ad-preactivate-advice): Adjust the cleanup to
[bpt/emacs.git] / test / automated / advice-tests.el
CommitLineData
231d8498
SM
1;;; advice-tests.el --- Test suite for the new advice thingy.
2
09b8afb6 3;; Copyright (C) 2012-2013 Free Software Foundation, Inc.
231d8498
SM
4
5;; This file is part of GNU Emacs.
6
7;; GNU Emacs is free software: you can redistribute it and/or modify
8;; it under the terms of the GNU General Public License as published by
9;; the Free Software Foundation, either version 3 of the License, or
10;; (at your option) any later version.
11
12;; GNU Emacs is distributed in the hope that it will be useful,
13;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15;; GNU General Public License for more details.
16
17;; You should have received a copy of the GNU General Public License
18;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
19
20;;; Commentary:
21
22;;; Code:
23
cb9c0a53
SM
24(ert-deftest advice-tests-nadvice ()
25 "Test nadvice code."
26 (defun sm-test1 (x) (+ x 4))
27 (should (equal (sm-test1 6) 10))
28 (advice-add 'sm-test1 :around (lambda (f y) (* (funcall f y) 5)))
29 (should (equal (sm-test1 6) 50))
30 (defun sm-test1 (x) (+ x 14))
31 (should (equal (sm-test1 6) 100))
32 (should (equal (null (get 'sm-test1 'defalias-fset-function)) nil))
33 (advice-remove 'sm-test1 (lambda (f y) (* (funcall f y) 5)))
34 (should (equal (sm-test1 6) 20))
35 (should (equal (get 'sm-test1 'defalias-fset-function) nil))
36
37 (advice-add 'sm-test3 :around
38 (lambda (f &rest args) `(toto ,(apply f args)))
39 '((name . wrap-with-toto)))
40 (defmacro sm-test3 (x) `(call-test3 ,x))
41 (should (equal (macroexpand '(sm-test3 56)) '(toto (call-test3 56)))))
42
43(ert-deftest advice-tests-advice ()
23ba2705 44 "Test advice code."
cb9c0a53
SM
45 (defun sm-test2 (x) (+ x 4))
46 (should (equal (sm-test2 6) 10))
47 (defadvice sm-test2 (around sm-test activate)
48 ad-do-it (setq ad-return-value (* ad-return-value 5)))
49 (should (equal (sm-test2 6) 50))
50 (ad-deactivate 'sm-test2)
51 (should (equal (sm-test2 6) 10))
52 (ad-activate 'sm-test2)
53 (should (equal (sm-test2 6) 50))
54 (defun sm-test2 (x) (+ x 14))
55 (should (equal (sm-test2 6) 100))
56 (should (equal (null (get 'sm-test2 'defalias-fset-function)) nil))
57 (ad-remove-advice 'sm-test2 'around 'sm-test)
58 (should (equal (sm-test2 6) 100))
59 (ad-activate 'sm-test2)
60 (should (equal (sm-test2 6) 20))
61 (should (equal (null (get 'sm-test2 'defalias-fset-function)) t))
62
63 (defadvice sm-test4 (around wrap-with-toto activate)
64 ad-do-it (setq ad-return-value `(toto ,ad-return-value)))
65 (defmacro sm-test4 (x) `(call-test4 ,x))
66 (should (equal (macroexpand '(sm-test4 56)) '(toto (call-test4 56))))
67 (defmacro sm-test4 (x) `(call-testq ,x))
68 (should (equal (macroexpand '(sm-test4 56)) '(toto (call-testq 56))))
69
70 ;; This used to signal an error (bug#12858).
71 (autoload 'sm-test6 "foo")
72 (defadvice sm-test6 (around test activate)
73 ad-do-it))
74
75(ert-deftest advice-tests-combination ()
76 "Combining old style and new style advices."
77 (defun sm-test5 (x) (+ x 4))
78 (should (equal (sm-test5 6) 10))
79 (advice-add 'sm-test5 :around (lambda (f y) (* (funcall f y) 5)))
80 (should (equal (sm-test5 6) 50))
81 (defadvice sm-test5 (around test activate)
82 ad-do-it (setq ad-return-value (+ ad-return-value 0.1)))
83 (should (equal (sm-test5 5) 45.1))
84 (ad-deactivate 'sm-test5)
85 (should (equal (sm-test5 6) 50))
86 (ad-activate 'sm-test5)
87 (should (equal (sm-test5 6) 50.1))
88 (defun sm-test5 (x) (+ x 14))
89 (should (equal (sm-test5 6) 100.1))
90 (advice-remove 'sm-test5 (lambda (f y) (* (funcall f y) 5)))
91 (should (equal (sm-test5 6) 20.1)))
92
93(ert-deftest advice-test-called-interactively-p ()
94 "Check interaction between advice and called-interactively-p."
95 (defun sm-test7 (&optional x) (interactive) (+ (or x 7) 4))
96 (advice-add 'sm-test7 :around
97 (lambda (f &rest args)
98 (list (cons 1 (called-interactively-p)) (apply f args))))
99 (should (equal (sm-test7) '((1 . nil) 11)))
100 (should (equal (call-interactively 'sm-test7) '((1 . t) 11)))
101 (let ((smi 7))
102 (advice-add 'sm-test7 :before
103 (lambda (&rest args)
104 (setq smi (called-interactively-p))))
105 (should (equal (list (sm-test7) smi)
106 '(((1 . nil) 11) nil)))
107 (should (equal (list (call-interactively 'sm-test7) smi)
108 '(((1 . t) 11) t))))
109 (advice-add 'sm-test7 :around
110 (lambda (f &rest args)
111 (cons (cons 2 (called-interactively-p)) (apply f args))))
112 (should (equal (call-interactively 'sm-test7) '((2 . t) (1 . t) 11))))
113
114(ert-deftest advice-test-interactive ()
115 "Check handling of interactive spec."
116 (defun sm-test8 (a) (interactive "p") a)
117 (defadvice sm-test8 (before adv1 activate) nil)
118 (defadvice sm-test8 (before adv2 activate) (interactive "P") nil)
119 (should (equal (interactive-form 'sm-test8) '(interactive "P"))))
120
121(ert-deftest advice-test-preactivate ()
122 (should (equal (null (get 'sm-test9 'defalias-fset-function)) t))
123 (defun sm-test9 (a) (interactive "p") a)
124 (should (equal (null (get 'sm-test9 'defalias-fset-function)) t))
125 (defadvice sm-test9 (before adv1 pre act protect compile) nil)
126 (should (equal (null (get 'sm-test9 'defalias-fset-function)) nil))
127 (defadvice sm-test9 (before adv2 pre act protect compile)
128 (interactive "P") nil)
129 (should (equal (interactive-form 'sm-test9) '(interactive "P"))))
231d8498 130
231d8498
SM
131;; Local Variables:
132;; no-byte-compile: t
133;; End:
134
135;;; advice-tests.el ends here.