Fix tests related to export

* lisp/ob-exp.el (org-babel-exp-process-buffer): Renamed from
  `org-export-blocks-preprocess'.
* lisp/ox.el (org-export-execute-babel-code): Apply previous renaming.
* testing/org-test.el (org-test-at-id): Make sure the function returns
  the value of the last form in its body.
* testing/lisp/test-ob-exp.el: Fix tests.
* testing/lisp/test-ob-lob.el: Fix tests.
This commit is contained in:
Nicolas Goaziou 2013-01-28 00:09:58 +01:00
parent e14cf78056
commit 1cac3127c2
5 changed files with 151 additions and 197 deletions

View File

@ -241,7 +241,7 @@ this template."
(insert rep))))))))))))) (insert rep)))))))))))))
(defvar org-src-preserve-indentation) ; From org-src.el (defvar org-src-preserve-indentation) ; From org-src.el
(defun org-export-blocks-preprocess () (defun org-babel-exp-process-buffer ()
"Execute all blocks in visible part of buffer." "Execute all blocks in visible part of buffer."
(interactive) (interactive)
(save-window-excursion (save-window-excursion

View File

@ -3142,7 +3142,7 @@ file should have."
;; properly resolved. ;; properly resolved.
(let ((reference (org-export-copy-buffer))) (let ((reference (org-export-copy-buffer)))
(unwind-protect (let ((org-current-export-file reference)) (unwind-protect (let ((org-current-export-file reference))
(org-export-blocks-preprocess)) (org-babel-exp-process-buffer))
(kill-buffer reference)))) (kill-buffer reference))))

View File

@ -23,14 +23,29 @@
;; Template test file for Org-mode tests ;; Template test file for Org-mode tests
;;; Code: ;;; Code:
(defmacro org-test-with-expanded-babel-code (&rest body)
"Execute BODY while in a buffer with all Babel code evaluated.
Current buffer is a copy of the original buffer."
`(let ((string (buffer-string))
(buf (current-buffer)))
(with-temp-buffer
(org-mode)
(insert string)
(let ((org-current-export-file buf))
(org-babel-exp-process-buffer))
(goto-char (point-min))
(progn ,@body))))
(ert-deftest test-ob-exp/org-babel-exp-src-blocks/w-no-headers () (ert-deftest test-ob-exp/org-babel-exp-src-blocks/w-no-headers ()
"Testing export without any headlines in the org-mode file." "Testing export without any headlines in the Org mode file."
(require 'ox-html)
(let ((html-file (concat (file-name-sans-extension org-test-no-heading-file) (let ((html-file (concat (file-name-sans-extension org-test-no-heading-file)
".html"))) ".html")))
(when (file-exists-p html-file) (delete-file html-file)) (when (file-exists-p html-file) (delete-file html-file))
(org-test-in-example-file org-test-no-heading-file (org-test-in-example-file org-test-no-heading-file
;; export the file to html ;; Export the file to HTML.
(org-export-as-html nil)) (org-export-to-file 'html html-file))
;; should create a .html file ;; should create a .html file
(should (file-exists-p html-file)) (should (file-exists-p html-file))
;; should not create a file with "::" appended to it's name ;; should not create a file with "::" appended to it's name
@ -39,18 +54,17 @@
(ert-deftest test-ob-exp/org-babel-exp-src-blocks/w-no-file () (ert-deftest test-ob-exp/org-babel-exp-src-blocks/w-no-file ()
"Testing export from buffers which are not visiting any file." "Testing export from buffers which are not visiting any file."
(when (get-buffer "*Org HTML Export*") (kill-buffer "*Org HTML Export*")) (require 'ox-html)
(should-not (get-buffer "*Org HTML Export*")) (let ((name (generate-new-buffer-name "*Org HTML Export*")))
;; export the file to HTML in a temporary buffer (org-test-in-example-file nil
(org-test-in-example-file nil (org-export-as-html-to-buffer nil)) (org-export-to-buffer 'html name nil nil t))
;; should create a .html buffer ;; Should create a HTML buffer.
(should (buffer-live-p (get-buffer "*Org HTML Export*"))) (should (buffer-live-p (get-buffer name)))
;; should contain the content of the buffer ;; Should contain the content of the buffer.
(save-excursion (with-current-buffer (get-buffer name)
(set-buffer (get-buffer "*Org HTML Export*")) (should (string-match (regexp-quote org-test-file-ob-anchor)
(should (string-match (regexp-quote org-test-file-ob-anchor) (buffer-string))))
(buffer-string)))) (when (get-buffer name) (kill-buffer name))))
(when (get-buffer "*Org HTML Export*") (kill-buffer "*Org HTML Export*")))
(ert-deftest test-ob-exp/org-babel-exp-src-blocks/w-no-headers2 () (ert-deftest test-ob-exp/org-babel-exp-src-blocks/w-no-headers2 ()
"Testing export without any headlines in the org-mode file." "Testing export without any headlines in the org-mode file."
@ -60,7 +74,7 @@
(when (file-exists-p html-file) (delete-file html-file)) (when (file-exists-p html-file) (delete-file html-file))
(org-test-in-example-file org-test-link-in-heading-file (org-test-in-example-file org-test-link-in-heading-file
;; export the file to html ;; export the file to html
(org-export-as-html nil)) (org-export-to-file 'html html-file))
;; should create a .html file ;; should create a .html file
(should (file-exists-p html-file)) (should (file-exists-p html-file))
;; should not create a file with "::" appended to it's name ;; should not create a file with "::" appended to it's name
@ -72,134 +86,72 @@
- yes expand on both export and tangle - yes expand on both export and tangle
- no expand on neither export or tangle - no expand on neither export or tangle
- tangle expand on only tangle not export" - tangle expand on only tangle not export"
(org-test-at-id "eb1f6498-5bd9-45e0-9c56-50717053e7b7" (should
(org-narrow-to-subtree) (equal
(let ((exported-html '("(message \"expanded1\")" "(message \"expanded2\")" ";; noweb-1-yes-start
(org-export-as-html nil nil 'string 'body-only)) (message \"expanded1\")
(test-point 0)) (message \"expanded1\")" ";; noweb-no-start
<<noweb-example1>>" ";; noweb-2-yes-start
(org-test-with-temp-text-in-file (message \"expanded2\")
exported-html (message \"expanded2\")" ";; noweb-tangle-start
<<noweb-example1>>
;; check following ouput exists and in order <<noweb-example2>>")
(mapcar (lambda (x) (org-test-at-id "eb1f6498-5bd9-45e0-9c56-50717053e7b7"
(should (< test-point (org-narrow-to-subtree)
(re-search-forward (org-element-map
x (org-test-with-expanded-babel-code (org-element-parse-buffer))
nil t))) 'src-block
(setq test-point (point))) (lambda (src) (org-trim (org-element-property :value src))))))))
'("<code>:noweb</code> header argument expansion"
"message" "expanded1"
"message" "expanded2"
"noweb-1-yes-start"
"message" "expanded1"
"noweb-no-start"
"&lt;&lt;noweb-example1&gt;&gt;"
"noweb-2-yes-start"
"message" "expanded2"
"noweb-tangle-start"
"&lt;&lt;noweb-example1&gt;&gt;"
"&lt;&lt;noweb-example2&gt;&gt;"))))))
(ert-deftest ob-exp/noweb-on-export-with-exports-results () (ert-deftest ob-exp/noweb-on-export-with-exports-results ()
"Noweb header arguments export correctly using :exports results. "Noweb header arguments export correctly using :exports results.
- yes expand on both export and tangle - yes expand on both export and tangle
- no expand on neither export or tangle - no expand on neither export or tangle
- tangle expand on only tangle not export" - tangle expand on only tangle not export"
(org-test-at-id "8701beb4-13d9-468c-997a-8e63e8b66f8d" (should
(org-narrow-to-subtree) (equal
(let ((exported-html '(";; noweb-no-start
(org-export-as-html nil nil 'string 'body-only)) <<noweb-example1>>" "<<noweb-example1>>
(test-point 0)) <<noweb-example2>>")
(org-test-at-id "8701beb4-13d9-468c-997a-8e63e8b66f8d"
(org-test-with-temp-text-in-file (org-narrow-to-subtree)
exported-html (org-element-map
(org-test-with-expanded-babel-code (org-element-parse-buffer))
;; check following ouput exists and in order 'src-block
(mapcar (lambda (x) (lambda (src) (org-trim (org-element-property :value src))))))))
(should (< test-point
(re-search-forward
x
nil t)))
(setq test-point (point)))
'("<code>:noweb</code> header argument expansion using :exports results"
"expanded1"
"expanded2"
"expanded1"
"noweb-no-start"
"&lt;&lt;noweb-example1&gt;&gt;"
"expanded2"
"&lt;&lt;noweb-example1&gt;&gt;"
"&lt;&lt;noweb-example2&gt;&gt;"))))))
(ert-deftest ob-exp/exports-both () (ert-deftest ob-exp/exports-both ()
"Test the :exports both header argument. "Test the \":exports both\" header argument.
The code block should create both <pre></pre> and <table></table> The code block evaluation should create both a code block and
elements in the final html." a table."
(org-test-at-id "92518f2a-a46a-4205-a3ab-bcce1008a4bb" (org-test-at-id "92518f2a-a46a-4205-a3ab-bcce1008a4bb"
(org-narrow-to-subtree) (org-narrow-to-subtree)
(let ((exported-html (let ((tree (org-test-with-expanded-babel-code (org-element-parse-buffer))))
(org-export-as-html nil nil 'string 'body-only)) (should (and (org-element-map tree 'src-block 'identity)
(test-point 0)) (org-element-map tree 'table 'identity))))))
(org-test-with-temp-text-in-file
exported-html
;; check following ouput exists and in order
(mapcar (lambda (x)
(should (< test-point
(re-search-forward
x
nil t)))
(setq test-point (point)))
'( "Pascal's Triangle &ndash; exports both test"
"<pre"
"defun" "pascals-triangle"
"if""list""list""let*""prev-triangle"
"pascals-triangle""prev-row""car""reverse""prev-triangle"
"append""prev-triangle""list""map""list"
"append""prev-row""append""prev-row""pascals-triangle"
"</pre>"
"<table""<tbody>"
"<tr>"">1<""</tr>"
"<tr>"">1<"">1<""</tr>"
"<tr>"">1<"">2<"">1<""</tr>"
"<tr>"">1<"">3<"">3<"">1<""</tr>"
"<tr>"">1<"">4<"">6<"">4<"">1<""</tr>"
"<tr>"">1<"">5<"">10<"">10<"">5<"">1<""</tr>"
"</tbody>""</table>"))))))
(ert-deftest ob-exp/mixed-blocks-with-exports-both () (ert-deftest ob-exp/mixed-blocks-with-exports-both ()
(org-test-at-id "5daa4d03-e3ea-46b7-b093-62c1b7632df3" (should
(org-narrow-to-subtree) (equal
(let ((exported-html '(property-drawer plain-list src-block fixed-width src-block plain-list)
(org-export-as-html nil nil 'string 'body-only)) (org-test-at-id "5daa4d03-e3ea-46b7-b093-62c1b7632df3"
(test-point 0)) (org-narrow-to-subtree)
(org-test-with-temp-text-in-file (mapcar 'org-element-type
exported-html (org-element-map
;; check following ouput exists and in order (org-test-with-expanded-babel-code
(mapcar (lambda (x) (org-element-parse-buffer 'greater-element))
(should (< test-point (re-search-forward x nil t))) 'section 'org-element-contents nil t))))))
(setq test-point (point)))
'("mixed blocks with exports both"
"<ul class=\"org-ul\">"
"<li>""a""</li>"
"<li>""b""</li>"
"<li>""c""</li>"
"</ul>"
"<pre"
"\"code block results\""
"</pre>"
"<pre class=\"example\">"
"code block results"
"</pre>"))))))
(ert-deftest ob-exp/export-with-name () (ert-deftest ob-exp/export-with-name ()
(let ((org-babel-exp-code-template (should
"=%name=\n#+BEGIN_SRC %lang%flags\nbody\n#+END_SRC")) (string-match
(org-test-at-id "b02ddd8a-eeb8-42ab-8664-8a759e6f43d9" "=qux="
(org-narrow-to-subtree) (let ((org-babel-exp-code-template
(let ((ascii (org-export-as-ascii nil nil 'string 'body-only))) "=%name=\n#+BEGIN_SRC %lang%flags\nbody\n#+END_SRC"))
(should (string-match "qux" ascii)))))) (org-test-at-id "b02ddd8a-eeb8-42ab-8664-8a759e6f43d9"
(org-narrow-to-subtree)
(org-test-with-expanded-babel-code
(buffer-string)))))))
(ert-deftest ob-exp/export-with-header-argument () (ert-deftest ob-exp/export-with-header-argument ()
(let ((org-babel-exp-code-template (let ((org-babel-exp-code-template
@ -211,50 +163,58 @@ elements in the final html."
#+BEGIN_SRC %lang%flags\nbody\n#+END_SRC")) #+BEGIN_SRC %lang%flags\nbody\n#+END_SRC"))
(org-test-at-id "b02ddd8a-eeb8-42ab-8664-8a759e6f43d9" (org-test-at-id "b02ddd8a-eeb8-42ab-8664-8a759e6f43d9"
(org-narrow-to-subtree) (org-narrow-to-subtree)
(let ((ascii (org-export-as-ascii nil nil 'string 'body-only))) (org-test-with-expanded-babel-code
(should (string-match "baz" ascii)) (should (string-match "baz" (buffer-string)))
(should (string-match "replace" ascii)))))) (should (string-match "replace" (buffer-string)))))))
(ert-deftest ob-exp/noweb-no-export-and-exports-both () (ert-deftest ob-exp/noweb-no-export-and-exports-both ()
(org-test-at-id "8a820f6c-7980-43db-8a24-0710d33729c9" (should
(org-narrow-to-subtree) (string-match
(let ((html (org-export-as-html nil nil 'string 'body-only))) "<<noweb-no-export-and-exports-both-1>>"
(should (string-match (regexp-quote "noweb-no-export-and-exports-both-1") (org-test-at-id "8a820f6c-7980-43db-8a24-0710d33729c9"
html))))) (org-narrow-to-subtree)
(org-test-with-expanded-babel-code
(org-element-map (org-element-parse-buffer) 'src-block
(lambda (src-block) (org-element-property :value src-block))
nil t))))))
(ert-deftest ob-exp/evaluate-all-executables-in-order () (ert-deftest ob-exp/evaluate-all-executables-in-order ()
(org-test-at-id "96cc7073-97ec-4556-87cf-1f9bffafd317" (should
(org-narrow-to-subtree) (equal '(5 4 3 2 1)
(let (*evaluation-collector*) (let (*evaluation-collector*)
(org-export-as-ascii nil nil 'string) (org-test-at-id "96cc7073-97ec-4556-87cf-1f9bffafd317"
(should (equal '(5 4 3 2 1) *evaluation-collector*))))) (org-narrow-to-subtree)
(buffer-string)
(fboundp 'org-export-execute-babel-code)
(org-test-with-expanded-babel-code *evaluation-collector*))))))
(ert-deftest ob-exp/exports-inline () (ert-deftest ob-exp/exports-inline ()
(org-test-at-id "54cb8dc3-298c-4883-a933-029b3c9d4b18" (should
(org-narrow-to-subtree) (string-match
(let ((html (org-export-as-html nil nil 'string 'body-only))) (regexp-quote "Here is one in the middle =1= of a line.
(dolist (rx '("middle <\\(code\\|tt\\)>1</\\(code\\|tt\\)> of" Here is one at the end of a line. =2=
"end of a line. <\\(code\\|tt\\)>2</\\(code\\|tt\\)>" =3= Here is one at the beginning of a line.")
"<\\(code\\|tt\\)>3</\\(code\\|tt\\)> Here is one")) (org-test-at-id "54cb8dc3-298c-4883-a933-029b3c9d4b18"
(should (string-match rx html)))))) (org-narrow-to-subtree)
(org-test-with-expanded-babel-code (buffer-string))))))
(ert-deftest ob-exp/export-call-line-information () (ert-deftest ob-exp/export-call-line-information ()
(org-test-at-id "bec63a04-491e-4caa-97f5-108f3020365c" (org-test-at-id "bec63a04-491e-4caa-97f5-108f3020365c"
(org-narrow-to-subtree) (org-narrow-to-subtree)
(let* ((org-babel-exp-call-line-template "\n: call: %line special-token") (let ((org-babel-exp-call-line-template "\n: call: %line special-token"))
(html (org-export-as-html nil nil 'string t))) (org-test-with-expanded-babel-code
(should (string-match "double" html)) (should (string-match "double" (buffer-string)))
(should (string-match "16" html)) (should (string-match "16" (buffer-string)))
(should (string-match "special-token" html))))) (should (string-match "special-token" (buffer-string)))))))
(ert-deftest ob-exp/noweb-strip-export-ensure-strips () (ert-deftest ob-exp/noweb-strip-export-ensure-strips ()
(org-test-at-id "8e7bd234-99b2-4b14-8cd6-53945e409775" (org-test-at-id "8e7bd234-99b2-4b14-8cd6-53945e409775"
(org-narrow-to-subtree) (org-narrow-to-subtree)
(org-babel-next-src-block 2) (org-babel-next-src-block 2)
(should (= 110 (org-babel-execute-src-block))) (should (= 110 (org-babel-execute-src-block)))
(let ((ascii (org-export-as-ascii nil nil 'string t))) (let ((result (org-test-with-expanded-babel-code (buffer-string))))
(should-not (string-match (regexp-quote "<<strip-export-1>>") ascii)) (should-not (string-match (regexp-quote "<<strip-export-1>>") result))
(should-not (string-match (regexp-quote "i=\"10\"") ascii))))) (should-not (string-match (regexp-quote "i=\"10\"") result)))))
(ert-deftest ob-exp/export-from-a-temp-buffer () (ert-deftest ob-exp/export-from-a-temp-buffer ()
:expected-result :failed :expected-result :failed
@ -276,8 +236,7 @@ elements in the final html."
(list foo <<bar>>) (list foo <<bar>>)
#+END_SRC #+END_SRC
" "
(let* ((org-current-export-file (current-buffer)) (let* ((ascii (org-export-as 'ascii)))
(ascii (org-export-as-ascii nil nil 'string)))
(should (string-match (regexp-quote (format nil "%S" '(:foo :bar))) (should (string-match (regexp-quote (format nil "%S" '(:foo :bar)))
ascii))))) ascii)))))

View File

@ -80,37 +80,31 @@
"Test the export of a variety of library babel call lines." "Test the export of a variety of library babel call lines."
(org-test-at-id "72ddeed3-2d17-4c7f-8192-a575d535d3fc" (org-test-at-id "72ddeed3-2d17-4c7f-8192-a575d535d3fc"
(org-narrow-to-subtree) (org-narrow-to-subtree)
(let ((html (org-export-as-html nil nil 'string 'body-only))) (let ((buf (current-buffer))
;; check the location of each exported number (string (buffer-string)))
(with-temp-buffer (with-temp-buffer
(insert html) (goto-char (point-min)) (org-mode)
;; 0 should be on a line by itself (insert string)
(should (re-search-forward "0" nil t)) (let ((org-current-export-file buf))
(should (string= "0" (buffer-substring (point-at-bol) (point-at-eol)))) (org-babel-exp-process-buffer))
;; 2 should be in <code> tags (message (buffer-string))
(should (re-search-forward "2" nil t)) (should (re-search-forward "^: 0" nil t))
(should (re-search-forward (regexp-quote "</code>") (point-at-eol) t)) (should (re-search-forward "call =2= stuck" nil t))
(should (re-search-backward (regexp-quote "<code>") (point-at-bol) t)) (should (re-search-forward
;; 4 should not be exported "exported =call_double(it=2)= because" nil t))
(should (not (re-search-forward "4" nil t))) (should (re-search-forward "^=6= because" nil t))
;; 6 should also be inline (should (re-search-forward "results 8 should" nil t))
(should (re-search-forward "6" nil t)) (should (re-search-forward "following 2\\*5==10= should" nil t))))))
(should (re-search-forward (regexp-quote "</code>") (point-at-eol) t))
(should (re-search-backward (regexp-quote "<code>") (point-at-bol) t))
;; 8 should not be quoted
(should (re-search-forward "8" nil t))
(should (not (= ?= (char-after (point)))))
(should (not (= ?= (char-before (- (point) 1)))))
;; 10 should export
(should (re-search-forward "10" nil t))))))
(ert-deftest test-ob-lob/do-not-eval-lob-lines-in-example-blocks-on-export () (ert-deftest test-ob-lob/do-not-eval-lob-lines-in-example-blocks-on-export ()
(require 'ox)
(org-test-with-temp-text-in-file " (org-test-with-temp-text-in-file "
for export for export
#+begin_example #+begin_example
#+call: rubbish() #+call: rubbish()
#+end_example" #+end_example"
(org-export-as-html nil))) (should (progn (org-export-execute-babel-code) t))))
(provide 'test-ob-lob) (provide 'test-ob-lob)

View File

@ -136,18 +136,19 @@ currently executed.")
(id-file (car id-location)) (id-file (car id-location))
(visited-p (get-file-buffer id-file)) (visited-p (get-file-buffer id-file))
to-be-removed) to-be-removed)
(save-window-excursion (unwind-protect
(save-match-data (save-window-excursion
(org-id-goto ,id) (save-match-data
(setq to-be-removed (current-buffer)) (org-id-goto ,id)
(condition-case nil (setq to-be-removed (current-buffer))
(progn (condition-case nil
(org-show-subtree) (progn
(org-show-block-all)) (org-show-subtree)
(error nil)) (org-show-block-all))
(save-restriction ,@body))) (error nil))
(unless visited-p (save-restriction ,@body)))
(kill-buffer to-be-removed)))) (unless (or visited-p (not to-be-removed))
(kill-buffer to-be-removed)))))
(def-edebug-spec org-test-at-id (form body)) (def-edebug-spec org-test-at-id (form body))
(defmacro org-test-in-example-file (file &rest body) (defmacro org-test-in-example-file (file &rest body)