test-ob-exp.el 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  1. ;;; test-ob-exp.el -*- lexical-binding: t; -*-
  2. ;; Copyright (c) 2010-2015, 2019 Eric Schulte
  3. ;; Authors: Eric Schulte
  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. ;; Template test file for Org tests
  17. ;;; Code:
  18. (require 'ob-exp)
  19. (require 'org-src)
  20. (require 'org-test "../testing/org-test")
  21. (defmacro org-test-with-expanded-babel-code (&rest body)
  22. "Execute BODY while in a buffer with all Babel code evaluated.
  23. Current buffer is a copy of the original buffer."
  24. `(let ((string (org-with-wide-buffer (buffer-string)))
  25. (narrowing (list (point-min) (point-max)))
  26. (org-export-use-babel t))
  27. (with-temp-buffer
  28. (org-mode)
  29. (insert string)
  30. (apply #'narrow-to-region narrowing)
  31. (org-babel-exp-process-buffer)
  32. (goto-char (point-min))
  33. (progn ,@body))))
  34. (ert-deftest test-ob-exp/org-babel-exp-src-blocks/w-no-headers ()
  35. "Testing export without any headlines in the Org mode file."
  36. (require 'ox-ascii)
  37. (let ((text-file (concat (file-name-sans-extension org-test-no-heading-file)
  38. ".txt")))
  39. (when (file-exists-p text-file) (delete-file text-file))
  40. (org-test-in-example-file org-test-no-heading-file
  41. ;; Export the file to HTML.
  42. (org-export-to-file 'ascii text-file))
  43. ;; should create a ".txt" file
  44. (should (file-exists-p text-file))
  45. ;; should not create a file with "::" appended to its name
  46. (should-not (file-exists-p (concat org-test-no-heading-file "::")))
  47. (when (file-exists-p text-file) (delete-file text-file))))
  48. (ert-deftest test-ob-exp/org-babel-exp-src-blocks/w-no-file ()
  49. "Testing export from buffers which are not visiting any file."
  50. (require 'ox-ascii)
  51. (let ((name (generate-new-buffer-name "*Org ASCII Export*")))
  52. (org-test-in-example-file nil
  53. (org-export-to-buffer 'ascii name nil nil nil t))
  54. ;; Should create a new buffer.
  55. (should (buffer-live-p (get-buffer name)))
  56. ;; Should contain the content of the buffer.
  57. (with-current-buffer (get-buffer name)
  58. (should (string-match (regexp-quote org-test-file-ob-anchor)
  59. (buffer-string))))
  60. (when (get-buffer name) (kill-buffer name))))
  61. (ert-deftest test-ob-exp/org-babel-exp-src-blocks/w-no-headers2 ()
  62. "Testing export without any headlines in the Org file."
  63. (let ((html-file (concat (file-name-sans-extension
  64. org-test-link-in-heading-file)
  65. ".html")))
  66. (when (file-exists-p html-file) (delete-file html-file))
  67. (org-test-in-example-file org-test-link-in-heading-file
  68. ;; export the file to html
  69. (org-export-to-file 'html html-file))
  70. ;; should create a .html file
  71. (should (file-exists-p html-file))
  72. ;; should not create a file with "::" appended to its name
  73. (should-not (file-exists-p (concat org-test-link-in-heading-file "::")))
  74. (when (file-exists-p html-file) (delete-file html-file))))
  75. (ert-deftest ob-exp/noweb-on-export ()
  76. "Noweb header arguments export correctly.
  77. - yes expand on both export and tangle
  78. - no expand on neither export or tangle
  79. - tangle expand on only tangle not export"
  80. (should
  81. (equal
  82. '("(message \"expanded1\")" "(message \"expanded2\")" ";; noweb-1-yes-start
  83. (message \"expanded1\")" ";; noweb-no-start
  84. <<noweb-example1>>" ";; noweb-2-yes-start
  85. (message \"expanded2\")"
  86. ";; noweb-tangle-start
  87. <<noweb-example1>>
  88. <<noweb-example2>>")
  89. (org-test-at-id "eb1f6498-5bd9-45e0-9c56-50717053e7b7"
  90. (org-narrow-to-subtree)
  91. (org-element-map
  92. (org-test-with-expanded-babel-code (org-element-parse-buffer))
  93. 'src-block
  94. (lambda (src) (org-trim (org-element-property :value src))))))))
  95. (ert-deftest ob-exp/noweb-on-export-with-exports-results ()
  96. "Noweb header arguments export correctly using :exports results.
  97. - yes expand on both export and tangle
  98. - no expand on neither export or tangle
  99. - tangle expand on only tangle not export"
  100. (should
  101. (equal
  102. '(";; noweb-no-start
  103. <<noweb-example1>>" "<<noweb-example1>>
  104. <<noweb-example2>>")
  105. (org-test-at-id "8701beb4-13d9-468c-997a-8e63e8b66f8d"
  106. (org-narrow-to-subtree)
  107. (org-element-map
  108. (org-test-with-expanded-babel-code (org-element-parse-buffer))
  109. 'src-block
  110. (lambda (src) (org-trim (org-element-property :value src))))))))
  111. (ert-deftest ob-exp/exports-both ()
  112. "Test the \":exports both\" header argument.
  113. The code block evaluation should create both a code block and
  114. a table."
  115. (org-test-at-id "92518f2a-a46a-4205-a3ab-bcce1008a4bb"
  116. (org-narrow-to-subtree)
  117. (let ((tree (org-test-with-expanded-babel-code (org-element-parse-buffer))))
  118. (should (and (org-element-map tree 'src-block 'identity)
  119. (org-element-map tree 'table 'identity))))))
  120. (ert-deftest ob-exp/mixed-blocks-with-exports-both ()
  121. (should
  122. (equal
  123. '(property-drawer plain-list src-block fixed-width src-block plain-list)
  124. (org-test-at-id "5daa4d03-e3ea-46b7-b093-62c1b7632df3"
  125. (org-narrow-to-subtree)
  126. (mapcar #'org-element-type
  127. (org-element-map
  128. (org-test-with-expanded-babel-code
  129. (org-element-parse-buffer 'greater-element))
  130. 'section 'org-element-contents nil t))))))
  131. (ert-deftest ob-exp/export-with-name ()
  132. (should
  133. (string-match
  134. "=qux="
  135. (let ((org-babel-exp-code-template
  136. "=%name=\n#+BEGIN_SRC %lang%flags\nbody\n#+END_SRC"))
  137. (org-test-at-id "b02ddd8a-eeb8-42ab-8664-8a759e6f43d9"
  138. (org-narrow-to-subtree)
  139. (org-test-with-expanded-babel-code
  140. (buffer-string)))))))
  141. (ert-deftest ob-exp/export-with-header-argument ()
  142. (let ((org-babel-exp-code-template
  143. "
  144. | header | value |
  145. |---------+----------|
  146. | foo | %foo |
  147. | results | %results |
  148. #+BEGIN_SRC %lang%flags\nbody\n#+END_SRC"))
  149. (org-test-at-id "b02ddd8a-eeb8-42ab-8664-8a759e6f43d9"
  150. (org-narrow-to-subtree)
  151. (org-test-with-expanded-babel-code
  152. (should (string-match "baz" (buffer-string)))
  153. (should (string-match "replace" (buffer-string)))))))
  154. (ert-deftest ob-exp/noweb-no-export-and-exports-both ()
  155. (should
  156. (string-match
  157. "<<noweb-no-export-and-exports-both-1>>"
  158. (org-test-at-id "8a820f6c-7980-43db-8a24-0710d33729c9"
  159. (org-narrow-to-subtree)
  160. (org-test-with-expanded-babel-code
  161. (org-element-map (org-element-parse-buffer) 'src-block
  162. (lambda (src-block) (org-element-property :value src-block))
  163. nil t))))))
  164. (ert-deftest ob-exp/evaluate-all-executables-in-order ()
  165. (defvar *evaluation-collector*)
  166. (should
  167. (equal '(5 4 3 2 1)
  168. (let ((org-export-use-babel t) *evaluation-collector*)
  169. (org-test-at-id "96cc7073-97ec-4556-87cf-1f9bffafd317"
  170. (org-narrow-to-subtree)
  171. (buffer-string)
  172. (org-test-with-expanded-babel-code *evaluation-collector*))))))
  173. (ert-deftest ob-exp/exports-inline ()
  174. (should
  175. (string-match
  176. (regexp-quote "Here is one in the middle {{{results(=1=)}}} of a line.
  177. Here is one at the end of a line. {{{results(=2=)}}}
  178. {{{results(=3=)}}} Here is one at the beginning of a line.")
  179. (org-test-at-id "54cb8dc3-298c-4883-a933-029b3c9d4b18"
  180. (org-narrow-to-subtree)
  181. (let ((org-babel-inline-result-wrap "=%s="))
  182. (org-test-with-expanded-babel-code (buffer-string)))))))
  183. (ert-deftest ob-exp/exports-inline-code ()
  184. (should
  185. (equal "src_emacs-lisp[]{(+ 1 1)}"
  186. (org-test-with-temp-text "src_emacs-lisp[:exports code]{(+ 1 1)}"
  187. (let ((org-babel-inline-result-wrap "=%s=")
  188. (org-export-use-babel t))
  189. (org-babel-exp-process-buffer))
  190. (buffer-string))))
  191. (should
  192. (equal "src_emacs-lisp[]{(+ 1 1)}"
  193. (org-test-with-temp-text "src_emacs-lisp[ :exports code ]{(+ 1 1)}"
  194. (let ((org-babel-inline-result-wrap "=%s=")
  195. (org-export-use-babel t))
  196. (org-babel-exp-process-buffer))
  197. (buffer-string))))
  198. ;; Do not escape characters in inline source blocks.
  199. (should
  200. (equal "src_c[]{*a}"
  201. (org-test-with-temp-text "src_c[ :exports code ]{*a}"
  202. (let ((org-babel-inline-result-wrap "=%s=")
  203. (org-export-use-babel t))
  204. (org-babel-exp-process-buffer))
  205. (buffer-string))))
  206. (should
  207. (equal "src_emacs-lisp[]{(+ 1 1)} {{{results(=2=)}}}"
  208. (org-test-with-temp-text "src_emacs-lisp[:exports both]{(+ 1 1)}"
  209. (let ((org-babel-inline-result-wrap "=%s=")
  210. (org-export-use-babel t))
  211. (org-babel-exp-process-buffer))
  212. (buffer-string))))
  213. (should
  214. (equal "{{{results(=2=)}}}"
  215. (org-test-with-temp-text
  216. "src_emacs-lisp[:exports results :results scalar]{(+ 1 1)}"
  217. (let ((org-babel-inline-result-wrap "=%s=")
  218. (org-export-use-babel t))
  219. (org-babel-exp-process-buffer))
  220. (buffer-string))))
  221. (should
  222. (equal "foosrc_emacs-lisp[:exports code]{(+ 1 1)}"
  223. (org-test-with-temp-text
  224. "foosrc_emacs-lisp[:exports code]{(+ 1 1)}"
  225. (let ((org-babel-inline-result-wrap "=%s=")
  226. (org-export-use-babel t))
  227. (org-babel-exp-process-buffer))
  228. (buffer-string))))
  229. (should
  230. (let ((text "src_emacs lisp{(+ 1 1)}"))
  231. (string-match (regexp-quote text)
  232. (org-test-with-temp-text
  233. text
  234. (let ((org-babel-inline-result-wrap "=%s=")
  235. (org-export-use-babel t))
  236. (org-babel-exp-process-buffer))
  237. (buffer-string)))))
  238. (should
  239. (string-match
  240. (replace-regexp-in-string
  241. "\\\\\\[]{" "\\(?:\\[]\\)?{" ;accept both src_sh[]{...} or src_sh{...}
  242. (regexp-quote "Here is one in the middle src_sh[]{echo 1} of a line.
  243. Here is one at the end of a line. src_sh[]{echo 2}
  244. src_sh[]{echo 3} Here is one at the beginning of a line.
  245. Here is one that is also evaluated: src_sh[]{echo 4} {{{results(=4=)}}}")
  246. nil t)
  247. (org-test-at-id "cd54fc88-1b6b-45b6-8511-4d8fa7fc8076"
  248. (org-narrow-to-subtree)
  249. (let ((org-babel-inline-result-wrap "=%s=")
  250. (org-export-use-babel t))
  251. (org-test-with-expanded-babel-code (buffer-string)))))))
  252. (ert-deftest ob-exp/exports-inline-code-double-eval ()
  253. "Based on default header arguments for inline code blocks (:exports
  254. results), the resulting code block `src_emacs-lisp{2}' should also be
  255. evaluated."
  256. (let ((org-babel-inline-result-wrap "=%s=")
  257. (org-export-use-babel t))
  258. (should
  259. (string-match "\\`{{{results(src_emacs-lisp\\[\\]{2})}}}$"
  260. (org-test-with-temp-text
  261. "src_emacs-lisp[:exports results :results code]{(+ 1 1)}"
  262. (org-babel-exp-process-buffer)
  263. (buffer-string))))))
  264. (ert-deftest ob-exp/exports-inline-code-eval-code-once ()
  265. "Ibid above, except that the resulting inline code block should not
  266. be evaluated."
  267. (let ((org-export-use-babel t))
  268. (should
  269. (string-match "{{{results(src_emacs-lisp\\(?:\\[[: a-zA-Z]+]\\)?{2})}}}$"
  270. (org-test-with-temp-text
  271. (concat "src_emacs-lisp[:exports results :results code "
  272. ":results_switches \":exports code\"]{(+ 1 1)}")
  273. (org-babel-exp-process-buffer)
  274. (buffer-string))))))
  275. (ert-deftest ob-exp/exports-inline-code-double-eval-exports-both ()
  276. (let ((org-export-use-babel t))
  277. (should
  278. (string-match (concat "\\`src_emacs-lisp\\(?:\\[]\\)?{(\\+ 1 1)} "
  279. "{{{results(src_emacs-lisp\\[ :exports code\\]{2})}}}$")
  280. (org-test-with-temp-text
  281. (concat "src_emacs-lisp[:exports both :results code "
  282. ":results_switches \":exports code\"]{(+ 1 1)}")
  283. (org-babel-exp-process-buffer)
  284. (buffer-string))))))
  285. (ert-deftest ob-exp/export-call-line-information ()
  286. (org-test-at-id "bec63a04-491e-4caa-97f5-108f3020365c"
  287. (org-narrow-to-subtree)
  288. (let ((org-babel-exp-call-line-template "\n: call: %line special-token"))
  289. (org-test-with-expanded-babel-code
  290. (should (string-match "double" (buffer-string)))
  291. (should (string-match "16" (buffer-string)))
  292. (should (string-match "special-token" (buffer-string)))))))
  293. (ert-deftest ob-exp/noweb-strip-export-ensure-strips ()
  294. (org-test-at-id "8e7bd234-99b2-4b14-8cd6-53945e409775"
  295. (org-narrow-to-subtree)
  296. (org-babel-next-src-block 2)
  297. (should (= 110 (org-babel-execute-src-block)))
  298. (let ((result (org-test-with-expanded-babel-code (buffer-string))))
  299. (should-not (string-match (regexp-quote "<<strip-export-1>>") result))
  300. (should-not (string-match (regexp-quote "i=\"10\"") result)))))
  301. (ert-deftest ob-exp/use-case-of-reading-entry-properties ()
  302. (org-test-at-id "cc5fbc20-bca5-437a-a7b8-2b4d7a03f820"
  303. (org-narrow-to-subtree)
  304. (let* ((case-fold-search nil)
  305. (result (org-test-with-expanded-babel-code (buffer-string)))
  306. (sect "a:1, b:0, c:3, d:0, e:0")
  307. (sub0 "a:1, b:2, c:4, d:0, e:0")
  308. (sub1 "a:1, b:2, c:5, d:0, e:6")
  309. (func sub0))
  310. ;; entry "section"
  311. (should (string-match (concat "_shell-sect-call\n: shell " sect "\n")
  312. result))
  313. (should (string-match (concat "_elisp-sect-call\n: elisp " sect "\n")
  314. result))
  315. (should (string-match (concat "\n- sect inline shell " sect "\n")
  316. result))
  317. (should (string-match (concat "\n- sect inline elisp " sect "\n")
  318. result))
  319. ;; entry "subsection", call without arguments
  320. (should (string-match (concat "_shell-sub0-call\n: shell " sub0 "\n")
  321. result))
  322. (should (string-match (concat "_elisp-sub0-call\n: elisp " sub0 "\n")
  323. result))
  324. (should (string-match (concat "\n- sub0 inline shell " sub0 "\n")
  325. result))
  326. (should (string-match (concat "\n- sub0 inline elisp " sub0 "\n")
  327. result))
  328. ;; entry "subsection", call with arguments
  329. (should (string-match (concat "_shell-sub1-call\n: shell " sub1 "\n")
  330. result))
  331. (should (string-match (concat "_elisp-sub1-call\n: elisp " sub1 "\n")
  332. result))
  333. (should (string-match (concat "\n- sub1 inline shell " sub1 "\n")
  334. result))
  335. (should (string-match (concat "\n- sub1 inline elisp " sub1 "\n")
  336. result))
  337. ;; entry "function definition"
  338. (should (string-match (concat "_location_shell\n: shell " func "\n")
  339. result))
  340. (should (string-match (concat "_location_elisp\n: elisp " func "\n")
  341. result)))))
  342. (ert-deftest ob-exp/export-from-a-temp-buffer ()
  343. (let ((org-export-use-babel t))
  344. (org-test-with-temp-text
  345. "
  346. #+Title: exporting from a temporary buffer
  347. #+name: foo
  348. #+BEGIN_SRC emacs-lisp
  349. :foo
  350. #+END_SRC
  351. #+name: bar
  352. #+BEGIN_SRC emacs-lisp
  353. :bar
  354. #+END_SRC
  355. #+BEGIN_SRC emacs-lisp :var foo=foo :noweb yes :exports results
  356. (list foo <<bar>>)
  357. #+END_SRC
  358. "
  359. (let* ((ascii (org-export-as 'ascii)))
  360. (should (string-match
  361. (regexp-quote " :foo :bar \n")
  362. ascii))))))
  363. (ert-deftest ob-export/export-with-results-before-block ()
  364. "Test export when results are inserted before source block."
  365. (let ((org-export-use-babel t))
  366. (should
  367. (equal
  368. "#+RESULTS: src1
  369. : 2
  370. #+NAME: src1
  371. #+begin_src emacs-lisp
  372. \(+ 1 1)
  373. #+end_src"
  374. (org-test-with-temp-text
  375. "#+RESULTS: src1
  376. #+NAME: src1
  377. #+BEGIN_SRC emacs-lisp :exports both
  378. \(+ 1 1)
  379. #+END_SRC"
  380. (org-babel-exp-process-buffer)
  381. (org-trim (org-no-properties (buffer-string))))))))
  382. (ert-deftest ob-export/export-src-block-with-switches ()
  383. "Test exporting a source block with switches."
  384. (should
  385. (string-match
  386. "\\`#\\+BEGIN_SRC emacs-lisp -n -r$"
  387. (org-test-with-temp-text
  388. "#+BEGIN_SRC emacs-lisp -n -r\n\(+ 1 1)\n#+END_SRC"
  389. (org-babel-exp-process-buffer)
  390. (buffer-string)))))
  391. (ert-deftest ob-export/export-src-block-with-flags ()
  392. "Test exporting a source block with a flag."
  393. (should
  394. (string-match
  395. "\\`#\\+BEGIN_SRC emacs-lisp -some-flag$"
  396. (org-test-with-temp-text
  397. "#+BEGIN_SRC emacs-lisp :flags -some-flag\n\(+ 1 1)\n#+END_SRC"
  398. (org-babel-exp-process-buffer)
  399. (buffer-string)))))
  400. (ert-deftest ob-export/export-and-indentation ()
  401. "Test indentation of evaluated source blocks during export."
  402. ;; No indentation.
  403. (should
  404. (string-match
  405. "^t"
  406. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n t\n#+END_SRC"
  407. (let ((indent-tabs-mode t)
  408. (tab-width 1)
  409. (org-src-preserve-indentation nil))
  410. (org-babel-exp-process-buffer)
  411. (buffer-string)))))
  412. ;; Preserve indentation with "-i" flag.
  413. (should
  414. (string-match
  415. "^ t"
  416. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp -i\n t\n#+END_SRC"
  417. (let ((indent-tabs-mode t)
  418. (tab-width 1))
  419. (org-babel-exp-process-buffer)
  420. (buffer-string)))))
  421. ;; Preserve indentation with a non-nil
  422. ;; `org-src-preserve-indentation'.
  423. (should
  424. (string-match
  425. "^ t"
  426. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n t\n#+END_SRC"
  427. (let ((indent-tabs-mode t)
  428. (tab-width 1)
  429. (org-src-preserve-indentation t))
  430. (org-babel-exp-process-buffer)
  431. (buffer-string))))))
  432. (ert-deftest ob-export/export-under-commented-headline ()
  433. "Test evaluation of code blocks under COMMENT headings."
  434. (let ((org-export-use-babel t)
  435. (org-babel-inline-result-wrap "=%s="))
  436. ;; Do not eval block in a commented headline.
  437. (should
  438. (string-match
  439. ": 2"
  440. (org-test-with-temp-text "* Headline
  441. #+BEGIN_SRC emacs-lisp :exports results
  442. \(+ 1 1)
  443. #+END_SRC"
  444. (org-babel-exp-process-buffer)
  445. (buffer-string))))
  446. (should-not
  447. (string-match
  448. ": 2"
  449. (org-test-with-temp-text "* COMMENT Headline
  450. #+BEGIN_SRC emacs-lisp :exports results
  451. \(+ 1 1)
  452. #+END_SRC"
  453. (org-babel-exp-process-buffer)
  454. (buffer-string))))
  455. ;; Do not eval inline blocks either.
  456. (should
  457. (string-match
  458. "=2="
  459. (org-test-with-temp-text "* Headline
  460. src_emacs-lisp{(+ 1 1)}"
  461. (org-babel-exp-process-buffer)
  462. (buffer-string))))
  463. (should-not
  464. (string-match
  465. "=2="
  466. (org-test-with-temp-text "* COMMENT Headline
  467. src_emacs-lisp{(+ 1 1)}"
  468. (org-babel-exp-process-buffer)
  469. (buffer-string))))
  470. ;; Also check parent headlines.
  471. (should-not
  472. (string-match
  473. ": 2"
  474. (org-test-with-temp-text "
  475. * COMMENT Headline
  476. ** Children
  477. #+BEGIN_SRC emacs-lisp :exports results
  478. \(+ 1 1)
  479. #+END_SRC"
  480. (org-babel-exp-process-buffer)
  481. (buffer-string))))))
  482. (ert-deftest ob-export/reference-in-post-header ()
  483. "Test references in :post header during export."
  484. (should
  485. (org-test-with-temp-text "
  486. #+NAME: foo
  487. #+BEGIN_SRC emacs-lisp :exports none :var bar=\"baz\"
  488. (concat \"bar\" bar)
  489. #+END_SRC
  490. #+NAME: nofun
  491. #+BEGIN_SRC emacs-lisp :exports results :post foo(\"nofun\")
  492. #+END_SRC"
  493. (org-babel-exp-process-buffer) t)))
  494. (ert-deftest ob-export/babel-evaluate ()
  495. "Test `org-export-use-babel' effect."
  496. ;; When nil, no Babel code is executed.
  497. (should-not
  498. (string-match-p
  499. "2"
  500. (org-test-with-temp-text
  501. "#+BEGIN_SRC emacs-lisp :exports results\n(+ 1 1)\n#+END_SRC"
  502. (let ((org-export-use-babel nil)) (org-babel-exp-process-buffer))
  503. (buffer-string))))
  504. (should-not
  505. (string-match-p
  506. "2"
  507. (org-test-with-temp-text
  508. "src_emacs-lisp{(+ 1 1)}"
  509. (let ((org-export-use-babel nil)) (org-babel-exp-process-buffer))
  510. (buffer-string))))
  511. ;; When non-nil, all Babel code types are executed.
  512. (should
  513. (string-match-p
  514. "2"
  515. (org-test-with-temp-text
  516. "#+BEGIN_SRC emacs-lisp :exports results\n(+ 1 1)\n#+END_SRC"
  517. (let ((org-export-use-babel t)) (org-babel-exp-process-buffer))
  518. (buffer-string))))
  519. (should
  520. (string-match-p
  521. "2"
  522. (org-test-with-temp-text
  523. "src_emacs-lisp{(+ 1 1)}"
  524. (let ((org-export-use-babel t)) (org-babel-exp-process-buffer))
  525. (buffer-string)))))
  526. (ert-deftest ob-export/body-with-coderef ()
  527. "Test exporting a code block with coderefs."
  528. (should
  529. (equal "#+begin_src emacs-lisp\n0 (ref:foo)\n#+end_src"
  530. (org-test-with-temp-text
  531. "#+BEGIN_SRC emacs-lisp :exports code\n0 (ref:foo)\n#+END_SRC"
  532. (let ((org-export-use-babel t)
  533. (org-coderef-label-format "(ref:foo)"))
  534. (org-babel-exp-process-buffer))
  535. (buffer-string))))
  536. (should
  537. (equal
  538. "#+begin_src emacs-lisp -l \"r:%s\"\n1 r:foo\n#+end_src"
  539. (org-test-with-temp-text
  540. "#+BEGIN_SRC emacs-lisp -l \"r:%s\" -lisp :exports code\n1 r:foo\n#+END_SRC"
  541. (let ((org-export-use-babel t))
  542. (org-babel-exp-process-buffer))
  543. (buffer-string)))))
  544. (ert-deftest ob-exp/src-block-with-affiliated-keyword ()
  545. "Test exporting a code block with affiliated keywords."
  546. ;; Pathological case: affiliated keyword matches inline source block
  547. ;; syntax.
  548. (should
  549. (equal "#+name: call_foo\n#+begin_src emacs-lisp\n42\n#+end_src"
  550. (org-test-with-temp-text
  551. "#+name: call_foo\n#+BEGIN_SRC emacs-lisp\n42\n#+END_SRC"
  552. (let ((org-export-use-babel t))
  553. (org-babel-exp-process-buffer))
  554. (buffer-string)))))
  555. (ert-deftest ob-exp/unknown-call-reference ()
  556. "Test exporting with a call that references an unknown name."
  557. (should-error
  558. (org-test-with-temp-text
  559. "call_foo()"
  560. (let ((org-export-use-babel t))
  561. (org-babel-exp-process-buffer)))
  562. :type 'user-error))
  563. (provide 'test-ob-exp)
  564. ;;; test-ob-exp.el ends here