0
0
Fork 1
mirror of https://git.savannah.gnu.org/git/emacs/org-mode.git synced 2024-09-05 22:13:00 +00:00
org-mode/testing/lisp/test-ob-lob.el
Nicolas Goaziou df10309489 ob-ref: Properly resolve references in ":post" arguments
* lisp/ob-core.el (org-babel-exp-reference-buffer): New variable, as
  a replacement for `org-current-export-file'.
(org-babel-check-confirm-evaluate): Use new variable.
* lisp/ob-exp.el (org-babel-exp-in-export-file): Use new variable.
(org-babel-exp-get-export-buffer): Remove function.
(org-babel-exp-process-buffer): Change signature.
* lisp/ob-ref.el (org-babel-ref-resolve): Use new variable during
  export in order to properly resolve references.
* lisp/ox.el (org-export-execute-babel-code): Use new variable.

* contrib/lisp/org-wikinodes.el (org-wikinodes-process-links-for-export):
  Remove a cond branch as it is always
  false (`org-current-export-file' couldn't be a string).

* testing/lisp/test-ob-lob.el (test-ob-lob/export-lob-lines): Update
  test.
* testing/lisp/test-ob.el (test-ob/eval-header-argument): Update test.
* testing/lisp/test-ob-exp.el (ob-export/reference-in-post-header):
  New test.

During export, Babel executes sequentially all blocks in the buffer
being exported.  This can lead to modifications preventing some
references from being resolved.  As a workaround, Babel stores
a pristine copy of the buffer in a variable so it can always find
needed references.

Before this patch, the variable storing this copy was
`org-current-export-file' and was dynamically bound in "ox.el".  It
was used to resolve noweb references (`org-babel-expand-noweb-references')
but not regular references (`org-babel-ref-resolve').

Now, the variable is `org-babel-exp-reference-buffer' and it is bound
from `org-babel-exp-process-buffer'.  It is used to resolve all
references.  In particular, this allows to use references in :post
header.

Thanks to Jarmo Hurri for reporting it.
2014-03-19 21:34:56 +01:00

156 lines
5.7 KiB
EmacsLisp
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

;;; test-ob-lob.el --- test for ob-lob.el
;; Copyright (c) 2010-2014 Eric Schulte
;; Authors: Eric Schulte
;; This file is not 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 <http://www.gnu.org/licenses/>.
;;; Tests
(org-babel-lob-ingest
(expand-file-name
"library-of-babel.org"
(expand-file-name
"doc"
(expand-file-name
".."
(expand-file-name
".."
(file-name-directory
(or load-file-name buffer-file-name)))))))
(ert-deftest test-ob-lob/ingest ()
"Test the ingestion of an org-mode file."
(should (< 0 (org-babel-lob-ingest
(expand-file-name "babel.org" org-test-example-dir)))))
(ert-deftest test-ob-lob/call-with-header-arguments ()
"Test the evaluation of a library of babel #+call: line."
(org-test-at-id "fab7e291-fde6-45fc-bf6e-a485b8bca2f0"
(move-beginning-of-line 1)
(forward-line 6)
(message (buffer-substring (point-at-bol) (point-at-eol)))
(should (string= "testing" (org-babel-lob-execute
(org-babel-lob-get-info))))
(forward-line 1)
(should (string= "testing" (caar (org-babel-lob-execute
(org-babel-lob-get-info)))))
(forward-line 1)
(should (string= "testing" (org-babel-lob-execute
(org-babel-lob-get-info))))
(forward-line 1)
(should (string= "testing" (caar (org-babel-lob-execute
(org-babel-lob-get-info)))))
(forward-line 1)
(should (string= "testing" (org-babel-lob-execute
(org-babel-lob-get-info))))
(forward-line 1)
(should (string= "testing" (caar (org-babel-lob-execute
(org-babel-lob-get-info)))))
(forward-line 1) (beginning-of-line) (forward-char 27)
(should (string= "testing" (org-babel-lob-execute
(org-babel-lob-get-info))))
(forward-line 1) (beginning-of-line) (forward-char 27)
(should (string= "testing" (caar (org-babel-lob-execute
(org-babel-lob-get-info)))))
(forward-line 1) (beginning-of-line)
(should (= 4 (org-babel-lob-execute (org-babel-lob-get-info))))
(forward-line 1)
(should (string= "testing" (org-babel-lob-execute
(org-babel-lob-get-info))))
(forward-line 1)
(should (string= "123" (org-babel-lob-execute (org-babel-lob-get-info))))))
(ert-deftest test-ob-lob/export-lob-lines ()
"Test the export of a variety of library babel call lines."
(org-test-at-id "72ddeed3-2d17-4c7f-8192-a575d535d3fc"
(org-narrow-to-subtree)
(let ((buf (current-buffer))
(string (buffer-string)))
(with-temp-buffer
(org-mode)
(insert string)
(org-babel-exp-process-buffer buf)
(message (buffer-string))
(goto-char (point-min))
(should (re-search-forward "^: 0" nil t))
(should (re-search-forward "call =2= stuck" nil t))
(should (re-search-forward
"exported =call_double(it=2)= because" nil t))
(should (re-search-forward "^=6= because" nil t))
(should (re-search-forward "results 8 should" nil t))
(should (re-search-forward "following 2\\*5==10= should" nil t))))))
(ert-deftest test-ob-lob/do-not-eval-lob-lines-in-example-blocks-on-export ()
(require 'ox)
(org-test-with-temp-text-in-file "
for export
#+begin_example
#+call: rubbish()
#+end_example"
(should (progn (org-export-execute-babel-code) t))))
(ert-deftest test-ob-lob/caching-call-line ()
(let ((temporary-value-for-test 0))
(org-test-with-temp-text "
#+name: call-line-caching-example
#+begin_src emacs-lisp :var bar=\"baz\"
(setq temporary-value-for-test (+ 1 temporary-value-for-test))
#+end_src
#+call: call-line-caching-example(\"qux\") :cache yes
"
(goto-char (point-max)) (forward-line -1)
;; first execution should flip value to t
(should (equal (org-babel-lob-execute (org-babel-lob-get-info)) 1))
;; if cached, second evaluation will retain the t value
;;
;; Note: This instance tests for equality with "1". We would
;; prefer if the cached result returned was actually 1, however
;; this is not the current behavior so this test is encoding
;; undesired behavior (because the current goal is simply to see
;; that caching is used on call lines).
;;
(should (equal (org-babel-lob-execute (org-babel-lob-get-info)) "1")))))
(ert-deftest test-ob-lob/named-caching-call-line ()
(let ((temporary-value-for-test 0))
(org-test-with-temp-text "
#+name: call-line-caching-example
#+begin_src emacs-lisp :var bar=\"baz\"
(setq temporary-value-for-test (+ 1 temporary-value-for-test))
#+end_src
#+name: call-line-caching-called
#+call: call-line-caching-example(\"qux\") :cache yes
"
(goto-char (point-max)) (forward-line -1)
;; first execution should flip value to t
(should (equal (org-babel-lob-execute (org-babel-lob-get-info)) 1))
;; if cached, second evaluation will retain the t value
;;
;; Note: This instance tests for equality with "1". We would
;; prefer if the cached result returned was actually 1, however
;; this is not the current behavior so this test is encoding
;; undesired behavior (because the current goal is simply to see
;; that caching is used on call lines).
;;
(should (equal (org-babel-lob-execute (org-babel-lob-get-info)) "1")))))
(provide 'test-ob-lob)
;;; test-ob-lob.el ends here