From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Michal Nazarewicz Newsgroups: gmane.emacs.bugs Subject: bug#22632: [PATCH 1/4] Introduce `ert-with-function-mocked' macro Date: Thu, 11 Feb 2016 16:02:32 +0100 Message-ID: <1455202955-25751-1-git-send-email-mina86@mina86.com> References: <1455202258-23963-1-git-send-email-mina86@mina86.com> NNTP-Posting-Host: plane.gmane.org X-Trace: ger.gmane.org 1455203006 2973 80.91.229.3 (11 Feb 2016 15:03:26 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Thu, 11 Feb 2016 15:03:26 +0000 (UTC) To: 22632@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Thu Feb 11 16:03:17 2016 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1aTsm2-0000Yj-FF for geb-bug-gnu-emacs@m.gmane.org; Thu, 11 Feb 2016 16:03:14 +0100 Original-Received: from localhost ([::1]:50782 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aTsm1-00075K-PX for geb-bug-gnu-emacs@m.gmane.org; Thu, 11 Feb 2016 10:03:13 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:48525) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aTslw-00073N-UL for bug-gnu-emacs@gnu.org; Thu, 11 Feb 2016 10:03:10 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aTslr-0001bs-4R for bug-gnu-emacs@gnu.org; Thu, 11 Feb 2016 10:03:08 -0500 Original-Received: from debbugs.gnu.org ([208.118.235.43]:55942) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aTslr-0001bo-10 for bug-gnu-emacs@gnu.org; Thu, 11 Feb 2016 10:03:03 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84) (envelope-from ) id 1aTslq-0000na-RD for bug-gnu-emacs@gnu.org; Thu, 11 Feb 2016 10:03:02 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Michal Nazarewicz Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Thu, 11 Feb 2016 15:03:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 22632 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch Original-Received: via spool by 22632-submit@debbugs.gnu.org id=B22632.14552029743029 (code B ref 22632); Thu, 11 Feb 2016 15:03:02 +0000 Original-Received: (at 22632) by debbugs.gnu.org; 11 Feb 2016 15:02:54 +0000 Original-Received: from localhost ([127.0.0.1]:36844 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84) (envelope-from ) id 1aTsli-0000ml-H7 for submit@debbugs.gnu.org; Thu, 11 Feb 2016 10:02:54 -0500 Original-Received: from mail-wm0-f52.google.com ([74.125.82.52]:35150) by debbugs.gnu.org with esmtp (Exim 4.84) (envelope-from ) id 1aTslg-0000mG-FD for 22632@debbugs.gnu.org; Thu, 11 Feb 2016 10:02:52 -0500 Original-Received: by mail-wm0-f52.google.com with SMTP id c200so77296359wme.0 for <22632@debbugs.gnu.org>; Thu, 11 Feb 2016 07:02:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=sender:from:to:subject:date:message-id:in-reply-to:references; bh=8ohLbfUDLyZqGv2l2q+JmTtguWxR8S2CeitjBSQDtJI=; b=Yed0bXq676vxMEC+Mugm2lemzC6ZoCj2HSlTe0TB4a6u0ROrq88h2O8wzDFFuu4vFq KFE0LWB+w4FnwPIKlh56rwJnKvsBmMH48NlgAua6cMX3bbMEtaq/lWhEKA+nsfQjvAKW Bycneru1fW8at+1dMhsu9LsTIMMjBTShp4LFFiPG3XuJQfsjXZmFFjxI/LB/4cJX1fNh yGpoeAR1YWx5ChP51tNmheqWLC51dhGTnnhxnuTIGSquD2Sr7UI0M3ZV2YlMebW2fbkA UAHp3udijKLJ2l36RpfDbI4PeLtUi3FuwJqjrRnOJeL2of97crYR7C92q1rpZT8FFVPv j8Gg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:sender:from:to:subject:date:message-id :in-reply-to:references; bh=8ohLbfUDLyZqGv2l2q+JmTtguWxR8S2CeitjBSQDtJI=; b=QSddzKY3F1JeG/mzX5lZeudutUr/uSTilbyaIx2Hlh4Zn30J5JB59/weF90H5XzHEZ c0s8bcuTzyULcDnx7OtOdArAwpzwTQPX1NUuS3QcTwkRy8IkDpp6n27iTI6g1BJr8UxS NKVoYt1XWnxtnl5BHu2aAUZrCMzHF2Tt1SKiJLYY2TB/TL72UCrQWoYfoyEWIj1mL5BN TVyi4QDwoWhOUfub/j0/V1XkCKckP7wnmMy4OQx2hNw78t+2yNqopoe9jYHS3JM0qGBk 8rLyIHQo7qEQyWZGWKsp0HlU6+eZx7BWNrseC80cjAPCd4BjtScPPm82E/3mCGre6Y1M vlyA== X-Gm-Message-State: AG10YOSwbixEQSdVxzUS/JMyUrtAjewD/+TcCJMwHiyLmh80vKBetgWJakceUbov+fE7C9pK X-Received: by 10.28.211.141 with SMTP id k135mr17300869wmg.51.1455202966837; Thu, 11 Feb 2016 07:02:46 -0800 (PST) Original-Received: from mpn.zrh.corp.google.com ([2620:0:105f:301:ddd1:edef:c1eb:262b]) by smtp.gmail.com with ESMTPSA id 73sm4757453wmy.22.2016.02.11.07.02.44 for <22632@debbugs.gnu.org> (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 11 Feb 2016 07:02:44 -0800 (PST) Original-Received: by mpn.zrh.corp.google.com (Postfix, from userid 126942) id A44281E027D; Thu, 11 Feb 2016 16:02:43 +0100 (CET) X-Mailer: git-send-email 2.7.0.rc3.207.g0ac5344 In-Reply-To: <1455202258-23963-1-git-send-email-mina86@mina86.com> X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 208.118.235.43 X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.bugs:112889 Archived-At: * lisp/emacs-lisp/ert-x.el (ert-with-function-mocked): New macro which allows evaluating code while particular function is replaced with a mock. The original definition of said function is restored once the macro finishes. --- etc/NEWS | 3 +++ lisp/emacs-lisp/ert-x.el | 40 ++++++++++++++++++++++++++++++++++ test/lisp/emacs-lisp/ert-x-tests.el | 43 +++++++++++++++++++++++++++++++++++++ 3 files changed, 86 insertions(+) diff --git a/etc/NEWS b/etc/NEWS index 37eb2bc..ac418be7 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -88,6 +88,9 @@ different group ID. ** Autoload files can be generated without timestamps, by setting `autoload-timestamps' to nil. +** `ert-with-function-mocked' of 'ert-x package allows mocking of functions +in unit tests. + * Changes in Emacs 25.2 on Non-Free Operating Systems diff --git a/lisp/emacs-lisp/ert-x.el b/lisp/emacs-lisp/ert-x.el index 2a2418f..eb10c84 100644 --- a/lisp/emacs-lisp/ert-x.el +++ b/lisp/emacs-lisp/ert-x.el @@ -285,6 +285,46 @@ ert-buffer-string-reindented (kill-buffer clone))))))) +(defmacro ert-with-function-mocked (name mock &rest body) + "Mocks function NAME with MOCK and run BODY. + +Once BODY finishes (be it normally by returning a value or +abnormally by throwing or signalling), the old definition of +function NAME is restored. + +BODY may further change the mock with `fset'. + +If MOCK is nil, the function NAME is mocked with a function +`ert-fail'ing when called. + +For example: + + ;; Regular use, function is mocked inside the BODY: + (should (eq 2 (+ 1 1))) + (ert-with-function-mocked ((+ (lambda (a b) (- a b)))) + (should (eq 0 (+ 1 1)))) + (should (eq 2 (+ 1 1))) + + ;; Macro correctly recovers from a throw or signal: + (should + (catch 'done + (ert-with-function-mocked ((+ (lambda (a b) (- a b)))) + (should (eq 0 (+ 1 1)))) + (throw 'done t))) + (should (eq 2 (+ 1 1))) +" + (declare (indent 2)) + (let ((old-var (make-symbol "old-var")) + (mock-var (make-symbol "mock-var"))) + `(let ((,old-var (symbol-function (quote ,name))) (,mock-var ,mock)) + (fset (quote ,name) + (or ,mock-var (lambda (&rest _) + (ert-fail (concat "`" ,(symbol-name name) + "' unexpectedly called."))))) + (unwind-protect + (progn ,@body) + (fset (quote ,name) ,old-var))))) + (provide 'ert-x) ;;; ert-x.el ends here diff --git a/test/lisp/emacs-lisp/ert-x-tests.el b/test/lisp/emacs-lisp/ert-x-tests.el index ef8642a..a2665e7 100644 --- a/test/lisp/emacs-lisp/ert-x-tests.el +++ b/test/lisp/emacs-lisp/ert-x-tests.el @@ -275,6 +275,49 @@ ert--hash-table-to-alist (should (equal (c x) (lisp x)))))) +(defun ert--dummy-id (a) + "Identity function. Used for tests only." + a) + +(ert-deftest ert-with-function-mocked () + (let ((mock-id (lambda (_) 21))) + (should (eq 42 (ert--dummy-id 42))) + + (ert-with-function-mocked ert--dummy-id nil + (fset 'ert--dummy-id mock-id) + (should (eq 21 (ert--dummy-id 42)))) + (should (eq 42 (ert--dummy-id 42))) + + (ert-with-function-mocked ert--dummy-id mock-id + (should (eq 21 (ert--dummy-id 42)))) + (should (eq 42 (ert--dummy-id 42))) + + (should + (catch 'exit + (ert-with-function-mocked ert--dummy-id mock-id + (should (eq 21 (ert--dummy-id 42)))) + (throw 'exit t))) + (should (eq 42 (ert--dummy-id 42))) + + (should + (string= "Foo" + (condition-case err + (progn + (ert-with-function-mocked ert--dummy-id mock-id + (should (eq 21 (ert--dummy-id 42)))) + (user-error "Foo")) + (user-error (cadr err))))) + (should (eq 42 (ert--dummy-id 42))) + + (should + (string= "`ert--dummy-id' unexpectedly called." + (condition-case err + (ert-with-function-mocked ert--dummy-id nil + (ert--dummy-id 42)) + (ert-test-failed (cadr err))))) + (should (eq 42 (ert--dummy-id 42))))) + + (provide 'ert-x-tests) ;;; ert-x-tests.el ends here -- 2.7.0.rc3.207.g0ac5344