lexbind-tests.el 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. ;;; lexbind-tests.el --- Testing the lexbind byte-compiler
  2. ;; Copyright (C) 2011-2012 Free Software Foundation, Inc.
  3. ;; Author: Stefan Monnier <monnier@iro.umontreal.ca>
  4. ;; Keywords:
  5. ;; This program is free software; you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; This program is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Commentary:
  16. ;;
  17. ;;; Code:
  18. (require 'ert)
  19. (defconst lexbind-tests
  20. `(
  21. (let ((f #'car))
  22. (let ((f (lambda (x) (cons (funcall f x) (cdr x)))))
  23. (funcall f '(1 . 2))))
  24. )
  25. "List of expression for test.
  26. Each element will be executed by interpreter and with
  27. bytecompiled code, and their results compared.")
  28. (defun lexbind-check-1 (pat)
  29. "Return non-nil if PAT is the same whether directly evalled or compiled."
  30. (let ((warning-minimum-log-level :emergency)
  31. (byte-compile-warnings nil)
  32. (v0 (condition-case nil
  33. (eval pat t)
  34. (error nil)))
  35. (v1 (condition-case nil
  36. (funcall (let ((lexical-binding t))
  37. (byte-compile `(lambda nil ,pat))))
  38. (error nil))))
  39. (equal v0 v1)))
  40. (put 'lexbind-check-1 'ert-explainer 'lexbind-explain-1)
  41. (defun lexbind-explain-1 (pat)
  42. (let ((v0 (condition-case nil
  43. (eval pat t)
  44. (error nil)))
  45. (v1 (condition-case nil
  46. (funcall (let ((lexical-binding t))
  47. (byte-compile (list 'lambda nil pat))))
  48. (error nil))))
  49. (format "Expression `%s' gives `%s' if directly evalled, `%s' if compiled."
  50. pat v0 v1)))
  51. (ert-deftest lexbind-tests ()
  52. "Test the Emacs byte compiler lexbind handling."
  53. (dolist (pat lexbind-tests)
  54. (should (lexbind-check-1 pat))))
  55. (provide 'lexbind-tests)
  56. ;;; lexbind-tests.el ends here