test-ob-exp.el 18 KB

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