test-ob-exp.el 21 KB

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