test-ob-emacs-lisp.el 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. ;;; test-ob-emacs-lisp.el -*- lexical-binding: t; -*-
  2. ;; Copyright (c) 2012-2022 Free Software Foundation, Inc.
  3. ;; Authors: Eric Schulte, Martyn Jago
  4. ;; This file is not part of GNU Emacs.
  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 <https://www.gnu.org/licenses/>.
  15. ;;; Comments:
  16. ;; Org tests for ob-emacs-lisp.el live here
  17. ;;; Code:
  18. (ert-deftest ob-emacs-lisp/commented-last-block-line-no-var ()
  19. (org-test-with-temp-text-in-file "
  20. #+begin_src emacs-lisp
  21. ;;
  22. #+end_src"
  23. (org-babel-next-src-block)
  24. (org-babel-execute-maybe)
  25. (should (re-search-forward "results:" nil t))
  26. (forward-line)
  27. (should
  28. (string=
  29. ""
  30. (buffer-substring-no-properties (point-at-bol) (point-at-eol)))))
  31. (org-test-with-temp-text-in-file "
  32. #+begin_src emacs-lisp
  33. \"some text\";;
  34. #+end_src"
  35. (org-babel-next-src-block)
  36. (org-babel-execute-maybe)
  37. (should (re-search-forward "results:" nil t))
  38. (forward-line)
  39. (should
  40. (string=
  41. ": some text"
  42. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))))
  43. (ert-deftest ob-emacs-lisp/commented-last-block-line-with-var ()
  44. (org-test-with-temp-text-in-file "
  45. #+begin_src emacs-lisp :var a=1
  46. ;;
  47. #+end_src"
  48. (org-babel-next-src-block)
  49. (org-babel-execute-maybe)
  50. (re-search-forward "results" nil t)
  51. (forward-line)
  52. (should (string=
  53. ""
  54. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))))
  55. (ert-deftest ob-emacs-lisp/commented-last-block-line ()
  56. (should
  57. (string= ": 2"
  58. (org-test-with-temp-text-in-file "
  59. #+begin_src emacs-lisp :var a=2
  60. 2;;
  61. #+end_src"
  62. (org-babel-next-src-block)
  63. (org-babel-execute-maybe)
  64. (re-search-forward "results" nil t)
  65. (buffer-substring-no-properties (line-beginning-position 2)
  66. (line-end-position 2))))))
  67. (ert-deftest ob-emacs-lisp/dynamic-lexical-execute ()
  68. (cl-flet ((execute (text)
  69. (org-test-with-temp-text-in-file text
  70. (org-babel-next-src-block)
  71. (org-babel-execute-maybe)
  72. (re-search-forward "results" nil t)
  73. (re-search-forward ": " nil t)
  74. (buffer-substring-no-properties (point) (point-at-eol)))))
  75. (should (string= "dynamic" (execute "
  76. #+begin_src emacs-lisp :lexical no :results verbatim
  77. \(let ((x 'dynamic)) (funcall (let ((x 'lexical)) (lambda () x))))
  78. #+end_src")))
  79. (should (string= "lexical" (execute "
  80. #+begin_src emacs-lisp :lexical yes :results verbatim
  81. \(let ((x 'dynamic)) (funcall (let ((x 'lexical)) (lambda () x))))
  82. #+end_src")))
  83. (defvar ob-emacs--x)
  84. (should (string= "dynamic" (let ((ob-emacs--x 'dynamic)) (execute "
  85. #+begin_src emacs-lisp :lexical no :results verbatim
  86. ob-emacs--x
  87. #+end_src"))))
  88. (should (string= "lexical" (let ((ob-emacs--x 'dynamic)) (execute "
  89. #+begin_src emacs-lisp :lexical '((ob-emacs--x . lexical)) :results verbatim
  90. ob-emacs--x
  91. #+end_src"))))
  92. ;; Src block execution uses `eval'. As of 2019-02-26, `eval' does
  93. ;; not dynamically bind `lexical-binding' to the value of its
  94. ;; LEXICAL parameter. Hence, (eval 'lexical-binding LEXICAL)
  95. ;; evaluates to the same value that just `lexical-binding'
  96. ;; evaluates to, even if LEXICAL is different. So tests like the
  97. ;; following do not work here:
  98. ;;
  99. ;; (should (string= "t" (execute "
  100. ;; #+begin_src emacs-lisp :lexical yes :results verbatim
  101. ;; lexical-binding
  102. ;; #+end_src")))
  103. ;;
  104. ;; However, the corresponding test in
  105. ;; `ob-emacs-lisp/dynamic-lexical-edit' does work.
  106. ))
  107. (ert-deftest ob-emacs-lisp/dynamic-lexical-edit ()
  108. (cl-flet ((execute (text)
  109. (org-test-with-temp-text-in-file text
  110. (org-babel-next-src-block)
  111. (org-edit-src-code)
  112. (goto-char (point-max))
  113. (prog1 (eval-last-sexp 0)
  114. (org-edit-src-exit)))))
  115. (should (eq 'dynamic (execute "
  116. #+begin_src emacs-lisp :lexical no :results verbatim
  117. \(let ((x 'dynamic)) (funcall (let ((x 'lexical)) (lambda () x))))
  118. #+end_src")))
  119. (should (eq 'lexical (execute "
  120. #+begin_src emacs-lisp :lexical yes :results verbatim
  121. \(let ((x 'dynamic)) (funcall (let ((x 'lexical)) (lambda () x))))
  122. #+end_src")))
  123. (defvar ob-emacs--x)
  124. (should (eq 'dynamic (let ((ob-emacs--x 'dynamic)) (execute "
  125. #+begin_src emacs-lisp :lexical no :results verbatim
  126. ob-emacs--x
  127. #+end_src"))))
  128. (should (eq 'lexical (let ((ob-emacs--x 'dynamic)) (execute "
  129. #+begin_src emacs-lisp :lexical '((ob-emacs--x . lexical)) :results verbatim
  130. ob-emacs--x
  131. #+end_src"))))
  132. (should (equal nil (execute "
  133. #+begin_src emacs-lisp :lexical no :results verbatim
  134. lexical-binding
  135. #+end_src")))
  136. (should (equal t (execute "
  137. #+begin_src emacs-lisp :lexical yes :results verbatim
  138. lexical-binding
  139. #+end_src")))
  140. (should (equal '((x . 0)) (execute "
  141. #+begin_src emacs-lisp :lexical '((x . 0)) :results verbatim
  142. lexical-binding
  143. #+end_src")))))
  144. (provide 'test-ob-emacs-lisp)
  145. ;;; test-ob-emacs-lisp.el ends here