emacs/test/lisp/emacs-lisp/testcover-tests.el

185 lines
7 KiB
EmacsLisp
Raw Normal View History

;;; testcover-tests.el --- Testcover test suite -*- lexical-binding:t -*-
;; Copyright (C) 2017-2018 Free Software Foundation, Inc.
;; Author: Gemini Lasswell
;; This file is part of GNU Emacs.
;; This program is free software: you can redistribute it and/or
;; modify it under the terms of the GNU General Public License as
;; published by the Free Software Foundation, either version 3 of the
;; License, or (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful, but
;; WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;; General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see `https://www.gnu.org/licenses/'.
;;; Commentary:
;; Testcover test suite.
;; * All the test cases are in testcover-resources/testcover-cases.el.
;; See that file for an explanation of the test case format.
;; * `testcover-tests-define-tests', which is run when this file is
;; loaded, reads testcover-resources/testcover-cases.el and defines
;; ERT tests for each test case.
;;; Code:
(require 'ert)
(require 'testcover)
(require 'skeleton)
;; Use `eval-and-compile' around all these definitions because they're
;; used by the macro `testcover-tests-define-tests'.
(eval-and-compile
(defvar testcover-tests-file-dir
(expand-file-name
"testcover-resources/"
(file-name-directory (or (bound-and-true-p byte-compile-current-file)
load-file-name
buffer-file-name)))
"Directory of the \"testcover-tests.el\" file."))
(eval-and-compile
(defvar testcover-tests-test-cases
(expand-file-name "testcases.el" testcover-tests-file-dir)
"File containing marked up code to instrument and check."))
;; Convert Testcover's overlays to plain text.
(eval-and-compile
(defun testcover-tests-markup-region (beg end &rest optargs)
"Mark up test code within region between BEG and END.
Convert Testcover's tan and red splotches to %%% and !!! for
testcases.el. This can be used to create test cases if Testcover
is working correctly on a code sample. OPTARGS are optional
arguments for `testcover-start'."
(interactive "r")
(let ((tempfile (make-temp-file "testcover-tests-" nil ".el"))
(code (buffer-substring beg end))
(marked-up-code))
(unwind-protect
(progn
(with-temp-file tempfile
(insert code))
(save-current-buffer
(let ((buf (find-file-noselect tempfile)))
(set-buffer buf)
(apply 'testcover-start (cons tempfile optargs))
(testcover-mark-all buf)
(dolist (overlay (overlays-in (point-min) (point-max)))
(let ((ov-face (overlay-get overlay 'face)))
(goto-char (overlay-end overlay))
(cond
((eq ov-face 'testcover-nohits) (insert "!!!"))
((eq ov-face 'testcover-1value) (insert "%%%"))
(t nil))))
(setq marked-up-code (buffer-string)))
(set-buffer-modified-p nil)))
(ignore-errors (kill-buffer (find-file-noselect tempfile)))
(ignore-errors (delete-file tempfile)))
;; Now replace the original code with the marked up code.
(delete-region beg end)
(insert marked-up-code))))
(eval-and-compile
(defun testcover-tests-unmarkup-region (beg end)
"Remove the markup used in testcases.el between BEG and END."
(interactive "r")
(save-excursion
(save-restriction
(narrow-to-region beg end)
(goto-char (point-min))
(while (re-search-forward "!!!\\|%%%" nil t)
(replace-match ""))))))
(define-skeleton testcover-tests-skeleton
"Write a testcase for testcover-tests.el."
"Enter name of test: "
";; ==== " str " ====\n"
"\"docstring\"\n"
";; Directives for ERT should go here, if any.\n"
";; ====\n"
";; Replace this line with annotated test code.\n")
;; Check a test case.
(eval-and-compile
(defun testcover-tests-run-test-case (marked-up-code)
"Test the operation of Testcover on the string MARKED-UP-CODE."
(let ((tempfile (make-temp-file "testcover-tests-" nil ".el")))
(unwind-protect
(progn
(with-temp-file tempfile
(insert marked-up-code))
;; Remove the marks and mark the code up again. The original
;; and recreated versions should match.
(save-current-buffer
(set-buffer (find-file-noselect tempfile))
;; Fail the test if the debugger tries to become active,
Rewrite Testcover's internals, fixing several bugs * lisp/emacs-lisp/testcover.el: Rewrite the internals of Testcover to analyze instrumented code instead of reinstrumenting it. Use new hooks in Edebug to collect code coverage information at runtime using Edebug's instrumentation. Includes fixes for: (bug#11307) (bug#24509) (bug#24688) (bug#24743) (bug#25316) (bug#25326). (testcover-compose-functions): Remove mapcar. (testcover-start, testcover-this-defun): Analyze code instead of reinstrumenting it. Set edebug-behavior for each definition. (testcover--read, testcover-1value, testcover-reinstrument) (testcover-reinstrument-list, testcover-reinstrument-compose): Deleted. (testcover-after-instrumentation, testcover-init-definition) (testcover-before): New functions. (testcover-enter): Change call signature to match edebug-enter. (testcover-after, testcover-mark): Add handling of 'maybe and 'noreturn. (testcover-analyze-coverage, testcover-analyze-coverage-progn) (testcover-analyze-coverage-edebug-after) (testcover-analyze-coverage-wrapped-form) (testcover-analyze-coverage-wrapped-application) (testcover-analyze-coverage-compose) (testcover-analyze-coverage-backquote) (testcover-analyze-coverage-backquote-form) (testcover-coverage-combine): New functions to analyze instrumented code. * lisp/emacs-lisp/gv.el: Modify edebug-after's gv-expander to instrument in the setter as well as the getter. * test/lisp/emacs-lisp/testcover-tests.el (testcover-tests-run-test-case): Use `edebug-default-enter' instead of `edebug-enter' to detect Edebug invocation during tests. * test/lisp/emacs-lisp/testcover-resources/testcases.el (constants-bug-25316) (customize-defcustom-bug-25326) (1-value-symbol-bug-25316) (quotes-within-backquotes-bug-25316) (backquote-1value-bug-24509) (pcase-bug-24688) (defun-in-backquote-bug-11307-and-24743) (closure-1value-bug) (backquoted-vector-bug-25316) (vector-in-macro-spec-bug-25316) (mapcar-is-not-compose): Remove expected failure tags. (function-with-edebug-spec-bug-25316): Remove expected failure tag and modify expected result. (quoted-backquote): New test. * lisp/textmodes/rst.el: Remove workarounds for bugs in Testcover. (rst-testcover-defcustom): Deleted. * lisp/subr.el (1value): Remove incorrect description of testcover-1value from docstring, replace with description of Testcover's treatment of 1value.
2017-09-25 13:45:07 -07:00
;; which can happen if Testcover fails to attach itself
;; correctly. Note that this will prevent debugging
;; these tests using Edebug.
(cl-letf (((symbol-function #'edebug-default-enter)
(lambda (&rest _args)
Rewrite Testcover's internals, fixing several bugs * lisp/emacs-lisp/testcover.el: Rewrite the internals of Testcover to analyze instrumented code instead of reinstrumenting it. Use new hooks in Edebug to collect code coverage information at runtime using Edebug's instrumentation. Includes fixes for: (bug#11307) (bug#24509) (bug#24688) (bug#24743) (bug#25316) (bug#25326). (testcover-compose-functions): Remove mapcar. (testcover-start, testcover-this-defun): Analyze code instead of reinstrumenting it. Set edebug-behavior for each definition. (testcover--read, testcover-1value, testcover-reinstrument) (testcover-reinstrument-list, testcover-reinstrument-compose): Deleted. (testcover-after-instrumentation, testcover-init-definition) (testcover-before): New functions. (testcover-enter): Change call signature to match edebug-enter. (testcover-after, testcover-mark): Add handling of 'maybe and 'noreturn. (testcover-analyze-coverage, testcover-analyze-coverage-progn) (testcover-analyze-coverage-edebug-after) (testcover-analyze-coverage-wrapped-form) (testcover-analyze-coverage-wrapped-application) (testcover-analyze-coverage-compose) (testcover-analyze-coverage-backquote) (testcover-analyze-coverage-backquote-form) (testcover-coverage-combine): New functions to analyze instrumented code. * lisp/emacs-lisp/gv.el: Modify edebug-after's gv-expander to instrument in the setter as well as the getter. * test/lisp/emacs-lisp/testcover-tests.el (testcover-tests-run-test-case): Use `edebug-default-enter' instead of `edebug-enter' to detect Edebug invocation during tests. * test/lisp/emacs-lisp/testcover-resources/testcases.el (constants-bug-25316) (customize-defcustom-bug-25326) (1-value-symbol-bug-25316) (quotes-within-backquotes-bug-25316) (backquote-1value-bug-24509) (pcase-bug-24688) (defun-in-backquote-bug-11307-and-24743) (closure-1value-bug) (backquoted-vector-bug-25316) (vector-in-macro-spec-bug-25316) (mapcar-is-not-compose): Remove expected failure tags. (function-with-edebug-spec-bug-25316): Remove expected failure tag and modify expected result. (quoted-backquote): New test. * lisp/textmodes/rst.el: Remove workarounds for bugs in Testcover. (rst-testcover-defcustom): Deleted. * lisp/subr.el (1value): Remove incorrect description of testcover-1value from docstring, replace with description of Testcover's treatment of 1value.
2017-09-25 13:45:07 -07:00
(ert-fail "Debugger invoked during test run"))))
(dolist (byte-compile '(t nil))
(testcover-tests-unmarkup-region (point-min) (point-max))
(unwind-protect
(testcover-tests-markup-region (point-min) (point-max) byte-compile)
(set-buffer-modified-p nil))
(should (string= marked-up-code
(buffer-string)))))))
(ignore-errors (kill-buffer (find-file-noselect tempfile)))
(ignore-errors (delete-file tempfile))))))
;; Convert test case file to ert-defmethod.
(eval-and-compile
(defun testcover-tests-build-test-cases ()
"Parse the test case file and return a list of ERT test definitions.
Construct and return a list of `ert-deftest' forms. See testcases.el
for documentation of the test definition format."
(let (results)
(with-temp-buffer
(insert-file-contents testcover-tests-test-cases)
(goto-char (point-min))
(while (re-search-forward
(concat "^;; ==== \\([^ ]+?\\) ====\n"
"\\(\\(?:.*\n\\)*?\\)"
";; ====\n"
"\\(\\(?:.*\n\\)*?\\)"
"\\(\\'\\|;; ====\\)")
nil t)
(let ((name (match-string 1))
(splice (car (read-from-string
(format "(%s)" (match-string 2)))))
(code (match-string 3)))
(push
`(ert-deftest ,(intern (concat "testcover-tests-" name)) ()
,@splice
(testcover-tests-run-test-case ,code))
results))
(beginning-of-line)))
results)))
;; Define all the tests.
(defmacro testcover-tests-define-tests ()
"Construct and define ERT test methods using the test case file."
(let* ((test-cases (testcover-tests-build-test-cases)))
`(progn ,@test-cases)))
(testcover-tests-define-tests)
(provide 'testcover-tests)
;;; testcover-tests.el ends here