test-org-export.el 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620
  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
  22. (cons type
  23. (lambda (obj contents info)
  24. (funcall
  25. (intern (format "org-element-%s-interpreter" type))
  26. obj contents)))
  27. transcode-table)))))
  28. (progn ,@body)))
  29. (defmacro org-test-with-parsed-data (data &rest body)
  30. "Execute body with parsed data available.
  31. DATA is a string containing the data to be parsed. BODY is the
  32. body to execute. Parse tree is available under the `tree'
  33. variable, and communication channel under `info'.
  34. This function calls `org-export-collect-tree-properties'. As
  35. such, `:ignore-list' (for `org-element-map') and
  36. `:parse-tree' (for `org-export-get-genealogy') properties are
  37. already filled in `info'."
  38. (declare (debug (form body)) (indent 1))
  39. `(org-test-with-temp-text ,data
  40. (let* ((tree (org-element-parse-buffer))
  41. (info (org-export-collect-tree-properties
  42. tree (org-export-get-environment))))
  43. ,@body)))
  44. ;;; Tests
  45. (ert-deftest test-org-export/parse-option-keyword ()
  46. "Test reading all standard #+OPTIONS: items."
  47. (should
  48. (equal
  49. (org-export-parse-option-keyword
  50. "H:1 num:t \\n:t timestamp:t arch:t author:t creator:t d:t email:t
  51. *:t e:t ::t f:t pri:t -:t ^:t toc:t |:t tags:t tasks:t <:t todo:t")
  52. '(:headline-levels
  53. 1 :preserve-breaks t :section-numbers t :time-stamp-file t
  54. :with-archived-trees t :with-author t :with-creator t :with-drawers t
  55. :with-email t :with-emphasize t :with-entities t :with-fixed-width t
  56. :with-footnotes t :with-priority t :with-special-strings t
  57. :with-sub-superscript t :with-toc t :with-tables t :with-tags t
  58. :with-tasks t :with-timestamps t :with-todo-keywords t)))
  59. ;; Test some special values.
  60. (should
  61. (equal
  62. (org-export-parse-option-keyword
  63. "arch:headline creator:comment d:(\"TEST\")
  64. ^:{} toc:1 tags:not-in-toc tasks:todo num:2 <:active")
  65. '( :section-numbers
  66. 2
  67. :with-archived-trees headline :with-creator comment
  68. :with-drawers ("TEST") :with-sub-superscript {} :with-toc 1
  69. :with-tags not-in-toc :with-tasks todo :with-timestamps active))))
  70. (ert-deftest test-org-export/get-inbuffer-options ()
  71. "Test reading all standard export keywords."
  72. (should
  73. (equal
  74. (org-test-with-temp-text "#+AUTHOR: Me, Myself and I
  75. #+CREATOR: Idem
  76. #+DATE: Today
  77. #+DESCRIPTION: Testing
  78. #+DESCRIPTION: with two lines
  79. #+EMAIL: some@email.org
  80. #+EXCLUDE_TAGS: noexport invisible
  81. #+KEYWORDS: test
  82. #+LANGUAGE: en
  83. #+SELECT_TAGS: export
  84. #+TITLE: Some title
  85. #+TITLE: with spaces"
  86. (org-export-get-inbuffer-options))
  87. '(:author
  88. ("Me, Myself and I") :creator "Idem" :date ("Today")
  89. :description "Testing\nwith two lines" :email "some@email.org"
  90. :exclude-tags ("noexport" "invisible") :keywords "test" :language "en"
  91. :select-tags ("export") :title ("Some title with spaces")))))
  92. (ert-deftest test-org-export/get-subtree-options ()
  93. "Test setting options from headline's properties."
  94. ;; EXPORT_TITLE.
  95. (org-test-with-temp-text "#+TITLE: Title
  96. * Headline
  97. :PROPERTIES:
  98. :EXPORT_TITLE: Subtree Title
  99. :END:
  100. Paragraph"
  101. (forward-line)
  102. (should (equal (plist-get (org-export-get-environment nil t) :title)
  103. '("Subtree Title"))))
  104. :title
  105. '("subtree-title")
  106. ;; EXPORT_OPTIONS.
  107. (org-test-with-temp-text "#+OPTIONS: H:1
  108. * Headline
  109. :PROPERTIES:
  110. :EXPORT_OPTIONS: H:2
  111. :END:
  112. Paragraph"
  113. (forward-line)
  114. (should
  115. (= 2 (plist-get (org-export-get-environment nil t) :headline-levels))))
  116. ;; EXPORT DATE.
  117. (org-test-with-temp-text "#+DATE: today
  118. * Headline
  119. :PROPERTIES:
  120. :EXPORT_DATE: 29-03-2012
  121. :END:
  122. Paragraph"
  123. (forward-line)
  124. (should (equal (plist-get (org-export-get-environment nil t) :date)
  125. '("29-03-2012")))))
  126. (ert-deftest test-org-export/handle-options ()
  127. "Test if export options have an impact on output."
  128. ;; Test exclude tags.
  129. (org-test-with-temp-text "* Head1 :noexport:"
  130. (org-test-with-backend test
  131. (should
  132. (equal (org-export-as 'test nil nil nil '(:exclude-tags ("noexport")))
  133. ""))))
  134. ;; Test include tags.
  135. (org-test-with-temp-text "
  136. * Head1
  137. * Head2
  138. ** Sub-Head2.1 :export:
  139. *** Sub-Head2.1.1
  140. * Head2"
  141. (org-test-with-backend test
  142. (should
  143. (equal
  144. "* Head2\n** Sub-Head2.1 :export:\n*** Sub-Head2.1.1\n"
  145. (let ((org-tags-column 0))
  146. (org-export-as 'test nil nil nil '(:select-tags ("export"))))))))
  147. ;; Test mixing include tags and exclude tags.
  148. (org-test-with-temp-text "
  149. * Head1 :export:
  150. ** Sub-Head1 :noexport:
  151. ** Sub-Head2
  152. * Head2 :noexport:
  153. ** Sub-Head1 :export:"
  154. (org-test-with-backend test
  155. (should
  156. (string-match
  157. "\\* Head1[ \t]+:export:\n\\*\\* Sub-Head2\n"
  158. (org-export-as
  159. 'test nil nil nil
  160. '(:select-tags ("export") :exclude-tags ("noexport")))))))
  161. ;; Ignore tasks.
  162. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  163. (org-test-with-temp-text "* TODO Head1"
  164. (org-test-with-backend test
  165. (should (equal (org-export-as 'test nil nil nil '(:with-tasks nil))
  166. "")))))
  167. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  168. (org-test-with-temp-text "* TODO Head1"
  169. (org-test-with-backend test
  170. (should (equal (org-export-as 'test nil nil nil '(:with-tasks t))
  171. "* TODO Head1\n")))))
  172. ;; Archived tree.
  173. (org-test-with-temp-text "* Head1 :archive:"
  174. (let ((org-archive-tag "archive"))
  175. (org-test-with-backend test
  176. (should
  177. (equal (org-export-as 'test nil nil nil '(:with-archived-trees nil))
  178. "")))))
  179. (org-test-with-temp-text "* Head1 :archive:\nbody\n** Sub-head 2"
  180. (let ((org-archive-tag "archive"))
  181. (org-test-with-backend test
  182. (should
  183. (string-match
  184. "\\* Head1[ \t]+:archive:"
  185. (org-export-as 'test nil nil nil
  186. '(:with-archived-trees headline)))))))
  187. (org-test-with-temp-text "* Head1 :archive:"
  188. (let ((org-archive-tag "archive"))
  189. (org-test-with-backend test
  190. (should
  191. (string-match
  192. "\\`\\* Head1[ \t]+:archive:\n\\'"
  193. (org-export-as 'test nil nil nil '(:with-archived-trees t)))))))
  194. ;; Drawers.
  195. (let ((org-drawers '("TEST")))
  196. (org-test-with-temp-text ":TEST:\ncontents\n:END:"
  197. (org-test-with-backend test
  198. (should (equal (org-export-as 'test nil nil nil '(:with-drawers nil))
  199. ""))
  200. (should (equal (org-export-as 'test nil nil nil '(:with-drawers t))
  201. ":TEST:\ncontents\n:END:\n")))))
  202. (let ((org-drawers '("FOO" "BAR")))
  203. (org-test-with-temp-text ":FOO:\nkeep\n:END:\n:BAR:\nremove\n:END:"
  204. (org-test-with-backend test
  205. (should
  206. (equal (org-export-as 'test nil nil nil '(:with-drawers ("FOO")))
  207. ":FOO:\nkeep\n:END:\n")))))
  208. ;; Timestamps.
  209. (org-test-with-temp-text "[2012-04-29 sun. 10:45]<2012-04-29 sun. 10:45>"
  210. (org-test-with-backend test
  211. (should
  212. (equal (org-export-as 'test nil nil nil '(:with-timestamps t))
  213. "[2012-04-29 sun. 10:45]<2012-04-29 sun. 10:45>\n"))
  214. (should
  215. (equal (org-export-as 'test nil nil nil '(:with-timestamps nil)) ""))
  216. (should
  217. (equal (org-export-as 'test nil nil nil '(:with-timestamps active))
  218. "<2012-04-29 sun. 10:45>\n"))
  219. (should
  220. (equal (org-export-as 'test nil nil nil '(:with-timestamps inactive))
  221. "[2012-04-29 sun. 10:45]\n"))))
  222. ;; Clocks.
  223. (let ((org-clock-string "CLOCK:"))
  224. (org-test-with-temp-text "CLOCK: [2012-04-29 sun. 10:45]"
  225. (org-test-with-backend test
  226. (should
  227. (equal (org-export-as 'test nil nil nil '(:with-clocks t))
  228. "CLOCK: [2012-04-29 sun. 10:45]\n"))
  229. (should
  230. (equal (org-export-as 'test nil nil nil '(:with-clocks nil)) "")))))
  231. ;; Plannings.
  232. (let ((org-closed-string "CLOSED:"))
  233. (org-test-with-temp-text "CLOSED: [2012-04-29 sun. 10:45]"
  234. (org-test-with-backend test
  235. (should
  236. (equal (org-export-as 'test nil nil nil '(:with-plannings t))
  237. "CLOSED: [2012-04-29 sun. 10:45]\n"))
  238. (should
  239. (equal (org-export-as 'test nil nil nil '(:with-plannings nil))
  240. ""))))))
  241. (ert-deftest test-org-export/comment-tree ()
  242. "Test if export process ignores commented trees."
  243. (let ((org-comment-string "COMMENT"))
  244. (org-test-with-temp-text "* COMMENT Head1"
  245. (org-test-with-backend test
  246. (should (equal (org-export-as 'test) ""))))))
  247. (ert-deftest test-org-export/export-scope ()
  248. "Test all export scopes."
  249. (org-test-with-temp-text "
  250. * Head1
  251. ** Head2
  252. text
  253. *** Head3"
  254. (org-test-with-backend test
  255. ;; Subtree.
  256. (forward-line 3)
  257. (should (equal (org-export-as 'test 'subtree) "text\n*** Head3\n"))
  258. ;; Visible.
  259. (goto-char (point-min))
  260. (forward-line)
  261. (org-cycle)
  262. (should (equal (org-export-as 'test nil 'visible) "* Head1\n"))
  263. ;; Body only.
  264. (flet ((org-test-template (body info) (format "BEGIN\n%sEND" body)))
  265. (push '(template . org-test-template) org-test-translate-alist)
  266. (should (equal (org-export-as 'test nil nil 'body-only)
  267. "* Head1\n** Head2\ntext\n*** Head3\n"))
  268. (should (equal (org-export-as 'test)
  269. "BEGIN\n* Head1\n** Head2\ntext\n*** Head3\nEND")))
  270. ;; Region.
  271. (goto-char (point-min))
  272. (forward-line 3)
  273. (transient-mark-mode 1)
  274. (push-mark (point) t t)
  275. (goto-char (point-at-eol))
  276. (should (equal (org-export-as 'test) "text\n"))))
  277. ;; Subtree with a code block calling another block outside.
  278. (org-test-with-temp-text "
  279. * Head1
  280. #+BEGIN_SRC emacs-lisp :noweb yes :exports results
  281. <<test>>
  282. #+END_SRC
  283. * Head2
  284. #+NAME: test
  285. #+BEGIN_SRC emacs-lisp
  286. \(+ 1 2)
  287. #+END_SRC"
  288. (org-test-with-backend test
  289. (forward-line 1)
  290. (should (equal (org-export-as 'test 'subtree) ": 3\n")))))
  291. (ert-deftest test-org-export/expand-include ()
  292. "Test file inclusion in an Org buffer."
  293. ;; Full insertion with recursive inclusion.
  294. (org-test-with-temp-text
  295. (format "#+INCLUDE: \"%s/examples/include.org\"" org-test-dir)
  296. (org-export-expand-include-keyword)
  297. (should (equal (buffer-string)
  298. "Small Org file with an include keyword.
  299. #+BEGIN_SRC emacs-lisp :exports results\n(+ 2 1)\n#+END_SRC
  300. Success!
  301. * Heading
  302. body\n")))
  303. ;; Localized insertion.
  304. (org-test-with-temp-text
  305. (format "#+INCLUDE: \"%s/examples/include.org\" :lines \"1-2\""
  306. org-test-dir)
  307. (org-export-expand-include-keyword)
  308. (should (equal (buffer-string)
  309. "Small Org file with an include keyword.\n")))
  310. ;; Insertion with constraints on headlines level.
  311. (org-test-with-temp-text
  312. (format
  313. "* Top heading\n#+INCLUDE: \"%s/examples/include.org\" :lines \"9-\""
  314. org-test-dir)
  315. (org-export-expand-include-keyword)
  316. (should (equal (buffer-string) "* Top heading\n** Heading\nbody\n")))
  317. ;; Inclusion within an example block.
  318. (org-test-with-temp-text
  319. (format "#+INCLUDE: \"%s/examples/include.org\" :lines \"1-2\" example"
  320. org-test-dir)
  321. (org-export-expand-include-keyword)
  322. (should
  323. (equal
  324. (buffer-string)
  325. "#+BEGIN_EXAMPLE\nSmall Org file with an include keyword.\n#+END_EXAMPLE\n")))
  326. ;; Inclusion within a src-block.
  327. (org-test-with-temp-text
  328. (format
  329. "#+INCLUDE: \"%s/examples/include.org\" :lines \"4-5\" src emacs-lisp"
  330. org-test-dir)
  331. (org-export-expand-include-keyword)
  332. (should (equal (buffer-string)
  333. "#+BEGIN_SRC emacs-lisp\n(+ 2 1)\n#+END_SRC\n"))))
  334. (ert-deftest test-org-export/user-ignore-list ()
  335. "Test if `:ignore-list' accepts user input."
  336. (org-test-with-backend test
  337. (flet ((skip-note-head
  338. (data backend info)
  339. ;; Ignore headlines with the word "note" in their title.
  340. (org-element-map
  341. data 'headline
  342. (lambda (headline)
  343. (when (string-match "\\<note\\>"
  344. (org-element-property :raw-value headline))
  345. (org-export-ignore-element headline info)))
  346. info)
  347. data))
  348. ;; Install function in parse tree filters.
  349. (let ((org-export-filter-parse-tree-functions '(skip-note-head)))
  350. (org-test-with-temp-text "* Head1\n* Head2 (note)\n"
  351. (should (equal (org-export-as 'test) "* Head1\n")))))))
  352. (ert-deftest test-org-export/before-parsing-hook ()
  353. "Test `org-export-before-parsing-hook'."
  354. (org-test-with-backend test
  355. (org-test-with-temp-text "* Headline 1\nBody 1\n* Headline 2\nBody 2"
  356. (let ((org-export-before-parsing-hook
  357. '((lambda ()
  358. (org-map-entries
  359. (lambda ()
  360. (delete-region (point) (progn (forward-line) (point)))))))))
  361. (should (equal (org-export-as 'test) "Body 1\nBody 2\n"))))))
  362. ;;; Affiliated Keywords
  363. (ert-deftest test-org-export/read-attribute ()
  364. "Test `org-export-read-attribute' specifications."
  365. ;; Standard test.
  366. (should
  367. (equal
  368. (org-export-read-attribute
  369. :attr_html
  370. (org-test-with-temp-text "#+ATTR_HTML: :a 1 :b 2\nParagraph"
  371. (org-element-current-element (point-max))))
  372. '(:a 1 :b 2)))
  373. ;; Return nil on empty attribute.
  374. (should-not
  375. (org-export-read-attribute
  376. :attr_html
  377. (org-test-with-temp-text "Paragraph"
  378. (org-element-current-element (point-max))))))
  379. ;;; Export Snippets
  380. (ert-deftest test-org-export/export-snippet ()
  381. "Test export snippets transcoding."
  382. (org-test-with-temp-text "@@test:A@@@@t:B@@"
  383. (org-test-with-backend test
  384. (let ((org-test-translate-alist
  385. (cons (cons 'export-snippet
  386. (lambda (snippet contents info)
  387. (when (eq (org-export-snippet-backend snippet) 'test)
  388. (org-element-property :value snippet))))
  389. org-test-translate-alist)))
  390. (let ((org-export-snippet-translation-alist nil))
  391. (should (equal (org-export-as 'test) "A\n")))
  392. (let ((org-export-snippet-translation-alist '(("t" . "test"))))
  393. (should (equal (org-export-as 'test) "AB\n")))))))
  394. ;;; Footnotes
  395. (ert-deftest test-org-export/footnotes ()
  396. "Test footnotes specifications."
  397. (let ((org-footnote-section nil)
  398. (org-export-with-footnotes t))
  399. ;; 1. Read every type of footnote.
  400. (org-test-with-parsed-data
  401. "Text[fn:1] [1] [fn:label:C] [fn::D]\n\n[fn:1] A\n\n[1] B"
  402. (should
  403. (equal
  404. '((1 . "A") (2 . "B") (3 . "C") (4 . "D"))
  405. (org-element-map
  406. tree 'footnote-reference
  407. (lambda (ref)
  408. (let ((def (org-export-get-footnote-definition ref info)))
  409. (cons (org-export-get-footnote-number ref info)
  410. (if (eq (org-element-property :type ref) 'inline) (car def)
  411. (car (org-element-contents
  412. (car (org-element-contents def))))))))
  413. info))))
  414. ;; 2. Test nested footnotes order.
  415. (org-test-with-parsed-data
  416. "Text[fn:1:A[fn:2]] [fn:3].\n\n[fn:2] B [fn:3] [fn::D].\n\n[fn:3] C."
  417. (should
  418. (equal
  419. '((1 . "fn:1") (2 . "fn:2") (3 . "fn:3") (4))
  420. (org-element-map
  421. tree 'footnote-reference
  422. (lambda (ref)
  423. (when (org-export-footnote-first-reference-p ref info)
  424. (cons (org-export-get-footnote-number ref info)
  425. (org-element-property :label ref))))
  426. info))))
  427. ;; 3. Test nested footnote in invisible definitions.
  428. (org-test-with-temp-text "Text[1]\n\n[1] B [2]\n\n[2] C."
  429. ;; Hide definitions.
  430. (narrow-to-region (point) (point-at-eol))
  431. (let* ((tree (org-element-parse-buffer))
  432. (info (org-combine-plists
  433. `(:parse-tree ,tree)
  434. (org-export-collect-tree-properties
  435. tree (org-export-get-environment)))))
  436. ;; Both footnotes should be seen.
  437. (should
  438. (= (length (org-export-collect-footnote-definitions tree info)) 2))))
  439. ;; 4. Test footnotes definitions collection.
  440. (org-test-with-parsed-data "Text[fn:1:A[fn:2]] [fn:3].
  441. \[fn:2] B [fn:3] [fn::D].
  442. \[fn:3] C."
  443. (should (= (length (org-export-collect-footnote-definitions tree info))
  444. 4)))
  445. ;; 5. Test export of footnotes defined outside parsing scope.
  446. (org-test-with-temp-text "[fn:1] Out of scope
  447. * Title
  448. Paragraph[fn:1]"
  449. (org-test-with-backend test
  450. (let ((org-test-translate-alist
  451. (cons (cons 'footnote-reference
  452. (lambda (fn contents info)
  453. (org-element-interpret-data
  454. (org-export-get-footnote-definition fn info))))
  455. org-test-translate-alist)))
  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
  926. (org-element-current-element (point-max)))
  927. '("(+ 1 1)\n"))))
  928. ;; 2. Code with reference.
  929. (org-test-with-temp-text
  930. "#+BEGIN_EXAMPLE\n(+ 1 1) (ref:test)\n#+END_EXAMPLE"
  931. (should (equal (org-export-unravel-code
  932. (org-element-current-element (point-max)))
  933. '("(+ 1 1)\n" (1 . "test")))))
  934. ;; 3. Code with user-defined reference.
  935. (org-test-with-temp-text
  936. "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\n(+ 1 1) [ref:test]\n#+END_EXAMPLE"
  937. (should (equal (org-export-unravel-code
  938. (org-element-current-element (point-max)))
  939. '("(+ 1 1)\n" (1 . "test")))))
  940. ;; 4. Code references keys are relative to the current block.
  941. (org-test-with-temp-text "
  942. #+BEGIN_EXAMPLE -n
  943. \(+ 1 1)
  944. #+END_EXAMPLE
  945. #+BEGIN_EXAMPLE +n
  946. \(+ 2 2)
  947. \(+ 3 3) (ref:one)
  948. #+END_EXAMPLE"
  949. (goto-line 5)
  950. (should (equal (org-export-unravel-code
  951. (org-element-current-element (point-max)))
  952. '("(+ 2 2)\n(+ 3 3)\n" (2 . "one")))))
  953. ;; 5. Free up comma-protected lines.
  954. ;;
  955. ;; 5.1. In an Org source block, every line is protected.
  956. (org-test-with-temp-text
  957. "#+BEGIN_SRC org\n,* Test\n,# comment\n,Text\n#+END_SRC"
  958. (should (equal (org-export-unravel-code
  959. (org-element-current-element (point-max)))
  960. '("* Test\n# comment\nText\n"))))
  961. ;; 5.2. In other blocks, only headlines, comments and keywords are
  962. ;; protected.
  963. (org-test-with-temp-text
  964. "#+BEGIN_EXAMPLE\n,* Headline\n, * Not headline\n,Keep\n#+END_EXAMPLE"
  965. (should (equal (org-export-unravel-code
  966. (org-element-current-element (point-max)))
  967. '("* Headline\n, * Not headline\n,Keep\n"))))))
  968. ;;; Tables
  969. (ert-deftest test-org-export/special-column ()
  970. "Test if the table's special column is properly recognized."
  971. ;; 1. First column is special if it contains only a special marking
  972. ;; characters or empty cells.
  973. (org-test-with-temp-text "
  974. | ! | 1 |
  975. | | 2 |"
  976. (should
  977. (org-export-table-has-special-column-p
  978. (org-element-map
  979. (org-element-parse-buffer) 'table 'identity nil 'first-match))))
  980. ;; 2. If the column contains anything else, it isn't special.
  981. (org-test-with-temp-text "
  982. | ! | 1 |
  983. | b | 2 |"
  984. (should-not
  985. (org-export-table-has-special-column-p
  986. (org-element-map
  987. (org-element-parse-buffer) 'table 'identity nil 'first-match))))
  988. ;; 3. Special marking characters are "#", "^", "*", "_", "/", "$"
  989. ;; and "!".
  990. (org-test-with-temp-text "
  991. | # | 1 |
  992. | ^ | 2 |
  993. | * | 3 |
  994. | _ | 4 |
  995. | / | 5 |
  996. | $ | 6 |
  997. | ! | 7 |"
  998. (should
  999. (org-export-table-has-special-column-p
  1000. (org-element-map
  1001. (org-element-parse-buffer) 'table 'identity nil 'first-match))))
  1002. ;; 4. A first column with only empty cells isn't considered as
  1003. ;; special.
  1004. (org-test-with-temp-text "
  1005. | | 1 |
  1006. | | 2 |"
  1007. (should-not
  1008. (org-export-table-has-special-column-p
  1009. (org-element-map
  1010. (org-element-parse-buffer) 'table 'identity nil 'first-match)))))
  1011. (ert-deftest test-org-export/table-row-is-special-p ()
  1012. "Test `org-export-table-row-is-special-p' specifications."
  1013. ;; 1. A row is special if it has a special marking character in the
  1014. ;; special column.
  1015. (org-test-with-parsed-data "| ! | 1 |"
  1016. (should
  1017. (org-export-table-row-is-special-p
  1018. (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  1019. ;; 2. A row is special when its first field is "/"
  1020. (org-test-with-parsed-data "
  1021. | / | 1 |
  1022. | a | b |"
  1023. (should
  1024. (org-export-table-row-is-special-p
  1025. (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  1026. ;; 3. A row only containing alignment cookies is also considered as
  1027. ;; special.
  1028. (org-test-with-parsed-data "| <5> | | <l> | <l22> |"
  1029. (should
  1030. (org-export-table-row-is-special-p
  1031. (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  1032. ;; 4. Everything else isn't considered as special.
  1033. (org-test-with-parsed-data "| \alpha | | c |"
  1034. (should-not
  1035. (org-export-table-row-is-special-p
  1036. (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  1037. ;; 5. Table's rules are never considered as special rows.
  1038. (org-test-with-parsed-data "|---+---|"
  1039. (should-not
  1040. (org-export-table-row-is-special-p
  1041. (org-element-map tree 'table-row 'identity nil 'first-match) info))))
  1042. (ert-deftest test-org-export/has-header-p ()
  1043. "Test `org-export-table-has-header-p' specifications."
  1044. ;; 1. With an header.
  1045. (org-test-with-parsed-data "
  1046. | a | b |
  1047. |---+---|
  1048. | c | d |"
  1049. (should
  1050. (org-export-table-has-header-p
  1051. (org-element-map tree 'table 'identity info 'first-match)
  1052. info)))
  1053. ;; 2. Without an header.
  1054. (org-test-with-parsed-data "
  1055. | a | b |
  1056. | c | d |"
  1057. (should-not
  1058. (org-export-table-has-header-p
  1059. (org-element-map tree 'table 'identity info 'first-match)
  1060. info)))
  1061. ;; 3. Don't get fooled with starting and ending rules.
  1062. (org-test-with-parsed-data "
  1063. |---+---|
  1064. | a | b |
  1065. | c | d |
  1066. |---+---|"
  1067. (should-not
  1068. (org-export-table-has-header-p
  1069. (org-element-map tree 'table 'identity info 'first-match)
  1070. info))))
  1071. (ert-deftest test-org-export/table-row-group ()
  1072. "Test `org-export-table-row-group' specifications."
  1073. ;; 1. A rule creates a new group.
  1074. (org-test-with-parsed-data "
  1075. | a | b |
  1076. |---+---|
  1077. | 1 | 2 |"
  1078. (should
  1079. (equal
  1080. '(1 nil 2)
  1081. (mapcar (lambda (row) (org-export-table-row-group row info))
  1082. (org-element-map tree 'table-row 'identity)))))
  1083. ;; 2. Special rows are ignored in count.
  1084. (org-test-with-parsed-data "
  1085. | / | < | > |
  1086. |---|---+---|
  1087. | | 1 | 2 |"
  1088. (should
  1089. (equal
  1090. '(nil nil 1)
  1091. (mapcar (lambda (row) (org-export-table-row-group row info))
  1092. (org-element-map tree 'table-row 'identity)))))
  1093. ;; 3. Double rules also are ignored in count.
  1094. (org-test-with-parsed-data "
  1095. | a | b |
  1096. |---+---|
  1097. |---+---|
  1098. | 1 | 2 |"
  1099. (should
  1100. (equal
  1101. '(1 nil nil 2)
  1102. (mapcar (lambda (row) (org-export-table-row-group row info))
  1103. (org-element-map tree 'table-row 'identity))))))
  1104. (ert-deftest test-org-export/table-cell-width ()
  1105. "Test `org-export-table-cell-width' specifications."
  1106. ;; 1. Width is primarily determined by width cookies. If no cookie
  1107. ;; is found, cell's width is nil.
  1108. (org-test-with-parsed-data "
  1109. | / | <l> | <6> | <l7> |
  1110. | | a | b | c |"
  1111. (should
  1112. (equal
  1113. '(nil 6 7)
  1114. (mapcar (lambda (cell) (org-export-table-cell-width cell info))
  1115. (org-element-map tree 'table-cell 'identity info)))))
  1116. ;; 2. The last width cookie has precedence.
  1117. (org-test-with-parsed-data "
  1118. | <6> |
  1119. | <7> |
  1120. | a |"
  1121. (should
  1122. (equal
  1123. '(7)
  1124. (mapcar (lambda (cell) (org-export-table-cell-width cell info))
  1125. (org-element-map tree 'table-cell 'identity info)))))
  1126. ;; 3. Valid width cookies must have a specific row.
  1127. (org-test-with-parsed-data "| <6> | cell |"
  1128. (should
  1129. (equal
  1130. '(nil nil)
  1131. (mapcar (lambda (cell) (org-export-table-cell-width cell info))
  1132. (org-element-map tree 'table-cell 'identity))))))
  1133. (ert-deftest test-org-export/table-cell-alignment ()
  1134. "Test `org-export-table-cell-alignment' specifications."
  1135. (let ((org-table-number-fraction 0.5)
  1136. (org-table-number-regexp "^[0-9]+$"))
  1137. ;; 1. Alignment is primarily determined by alignment cookies.
  1138. (org-test-with-temp-text "| <l> | <c> | <r> |"
  1139. (let* ((tree (org-element-parse-buffer))
  1140. (info `(:parse-tree ,tree)))
  1141. (should
  1142. (equal
  1143. '(left center right)
  1144. (mapcar (lambda (cell) (org-export-table-cell-alignment cell info))
  1145. (org-element-map tree 'table-cell 'identity))))))
  1146. ;; 2. The last alignment cookie has precedence.
  1147. (org-test-with-parsed-data "
  1148. | <l8> |
  1149. | cell |
  1150. | <r9> |"
  1151. (should
  1152. (equal
  1153. '(right right right)
  1154. (mapcar (lambda (cell) (org-export-table-cell-alignment cell info))
  1155. (org-element-map tree 'table-cell 'identity)))))
  1156. ;; 3. If there's no cookie, cell's contents determine alignment.
  1157. ;; A column mostly made of cells containing numbers will align
  1158. ;; its cells to the right.
  1159. (org-test-with-parsed-data "
  1160. | 123 |
  1161. | some text |
  1162. | 12345 |"
  1163. (should
  1164. (equal
  1165. '(right right right)
  1166. (mapcar (lambda (cell)
  1167. (org-export-table-cell-alignment cell info))
  1168. (org-element-map tree 'table-cell 'identity)))))
  1169. ;; 4. Otherwise, they will be aligned to the left.
  1170. (org-test-with-parsed-data "
  1171. | text |
  1172. | some text |
  1173. | \alpha |"
  1174. (should
  1175. (equal
  1176. '(left left left)
  1177. (mapcar (lambda (cell)
  1178. (org-export-table-cell-alignment cell info))
  1179. (org-element-map tree 'table-cell 'identity)))))))
  1180. (ert-deftest test-org-export/table-cell-borders ()
  1181. "Test `org-export-table-cell-borders' specifications."
  1182. ;; 1. Recognize various column groups indicators.
  1183. (org-test-with-parsed-data "| / | < | > | <> |"
  1184. (should
  1185. (equal
  1186. '((right bottom top) (left bottom top) (right bottom top)
  1187. (right left bottom top))
  1188. (mapcar (lambda (cell)
  1189. (org-export-table-cell-borders cell info))
  1190. (org-element-map tree 'table-cell 'identity)))))
  1191. ;; 2. Accept shortcuts to define column groups.
  1192. (org-test-with-parsed-data "| / | < | < |"
  1193. (should
  1194. (equal
  1195. '((right bottom top) (right left bottom top) (left bottom top))
  1196. (mapcar (lambda (cell)
  1197. (org-export-table-cell-borders cell info))
  1198. (org-element-map tree 'table-cell 'identity)))))
  1199. ;; 3. A valid column groups row must start with a "/".
  1200. (org-test-with-parsed-data "
  1201. | | < |
  1202. | a | b |"
  1203. (should
  1204. (equal '((top) (top) (bottom) (bottom))
  1205. (mapcar (lambda (cell)
  1206. (org-export-table-cell-borders cell info))
  1207. (org-element-map tree 'table-cell 'identity)))))
  1208. ;; 4. Take table rules into consideration.
  1209. (org-test-with-parsed-data "
  1210. | 1 |
  1211. |---|
  1212. | 2 |"
  1213. (should
  1214. (equal '((below top) (bottom above))
  1215. (mapcar (lambda (cell)
  1216. (org-export-table-cell-borders cell info))
  1217. (org-element-map tree 'table-cell 'identity)))))
  1218. ;; 5. Top and (resp. bottom) rules induce both `top' and `above'
  1219. ;; (resp. `bottom' and `below') borders. Any special row is
  1220. ;; ignored.
  1221. (org-test-with-parsed-data "
  1222. |---+----|
  1223. | / | |
  1224. | | 1 |
  1225. |---+----|"
  1226. (should
  1227. (equal '((bottom below top above))
  1228. (last
  1229. (mapcar (lambda (cell)
  1230. (org-export-table-cell-borders cell info))
  1231. (org-element-map tree 'table-cell 'identity)))))))
  1232. (ert-deftest test-org-export/table-dimensions ()
  1233. "Test `org-export-table-dimensions' specifications."
  1234. ;; 1. Standard test.
  1235. (org-test-with-parsed-data "
  1236. | 1 | 2 | 3 |
  1237. | 4 | 5 | 6 |"
  1238. (should
  1239. (equal '(2 . 3)
  1240. (org-export-table-dimensions
  1241. (org-element-map tree 'table 'identity info 'first-match) info))))
  1242. ;; 2. Ignore horizontal rules and special columns.
  1243. (org-test-with-parsed-data "
  1244. | / | < | > |
  1245. | 1 | 2 | 3 |
  1246. |---+---+---|
  1247. | 4 | 5 | 6 |"
  1248. (should
  1249. (equal '(2 . 3)
  1250. (org-export-table-dimensions
  1251. (org-element-map tree 'table 'identity info 'first-match) info)))))
  1252. (ert-deftest test-org-export/table-cell-address ()
  1253. "Test `org-export-table-cell-address' specifications."
  1254. ;; 1. Standard test: index is 0-based.
  1255. (org-test-with-parsed-data "| a | b |"
  1256. (should
  1257. (equal '((0 . 0) (0 . 1))
  1258. (org-element-map
  1259. tree 'table-cell
  1260. (lambda (cell) (org-export-table-cell-address cell info))
  1261. info))))
  1262. ;; 2. Special column isn't counted, nor are special rows.
  1263. (org-test-with-parsed-data "
  1264. | / | <> |
  1265. | | c |"
  1266. (should
  1267. (equal '(0 . 0)
  1268. (org-export-table-cell-address
  1269. (car (last (org-element-map tree 'table-cell 'identity info)))
  1270. info))))
  1271. ;; 3. Tables rules do not count either.
  1272. (org-test-with-parsed-data "
  1273. | a |
  1274. |---|
  1275. | b |
  1276. |---|
  1277. | c |"
  1278. (should
  1279. (equal '(2 . 0)
  1280. (org-export-table-cell-address
  1281. (car (last (org-element-map tree 'table-cell 'identity info)))
  1282. info))))
  1283. ;; 4. Return nil for special cells.
  1284. (org-test-with-parsed-data "| / | a |"
  1285. (should-not
  1286. (org-export-table-cell-address
  1287. (org-element-map tree 'table-cell 'identity nil 'first-match)
  1288. info))))
  1289. (ert-deftest test-org-export/get-table-cell-at ()
  1290. "Test `org-export-get-table-cell-at' specifications."
  1291. ;; 1. Address ignores special columns, special rows and rules.
  1292. (org-test-with-parsed-data "
  1293. | / | <> |
  1294. | | a |
  1295. |---+----|
  1296. | | b |"
  1297. (should
  1298. (equal '("b")
  1299. (org-element-contents
  1300. (org-export-get-table-cell-at
  1301. '(1 . 0)
  1302. (org-element-map tree 'table 'identity info 'first-match)
  1303. info)))))
  1304. ;; 2. Return value for a non-existent address is nil.
  1305. (org-test-with-parsed-data "| a |"
  1306. (should-not
  1307. (org-export-get-table-cell-at
  1308. '(2 . 2)
  1309. (org-element-map tree 'table 'identity info 'first-match)
  1310. info)))
  1311. (org-test-with-parsed-data "| / |"
  1312. (should-not
  1313. (org-export-get-table-cell-at
  1314. '(0 . 0)
  1315. (org-element-map tree 'table 'identity info 'first-match)
  1316. info))))
  1317. (ert-deftest test-org-export/table-cell-starts-colgroup-p ()
  1318. "Test `org-export-table-cell-starts-colgroup-p' specifications."
  1319. ;; 1. A cell at a beginning of a row always starts a column group.
  1320. (org-test-with-parsed-data "| a |"
  1321. (should
  1322. (org-export-table-cell-starts-colgroup-p
  1323. (org-element-map tree 'table-cell 'identity info 'first-match)
  1324. info)))
  1325. ;; 2. Special column should be ignored when determining the
  1326. ;; beginning of the row.
  1327. (org-test-with-parsed-data "
  1328. | / | |
  1329. | | a |"
  1330. (should
  1331. (org-export-table-cell-starts-colgroup-p
  1332. (org-element-map tree 'table-cell 'identity info 'first-match)
  1333. info)))
  1334. ;; 2. Explicit column groups.
  1335. (org-test-with-parsed-data "
  1336. | / | | < |
  1337. | a | b | c |"
  1338. (should
  1339. (equal
  1340. '(yes no yes)
  1341. (org-element-map
  1342. tree 'table-cell
  1343. (lambda (cell)
  1344. (if (org-export-table-cell-starts-colgroup-p cell info) 'yes 'no))
  1345. info)))))
  1346. (ert-deftest test-org-export/table-cell-ends-colgroup-p ()
  1347. "Test `org-export-table-cell-ends-colgroup-p' specifications."
  1348. ;; 1. A cell at the end of a row always ends a column group.
  1349. (org-test-with-parsed-data "| a |"
  1350. (should
  1351. (org-export-table-cell-ends-colgroup-p
  1352. (org-element-map tree 'table-cell 'identity info 'first-match)
  1353. info)))
  1354. ;; 2. Special column should be ignored when determining the
  1355. ;; beginning of the row.
  1356. (org-test-with-parsed-data "
  1357. | / | |
  1358. | | a |"
  1359. (should
  1360. (org-export-table-cell-ends-colgroup-p
  1361. (org-element-map tree 'table-cell 'identity info 'first-match)
  1362. info)))
  1363. ;; 3. Explicit column groups.
  1364. (org-test-with-parsed-data "
  1365. | / | < | |
  1366. | a | b | c |"
  1367. (should
  1368. (equal
  1369. '(yes no yes)
  1370. (org-element-map
  1371. tree 'table-cell
  1372. (lambda (cell)
  1373. (if (org-export-table-cell-ends-colgroup-p cell info) 'yes 'no))
  1374. info)))))
  1375. (ert-deftest test-org-export/table-row-starts-rowgroup-p ()
  1376. "Test `org-export-table-row-starts-rowgroup-p' specifications."
  1377. ;; 1. A row at the beginning of a table always starts a row group.
  1378. ;; So does a row following a table rule.
  1379. (org-test-with-parsed-data "
  1380. | a |
  1381. |---|
  1382. | b |"
  1383. (should
  1384. (equal
  1385. '(yes no yes)
  1386. (org-element-map
  1387. tree 'table-row
  1388. (lambda (row)
  1389. (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
  1390. info))))
  1391. ;; 2. Special rows should be ignored when determining the beginning
  1392. ;; of the row.
  1393. (org-test-with-parsed-data "
  1394. | / | < |
  1395. | | a |
  1396. |---+---|
  1397. | / | < |
  1398. | | b |"
  1399. (should
  1400. (equal
  1401. '(yes no yes)
  1402. (org-element-map
  1403. tree 'table-row
  1404. (lambda (row)
  1405. (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
  1406. info)))))
  1407. (ert-deftest test-org-export/table-row-ends-rowgroup-p ()
  1408. "Test `org-export-table-row-ends-rowgroup-p' specifications."
  1409. ;; 1. A row at the end of a table always ends a row group. So does
  1410. ;; a row preceding a table rule.
  1411. (org-test-with-parsed-data "
  1412. | a |
  1413. |---|
  1414. | b |"
  1415. (should
  1416. (equal
  1417. '(yes no yes)
  1418. (org-element-map
  1419. tree 'table-row
  1420. (lambda (row)
  1421. (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
  1422. info))))
  1423. ;; 2. Special rows should be ignored when determining the beginning
  1424. ;; of the row.
  1425. (org-test-with-parsed-data "
  1426. | | a |
  1427. | / | < |
  1428. |---+---|
  1429. | | b |
  1430. | / | < |"
  1431. (should
  1432. (equal
  1433. '(yes no yes)
  1434. (org-element-map
  1435. tree 'table-row
  1436. (lambda (row)
  1437. (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
  1438. info)))))
  1439. (ert-deftest test-org-export/table-row-starts-header-p ()
  1440. "Test `org-export-table-row-starts-header-p' specifications."
  1441. ;; 1. Only the row starting the first row group starts the table
  1442. ;; header.
  1443. (org-test-with-parsed-data "
  1444. | a |
  1445. | b |
  1446. |---|
  1447. | c |"
  1448. (should
  1449. (equal
  1450. '(yes no no no)
  1451. (org-element-map
  1452. tree 'table-row
  1453. (lambda (row)
  1454. (if (org-export-table-row-starts-header-p row info) 'yes 'no))
  1455. info))))
  1456. ;; 2. A row cannot start an header if there's no header in the
  1457. ;; table.
  1458. (org-test-with-parsed-data "
  1459. | a |
  1460. |---|"
  1461. (should-not
  1462. (org-export-table-row-starts-header-p
  1463. (org-element-map tree 'table-row 'identity info 'first-match)
  1464. info))))
  1465. (ert-deftest test-org-export/table-row-ends-header-p ()
  1466. "Test `org-export-table-row-ends-header-p' specifications."
  1467. ;; 1. Only the row starting the first row group starts the table
  1468. ;; header.
  1469. (org-test-with-parsed-data "
  1470. | a |
  1471. | b |
  1472. |---|
  1473. | c |"
  1474. (should
  1475. (equal
  1476. '(no yes no no)
  1477. (org-element-map
  1478. tree 'table-row
  1479. (lambda (row)
  1480. (if (org-export-table-row-ends-header-p row info) 'yes 'no))
  1481. info))))
  1482. ;; 2. A row cannot start an header if there's no header in the
  1483. ;; table.
  1484. (org-test-with-parsed-data "
  1485. | a |
  1486. |---|"
  1487. (should-not
  1488. (org-export-table-row-ends-header-p
  1489. (org-element-map tree 'table-row 'identity info 'first-match)
  1490. info))))
  1491. ;;; Topology
  1492. (ert-deftest test-org-export/get-next-element ()
  1493. "Test `org-export-get-next-element' specifications."
  1494. ;; Standard test.
  1495. (should
  1496. (equal "b"
  1497. (org-test-with-parsed-data "* Headline\n*a* b"
  1498. (org-export-get-next-element
  1499. (org-element-map tree 'bold 'identity info t)))))
  1500. ;; Return nil when no previous element.
  1501. (should-not
  1502. (org-test-with-parsed-data "* Headline\na *b*"
  1503. (org-export-get-next-element
  1504. (org-element-map tree 'bold 'identity info t)))))
  1505. (ert-deftest test-org-export/get-previous-element ()
  1506. "Test `org-export-get-previous-element' specifications."
  1507. ;; Standard test.
  1508. (should
  1509. (equal "a "
  1510. (org-test-with-parsed-data "* Headline\na *b*"
  1511. (org-export-get-previous-element
  1512. (org-element-map tree 'bold 'identity info t)))))
  1513. ;; Return nil when no previous element.
  1514. (should-not
  1515. (org-test-with-parsed-data "* Headline\n*a* b"
  1516. (org-export-get-previous-element
  1517. (org-element-map tree 'bold 'identity info t)))))
  1518. (provide 'test-org-export)
  1519. ;;; test-org-export.el end here