;;; advice-tests.el --- Test suite for the new advice thingy.
-;; Copyright (C) 2012-2013 Free Software Foundation, Inc.
+;; Copyright (C) 2012-2014 Free Software Foundation, Inc.
;; This file is part of GNU Emacs.
;;; Code:
+(require 'ert)
+
(ert-deftest advice-tests-nadvice ()
"Test nadvice code."
+ (advice-add 'sm-test1 :around (lambda (f y) (* (funcall f y) 5)))
+ (advice-add 'sm-test1 :around (lambda (f y) (* (funcall f y) 2)))
+ (advice-remove 'sm-test1 (lambda (f y) (* (funcall f y) 5)))
(defun sm-test1 (x) (+ x 4))
+ (should (equal (sm-test1 6) 20))
+ (advice-remove 'sm-test1 (lambda (f y) (* (funcall f y) 2)))
(should (equal (sm-test1 6) 10))
(advice-add 'sm-test1 :around (lambda (f y) (* (funcall f y) 5)))
(should (equal (sm-test1 6) 50))
(defmacro sm-test3 (x) `(call-test3 ,x))
(should (equal (macroexpand '(sm-test3 56)) '(toto (call-test3 56)))))
+(ert-deftest advice-tests-macroaliases ()
+ "Test nadvice code on aliases to macros."
+ (defmacro sm-test1 (a) `(list ',a))
+ (defalias 'sm-test1-alias 'sm-test1)
+ (should (equal (macroexpand '(sm-test1-alias 5)) '(list '5)))
+ (advice-add 'sm-test1-alias :around
+ (lambda (f &rest args) `(cons 1 ,(apply f args))))
+ (should (equal (macroexpand '(sm-test1-alias 5)) '(cons 1 (list '5))))
+ (defmacro sm-test1 (a) `(list 0 ',a))
+ (should (equal (macroexpand '(sm-test1-alias 5)) '(cons 1 (list 0 '5)))))
+
+
(ert-deftest advice-tests-advice ()
"Test advice code."
(defun sm-test2 (x) (+ x 4))
(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-around ()
+ "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)))
+ (advice-add 'sm-test7.2 :around
+ (lambda (f &rest args)
+ (list (cons 1 (called-interactively-p)) (apply f args))))
+ (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-filter-args ()
+ "Check interaction between filter-args advice and called-interactively-p."
+ :expected-result :failed
+ (defun sm-test7.3 () (interactive) (cons 1 (called-interactively-p)))
+ (advice-add 'sm-test7.3 :filter-args #'list)
+ (should (equal (sm-test7.3) '(1 . nil)))
+ (should (equal (call-interactively 'sm-test7.3) '(1 . t))))
+
+(ert-deftest advice-test-call-interactively ()
+ "Check interaction between advice on call-interactively and called-interactively-p."
+ (defun sm-test7.4 () (interactive) (cons 1 (called-interactively-p)))
+ (let ((old (symbol-function 'call-interactively)))
+ (unwind-protect
+ (progn
+ (advice-add 'call-interactively :before #'ignore)
+ (should (equal (sm-test7.4) '(1 . nil)))
+ (should (equal (call-interactively 'sm-test7.4) '(1 . t))))
+ (fset 'call-interactively old))))
+
(ert-deftest advice-test-interactive ()
"Check handling of interactive spec."
(defun sm-test8 (a) (interactive "p") a)