aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorMattias EngdegÄrd2021-04-09 18:42:12 +0200
committerMattias EngdegÄrd2021-04-09 19:20:54 +0200
commita2a7cfde29aa71f9ea503b8dc467d694f6e5b69f (patch)
treed9da881eaead51b151e1e60c93c8c8b978d55924 /test
parent40db60563c6b259e1208b6931f0a343849026814 (diff)
downloademacs-a2a7cfde29aa71f9ea503b8dc467d694f6e5b69f.tar.gz
emacs-a2a7cfde29aa71f9ea503b8dc467d694f6e5b69f.zip
Clean up bytecomp-tests.el
Now all test cases are run with both lexical and dynamic binding where applicable, comparing interpreted against compiled results. Previously, almost all tests were only run with dynamic binding which was definitely not intended. * test/lisp/emacs-lisp/bytecomp-tests.el (byte-opt-testsuite-arith-data): Rename to bytecomp-tests--test-cases. (bytecomp-check-1, bytecomp-explain-1, bytecomp-tests) (bytecomp-lexbind-tests, bytecomp-lexbind-check-1) (bytecomp-lexbind-explain-1): Remove. (bytecomp-tests--eval-interpreted, bytecomp-tests--eval-compiled) (bytecomp-tests-lexbind, bytecomp-tests-dynbind) (bytecomp-tests--test-cases-lexbind-only): New.
Diffstat (limited to 'test')
-rw-r--r--test/lisp/emacs-lisp/bytecomp-tests.el150
1 files changed, 47 insertions, 103 deletions
diff --git a/test/lisp/emacs-lisp/bytecomp-tests.el b/test/lisp/emacs-lisp/bytecomp-tests.el
index 0f7a0ccc851..b1377e59f77 100644
--- a/test/lisp/emacs-lisp/bytecomp-tests.el
+++ b/test/lisp/emacs-lisp/bytecomp-tests.el
@@ -41,7 +41,7 @@
41 "Identity, but hidden from some optimisations." 41 "Identity, but hidden from some optimisations."
42 x) 42 x)
43 43
44(defconst byte-opt-testsuite-arith-data 44(defconst bytecomp-tests--test-cases
45 '( 45 '(
46 ;; some functional tests 46 ;; some functional tests
47 (let ((a most-positive-fixnum) (b 1) (c 1.0)) (+ a b c)) 47 (let ((a most-positive-fixnum) (b 1) (c 1.0)) (+ a b c))
@@ -430,69 +430,54 @@
430 (list s x i)) 430 (list s x i))
431 431
432 (let ((x 2)) 432 (let ((x 2))
433 (list (or (bytecomp-test-identity 'a) (setq x 3)) x))) 433 (list (or (bytecomp-test-identity 'a) (setq x 3)) x))
434 "List of expression for test. 434 )
435Each element will be executed by interpreter and with 435 "List of expressions for cross-testing interpreted and compiled code.")
436bytecompiled code, and their results compared.")
437 436
438(defun bytecomp-check-1 (pat) 437(defconst bytecomp-tests--test-cases-lexbind-only
439 "Return non-nil if PAT is the same whether directly evalled or compiled." 438 `(
440 (let ((warning-minimum-log-level :emergency) 439 ;; This would infloop (and exhaust stack) with dynamic binding.
441 (byte-compile-warnings nil) 440 (let ((f #'car))
442 (v0 (condition-case err 441 (let ((f (lambda (x) (cons (funcall f x) (cdr x)))))
443 (eval pat) 442 (funcall f '(1 . 2))))
444 (error (list 'bytecomp-check-error (car err))))) 443 )
445 (v1 (condition-case err 444 "List of expressions for cross-testing interpreted and compiled code.
446 (funcall (byte-compile (list 'lambda nil pat))) 445These are only tested with lexical binding.")
447 (error (list 'bytecomp-check-error (car err)))))) 446
448 (equal v0 v1))) 447(defun bytecomp-tests--eval-interpreted (form)
449 448 "Evaluate FORM using the Lisp interpreter, returning errors as a
450(put 'bytecomp-check-1 'ert-explainer 'bytecomp-explain-1) 449special value."
451 450 (condition-case err
452(defun bytecomp-explain-1 (pat) 451 (eval form lexical-binding)
453 (let ((v0 (condition-case err 452 (error (list 'bytecomp-check-error (car err)))))
454 (eval pat) 453
455 (error (list 'bytecomp-check-error (car err))))) 454(defun bytecomp-tests--eval-compiled (form)
456 (v1 (condition-case err 455 "Evaluate FORM using the Lisp byte-code compiler, returning errors as a
457 (funcall (byte-compile (list 'lambda nil pat))) 456special value."
458 (error (list 'bytecomp-check-error (car err))))))
459 (format "Expression `%s' gives `%s' if directly evalled, `%s' if compiled."
460 pat v0 v1)))
461
462(ert-deftest bytecomp-tests ()
463 "Test the Emacs byte compiler."
464 (dolist (pat byte-opt-testsuite-arith-data)
465 (should (bytecomp-check-1 pat))))
466
467(defun test-byte-opt-arithmetic (&optional arg)
468 "Unit test for byte-opt arithmetic operations.
469Subtests signal errors if something goes wrong."
470 (interactive "P")
471 (switch-to-buffer (generate-new-buffer "*Font Pase Test*"))
472 (let ((warning-minimum-log-level :emergency) 457 (let ((warning-minimum-log-level :emergency)
473 (byte-compile-warnings nil) 458 (byte-compile-warnings nil))
474 (pass-face '((t :foreground "green"))) 459 (condition-case err
475 (fail-face '((t :foreground "red"))) 460 (funcall (byte-compile (list 'lambda nil form)))
476 (print-escape-nonascii t) 461 (error (list 'bytecomp-check-error (car err))))))
477 (print-escape-newlines t) 462
478 (print-quoted t) 463(ert-deftest bytecomp-tests-lexbind ()
479 v0 v1) 464 "Check that various expressions behave the same when interpreted and
480 (dolist (pat byte-opt-testsuite-arith-data) 465byte-compiled. Run with lexical binding."
481 (condition-case err 466 (let ((lexical-binding t))
482 (setq v0 (eval pat)) 467 (dolist (form (append bytecomp-tests--test-cases-lexbind-only
483 (error (setq v0 (list 'bytecomp-check-error (car err))))) 468 bytecomp-tests--test-cases))
484 (condition-case err 469 (ert-info ((prin1-to-string form) :prefix "form: ")
485 (setq v1 (funcall (byte-compile (list 'lambda nil pat)))) 470 (should (equal (bytecomp-tests--eval-interpreted form)
486 (error (setq v1 (list 'bytecomp-check-error (car err))))) 471 (bytecomp-tests--eval-compiled form)))))))
487 (insert (format "%s" pat)) 472
488 (indent-to-column 65) 473(ert-deftest bytecomp-tests-dynbind ()
489 (if (equal v0 v1) 474 "Check that various expressions behave the same when interpreted and
490 (insert (propertize "OK" 'face pass-face)) 475byte-compiled. Run with dynamic binding."
491 (insert (propertize "FAIL\n" 'face fail-face)) 476 (let ((lexical-binding nil))
492 (indent-to-column 55) 477 (dolist (form bytecomp-tests--test-cases)
493 (insert (propertize (format "[%s] vs [%s]" v0 v1) 478 (ert-info ((prin1-to-string form) :prefix "form: ")
494 'face fail-face))) 479 (should (equal (bytecomp-tests--eval-interpreted form)
495 (insert "\n")))) 480 (bytecomp-tests--eval-compiled form)))))))
496 481
497(defun test-byte-comp-compile-and-load (compile &rest forms) 482(defun test-byte-comp-compile-and-load (compile &rest forms)
498 (declare (indent 1)) 483 (declare (indent 1))
@@ -813,47 +798,6 @@ Subtests signal errors if something goes wrong."
813 (defun def () (m)))) 798 (defun def () (m))))
814 (should (equal (funcall 'def) 4))) 799 (should (equal (funcall 'def) 4)))
815 800
816(defconst bytecomp-lexbind-tests
817 `(
818 (let ((f #'car))
819 (let ((f (lambda (x) (cons (funcall f x) (cdr x)))))
820 (funcall f '(1 . 2))))
821 )
822 "List of expression for test.
823Each element will be executed by interpreter and with
824bytecompiled code, and their results compared.")
825
826(defun bytecomp-lexbind-check-1 (pat)
827 "Return non-nil if PAT is the same whether directly evalled or compiled."
828 (let ((warning-minimum-log-level :emergency)
829 (byte-compile-warnings nil)
830 (v0 (condition-case err
831 (eval pat t)
832 (error (list 'bytecomp-check-error (car err)))))
833 (v1 (condition-case err
834 (funcall (let ((lexical-binding t))
835 (byte-compile `(lambda nil ,pat))))
836 (error (list 'bytecomp-check-error (car err))))))
837 (equal v0 v1)))
838
839(put 'bytecomp-lexbind-check-1 'ert-explainer 'bytecomp-lexbind-explain-1)
840
841(defun bytecomp-lexbind-explain-1 (pat)
842 (let ((v0 (condition-case err
843 (eval pat t)
844 (error (list 'bytecomp-check-error (car err)))))
845 (v1 (condition-case err
846 (funcall (let ((lexical-binding t))
847 (byte-compile (list 'lambda nil pat))))
848 (error (list 'bytecomp-check-error (car err))))))
849 (format "Expression `%s' gives `%s' if directly evalled, `%s' if compiled."
850 pat v0 v1)))
851
852(ert-deftest bytecomp-lexbind-tests ()
853 "Test the Emacs byte compiler lexbind handling."
854 (dolist (pat bytecomp-lexbind-tests)
855 (should (bytecomp-lexbind-check-1 pat))))
856
857(defmacro bytecomp-tests--with-temp-file (file-name-var &rest body) 801(defmacro bytecomp-tests--with-temp-file (file-name-var &rest body)
858 (declare (indent 1)) 802 (declare (indent 1))
859 (cl-check-type file-name-var symbol) 803 (cl-check-type file-name-var symbol)