test-ox.el 80 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422
  1. ;;; test-ox.el --- Tests for ox.el
  2. ;; Copyright (C) 2012, 2013 Nicolas Goaziou
  3. ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
  4. ;; This file is not part of GNU Emacs.
  5. ;; This program is free software; you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; This program is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Code:
  16. (unless (featurep 'ox)
  17. (signal 'missing-test-dependency "org-export"))
  18. (defmacro org-test-with-backend (backend &rest body)
  19. "Execute body with an export back-end defined.
  20. BACKEND is the name of the back-end. BODY is the body to
  21. execute. The defined back-end simply returns parsed data as Org
  22. syntax."
  23. (declare (debug (form body)) (indent 1))
  24. `(let ((org-export-registered-backends
  25. ',(list
  26. (list backend
  27. :translate-alist
  28. (let (transcode-table)
  29. (dolist (type (append org-element-all-elements
  30. org-element-all-objects)
  31. transcode-table)
  32. (push
  33. (cons type
  34. (lambda (obj contents info)
  35. (funcall
  36. (intern (format "org-element-%s-interpreter"
  37. type))
  38. obj contents)))
  39. transcode-table)))))))
  40. (progn ,@body)))
  41. (defmacro org-test-with-parsed-data (data &rest body)
  42. "Execute body with parsed data available.
  43. DATA is a string containing the data to be parsed. BODY is the
  44. body to execute. Parse tree is available under the `tree'
  45. variable, and communication channel under `info'.
  46. This function calls `org-export-collect-tree-properties'. As
  47. such, `:ignore-list' (for `org-element-map') and
  48. `:parse-tree' (for `org-export-get-genealogy') properties are
  49. already filled in `info'."
  50. (declare (debug (form body)) (indent 1))
  51. `(org-test-with-temp-text ,data
  52. (let* ((tree (org-element-parse-buffer))
  53. (info (org-export-collect-tree-properties
  54. tree (org-export-get-environment))))
  55. ,@body)))
  56. ;;; Internal Tests
  57. (ert-deftest test-org-export/bind-keyword ()
  58. "Test reading #+BIND: keywords."
  59. ;; Test with `org-export-allow-bind-keywords' set to t.
  60. (should
  61. (org-test-with-temp-text "#+BIND: variable value"
  62. (let ((org-export-allow-bind-keywords t))
  63. (org-export--install-letbind-maybe)
  64. (eq variable 'value))))
  65. ;; Test with `org-export-allow-bind-keywords' set to nil.
  66. (should-not
  67. (org-test-with-temp-text "#+BIND: variable value"
  68. (let ((org-export-allow-bind-keywords nil))
  69. (org-export--install-letbind-maybe)
  70. (boundp 'variable))))
  71. ;; BIND keywords are case-insensitive.
  72. (should
  73. (org-test-with-temp-text "#+bind: variable value"
  74. (let ((org-export-allow-bind-keywords t))
  75. (org-export--install-letbind-maybe)
  76. (eq variable 'value)))))
  77. (ert-deftest test-org-export/parse-option-keyword ()
  78. "Test reading all standard #+OPTIONS: items."
  79. (should
  80. (equal
  81. (org-export--parse-option-keyword
  82. "H:1 num:t \\n:t timestamp:t arch:t author:t creator:t d:t email:t
  83. *:t e:t ::t f:t pri:t -:t ^:t toc:t |:t tags:t tasks:t <:t todo:t inline:nil
  84. stat:t")
  85. '(:headline-levels
  86. 1 :preserve-breaks t :section-numbers t :time-stamp-file t
  87. :with-archived-trees t :with-author t :with-creator t :with-drawers t
  88. :with-email t :with-emphasize t :with-entities t :with-fixed-width t
  89. :with-footnotes t :with-inlinetasks nil :with-priority t
  90. :with-special-strings t :with-statistics-cookies t :with-sub-superscript t
  91. :with-toc t :with-tables t :with-tags t :with-tasks t :with-timestamps t
  92. :with-todo-keywords t)))
  93. ;; Test some special values.
  94. (should
  95. (equal
  96. (org-export--parse-option-keyword
  97. "arch:headline creator:comment d:(\"TEST\")
  98. ^:{} toc:1 tags:not-in-toc tasks:todo num:2 <:active")
  99. '( :section-numbers
  100. 2
  101. :with-archived-trees headline :with-creator comment
  102. :with-drawers ("TEST") :with-sub-superscript {} :with-toc 1
  103. :with-tags not-in-toc :with-tasks todo :with-timestamps active))))
  104. (ert-deftest test-org-export/get-inbuffer-options ()
  105. "Test reading all standard export keywords."
  106. ;; Properties should follow buffer order.
  107. (should
  108. (equal
  109. (org-test-with-temp-text "#+LANGUAGE: fr\n#+CREATOR: Me\n#+EMAIL: email"
  110. (org-export--get-inbuffer-options))
  111. '(:language "fr" :creator "Me" :email "email")))
  112. ;; Parse document keywords.
  113. (should
  114. (equal
  115. (org-test-with-temp-text "#+AUTHOR: Me"
  116. (org-export--get-inbuffer-options))
  117. '(:author ("Me"))))
  118. ;; Test `space' behaviour.
  119. (should
  120. (equal
  121. (org-test-with-temp-text "#+TITLE: Some title\n#+TITLE: with spaces"
  122. (org-export--get-inbuffer-options))
  123. '(:title ("Some title with spaces"))))
  124. ;; Test `newline' behaviour.
  125. (should
  126. (equal
  127. (org-test-with-temp-text "#+DESCRIPTION: With\n#+DESCRIPTION: two lines"
  128. (org-export--get-inbuffer-options))
  129. '(:description "With\ntwo lines")))
  130. ;; Test `split' behaviour.
  131. (should
  132. (equal
  133. (org-test-with-temp-text "#+SELECT_TAGS: a\n#+SELECT_TAGS: b"
  134. (org-export--get-inbuffer-options))
  135. '(:select-tags ("a" "b"))))
  136. ;; Options set through SETUPFILE.
  137. (should
  138. (equal
  139. (org-test-with-temp-text
  140. (format "#+DESCRIPTION: l1
  141. #+LANGUAGE: es
  142. #+SELECT_TAGS: a
  143. #+TITLE: a
  144. #+SETUPFILE: \"%s/examples/setupfile.org\"
  145. #+DESCRIPTION: l3
  146. #+LANGUAGE: fr
  147. #+SELECT_TAGS: c
  148. #+TITLE: c"
  149. org-test-dir)
  150. (org-export--get-inbuffer-options))
  151. '(:description "l1\nl2\nl3":language "fr" :select-tags ("a" "b" "c")
  152. :title ("a b c")))))
  153. (ert-deftest test-org-export/get-subtree-options ()
  154. "Test setting options from headline's properties."
  155. ;; EXPORT_TITLE.
  156. (org-test-with-temp-text "#+TITLE: Title
  157. * Headline
  158. :PROPERTIES:
  159. :EXPORT_TITLE: Subtree Title
  160. :END:
  161. Paragraph"
  162. (forward-line)
  163. (should (equal (plist-get (org-export-get-environment nil t) :title)
  164. '("Subtree Title"))))
  165. :title
  166. '("subtree-title")
  167. ;; EXPORT_OPTIONS.
  168. (org-test-with-temp-text "#+OPTIONS: H:1
  169. * Headline
  170. :PROPERTIES:
  171. :EXPORT_OPTIONS: H:2
  172. :END:
  173. Paragraph"
  174. (forward-line)
  175. (should
  176. (= 2 (plist-get (org-export-get-environment nil t) :headline-levels))))
  177. ;; EXPORT_DATE.
  178. (org-test-with-temp-text "#+DATE: today
  179. * Headline
  180. :PROPERTIES:
  181. :EXPORT_DATE: 29-03-2012
  182. :END:
  183. Paragraph"
  184. (forward-line)
  185. (should (equal (plist-get (org-export-get-environment nil t) :date)
  186. '("29-03-2012"))))
  187. ;; Properties with `split' behaviour are stored as a list of
  188. ;; strings.
  189. (should
  190. (equal '("a" "b")
  191. (org-test-with-temp-text "#+EXCLUDE_TAGS: noexport
  192. * Headline
  193. :PROPERTIES:
  194. :EXPORT_EXCLUDE_TAGS: a b
  195. :END:
  196. Paragraph"
  197. (progn
  198. (forward-line)
  199. (plist-get (org-export-get-environment nil t) :exclude-tags)))))
  200. ;; Handle :PROPERTY+: syntax.
  201. (should
  202. (equal '("a" "b")
  203. (org-test-with-temp-text "#+EXCLUDE_TAGS: noexport
  204. * Headline
  205. :PROPERTIES:
  206. :EXPORT_EXCLUDE_TAGS: a
  207. :EXPORT_EXCLUDE_TAGS+: b
  208. :END:
  209. Paragraph"
  210. (progn
  211. (forward-line)
  212. (plist-get (org-export-get-environment nil t) :exclude-tags)))))
  213. ;; Export properties are case-insensitive.
  214. (org-test-with-temp-text "* Headline
  215. :PROPERTIES:
  216. :EXPORT_Date: 29-03-2012
  217. :END:
  218. Paragraph"
  219. (should (equal (plist-get (org-export-get-environment nil t) :date)
  220. '("29-03-2012"))))
  221. ;; Still grab correct options when section above is empty.
  222. (should
  223. (equal '("H1")
  224. (org-test-with-temp-text "* H1\n** H11\n** H12"
  225. (progn (forward-line 2)
  226. (plist-get (org-export-get-environment nil t) :title))))))
  227. (ert-deftest test-org-export/handle-options ()
  228. "Test if export options have an impact on output."
  229. ;; Test exclude tags for headlines and inlinetasks.
  230. (should
  231. (equal ""
  232. (org-test-with-temp-text "* Head1 :noexp:"
  233. (org-test-with-backend test
  234. (org-export-as 'test nil nil nil '(:exclude-tags ("noexp")))))))
  235. ;; Test include tags for headlines and inlinetasks.
  236. (should
  237. (equal "* H2\n** Sub :exp:\n*** Sub Sub\n"
  238. (org-test-with-temp-text "* H1\n* H2\n** Sub :exp:\n*** Sub Sub\n* H3"
  239. (let ((org-tags-column 0))
  240. (org-test-with-backend test
  241. (org-export-as 'test nil nil nil '(:select-tags ("exp"))))))))
  242. ;; Test mixing include tags and exclude tags.
  243. (org-test-with-temp-text "
  244. * Head1 :export:
  245. ** Sub-Head1 :noexport:
  246. ** Sub-Head2
  247. * Head2 :noexport:
  248. ** Sub-Head1 :export:"
  249. (org-test-with-backend test
  250. (should
  251. (string-match
  252. "\\* Head1[ \t]+:export:\n\\*\\* Sub-Head2\n"
  253. (org-export-as
  254. 'test nil nil nil
  255. '(:select-tags ("export") :exclude-tags ("noexport")))))))
  256. ;; Ignore tasks.
  257. (should
  258. (equal ""
  259. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  260. (org-test-with-temp-text "* TODO Head1"
  261. (org-test-with-backend test
  262. (org-export-as 'test nil nil nil '(:with-tasks nil)))))))
  263. (should
  264. (equal "* TODO Head1\n"
  265. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  266. (org-test-with-temp-text "* TODO Head1"
  267. (org-test-with-backend test
  268. (org-export-as 'test nil nil nil '(:with-tasks t)))))))
  269. ;; Archived tree.
  270. (org-test-with-temp-text "* Head1 :archive:"
  271. (let ((org-archive-tag "archive"))
  272. (org-test-with-backend test
  273. (should
  274. (equal (org-export-as 'test nil nil nil '(:with-archived-trees nil))
  275. "")))))
  276. (org-test-with-temp-text "* Head1 :archive:\nbody\n** Sub-head 2"
  277. (let ((org-archive-tag "archive"))
  278. (org-test-with-backend test
  279. (should
  280. (string-match
  281. "\\* Head1[ \t]+:archive:"
  282. (org-export-as 'test nil nil nil
  283. '(:with-archived-trees headline)))))))
  284. (org-test-with-temp-text "* Head1 :archive:"
  285. (let ((org-archive-tag "archive"))
  286. (org-test-with-backend test
  287. (should
  288. (string-match
  289. "\\`\\* Head1[ \t]+:archive:\n\\'"
  290. (org-export-as 'test nil nil nil '(:with-archived-trees t)))))))
  291. ;; Clocks.
  292. (let ((org-clock-string "CLOCK:"))
  293. (org-test-with-temp-text "CLOCK: [2012-04-29 sun. 10:45]"
  294. (org-test-with-backend test
  295. (should
  296. (equal (org-export-as 'test nil nil nil '(:with-clocks t))
  297. "CLOCK: [2012-04-29 sun. 10:45]\n"))
  298. (should
  299. (equal (org-export-as 'test nil nil nil '(:with-clocks nil)) "")))))
  300. ;; Drawers.
  301. (let ((org-drawers '("TEST")))
  302. (org-test-with-temp-text ":TEST:\ncontents\n:END:"
  303. (org-test-with-backend test
  304. (should (equal (org-export-as 'test nil nil nil '(:with-drawers nil))
  305. ""))
  306. (should (equal (org-export-as 'test nil nil nil '(:with-drawers t))
  307. ":TEST:\ncontents\n:END:\n")))))
  308. (let ((org-drawers '("FOO" "BAR")))
  309. (org-test-with-temp-text ":FOO:\nkeep\n:END:\n:BAR:\nremove\n:END:"
  310. (org-test-with-backend test
  311. (should
  312. (equal (org-export-as 'test nil nil nil '(:with-drawers ("FOO")))
  313. ":FOO:\nkeep\n:END:\n")))))
  314. (let ((org-drawers '("FOO" "BAR")))
  315. (org-test-with-temp-text ":FOO:\nkeep\n:END:\n:BAR:\nremove\n:END:"
  316. (org-test-with-backend test
  317. (should
  318. (equal (org-export-as 'test nil nil nil '(:with-drawers (not "BAR")))
  319. ":FOO:\nkeep\n:END:\n")))))
  320. ;; Inlinetasks.
  321. (when (featurep 'org-inlinetask)
  322. (should
  323. (equal
  324. (let ((org-inlinetask-min-level 15))
  325. (org-test-with-temp-text "*************** Task"
  326. (org-test-with-backend test
  327. (org-export-as 'test nil nil nil '(:with-inlinetasks nil)))))
  328. ""))
  329. (should
  330. (equal
  331. (let ((org-inlinetask-min-level 15))
  332. (org-test-with-temp-text
  333. "*************** Task\nContents\n*************** END"
  334. (org-test-with-backend test
  335. (org-export-as 'test nil nil nil '(:with-inlinetasks nil)))))
  336. "")))
  337. ;; Plannings.
  338. (let ((org-closed-string "CLOSED:"))
  339. (org-test-with-temp-text "CLOSED: [2012-04-29 sun. 10:45]"
  340. (org-test-with-backend test
  341. (should
  342. (equal (org-export-as 'test nil nil nil '(:with-plannings t))
  343. "CLOSED: [2012-04-29 sun. 10:45]\n"))
  344. (should
  345. (equal (org-export-as 'test nil nil nil '(:with-plannings nil))
  346. "")))))
  347. ;; Statistics cookies.
  348. (should
  349. (equal ""
  350. (org-test-with-temp-text "[0/0]"
  351. (org-test-with-backend test
  352. (org-export-as
  353. 'test nil nil nil '(:with-statistics-cookies nil))))))
  354. ;; Timestamps.
  355. (org-test-with-temp-text "[2012-04-29 sun. 10:45]<2012-04-29 sun. 10:45>"
  356. (org-test-with-backend test
  357. (should
  358. (equal (org-export-as 'test nil nil nil '(:with-timestamps t))
  359. "[2012-04-29 sun. 10:45]<2012-04-29 sun. 10:45>\n"))
  360. (should
  361. (equal (org-export-as 'test nil nil nil '(:with-timestamps nil)) ""))
  362. (should
  363. (equal (org-export-as 'test nil nil nil '(:with-timestamps active))
  364. "<2012-04-29 sun. 10:45>\n"))
  365. (should
  366. (equal (org-export-as 'test nil nil nil '(:with-timestamps inactive))
  367. "[2012-04-29 sun. 10:45]\n")))))
  368. (ert-deftest test-org-export/comment-tree ()
  369. "Test if export process ignores commented trees."
  370. (let ((org-comment-string "COMMENT"))
  371. (org-test-with-temp-text "* COMMENT Head1"
  372. (org-test-with-backend test
  373. (should (equal (org-export-as 'test) ""))))))
  374. (ert-deftest test-org-export/export-scope ()
  375. "Test all export scopes."
  376. (org-test-with-temp-text "
  377. * Head1
  378. ** Head2
  379. text
  380. *** Head3"
  381. (org-test-with-backend test
  382. ;; Subtree.
  383. (forward-line 3)
  384. (should (equal (org-export-as 'test 'subtree) "text\n*** Head3\n"))
  385. ;; Visible.
  386. (goto-char (point-min))
  387. (forward-line)
  388. (org-cycle)
  389. (should (equal (org-export-as 'test nil 'visible) "* Head1\n"))
  390. ;; Region.
  391. (goto-char (point-min))
  392. (forward-line 3)
  393. (transient-mark-mode 1)
  394. (push-mark (point) t t)
  395. (goto-char (point-at-eol))
  396. (should (equal (org-export-as 'test) "text\n"))))
  397. ;; Subtree with a code block calling another block outside.
  398. (should
  399. (equal ": 3\n"
  400. (org-test-with-temp-text "
  401. * Head1
  402. #+BEGIN_SRC emacs-lisp :noweb yes :exports results
  403. <<test>>
  404. #+END_SRC
  405. * Head2
  406. #+NAME: test
  407. #+BEGIN_SRC emacs-lisp
  408. \(+ 1 2)
  409. #+END_SRC"
  410. (org-test-with-backend test
  411. (forward-line 1)
  412. (org-export-as 'test 'subtree)))))
  413. ;; Body only.
  414. (org-test-with-temp-text "Text"
  415. (org-test-with-backend test
  416. (plist-put
  417. (cdr (assq 'test org-export-registered-backends))
  418. :translate-alist
  419. (cons (cons 'template (lambda (body info) (format "BEGIN\n%sEND" body)))
  420. (org-export-backend-translate-table 'test)))
  421. (should (equal (org-export-as 'test nil nil 'body-only) "Text\n"))
  422. (should (equal (org-export-as 'test) "BEGIN\nText\nEND")))))
  423. (ert-deftest test-org-export/output-file-name ()
  424. "Test `org-export-output-file-name' specifications."
  425. ;; Export from a file: name is built from original file name.
  426. (should
  427. (org-test-with-temp-text-in-file "Test"
  428. (equal (concat (file-name-as-directory ".")
  429. (file-name-nondirectory
  430. (file-name-sans-extension (buffer-file-name))))
  431. (file-name-sans-extension (org-export-output-file-name ".ext")))))
  432. ;; When exporting to subtree, check EXPORT_FILE_NAME property first.
  433. (should
  434. (org-test-with-temp-text-in-file
  435. "* Test\n :PROPERTIES:\n :EXPORT_FILE_NAME: test\n :END:"
  436. (equal (org-export-output-file-name ".ext" t) "./test.ext")))
  437. ;; From a buffer not associated to a file, too.
  438. (should
  439. (org-test-with-temp-text
  440. "* Test\n :PROPERTIES:\n :EXPORT_FILE_NAME: test\n :END:"
  441. (equal (org-export-output-file-name ".ext" t) "./test.ext")))
  442. ;; When provided name is absolute, preserve it.
  443. (should
  444. (org-test-with-temp-text
  445. (format "* Test\n :PROPERTIES:\n :EXPORT_FILE_NAME: %s\n :END:"
  446. (expand-file-name "test"))
  447. (file-name-absolute-p (org-export-output-file-name ".ext" t))))
  448. ;; When PUB-DIR argument is provided, use it.
  449. (should
  450. (org-test-with-temp-text-in-file "Test"
  451. (equal (file-name-directory
  452. (org-export-output-file-name ".ext" nil "dir/"))
  453. "dir/")))
  454. ;; When returned name would overwrite original file, add EXTENSION
  455. ;; another time.
  456. (should
  457. (org-test-at-id "75282ba2-f77a-4309-a970-e87c149fe125"
  458. (equal (org-export-output-file-name ".org") "./normal.org.org"))))
  459. (ert-deftest test-org-export/expand-include ()
  460. "Test file inclusion in an Org buffer."
  461. ;; Error when file isn't specified.
  462. (should-error
  463. (org-test-with-temp-text "#+INCLUDE: dummy.org"
  464. (org-export-expand-include-keyword)))
  465. ;; Full insertion with recursive inclusion.
  466. (org-test-with-temp-text
  467. (format "#+INCLUDE: \"%s/examples/include.org\"" org-test-dir)
  468. (org-export-expand-include-keyword)
  469. (should (equal (buffer-string)
  470. "Small Org file with an include keyword.
  471. #+BEGIN_SRC emacs-lisp :exports results\n(+ 2 1)\n#+END_SRC
  472. Success!
  473. * Heading
  474. body\n")))
  475. ;; Localized insertion.
  476. (org-test-with-temp-text
  477. (format "#+INCLUDE: \"%s/examples/include.org\" :lines \"1-2\""
  478. org-test-dir)
  479. (org-export-expand-include-keyword)
  480. (should (equal (buffer-string)
  481. "Small Org file with an include keyword.\n")))
  482. ;; Insertion with constraints on headlines level.
  483. (should
  484. (equal
  485. "* Top heading\n** Heading\nbody\n"
  486. (org-test-with-temp-text
  487. (format
  488. "* Top heading\n#+INCLUDE: \"%s/examples/include.org\" :lines \"9-\""
  489. org-test-dir)
  490. (org-export-expand-include-keyword)
  491. (buffer-string))))
  492. (should
  493. (equal
  494. "* Top heading\n* Heading\nbody\n"
  495. (org-test-with-temp-text
  496. (format
  497. "* Top heading\n#+INCLUDE: \"%s/examples/include.org\" :lines \"9-\" :minlevel 1"
  498. org-test-dir)
  499. (org-export-expand-include-keyword)
  500. (buffer-string))))
  501. ;; Inclusion within an example block.
  502. (org-test-with-temp-text
  503. (format "#+INCLUDE: \"%s/examples/include.org\" :lines \"1-2\" example"
  504. org-test-dir)
  505. (org-export-expand-include-keyword)
  506. (should
  507. (equal
  508. (buffer-string)
  509. "#+BEGIN_EXAMPLE\nSmall Org file with an include keyword.\n#+END_EXAMPLE\n")))
  510. ;; Inclusion within a src-block.
  511. (org-test-with-temp-text
  512. (format
  513. "#+INCLUDE: \"%s/examples/include.org\" :lines \"4-5\" src emacs-lisp"
  514. org-test-dir)
  515. (org-export-expand-include-keyword)
  516. (should (equal (buffer-string)
  517. "#+BEGIN_SRC emacs-lisp\n(+ 2 1)\n#+END_SRC\n"))))
  518. (ert-deftest test-org-export/expand-macro ()
  519. "Test macro expansion in an Org buffer."
  520. ;; Standard macro expansion.
  521. (should
  522. (equal "#+MACRO: macro1 value\nvalue\n"
  523. (org-test-with-temp-text "#+MACRO: macro1 value\n{{{macro1}}}"
  524. (org-test-with-backend test (org-export-as 'test)))))
  525. ;; Expand specific macros.
  526. (should
  527. (equal "me 2012-03-29 me@here Title\n"
  528. (org-test-with-temp-text
  529. "
  530. #+TITLE: Title
  531. #+DATE: 2012-03-29
  532. #+AUTHOR: me
  533. #+EMAIL: me@here
  534. {{{author}}} {{{date}}} {{{email}}} {{{title}}}"
  535. (let ((output (org-test-with-backend test (org-export-as 'test))))
  536. (substring output (string-match ".*\n\\'" output))))))
  537. ;; Expand specific macros when property contained a regular macro
  538. ;; already.
  539. (should
  540. (equal "value\n"
  541. (org-test-with-temp-text "
  542. #+MACRO: macro1 value
  543. #+TITLE: {{{macro1}}}
  544. {{{title}}}"
  545. (let ((output (org-test-with-backend test (org-export-as 'test))))
  546. (substring output (string-match ".*\n\\'" output))))))
  547. ;; Expand macros with templates in included files.
  548. (should
  549. (equal "success\n"
  550. (org-test-with-temp-text
  551. (format "#+INCLUDE: \"%s/examples/macro-templates.org\"
  552. {{{included-macro}}}" org-test-dir)
  553. (let ((output (org-test-with-backend test (org-export-as 'test))))
  554. (substring output (string-match ".*\n\\'" output)))))))
  555. (ert-deftest test-org-export/user-ignore-list ()
  556. "Test if `:ignore-list' accepts user input."
  557. (org-test-with-backend test
  558. (flet ((skip-note-head
  559. (data backend info)
  560. ;; Ignore headlines with the word "note" in their title.
  561. (org-element-map data 'headline
  562. (lambda (headline)
  563. (when (string-match "\\<note\\>"
  564. (org-element-property :raw-value headline))
  565. (org-export-ignore-element headline info)))
  566. info)
  567. data))
  568. ;; Install function in parse tree filters.
  569. (let ((org-export-filter-parse-tree-functions '(skip-note-head)))
  570. (org-test-with-temp-text "* Head1\n* Head2 (note)\n"
  571. (should (equal (org-export-as 'test) "* Head1\n")))))))
  572. (ert-deftest test-org-export/before-processing-hook ()
  573. "Test `org-export-before-processing-hook'."
  574. (should
  575. (equal
  576. "#+MACRO: mac val\nTest\n"
  577. (org-test-with-backend test
  578. (org-test-with-temp-text "#+MACRO: mac val\n{{{mac}}} Test"
  579. (let ((org-export-before-processing-hook
  580. '((lambda (backend)
  581. (while (re-search-forward "{{{" nil t)
  582. (let ((object (org-element-context)))
  583. (when (eq (org-element-type object) 'macro)
  584. (delete-region
  585. (org-element-property :begin object)
  586. (org-element-property :end object)))))))))
  587. (org-export-as 'test)))))))
  588. (ert-deftest test-org-export/before-parsing-hook ()
  589. "Test `org-export-before-parsing-hook'."
  590. (should
  591. (equal "Body 1\nBody 2\n"
  592. (org-test-with-backend test
  593. (org-test-with-temp-text "* Headline 1\nBody 1\n* Headline 2\nBody 2"
  594. (let ((org-export-before-parsing-hook
  595. '((lambda (backend)
  596. (goto-char (point-min))
  597. (while (re-search-forward org-outline-regexp-bol nil t)
  598. (delete-region
  599. (point-at-bol) (progn (forward-line) (point))))))))
  600. (org-export-as 'test)))))))
  601. ;;; Affiliated Keywords
  602. (ert-deftest test-org-export/read-attribute ()
  603. "Test `org-export-read-attribute' specifications."
  604. ;; Standard test.
  605. (should
  606. (equal
  607. (org-export-read-attribute
  608. :attr_html
  609. (org-test-with-temp-text "#+ATTR_HTML: :a 1 :b 2\nParagraph"
  610. (org-element-at-point)))
  611. '(:a "1" :b "2")))
  612. ;; Return nil on empty attribute.
  613. (should-not
  614. (org-export-read-attribute
  615. :attr_html
  616. (org-test-with-temp-text "Paragraph" (org-element-at-point))))
  617. ;; Return nil on "nil" string.
  618. (should
  619. (equal '(:a nil :b nil)
  620. (org-export-read-attribute
  621. :attr_html
  622. (org-test-with-temp-text "#+ATTR_HTML: :a nil :b nil\nParagraph"
  623. (org-element-at-point)))))
  624. ;; Return nil on empty string.
  625. (should
  626. (equal '(:a nil :b nil)
  627. (org-export-read-attribute
  628. :attr_html
  629. (org-test-with-temp-text "#+ATTR_HTML: :a :b\nParagraph"
  630. (org-element-at-point)))))
  631. ;; Ignore text before first property.
  632. (should-not
  633. (member "ignore"
  634. (org-export-read-attribute
  635. :attr_html
  636. (org-test-with-temp-text "#+ATTR_HTML: ignore :a 1\nParagraph"
  637. (org-element-at-point))))))
  638. (ert-deftest test-org-export/get-caption ()
  639. "Test `org-export-get-caption' specifications."
  640. ;; Without optional argument, return long caption
  641. (should
  642. (equal
  643. '("l")
  644. (org-test-with-temp-text "#+CAPTION[s]: l\nPara"
  645. (org-export-get-caption (org-element-at-point)))))
  646. ;; With optional argument, return short caption.
  647. (should
  648. (equal
  649. '("s")
  650. (org-test-with-temp-text "#+CAPTION[s]: l\nPara"
  651. (org-export-get-caption (org-element-at-point) t))))
  652. ;; Multiple lines are separated by white spaces.
  653. (should
  654. (equal
  655. '("a" " " "b")
  656. (org-test-with-temp-text "#+CAPTION: a\n#+CAPTION: b\nPara"
  657. (org-export-get-caption (org-element-at-point))))))
  658. ;;; Back-End Tools
  659. (ert-deftest test-org-export/define-backend ()
  660. "Test back-end definition and accessors."
  661. ;; Translate table.
  662. (should
  663. (equal '((headline . my-headline-test))
  664. (let (org-export-registered-backends)
  665. (org-export-define-backend 'test '((headline . my-headline-test)))
  666. (org-export-backend-translate-table 'test))))
  667. ;; Filters.
  668. (should
  669. (equal '((:filter-headline . my-filter))
  670. (let (org-export-registered-backends)
  671. (org-export-define-backend 'test
  672. '((headline . my-headline-test))
  673. :filters-alist '((:filter-headline . my-filter)))
  674. (org-export-backend-filters 'test))))
  675. ;; Options.
  676. (should
  677. (equal '((:prop value))
  678. (let (org-export-registered-backends)
  679. (org-export-define-backend 'test
  680. '((headline . my-headline-test))
  681. :options-alist '((:prop value)))
  682. (org-export-backend-options 'test))))
  683. ;; Menu.
  684. (should
  685. (equal '(?k "Test Export" test)
  686. (let (org-export-registered-backends)
  687. (org-export-define-backend 'test
  688. '((headline . my-headline-test))
  689. :menu-entry '(?k "Test Export" test))
  690. (org-export-backend-menu 'test))))
  691. ;; Export Blocks.
  692. (should
  693. (equal '(("TEST" . org-element-export-block-parser))
  694. (let (org-export-registered-backends org-element-block-name-alist)
  695. (org-export-define-backend 'test
  696. '((headline . my-headline-test))
  697. :export-block '("test"))
  698. org-element-block-name-alist))))
  699. (ert-deftest test-org-export/define-derived-backend ()
  700. "Test `org-export-define-derived-backend' specifications."
  701. ;; Error when parent back-end is not defined.
  702. (should-error
  703. (let (org-export-registered-backends)
  704. (org-export-define-derived-backend 'test 'parent)))
  705. ;; Append translation table to parent's.
  706. (should
  707. (equal '((:headline . test) (:headline . parent))
  708. (let (org-export-registered-backends)
  709. (org-export-define-backend 'parent '((:headline . parent)))
  710. (org-export-define-derived-backend 'test 'parent
  711. :translate-alist '((:headline . test)))
  712. (org-export-backend-translate-table 'test))))
  713. ;; Options defined in the new back have priority over those defined
  714. ;; in parent.
  715. (should
  716. (eq 'test
  717. (let (org-export-registered-backends)
  718. (org-export-define-backend 'parent
  719. '((:headline . parent))
  720. :options-alist '((:a nil nil 'parent)))
  721. (org-export-define-derived-backend 'test 'parent
  722. :options-alist '((:a nil nil 'test)))
  723. (plist-get (org-export--get-global-options 'test) :a)))))
  724. (ert-deftest test-org-export/derived-backend-p ()
  725. "Test `org-export-derived-backend-p' specifications."
  726. ;; Non-nil with direct match.
  727. (should
  728. (let (org-export-registered-backends)
  729. (org-export-define-backend 'test '((headline . test)))
  730. (org-export-derived-backend-p 'test 'test)))
  731. (should
  732. (let (org-export-registered-backends)
  733. (org-export-define-backend 'test '((headline . test)))
  734. (org-export-define-derived-backend 'test2 'test)
  735. (org-export-derived-backend-p 'test2 'test2)))
  736. ;; Non-nil with a direct parent.
  737. (should
  738. (let (org-export-registered-backends)
  739. (org-export-define-backend 'test '((headline . test)))
  740. (org-export-define-derived-backend 'test2 'test)
  741. (org-export-derived-backend-p 'test2 'test)))
  742. ;; Non-nil with an indirect parent.
  743. (should
  744. (let (org-export-registered-backends)
  745. (org-export-define-backend 'test '((headline . test)))
  746. (org-export-define-derived-backend 'test2 'test)
  747. (org-export-define-derived-backend 'test3 'test2)
  748. (org-export-derived-backend-p 'test3 'test)))
  749. ;; Nil otherwise.
  750. (should-not
  751. (let (org-export-registered-backends)
  752. (org-export-define-backend 'test '((headline . test)))
  753. (org-export-define-backend 'test2 '((headline . test2)))
  754. (org-export-derived-backend-p 'test2 'test)))
  755. (should-not
  756. (let (org-export-registered-backends)
  757. (org-export-define-backend 'test '((headline . test)))
  758. (org-export-define-backend 'test2 '((headline . test2)))
  759. (org-export-define-derived-backend 'test3 'test2)
  760. (org-export-derived-backend-p 'test3 'test))))
  761. (ert-deftest test-org-export/with-backend ()
  762. "Test `org-export-with-backend' definition."
  763. ;; Error when calling an undefined back-end
  764. (should-error
  765. (let (org-export-registered-backends)
  766. (org-export-with-backend 'test "Test")))
  767. ;; Error when called back-end doesn't have an appropriate
  768. ;; transcoder.
  769. (should-error
  770. (let (org-export-registered-backends)
  771. (org-export-define-backend 'test ((headline . ignore)))
  772. (org-export-with-backend 'test "Test")))
  773. ;; Otherwise, export using correct transcoder
  774. (should
  775. (equal "Success"
  776. (let (org-export-registered-backends)
  777. (org-export-define-backend 'test
  778. '((plain-text . (lambda (text contents info) "Failure"))))
  779. (org-export-define-backend 'test2
  780. '((plain-text . (lambda (text contents info) "Success"))))
  781. (org-export-with-backend 'test2 "Test")))))
  782. (ert-deftest test-org-export/data-with-translations ()
  783. "Test `org-export-data-with-translations' specifications."
  784. (should
  785. (equal
  786. "Success!"
  787. (org-export-data-with-translations
  788. '(bold nil "Test")
  789. '((plain-text . (lambda (text info) "Success"))
  790. (bold . (lambda (bold contents info) (concat contents "!"))))
  791. '(:with-emphasize t)))))
  792. (ert-deftest test-org-export/data-with-backend ()
  793. "Test `org-export-data-with-backend' specifications."
  794. ;; Error when calling an undefined back-end.
  795. (should-error
  796. (let (org-export-registered-backends)
  797. (org-export-data-with-backend 'test "Test" nil)))
  798. ;; Otherwise, export data recursively, using correct back-end.
  799. (should
  800. (equal
  801. "Success!"
  802. (let (org-export-registered-backends)
  803. (org-export-define-backend 'test
  804. '((plain-text . (lambda (text info) "Success"))
  805. (bold . (lambda (bold contents info) (concat contents "!")))))
  806. (org-export-data-with-backend
  807. '(bold nil "Test") 'test '(:with-emphasize t))))))
  808. ;;; Export Snippets
  809. (ert-deftest test-org-export/export-snippet ()
  810. "Test export snippets transcoding."
  811. (org-test-with-temp-text "@@test:A@@@@t:B@@"
  812. (org-test-with-backend test
  813. (plist-put
  814. (cdr (assq 'test org-export-registered-backends))
  815. :translate-alist
  816. (cons (cons 'export-snippet
  817. (lambda (snippet contents info)
  818. (when (eq (org-export-snippet-backend snippet) 'test)
  819. (org-element-property :value snippet))))
  820. (org-export-backend-translate-table 'test)))
  821. (let ((org-export-snippet-translation-alist nil))
  822. (should (equal (org-export-as 'test) "A\n")))
  823. (let ((org-export-snippet-translation-alist '(("t" . "test"))))
  824. (should (equal (org-export-as 'test) "AB\n"))))))
  825. ;;; Footnotes
  826. (ert-deftest test-org-export/footnotes ()
  827. "Test footnotes specifications."
  828. (let ((org-footnote-section nil)
  829. (org-export-with-footnotes t))
  830. ;; 1. Read every type of footnote.
  831. (should
  832. (equal
  833. '((1 . "A\n") (2 . "B") (3 . "C") (4 . "D"))
  834. (org-test-with-parsed-data
  835. "Text[fn:1] [1] [fn:label:C] [fn::D]\n\n[fn:1] A\n\n[1] B"
  836. (org-element-map tree 'footnote-reference
  837. (lambda (ref)
  838. (let ((def (org-export-get-footnote-definition ref info)))
  839. (cons (org-export-get-footnote-number ref info)
  840. (if (eq (org-element-property :type ref) 'inline) (car def)
  841. (car (org-element-contents
  842. (car (org-element-contents def))))))))
  843. info))))
  844. ;; 2. Test nested footnotes order.
  845. (org-test-with-parsed-data
  846. "Text[fn:1:A[fn:2]] [fn:3].\n\n[fn:2] B [fn:3] [fn::D].\n\n[fn:3] C."
  847. (should
  848. (equal
  849. '((1 . "fn:1") (2 . "fn:2") (3 . "fn:3") (4))
  850. (org-element-map tree 'footnote-reference
  851. (lambda (ref)
  852. (when (org-export-footnote-first-reference-p ref info)
  853. (cons (org-export-get-footnote-number ref info)
  854. (org-element-property :label ref))))
  855. info))))
  856. ;; 3. Test nested footnote in invisible definitions.
  857. (org-test-with-temp-text "Text[1]\n\n[1] B [2]\n\n[2] C."
  858. ;; Hide definitions.
  859. (narrow-to-region (point) (point-at-eol))
  860. (let* ((tree (org-element-parse-buffer))
  861. (info (org-combine-plists
  862. `(:parse-tree ,tree)
  863. (org-export-collect-tree-properties
  864. tree (org-export-get-environment)))))
  865. ;; Both footnotes should be seen.
  866. (should
  867. (= (length (org-export-collect-footnote-definitions tree info)) 2))))
  868. ;; 4. Test footnotes definitions collection.
  869. (org-test-with-parsed-data "Text[fn:1:A[fn:2]] [fn:3].
  870. \[fn:2] B [fn:3] [fn::D].
  871. \[fn:3] C."
  872. (should (= (length (org-export-collect-footnote-definitions tree info))
  873. 4)))
  874. ;; 5. Test export of footnotes defined outside parsing scope.
  875. (org-test-with-temp-text "[fn:1] Out of scope
  876. * Title
  877. Paragraph[fn:1]"
  878. (org-test-with-backend test
  879. (plist-put
  880. (cdr (assq 'test org-export-registered-backends))
  881. :translate-alist
  882. (cons (cons 'footnote-reference
  883. (lambda (fn contents info)
  884. (org-element-interpret-data
  885. (org-export-get-footnote-definition fn info))))
  886. (org-export-backend-translate-table 'test)))
  887. (forward-line)
  888. (should (equal "ParagraphOut of scope\n"
  889. (org-export-as 'test 'subtree)))))
  890. ;; 6. Footnotes without a definition should be provided a fallback
  891. ;; definition.
  892. (should
  893. (org-test-with-parsed-data "[fn:1]"
  894. (org-export-get-footnote-definition
  895. (org-element-map tree 'footnote-reference 'identity info t) info)))
  896. ;; 7. Footnote section should be ignored in TOC and in headlines
  897. ;; numbering.
  898. (should
  899. (= 1 (let ((org-footnote-section "Footnotes"))
  900. (length (org-test-with-parsed-data "* H1\n* Footnotes\n"
  901. (org-export-collect-headlines info))))))
  902. (should
  903. (equal '(2)
  904. (let ((org-footnote-section "Footnotes"))
  905. (org-test-with-parsed-data "* H1\n* Footnotes\n* H2"
  906. (org-element-map tree 'headline
  907. (lambda (hl)
  908. (when (equal (org-element-property :raw-value hl) "H2")
  909. (org-export-get-headline-number hl info)))
  910. info t)))))))
  911. ;;; Headlines and Inlinetasks
  912. (ert-deftest test-org-export/get-relative-level ()
  913. "Test `org-export-get-relative-level' specifications."
  914. ;; Standard test.
  915. (should
  916. (equal '(1 2)
  917. (let ((org-odd-levels-only nil))
  918. (org-test-with-parsed-data "* Headline 1\n** Headline 2"
  919. (org-element-map tree 'headline
  920. (lambda (h) (org-export-get-relative-level h info))
  921. info)))))
  922. ;; Missing levels
  923. (should
  924. (equal '(1 3)
  925. (let ((org-odd-levels-only nil))
  926. (org-test-with-parsed-data "** Headline 1\n**** Headline 2"
  927. (org-element-map tree 'headline
  928. (lambda (h) (org-export-get-relative-level h info))
  929. info))))))
  930. (ert-deftest test-org-export/low-level-p ()
  931. "Test `org-export-low-level-p' specifications."
  932. (should
  933. (equal
  934. '(no yes)
  935. (let ((org-odd-levels-only nil))
  936. (org-test-with-parsed-data "* Headline 1\n** Headline 2"
  937. (org-element-map tree 'headline
  938. (lambda (h) (if (org-export-low-level-p h info) 'yes 'no))
  939. (plist-put info :headline-levels 1)))))))
  940. (ert-deftest test-org-export/get-headline-number ()
  941. "Test `org-export-get-headline-number' specifications."
  942. ;; Standard test.
  943. (should
  944. (equal
  945. '((1) (1 1))
  946. (let ((org-odd-levels-only nil))
  947. (org-test-with-parsed-data "* Headline 1\n** Headline 2"
  948. (org-element-map tree 'headline
  949. (lambda (h) (org-export-get-headline-number h info))
  950. info)))))
  951. ;; Missing levels are replaced with 0.
  952. (should
  953. (equal
  954. '((1) (1 0 1))
  955. (let ((org-odd-levels-only nil))
  956. (org-test-with-parsed-data "* Headline 1\n*** Headline 2"
  957. (org-element-map tree 'headline
  958. (lambda (h) (org-export-get-headline-number h info))
  959. info))))))
  960. (ert-deftest test-org-export/numbered-headline-p ()
  961. "Test `org-export-numbered-headline-p' specifications."
  962. ;; If `:section-numbers' is nil, never number headlines.
  963. (should-not
  964. (org-test-with-parsed-data "* Headline"
  965. (org-element-map tree 'headline
  966. (lambda (h) (org-export-numbered-headline-p h info))
  967. (plist-put info :section-numbers nil))))
  968. ;; If `:section-numbers' is a number, only number headlines with
  969. ;; a level greater that it.
  970. (should
  971. (equal
  972. '(yes no)
  973. (org-test-with-parsed-data "* Headline 1\n** Headline 2"
  974. (org-element-map tree 'headline
  975. (lambda (h) (if (org-export-numbered-headline-p h info) 'yes 'no))
  976. (plist-put info :section-numbers 1)))))
  977. ;; Otherwise, headlines are always numbered.
  978. (should
  979. (org-test-with-parsed-data "* Headline"
  980. (org-element-map tree 'headline
  981. (lambda (h) (org-export-numbered-headline-p h info))
  982. (plist-put info :section-numbers t)))))
  983. (ert-deftest test-org-export/number-to-roman ()
  984. "Test `org-export-number-to-roman' specifications."
  985. ;; If number is negative, return it as a string.
  986. (should (equal (org-export-number-to-roman -1) "-1"))
  987. ;; Otherwise, return it as a roman number.
  988. (should (equal (org-export-number-to-roman 1449) "MCDXLIX")))
  989. (ert-deftest test-org-export/get-optional-title ()
  990. "Test `org-export-get-alt-title' specifications."
  991. ;; If ALT_TITLE property is defined, use it.
  992. (should
  993. (equal '("opt")
  994. (org-test-with-parsed-data
  995. "* Headline\n:PROPERTIES:\n:ALT_TITLE: opt\n:END:"
  996. (org-export-get-alt-title
  997. (org-element-map tree 'headline 'identity info t)
  998. info))))
  999. ;; Otherwise, fall-back to regular title.
  1000. (should
  1001. (equal '("Headline")
  1002. (org-test-with-parsed-data "* Headline"
  1003. (org-export-get-alt-title
  1004. (org-element-map tree 'headline 'identity info t)
  1005. info)))))
  1006. (ert-deftest test-org-export/get-tags ()
  1007. "Test `org-export-get-tags' specifications."
  1008. (let ((org-export-exclude-tags '("noexport"))
  1009. (org-export-select-tags '("export")))
  1010. ;; Standard test: tags which are not a select tag, an exclude tag,
  1011. ;; or specified as optional argument shouldn't be ignored.
  1012. (should
  1013. (org-test-with-parsed-data "* Headline :tag:"
  1014. (org-export-get-tags (org-element-map tree 'headline 'identity info t)
  1015. info)))
  1016. ;; Exclude tags are removed.
  1017. (should-not
  1018. (org-test-with-parsed-data "* Headline :noexport:"
  1019. (org-export-get-tags (org-element-map tree 'headline 'identity info t)
  1020. info)))
  1021. ;; Select tags are removed.
  1022. (should-not
  1023. (org-test-with-parsed-data "* Headline :export:"
  1024. (org-export-get-tags (org-element-map tree 'headline 'identity info t)
  1025. info)))
  1026. (should
  1027. (equal
  1028. '("tag")
  1029. (org-test-with-parsed-data "* Headline :tag:export:"
  1030. (org-export-get-tags (org-element-map tree 'headline 'identity info t)
  1031. info))))
  1032. ;; Tags provided in the optional argument are also ignored.
  1033. (should-not
  1034. (org-test-with-parsed-data "* Headline :ignore:"
  1035. (org-export-get-tags (org-element-map tree 'headline 'identity info t)
  1036. info '("ignore"))))
  1037. ;; Allow tag inheritance.
  1038. (should
  1039. (equal
  1040. '(("tag") ("tag"))
  1041. (org-test-with-parsed-data "* Headline :tag:\n** Sub-heading"
  1042. (org-element-map tree 'headline
  1043. (lambda (hl) (org-export-get-tags hl info nil t)) info))))
  1044. ;; Tag inheritance checks FILETAGS keywords.
  1045. (should
  1046. (equal
  1047. '(("a" "b" "tag"))
  1048. (org-test-with-parsed-data "#+FILETAGS: :a:b:\n* Headline :tag:"
  1049. (org-element-map tree 'headline
  1050. (lambda (hl) (org-export-get-tags hl info nil t)) info))))))
  1051. (ert-deftest test-org-export/get-node-property ()
  1052. "Test`org-export-get-node-property' specifications."
  1053. ;; Standard test.
  1054. (should
  1055. (equal "value"
  1056. (org-test-with-parsed-data "* Headline
  1057. :PROPERTIES:
  1058. :prop: value
  1059. :END:"
  1060. (org-export-get-node-property
  1061. :PROP (org-element-map tree 'headline 'identity nil t)))))
  1062. ;; Test inheritance.
  1063. (should
  1064. (equal "value"
  1065. (org-test-with-parsed-data "* Parent
  1066. :PROPERTIES:
  1067. :prop: value
  1068. :END:
  1069. ** Headline
  1070. Paragraph"
  1071. (org-export-get-node-property
  1072. :PROP (org-element-map tree 'paragraph 'identity nil t) t))))
  1073. ;; Cannot return a value before the first headline.
  1074. (should-not
  1075. (org-test-with-parsed-data "Paragraph
  1076. * Headline
  1077. :PROPERTIES:
  1078. :prop: value
  1079. :END:"
  1080. (org-export-get-node-property
  1081. :PROP (org-element-map tree 'paragraph 'identity nil t)))))
  1082. (ert-deftest test-org-export/get-category ()
  1083. "Test `org-export-get-category' specifications."
  1084. ;; Standard test.
  1085. (should
  1086. (equal "value"
  1087. (org-test-with-parsed-data "* Headline
  1088. :PROPERTIES:
  1089. :CATEGORY: value
  1090. :END:"
  1091. (org-export-get-category
  1092. (org-element-map tree 'headline 'identity nil t) info))))
  1093. ;; Test inheritance from a parent headline.
  1094. (should
  1095. (equal '("value" "value")
  1096. (org-test-with-parsed-data "* Headline1
  1097. :PROPERTIES:
  1098. :CATEGORY: value
  1099. :END:
  1100. ** Headline2"
  1101. (org-element-map tree 'headline
  1102. (lambda (hl) (org-export-get-category hl info)) info))))
  1103. ;; Test inheritance from #+CATEGORY keyword
  1104. (should
  1105. (equal "value"
  1106. (org-test-with-parsed-data "#+CATEGORY: value
  1107. * Headline"
  1108. (org-export-get-category
  1109. (org-element-map tree 'headline 'identity nil t) info))))
  1110. ;; Test inheritance from file name.
  1111. (should
  1112. (equal "test"
  1113. (org-test-with-parsed-data "* Headline"
  1114. (let ((info (plist-put info :input-file "~/test.org")))
  1115. (org-export-get-category
  1116. (org-element-map tree 'headline 'identity nil t) info)))))
  1117. ;; Fall-back value.
  1118. (should
  1119. (equal "???"
  1120. (org-test-with-parsed-data "* Headline"
  1121. (org-export-get-category
  1122. (org-element-map tree 'headline 'identity nil t) info)))))
  1123. (ert-deftest test-org-export/first-sibling-p ()
  1124. "Test `org-export-first-sibling-p' specifications."
  1125. ;; Standard test.
  1126. (should
  1127. (equal
  1128. '(yes yes no)
  1129. (org-test-with-parsed-data "* Headline\n** Headline 2\n** Headline 3"
  1130. (org-element-map tree 'headline
  1131. (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
  1132. info))))
  1133. ;; Ignore headlines not exported.
  1134. (should
  1135. (equal
  1136. '(yes)
  1137. (let ((org-export-exclude-tags '("ignore")))
  1138. (org-test-with-parsed-data "* Headline :ignore:\n* Headline 2"
  1139. (org-element-map tree 'headline
  1140. (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
  1141. info))))))
  1142. (ert-deftest test-org-export/last-sibling-p ()
  1143. "Test `org-export-last-sibling-p' specifications."
  1144. ;; Standard test.
  1145. (should
  1146. (equal
  1147. '(yes no yes)
  1148. (org-test-with-parsed-data "* Headline\n** Headline 2\n** Headline 3"
  1149. (org-element-map tree 'headline
  1150. (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
  1151. info))))
  1152. ;; Ignore headlines not exported.
  1153. (should
  1154. (equal
  1155. '(yes)
  1156. (let ((org-export-exclude-tags '("ignore")))
  1157. (org-test-with-parsed-data "* Headline\n* Headline 2 :ignore:"
  1158. (org-element-map tree 'headline
  1159. (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
  1160. info))))))
  1161. (ert-deftest test-org-export/handle-inlinetasks ()
  1162. "Test inlinetask export."
  1163. ;; Inlinetask with an exclude tag.
  1164. (when (featurep 'org-inlinetask)
  1165. (should
  1166. (equal
  1167. ""
  1168. (let ((org-inlinetask-min-level 3))
  1169. (org-test-with-temp-text "*** Inlinetask :noexp:\nContents\n*** end"
  1170. (org-test-with-backend test
  1171. (org-export-as 'test nil nil nil '(:exclude-tags ("noexp"))))))))
  1172. ;; Inlinetask with an include tag.
  1173. (should
  1174. (equal
  1175. "* H2\n*** Inline :exp:\n"
  1176. (let ((org-inlinetask-min-level 3)
  1177. (org-tags-column 0))
  1178. (org-test-with-temp-text "* H1\n* H2\n*** Inline :exp:"
  1179. (org-test-with-backend test
  1180. (org-export-as 'test nil nil nil '(:select-tags ("exp"))))))))
  1181. ;; Ignore inlinetask with a TODO keyword and tasks excluded.
  1182. (should
  1183. (equal ""
  1184. (let ((org-todo-keywords '((sequence "TODO" "DONE")))
  1185. (org-inlinetask-min-level 3))
  1186. (org-test-with-temp-text "*** TODO Inline"
  1187. (org-test-with-backend test
  1188. (org-export-as 'test nil nil nil '(:with-tasks nil)))))))))
  1189. ;;; Links
  1190. (ert-deftest test-org-export/get-coderef-format ()
  1191. "Test `org-export-get-coderef-format' specifications."
  1192. ;; A link without description returns "%s"
  1193. (should (equal (org-export-get-coderef-format "(ref:line)" nil)
  1194. "%s"))
  1195. ;; Return "%s" when path is matched within description.
  1196. (should (equal (org-export-get-coderef-format "path" "desc (path)")
  1197. "desc %s"))
  1198. ;; Otherwise return description.
  1199. (should (equal (org-export-get-coderef-format "path" "desc")
  1200. "desc")))
  1201. (ert-deftest test-org-export/inline-image-p ()
  1202. "Test `org-export-inline-image-p' specifications."
  1203. (should
  1204. (org-export-inline-image-p
  1205. (org-test-with-temp-text "[[#id]]"
  1206. (org-element-map (org-element-parse-buffer) 'link 'identity nil t))
  1207. '(("custom-id" . "id")))))
  1208. (ert-deftest test-org-export/fuzzy-link ()
  1209. "Test fuzzy links specifications."
  1210. ;; 1. Links to invisible (keyword) targets should be ignored.
  1211. (org-test-with-parsed-data
  1212. "Paragraph.\n#+TARGET: Test\n[[Test]]"
  1213. (should-not
  1214. (org-element-map tree 'link
  1215. (lambda (link)
  1216. (org-export-get-ordinal
  1217. (org-export-resolve-fuzzy-link link info) info)) info)))
  1218. ;; 2. Link to an headline should return headline's number.
  1219. (org-test-with-parsed-data
  1220. "Paragraph.\n* Head1\n* Head2\n* Head3\n[[Head2]]"
  1221. (should
  1222. ;; Note: Headline's number is in fact a list of numbers.
  1223. (equal '(2)
  1224. (org-element-map tree 'link
  1225. (lambda (link)
  1226. (org-export-get-ordinal
  1227. (org-export-resolve-fuzzy-link link info) info)) info t))))
  1228. ;; 3. Link to a target in an item should return item's number.
  1229. (org-test-with-parsed-data
  1230. "- Item1\n - Item11\n - <<test>>Item12\n- Item2\n\n\n[[test]]"
  1231. (should
  1232. ;; Note: Item's number is in fact a list of numbers.
  1233. (equal '(1 2)
  1234. (org-element-map tree 'link
  1235. (lambda (link)
  1236. (org-export-get-ordinal
  1237. (org-export-resolve-fuzzy-link link info) info)) info t))))
  1238. ;; 4. Link to a target in a footnote should return footnote's
  1239. ;; number.
  1240. (org-test-with-parsed-data "
  1241. Paragraph[1][2][fn:lbl3:C<<target>>][[test]][[target]]\n[1] A\n\n[2] <<test>>B"
  1242. (should
  1243. (equal '(2 3)
  1244. (org-element-map tree 'link
  1245. (lambda (link)
  1246. (org-export-get-ordinal
  1247. (org-export-resolve-fuzzy-link link info) info)) info))))
  1248. ;; 5. Link to a named element should return sequence number of that
  1249. ;; element.
  1250. (org-test-with-parsed-data
  1251. "#+NAME: tbl1\n|1|2|\n#+NAME: tbl2\n|3|4|\n#+NAME: tbl3\n|5|6|\n[[tbl2]]"
  1252. (should
  1253. (= 2
  1254. (org-element-map tree 'link
  1255. (lambda (link)
  1256. (org-export-get-ordinal
  1257. (org-export-resolve-fuzzy-link link info) info)) info t))))
  1258. ;; 6. Link to a target not within an item, a table, a footnote
  1259. ;; reference or definition should return section number.
  1260. (org-test-with-parsed-data
  1261. "* Head1\n* Head2\nParagraph<<target>>\n* Head3\n[[target]]"
  1262. (should
  1263. (equal '(2)
  1264. (org-element-map tree 'link
  1265. (lambda (link)
  1266. (org-export-get-ordinal
  1267. (org-export-resolve-fuzzy-link link info) info)) info t))))
  1268. ;; 7. Space are not significant when matching a fuzzy link.
  1269. (should
  1270. (org-test-with-parsed-data "* Head 1\n[[Head\n 1]]"
  1271. (org-element-map tree 'link
  1272. (lambda (link) (org-export-resolve-fuzzy-link link info))
  1273. info t)))
  1274. ;; 8. Statistics cookies are ignored for headline match.
  1275. (should
  1276. (org-test-with-parsed-data "* Head [0/0]\n[[Head]]"
  1277. (org-element-map tree 'link
  1278. (lambda (link) (org-export-resolve-fuzzy-link link info))
  1279. info t)))
  1280. (should
  1281. (org-test-with-parsed-data "* Head [100%]\n[[Head]]"
  1282. (org-element-map tree 'link
  1283. (lambda (link) (org-export-resolve-fuzzy-link link info))
  1284. info t))))
  1285. (ert-deftest test-org-export/resolve-coderef ()
  1286. "Test `org-export-resolve-coderef' specifications."
  1287. (let ((org-coderef-label-format "(ref:%s)"))
  1288. ;; 1. A link to a "-n -k -r" block returns line number.
  1289. (org-test-with-parsed-data
  1290. "#+BEGIN_EXAMPLE -n -k -r\nText (ref:coderef)\n#+END_EXAMPLE"
  1291. (should (= (org-export-resolve-coderef "coderef" info) 1)))
  1292. (org-test-with-parsed-data
  1293. "#+BEGIN_SRC emacs-lisp -n -k -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  1294. (should (= (org-export-resolve-coderef "coderef" info) 1)))
  1295. ;; 2. A link to a "-n -r" block returns line number.
  1296. (org-test-with-parsed-data
  1297. "#+BEGIN_EXAMPLE -n -r\nText (ref:coderef)\n#+END_EXAMPLE"
  1298. (should (= (org-export-resolve-coderef "coderef" info) 1)))
  1299. (org-test-with-parsed-data
  1300. "#+BEGIN_SRC emacs-lisp -n -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  1301. (should (= (org-export-resolve-coderef "coderef" info) 1)))
  1302. ;; 3. A link to a "-n" block returns coderef.
  1303. (org-test-with-parsed-data
  1304. "#+BEGIN_SRC emacs-lisp -n\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  1305. (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
  1306. (org-test-with-parsed-data
  1307. "#+BEGIN_EXAMPLE -n\nText (ref:coderef)\n#+END_EXAMPLE"
  1308. (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
  1309. ;; 4. A link to a "-r" block returns line number.
  1310. (org-test-with-parsed-data
  1311. "#+BEGIN_SRC emacs-lisp -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  1312. (should (= (org-export-resolve-coderef "coderef" info) 1)))
  1313. (org-test-with-parsed-data
  1314. "#+BEGIN_EXAMPLE -r\nText (ref:coderef)\n#+END_EXAMPLE"
  1315. (should (= (org-export-resolve-coderef "coderef" info) 1)))
  1316. ;; 5. A link to a block without a switch returns coderef.
  1317. (org-test-with-parsed-data
  1318. "#+BEGIN_SRC emacs-lisp\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  1319. (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
  1320. (org-test-with-parsed-data
  1321. "#+BEGIN_EXAMPLE\nText (ref:coderef)\n#+END_EXAMPLE"
  1322. (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
  1323. ;; 6. Correctly handle continued line numbers. A "+n" switch
  1324. ;; should resume numbering from previous block with numbered
  1325. ;; lines, ignoring blocks not numbering lines in the process.
  1326. ;; A "-n" switch resets count.
  1327. (org-test-with-parsed-data "
  1328. #+BEGIN_EXAMPLE -n
  1329. Text.
  1330. #+END_EXAMPLE
  1331. #+BEGIN_SRC emacs-lisp
  1332. \(- 1 1)
  1333. #+END_SRC
  1334. #+BEGIN_SRC emacs-lisp +n -r
  1335. \(+ 1 1) (ref:addition)
  1336. #+END_SRC
  1337. #+BEGIN_EXAMPLE -n -r
  1338. Another text. (ref:text)
  1339. #+END_EXAMPLE"
  1340. (should (= (org-export-resolve-coderef "addition" info) 2))
  1341. (should (= (org-export-resolve-coderef "text" info) 1)))
  1342. ;; 7. Recognize coderef with user-specified syntax.
  1343. (org-test-with-parsed-data
  1344. "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\nText. [ref:text]\n#+END_EXAMPLE"
  1345. (should (equal (org-export-resolve-coderef "text" info) "text")))))
  1346. (ert-deftest test-org-export/resolve-fuzzy-link ()
  1347. "Test `org-export-resolve-fuzzy-link' specifications."
  1348. ;; 1. Match target objects.
  1349. (org-test-with-parsed-data "<<target>> [[target]]"
  1350. (should
  1351. (org-export-resolve-fuzzy-link
  1352. (org-element-map tree 'link 'identity info t) info)))
  1353. ;; 2. Match target elements.
  1354. (org-test-with-parsed-data "#+TARGET: target\n[[target]]"
  1355. (should
  1356. (org-export-resolve-fuzzy-link
  1357. (org-element-map tree 'link 'identity info t) info)))
  1358. ;; 3. Match named elements.
  1359. (org-test-with-parsed-data "#+NAME: target\nParagraph\n\n[[target]]"
  1360. (should
  1361. (org-export-resolve-fuzzy-link
  1362. (org-element-map tree 'link 'identity info t) info)))
  1363. ;; 4. Match exact headline's name.
  1364. (org-test-with-parsed-data "* My headline\n[[My headline]]"
  1365. (should
  1366. (org-export-resolve-fuzzy-link
  1367. (org-element-map tree 'link 'identity info t) info)))
  1368. ;; 5. Targets objects have priority over named elements and headline
  1369. ;; titles.
  1370. (org-test-with-parsed-data
  1371. "* target\n#+NAME: target\n<<target>>\n\n[[target]]"
  1372. (should
  1373. (eq 'target
  1374. (org-element-type
  1375. (org-export-resolve-fuzzy-link
  1376. (org-element-map tree 'link 'identity info t) info)))))
  1377. ;; 6. Named elements have priority over headline titles.
  1378. (org-test-with-parsed-data
  1379. "* target\n#+NAME: target\nParagraph\n\n[[target]]"
  1380. (should
  1381. (eq 'paragraph
  1382. (org-element-type
  1383. (org-export-resolve-fuzzy-link
  1384. (org-element-map tree 'link 'identity info t) info)))))
  1385. ;; 7. If link's path starts with a "*", only match headline titles,
  1386. ;; though.
  1387. (org-test-with-parsed-data
  1388. "* target\n#+NAME: target\n<<target>>\n\n[[*target]]"
  1389. (should
  1390. (eq 'headline
  1391. (org-element-type
  1392. (org-export-resolve-fuzzy-link
  1393. (org-element-map tree 'link 'identity info t) info)))))
  1394. ;; 8. Return nil if no match.
  1395. (org-test-with-parsed-data "[[target]]"
  1396. (should-not
  1397. (org-export-resolve-fuzzy-link
  1398. (org-element-map tree 'link 'identity info t) info))))
  1399. (ert-deftest test-org-export/resolve-id-link ()
  1400. "Test `org-export-resolve-id-link' specifications."
  1401. ;; 1. Regular test for custom-id link.
  1402. (org-test-with-parsed-data "* Headline1
  1403. :PROPERTIES:
  1404. :CUSTOM_ID: test
  1405. :END:
  1406. * Headline 2
  1407. \[[#test]]"
  1408. (should
  1409. (org-export-resolve-id-link
  1410. (org-element-map tree 'link 'identity info t) info)))
  1411. ;; 2. Failing test for custom-id link.
  1412. (org-test-with-parsed-data "* Headline1
  1413. :PROPERTIES:
  1414. :CUSTOM_ID: test
  1415. :END:
  1416. * Headline 2
  1417. \[[#no-match]]"
  1418. (should-not
  1419. (org-export-resolve-id-link
  1420. (org-element-map tree 'link 'identity info t) info)))
  1421. ;; 3. Test for internal id target.
  1422. (org-test-with-parsed-data "* Headline1
  1423. :PROPERTIES:
  1424. :ID: aaaa
  1425. :END:
  1426. * Headline 2
  1427. \[[id:aaaa]]"
  1428. (should
  1429. (org-export-resolve-id-link
  1430. (org-element-map tree 'link 'identity info t) info)))
  1431. ;; 4. Test for external id target.
  1432. (org-test-with-parsed-data "[[id:aaaa]]"
  1433. (should
  1434. (org-export-resolve-id-link
  1435. (org-element-map tree 'link 'identity info t)
  1436. (org-combine-plists info '(:id-alist (("aaaa" . "external-file"))))))))
  1437. (ert-deftest test-org-export/resolve-radio-link ()
  1438. "Test `org-export-resolve-radio-link' specifications."
  1439. ;; Standard test.
  1440. (should
  1441. (org-test-with-temp-text "<<<radio>>> radio"
  1442. (org-update-radio-target-regexp)
  1443. (let* ((tree (org-element-parse-buffer))
  1444. (info `(:parse-tree ,tree)))
  1445. (org-export-resolve-radio-link
  1446. (org-element-map tree 'link 'identity info t)
  1447. info))))
  1448. ;; Radio targets are case-insensitive.
  1449. (should
  1450. (org-test-with-temp-text "<<<RADIO>>> radio"
  1451. (org-update-radio-target-regexp)
  1452. (let* ((tree (org-element-parse-buffer))
  1453. (info `(:parse-tree ,tree)))
  1454. (org-export-resolve-radio-link
  1455. (org-element-map tree 'link 'identity info t)
  1456. info))))
  1457. ;; Radio target with objects.
  1458. (should
  1459. (org-test-with-temp-text "<<<radio \\alpha>>> radio \\alpha"
  1460. (org-update-radio-target-regexp)
  1461. (let* ((tree (org-element-parse-buffer))
  1462. (info `(:parse-tree ,tree)))
  1463. (org-export-resolve-radio-link
  1464. (org-element-map tree 'link 'identity info t)
  1465. info)))))
  1466. ;;; Src-block and example-block
  1467. (ert-deftest test-org-export/unravel-code ()
  1468. "Test `org-export-unravel-code' function."
  1469. (let ((org-coderef-label-format "(ref:%s)"))
  1470. ;; 1. Code without reference.
  1471. (org-test-with-temp-text "#+BEGIN_EXAMPLE\n(+ 1 1)\n#+END_EXAMPLE"
  1472. (should (equal (org-export-unravel-code (org-element-at-point))
  1473. '("(+ 1 1)\n"))))
  1474. ;; 2. Code with reference.
  1475. (org-test-with-temp-text
  1476. "#+BEGIN_EXAMPLE\n(+ 1 1) (ref:test)\n#+END_EXAMPLE"
  1477. (should (equal (org-export-unravel-code (org-element-at-point))
  1478. '("(+ 1 1)\n" (1 . "test")))))
  1479. ;; 3. Code with user-defined reference.
  1480. (org-test-with-temp-text
  1481. "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\n(+ 1 1) [ref:test]\n#+END_EXAMPLE"
  1482. (should (equal (org-export-unravel-code (org-element-at-point))
  1483. '("(+ 1 1)\n" (1 . "test")))))
  1484. ;; 4. Code references keys are relative to the current block.
  1485. (org-test-with-temp-text "
  1486. #+BEGIN_EXAMPLE -n
  1487. \(+ 1 1)
  1488. #+END_EXAMPLE
  1489. #+BEGIN_EXAMPLE +n
  1490. \(+ 2 2)
  1491. \(+ 3 3) (ref:one)
  1492. #+END_EXAMPLE"
  1493. (goto-line 5)
  1494. (should (equal (org-export-unravel-code (org-element-at-point))
  1495. '("(+ 2 2)\n(+ 3 3)\n" (2 . "one")))))))
  1496. (ert-deftest test-org-export/format-code-default ()
  1497. "Test `org-export-format-code-default' specifications."
  1498. ;; Return the empty string when code is empty.
  1499. (should
  1500. (equal ""
  1501. (org-test-with-parsed-data "#+BEGIN_SRC emacs-lisp\n\n\n#+END_SRC"
  1502. (org-export-format-code-default
  1503. (org-element-map tree 'src-block 'identity info t) info))))
  1504. ;; Number lines, two whitespace characters before the actual loc.
  1505. (should
  1506. (equal "1 a\n2 b\n"
  1507. (org-test-with-parsed-data
  1508. "#+BEGIN_SRC emacs-lisp +n\na\nb\n#+END_SRC"
  1509. (org-export-format-code-default
  1510. (org-element-map tree 'src-block 'identity info t) info))))
  1511. ;; Put references 6 whitespace characters after the widest line,
  1512. ;; wrapped within parenthesis.
  1513. (should
  1514. (equal "123 (a)\n1 (b)\n"
  1515. (let ((org-coderef-label-format "(ref:%s)"))
  1516. (org-test-with-parsed-data
  1517. "#+BEGIN_SRC emacs-lisp\n123 (ref:a)\n1 (ref:b)\n#+END_SRC"
  1518. (org-export-format-code-default
  1519. (org-element-map tree 'src-block 'identity info t) info))))))
  1520. ;;; Smart Quotes
  1521. (ert-deftest test-org-export/activate-smart-quotes ()
  1522. "Test `org-export-activate-smart-quotes' specifications."
  1523. ;; Opening double quotes: standard test.
  1524. (should
  1525. (equal
  1526. '("some &ldquo;paragraph")
  1527. (let ((org-export-default-language "en"))
  1528. (org-test-with-parsed-data "some \"paragraph"
  1529. (org-element-map tree 'plain-text
  1530. (lambda (s) (org-export-activate-smart-quotes s :html info))
  1531. info)))))
  1532. ;; Opening quotes: at the beginning of a paragraph.
  1533. (should
  1534. (equal
  1535. '("&ldquo;begin")
  1536. (let ((org-export-default-language "en"))
  1537. (org-test-with-parsed-data "\"begin"
  1538. (org-element-map tree 'plain-text
  1539. (lambda (s) (org-export-activate-smart-quotes s :html info))
  1540. info)))))
  1541. ;; Opening quotes: after an object.
  1542. (should
  1543. (equal
  1544. '("&ldquo;begin")
  1545. (let ((org-export-default-language "en"))
  1546. (org-test-with-parsed-data "=verb= \"begin"
  1547. (org-element-map tree 'plain-text
  1548. (lambda (s) (org-export-activate-smart-quotes s :html info))
  1549. info)))))
  1550. ;; Closing quotes: standard test.
  1551. (should
  1552. (equal
  1553. '("some&rdquo; paragraph")
  1554. (let ((org-export-default-language "en"))
  1555. (org-test-with-parsed-data "some\" paragraph"
  1556. (org-element-map tree 'plain-text
  1557. (lambda (s) (org-export-activate-smart-quotes s :html info))
  1558. info)))))
  1559. ;; Closing quotes: at the end of a paragraph.
  1560. (should
  1561. (equal
  1562. '("end&rdquo;")
  1563. (let ((org-export-default-language "en"))
  1564. (org-test-with-parsed-data "end\""
  1565. (org-element-map tree 'plain-text
  1566. (lambda (s) (org-export-activate-smart-quotes s :html info))
  1567. info)))))
  1568. ;; Apostrophe: standard test.
  1569. (should
  1570. (equal
  1571. '("It shouldn&rsquo;t fail")
  1572. (let ((org-export-default-language "en"))
  1573. (org-test-with-parsed-data "It shouldn't fail"
  1574. (org-element-map tree 'plain-text
  1575. (lambda (s) (org-export-activate-smart-quotes s :html info))
  1576. info)))))
  1577. ;; Apostrophe: before an object.
  1578. (should
  1579. (equal
  1580. '("a&rsquo;")
  1581. (let ((org-export-default-language "en"))
  1582. (org-test-with-parsed-data "a'=b="
  1583. (org-element-map tree 'plain-text
  1584. (lambda (s) (org-export-activate-smart-quotes s :html info))
  1585. info)))))
  1586. ;; Apostrophe: after an object.
  1587. (should
  1588. (equal
  1589. '("&rsquo;s")
  1590. (let ((org-export-default-language "en"))
  1591. (org-test-with-parsed-data "=code='s"
  1592. (org-element-map tree 'plain-text
  1593. (lambda (s) (org-export-activate-smart-quotes s :html info))
  1594. info)))))
  1595. ;; Special case: isolated quotes.
  1596. (should
  1597. (equal '("&ldquo;" "&rdquo;")
  1598. (let ((org-export-default-language "en"))
  1599. (org-test-with-parsed-data "\"$x$\""
  1600. (org-element-map tree 'plain-text
  1601. (lambda (s) (org-export-activate-smart-quotes s :html info))
  1602. info)))))
  1603. ;; Smart quotes in secondary strings.
  1604. (should
  1605. (equal '("&ldquo;" "&rdquo;")
  1606. (let ((org-export-default-language "en"))
  1607. (org-test-with-parsed-data "* \"$x$\""
  1608. (org-element-map tree 'plain-text
  1609. (lambda (s) (org-export-activate-smart-quotes s :html info))
  1610. info)))))
  1611. ;; Smart quotes in document keywords.
  1612. (should
  1613. (equal '("&ldquo;" "&rdquo;")
  1614. (let ((org-export-default-language "en"))
  1615. (org-test-with-parsed-data "#+TITLE: \"$x$\""
  1616. (org-element-map (plist-get info :title) 'plain-text
  1617. (lambda (s) (org-export-activate-smart-quotes s :html info))
  1618. info)))))
  1619. ;; Smart quotes in parsed affiliated keywords.
  1620. (should
  1621. (equal '("&ldquo;" "&rdquo;" "Paragraph")
  1622. (let ((org-export-default-language "en"))
  1623. (org-test-with-parsed-data "#+CAPTION: \"$x$\"\nParagraph"
  1624. (org-element-map tree 'plain-text
  1625. (lambda (s) (org-export-activate-smart-quotes s :html info))
  1626. info nil nil t))))))
  1627. ;;; Tables
  1628. (ert-deftest test-org-export/special-column ()
  1629. "Test if the table's special column is properly recognized."
  1630. ;; 1. First column is special if it contains only a special marking
  1631. ;; characters or empty cells.
  1632. (org-test-with-temp-text "
  1633. | ! | 1 |
  1634. | | 2 |"
  1635. (should
  1636. (org-export-table-has-special-column-p
  1637. (org-element-map
  1638. (org-element-parse-buffer) 'table 'identity nil 'first-match))))
  1639. ;; 2. If the column contains anything else, it isn't special.
  1640. (org-test-with-temp-text "
  1641. | ! | 1 |
  1642. | b | 2 |"
  1643. (should-not
  1644. (org-export-table-has-special-column-p
  1645. (org-element-map
  1646. (org-element-parse-buffer) 'table 'identity nil 'first-match))))
  1647. ;; 3. Special marking characters are "#", "^", "*", "_", "/", "$"
  1648. ;; and "!".
  1649. (org-test-with-temp-text "
  1650. | # | 1 |
  1651. | ^ | 2 |
  1652. | * | 3 |
  1653. | _ | 4 |
  1654. | / | 5 |
  1655. | $ | 6 |
  1656. | ! | 7 |"
  1657. (should
  1658. (org-export-table-has-special-column-p
  1659. (org-element-map
  1660. (org-element-parse-buffer) 'table 'identity nil 'first-match))))
  1661. ;; 4. A first column with only empty cells isn't considered as
  1662. ;; special.
  1663. (org-test-with-temp-text "
  1664. | | 1 |
  1665. | | 2 |"
  1666. (should-not
  1667. (org-export-table-has-special-column-p
  1668. (org-element-map
  1669. (org-element-parse-buffer) 'table 'identity nil 'first-match)))))
  1670. (ert-deftest test-org-export/table-row-is-special-p ()
  1671. "Test `org-export-table-row-is-special-p' specifications."
  1672. ;; 1. A row is special if it has a special marking character in the
  1673. ;; special column.
  1674. (org-test-with-parsed-data "| ! | 1 |"
  1675. (should
  1676. (org-export-table-row-is-special-p
  1677. (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  1678. ;; 2. A row is special when its first field is "/"
  1679. (org-test-with-parsed-data "
  1680. | / | 1 |
  1681. | a | b |"
  1682. (should
  1683. (org-export-table-row-is-special-p
  1684. (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  1685. ;; 3. A row only containing alignment cookies is also considered as
  1686. ;; special.
  1687. (org-test-with-parsed-data "| <5> | | <l> | <l22> |"
  1688. (should
  1689. (org-export-table-row-is-special-p
  1690. (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  1691. ;; 4. Everything else isn't considered as special.
  1692. (org-test-with-parsed-data "| \alpha | | c |"
  1693. (should-not
  1694. (org-export-table-row-is-special-p
  1695. (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  1696. ;; 5. Table's rules are never considered as special rows.
  1697. (org-test-with-parsed-data "|---+---|"
  1698. (should-not
  1699. (org-export-table-row-is-special-p
  1700. (org-element-map tree 'table-row 'identity nil 'first-match) info))))
  1701. (ert-deftest test-org-export/has-header-p ()
  1702. "Test `org-export-table-has-header-p' specifications."
  1703. ;; 1. With an header.
  1704. (org-test-with-parsed-data "
  1705. | a | b |
  1706. |---+---|
  1707. | c | d |"
  1708. (should
  1709. (org-export-table-has-header-p
  1710. (org-element-map tree 'table 'identity info 'first-match)
  1711. info)))
  1712. ;; 2. Without an header.
  1713. (org-test-with-parsed-data "
  1714. | a | b |
  1715. | c | d |"
  1716. (should-not
  1717. (org-export-table-has-header-p
  1718. (org-element-map tree 'table 'identity info 'first-match)
  1719. info)))
  1720. ;; 3. Don't get fooled with starting and ending rules.
  1721. (org-test-with-parsed-data "
  1722. |---+---|
  1723. | a | b |
  1724. | c | d |
  1725. |---+---|"
  1726. (should-not
  1727. (org-export-table-has-header-p
  1728. (org-element-map tree 'table 'identity info 'first-match)
  1729. info))))
  1730. (ert-deftest test-org-export/table-row-group ()
  1731. "Test `org-export-table-row-group' specifications."
  1732. ;; 1. A rule creates a new group.
  1733. (org-test-with-parsed-data "
  1734. | a | b |
  1735. |---+---|
  1736. | 1 | 2 |"
  1737. (should
  1738. (equal
  1739. '(1 nil 2)
  1740. (mapcar (lambda (row) (org-export-table-row-group row info))
  1741. (org-element-map tree 'table-row 'identity)))))
  1742. ;; 2. Special rows are ignored in count.
  1743. (org-test-with-parsed-data "
  1744. | / | < | > |
  1745. |---|---+---|
  1746. | | 1 | 2 |"
  1747. (should
  1748. (equal
  1749. '(nil nil 1)
  1750. (mapcar (lambda (row) (org-export-table-row-group row info))
  1751. (org-element-map tree 'table-row 'identity)))))
  1752. ;; 3. Double rules also are ignored in count.
  1753. (org-test-with-parsed-data "
  1754. | a | b |
  1755. |---+---|
  1756. |---+---|
  1757. | 1 | 2 |"
  1758. (should
  1759. (equal
  1760. '(1 nil nil 2)
  1761. (mapcar (lambda (row) (org-export-table-row-group row info))
  1762. (org-element-map tree 'table-row 'identity))))))
  1763. (ert-deftest test-org-export/table-cell-width ()
  1764. "Test `org-export-table-cell-width' specifications."
  1765. ;; 1. Width is primarily determined by width cookies. If no cookie
  1766. ;; is found, cell's width is nil.
  1767. (org-test-with-parsed-data "
  1768. | / | <l> | <6> | <l7> |
  1769. | | a | b | c |"
  1770. (should
  1771. (equal
  1772. '(nil 6 7)
  1773. (mapcar (lambda (cell) (org-export-table-cell-width cell info))
  1774. (org-element-map tree 'table-cell 'identity info)))))
  1775. ;; 2. The last width cookie has precedence.
  1776. (org-test-with-parsed-data "
  1777. | <6> |
  1778. | <7> |
  1779. | a |"
  1780. (should
  1781. (equal
  1782. '(7)
  1783. (mapcar (lambda (cell) (org-export-table-cell-width cell info))
  1784. (org-element-map tree 'table-cell 'identity info)))))
  1785. ;; 3. Valid width cookies must have a specific row.
  1786. (org-test-with-parsed-data "| <6> | cell |"
  1787. (should
  1788. (equal
  1789. '(nil nil)
  1790. (mapcar (lambda (cell) (org-export-table-cell-width cell info))
  1791. (org-element-map tree 'table-cell 'identity))))))
  1792. (ert-deftest test-org-export/table-cell-alignment ()
  1793. "Test `org-export-table-cell-alignment' specifications."
  1794. (let ((org-table-number-fraction 0.5)
  1795. (org-table-number-regexp "^[0-9]+$"))
  1796. ;; 1. Alignment is primarily determined by alignment cookies.
  1797. (org-test-with-temp-text "| <l> | <c> | <r> |"
  1798. (let* ((tree (org-element-parse-buffer))
  1799. (info `(:parse-tree ,tree)))
  1800. (should
  1801. (equal
  1802. '(left center right)
  1803. (mapcar (lambda (cell) (org-export-table-cell-alignment cell info))
  1804. (org-element-map tree 'table-cell 'identity))))))
  1805. ;; 2. The last alignment cookie has precedence.
  1806. (org-test-with-parsed-data "
  1807. | <l8> |
  1808. | cell |
  1809. | <r9> |"
  1810. (should
  1811. (equal
  1812. '(right right right)
  1813. (mapcar (lambda (cell) (org-export-table-cell-alignment cell info))
  1814. (org-element-map tree 'table-cell 'identity)))))
  1815. ;; 3. If there's no cookie, cell's contents determine alignment.
  1816. ;; A column mostly made of cells containing numbers will align
  1817. ;; its cells to the right.
  1818. (org-test-with-parsed-data "
  1819. | 123 |
  1820. | some text |
  1821. | 12345 |"
  1822. (should
  1823. (equal
  1824. '(right right right)
  1825. (mapcar (lambda (cell)
  1826. (org-export-table-cell-alignment cell info))
  1827. (org-element-map tree 'table-cell 'identity)))))
  1828. ;; 4. Otherwise, they will be aligned to the left.
  1829. (org-test-with-parsed-data "
  1830. | text |
  1831. | some text |
  1832. | \alpha |"
  1833. (should
  1834. (equal
  1835. '(left left left)
  1836. (mapcar (lambda (cell)
  1837. (org-export-table-cell-alignment cell info))
  1838. (org-element-map tree 'table-cell 'identity)))))))
  1839. (ert-deftest test-org-export/table-cell-borders ()
  1840. "Test `org-export-table-cell-borders' specifications."
  1841. ;; 1. Recognize various column groups indicators.
  1842. (org-test-with-parsed-data "| / | < | > | <> |"
  1843. (should
  1844. (equal
  1845. '((right bottom top) (left bottom top) (right bottom top)
  1846. (right left bottom top))
  1847. (mapcar (lambda (cell)
  1848. (org-export-table-cell-borders cell info))
  1849. (org-element-map tree 'table-cell 'identity)))))
  1850. ;; 2. Accept shortcuts to define column groups.
  1851. (org-test-with-parsed-data "| / | < | < |"
  1852. (should
  1853. (equal
  1854. '((right bottom top) (right left bottom top) (left bottom top))
  1855. (mapcar (lambda (cell)
  1856. (org-export-table-cell-borders cell info))
  1857. (org-element-map tree 'table-cell 'identity)))))
  1858. ;; 3. A valid column groups row must start with a "/".
  1859. (org-test-with-parsed-data "
  1860. | | < |
  1861. | a | b |"
  1862. (should
  1863. (equal '((top) (top) (bottom) (bottom))
  1864. (mapcar (lambda (cell)
  1865. (org-export-table-cell-borders cell info))
  1866. (org-element-map tree 'table-cell 'identity)))))
  1867. ;; 4. Take table rules into consideration.
  1868. (org-test-with-parsed-data "
  1869. | 1 |
  1870. |---|
  1871. | 2 |"
  1872. (should
  1873. (equal '((below top) (bottom above))
  1874. (mapcar (lambda (cell)
  1875. (org-export-table-cell-borders cell info))
  1876. (org-element-map tree 'table-cell 'identity)))))
  1877. ;; 5. Top and (resp. bottom) rules induce both `top' and `above'
  1878. ;; (resp. `bottom' and `below') borders. Any special row is
  1879. ;; ignored.
  1880. (org-test-with-parsed-data "
  1881. |---+----|
  1882. | / | |
  1883. | | 1 |
  1884. |---+----|"
  1885. (should
  1886. (equal '((bottom below top above))
  1887. (last
  1888. (mapcar (lambda (cell)
  1889. (org-export-table-cell-borders cell info))
  1890. (org-element-map tree 'table-cell 'identity)))))))
  1891. (ert-deftest test-org-export/table-dimensions ()
  1892. "Test `org-export-table-dimensions' specifications."
  1893. ;; 1. Standard test.
  1894. (org-test-with-parsed-data "
  1895. | 1 | 2 | 3 |
  1896. | 4 | 5 | 6 |"
  1897. (should
  1898. (equal '(2 . 3)
  1899. (org-export-table-dimensions
  1900. (org-element-map tree 'table 'identity info 'first-match) info))))
  1901. ;; 2. Ignore horizontal rules and special columns.
  1902. (org-test-with-parsed-data "
  1903. | / | < | > |
  1904. | 1 | 2 | 3 |
  1905. |---+---+---|
  1906. | 4 | 5 | 6 |"
  1907. (should
  1908. (equal '(2 . 3)
  1909. (org-export-table-dimensions
  1910. (org-element-map tree 'table 'identity info 'first-match) info)))))
  1911. (ert-deftest test-org-export/table-cell-address ()
  1912. "Test `org-export-table-cell-address' specifications."
  1913. ;; 1. Standard test: index is 0-based.
  1914. (org-test-with-parsed-data "| a | b |"
  1915. (should
  1916. (equal '((0 . 0) (0 . 1))
  1917. (org-element-map tree 'table-cell
  1918. (lambda (cell) (org-export-table-cell-address cell info))
  1919. info))))
  1920. ;; 2. Special column isn't counted, nor are special rows.
  1921. (org-test-with-parsed-data "
  1922. | / | <> |
  1923. | | c |"
  1924. (should
  1925. (equal '(0 . 0)
  1926. (org-export-table-cell-address
  1927. (car (last (org-element-map tree 'table-cell 'identity info)))
  1928. info))))
  1929. ;; 3. Tables rules do not count either.
  1930. (org-test-with-parsed-data "
  1931. | a |
  1932. |---|
  1933. | b |
  1934. |---|
  1935. | c |"
  1936. (should
  1937. (equal '(2 . 0)
  1938. (org-export-table-cell-address
  1939. (car (last (org-element-map tree 'table-cell 'identity info)))
  1940. info))))
  1941. ;; 4. Return nil for special cells.
  1942. (org-test-with-parsed-data "| / | a |"
  1943. (should-not
  1944. (org-export-table-cell-address
  1945. (org-element-map tree 'table-cell 'identity nil 'first-match)
  1946. info))))
  1947. (ert-deftest test-org-export/get-table-cell-at ()
  1948. "Test `org-export-get-table-cell-at' specifications."
  1949. ;; 1. Address ignores special columns, special rows and rules.
  1950. (org-test-with-parsed-data "
  1951. | / | <> |
  1952. | | a |
  1953. |---+----|
  1954. | | b |"
  1955. (should
  1956. (equal '("b")
  1957. (org-element-contents
  1958. (org-export-get-table-cell-at
  1959. '(1 . 0)
  1960. (org-element-map tree 'table 'identity info 'first-match)
  1961. info)))))
  1962. ;; 2. Return value for a non-existent address is nil.
  1963. (org-test-with-parsed-data "| a |"
  1964. (should-not
  1965. (org-export-get-table-cell-at
  1966. '(2 . 2)
  1967. (org-element-map tree 'table 'identity info 'first-match)
  1968. info)))
  1969. (org-test-with-parsed-data "| / |"
  1970. (should-not
  1971. (org-export-get-table-cell-at
  1972. '(0 . 0)
  1973. (org-element-map tree 'table 'identity info 'first-match)
  1974. info))))
  1975. (ert-deftest test-org-export/table-cell-starts-colgroup-p ()
  1976. "Test `org-export-table-cell-starts-colgroup-p' specifications."
  1977. ;; 1. A cell at a beginning of a row always starts a column group.
  1978. (org-test-with-parsed-data "| a |"
  1979. (should
  1980. (org-export-table-cell-starts-colgroup-p
  1981. (org-element-map tree 'table-cell 'identity info 'first-match)
  1982. info)))
  1983. ;; 2. Special column should be ignored when determining the
  1984. ;; beginning of the row.
  1985. (org-test-with-parsed-data "
  1986. | / | |
  1987. | | a |"
  1988. (should
  1989. (org-export-table-cell-starts-colgroup-p
  1990. (org-element-map tree 'table-cell 'identity info 'first-match)
  1991. info)))
  1992. ;; 2. Explicit column groups.
  1993. (org-test-with-parsed-data "
  1994. | / | | < |
  1995. | a | b | c |"
  1996. (should
  1997. (equal
  1998. '(yes no yes)
  1999. (org-element-map tree 'table-cell
  2000. (lambda (cell)
  2001. (if (org-export-table-cell-starts-colgroup-p cell info) 'yes 'no))
  2002. info)))))
  2003. (ert-deftest test-org-export/table-cell-ends-colgroup-p ()
  2004. "Test `org-export-table-cell-ends-colgroup-p' specifications."
  2005. ;; 1. A cell at the end of a row always ends a column group.
  2006. (org-test-with-parsed-data "| a |"
  2007. (should
  2008. (org-export-table-cell-ends-colgroup-p
  2009. (org-element-map tree 'table-cell 'identity info 'first-match)
  2010. info)))
  2011. ;; 2. Special column should be ignored when determining the
  2012. ;; beginning of the row.
  2013. (org-test-with-parsed-data "
  2014. | / | |
  2015. | | a |"
  2016. (should
  2017. (org-export-table-cell-ends-colgroup-p
  2018. (org-element-map tree 'table-cell 'identity info 'first-match)
  2019. info)))
  2020. ;; 3. Explicit column groups.
  2021. (org-test-with-parsed-data "
  2022. | / | < | |
  2023. | a | b | c |"
  2024. (should
  2025. (equal
  2026. '(yes no yes)
  2027. (org-element-map tree 'table-cell
  2028. (lambda (cell)
  2029. (if (org-export-table-cell-ends-colgroup-p cell info) 'yes 'no))
  2030. info)))))
  2031. (ert-deftest test-org-export/table-row-starts-rowgroup-p ()
  2032. "Test `org-export-table-row-starts-rowgroup-p' specifications."
  2033. ;; 1. A row at the beginning of a table always starts a row group.
  2034. ;; So does a row following a table rule.
  2035. (org-test-with-parsed-data "
  2036. | a |
  2037. |---|
  2038. | b |"
  2039. (should
  2040. (equal
  2041. '(yes no yes)
  2042. (org-element-map tree 'table-row
  2043. (lambda (row)
  2044. (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
  2045. info))))
  2046. ;; 2. Special rows should be ignored when determining the beginning
  2047. ;; of the row.
  2048. (org-test-with-parsed-data "
  2049. | / | < |
  2050. | | a |
  2051. |---+---|
  2052. | / | < |
  2053. | | b |"
  2054. (should
  2055. (equal
  2056. '(yes no yes)
  2057. (org-element-map tree 'table-row
  2058. (lambda (row)
  2059. (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
  2060. info)))))
  2061. (ert-deftest test-org-export/table-row-ends-rowgroup-p ()
  2062. "Test `org-export-table-row-ends-rowgroup-p' specifications."
  2063. ;; 1. A row at the end of a table always ends a row group. So does
  2064. ;; a row preceding a table rule.
  2065. (org-test-with-parsed-data "
  2066. | a |
  2067. |---|
  2068. | b |"
  2069. (should
  2070. (equal
  2071. '(yes no yes)
  2072. (org-element-map tree 'table-row
  2073. (lambda (row)
  2074. (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
  2075. info))))
  2076. ;; 2. Special rows should be ignored when determining the beginning
  2077. ;; of the row.
  2078. (org-test-with-parsed-data "
  2079. | | a |
  2080. | / | < |
  2081. |---+---|
  2082. | | b |
  2083. | / | < |"
  2084. (should
  2085. (equal
  2086. '(yes no yes)
  2087. (org-element-map tree 'table-row
  2088. (lambda (row)
  2089. (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
  2090. info)))))
  2091. (ert-deftest test-org-export/table-row-starts-header-p ()
  2092. "Test `org-export-table-row-starts-header-p' specifications."
  2093. ;; 1. Only the row starting the first row group starts the table
  2094. ;; header.
  2095. (org-test-with-parsed-data "
  2096. | a |
  2097. | b |
  2098. |---|
  2099. | c |"
  2100. (should
  2101. (equal
  2102. '(yes no no no)
  2103. (org-element-map tree 'table-row
  2104. (lambda (row)
  2105. (if (org-export-table-row-starts-header-p row info) 'yes 'no))
  2106. info))))
  2107. ;; 2. A row cannot start an header if there's no header in the
  2108. ;; table.
  2109. (org-test-with-parsed-data "
  2110. | a |
  2111. |---|"
  2112. (should-not
  2113. (org-export-table-row-starts-header-p
  2114. (org-element-map tree 'table-row 'identity info 'first-match)
  2115. info))))
  2116. (ert-deftest test-org-export/table-row-ends-header-p ()
  2117. "Test `org-export-table-row-ends-header-p' specifications."
  2118. ;; 1. Only the row starting the first row group starts the table
  2119. ;; header.
  2120. (org-test-with-parsed-data "
  2121. | a |
  2122. | b |
  2123. |---|
  2124. | c |"
  2125. (should
  2126. (equal
  2127. '(no yes no no)
  2128. (org-element-map tree 'table-row
  2129. (lambda (row)
  2130. (if (org-export-table-row-ends-header-p row info) 'yes 'no))
  2131. info))))
  2132. ;; 2. A row cannot start an header if there's no header in the
  2133. ;; table.
  2134. (org-test-with-parsed-data "
  2135. | a |
  2136. |---|"
  2137. (should-not
  2138. (org-export-table-row-ends-header-p
  2139. (org-element-map tree 'table-row 'identity info 'first-match)
  2140. info))))
  2141. ;;; Templates
  2142. (ert-deftest test-org-export/inner-template ()
  2143. "Test `inner-template' translator specifications."
  2144. (should
  2145. (equal "Success!"
  2146. (let (org-export-registered-backends)
  2147. (org-export-define-backend 'test
  2148. '((inner-template . (lambda (contents info) "Success!"))
  2149. (headline . (lambda (h c i) "Headline"))))
  2150. (org-test-with-temp-text "* Headline"
  2151. (org-export-as 'test)))))
  2152. ;; Inner template is applied even in a "body-only" export.
  2153. (should
  2154. (equal "Success!"
  2155. (let (org-export-registered-backends)
  2156. (org-export-define-backend 'test
  2157. '((inner-template . (lambda (contents info) "Success!"))
  2158. (headline . (lambda (h c i) "Headline"))))
  2159. (org-test-with-temp-text "* Headline"
  2160. (org-export-as 'test nil nil 'body-only))))))
  2161. (ert-deftest test-org-export/template ()
  2162. "Test `template' translator specifications."
  2163. (should
  2164. (equal "Success!"
  2165. (let (org-export-registered-backends)
  2166. (org-export-define-backend 'test
  2167. '((template . (lambda (contents info) "Success!"))
  2168. (headline . (lambda (h c i) "Headline"))))
  2169. (org-test-with-temp-text "* Headline"
  2170. (org-export-as 'test)))))
  2171. ;; Template is not applied in a "body-only" export.
  2172. (should-not
  2173. (equal "Success!"
  2174. (let (org-export-registered-backends)
  2175. (org-export-define-backend 'test
  2176. '((template . (lambda (contents info) "Success!"))
  2177. (headline . (lambda (h c i) "Headline"))))
  2178. (org-test-with-temp-text "* Headline"
  2179. (org-export-as 'test nil nil 'body-only))))))
  2180. ;;; Topology
  2181. (ert-deftest test-org-export/get-next-element ()
  2182. "Test `org-export-get-next-element' specifications."
  2183. ;; Standard test.
  2184. (should
  2185. (equal "b"
  2186. (org-test-with-parsed-data "* Headline\n*a* b"
  2187. (org-export-get-next-element
  2188. (org-element-map tree 'bold 'identity info t) info))))
  2189. ;; Return nil when no previous element.
  2190. (should-not
  2191. (org-test-with-parsed-data "* Headline\na *b*"
  2192. (org-export-get-next-element
  2193. (org-element-map tree 'bold 'identity info t) info)))
  2194. ;; Non-exportable elements are ignored.
  2195. (should-not
  2196. (let ((org-export-with-timestamps nil))
  2197. (org-test-with-parsed-data "\alpha <2012-03-29 Thu>"
  2198. (org-export-get-next-element
  2199. (org-element-map tree 'entity 'identity info t) info))))
  2200. ;; Find next element in secondary strings.
  2201. (should
  2202. (eq 'verbatim
  2203. (org-test-with-parsed-data "* a =verb="
  2204. (org-element-type
  2205. (org-export-get-next-element
  2206. (org-element-map tree 'plain-text 'identity info t) info)))))
  2207. ;; Find next element in document keywords.
  2208. (should
  2209. (eq 'verbatim
  2210. (org-test-with-parsed-data "#+TITLE: a =verb="
  2211. (org-element-type
  2212. (org-export-get-next-element
  2213. (org-element-map
  2214. (plist-get info :title) 'plain-text 'identity info t) info)))))
  2215. ;; Find next element in parsed affiliated keywords.
  2216. (should
  2217. (eq 'verbatim
  2218. (org-test-with-parsed-data "#+CAPTION: a =verb=\nParagraph"
  2219. (org-element-type
  2220. (org-export-get-next-element
  2221. (org-element-map tree 'plain-text 'identity info t nil t) info)))))
  2222. ;; With optional argument N, return a list containing all the
  2223. ;; following elements.
  2224. (should
  2225. (equal
  2226. '(bold code underline)
  2227. (org-test-with-parsed-data "_a_ /b/ *c* ~d~ _e_"
  2228. (mapcar 'car
  2229. (org-export-get-next-element
  2230. (org-element-map tree 'italic 'identity info t) info t)))))
  2231. ;; When N is a positive integer, return a list containing up to
  2232. ;; N following elements.
  2233. (should
  2234. (equal
  2235. '(bold code)
  2236. (org-test-with-parsed-data "_a_ /b/ *c* ~d~ _e_"
  2237. (mapcar 'car
  2238. (org-export-get-next-element
  2239. (org-element-map tree 'italic 'identity info t) info 2))))))
  2240. (ert-deftest test-org-export/get-previous-element ()
  2241. "Test `org-export-get-previous-element' specifications."
  2242. ;; Standard test.
  2243. (should
  2244. (equal "a "
  2245. (org-test-with-parsed-data "* Headline\na *b*"
  2246. (org-export-get-previous-element
  2247. (org-element-map tree 'bold 'identity info t) info))))
  2248. ;; Return nil when no previous element.
  2249. (should-not
  2250. (org-test-with-parsed-data "* Headline\n*a* b"
  2251. (org-export-get-previous-element
  2252. (org-element-map tree 'bold 'identity info t) info)))
  2253. ;; Non-exportable elements are ignored.
  2254. (should-not
  2255. (let ((org-export-with-timestamps nil))
  2256. (org-test-with-parsed-data "<2012-03-29 Thu> \alpha"
  2257. (org-export-get-previous-element
  2258. (org-element-map tree 'entity 'identity info t) info))))
  2259. ;; Find previous element in secondary strings.
  2260. (should
  2261. (eq 'verbatim
  2262. (org-test-with-parsed-data "* =verb= a"
  2263. (org-element-type
  2264. (org-export-get-previous-element
  2265. (org-element-map tree 'plain-text 'identity info t) info)))))
  2266. ;; Find previous element in document keywords.
  2267. (should
  2268. (eq 'verbatim
  2269. (org-test-with-parsed-data "#+TITLE: =verb= a"
  2270. (org-element-type
  2271. (org-export-get-previous-element
  2272. (org-element-map
  2273. (plist-get info :title) 'plain-text 'identity info t) info)))))
  2274. ;; Find previous element in parsed affiliated keywords.
  2275. (should
  2276. (eq 'verbatim
  2277. (org-test-with-parsed-data "#+CAPTION: =verb= a\nParagraph"
  2278. (org-element-type
  2279. (org-export-get-previous-element
  2280. (org-element-map tree 'plain-text 'identity info t nil t) info)))))
  2281. ;; With optional argument N, return a list containing up to
  2282. ;; N previous elements.
  2283. (should
  2284. (equal '(underline italic bold)
  2285. (org-test-with-parsed-data "_a_ /b/ *c* ~d~"
  2286. (mapcar 'car
  2287. (org-export-get-previous-element
  2288. (org-element-map tree 'code 'identity info t) info t)))))
  2289. ;; When N is a positive integer, return a list containing up to
  2290. ;; N previous elements.
  2291. (should
  2292. (equal '(italic bold)
  2293. (org-test-with-parsed-data "_a_ /b/ *c* ~d~"
  2294. (mapcar 'car
  2295. (org-export-get-previous-element
  2296. (org-element-map tree 'code 'identity info t) info 2))))))
  2297. (provide 'test-ox)
  2298. ;;; test-org-export.el end here