1;;; macroexp-tests.el --- Tests for macroexp.el -*- lexical-binding: t; -*- 2 3;; Copyright (C) 2021 Free Software Foundation, Inc. 4 5;; Author: Stefan Monnier <monnier@iro.umontreal.ca> 6;; Keywords: 7 8;; This file is part of GNU Emacs. 9 10;; GNU Emacs is free software: you can redistribute it and/or modify 11;; it under the terms of the GNU General Public License as published by 12;; the Free Software Foundation, either version 3 of the License, or 13;; (at your option) any later version. 14 15;; GNU Emacs is distributed in the hope that it will be useful, 16;; but WITHOUT ANY WARRANTY; without even the implied warranty of 17;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18;; GNU General Public License for more details. 19 20;; You should have received a copy of the GNU General Public License 21;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. 22 23;;; Commentary: 24 25;;; Code: 26 27(require 'macroexp) 28(require 'ert-x) 29 30(ert-deftest macroexp--tests-fgrep () 31 (should (equal (macroexp--fgrep '((x) (y)) '([x] z ((u)))) 32 '((x)))) 33 (should (equal (macroexp--fgrep '((x) (y)) '#2=([y] ((y #2#)))) 34 '((y)))) 35 (should (equal (macroexp--fgrep '((x) (y)) '#2=([r] ((a x)) a b c d . #2#)) 36 '((x))))) 37 38(defconst macroexp--tests-filename (macroexp-file-name)) 39 40(defmacro macroexp--test-get-file-name () (macroexp-file-name)) 41 42(ert-deftest macroexp--tests-file-name () 43 (should (string-match 44 "\\`macroexp-tests.elc?\\'" 45 (file-name-nondirectory macroexp--tests-filename))) 46 (let ((rsrc-dir (expand-file-name 47 "macroexp-resources" 48 (file-name-directory macroexp--tests-filename)))) 49 (with-current-buffer 50 (find-file-noselect (expand-file-name "m1.el" rsrc-dir)) 51 (defvar macroexp--m1-tests-filename) 52 (declare-function macroexp--m1-tests-file-name "m1" ()) 53 ;; `macroexp-file-name' should work with `eval-buffer'. 54 (eval-buffer) 55 (should (equal "m1.el" 56 (file-name-nondirectory macroexp--m1-tests-filename))) 57 (should (equal "m1.el" 58 (file-name-nondirectory (macroexp--m1-tests-file-name)))) 59 (search-forward "macroexp--m1-tests-filename") 60 (makunbound 'macroexp--m1-tests-filename) 61 ;; `macroexp-file-name' should also work with `eval-defun'. 62 (eval-defun nil) 63 (should (equal "m1.el" 64 (file-name-nondirectory macroexp--m1-tests-filename)))) 65 66 ;; Test the case where we load a file which byte-compiles another. 67 (defvar macroexp--m1-tests-comp-filename) 68 (makunbound 'macroexp--m1-tests-comp-filename) 69 (load (expand-file-name "m2.el" rsrc-dir)) 70 (should (equal "m1.el" 71 (file-name-nondirectory macroexp--m1-tests-comp-filename))))) 72 73(defun macroexp-tests--run-emacs (&rest args) 74 "Run Emacs in batch mode with ARGS, return output." 75 (let ((emacs (expand-file-name invocation-name invocation-directory))) 76 (with-temp-buffer 77 (let ((res (apply #'call-process emacs nil t nil 78 "-Q" "--batch" args)) 79 (output (buffer-string))) 80 (unless (equal res 0) 81 (message "%s" output) 82 (error "Inferior Emacs exited with status %S" res)) 83 output)))) 84 85(defun macroexp-tests--eval-in-subprocess (file expr) 86 (let ((output (macroexp-tests--run-emacs 87 "-l" file (format "--eval=(print %S)" expr)))) 88 (car (read-from-string output)))) 89 90(defun macroexp-tests--byte-compile-in-subprocess (file) 91 "Byte-compile FILE using a subprocess to avoid contaminating the lisp state." 92 (let ((output (macroexp-tests--run-emacs "-f" "batch-byte-compile" file))) 93 (when output 94 (message "%s" output)))) 95 96(ert-deftest macroexp--tests-dynamic-variable-p () 97 "Test `macroexp--dynamic-variable-p'." 98 (let* ((vk-el (ert-resource-file "vk.el")) 99 (vk-elc (concat vk-el "c")) 100 (expr '(list (vk-f1 0) 101 (vk-f2 0) 102 vk-val3 103 (funcall vk-f4 0) 104 (funcall vk-f5 0) 105 (vk-f6) 106 (vk-f7)))) 107 ;; We compile and run the test in separate processes for complete 108 ;; isolation between test cases. 109 (should (equal (macroexp-tests--eval-in-subprocess vk-el expr) 110 '((dyn dyn dyn dyn lex lex) 111 (dyn dyn lex lex) 112 (dyn dyn dyn dyn lex lex) 113 (dyn dyn dyn dyn dyn) 114 (dyn dyn dyn lex lex) 115 (dyn dyn dyn dyn) 116 (dyn dyn dyn lex)))) 117 (macroexp-tests--byte-compile-in-subprocess vk-el) 118 (should (equal (macroexp-tests--eval-in-subprocess vk-elc expr) 119 '((dyn dyn dyn dyn lex lex) 120 (dyn dyn lex lex) 121 (dyn dyn dyn dyn lex lex) 122 (dyn dyn dyn dyn dyn) 123 (dyn dyn dyn lex lex) 124 (dyn dyn dyn dyn) 125 (dyn dyn dyn lex)))))) 126 127;;; macroexp-tests.el ends here 128