test-ob-exp.el 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. ;;; test-ob-exp.el
  2. ;; Copyright (c) 2010-2013 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. (let ((org-current-export-file buf))
  27. (org-babel-exp-process-buffer))
  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 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\")" ";; 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 (*evaluation-collector*)
  165. (org-test-at-id "96cc7073-97ec-4556-87cf-1f9bffafd317"
  166. (org-narrow-to-subtree)
  167. (buffer-string)
  168. (fboundp 'org-export-execute-babel-code)
  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 =1= of a line.
  174. Here is one at the end of a line. =2=
  175. =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. (org-test-with-expanded-babel-code (buffer-string))))))
  179. (ert-deftest ob-exp/export-call-line-information ()
  180. (org-test-at-id "bec63a04-491e-4caa-97f5-108f3020365c"
  181. (org-narrow-to-subtree)
  182. (let ((org-babel-exp-call-line-template "\n: call: %line special-token"))
  183. (org-test-with-expanded-babel-code
  184. (should (string-match "double" (buffer-string)))
  185. (should (string-match "16" (buffer-string)))
  186. (should (string-match "special-token" (buffer-string)))))))
  187. (ert-deftest ob-exp/noweb-strip-export-ensure-strips ()
  188. (org-test-at-id "8e7bd234-99b2-4b14-8cd6-53945e409775"
  189. (org-narrow-to-subtree)
  190. (org-babel-next-src-block 2)
  191. (should (= 110 (org-babel-execute-src-block)))
  192. (let ((result (org-test-with-expanded-babel-code (buffer-string))))
  193. (should-not (string-match (regexp-quote "<<strip-export-1>>") result))
  194. (should-not (string-match (regexp-quote "i=\"10\"") result)))))
  195. (ert-deftest ob-exp/use-case-of-reading-entry-properties ()
  196. (org-test-at-id "cc5fbc20-bca5-437a-a7b8-2b4d7a03f820"
  197. (org-narrow-to-subtree)
  198. (let* ((case-fold-search nil)
  199. (result (org-test-with-expanded-babel-code (buffer-string)))
  200. (sect "a:1, b:0, c:3, d:0, e:0")
  201. (sub0 "a:1, b:2, c:4, d:0, e:0")
  202. (sub1 "a:1, b:2, c:5, d:0, e:6")
  203. (func sub0))
  204. ;; entry "section"
  205. (should (string-match (concat "\"sect call\".*)\n: shell " sect "\n")
  206. result))
  207. (should (string-match (concat "sect call\\](.*)\n: elisp " sect "\n")
  208. result))
  209. (should (string-match (concat "\n- sect inline =shell " sect "=\n")
  210. result))
  211. (should (string-match (concat "\n- sect inline =elisp " sect "=\n")
  212. result))
  213. ;; entry "subsection", call without arguments
  214. (should (string-match (concat "\"sub0 call\".*)\n: shell " sub0 "\n")
  215. result))
  216. (should (string-match (concat "sub0 call\\](.*)\n: elisp " sub0 "\n")
  217. result))
  218. (should (string-match (concat "\n- sub0 inline =shell " sub0 "=\n")
  219. result))
  220. (should (string-match (concat "\n- sub0 inline =elisp " sub0 "=\n")
  221. result))
  222. ;; entry "subsection", call with arguments
  223. (should (string-match (concat "\"sub1 call\".*)\n: shell " sub1 "\n")
  224. result))
  225. (should (string-match (concat "sub1 call\\](.*)\n: elisp " sub1 "\n")
  226. result))
  227. (should (string-match (concat "\n- sub1 inline =shell " sub1 "=\n")
  228. result))
  229. (should (string-match (concat "\n- sub1 inline =elisp " sub1 "=\n")
  230. result))
  231. ;; entry "function definition"
  232. (should (string-match (concat "_location_shell\n: shell " func "\n")
  233. result))
  234. (should (string-match (concat "_location_elisp\n: elisp " func "\n")
  235. result)))))
  236. (ert-deftest ob-exp/export-from-a-temp-buffer ()
  237. :expected-result :failed
  238. (org-test-with-temp-text
  239. "
  240. #+Title: exporting from a temporary buffer
  241. #+name: foo
  242. #+BEGIN_SRC emacs-lisp
  243. :foo
  244. #+END_SRC
  245. #+name: bar
  246. #+BEGIN_SRC emacs-lisp
  247. :bar
  248. #+END_SRC
  249. #+BEGIN_SRC emacs-lisp :var foo=foo :noweb yes :exports results
  250. (list foo <<bar>>)
  251. #+END_SRC
  252. "
  253. (let* ((ascii (org-export-as 'ascii)))
  254. (should (string-match (regexp-quote (format nil "%S" '(:foo :bar)))
  255. ascii)))))
  256. (provide 'test-ob-exp)
  257. ;;; test-ob-exp.el ends here