test-org-export.el 7.3 KB

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