test-ob-exp.el 8.9 KB

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