test-org-export.el 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578
  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. (unless (featurep 'org-export)
  9. (signal 'missing-test-dependency "org-export"))
  10. (defmacro org-test-with-backend (backend &rest body)
  11. "Execute body with an export back-end defined.
  12. BACKEND is the name of the back-end. BODY is the body to
  13. execute. The defined back-end simply returns parsed data as Org
  14. syntax."
  15. (declare (debug (form body)) (indent 1))
  16. `(let ((,(intern (format "org-%s-translate-alist" backend))
  17. ',(let (transcode-table)
  18. (dolist (type (append org-element-all-elements
  19. org-element-all-objects)
  20. transcode-table)
  21. (push (cons type (intern (format "org-%s-%s" backend type)))
  22. transcode-table)))))
  23. (flet ,(let (transcoders)
  24. (dolist (type (append org-element-all-elements
  25. org-element-all-objects)
  26. transcoders)
  27. (push `(,(intern (format "org-%s-%s" backend type))
  28. (obj contents info)
  29. (,(intern (format "org-element-%s-interpreter" type))
  30. obj contents))
  31. transcoders)))
  32. ,@body)))
  33. (defmacro org-test-with-parsed-data (data &rest body)
  34. "Execute body with parsed data available.
  35. DATA is a string containing the data to be parsed. BODY is the
  36. body to execute. Parse tree is available under the `tree'
  37. variable, and communication channel under `info'.
  38. This function calls `org-export-collect-tree-properties'. As
  39. such, `:ignore-list' (for `org-element-map') and
  40. `:parse-tree' (for `org-export-get-genealogy') properties are
  41. already filled in `info'."
  42. (declare (debug (form body)) (indent 1))
  43. `(org-test-with-temp-text ,data
  44. (let* ((tree (org-element-parse-buffer))
  45. (info (org-export-collect-tree-properties
  46. tree (org-export-get-environment))))
  47. ,@body)))
  48. ;;; Tests
  49. (ert-deftest test-org-export/parse-option-keyword ()
  50. "Test reading all standard #+OPTIONS: items."
  51. (should
  52. (equal
  53. (org-export-parse-option-keyword
  54. "H:1 num:t \\n:t timestamp:t arch:t author:t creator:t d:t email:t
  55. *:t e:t ::t f:t pri:t -:t ^:t toc:t |:t tags:t tasks:t <:t todo:t")
  56. '(:headline-levels
  57. 1 :preserve-breaks t :section-numbers t :time-stamp-file t
  58. :with-archived-trees t :with-author t :with-creator t :with-drawers t
  59. :with-email t :with-emphasize t :with-entities t :with-fixed-width t
  60. :with-footnotes t :with-priority t :with-special-strings t
  61. :with-sub-superscript t :with-toc t :with-tables t :with-tags t
  62. :with-tasks t :with-timestamps t :with-todo-keywords t)))
  63. ;; Test some special values.
  64. (should
  65. (equal
  66. (org-export-parse-option-keyword
  67. "arch:headline creator:comment d:(\"TEST\")
  68. ^:{} toc:1 tags:not-in-toc tasks:todo num:2 <:active")
  69. '( :section-numbers
  70. 2
  71. :with-archived-trees headline :with-creator comment
  72. :with-drawers ("TEST") :with-sub-superscript {} :with-toc 1
  73. :with-tags not-in-toc :with-tasks todo :with-timestamps active))))
  74. (ert-deftest test-org-export/get-inbuffer-options ()
  75. "Test reading all standard export keywords."
  76. (should
  77. (equal
  78. (org-test-with-temp-text "#+AUTHOR: Me, Myself and I
  79. #+CREATOR: Idem
  80. #+DATE: Today
  81. #+DESCRIPTION: Testing
  82. #+DESCRIPTION: with two lines
  83. #+EMAIL: some@email.org
  84. #+EXPORT_EXCLUDE_TAGS: noexport invisible
  85. #+KEYWORDS: test
  86. #+LANGUAGE: en
  87. #+EXPORT_SELECT_TAGS: export
  88. #+TITLE: Some title
  89. #+TITLE: with spaces"
  90. (org-export-get-inbuffer-options))
  91. '(:author
  92. ("Me, Myself and I") :creator "Idem" :date ("Today")
  93. :description "Testing\nwith two lines" :email "some@email.org"
  94. :exclude-tags ("noexport" "invisible") :keywords "test" :language "en"
  95. :select-tags ("export") :title ("Some title with spaces")))))
  96. (ert-deftest test-org-export/handle-options ()
  97. "Test if export options have an impact on output."
  98. ;; Test exclude tags.
  99. (org-test-with-temp-text "* Head1 :noexport:"
  100. (org-test-with-backend test
  101. (should
  102. (equal (org-export-as 'test nil nil nil '(:exclude-tags ("noexport")))
  103. ""))))
  104. ;; Test include tags.
  105. (org-test-with-temp-text "
  106. * Head1
  107. * Head2
  108. ** Sub-Head2.1 :export:
  109. *** Sub-Head2.1.1
  110. * Head2"
  111. (org-test-with-backend test
  112. (should
  113. (equal
  114. "* Head2\n** Sub-Head2.1 :export:\n*** Sub-Head2.1.1\n"
  115. (let ((org-tags-column 0))
  116. (org-export-as 'test nil nil nil '(:select-tags ("export"))))))))
  117. ;; Test mixing include tags and exclude tags.
  118. (org-test-with-temp-text "
  119. * Head1 :export:
  120. ** Sub-Head1 :noexport:
  121. ** Sub-Head2
  122. * Head2 :noexport:
  123. ** Sub-Head1 :export:"
  124. (org-test-with-backend test
  125. (should
  126. (string-match
  127. "\\* Head1[ \t]+:export:\n\\*\\* Sub-Head2\n"
  128. (org-export-as
  129. 'test nil nil nil
  130. '(:select-tags ("export") :exclude-tags ("noexport")))))))
  131. ;; Ignore tasks.
  132. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  133. (org-test-with-temp-text "* TODO Head1"
  134. (org-test-with-backend test
  135. (should (equal (org-export-as 'test nil nil nil '(:with-tasks nil))
  136. "")))))
  137. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  138. (org-test-with-temp-text "* TODO Head1"
  139. (org-test-with-backend test
  140. (should (equal (org-export-as 'test nil nil nil '(:with-tasks t))
  141. "* TODO Head1\n")))))
  142. ;; Archived tree.
  143. (org-test-with-temp-text "* Head1 :archive:"
  144. (let ((org-archive-tag "archive"))
  145. (org-test-with-backend test
  146. (should
  147. (equal (org-export-as 'test nil nil nil '(:with-archived-trees nil))
  148. "")))))
  149. (org-test-with-temp-text "* Head1 :archive:\nbody\n** Sub-head 2"
  150. (let ((org-archive-tag "archive"))
  151. (org-test-with-backend test
  152. (should
  153. (string-match
  154. "\\* Head1[ \t]+:archive:"
  155. (org-export-as 'test nil nil nil
  156. '(:with-archived-trees headline)))))))
  157. (org-test-with-temp-text "* Head1 :archive:"
  158. (let ((org-archive-tag "archive"))
  159. (org-test-with-backend test
  160. (should
  161. (string-match
  162. "\\`\\* Head1[ \t]+:archive:\n\\'"
  163. (org-export-as 'test nil nil nil '(:with-archived-trees t)))))))
  164. ;; Drawers.
  165. (let ((org-drawers '("TEST")))
  166. (org-test-with-temp-text ":TEST:\ncontents\n:END:"
  167. (org-test-with-backend test
  168. (should (equal (org-export-as 'test nil nil nil '(:with-drawers nil))
  169. ""))
  170. (should (equal (org-export-as 'test nil nil nil '(:with-drawers t))
  171. ":TEST:\ncontents\n:END:\n")))))
  172. (let ((org-drawers '("FOO" "BAR")))
  173. (org-test-with-temp-text ":FOO:\nkeep\n:END:\n:BAR:\nremove\n:END:"
  174. (org-test-with-backend test
  175. (should
  176. (equal (org-export-as 'test nil nil nil '(:with-drawers ("FOO")))
  177. ":FOO:\nkeep\n:END:\n")))))
  178. ;; Timestamps.
  179. (org-test-with-temp-text "[2012-04-29 sun. 10:45]<2012-04-29 sun. 10:45>"
  180. (org-test-with-backend test
  181. (should
  182. (equal (org-export-as 'test nil nil nil '(:with-timestamps t))
  183. "[2012-04-29 sun. 10:45]<2012-04-29 sun. 10:45>\n"))
  184. (should
  185. (equal (org-export-as 'test nil nil nil '(:with-timestamps nil)) ""))
  186. (should
  187. (equal (org-export-as 'test nil nil nil '(:with-timestamps active))
  188. "<2012-04-29 sun. 10:45>\n"))
  189. (should
  190. (equal (org-export-as 'test nil nil nil '(:with-timestamps inactive))
  191. "[2012-04-29 sun. 10:45]\n"))))
  192. ;; Clocks.
  193. (let ((org-clock-string "CLOCK:"))
  194. (org-test-with-temp-text "CLOCK: [2012-04-29 sun. 10:45]"
  195. (org-test-with-backend test
  196. (should
  197. (equal (org-export-as 'test nil nil nil '(:with-clocks t))
  198. "CLOCK: [2012-04-29 sun. 10:45]\n"))
  199. (should
  200. (equal (org-export-as 'test nil nil nil '(:with-clocks nil)) "")))))
  201. ;; Plannings.
  202. (let ((org-closed-string "CLOSED:"))
  203. (org-test-with-temp-text "CLOSED: [2012-04-29 sun. 10:45]"
  204. (org-test-with-backend test
  205. (should
  206. (equal (org-export-as 'test nil nil nil '(:with-plannings t))
  207. "CLOSED: [2012-04-29 sun. 10:45]\n"))
  208. (should
  209. (equal (org-export-as 'test nil nil nil '(:with-plannings nil))
  210. ""))))))
  211. (ert-deftest test-org-export/comment-tree ()
  212. "Test if export process ignores commented trees."
  213. (let ((org-comment-string "COMMENT"))
  214. (org-test-with-temp-text "* COMMENT Head1"
  215. (org-test-with-backend test
  216. (should (equal (org-export-as 'test) ""))))))
  217. (ert-deftest test-org-export/export-scope ()
  218. "Test all export scopes."
  219. (org-test-with-temp-text "
  220. * Head1
  221. ** Head2
  222. text
  223. *** Head3"
  224. (org-test-with-backend test
  225. ;; Subtree.
  226. (forward-line 3)
  227. (should (equal (org-export-as 'test 'subtree) "text\n*** Head3\n"))
  228. ;; Visible.
  229. (goto-char (point-min))
  230. (forward-line)
  231. (org-cycle)
  232. (should (equal (org-export-as 'test nil 'visible) "* Head1\n"))
  233. ;; Body only.
  234. (flet ((org-test-template (body info) (format "BEGIN\n%sEND" body)))
  235. (push '(template . org-test-template) org-test-translate-alist)
  236. (should (equal (org-export-as 'test nil nil 'body-only)
  237. "* Head1\n** Head2\ntext\n*** Head3\n"))
  238. (should (equal (org-export-as 'test)
  239. "BEGIN\n* Head1\n** Head2\ntext\n*** Head3\nEND")))
  240. ;; Region.
  241. (goto-char (point-min))
  242. (forward-line 3)
  243. (transient-mark-mode 1)
  244. (push-mark (point) t t)
  245. (goto-char (point-at-eol))
  246. (should (equal (org-export-as 'test) "text\n"))))
  247. ;; Subtree with a code block calling another block outside.
  248. (org-test-with-temp-text "
  249. * Head1
  250. #+BEGIN_SRC emacs-lisp :noweb yes :exports results
  251. <<test>>
  252. #+END_SRC
  253. * Head2
  254. #+NAME: test
  255. #+BEGIN_SRC emacs-lisp
  256. \(+ 1 2)
  257. #+END_SRC"
  258. (org-test-with-backend test
  259. (forward-line 1)
  260. (should (equal (org-export-as 'test 'subtree) ": 3\n"))))
  261. ;; Subtree's EXPORT_TITLE property.
  262. (should
  263. (equal
  264. (plist-get (org-test-with-temp-text "* Headline
  265. :PROPERTIES:
  266. :EXPORT_TITLE: subtree-title
  267. :END:
  268. Paragraph"
  269. (org-export-get-environment nil t))
  270. :title)
  271. '("subtree-title")))
  272. ;; Subtree's EXPORT_TITLE property.
  273. (org-test-with-temp-text "#+OPTIONS: H:1
  274. * Headline
  275. :PROPERTIES:
  276. :EXPORT_OPTIONS: H:2
  277. :END:
  278. Paragraph"
  279. (forward-line)
  280. (should
  281. (= 2 (plist-get (org-export-get-environment nil t) :headline-levels)))))
  282. (ert-deftest test-org-export/export-snippet ()
  283. "Test export snippets transcoding."
  284. (org-test-with-temp-text "@@test:A@@@@t:B@@"
  285. (org-test-with-backend test
  286. (flet ((org-test-export-snippet
  287. (snippet contents info)
  288. (when (eq (org-export-snippet-backend snippet) 'test)
  289. (org-element-property :value snippet))))
  290. (let ((org-export-snippet-translation-alist nil))
  291. (should (equal (org-export-as 'test) "A\n")))
  292. (let ((org-export-snippet-translation-alist '(("t" . "test"))))
  293. (should (equal (org-export-as 'test) "AB\n")))))))
  294. (ert-deftest test-org-export/expand-include ()
  295. "Test file inclusion in an Org buffer."
  296. ;; Full insertion with recursive inclusion.
  297. (org-test-with-temp-text
  298. (format "#+INCLUDE: \"%s/examples/include.org\"" org-test-dir)
  299. (org-export-expand-include-keyword)
  300. (should (equal (buffer-string)
  301. "Small Org file with an include keyword.
  302. #+BEGIN_SRC emacs-lisp :exports results\n(+ 2 1)\n#+END_SRC
  303. Success!
  304. * Heading
  305. body\n")))
  306. ;; Localized insertion.
  307. (org-test-with-temp-text
  308. (format "#+INCLUDE: \"%s/examples/include.org\" :lines \"1-2\""
  309. org-test-dir)
  310. (org-export-expand-include-keyword)
  311. (should (equal (buffer-string)
  312. "Small Org file with an include keyword.\n")))
  313. ;; Insertion with constraints on headlines level.
  314. (org-test-with-temp-text
  315. (format
  316. "* Top heading\n#+INCLUDE: \"%s/examples/include.org\" :lines \"9-\""
  317. org-test-dir)
  318. (org-export-expand-include-keyword)
  319. (should (equal (buffer-string) "* Top heading\n** Heading\nbody\n")))
  320. ;; Inclusion within an example block.
  321. (org-test-with-temp-text
  322. (format "#+INCLUDE: \"%s/examples/include.org\" :lines \"1-2\" example"
  323. org-test-dir)
  324. (org-export-expand-include-keyword)
  325. (should
  326. (equal
  327. (buffer-string)
  328. "#+BEGIN_EXAMPLE\nSmall Org file with an include keyword.\n#+END_EXAMPLE\n")))
  329. ;; Inclusion within a src-block.
  330. (org-test-with-temp-text
  331. (format
  332. "#+INCLUDE: \"%s/examples/include.org\" :lines \"4-5\" src emacs-lisp"
  333. org-test-dir)
  334. (org-export-expand-include-keyword)
  335. (should (equal (buffer-string)
  336. "#+BEGIN_SRC emacs-lisp\n(+ 2 1)\n#+END_SRC\n"))))
  337. (ert-deftest test-org-export/user-ignore-list ()
  338. "Test if `:ignore-list' accepts user input."
  339. (org-test-with-backend test
  340. (flet ((skip-note-head
  341. (data backend info)
  342. ;; Ignore headlines with the word "note" in their title.
  343. (org-element-map
  344. data 'headline
  345. (lambda (headline)
  346. (when (string-match "\\<note\\>"
  347. (org-element-property :raw-value headline))
  348. (org-export-ignore-element headline info)))
  349. info)
  350. data))
  351. ;; Install function in parse tree filters.
  352. (let ((org-export-filter-parse-tree-functions '(skip-note-head)))
  353. (org-test-with-temp-text "* Head1\n* Head2 (note)\n"
  354. (should (equal (org-export-as 'test) "* Head1\n")))))))
  355. (ert-deftest test-org-export/before-parsing-hook ()
  356. "Test `org-export-before-parsing-hook'."
  357. (org-test-with-backend test
  358. (org-test-with-temp-text "* Headline 1\nBody 1\n* Headline 2\nBody 2"
  359. (let ((org-export-before-parsing-hook
  360. '((lambda ()
  361. (org-map-entries
  362. (lambda ()
  363. (delete-region (point) (progn (forward-line) (point)))))))))
  364. (should (equal (org-export-as 'test) "Body 1\nBody 2\n"))))))
  365. (ert-deftest test-org-export/set-element ()
  366. "Test `org-export-set-element' specifications."
  367. (org-test-with-parsed-data "* Headline\n*a*"
  368. (org-export-set-element
  369. (org-element-map tree 'bold 'identity nil t)
  370. '(italic nil "b"))
  371. ;; Check if object is correctly replaced.
  372. (should (org-element-map tree 'italic 'identity))
  373. (should-not (org-element-map tree 'bold 'identity))
  374. ;; Check if new object's parent is correctly set.
  375. (should
  376. (equal
  377. (org-element-property :parent
  378. (org-element-map tree 'italic 'identity nil t))
  379. (org-element-map tree 'paragraph 'identity nil t)))))
  380. ;;; Affiliated Keywords
  381. (ert-deftest test-org-export/read-attribute ()
  382. "Test `org-export-read-attribute' specifications."
  383. ;; Standard test.
  384. (should
  385. (equal
  386. (org-export-read-attribute
  387. :attr_html
  388. (org-test-with-temp-text "#+ATTR_HTML: :a 1 :b 2\nParagraph"
  389. (org-element-current-element)))
  390. '(:a 1 :b 2)))
  391. ;; Return nil on empty attribute.
  392. (should-not
  393. (org-export-read-attribute
  394. :attr_html
  395. (org-test-with-temp-text "Paragraph" (org-element-current-element)))))
  396. ;;; Footnotes
  397. (ert-deftest test-org-export/footnotes ()
  398. "Test footnotes specifications."
  399. (let ((org-footnote-section nil)
  400. (org-export-with-footnotes t))
  401. ;; 1. Read every type of footnote.
  402. (org-test-with-parsed-data
  403. "Text[fn:1] [1] [fn:label:C] [fn::D]\n\n[fn:1] A\n\n[1] B"
  404. (should
  405. (equal
  406. '((1 . "A") (2 . "B") (3 . "C") (4 . "D"))
  407. (org-element-map
  408. tree 'footnote-reference
  409. (lambda (ref)
  410. (let ((def (org-export-get-footnote-definition ref info)))
  411. (cons (org-export-get-footnote-number ref info)
  412. (if (eq (org-element-property :type ref) 'inline) (car def)
  413. (car (org-element-contents
  414. (car (org-element-contents def))))))))
  415. info))))
  416. ;; 2. Test nested footnotes order.
  417. (org-test-with-parsed-data
  418. "Text[fn:1:A[fn:2]] [fn:3].\n\n[fn:2] B [fn:3] [fn::D].\n\n[fn:3] C."
  419. (should
  420. (equal
  421. '((1 . "fn:1") (2 . "fn:2") (3 . "fn:3") (4))
  422. (org-element-map
  423. tree 'footnote-reference
  424. (lambda (ref)
  425. (when (org-export-footnote-first-reference-p ref info)
  426. (cons (org-export-get-footnote-number ref info)
  427. (org-element-property :label ref))))
  428. info))))
  429. ;; 3. Test nested footnote in invisible definitions.
  430. (org-test-with-temp-text "Text[1]\n\n[1] B [2]\n\n[2] C."
  431. ;; Hide definitions.
  432. (narrow-to-region (point) (point-at-eol))
  433. (let* ((tree (org-element-parse-buffer))
  434. (info (org-combine-plists
  435. `(:parse-tree ,tree)
  436. (org-export-collect-tree-properties
  437. tree (org-export-get-environment)))))
  438. ;; Both footnotes should be seen.
  439. (should
  440. (= (length (org-export-collect-footnote-definitions tree info)) 2))))
  441. ;; 4. Test footnotes definitions collection.
  442. (org-test-with-parsed-data "Text[fn:1:A[fn:2]] [fn:3].
  443. \[fn:2] B [fn:3] [fn::D].
  444. \[fn:3] C."
  445. (should (= (length (org-export-collect-footnote-definitions tree info))
  446. 4)))
  447. ;; 5. Test export of footnotes defined outside parsing scope.
  448. (org-test-with-temp-text "[fn:1] Out of scope
  449. * Title
  450. Paragraph[fn:1]"
  451. (org-test-with-backend test
  452. (flet ((org-test-footnote-reference
  453. (fn-ref contents info)
  454. (org-element-interpret-data
  455. (org-export-get-footnote-definition fn-ref info))))
  456. (forward-line)
  457. (should (equal "ParagraphOut of scope\n"
  458. (org-export-as 'test 'subtree))))))))
  459. ;;; Headlines and Inlinetasks
  460. (ert-deftest test-org-export/get-relative-level ()
  461. "Test `org-export-get-relative-level' specifications."
  462. ;; Standard test.
  463. (should
  464. (equal '(1 2)
  465. (let ((org-odd-levels-only nil))
  466. (org-test-with-parsed-data "* Headline 1\n** Headline 2"
  467. (org-element-map
  468. tree 'headline
  469. (lambda (h) (org-export-get-relative-level h info))
  470. info)))))
  471. ;; Missing levels
  472. (should
  473. (equal '(1 3)
  474. (let ((org-odd-levels-only nil))
  475. (org-test-with-parsed-data "** Headline 1\n**** Headline 2"
  476. (org-element-map
  477. tree 'headline
  478. (lambda (h) (org-export-get-relative-level h info))
  479. info))))))
  480. (ert-deftest test-org-export/low-level-p ()
  481. "Test `org-export-low-level-p' specifications."
  482. (should
  483. (equal
  484. '(no yes)
  485. (let ((org-odd-levels-only nil))
  486. (org-test-with-parsed-data "* Headline 1\n** Headline 2"
  487. (org-element-map
  488. tree 'headline
  489. (lambda (h) (if (org-export-low-level-p h info) 'yes 'no))
  490. (plist-put info :headline-levels 1)))))))
  491. (ert-deftest test-org-export/get-headline-number ()
  492. "Test `org-export-get-headline-number' specifications."
  493. ;; Standard test.
  494. (should
  495. (equal
  496. '((1) (1 1))
  497. (let ((org-odd-levels-only nil))
  498. (org-test-with-parsed-data "* Headline 1\n** Headline 2"
  499. (org-element-map
  500. tree 'headline
  501. (lambda (h) (org-export-get-headline-number h info))
  502. info)))))
  503. ;; Missing levels are replaced with 0.
  504. (should
  505. (equal
  506. '((1) (1 0 1))
  507. (let ((org-odd-levels-only nil))
  508. (org-test-with-parsed-data "* Headline 1\n*** Headline 2"
  509. (org-element-map
  510. tree 'headline
  511. (lambda (h) (org-export-get-headline-number h info))
  512. info))))))
  513. (ert-deftest test-org-export/numbered-headline-p ()
  514. "Test `org-export-numbered-headline-p' specifications."
  515. ;; If `:section-numbers' is nil, never number headlines.
  516. (should-not
  517. (org-test-with-parsed-data "* Headline"
  518. (org-element-map
  519. tree 'headline
  520. (lambda (h) (org-export-numbered-headline-p h info))
  521. (plist-put info :section-numbers nil))))
  522. ;; If `:section-numbers' is a number, only number headlines with
  523. ;; a level greater that it.
  524. (should
  525. (equal
  526. '(yes no)
  527. (org-test-with-parsed-data "* Headline 1\n** Headline 2"
  528. (org-element-map
  529. tree 'headline
  530. (lambda (h) (if (org-export-numbered-headline-p h info) 'yes 'no))
  531. (plist-put info :section-numbers 1)))))
  532. ;; Otherwise, headlines are always numbered.
  533. (should
  534. (org-test-with-parsed-data "* Headline"
  535. (org-element-map
  536. tree 'headline
  537. (lambda (h) (org-export-numbered-headline-p h info))
  538. (plist-put info :section-numbers t)))))
  539. (ert-deftest test-org-export/number-to-roman ()
  540. "Test `org-export-number-to-roman' specifications."
  541. ;; If number is negative, return it as a string.
  542. (should (equal (org-export-number-to-roman -1) "-1"))
  543. ;; Otherwise, return it as a roman number.
  544. (should (equal (org-export-number-to-roman 1449) "MCDXLIX")))
  545. (ert-deftest test-org-export/get-tags ()
  546. "Test `org-export-get-tags' specifications."
  547. (let ((org-export-exclude-tags '("noexport"))
  548. (org-export-select-tags '("export")))
  549. ;; Standard test: tags which are not a select tag, an exclude tag,
  550. ;; or specified as optional argument shouldn't be ignored.
  551. (should
  552. (org-test-with-parsed-data "* Headline :tag:"
  553. (org-export-get-tags (org-element-map tree 'headline 'identity info t)
  554. info)))
  555. ;; Exclude tags are removed.
  556. (should-not
  557. (org-test-with-parsed-data "* Headline :noexport:"
  558. (org-export-get-tags (org-element-map tree 'headline 'identity info t)
  559. info)))
  560. ;; Select tags are removed.
  561. (should-not
  562. (org-test-with-parsed-data "* Headline :export:"
  563. (org-export-get-tags (org-element-map tree 'headline 'identity info t)
  564. info)))
  565. (should
  566. (equal
  567. '("tag")
  568. (org-test-with-parsed-data "* Headline :tag:export:"
  569. (org-export-get-tags (org-element-map tree 'headline 'identity info t)
  570. info))))
  571. ;; Tags provided in the optional argument are also ignored.
  572. (should-not
  573. (org-test-with-parsed-data "* Headline :ignore:"
  574. (org-export-get-tags (org-element-map tree 'headline 'identity info t)
  575. info '("ignore"))))))
  576. (ert-deftest test-org-export/first-sibling-p ()
  577. "Test `org-export-first-sibling-p' specifications."
  578. (should
  579. (equal
  580. '(yes yes no)
  581. (org-test-with-temp-text "* Headline\n** Headline 2\n** Headline 3"
  582. (org-element-map
  583. (org-element-parse-buffer) 'headline
  584. (lambda (h) (if (org-export-first-sibling-p h) 'yes 'no)))))))
  585. (ert-deftest test-org-export/last-sibling-p ()
  586. "Test `org-export-last-sibling-p' specifications."
  587. (should
  588. (equal
  589. '(yes no yes)
  590. (org-test-with-temp-text "* Headline\n** Headline 2\n** Headline 3"
  591. (org-element-map
  592. (org-element-parse-buffer) 'headline
  593. (lambda (h) (if (org-export-last-sibling-p h) 'yes 'no)))))))
  594. ;;; Links
  595. (ert-deftest test-org-export/get-coderef-format ()
  596. "Test `org-export-get-coderef-format' specifications."
  597. ;; A link without description returns "%s"
  598. (should (equal (org-export-get-coderef-format "(ref:line)" nil)
  599. "%s"))
  600. ;; Return "%s" when path is matched within description.
  601. (should (equal (org-export-get-coderef-format "path" "desc (path)")
  602. "desc %s"))
  603. ;; Otherwise return description.
  604. (should (equal (org-export-get-coderef-format "path" "desc")
  605. "desc")))
  606. (ert-deftest test-org-export/inline-image-p ()
  607. "Test `org-export-inline-image-p' specifications."
  608. (should
  609. (org-export-inline-image-p
  610. (org-test-with-temp-text "[[#id]]"
  611. (org-element-map
  612. (org-element-parse-buffer) 'link 'identity nil t))
  613. '(("custom-id" . "id")))))
  614. (ert-deftest test-org-export/fuzzy-link ()
  615. "Test fuzzy links specifications."
  616. ;; 1. Links to invisible (keyword) targets should be ignored.
  617. (org-test-with-parsed-data
  618. "Paragraph.\n#+TARGET: Test\n[[Test]]"
  619. (should-not
  620. (org-element-map
  621. tree 'link
  622. (lambda (link)
  623. (org-export-get-ordinal
  624. (org-export-resolve-fuzzy-link link info) info)) info)))
  625. ;; 2. Link to an headline should return headline's number.
  626. (org-test-with-parsed-data
  627. "Paragraph.\n* Head1\n* Head2\n* Head3\n[[Head2]]"
  628. (should
  629. ;; Note: Headline's number is in fact a list of numbers.
  630. (equal '(2)
  631. (org-element-map
  632. tree 'link
  633. (lambda (link)
  634. (org-export-get-ordinal
  635. (org-export-resolve-fuzzy-link link info) info)) info t))))
  636. ;; 3. Link to a target in an item should return item's number.
  637. (org-test-with-parsed-data
  638. "- Item1\n - Item11\n - <<test>>Item12\n- Item2\n\n\n[[test]]"
  639. (should
  640. ;; Note: Item's number is in fact a list of numbers.
  641. (equal '(1 2)
  642. (org-element-map
  643. tree 'link
  644. (lambda (link)
  645. (org-export-get-ordinal
  646. (org-export-resolve-fuzzy-link link info) info)) info t))))
  647. ;; 4. Link to a target in a footnote should return footnote's
  648. ;; number.
  649. (org-test-with-parsed-data "
  650. Paragraph[1][2][fn:lbl3:C<<target>>][[test]][[target]]\n[1] A\n\n[2] <<test>>B"
  651. (should
  652. (equal '(2 3)
  653. (org-element-map
  654. tree 'link
  655. (lambda (link)
  656. (org-export-get-ordinal
  657. (org-export-resolve-fuzzy-link link info) info)) info))))
  658. ;; 5. Link to a named element should return sequence number of that
  659. ;; element.
  660. (org-test-with-parsed-data
  661. "#+NAME: tbl1\n|1|2|\n#+NAME: tbl2\n|3|4|\n#+NAME: tbl3\n|5|6|\n[[tbl2]]"
  662. (should
  663. (= 2
  664. (org-element-map
  665. tree 'link
  666. (lambda (link)
  667. (org-export-get-ordinal
  668. (org-export-resolve-fuzzy-link link info) info)) info t))))
  669. ;; 6. Link to a target not within an item, a table, a footnote
  670. ;; reference or definition should return section number.
  671. (org-test-with-parsed-data
  672. "* Head1\n* Head2\nParagraph<<target>>\n* Head3\n[[target]]"
  673. (should
  674. (equal '(2)
  675. (org-element-map
  676. tree 'link
  677. (lambda (link)
  678. (org-export-get-ordinal
  679. (org-export-resolve-fuzzy-link link info) info)) info t)))))
  680. (ert-deftest test-org-export/resolve-coderef ()
  681. "Test `org-export-resolve-coderef' specifications."
  682. (let ((org-coderef-label-format "(ref:%s)"))
  683. ;; 1. A link to a "-n -k -r" block returns line number.
  684. (org-test-with-parsed-data
  685. "#+BEGIN_EXAMPLE -n -k -r\nText (ref:coderef)\n#+END_EXAMPLE"
  686. (should (= (org-export-resolve-coderef "coderef" info) 1)))
  687. (org-test-with-parsed-data
  688. "#+BEGIN_SRC emacs-lisp -n -k -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  689. (should (= (org-export-resolve-coderef "coderef" info) 1)))
  690. ;; 2. A link to a "-n -r" block returns line number.
  691. (org-test-with-parsed-data
  692. "#+BEGIN_EXAMPLE -n -r\nText (ref:coderef)\n#+END_EXAMPLE"
  693. (should (= (org-export-resolve-coderef "coderef" info) 1)))
  694. (org-test-with-parsed-data
  695. "#+BEGIN_SRC emacs-lisp -n -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  696. (should (= (org-export-resolve-coderef "coderef" info) 1)))
  697. ;; 3. A link to a "-n" block returns coderef.
  698. (org-test-with-parsed-data
  699. "#+BEGIN_SRC emacs-lisp -n\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  700. (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
  701. (org-test-with-parsed-data
  702. "#+BEGIN_EXAMPLE -n\nText (ref:coderef)\n#+END_EXAMPLE"
  703. (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
  704. ;; 4. A link to a "-r" block returns line number.
  705. (org-test-with-parsed-data
  706. "#+BEGIN_SRC emacs-lisp -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  707. (should (= (org-export-resolve-coderef "coderef" info) 1)))
  708. (org-test-with-parsed-data
  709. "#+BEGIN_EXAMPLE -r\nText (ref:coderef)\n#+END_EXAMPLE"
  710. (should (= (org-export-resolve-coderef "coderef" info) 1)))
  711. ;; 5. A link to a block without a switch returns coderef.
  712. (org-test-with-parsed-data
  713. "#+BEGIN_SRC emacs-lisp\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  714. (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
  715. (org-test-with-parsed-data
  716. "#+BEGIN_EXAMPLE\nText (ref:coderef)\n#+END_EXAMPLE"
  717. (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
  718. ;; 6. Correctly handle continued line numbers. A "+n" switch
  719. ;; should resume numbering from previous block with numbered
  720. ;; lines, ignoring blocks not numbering lines in the process.
  721. ;; A "-n" switch resets count.
  722. (org-test-with-parsed-data "
  723. #+BEGIN_EXAMPLE -n
  724. Text.
  725. #+END_EXAMPLE
  726. #+BEGIN_SRC emacs-lisp
  727. \(- 1 1)
  728. #+END_SRC
  729. #+BEGIN_SRC emacs-lisp +n -r
  730. \(+ 1 1) (ref:addition)
  731. #+END_SRC
  732. #+BEGIN_EXAMPLE -n -r
  733. Another text. (ref:text)
  734. #+END_EXAMPLE"
  735. (should (= (org-export-resolve-coderef "addition" info) 2))
  736. (should (= (org-export-resolve-coderef "text" info) 1)))
  737. ;; 7. Recognize coderef with user-specified syntax.
  738. (org-test-with-parsed-data
  739. "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\nText. [ref:text]\n#+END_EXAMPLE"
  740. (should (equal (org-export-resolve-coderef "text" info) "text")))))
  741. (ert-deftest test-org-export/resolve-fuzzy-link ()
  742. "Test `org-export-resolve-fuzzy-link' specifications."
  743. ;; 1. Match target objects.
  744. (org-test-with-parsed-data "<<target>> [[target]]"
  745. (should
  746. (org-export-resolve-fuzzy-link
  747. (org-element-map tree 'link 'identity info t) info)))
  748. ;; 2. Match target elements.
  749. (org-test-with-parsed-data "#+TARGET: target\n[[target]]"
  750. (should
  751. (org-export-resolve-fuzzy-link
  752. (org-element-map tree 'link 'identity info t) info)))
  753. ;; 3. Match named elements.
  754. (org-test-with-parsed-data "#+NAME: target\nParagraph\n\n[[target]]"
  755. (should
  756. (org-export-resolve-fuzzy-link
  757. (org-element-map tree 'link 'identity info t) info)))
  758. ;; 4. Match exact headline's name.
  759. (org-test-with-parsed-data "* My headline\n[[My headline]]"
  760. (should
  761. (org-export-resolve-fuzzy-link
  762. (org-element-map tree 'link 'identity info t) info)))
  763. ;; 5. Targets objects have priority over named elements and headline
  764. ;; titles.
  765. (org-test-with-parsed-data
  766. "* target\n#+NAME: target\n<<target>>\n\n[[target]]"
  767. (should
  768. (eq 'target
  769. (org-element-type
  770. (org-export-resolve-fuzzy-link
  771. (org-element-map tree 'link 'identity info t) info)))))
  772. ;; 6. Named elements have priority over headline titles.
  773. (org-test-with-parsed-data
  774. "* target\n#+NAME: target\nParagraph\n\n[[target]]"
  775. (should
  776. (eq 'paragraph
  777. (org-element-type
  778. (org-export-resolve-fuzzy-link
  779. (org-element-map tree 'link 'identity info t) info)))))
  780. ;; 7. If link's path starts with a "*", only match headline titles,
  781. ;; though.
  782. (org-test-with-parsed-data
  783. "* target\n#+NAME: target\n<<target>>\n\n[[*target]]"
  784. (should
  785. (eq 'headline
  786. (org-element-type
  787. (org-export-resolve-fuzzy-link
  788. (org-element-map tree 'link 'identity info t) info)))))
  789. ;; 8. Return nil if no match.
  790. (org-test-with-parsed-data "[[target]]"
  791. (should-not
  792. (org-export-resolve-fuzzy-link
  793. (org-element-map tree 'link 'identity info t) info))))
  794. (ert-deftest test-org-export/resolve-id-link ()
  795. "Test `org-export-resolve-id-link' specifications."
  796. ;; 1. Regular test for custom-id link.
  797. (org-test-with-parsed-data "* Headline1
  798. :PROPERTIES:
  799. :CUSTOM-ID: test
  800. :END:
  801. * Headline 2
  802. \[[#test]]"
  803. (should
  804. (org-export-resolve-id-link
  805. (org-element-map tree 'link 'identity info t) info)))
  806. ;; 2. Failing test for custom-id link.
  807. (org-test-with-parsed-data "* Headline1
  808. :PROPERTIES:
  809. :CUSTOM-ID: test
  810. :END:
  811. * Headline 2
  812. \[[#no-match]]"
  813. (should-not
  814. (org-export-resolve-id-link
  815. (org-element-map tree 'link 'identity info t) info)))
  816. ;; 3. Test for internal id target.
  817. (org-test-with-parsed-data "* Headline1
  818. :PROPERTIES:
  819. :ID: aaaa
  820. :END:
  821. * Headline 2
  822. \[[id:aaaa]]"
  823. (should
  824. (org-export-resolve-id-link
  825. (org-element-map tree 'link 'identity info t) info)))
  826. ;; 4. Test for external id target.
  827. (org-test-with-parsed-data "[[id:aaaa]]"
  828. (should
  829. (org-export-resolve-id-link
  830. (org-element-map tree 'link 'identity info t)
  831. (org-combine-plists info '(:id-alist (("aaaa" . "external-file"))))))))
  832. (ert-deftest test-org-export/resolve-radio-link ()
  833. "Test `org-export-resolve-radio-link' specifications."
  834. ;; Standard test.
  835. (org-test-with-temp-text "<<<radio>>> radio"
  836. (org-update-radio-target-regexp)
  837. (should
  838. (let* ((tree (org-element-parse-buffer))
  839. (info `(:parse-tree ,tree)))
  840. (org-export-resolve-radio-link
  841. (org-element-map tree 'link 'identity info t)
  842. info))))
  843. ;; Radio target with objects.
  844. (org-test-with-temp-text "<<<radio \\alpha>>> radio \\alpha"
  845. (org-update-radio-target-regexp)
  846. (should
  847. (let* ((tree (org-element-parse-buffer))
  848. (info `(:parse-tree ,tree)))
  849. (org-export-resolve-radio-link
  850. (org-element-map tree 'link 'identity info t)
  851. info)))))
  852. ;;; Macro
  853. (ert-deftest test-org-export/define-macro ()
  854. "Try defining various Org macro using in-buffer #+MACRO: keyword."
  855. ;; Parsed macro.
  856. (should (equal (org-test-with-temp-text "#+MACRO: one 1"
  857. (org-export-get-inbuffer-options))
  858. '(:macro-one ("1"))))
  859. ;; Evaled macro.
  860. (should (equal (org-test-with-temp-text "#+MACRO: two (eval (+ 1 1))"
  861. (org-export-get-inbuffer-options))
  862. '(:macro-two ("(eval (+ 1 1))"))))
  863. ;; Incomplete macro.
  864. (should-not (org-test-with-temp-text "#+MACRO: three"
  865. (org-export-get-inbuffer-options)))
  866. ;; Macro with newline character.
  867. (should (equal (org-test-with-temp-text "#+MACRO: four a\\nb"
  868. (org-export-get-inbuffer-options))
  869. '(:macro-four ("a\nb"))))
  870. ;; Macro with protected newline character.
  871. (should (equal (org-test-with-temp-text "#+MACRO: five a\\\\nb"
  872. (org-export-get-inbuffer-options))
  873. '(:macro-five ("a\\nb"))))
  874. ;; Recursive macro.
  875. (org-test-with-temp-text "#+MACRO: six 6\n#+MACRO: seven 1 + {{{six}}}"
  876. (should
  877. (equal
  878. (org-export-get-inbuffer-options)
  879. '(:macro-six
  880. ("6")
  881. :macro-seven
  882. ("1 + " (macro (:key "six" :value "{{{six}}}" :args nil :begin 5 :end 14
  883. :post-blank 0 :parent nil))))))))
  884. (ert-deftest test-org-export/expand-macro ()
  885. "Test `org-export-expand-macro' specifications."
  886. ;; Standard test.
  887. (should
  888. (equal
  889. "some text"
  890. (org-test-with-parsed-data "#+MACRO: macro some text\n{{{macro}}}"
  891. (org-export-expand-macro
  892. (org-element-map tree 'macro 'identity info t) info))))
  893. ;; Macro with arguments.
  894. (should
  895. (equal
  896. "some text"
  897. (org-test-with-parsed-data "#+MACRO: macro $1 $2\n{{{macro(some,text)}}}"
  898. (org-export-expand-macro
  899. (org-element-map tree 'macro 'identity info t) info))))
  900. ;; Macro with "eval"
  901. (should
  902. (equal
  903. "3"
  904. (org-test-with-parsed-data "#+MACRO: add (eval (+ $1 $2))\n{{{add(1,2)}}}"
  905. (org-export-expand-macro
  906. (org-element-map tree 'macro 'identity info t) info))))
  907. ;; Nested macros.
  908. (should
  909. (equal
  910. "inner outer"
  911. (org-test-with-parsed-data
  912. "#+MACRO: in inner\n#+MACRO: out {{{in}}} outer\n{{{out}}}"
  913. (flet ((translate-macro (macro contents info)
  914. (org-export-expand-macro macro info)))
  915. (org-export-expand-macro
  916. (org-element-map tree 'macro 'identity info t)
  917. (org-combine-plists
  918. info `(:translate-alist ((macro . translate-macro))))))))))
  919. ;;; Src-block and example-block
  920. (ert-deftest test-org-export/unravel-code ()
  921. "Test `org-export-unravel-code' function."
  922. (let ((org-coderef-label-format "(ref:%s)"))
  923. ;; 1. Code without reference.
  924. (org-test-with-temp-text "#+BEGIN_EXAMPLE\n(+ 1 1)\n#+END_EXAMPLE"
  925. (should (equal (org-export-unravel-code (org-element-current-element))
  926. '("(+ 1 1)\n"))))
  927. ;; 2. Code with reference.
  928. (org-test-with-temp-text
  929. "#+BEGIN_EXAMPLE\n(+ 1 1) (ref:test)\n#+END_EXAMPLE"
  930. (should (equal (org-export-unravel-code (org-element-current-element))
  931. '("(+ 1 1)\n" (1 . "test")))))
  932. ;; 3. Code with user-defined reference.
  933. (org-test-with-temp-text
  934. "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\n(+ 1 1) [ref:test]\n#+END_EXAMPLE"
  935. (should (equal (org-export-unravel-code (org-element-current-element))
  936. '("(+ 1 1)\n" (1 . "test")))))
  937. ;; 4. Code references keys are relative to the current block.
  938. (org-test-with-temp-text "
  939. #+BEGIN_EXAMPLE -n
  940. \(+ 1 1)
  941. #+END_EXAMPLE
  942. #+BEGIN_EXAMPLE +n
  943. \(+ 2 2)
  944. \(+ 3 3) (ref:one)
  945. #+END_EXAMPLE"
  946. (goto-line 5)
  947. (should (equal (org-export-unravel-code (org-element-current-element))
  948. '("(+ 2 2)\n(+ 3 3)\n" (2 . "one")))))
  949. ;; 5. Free up comma-protected lines.
  950. ;;
  951. ;; 5.1. In an Org source block, every line is protected.
  952. (org-test-with-temp-text
  953. "#+BEGIN_SRC org\n,* Test\n,# comment\n,Text\n#+END_SRC"
  954. (should (equal (org-export-unravel-code (org-element-current-element))
  955. '("* Test\n# comment\nText\n"))))
  956. ;; 5.2. In other blocks, only headlines, comments and keywords are
  957. ;; protected.
  958. (org-test-with-temp-text
  959. "#+BEGIN_EXAMPLE\n,* Headline\n, * Not headline\n,Keep\n#+END_EXAMPLE"
  960. (should (equal (org-export-unravel-code (org-element-current-element))
  961. '("* Headline\n, * Not headline\n,Keep\n"))))))
  962. ;;; Tables
  963. (ert-deftest test-org-export/special-column ()
  964. "Test if the table's special column is properly recognized."
  965. ;; 1. First column is special if it contains only a special marking
  966. ;; characters or empty cells.
  967. (org-test-with-temp-text "
  968. | ! | 1 |
  969. | | 2 |"
  970. (should
  971. (org-export-table-has-special-column-p
  972. (org-element-map
  973. (org-element-parse-buffer) 'table 'identity nil 'first-match))))
  974. ;; 2. If the column contains anything else, it isn't special.
  975. (org-test-with-temp-text "
  976. | ! | 1 |
  977. | b | 2 |"
  978. (should-not
  979. (org-export-table-has-special-column-p
  980. (org-element-map
  981. (org-element-parse-buffer) 'table 'identity nil 'first-match))))
  982. ;; 3. Special marking characters are "#", "^", "*", "_", "/", "$"
  983. ;; and "!".
  984. (org-test-with-temp-text "
  985. | # | 1 |
  986. | ^ | 2 |
  987. | * | 3 |
  988. | _ | 4 |
  989. | / | 5 |
  990. | $ | 6 |
  991. | ! | 7 |"
  992. (should
  993. (org-export-table-has-special-column-p
  994. (org-element-map
  995. (org-element-parse-buffer) 'table 'identity nil 'first-match))))
  996. ;; 4. A first column with only empty cells isn't considered as
  997. ;; special.
  998. (org-test-with-temp-text "
  999. | | 1 |
  1000. | | 2 |"
  1001. (should-not
  1002. (org-export-table-has-special-column-p
  1003. (org-element-map
  1004. (org-element-parse-buffer) 'table 'identity nil 'first-match)))))
  1005. (ert-deftest test-org-export/table-row-is-special-p ()
  1006. "Test `org-export-table-row-is-special-p' specifications."
  1007. ;; 1. A row is special if it has a special marking character in the
  1008. ;; special column.
  1009. (org-test-with-parsed-data "| ! | 1 |"
  1010. (should
  1011. (org-export-table-row-is-special-p
  1012. (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  1013. ;; 2. A row is special when its first field is "/"
  1014. (org-test-with-parsed-data "
  1015. | / | 1 |
  1016. | a | b |"
  1017. (should
  1018. (org-export-table-row-is-special-p
  1019. (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  1020. ;; 3. A row only containing alignment cookies is also considered as
  1021. ;; special.
  1022. (org-test-with-parsed-data "| <5> | | <l> | <l22> |"
  1023. (should
  1024. (org-export-table-row-is-special-p
  1025. (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  1026. ;; 4. Everything else isn't considered as special.
  1027. (org-test-with-parsed-data "| \alpha | | c |"
  1028. (should-not
  1029. (org-export-table-row-is-special-p
  1030. (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  1031. ;; 5. Table's rules are never considered as special rows.
  1032. (org-test-with-parsed-data "|---+---|"
  1033. (should-not
  1034. (org-export-table-row-is-special-p
  1035. (org-element-map tree 'table-row 'identity nil 'first-match) info))))
  1036. (ert-deftest test-org-export/has-header-p ()
  1037. "Test `org-export-table-has-header-p' specifications."
  1038. ;; 1. With an header.
  1039. (org-test-with-parsed-data "
  1040. | a | b |
  1041. |---+---|
  1042. | c | d |"
  1043. (should
  1044. (org-export-table-has-header-p
  1045. (org-element-map tree 'table 'identity info 'first-match)
  1046. info)))
  1047. ;; 2. Without an header.
  1048. (org-test-with-parsed-data "
  1049. | a | b |
  1050. | c | d |"
  1051. (should-not
  1052. (org-export-table-has-header-p
  1053. (org-element-map tree 'table 'identity info 'first-match)
  1054. info)))
  1055. ;; 3. Don't get fooled with starting and ending rules.
  1056. (org-test-with-parsed-data "
  1057. |---+---|
  1058. | a | b |
  1059. | c | d |
  1060. |---+---|"
  1061. (should-not
  1062. (org-export-table-has-header-p
  1063. (org-element-map tree 'table 'identity info 'first-match)
  1064. info))))
  1065. (ert-deftest test-org-export/table-row-group ()
  1066. "Test `org-export-table-row-group' specifications."
  1067. ;; 1. A rule creates a new group.
  1068. (org-test-with-parsed-data "
  1069. | a | b |
  1070. |---+---|
  1071. | 1 | 2 |"
  1072. (should
  1073. (equal
  1074. '(1 nil 2)
  1075. (mapcar (lambda (row) (org-export-table-row-group row info))
  1076. (org-element-map tree 'table-row 'identity)))))
  1077. ;; 2. Special rows are ignored in count.
  1078. (org-test-with-parsed-data "
  1079. | / | < | > |
  1080. |---|---+---|
  1081. | | 1 | 2 |"
  1082. (should
  1083. (equal
  1084. '(nil nil 1)
  1085. (mapcar (lambda (row) (org-export-table-row-group row info))
  1086. (org-element-map tree 'table-row 'identity)))))
  1087. ;; 3. Double rules also are ignored in count.
  1088. (org-test-with-parsed-data "
  1089. | a | b |
  1090. |---+---|
  1091. |---+---|
  1092. | 1 | 2 |"
  1093. (should
  1094. (equal
  1095. '(1 nil nil 2)
  1096. (mapcar (lambda (row) (org-export-table-row-group row info))
  1097. (org-element-map tree 'table-row 'identity))))))
  1098. (ert-deftest test-org-export/table-cell-width ()
  1099. "Test `org-export-table-cell-width' specifications."
  1100. ;; 1. Width is primarily determined by width cookies. If no cookie
  1101. ;; is found, cell's width is nil.
  1102. (org-test-with-parsed-data "
  1103. | / | <l> | <6> | <l7> |
  1104. | | a | b | c |"
  1105. (should
  1106. (equal
  1107. '(nil 6 7)
  1108. (mapcar (lambda (cell) (org-export-table-cell-width cell info))
  1109. (org-element-map tree 'table-cell 'identity info)))))
  1110. ;; 2. The last width cookie has precedence.
  1111. (org-test-with-parsed-data "
  1112. | <6> |
  1113. | <7> |
  1114. | a |"
  1115. (should
  1116. (equal
  1117. '(7)
  1118. (mapcar (lambda (cell) (org-export-table-cell-width cell info))
  1119. (org-element-map tree 'table-cell 'identity info)))))
  1120. ;; 3. Valid width cookies must have a specific row.
  1121. (org-test-with-parsed-data "| <6> | cell |"
  1122. (should
  1123. (equal
  1124. '(nil nil)
  1125. (mapcar (lambda (cell) (org-export-table-cell-width cell info))
  1126. (org-element-map tree 'table-cell 'identity))))))
  1127. (ert-deftest test-org-export/table-cell-alignment ()
  1128. "Test `org-export-table-cell-alignment' specifications."
  1129. (let ((org-table-number-fraction 0.5)
  1130. (org-table-number-regexp "^[0-9]+$"))
  1131. ;; 1. Alignment is primarily determined by alignment cookies.
  1132. (org-test-with-temp-text "| <l> | <c> | <r> |"
  1133. (let* ((tree (org-element-parse-buffer))
  1134. (info `(:parse-tree ,tree)))
  1135. (should
  1136. (equal
  1137. '(left center right)
  1138. (mapcar (lambda (cell) (org-export-table-cell-alignment cell info))
  1139. (org-element-map tree 'table-cell 'identity))))))
  1140. ;; 2. The last alignment cookie has precedence.
  1141. (org-test-with-parsed-data "
  1142. | <l8> |
  1143. | cell |
  1144. | <r9> |"
  1145. (should
  1146. (equal
  1147. '(right right right)
  1148. (mapcar (lambda (cell) (org-export-table-cell-alignment cell info))
  1149. (org-element-map tree 'table-cell 'identity)))))
  1150. ;; 3. If there's no cookie, cell's contents determine alignment.
  1151. ;; A column mostly made of cells containing numbers will align
  1152. ;; its cells to the right.
  1153. (org-test-with-parsed-data "
  1154. | 123 |
  1155. | some text |
  1156. | 12345 |"
  1157. (should
  1158. (equal
  1159. '(right right right)
  1160. (mapcar (lambda (cell)
  1161. (org-export-table-cell-alignment cell info))
  1162. (org-element-map tree 'table-cell 'identity)))))
  1163. ;; 4. Otherwise, they will be aligned to the left.
  1164. (org-test-with-parsed-data "
  1165. | text |
  1166. | some text |
  1167. | \alpha |"
  1168. (should
  1169. (equal
  1170. '(left left left)
  1171. (mapcar (lambda (cell)
  1172. (org-export-table-cell-alignment cell info))
  1173. (org-element-map tree 'table-cell 'identity)))))))
  1174. (ert-deftest test-org-export/table-cell-borders ()
  1175. "Test `org-export-table-cell-borders' specifications."
  1176. ;; 1. Recognize various column groups indicators.
  1177. (org-test-with-parsed-data "| / | < | > | <> |"
  1178. (should
  1179. (equal
  1180. '((right bottom top) (left bottom top) (right bottom top)
  1181. (right left bottom top))
  1182. (mapcar (lambda (cell)
  1183. (org-export-table-cell-borders cell info))
  1184. (org-element-map tree 'table-cell 'identity)))))
  1185. ;; 2. Accept shortcuts to define column groups.
  1186. (org-test-with-parsed-data "| / | < | < |"
  1187. (should
  1188. (equal
  1189. '((right bottom top) (right left bottom top) (left bottom top))
  1190. (mapcar (lambda (cell)
  1191. (org-export-table-cell-borders cell info))
  1192. (org-element-map tree 'table-cell 'identity)))))
  1193. ;; 3. A valid column groups row must start with a "/".
  1194. (org-test-with-parsed-data "
  1195. | | < |
  1196. | a | b |"
  1197. (should
  1198. (equal '((top) (top) (bottom) (bottom))
  1199. (mapcar (lambda (cell)
  1200. (org-export-table-cell-borders cell info))
  1201. (org-element-map tree 'table-cell 'identity)))))
  1202. ;; 4. Take table rules into consideration.
  1203. (org-test-with-parsed-data "
  1204. | 1 |
  1205. |---|
  1206. | 2 |"
  1207. (should
  1208. (equal '((below top) (bottom above))
  1209. (mapcar (lambda (cell)
  1210. (org-export-table-cell-borders cell info))
  1211. (org-element-map tree 'table-cell 'identity)))))
  1212. ;; 5. Top and (resp. bottom) rules induce both `top' and `above'
  1213. ;; (resp. `bottom' and `below') borders. Any special row is
  1214. ;; ignored.
  1215. (org-test-with-parsed-data "
  1216. |---+----|
  1217. | / | |
  1218. | | 1 |
  1219. |---+----|"
  1220. (should
  1221. (equal '((bottom below top above))
  1222. (last
  1223. (mapcar (lambda (cell)
  1224. (org-export-table-cell-borders cell info))
  1225. (org-element-map tree 'table-cell 'identity)))))))
  1226. (ert-deftest test-org-export/table-dimensions ()
  1227. "Test `org-export-table-dimensions' specifications."
  1228. ;; 1. Standard test.
  1229. (org-test-with-parsed-data "
  1230. | 1 | 2 | 3 |
  1231. | 4 | 5 | 6 |"
  1232. (should
  1233. (equal '(2 . 3)
  1234. (org-export-table-dimensions
  1235. (org-element-map tree 'table 'identity info 'first-match) info))))
  1236. ;; 2. Ignore horizontal rules and special columns.
  1237. (org-test-with-parsed-data "
  1238. | / | < | > |
  1239. | 1 | 2 | 3 |
  1240. |---+---+---|
  1241. | 4 | 5 | 6 |"
  1242. (should
  1243. (equal '(2 . 3)
  1244. (org-export-table-dimensions
  1245. (org-element-map tree 'table 'identity info 'first-match) info)))))
  1246. (ert-deftest test-org-export/table-cell-address ()
  1247. "Test `org-export-table-cell-address' specifications."
  1248. ;; 1. Standard test: index is 0-based.
  1249. (org-test-with-parsed-data "| a | b |"
  1250. (should
  1251. (equal '((0 . 0) (0 . 1))
  1252. (org-element-map
  1253. tree 'table-cell
  1254. (lambda (cell) (org-export-table-cell-address cell info))
  1255. info))))
  1256. ;; 2. Special column isn't counted, nor are special rows.
  1257. (org-test-with-parsed-data "
  1258. | / | <> |
  1259. | | c |"
  1260. (should
  1261. (equal '(0 . 0)
  1262. (org-export-table-cell-address
  1263. (car (last (org-element-map tree 'table-cell 'identity info)))
  1264. info))))
  1265. ;; 3. Tables rules do not count either.
  1266. (org-test-with-parsed-data "
  1267. | a |
  1268. |---|
  1269. | b |
  1270. |---|
  1271. | c |"
  1272. (should
  1273. (equal '(2 . 0)
  1274. (org-export-table-cell-address
  1275. (car (last (org-element-map tree 'table-cell 'identity info)))
  1276. info))))
  1277. ;; 4. Return nil for special cells.
  1278. (org-test-with-parsed-data "| / | a |"
  1279. (should-not
  1280. (org-export-table-cell-address
  1281. (org-element-map tree 'table-cell 'identity nil 'first-match)
  1282. info))))
  1283. (ert-deftest test-org-export/get-table-cell-at ()
  1284. "Test `org-export-get-table-cell-at' specifications."
  1285. ;; 1. Address ignores special columns, special rows and rules.
  1286. (org-test-with-parsed-data "
  1287. | / | <> |
  1288. | | a |
  1289. |---+----|
  1290. | | b |"
  1291. (should
  1292. (equal '("b")
  1293. (org-element-contents
  1294. (org-export-get-table-cell-at
  1295. '(1 . 0)
  1296. (org-element-map tree 'table 'identity info 'first-match)
  1297. info)))))
  1298. ;; 2. Return value for a non-existent address is nil.
  1299. (org-test-with-parsed-data "| a |"
  1300. (should-not
  1301. (org-export-get-table-cell-at
  1302. '(2 . 2)
  1303. (org-element-map tree 'table 'identity info 'first-match)
  1304. info)))
  1305. (org-test-with-parsed-data "| / |"
  1306. (should-not
  1307. (org-export-get-table-cell-at
  1308. '(0 . 0)
  1309. (org-element-map tree 'table 'identity info 'first-match)
  1310. info))))
  1311. (ert-deftest test-org-export/table-cell-starts-colgroup-p ()
  1312. "Test `org-export-table-cell-starts-colgroup-p' specifications."
  1313. ;; 1. A cell at a beginning of a row always starts a column group.
  1314. (org-test-with-parsed-data "| a |"
  1315. (should
  1316. (org-export-table-cell-starts-colgroup-p
  1317. (org-element-map tree 'table-cell 'identity info 'first-match)
  1318. info)))
  1319. ;; 2. Special column should be ignored when determining the
  1320. ;; beginning of the row.
  1321. (org-test-with-parsed-data "
  1322. | / | |
  1323. | | a |"
  1324. (should
  1325. (org-export-table-cell-starts-colgroup-p
  1326. (org-element-map tree 'table-cell 'identity info 'first-match)
  1327. info)))
  1328. ;; 2. Explicit column groups.
  1329. (org-test-with-parsed-data "
  1330. | / | | < |
  1331. | a | b | c |"
  1332. (should
  1333. (equal
  1334. '(yes no yes)
  1335. (org-element-map
  1336. tree 'table-cell
  1337. (lambda (cell)
  1338. (if (org-export-table-cell-starts-colgroup-p cell info) 'yes 'no))
  1339. info)))))
  1340. (ert-deftest test-org-export/table-cell-ends-colgroup-p ()
  1341. "Test `org-export-table-cell-ends-colgroup-p' specifications."
  1342. ;; 1. A cell at the end of a row always ends a column group.
  1343. (org-test-with-parsed-data "| a |"
  1344. (should
  1345. (org-export-table-cell-ends-colgroup-p
  1346. (org-element-map tree 'table-cell 'identity info 'first-match)
  1347. info)))
  1348. ;; 2. Special column should be ignored when determining the
  1349. ;; beginning of the row.
  1350. (org-test-with-parsed-data "
  1351. | / | |
  1352. | | a |"
  1353. (should
  1354. (org-export-table-cell-ends-colgroup-p
  1355. (org-element-map tree 'table-cell 'identity info 'first-match)
  1356. info)))
  1357. ;; 3. Explicit column groups.
  1358. (org-test-with-parsed-data "
  1359. | / | < | |
  1360. | a | b | c |"
  1361. (should
  1362. (equal
  1363. '(yes no yes)
  1364. (org-element-map
  1365. tree 'table-cell
  1366. (lambda (cell)
  1367. (if (org-export-table-cell-ends-colgroup-p cell info) 'yes 'no))
  1368. info)))))
  1369. (ert-deftest test-org-export/table-row-starts-rowgroup-p ()
  1370. "Test `org-export-table-row-starts-rowgroup-p' specifications."
  1371. ;; 1. A row at the beginning of a table always starts a row group.
  1372. ;; So does a row following a table rule.
  1373. (org-test-with-parsed-data "
  1374. | a |
  1375. |---|
  1376. | b |"
  1377. (should
  1378. (equal
  1379. '(yes no yes)
  1380. (org-element-map
  1381. tree 'table-row
  1382. (lambda (row)
  1383. (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
  1384. info))))
  1385. ;; 2. Special rows should be ignored when determining the beginning
  1386. ;; of the row.
  1387. (org-test-with-parsed-data "
  1388. | / | < |
  1389. | | a |
  1390. |---+---|
  1391. | / | < |
  1392. | | b |"
  1393. (should
  1394. (equal
  1395. '(yes no yes)
  1396. (org-element-map
  1397. tree 'table-row
  1398. (lambda (row)
  1399. (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
  1400. info)))))
  1401. (ert-deftest test-org-export/table-row-ends-rowgroup-p ()
  1402. "Test `org-export-table-row-ends-rowgroup-p' specifications."
  1403. ;; 1. A row at the end of a table always ends a row group. So does
  1404. ;; a row preceding a table rule.
  1405. (org-test-with-parsed-data "
  1406. | a |
  1407. |---|
  1408. | b |"
  1409. (should
  1410. (equal
  1411. '(yes no yes)
  1412. (org-element-map
  1413. tree 'table-row
  1414. (lambda (row)
  1415. (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
  1416. info))))
  1417. ;; 2. Special rows should be ignored when determining the beginning
  1418. ;; of the row.
  1419. (org-test-with-parsed-data "
  1420. | | a |
  1421. | / | < |
  1422. |---+---|
  1423. | | b |
  1424. | / | < |"
  1425. (should
  1426. (equal
  1427. '(yes no yes)
  1428. (org-element-map
  1429. tree 'table-row
  1430. (lambda (row)
  1431. (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
  1432. info)))))
  1433. (ert-deftest test-org-export/table-row-starts-header-p ()
  1434. "Test `org-export-table-row-starts-header-p' specifications."
  1435. ;; 1. Only the row starting the first row group starts the table
  1436. ;; header.
  1437. (org-test-with-parsed-data "
  1438. | a |
  1439. | b |
  1440. |---|
  1441. | c |"
  1442. (should
  1443. (equal
  1444. '(yes no no no)
  1445. (org-element-map
  1446. tree 'table-row
  1447. (lambda (row)
  1448. (if (org-export-table-row-starts-header-p row info) 'yes 'no))
  1449. info))))
  1450. ;; 2. A row cannot start an header if there's no header in the
  1451. ;; table.
  1452. (org-test-with-parsed-data "
  1453. | a |
  1454. |---|"
  1455. (should-not
  1456. (org-export-table-row-starts-header-p
  1457. (org-element-map tree 'table-row 'identity info 'first-match)
  1458. info))))
  1459. (ert-deftest test-org-export/table-row-ends-header-p ()
  1460. "Test `org-export-table-row-ends-header-p' specifications."
  1461. ;; 1. Only the row starting the first row group starts the table
  1462. ;; header.
  1463. (org-test-with-parsed-data "
  1464. | a |
  1465. | b |
  1466. |---|
  1467. | c |"
  1468. (should
  1469. (equal
  1470. '(no yes no no)
  1471. (org-element-map
  1472. tree 'table-row
  1473. (lambda (row)
  1474. (if (org-export-table-row-ends-header-p row info) 'yes 'no))
  1475. info))))
  1476. ;; 2. A row cannot start an header if there's no header in the
  1477. ;; table.
  1478. (org-test-with-parsed-data "
  1479. | a |
  1480. |---|"
  1481. (should-not
  1482. (org-export-table-row-ends-header-p
  1483. (org-element-map tree 'table-row 'identity info 'first-match)
  1484. info))))
  1485. (provide 'test-org-export)
  1486. ;;; test-org-export.el end here