test-ob-exp.el 17 KB

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