test-ob-exp.el 20 KB

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