test-ob-exp.el 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. ;;; test-ob-exp.el
  2. ;; Copyright (c) 2010-2012 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. (ert-deftest test-ob-exp/org-babel-exp-src-blocks/w-no-headers ()
  19. "Testing export without any headlines in the org-mode file."
  20. (let ((html-file (concat (file-name-sans-extension org-test-no-heading-file)
  21. ".html")))
  22. (when (file-exists-p html-file) (delete-file html-file))
  23. (org-test-in-example-file org-test-no-heading-file
  24. ;; export the file to html
  25. (org-export-as-html nil))
  26. ;; should create a .html file
  27. (should (file-exists-p html-file))
  28. ;; should not create a file with "::" appended to it's name
  29. (should-not (file-exists-p (concat org-test-no-heading-file "::")))
  30. (when (file-exists-p html-file) (delete-file html-file))))
  31. (ert-deftest test-ob-exp/org-babel-exp-src-blocks/w-no-file ()
  32. "Testing export from buffers which are not visiting any file."
  33. (when (get-buffer "*Org HTML Export*") (kill-buffer "*Org HTML Export*"))
  34. (should-not (get-buffer "*Org HTML Export*"))
  35. ;; export the file to HTML in a temporary buffer
  36. (org-test-in-example-file nil (org-export-as-html-to-buffer nil))
  37. ;; should create a .html buffer
  38. (should (buffer-live-p (get-buffer "*Org HTML Export*")))
  39. ;; should contain the content of the buffer
  40. (save-excursion
  41. (set-buffer (get-buffer "*Org HTML Export*"))
  42. (should (string-match (regexp-quote org-test-file-ob-anchor)
  43. (buffer-string))))
  44. (when (get-buffer "*Org HTML Export*") (kill-buffer "*Org HTML Export*")))
  45. (ert-deftest test-ob-exp/org-babel-exp-src-blocks/w-no-headers2 ()
  46. "Testing export without any headlines in the org-mode file."
  47. (let ((html-file (concat (file-name-sans-extension
  48. org-test-link-in-heading-file)
  49. ".html")))
  50. (when (file-exists-p html-file) (delete-file html-file))
  51. (org-test-in-example-file org-test-link-in-heading-file
  52. ;; export the file to html
  53. (org-export-as-html nil))
  54. ;; should create a .html file
  55. (should (file-exists-p html-file))
  56. ;; should not create a file with "::" appended to it's name
  57. (should-not (file-exists-p (concat org-test-link-in-heading-file "::")))
  58. (when (file-exists-p html-file) (delete-file html-file))))
  59. (ert-deftest ob-exp/noweb-on-export ()
  60. "Noweb header arguments export correctly.
  61. - yes expand on both export and tangle
  62. - no expand on neither export or tangle
  63. - tangle expand on only tangle not export"
  64. (org-test-at-id "eb1f6498-5bd9-45e0-9c56-50717053e7b7"
  65. (org-narrow-to-subtree)
  66. (let ((exported-html
  67. (org-export-as-html nil nil nil 'string 'body-only))
  68. (test-point 0))
  69. (org-test-with-temp-text-in-file
  70. exported-html
  71. ;; check following ouput exists and in order
  72. (mapcar (lambda (x)
  73. (should (< test-point
  74. (re-search-forward
  75. x
  76. nil t)))
  77. (setq test-point (point)))
  78. '("<code>:noweb</code> header argument expansion"
  79. "message" "expanded1"
  80. "message" "expanded2"
  81. "noweb-1-yes-start"
  82. "message" "expanded1"
  83. "noweb-no-start"
  84. "&lt;&lt;noweb-example1&gt;&gt;"
  85. "noweb-2-yes-start"
  86. "message" "expanded2"
  87. "noweb-tangle-start"
  88. "&lt;&lt;noweb-example1&gt;&gt;"
  89. "&lt;&lt;noweb-example2&gt;&gt;"))))))
  90. (ert-deftest ob-exp/noweb-on-export-with-exports-results ()
  91. "Noweb header arguments export correctly using :exports results.
  92. - yes expand on both export and tangle
  93. - no expand on neither export or tangle
  94. - tangle expand on only tangle not export"
  95. (org-test-at-id "8701beb4-13d9-468c-997a-8e63e8b66f8d"
  96. (org-narrow-to-subtree)
  97. (let ((exported-html
  98. (org-export-as-html nil nil nil 'string 'body-only))
  99. (test-point 0))
  100. (org-test-with-temp-text-in-file
  101. exported-html
  102. ;; check following ouput exists and in order
  103. (mapcar (lambda (x)
  104. (should (< test-point
  105. (re-search-forward
  106. x
  107. nil t)))
  108. (setq test-point (point)))
  109. '("<code>:noweb</code> header argument expansion using :exports results"
  110. "expanded1"
  111. "expanded2"
  112. "expanded1"
  113. "noweb-no-start"
  114. "&lt;&lt;noweb-example1&gt;&gt;"
  115. "expanded2"
  116. "&lt;&lt;noweb-example1&gt;&gt;"
  117. "&lt;&lt;noweb-example2&gt;&gt;"))))))
  118. (ert-deftest ob-exp/exports-both ()
  119. "Test the :exports both header argument.
  120. The code block should create both <pre></pre> and <table></table>
  121. elements in the final html."
  122. (org-test-at-id "92518f2a-a46a-4205-a3ab-bcce1008a4bb"
  123. (org-narrow-to-subtree)
  124. (let ((exported-html
  125. (org-export-as-html nil nil nil 'string 'body-only))
  126. (test-point 0))
  127. (org-test-with-temp-text-in-file
  128. exported-html
  129. ;; check following ouput exists and in order
  130. (mapcar (lambda (x)
  131. (should (< test-point
  132. (re-search-forward
  133. x
  134. nil t)))
  135. (setq test-point (point)))
  136. '( "Pascal's Triangle &ndash; exports both test"
  137. "<pre"
  138. "defun" "pascals-triangle"
  139. "if""list""list""let*""prev-triangle"
  140. "pascals-triangle""prev-row""car""reverse""prev-triangle"
  141. "append""prev-triangle""list""map""list"
  142. "append""prev-row""append""prev-row""pascals-triangle"
  143. "</pre>"
  144. "<table""<tbody>"
  145. "<tr>"">1<""</tr>"
  146. "<tr>"">1<"">1<""</tr>"
  147. "<tr>"">1<"">2<"">1<""</tr>"
  148. "<tr>"">1<"">3<"">3<"">1<""</tr>"
  149. "<tr>"">1<"">4<"">6<"">4<"">1<""</tr>"
  150. "<tr>"">1<"">5<"">10<"">10<"">5<"">1<""</tr>"
  151. "</tbody>""</table>"))))))
  152. (ert-deftest ob-exp/mixed-blocks-with-exports-both ()
  153. (org-test-at-id "5daa4d03-e3ea-46b7-b093-62c1b7632df3"
  154. (org-narrow-to-subtree)
  155. (let ((exported-html
  156. (org-export-as-html nil nil nil 'string 'body-only))
  157. (test-point 0))
  158. (org-test-with-temp-text-in-file
  159. exported-html
  160. ;; check following ouput exists and in order
  161. (mapcar (lambda (x)
  162. (should (< test-point
  163. (re-search-forward
  164. x
  165. nil t)))
  166. (setq test-point (point)))
  167. '("mixed blocks with exports both"
  168. "<ul>"
  169. "<li>""a""</li>"
  170. "<li>""b""</li>"
  171. "<li>""c""</li>"
  172. "</ul>"
  173. "<pre"
  174. "\"code block results\""
  175. "</pre>"
  176. "<pre class=\"example\">"
  177. "code block results"
  178. "</pre>"))))))
  179. (ert-deftest ob-exp/export-with-name ()
  180. (let ((org-babel-exp-code-template
  181. "=%name=\n#+BEGIN_SRC %lang%flags\nbody\n#+END_SRC"))
  182. (org-test-at-id "b02ddd8a-eeb8-42ab-8664-8a759e6f43d9"
  183. (org-narrow-to-subtree)
  184. (let ((ascii (org-export-as-ascii nil nil nil 'string 'body-only)))
  185. (should (string-match "qux" ascii))))))
  186. (ert-deftest ob-exp/export-with-header-argument ()
  187. (let ((org-babel-exp-code-template
  188. "
  189. | header | value |
  190. |---------+----------|
  191. | foo | %foo |
  192. | results | %results |
  193. #+BEGIN_SRC %lang%flags\nbody\n#+END_SRC"))
  194. (org-test-at-id "b02ddd8a-eeb8-42ab-8664-8a759e6f43d9"
  195. (org-narrow-to-subtree)
  196. (let ((ascii (org-export-as-ascii nil nil nil 'string 'body-only)))
  197. (should (string-match "baz" ascii))
  198. (should (string-match "replace" ascii))))))
  199. (ert-deftest ob-exp/noweb-no-export-and-exports-both ()
  200. (org-test-at-id "8a820f6c-7980-43db-8a24-0710d33729c9"
  201. (org-narrow-to-subtree)
  202. (let ((html (org-export-as-html nil nil nil 'string 'body-only)))
  203. (should (string-match (regexp-quote "noweb-no-export-and-exports-both-1")
  204. html)))))
  205. (ert-deftest ob-exp/evaluate-all-executables-in-order ()
  206. (org-test-at-id "96cc7073-97ec-4556-87cf-1f9bffafd317"
  207. (org-narrow-to-subtree)
  208. (let (*evaluation-collector*)
  209. (org-export-as-ascii nil nil nil 'string)
  210. (should (equal '(5 4 3 2 1) *evaluation-collector*)))))
  211. (ert-deftest ob-exp/export-call-line-information ()
  212. (org-test-at-id "bec63a04-491e-4caa-97f5-108f3020365c"
  213. (org-narrow-to-subtree)
  214. (let* ((org-babel-exp-call-line-template "\n: call: %line special-token")
  215. (html (org-export-as-html nil nil nil 'string t)))
  216. (should (string-match "double" html))
  217. (should (string-match "16" html))
  218. (should (string-match "special-token" html)))))
  219. (ert-deftest ob-exp/noweb-strip-export-ensure-strips ()
  220. (org-test-at-id "8e7bd234-99b2-4b14-8cd6-53945e409775"
  221. (org-narrow-to-subtree)
  222. (org-babel-next-src-block 2)
  223. (should (= 110 (org-babel-execute-src-block)))
  224. (let ((ascii (org-export-as-ascii nil nil nil 'string t)))
  225. (should-not (string-match (regexp-quote "<<strip-export-1>>") ascii))
  226. (should-not (string-match (regexp-quote "i=\"10\"") ascii)))))
  227. (ert-deftest ob-exp/export-from-a-temp-buffer ()
  228. :expected-result :failed
  229. (org-test-with-temp-text
  230. "
  231. #+Title: exporting from a temporary buffer
  232. #+name: foo
  233. #+BEGIN_SRC emacs-lisp
  234. :foo
  235. #+END_SRC
  236. #+name: bar
  237. #+BEGIN_SRC emacs-lisp
  238. :bar
  239. #+END_SRC
  240. #+BEGIN_SRC emacs-lisp :var foo=foo :noweb yes :exports results
  241. (list foo <<bar>>)
  242. #+END_SRC
  243. "
  244. (let* ((org-current-export-file (current-buffer))
  245. (ascii (org-export-as-ascii nil nil nil 'string)))
  246. (should (string-match (regexp-quote (format nil "%S" '(:foo :bar)))
  247. ascii)))))
  248. (provide 'test-ob-exp)
  249. ;;; test-ob-exp.el ends here