test-ob-exp.el 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  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 <http://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. (should
  195. (equal "src_emacs-lisp[]{(+ 1 1)} {{{results(=2=)}}}"
  196. (org-test-with-temp-text "src_emacs-lisp[:exports both]{(+ 1 1)}"
  197. (let ((org-babel-inline-result-wrap "=%s=")
  198. (org-export-use-babel t))
  199. (org-babel-exp-process-buffer))
  200. (buffer-string))))
  201. (should
  202. (equal "{{{results(=2=)}}}"
  203. (org-test-with-temp-text
  204. "src_emacs-lisp[:exports results :results scalar]{(+ 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 "foosrc_emacs-lisp[:exports code]{(+ 1 1)}"
  211. (org-test-with-temp-text
  212. "foosrc_emacs-lisp[:exports code]{(+ 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. (let ((text "src_emacs lisp{(+ 1 1)}"))
  219. (string-match (regexp-quote text)
  220. (org-test-with-temp-text
  221. text
  222. (let ((org-babel-inline-result-wrap "=%s=")
  223. (org-export-use-babel t))
  224. (org-babel-exp-process-buffer))
  225. (buffer-string)))))
  226. (should
  227. (string-match
  228. (replace-regexp-in-string
  229. "\\\\\\[]{" "\\(?:\\[]\\)?{" ;accept both src_sh[]{...} or src_sh{...}
  230. (regexp-quote "Here is one in the middle src_sh[]{echo 1} of a line.
  231. Here is one at the end of a line. src_sh[]{echo 2}
  232. src_sh[]{echo 3} Here is one at the beginning of a line.
  233. Here is one that is also evaluated: src_sh[]{echo 4} {{{results(=4=)}}}")
  234. nil t)
  235. (org-test-at-id "cd54fc88-1b6b-45b6-8511-4d8fa7fc8076"
  236. (org-narrow-to-subtree)
  237. (let ((org-babel-inline-result-wrap "=%s=")
  238. (org-export-use-babel t))
  239. (org-test-with-expanded-babel-code (buffer-string)))))))
  240. (ert-deftest ob-exp/exports-inline-code-double-eval ()
  241. "Based on default header arguments for inline code blocks (:exports
  242. results), the resulting code block `src_emacs-lisp{2}' should also be
  243. evaluated."
  244. (let ((org-babel-inline-result-wrap "=%s=")
  245. (org-export-use-babel t))
  246. (should
  247. (string-match "\\`{{{results(src_emacs-lisp\\[\\]{2})}}}$"
  248. (org-test-with-temp-text
  249. "src_emacs-lisp[:exports results :results code]{(+ 1 1)}"
  250. (org-babel-exp-process-buffer)
  251. (buffer-string))))))
  252. (ert-deftest ob-exp/exports-inline-code-eval-code-once ()
  253. "Ibid above, except that the resulting inline code block should not
  254. be evaluated."
  255. (let ((org-export-use-babel t))
  256. (should
  257. (string-match "{{{results(src_emacs-lisp\\(?:\\[[: a-zA-Z]+]\\)?{2})}}}$"
  258. (org-test-with-temp-text
  259. (concat "src_emacs-lisp[:exports results :results code "
  260. ":results_switches \":exports code\"]{(+ 1 1)}")
  261. (org-babel-exp-process-buffer)
  262. (buffer-string))))))
  263. (ert-deftest ob-exp/exports-inline-code-double-eval-exports-both ()
  264. (let ((org-export-use-babel t))
  265. (should
  266. (string-match (concat "\\`src_emacs-lisp\\(?:\\[]\\)?{(\\+ 1 1)} "
  267. "{{{results(src_emacs-lisp\\[ :exports code\\]{2})}}}$")
  268. (org-test-with-temp-text
  269. (concat "src_emacs-lisp[:exports both :results code "
  270. ":results_switches \":exports code\"]{(+ 1 1)}")
  271. (org-babel-exp-process-buffer)
  272. (buffer-string))))))
  273. (ert-deftest ob-exp/export-call-line-information ()
  274. (org-test-at-id "bec63a04-491e-4caa-97f5-108f3020365c"
  275. (org-narrow-to-subtree)
  276. (let ((org-babel-exp-call-line-template "\n: call: %line special-token"))
  277. (org-test-with-expanded-babel-code
  278. (should (string-match "double" (buffer-string)))
  279. (should (string-match "16" (buffer-string)))
  280. (should (string-match "special-token" (buffer-string)))))))
  281. (ert-deftest ob-exp/noweb-strip-export-ensure-strips ()
  282. (org-test-at-id "8e7bd234-99b2-4b14-8cd6-53945e409775"
  283. (org-narrow-to-subtree)
  284. (org-babel-next-src-block 2)
  285. (should (= 110 (org-babel-execute-src-block)))
  286. (let ((result (org-test-with-expanded-babel-code (buffer-string))))
  287. (should-not (string-match (regexp-quote "<<strip-export-1>>") result))
  288. (should-not (string-match (regexp-quote "i=\"10\"") result)))))
  289. (ert-deftest ob-exp/use-case-of-reading-entry-properties ()
  290. (org-test-at-id "cc5fbc20-bca5-437a-a7b8-2b4d7a03f820"
  291. (org-narrow-to-subtree)
  292. (let* ((case-fold-search nil)
  293. (result (org-test-with-expanded-babel-code (buffer-string)))
  294. (sect "a:1, b:0, c:3, d:0, e:0")
  295. (sub0 "a:1, b:2, c:4, d:0, e:0")
  296. (sub1 "a:1, b:2, c:5, d:0, e:6")
  297. (func sub0))
  298. ;; entry "section"
  299. (should (string-match (concat "_shell-sect-call\n: shell " sect "\n")
  300. result))
  301. (should (string-match (concat "_elisp-sect-call\n: elisp " sect "\n")
  302. result))
  303. (should (string-match (concat "\n- sect inline shell " sect "\n")
  304. result))
  305. (should (string-match (concat "\n- sect inline elisp " sect "\n")
  306. result))
  307. ;; entry "subsection", call without arguments
  308. (should (string-match (concat "_shell-sub0-call\n: shell " sub0 "\n")
  309. result))
  310. (should (string-match (concat "_elisp-sub0-call\n: elisp " sub0 "\n")
  311. result))
  312. (should (string-match (concat "\n- sub0 inline shell " sub0 "\n")
  313. result))
  314. (should (string-match (concat "\n- sub0 inline elisp " sub0 "\n")
  315. result))
  316. ;; entry "subsection", call with arguments
  317. (should (string-match (concat "_shell-sub1-call\n: shell " sub1 "\n")
  318. result))
  319. (should (string-match (concat "_elisp-sub1-call\n: elisp " sub1 "\n")
  320. result))
  321. (should (string-match (concat "\n- sub1 inline shell " sub1 "\n")
  322. result))
  323. (should (string-match (concat "\n- sub1 inline elisp " sub1 "\n")
  324. result))
  325. ;; entry "function definition"
  326. (should (string-match (concat "_location_shell\n: shell " func "\n")
  327. result))
  328. (should (string-match (concat "_location_elisp\n: elisp " func "\n")
  329. result)))))
  330. (ert-deftest ob-exp/export-from-a-temp-buffer ()
  331. (let ((org-export-use-babel t))
  332. (org-test-with-temp-text
  333. "
  334. #+Title: exporting from a temporary buffer
  335. #+name: foo
  336. #+BEGIN_SRC emacs-lisp
  337. :foo
  338. #+END_SRC
  339. #+name: bar
  340. #+BEGIN_SRC emacs-lisp
  341. :bar
  342. #+END_SRC
  343. #+BEGIN_SRC emacs-lisp :var foo=foo :noweb yes :exports results
  344. (list foo <<bar>>)
  345. #+END_SRC
  346. "
  347. (let* ((ascii (org-export-as 'ascii)))
  348. (should (string-match
  349. (regexp-quote " :foo :bar \n")
  350. ascii))))))
  351. (ert-deftest ob-export/export-with-results-before-block ()
  352. "Test export when results are inserted before source block."
  353. (let ((org-export-use-babel t))
  354. (should
  355. (equal
  356. "#+RESULTS: src1
  357. : 2
  358. #+NAME: src1
  359. #+BEGIN_SRC emacs-lisp
  360. \(+ 1 1)
  361. #+END_SRC"
  362. (org-test-with-temp-text
  363. "#+RESULTS: src1
  364. #+NAME: src1
  365. #+BEGIN_SRC emacs-lisp :exports both
  366. \(+ 1 1)
  367. #+END_SRC"
  368. (org-babel-exp-process-buffer)
  369. (org-trim (org-no-properties (buffer-string))))))))
  370. (ert-deftest ob-export/export-src-block-with-switches ()
  371. "Test exporting a source block with switches."
  372. (should
  373. (string-match
  374. "\\`#\\+BEGIN_SRC emacs-lisp -n -r$"
  375. (org-test-with-temp-text
  376. "#+BEGIN_SRC emacs-lisp -n -r\n\(+ 1 1)\n#+END_SRC"
  377. (org-babel-exp-process-buffer)
  378. (buffer-string)))))
  379. (ert-deftest ob-export/export-src-block-with-flags ()
  380. "Test exporting a source block with a flag."
  381. (should
  382. (string-match
  383. "\\`#\\+BEGIN_SRC emacs-lisp -some-flag$"
  384. (org-test-with-temp-text
  385. "#+BEGIN_SRC emacs-lisp :flags -some-flag\n\(+ 1 1)\n#+END_SRC"
  386. (org-babel-exp-process-buffer)
  387. (buffer-string)))))
  388. (ert-deftest ob-export/export-and-indentation ()
  389. "Test indentation of evaluated source blocks during export."
  390. ;; No indentation.
  391. (should
  392. (string-match
  393. "^t"
  394. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n t\n#+END_SRC"
  395. (let ((indent-tabs-mode t)
  396. (tab-width 1)
  397. (org-src-preserve-indentation nil))
  398. (org-babel-exp-process-buffer)
  399. (buffer-string)))))
  400. ;; Preserve indentation with "-i" flag.
  401. (should
  402. (string-match
  403. "^ t"
  404. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp -i\n t\n#+END_SRC"
  405. (let ((indent-tabs-mode t)
  406. (tab-width 1))
  407. (org-babel-exp-process-buffer)
  408. (buffer-string)))))
  409. ;; Preserve indentation with a non-nil
  410. ;; `org-src-preserve-indentation'.
  411. (should
  412. (string-match
  413. "^ t"
  414. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n t\n#+END_SRC"
  415. (let ((indent-tabs-mode t)
  416. (tab-width 1)
  417. (org-src-preserve-indentation t))
  418. (org-babel-exp-process-buffer)
  419. (buffer-string))))))
  420. (ert-deftest ob-export/export-under-commented-headline ()
  421. "Test evaluation of code blocks under COMMENT headings."
  422. (let ((org-export-use-babel t)
  423. (org-babel-inline-result-wrap "=%s="))
  424. ;; Do not eval block in a commented headline.
  425. (should
  426. (string-match
  427. ": 2"
  428. (org-test-with-temp-text "* Headline
  429. #+BEGIN_SRC emacs-lisp :exports results
  430. \(+ 1 1)
  431. #+END_SRC"
  432. (org-babel-exp-process-buffer)
  433. (buffer-string))))
  434. (should-not
  435. (string-match
  436. ": 2"
  437. (org-test-with-temp-text "* COMMENT Headline
  438. #+BEGIN_SRC emacs-lisp :exports results
  439. \(+ 1 1)
  440. #+END_SRC"
  441. (org-babel-exp-process-buffer)
  442. (buffer-string))))
  443. ;; Do not eval inline blocks either.
  444. (should
  445. (string-match
  446. "=2="
  447. (org-test-with-temp-text "* Headline
  448. src_emacs-lisp{(+ 1 1)}"
  449. (org-babel-exp-process-buffer)
  450. (buffer-string))))
  451. (should-not
  452. (string-match
  453. "=2="
  454. (org-test-with-temp-text "* COMMENT Headline
  455. src_emacs-lisp{(+ 1 1)}"
  456. (org-babel-exp-process-buffer)
  457. (buffer-string))))
  458. ;; Also check parent headlines.
  459. (should-not
  460. (string-match
  461. ": 2"
  462. (org-test-with-temp-text "
  463. * COMMENT Headline
  464. ** Children
  465. #+BEGIN_SRC emacs-lisp :exports results
  466. \(+ 1 1)
  467. #+END_SRC"
  468. (org-babel-exp-process-buffer)
  469. (buffer-string))))))
  470. (ert-deftest ob-export/reference-in-post-header ()
  471. "Test references in :post header during export."
  472. (should
  473. (org-test-with-temp-text "
  474. #+NAME: foo
  475. #+BEGIN_SRC emacs-lisp :exports none :var bar=\"baz\"
  476. (concat \"bar\" bar)
  477. #+END_SRC
  478. #+NAME: nofun
  479. #+BEGIN_SRC emacs-lisp :exports results :post foo(\"nofun\")
  480. #+END_SRC"
  481. (org-babel-exp-process-buffer) t)))
  482. (ert-deftest ob-export/babel-evaluate ()
  483. "Test `org-export-use-babel' effect."
  484. ;; When nil, no Babel code is executed.
  485. (should-not
  486. (string-match-p
  487. "2"
  488. (org-test-with-temp-text
  489. "#+BEGIN_SRC emacs-lisp :exports results\n(+ 1 1)\n#+END_SRC"
  490. (let ((org-export-use-babel nil)) (org-babel-exp-process-buffer))
  491. (buffer-string))))
  492. (should-not
  493. (string-match-p
  494. "2"
  495. (org-test-with-temp-text
  496. "src_emacs-lisp{(+ 1 1)}"
  497. (let ((org-export-use-babel nil)) (org-babel-exp-process-buffer))
  498. (buffer-string))))
  499. ;; When non-nil, all Babel code types are executed.
  500. (should
  501. (string-match-p
  502. "2"
  503. (org-test-with-temp-text
  504. "#+BEGIN_SRC emacs-lisp :exports results\n(+ 1 1)\n#+END_SRC"
  505. (let ((org-export-use-babel t)) (org-babel-exp-process-buffer))
  506. (buffer-string))))
  507. (should
  508. (string-match-p
  509. "2"
  510. (org-test-with-temp-text
  511. "src_emacs-lisp{(+ 1 1)}"
  512. (let ((org-export-use-babel t)) (org-babel-exp-process-buffer))
  513. (buffer-string)))))
  514. (ert-deftest ob-export/body-with-coderef ()
  515. "Test exporting a code block with coderefs."
  516. (should
  517. (equal "#+BEGIN_SRC emacs-lisp\n0 (ref:foo)\n#+END_SRC"
  518. (org-test-with-temp-text
  519. "#+BEGIN_SRC emacs-lisp :exports code\n0 (ref:foo)\n#+END_SRC"
  520. (let ((org-export-use-babel t)
  521. (org-coderef-label-format "(ref:foo)"))
  522. (org-babel-exp-process-buffer))
  523. (buffer-string))))
  524. (should
  525. (equal
  526. "#+BEGIN_SRC emacs-lisp -l \"r:%s\"\n1 r:foo\n#+END_SRC"
  527. (org-test-with-temp-text
  528. "#+BEGIN_SRC emacs-lisp -l \"r:%s\" -lisp :exports code\n1 r:foo\n#+END_SRC"
  529. (let ((org-export-use-babel t))
  530. (org-babel-exp-process-buffer))
  531. (buffer-string)))))
  532. (ert-deftest ob-exp/src-block-with-affiliated-keyword ()
  533. "Test exporting a code block with affiliated keywords."
  534. ;; Pathological case: affiliated keyword matches inline source block
  535. ;; syntax.
  536. (should
  537. (equal "#+name: call_foo\n#+BEGIN_SRC emacs-lisp\n42\n#+END_SRC"
  538. (org-test-with-temp-text
  539. "#+name: call_foo\n#+BEGIN_SRC emacs-lisp\n42\n#+END_SRC"
  540. (let ((org-export-use-babel t))
  541. (org-babel-exp-process-buffer))
  542. (buffer-string)))))
  543. (ert-deftest ob-exp/unknown-call-reference ()
  544. "Test exporting with a call that references an unknown name."
  545. (should-error
  546. (org-test-with-temp-text
  547. "call_foo()"
  548. (let ((org-export-use-babel t))
  549. (org-babel-exp-process-buffer)))
  550. :type 'user-error))
  551. (provide 'test-ob-exp)
  552. ;;; test-ob-exp.el ends here