]> code.delx.au - gnu-emacs/blob - test/automated/advice-tests.el
Merge from emacs-24; up to 2012-12-31T11:35:13Z!rudalics@gmx.at
[gnu-emacs] / test / automated / advice-tests.el
1 ;;; advice-tests.el --- Test suite for the new advice thingy.
2
3 ;; Copyright (C) 2012-2013 Free Software Foundation, Inc.
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
24 (require 'ert)
25
26 (ert-deftest advice-tests-nadvice ()
27 "Test nadvice code."
28 (defun sm-test1 (x) (+ x 4))
29 (should (equal (sm-test1 6) 10))
30 (advice-add 'sm-test1 :around (lambda (f y) (* (funcall f y) 5)))
31 (should (equal (sm-test1 6) 50))
32 (defun sm-test1 (x) (+ x 14))
33 (should (equal (sm-test1 6) 100))
34 (should (equal (null (get 'sm-test1 'defalias-fset-function)) nil))
35 (advice-remove 'sm-test1 (lambda (f y) (* (funcall f y) 5)))
36 (should (equal (sm-test1 6) 20))
37 (should (equal (get 'sm-test1 'defalias-fset-function) nil))
38
39 (advice-add 'sm-test3 :around
40 (lambda (f &rest args) `(toto ,(apply f args)))
41 '((name . wrap-with-toto)))
42 (defmacro sm-test3 (x) `(call-test3 ,x))
43 (should (equal (macroexpand '(sm-test3 56)) '(toto (call-test3 56)))))
44
45 (ert-deftest advice-tests-advice ()
46 "Test advice code."
47 (defun sm-test2 (x) (+ x 4))
48 (should (equal (sm-test2 6) 10))
49 (defadvice sm-test2 (around sm-test activate)
50 ad-do-it (setq ad-return-value (* ad-return-value 5)))
51 (should (equal (sm-test2 6) 50))
52 (ad-deactivate 'sm-test2)
53 (should (equal (sm-test2 6) 10))
54 (ad-activate 'sm-test2)
55 (should (equal (sm-test2 6) 50))
56 (defun sm-test2 (x) (+ x 14))
57 (should (equal (sm-test2 6) 100))
58 (should (equal (null (get 'sm-test2 'defalias-fset-function)) nil))
59 (ad-remove-advice 'sm-test2 'around 'sm-test)
60 (should (equal (sm-test2 6) 100))
61 (ad-activate 'sm-test2)
62 (should (equal (sm-test2 6) 20))
63 (should (equal (null (get 'sm-test2 'defalias-fset-function)) t))
64
65 (defadvice sm-test4 (around wrap-with-toto activate)
66 ad-do-it (setq ad-return-value `(toto ,ad-return-value)))
67 (defmacro sm-test4 (x) `(call-test4 ,x))
68 (should (equal (macroexpand '(sm-test4 56)) '(toto (call-test4 56))))
69 (defmacro sm-test4 (x) `(call-testq ,x))
70 (should (equal (macroexpand '(sm-test4 56)) '(toto (call-testq 56))))
71
72 ;; This used to signal an error (bug#12858).
73 (autoload 'sm-test6 "foo")
74 (defadvice sm-test6 (around test activate)
75 ad-do-it))
76
77 (ert-deftest advice-tests-combination ()
78 "Combining old style and new style advices."
79 (defun sm-test5 (x) (+ x 4))
80 (should (equal (sm-test5 6) 10))
81 (advice-add 'sm-test5 :around (lambda (f y) (* (funcall f y) 5)))
82 (should (equal (sm-test5 6) 50))
83 (defadvice sm-test5 (around test activate)
84 ad-do-it (setq ad-return-value (+ ad-return-value 0.1)))
85 (should (equal (sm-test5 5) 45.1))
86 (ad-deactivate 'sm-test5)
87 (should (equal (sm-test5 6) 50))
88 (ad-activate 'sm-test5)
89 (should (equal (sm-test5 6) 50.1))
90 (defun sm-test5 (x) (+ x 14))
91 (should (equal (sm-test5 6) 100.1))
92 (advice-remove 'sm-test5 (lambda (f y) (* (funcall f y) 5)))
93 (should (equal (sm-test5 6) 20.1)))
94
95 (ert-deftest advice-test-called-interactively-p ()
96 "Check interaction between advice and called-interactively-p."
97 (defun sm-test7 (&optional x) (interactive) (+ (or x 7) 4))
98 (advice-add 'sm-test7 :around
99 (lambda (f &rest args)
100 (list (cons 1 (called-interactively-p)) (apply f args))))
101 (should (equal (sm-test7) '((1 . nil) 11)))
102 (should (equal (call-interactively 'sm-test7) '((1 . t) 11)))
103 (let ((smi 7))
104 (advice-add 'sm-test7 :before
105 (lambda (&rest args)
106 (setq smi (called-interactively-p))))
107 (should (equal (list (sm-test7) smi)
108 '(((1 . nil) 11) nil)))
109 (should (equal (list (call-interactively 'sm-test7) smi)
110 '(((1 . t) 11) t))))
111 (advice-add 'sm-test7 :around
112 (lambda (f &rest args)
113 (cons (cons 2 (called-interactively-p)) (apply f args))))
114 (should (equal (call-interactively 'sm-test7) '((2 . t) (1 . t) 11))))
115
116 (ert-deftest advice-test-interactive ()
117 "Check handling of interactive spec."
118 (defun sm-test8 (a) (interactive "p") a)
119 (defadvice sm-test8 (before adv1 activate) nil)
120 (defadvice sm-test8 (before adv2 activate) (interactive "P") nil)
121 (should (equal (interactive-form 'sm-test8) '(interactive "P"))))
122
123 (ert-deftest advice-test-preactivate ()
124 (should (equal (null (get 'sm-test9 'defalias-fset-function)) t))
125 (defun sm-test9 (a) (interactive "p") a)
126 (should (equal (null (get 'sm-test9 'defalias-fset-function)) t))
127 (defadvice sm-test9 (before adv1 pre act protect compile) nil)
128 (should (equal (null (get 'sm-test9 'defalias-fset-function)) nil))
129 (defadvice sm-test9 (before adv2 pre act protect compile)
130 (interactive "P") nil)
131 (should (equal (interactive-form 'sm-test9) '(interactive "P"))))
132
133 ;; Local Variables:
134 ;; no-byte-compile: t
135 ;; End:
136
137 ;;; advice-tests.el ends here.