test-org-export.el 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. ;;; test-org-export.el --- Tests for org-export.el
  2. ;; Copyright (C) 2012 Nicolas Goaziou
  3. ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
  4. ;; Released under the GNU General Public License version 3
  5. ;; see: http://www.gnu.org/licenses/gpl-3.0.html
  6. ;;;; Comments
  7. ;;; Code:
  8. (let ((load-path (cons (expand-file-name
  9. ".." (file-name-directory
  10. (or load-file-name buffer-file-name)))
  11. load-path)))
  12. (require 'org-test)
  13. (require 'org-test-ob-consts)
  14. (require 'org-export))
  15. ;;; Tests
  16. (defmacro org-test-with-backend (backend &rest body)
  17. "Execute body with an export back-end defined.
  18. BACKEND is the name, as a string, of the back-end. BODY is the
  19. body to execute. The defined back-end simply returns parsed data
  20. as Org syntax."
  21. (declare (debug (form body)) (indent 1))
  22. `(flet ,(let (transcoders)
  23. (dolist (type (append org-element-all-elements
  24. org-element-all-objects)
  25. transcoders)
  26. (push `(,(intern (format "org-%s-%s" backend type))
  27. (obj contents info)
  28. (,(intern (format "org-element-%s-interpreter" type))
  29. obj contents))
  30. transcoders)))
  31. ,@body))
  32. (ert-deftest test-org-export/parse-option-keyword ()
  33. "Test reading all standard #+OPTIONS: items."
  34. (should
  35. (equal
  36. (org-export-parse-option-keyword
  37. "H:1 num:t \\n:t timestamp:t arch:t author:t creator:t d:t email:t
  38. *:t e:t ::t f:t pri:t -:t ^:t toc:t |:t tags:t tasks:t <:t todo:t")
  39. '(:headline-levels
  40. 1 :preserve-breaks t :section-numbers t :time-stamp-file t
  41. :with-archived-trees t :with-author t :with-creator t :with-drawers t
  42. :with-email t :with-emphasize t :with-entities t :with-fixed-width t
  43. :with-footnotes t :with-priority t :with-special-strings t
  44. :with-sub-superscript t :with-toc t :with-tables t :with-tags t
  45. :with-tasks t :with-timestamps t :with-todo-keywords t)))
  46. ;; Test some special values.
  47. (should
  48. (equal
  49. (org-export-parse-option-keyword
  50. "arch:headline creator:comment d:(\"TEST\")
  51. ^:{} toc:1 tags:not-in-toc tasks:todo")
  52. '(:with-archived-trees
  53. headline :with-creator comment :with-drawers ("TEST")
  54. :with-sub-superscript {} :with-toc 1 :with-tags not-in-toc
  55. :with-tasks todo))))
  56. (ert-deftest test-org-export/get-inbuffer-options ()
  57. "Test reading all standard export keywords."
  58. (should
  59. (equal
  60. (org-test-with-temp-text "#+AUTHOR: Me, Myself and I
  61. #+CREATOR: Idem
  62. #+DATE: Today
  63. #+DESCRIPTION: Testing
  64. #+DESCRIPTION: with two lines
  65. #+EMAIL: some@email.org
  66. #+EXPORT_EXCLUDE_TAGS: noexport invisible
  67. #+KEYWORDS: test
  68. #+LANGUAGE: en
  69. #+EXPORT_SELECT_TAGS: export
  70. #+TITLE: Some title
  71. #+TITLE: with spaces"
  72. (org-export-get-inbuffer-options))
  73. '(:author
  74. "Me, Myself and I" :creator "Idem" :date "Today"
  75. :description "Testing\nwith two lines" :email "some@email.org"
  76. :exclude-tags ("noexport" "invisible") :keywords "test" :language "en"
  77. :select-tags ("export") :title "Some title with spaces"))))
  78. (ert-deftest test-org-export/define-macro ()
  79. "Try defining various Org macro using in-buffer #+MACRO: keyword."
  80. ;; Parsed macro.
  81. (should (equal (org-test-with-temp-text "#+MACRO: one 1"
  82. (org-export-get-inbuffer-options))
  83. '(:macro-one ("1"))))
  84. ;; Evaled macro.
  85. (should (equal (org-test-with-temp-text "#+MACRO: two (eval (+ 1 1))"
  86. (org-export-get-inbuffer-options))
  87. '(:macro-two "(eval (+ 1 1))")))
  88. ;; Incomplete macro.
  89. (should-not (org-test-with-temp-text "#+MACRO: three"
  90. (org-export-get-inbuffer-options)))
  91. ;; Macro with newline character.
  92. (should (equal (org-test-with-temp-text "#+MACRO: four a\\nb"
  93. (org-export-get-inbuffer-options))
  94. '(:macro-four ("a\nb"))))
  95. ;; Macro with protected newline character.
  96. (should (equal (org-test-with-temp-text "#+MACRO: five a\\\\nb"
  97. (org-export-get-inbuffer-options))
  98. '(:macro-five ("a\\nb"))))
  99. ;; Recursive macro.
  100. (org-test-with-temp-text "#+MACRO: six 6\n#+MACRO: seven 1 + {{{six}}}"
  101. (should
  102. (equal
  103. (org-export-get-inbuffer-options)
  104. '(:macro-six
  105. ("6")
  106. :macro-seven
  107. ("1 + " (macro (:key "six" :value "{{{six}}}" :args nil :begin 5 :end 14
  108. :post-blank 0))))))))
  109. (ert-deftest test-org-export/handle-options ()
  110. "Test if export options have an impact on output."
  111. ;; Test exclude tags.
  112. (org-test-with-temp-text "* Head1 :noexport:"
  113. (org-test-with-backend "test"
  114. (should
  115. (equal (org-export-as 'test nil nil nil '(:exclude-tags ("noexport")))
  116. ""))))
  117. ;; Test include tags.
  118. (org-test-with-temp-text "
  119. * Head1
  120. ** Sub-Head1.1 :export:
  121. *** Sub-Head1.1.1
  122. * Head2"
  123. (org-test-with-backend "test"
  124. (should
  125. (string-match
  126. "\\* Head1\n\\*\\* Sub-Head1.1[ \t]+:export:\n\\*\\*\\* Sub-Head1.1.1\n"
  127. (org-export-as 'test nil nil nil '(:select-tags ("export")))))))
  128. ;; Test mixing include tags and exclude tags.
  129. (org-test-with-temp-text "
  130. * Head1 :export:
  131. ** Sub-Head1 :noexport:
  132. ** Sub-Head2
  133. * Head2 :noexport:
  134. ** Sub-Head1 :export:"
  135. (org-test-with-backend "test"
  136. (should
  137. (string-match
  138. "\\* Head1[ \t]+:export:\n\\*\\* Sub-Head2\n"
  139. (org-export-as
  140. 'test nil nil nil
  141. '(:select-tags ("export") :exclude-tags ("noexport")))))))
  142. ;; Ignore tasks.
  143. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  144. (org-test-with-temp-text "* TODO Head1"
  145. (org-test-with-backend "test"
  146. (should (equal (org-export-as 'test nil nil nil '(:with-tasks nil))
  147. "")))))
  148. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  149. (org-test-with-temp-text "* TODO Head1"
  150. (org-test-with-backend "test"
  151. (should (equal (org-export-as 'test nil nil nil '(:with-tasks t))
  152. "* TODO Head1\n")))))
  153. ;; Archived tree.
  154. (org-test-with-temp-text "* Head1 :archive:"
  155. (let ((org-archive-tag "archive"))
  156. (org-test-with-backend "test"
  157. (should
  158. (equal (org-export-as 'test nil nil nil '(:with-archived-trees nil))
  159. "")))))
  160. (org-test-with-temp-text "* Head1 :archive:\nbody\n** Sub-head 2"
  161. (let ((org-archive-tag "archive"))
  162. (org-test-with-backend "test"
  163. (should
  164. (string-match
  165. "\\* Head1[ \t]+:archive:"
  166. (org-export-as 'test nil nil nil
  167. '(:with-archived-trees headline)))))))
  168. (org-test-with-temp-text "* Head1 :archive:"
  169. (let ((org-archive-tag "archive"))
  170. (org-test-with-backend "test"
  171. (should
  172. (string-match
  173. "\\`\\* Head1[ \t]+:archive:\n\\'"
  174. (org-export-as 'test nil nil nil '(:with-archived-trees t)))))))
  175. ;; Drawers.
  176. (let ((org-drawers '("TEST")))
  177. (org-test-with-temp-text ":TEST:\ncontents\n:END:"
  178. (org-test-with-backend "test"
  179. (should (equal (org-export-as 'test nil nil nil '(:with-drawers nil))
  180. "")))))
  181. (let ((org-drawers '("TEST")))
  182. (org-test-with-temp-text ":TEST:\ncontents\n:END:"
  183. (org-test-with-backend "test"
  184. (should (equal (org-export-as 'test nil nil nil '(:with-drawers t))
  185. ":TEST:\ncontents\n:END:\n"))))))
  186. (ert-deftest test-org-export/comment-tree ()
  187. "Test if export process ignores commented trees."
  188. (let ((org-comment-string "COMMENT"))
  189. (org-test-with-temp-text "* COMMENT Head1"
  190. (org-test-with-backend "test"
  191. (should (equal (org-export-as 'test) ""))))))
  192. (ert-deftest test-org-export/export-scope ()
  193. "Test all export scopes."
  194. (org-test-with-temp-text "
  195. * Head1
  196. ** Head2
  197. text
  198. *** Head3"
  199. (org-test-with-backend "test"
  200. ;; Subtree.
  201. (forward-line 3)
  202. (should (equal (org-export-as 'test 'subtree) "text\n*** Head3\n"))
  203. ;; Visible.
  204. (goto-char (point-min))
  205. (forward-line)
  206. (org-cycle)
  207. (should (equal (org-export-as 'test nil 'visible) "* Head1\n"))
  208. ;; Body only.
  209. (flet ((org-test-template (body info) (format "BEGIN\n%sEND" body)))
  210. (should (equal (org-export-as 'test nil nil 'body-only)
  211. "* Head1\n** Head2\ntext\n*** Head3\n"))
  212. (should (equal (org-export-as 'test)
  213. "BEGIN\n* Head1\n** Head2\ntext\n*** Head3\nEND")))
  214. ;; Region.
  215. (goto-char (point-min))
  216. (forward-line 3)
  217. (mark-paragraph)
  218. (should (equal (org-export-as 'test) "text\n")))))
  219. (ert-deftest test-org-export/export-snippet ()
  220. "Test export snippets transcoding."
  221. (org-test-with-temp-text "@test{A}@t{B}"
  222. (org-test-with-backend "test"
  223. (flet ((org-test-export-snippet
  224. (snippet contents info)
  225. (when (eq (org-export-snippet-backend snippet) 'test)
  226. (org-element-property :value snippet))))
  227. (let ((org-export-snippet-translation-alist nil))
  228. (should (equal (org-export-as 'test) "A\n")))
  229. (let ((org-export-snippet-translation-alist '(("t" . "test"))))
  230. (should (equal (org-export-as 'test) "AB\n")))))))