test-ob-exp.el 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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/export-call-line-information ()
  179. (org-test-at-id "bec63a04-491e-4caa-97f5-108f3020365c"
  180. (org-narrow-to-subtree)
  181. (let ((org-babel-exp-call-line-template "\n: call: %line special-token"))
  182. (org-test-with-expanded-babel-code
  183. (should (string-match "double" (buffer-string)))
  184. (should (string-match "16" (buffer-string)))
  185. (should (string-match "special-token" (buffer-string)))))))
  186. (ert-deftest ob-exp/noweb-strip-export-ensure-strips ()
  187. (org-test-at-id "8e7bd234-99b2-4b14-8cd6-53945e409775"
  188. (org-narrow-to-subtree)
  189. (org-babel-next-src-block 2)
  190. (should (= 110 (org-babel-execute-src-block)))
  191. (let ((result (org-test-with-expanded-babel-code (buffer-string))))
  192. (should-not (string-match (regexp-quote "<<strip-export-1>>") result))
  193. (should-not (string-match (regexp-quote "i=\"10\"") result)))))
  194. (ert-deftest ob-exp/use-case-of-reading-entry-properties ()
  195. (org-test-at-id "cc5fbc20-bca5-437a-a7b8-2b4d7a03f820"
  196. (org-narrow-to-subtree)
  197. (let* ((case-fold-search nil)
  198. (result (org-test-with-expanded-babel-code (buffer-string)))
  199. (sect "a:1, b:0, c:3, d:0, e:0")
  200. (sub0 "a:1, b:2, c:4, d:0, e:0")
  201. (sub1 "a:1, b:2, c:5, d:0, e:6")
  202. (func sub0))
  203. ;; entry "section"
  204. (should (string-match (concat "_shell sect call\n: shell " sect "\n")
  205. result))
  206. (should (string-match (concat "_elisp sect call\n: elisp " sect "\n")
  207. result))
  208. (should (string-match (concat "\n- sect inline =shell " sect "=\n")
  209. result))
  210. (should (string-match (concat "\n- sect inline =elisp " sect "=\n")
  211. result))
  212. ;; entry "subsection", call without arguments
  213. (should (string-match (concat "_shell sub0 call\n: shell " sub0 "\n")
  214. result))
  215. (should (string-match (concat "_elisp sub0 call\n: elisp " sub0 "\n")
  216. result))
  217. (should (string-match (concat "\n- sub0 inline =shell " sub0 "=\n")
  218. result))
  219. (should (string-match (concat "\n- sub0 inline =elisp " sub0 "=\n")
  220. result))
  221. ;; entry "subsection", call with arguments
  222. (should (string-match (concat "_shell sub1 call\n: shell " sub1 "\n")
  223. result))
  224. (should (string-match (concat "_elisp sub1 call\n: elisp " sub1 "\n")
  225. result))
  226. (should (string-match (concat "\n- sub1 inline =shell " sub1 "=\n")
  227. result))
  228. (should (string-match (concat "\n- sub1 inline =elisp " sub1 "=\n")
  229. result))
  230. ;; entry "function definition"
  231. (should (string-match (concat "_location_shell\n: shell " func "\n")
  232. result))
  233. (should (string-match (concat "_location_elisp\n: elisp " func "\n")
  234. result)))))
  235. (ert-deftest ob-exp/export-from-a-temp-buffer ()
  236. :expected-result :failed
  237. (org-test-with-temp-text
  238. "
  239. #+Title: exporting from a temporary buffer
  240. #+name: foo
  241. #+BEGIN_SRC emacs-lisp
  242. :foo
  243. #+END_SRC
  244. #+name: bar
  245. #+BEGIN_SRC emacs-lisp
  246. :bar
  247. #+END_SRC
  248. #+BEGIN_SRC emacs-lisp :var foo=foo :noweb yes :exports results
  249. (list foo <<bar>>)
  250. #+END_SRC
  251. "
  252. (let* ((ascii (org-export-as 'ascii)))
  253. (should (string-match (regexp-quote (format nil "%S" '(:foo :bar)))
  254. ascii)))))
  255. (ert-deftest ob-export/export-with-results-before-block ()
  256. "Test export when results are inserted before source block."
  257. (should
  258. (equal
  259. "#+RESULTS: src1
  260. : 2
  261. #+NAME: src1
  262. #+BEGIN_SRC emacs-lisp
  263. \(+ 1 1)
  264. #+END_SRC"
  265. (org-test-with-temp-text
  266. "#+RESULTS: src1
  267. #+NAME: src1
  268. #+BEGIN_SRC emacs-lisp :exports both
  269. \(+ 1 1)
  270. #+END_SRC"
  271. (org-export-execute-babel-code)
  272. (buffer-string)))))
  273. (ert-deftest ob-export/export-src-block-with-switches ()
  274. "Test exporting a source block with switches."
  275. (should
  276. (string-match
  277. "\\`#\\+BEGIN_SRC emacs-lisp -n -r$"
  278. (org-test-with-temp-text
  279. "#+BEGIN_SRC emacs-lisp -n -r\n\(+ 1 1)\n#+END_SRC"
  280. (org-export-execute-babel-code)
  281. (buffer-string)))))
  282. (ert-deftest ob-export/export-src-block-with-flags ()
  283. "Test exporting a source block with a flag."
  284. (should
  285. (string-match
  286. "\\`#\\+BEGIN_SRC emacs-lisp -some-flag$"
  287. (org-test-with-temp-text
  288. "#+BEGIN_SRC emacs-lisp :flags -some-flag\n\(+ 1 1)\n#+END_SRC"
  289. (org-export-execute-babel-code)
  290. (buffer-string)))))
  291. (ert-deftest ob-export/export-and-indentation ()
  292. "Test indentation of evaluated source blocks during export."
  293. ;; No indentation.
  294. (should
  295. (string-match
  296. "^t"
  297. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n t\n#+END_SRC"
  298. (let ((indent-tabs-mode t)
  299. (tab-width 1)
  300. (org-src-preserve-indentation nil))
  301. (org-export-execute-babel-code)
  302. (buffer-string)))))
  303. ;; Preserve indentation with "-i" flag.
  304. (should
  305. (string-match
  306. "^ t"
  307. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp -i\n t\n#+END_SRC"
  308. (let ((indent-tabs-mode t)
  309. (tab-width 1))
  310. (org-export-execute-babel-code)
  311. (buffer-string)))))
  312. ;; Preserve indentation with a non-nil
  313. ;; `org-src-preserve-indentation'.
  314. (should
  315. (string-match
  316. "^ t"
  317. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n t\n#+END_SRC"
  318. (let ((indent-tabs-mode t)
  319. (tab-width 1)
  320. (org-src-preserve-indentation t))
  321. (org-export-execute-babel-code)
  322. (buffer-string))))))
  323. (ert-deftest ob-export/export-under-commented-headline ()
  324. "Test evaluation of code blocks under COMMENT headings."
  325. ;; Do not eval block in a commented headline.
  326. (should
  327. (string-match
  328. ": 2"
  329. (org-test-with-temp-text "* Headline
  330. #+BEGIN_SRC emacs-lisp :exports results
  331. \(+ 1 1)
  332. #+END_SRC"
  333. (org-export-execute-babel-code)
  334. (buffer-string))))
  335. (should-not
  336. (string-match
  337. ": 2"
  338. (org-test-with-temp-text "* COMMENT Headline
  339. #+BEGIN_SRC emacs-lisp :exports results
  340. \(+ 1 1)
  341. #+END_SRC"
  342. (org-export-execute-babel-code)
  343. (buffer-string))))
  344. ;; Do not eval inline blocks either.
  345. (should
  346. (string-match
  347. "=2="
  348. (org-test-with-temp-text "* Headline
  349. src_emacs-lisp{(+ 1 1)}"
  350. (org-export-execute-babel-code)
  351. (buffer-string))))
  352. (should-not
  353. (string-match
  354. "=2="
  355. (org-test-with-temp-text "* COMMENT Headline
  356. src_emacs-lisp{(+ 1 1)}"
  357. (org-export-execute-babel-code)
  358. (buffer-string))))
  359. ;; Also check parent headlines.
  360. (should-not
  361. (string-match
  362. ": 2"
  363. (org-test-with-temp-text "
  364. * COMMENT Headline
  365. ** Children
  366. #+BEGIN_SRC emacs-lisp :exports results
  367. \(+ 1 1)
  368. #+END_SRC"
  369. (org-export-execute-babel-code)
  370. (buffer-string)))))
  371. (ert-deftest ob-export/reference-in-post-header ()
  372. "Test references in :post header during export."
  373. (should
  374. (org-test-with-temp-text "
  375. #+NAME: foo
  376. #+BEGIN_SRC emacs-lisp :exports none :var bar=\"baz\"
  377. (concat \"bar\" bar)
  378. #+END_SRC
  379. #+NAME: nofun
  380. #+BEGIN_SRC emacs-lisp :exports results :post foo(\"nofun\")
  381. #+END_SRC"
  382. (org-export-execute-babel-code) t)))
  383. (provide 'test-ob-exp)
  384. ;;; test-ob-exp.el ends here