test-ox.el 121 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658
  1. ;;; test-ox.el --- Tests for ox.el
  2. ;; Copyright (C) 2012-2015 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. (defun org-test-default-backend ()
  19. "Return a default export back-end.
  20. This back-end simply returns parsed data as Org syntax."
  21. (org-export-create-backend
  22. :transcoders (let (transcode-table)
  23. (dolist (type (append org-element-all-elements
  24. org-element-all-objects)
  25. transcode-table)
  26. (push
  27. (cons type
  28. (lambda (obj contents info)
  29. (funcall
  30. (intern (format "org-element-%s-interpreter"
  31. type))
  32. obj contents)))
  33. transcode-table)))))
  34. (defmacro org-test-with-parsed-data (data &rest body)
  35. "Execute body with parsed data available.
  36. DATA is a string containing the data to be parsed. BODY is the
  37. body to execute. Parse tree is available under the `tree'
  38. variable, and communication channel under `info'."
  39. (declare (debug (form body)) (indent 1))
  40. `(org-test-with-temp-text ,data
  41. (org-export--delete-comments)
  42. (let* ((tree (org-element-parse-buffer))
  43. (info (org-export-get-environment)))
  44. (org-export--prune-tree tree info)
  45. (org-export--remove-uninterpreted-data tree info)
  46. (let ((info (org-combine-plists
  47. info (org-export-collect-tree-properties tree info))))
  48. ,@body))))
  49. ;;; Internal Tests
  50. (ert-deftest test-org-export/bind-keyword ()
  51. "Test reading #+BIND: keywords."
  52. ;; Test with `org-export-allow-bind-keywords' set to t.
  53. (should
  54. (org-test-with-temp-text "#+BIND: test-ox-var value"
  55. (let ((org-export-allow-bind-keywords t))
  56. (org-export-get-environment)
  57. (eq test-ox-var 'value))))
  58. ;; Test with `org-export-allow-bind-keywords' set to nil.
  59. (should-not
  60. (org-test-with-temp-text "#+BIND: test-ox-var value"
  61. (let ((org-export-allow-bind-keywords nil))
  62. (org-export-get-environment)
  63. (boundp 'test-ox-var))))
  64. ;; BIND keywords are case-insensitive.
  65. (should
  66. (org-test-with-temp-text "#+bind: test-ox-var value"
  67. (let ((org-export-allow-bind-keywords t))
  68. (org-export-get-environment)
  69. (eq test-ox-var 'value))))
  70. ;; Preserve order of BIND keywords.
  71. (should
  72. (org-test-with-temp-text "#+BIND: test-ox-var 1\n#+BIND: test-ox-var 2"
  73. (let ((org-export-allow-bind-keywords t))
  74. (org-export-get-environment)
  75. (eq test-ox-var 2))))
  76. ;; Read BIND keywords in setup files.
  77. (should
  78. (org-test-with-temp-text
  79. (format "#+SETUPFILE: \"%s/examples/setupfile.org\"" org-test-dir)
  80. (let ((org-export-allow-bind-keywords t))
  81. (org-export-get-environment)
  82. (eq variable 'value))))
  83. ;; Verify that bound variables are seen during export.
  84. (should
  85. (equal "Yes\n"
  86. (org-test-with-temp-text "#+BIND: test-ox-var value"
  87. (let ((org-export-allow-bind-keywords t))
  88. (org-export-as
  89. (org-export-create-backend
  90. :transcoders
  91. '((section . (lambda (s c i)
  92. (if (eq test-ox-var 'value) "Yes" "No")))))))))))
  93. (ert-deftest test-org-export/parse-option-keyword ()
  94. "Test reading all standard #+OPTIONS: items."
  95. (should
  96. (equal
  97. (org-export--parse-option-keyword
  98. "H:1 num:t \\n:t timestamp:t arch:t author:t creator:t d:t email:t
  99. *:t e:t ::t f:t pri:t -:t ^:t toc:t |:t tags:t tasks:t <:t todo:t inline:nil
  100. stat:t title:t")
  101. '(:headline-levels
  102. 1 :preserve-breaks t :section-numbers t :time-stamp-file t
  103. :with-archived-trees t :with-author t :with-creator t :with-drawers t
  104. :with-email t :with-emphasize t :with-entities t :with-fixed-width t
  105. :with-footnotes t :with-inlinetasks nil :with-priority t
  106. :with-special-strings t :with-statistics-cookies t :with-sub-superscript t
  107. :with-toc t :with-tables t :with-tags t :with-tasks t :with-timestamps t
  108. :with-title t :with-todo-keywords t)))
  109. ;; Test some special values.
  110. (should
  111. (equal
  112. (org-export--parse-option-keyword
  113. "arch:headline d:(\"TEST\") ^:{} toc:1 tags:not-in-toc tasks:todo num:2 <:active")
  114. '( :section-numbers
  115. 2
  116. :with-archived-trees headline :with-drawers ("TEST")
  117. :with-sub-superscript {} :with-toc 1 :with-tags not-in-toc
  118. :with-tasks todo :with-timestamps active))))
  119. (ert-deftest test-org-export/get-inbuffer-options ()
  120. "Test reading all standard export keywords."
  121. ;; Properties should follow buffer order.
  122. (should
  123. (equal
  124. (org-test-with-temp-text "#+LANGUAGE: fr\n#+CREATOR: Me\n#+EMAIL: email"
  125. (org-export--get-inbuffer-options))
  126. '(:language "fr" :creator "Me" :email "email")))
  127. ;; Test `space' behaviour.
  128. (should
  129. (equal
  130. (let ((back-end (org-export-create-backend
  131. :options '((:keyword "KEYWORD" nil nil space)))))
  132. (org-test-with-temp-text "#+KEYWORD: With\n#+KEYWORD: spaces"
  133. (org-export--get-inbuffer-options back-end)))
  134. '(:keyword "With spaces")))
  135. ;; Test `newline' behaviour.
  136. (should
  137. (equal
  138. (let ((back-end (org-export-create-backend
  139. :options '((:keyword "KEYWORD" nil nil newline)))))
  140. (org-test-with-temp-text "#+KEYWORD: With\n#+KEYWORD: two lines"
  141. (org-export--get-inbuffer-options back-end)))
  142. '(:keyword "With\ntwo lines")))
  143. ;; Test `split' behaviour.
  144. (should
  145. (equal
  146. (org-test-with-temp-text "#+SELECT_TAGS: a\n#+SELECT_TAGS: b"
  147. (org-export--get-inbuffer-options))
  148. '(:select-tags ("a" "b"))))
  149. ;; Test `parse' behaviour. `parse' implies `space' but preserve
  150. ;; line breaks. Multi-line objects are allowed.
  151. (should
  152. (org-element-map
  153. (org-test-with-temp-text "#+TITLE: *bold*"
  154. (plist-get (org-export--get-inbuffer-options) :title))
  155. 'bold #'identity nil t))
  156. (should
  157. (equal
  158. (org-test-with-temp-text "#+TITLE: Some title\n#+TITLE: with spaces"
  159. (plist-get (org-export--get-inbuffer-options) :title))
  160. '("Some title with spaces")))
  161. (should
  162. (org-element-map
  163. (org-test-with-temp-text "#+TITLE: Some title\\\\\n#+TITLE: with spaces"
  164. (plist-get (org-export--get-inbuffer-options) :title))
  165. 'line-break #'identity nil t))
  166. (should
  167. (org-element-map
  168. (org-test-with-temp-text "#+TITLE: *bold\n#+TITLE: sentence*"
  169. (plist-get (org-export--get-inbuffer-options) :title))
  170. 'bold #'identity nil t))
  171. ;; Options set through SETUPFILE.
  172. (should
  173. (equal
  174. (org-test-with-temp-text
  175. (format "#+DESCRIPTION: l1
  176. #+LANGUAGE: es
  177. #+SELECT_TAGS: a
  178. #+TITLE: a
  179. #+SETUPFILE: \"%s/examples/setupfile.org\"
  180. #+LANGUAGE: fr
  181. #+SELECT_TAGS: c
  182. #+TITLE: c"
  183. org-test-dir)
  184. (org-export--get-inbuffer-options))
  185. '(:language "fr" :select-tags ("a" "b" "c") :title ("a b c"))))
  186. ;; More than one property can refer to the same buffer keyword.
  187. (should
  188. (equal '(:k2 "value" :k1 "value")
  189. (let ((backend (org-export-create-backend
  190. :options '((:k1 "KEYWORD")
  191. (:k2 "KEYWORD")))))
  192. (org-test-with-temp-text "#+KEYWORD: value"
  193. (org-export--get-inbuffer-options backend)))))
  194. ;; Keywords in commented subtrees are ignored.
  195. (should-not
  196. (equal "Me"
  197. (org-test-with-parsed-data "* COMMENT H1\n#+AUTHOR: Me"
  198. (plist-get info :author))))
  199. (should-not
  200. (equal "Mine"
  201. (org-test-with-parsed-data "* COMMENT H1\n** H2\n#+EMAIL: Mine"
  202. (plist-get info :email)))))
  203. (ert-deftest test-org-export/get-subtree-options ()
  204. "Test setting options from headline's properties."
  205. ;; EXPORT_TITLE.
  206. (org-test-with-temp-text "#+TITLE: Title
  207. * Headline
  208. :PROPERTIES:
  209. :EXPORT_TITLE: Subtree Title
  210. :END:
  211. Paragraph"
  212. (forward-line)
  213. (should (equal (plist-get (org-export-get-environment nil t) :title)
  214. '("Subtree Title"))))
  215. :title
  216. '("subtree-title")
  217. ;; EXPORT_OPTIONS.
  218. (org-test-with-temp-text "#+OPTIONS: H:1
  219. * Headline
  220. :PROPERTIES:
  221. :EXPORT_OPTIONS: H:2
  222. :END:
  223. Paragraph"
  224. (forward-line)
  225. (should
  226. (= 2 (plist-get (org-export-get-environment nil t) :headline-levels))))
  227. ;; EXPORT_DATE.
  228. (org-test-with-temp-text "#+DATE: today
  229. * Headline
  230. :PROPERTIES:
  231. :EXPORT_DATE: 29-03-2012
  232. :END:
  233. Paragraph"
  234. (forward-line)
  235. (should (equal (plist-get (org-export-get-environment nil t) :date)
  236. '("29-03-2012"))))
  237. ;; Properties with `split' behaviour are stored as a list of
  238. ;; strings.
  239. (should
  240. (equal '("a" "b")
  241. (org-test-with-temp-text "#+EXCLUDE_TAGS: noexport
  242. * Headline
  243. :PROPERTIES:
  244. :EXPORT_EXCLUDE_TAGS: a b
  245. :END:
  246. Paragraph"
  247. (progn
  248. (forward-line)
  249. (plist-get (org-export-get-environment nil t) :exclude-tags)))))
  250. ;; Handle :PROPERTY+: syntax.
  251. (should
  252. (equal '("a" "b")
  253. (org-test-with-temp-text "#+EXCLUDE_TAGS: noexport
  254. * Headline
  255. :PROPERTIES:
  256. :EXPORT_EXCLUDE_TAGS: a
  257. :EXPORT_EXCLUDE_TAGS+: b
  258. :END:
  259. Paragraph"
  260. (progn
  261. (forward-line)
  262. (plist-get (org-export-get-environment nil t) :exclude-tags)))))
  263. ;; Export properties are case-insensitive.
  264. (org-test-with-temp-text "* Headline
  265. :PROPERTIES:
  266. :EXPORT_Date: 29-03-2012
  267. :END:
  268. Paragraph"
  269. (should (equal (plist-get (org-export-get-environment nil t) :date)
  270. '("29-03-2012"))))
  271. ;; Still grab correct options when section above is empty.
  272. (should
  273. (equal '("H1")
  274. (org-test-with-temp-text "* H1\n** H11\n** H12"
  275. (progn (forward-line 2)
  276. (plist-get (org-export-get-environment nil t) :title))))))
  277. (ert-deftest test-org-export/set-title ()
  278. "Test title setting."
  279. ;; Without TITLE keyword.
  280. (should
  281. (equal
  282. ""
  283. (let (org-export-filter-body-functions
  284. org-export-filter-final-output-functions)
  285. (org-test-with-temp-text "Test"
  286. (org-export-as
  287. (org-export-create-backend
  288. :transcoders
  289. '((template . (lambda (text info)
  290. (org-element-interpret-data
  291. (plist-get info :title)))))))))))
  292. ;; With a blank TITLE keyword.
  293. (should
  294. (equal
  295. ""
  296. (let (org-export-filter-body-functions
  297. org-export-filter-final-output-functions)
  298. (org-test-with-temp-text "#+TITLE:\nTest"
  299. (org-export-as
  300. (org-export-create-backend
  301. :transcoders
  302. '((template . (lambda (text info)
  303. (org-element-interpret-data
  304. (plist-get info :title)))))))))))
  305. ;; With a non-empty TITLE keyword.
  306. (should
  307. (equal
  308. "Title"
  309. (org-test-with-temp-text "#+TITLE: Title\nTest"
  310. (org-export-as
  311. (org-export-create-backend
  312. :transcoders
  313. '((template . (lambda (text info)
  314. (org-element-interpret-data
  315. (plist-get info :title))))))))))
  316. ;; When exporting a subtree, its heading becomes the headline of the
  317. ;; document...
  318. (should
  319. (equal
  320. "Headline"
  321. (org-test-with-temp-text "* Headline\nBody"
  322. (org-export-as
  323. (org-export-create-backend
  324. :transcoders
  325. '((template . (lambda (text info)
  326. (org-element-interpret-data
  327. (plist-get info :title))))))
  328. 'subtree))))
  329. ;; ... unless there is an EXPORT_TITLE property at the root of the
  330. ;; subtree.
  331. (should
  332. (equal
  333. "B"
  334. (org-test-with-temp-text
  335. "* A\n :PROPERTIES:\n :EXPORT_TITLE: B\n :END:\nBody"
  336. (org-export-as
  337. (org-export-create-backend
  338. :transcoders
  339. '((template . (lambda (text info)
  340. (org-element-interpret-data
  341. (plist-get info :title))))))
  342. 'subtree)))))
  343. (ert-deftest test-org-export/handle-options ()
  344. "Test if export options have an impact on output."
  345. ;; Test exclude tags for headlines and inlinetasks.
  346. (should
  347. (equal ""
  348. (let (org-export-filter-body-functions
  349. org-export-filter-final-output-functions)
  350. (org-test-with-temp-text "* Head1 :noexp:"
  351. (org-export-as (org-test-default-backend)
  352. nil nil nil '(:exclude-tags ("noexp")))))))
  353. ;; Test include tags for headlines and inlinetasks.
  354. (should
  355. (equal "* H2\n** Sub :exp:\n*** Sub Sub\n"
  356. (org-test-with-temp-text "* H1\n* H2\n** Sub :exp:\n*** Sub Sub\n* H3"
  357. (let ((org-tags-column 0))
  358. (org-export-as (org-test-default-backend)
  359. nil nil nil '(:select-tags ("exp")))))))
  360. ;; If there is an include tag, ignore the section before the first
  361. ;; headline, if any.
  362. (should
  363. (equal "* H1 :exp:\nBody\n"
  364. (org-test-with-temp-text "First section\n* H1 :exp:\nBody"
  365. (let ((org-tags-column 0))
  366. (org-export-as (org-test-default-backend)
  367. nil nil nil '(:select-tags ("exp")))))))
  368. (should-not
  369. (equal "* H1 :exp:\n"
  370. (org-test-with-temp-text "* H1 :exp:\nBody"
  371. (let ((org-tags-column 0))
  372. (org-export-as (org-test-default-backend)
  373. nil nil nil '(:select-tags ("exp")))))))
  374. ;; Test mixing include tags and exclude tags.
  375. (should
  376. (string-match
  377. "\\* Head1[ \t]+:export:\n\\*\\* Sub-Head2\n"
  378. (org-test-with-temp-text "
  379. * Head1 :export:
  380. ** Sub-Head1 :noexport:
  381. ** Sub-Head2
  382. * Head2 :noexport:
  383. ** Sub-Head1 :export:"
  384. (org-export-as (org-test-default-backend) nil nil nil
  385. '(:select-tags ("export") :exclude-tags ("noexport"))))))
  386. ;; Ignore tasks.
  387. (should
  388. (equal ""
  389. (let ((org-todo-keywords '((sequence "TODO" "DONE")))
  390. org-export-filter-body-functions
  391. org-export-filter-final-output-functions)
  392. (org-test-with-temp-text "* TODO Head1"
  393. (org-export-as (org-test-default-backend)
  394. nil nil nil '(:with-tasks nil))))))
  395. (should
  396. (equal "* TODO Head1\n"
  397. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  398. (org-test-with-temp-text "* TODO Head1"
  399. (org-export-as (org-test-default-backend)
  400. nil nil nil '(:with-tasks t))))))
  401. ;; Archived tree.
  402. (should
  403. (equal ""
  404. (let (org-export-filter-body-functions
  405. org-export-filter-final-output-functions)
  406. (org-test-with-temp-text "* Head1 :archive:"
  407. (let ((org-archive-tag "archive"))
  408. (org-export-as (org-test-default-backend)
  409. nil nil nil '(:with-archived-trees nil)))))))
  410. (should
  411. (string-match
  412. "\\* Head1[ \t]+:archive:"
  413. (org-test-with-temp-text "* Head1 :archive:\nbody\n** Sub-head 2"
  414. (let ((org-archive-tag "archive"))
  415. (org-export-as (org-test-default-backend) nil nil nil
  416. '(:with-archived-trees headline))))))
  417. (should
  418. (string-match
  419. "\\`\\* Head1[ \t]+:archive:\n\\'"
  420. (org-test-with-temp-text "* Head1 :archive:"
  421. (let ((org-archive-tag "archive"))
  422. (org-export-as (org-test-default-backend)
  423. nil nil nil '(:with-archived-trees t))))))
  424. ;; Clocks.
  425. (should
  426. (string-match "CLOCK: \\[2012-04-29 .* 10:45\\]"
  427. (org-test-with-temp-text "CLOCK: [2012-04-29 sun. 10:45]"
  428. (org-export-as (org-test-default-backend)
  429. nil nil nil '(:with-clocks t)))))
  430. (should
  431. (equal ""
  432. (let (org-export-filter-body-functions
  433. org-export-filter-final-output-functions)
  434. (org-test-with-temp-text "CLOCK: [2012-04-29 sun. 10:45]"
  435. (org-export-as (org-test-default-backend)
  436. nil nil nil '(:with-clocks nil))))))
  437. ;; Drawers.
  438. (should
  439. (equal ""
  440. (let (org-export-filter-body-functions
  441. org-export-filter-final-output-functions)
  442. (org-test-with-temp-text ":TEST:\ncontents\n:END:"
  443. (org-export-as (org-test-default-backend)
  444. nil nil nil '(:with-drawers nil))))))
  445. (should
  446. (equal ":TEST:\ncontents\n:END:\n"
  447. (org-test-with-temp-text ":TEST:\ncontents\n:END:"
  448. (org-export-as (org-test-default-backend)
  449. nil nil nil '(:with-drawers t)))))
  450. (should
  451. (equal ":FOO:\nkeep\n:END:\n"
  452. (org-test-with-temp-text ":FOO:\nkeep\n:END:\n:BAR:\nremove\n:END:"
  453. (org-export-as (org-test-default-backend)
  454. nil nil nil '(:with-drawers ("FOO"))))))
  455. (should
  456. (equal ":FOO:\nkeep\n:END:\n"
  457. (org-test-with-temp-text ":FOO:\nkeep\n:END:\n:BAR:\nremove\n:END:"
  458. (org-export-as (org-test-default-backend)
  459. nil nil nil '(:with-drawers (not "BAR"))))))
  460. ;; Fixed-width.
  461. (should
  462. (equal ": A\n"
  463. (org-test-with-temp-text ": A"
  464. (org-export-as (org-test-default-backend) nil nil nil
  465. '(:with-fixed-width t)))))
  466. (should
  467. (equal ""
  468. (let (org-export-filter-body-functions
  469. org-export-filter-final-output-functions)
  470. (org-test-with-temp-text ": A"
  471. (org-export-as (org-test-default-backend) nil nil nil
  472. '(:with-fixed-width nil))))))
  473. ;; Footnotes.
  474. (should
  475. (equal "Footnote?"
  476. (let ((org-footnote-section nil))
  477. (org-test-with-temp-text "Footnote?[fn:1]\n\n[fn:1] Def"
  478. (org-trim (org-export-as (org-test-default-backend)
  479. nil nil nil '(:with-footnotes nil)))))))
  480. (should
  481. (equal "Footnote?[fn:1]\n\n[fn:1] Def"
  482. (let ((org-footnote-section nil))
  483. (org-test-with-temp-text "Footnote?[fn:1]\n\n[fn:1] Def"
  484. (org-trim (org-export-as (org-test-default-backend)
  485. nil nil nil '(:with-footnotes t)))))))
  486. ;; Inlinetasks.
  487. (when (featurep 'org-inlinetask)
  488. (should
  489. (equal
  490. ""
  491. (let ((org-inlinetask-min-level 15)
  492. org-export-filter-body-functions
  493. org-export-filter-final-output-functions)
  494. (org-test-with-temp-text "*************** Task"
  495. (org-export-as (org-test-default-backend)
  496. nil nil nil '(:with-inlinetasks nil))))))
  497. (should
  498. (equal
  499. ""
  500. (let ((org-inlinetask-min-level 15)
  501. org-export-filter-body-functions
  502. org-export-filter-final-output-functions)
  503. (org-test-with-temp-text
  504. "*************** Task\nContents\n*************** END"
  505. (org-export-as (org-test-default-backend)
  506. nil nil nil '(:with-inlinetasks nil)))))))
  507. ;; Plannings.
  508. (should
  509. (string-match
  510. "* H\nCLOSED: \\[2012-04-29 .* 10:45\\]"
  511. (let ((org-closed-string "CLOSED:"))
  512. (org-test-with-temp-text "* H\nCLOSED: [2012-04-29 sun. 10:45]"
  513. (org-export-as (org-test-default-backend)
  514. nil nil nil '(:with-planning t))))))
  515. (should
  516. (equal "* H\n"
  517. (let ((org-closed-string "CLOSED:"))
  518. (org-test-with-temp-text "* H\nCLOSED: [2012-04-29 sun. 10:45]"
  519. (org-export-as (org-test-default-backend)
  520. nil nil nil '(:with-planning nil))))))
  521. ;; Property Drawers.
  522. (should
  523. (equal "* H1\n"
  524. (org-test-with-temp-text
  525. "* H1\n :PROPERTIES:\n :PROP: value\n :END:"
  526. (org-export-as (org-test-default-backend)
  527. nil nil nil '(:with-properties nil)))))
  528. (should
  529. (equal "* H1\n:PROPERTIES:\n:PROP: value\n:END:\n"
  530. (org-test-with-temp-text
  531. "* H1\n :PROPERTIES:\n :PROP: value\n :END:"
  532. (org-export-as (org-test-default-backend)
  533. nil nil nil '(:with-properties t)))))
  534. (should
  535. (equal "* H1\n:PROPERTIES:\n:B: 2\n:END:\n"
  536. (org-test-with-temp-text
  537. "* H1\n :PROPERTIES:\n :A: 1\n :B: 2\n:END:"
  538. (org-export-as (org-test-default-backend)
  539. nil nil nil '(:with-properties ("B"))))))
  540. ;; Statistics cookies.
  541. (should
  542. (equal ""
  543. (let (org-export-filter-body-functions
  544. org-export-filter-final-output-functions)
  545. (org-trim
  546. (org-test-with-temp-text "[0/0]"
  547. (org-export-as (org-test-default-backend)
  548. nil nil nil '(:with-statistics-cookies nil)))))))
  549. ;; Tables.
  550. (should
  551. (equal "| A |\n"
  552. (org-test-with-temp-text "| A |"
  553. (org-export-as (org-test-default-backend) nil nil nil
  554. '(:with-tables t)))))
  555. (should
  556. (equal ""
  557. (let (org-export-filter-body-functions
  558. org-export-filter-final-output-functions)
  559. (org-test-with-temp-text "| A |"
  560. (org-export-as (org-test-default-backend) nil nil nil
  561. '(:with-tables nil)))))))
  562. (ert-deftest test-org-export/with-timestamps ()
  563. "Test `org-export-with-timestamps' specifications."
  564. ;; t value.
  565. (should
  566. (string-match
  567. "\\[2012-04-29 .*? 10:45\\]<2012-04-29 .*? 10:45>"
  568. (org-test-with-temp-text "[2012-04-29 sun. 10:45]<2012-04-29 sun. 10:45>"
  569. (org-export-as (org-test-default-backend)
  570. nil nil nil '(:with-timestamps t)))))
  571. ;; nil value.
  572. (should
  573. (equal
  574. ""
  575. (let (org-export-filter-body-functions
  576. org-export-filter-final-output-functions)
  577. (org-trim
  578. (org-test-with-temp-text "[2012-04-29 sun. 10:45]<2012-04-29 sun. 10:45>"
  579. (org-export-as (org-test-default-backend)
  580. nil nil nil '(:with-timestamps nil)))))))
  581. ;; `active' value.
  582. (should
  583. (string-match
  584. "<2012-03-29 .*?>\n\nParagraph <2012-03-29 .*?>\\[2012-03-29 .*?\\]"
  585. (org-test-with-temp-text
  586. "<2012-03-29 Thu>[2012-03-29 Thu]
  587. Paragraph <2012-03-29 Thu>[2012-03-29 Thu]"
  588. (org-export-as (org-test-default-backend)
  589. nil nil nil '(:with-timestamps active)))))
  590. ;; `inactive' value.
  591. (should
  592. (string-match
  593. "\\[2012-03-29 .*?\\]\n\nParagraph <2012-03-29 .*?>\\[2012-03-29 .*?\\]"
  594. (org-test-with-temp-text
  595. "<2012-03-29 Thu>[2012-03-29 Thu]
  596. Paragraph <2012-03-29 Thu>[2012-03-29 Thu]"
  597. (org-export-as (org-test-default-backend)
  598. nil nil nil '(:with-timestamps inactive))))))
  599. (ert-deftest test-org-export/comment-tree ()
  600. "Test if export process ignores commented trees."
  601. (should
  602. (equal ""
  603. (let (org-export-filter-body-functions
  604. org-export-filter-final-output-functions)
  605. (org-test-with-temp-text "* COMMENT Head1"
  606. (org-export-as (org-test-default-backend)))))))
  607. (ert-deftest test-org-export/uninterpreted ()
  608. "Test handling of uninterpreted elements."
  609. ;; Entities.
  610. (should
  611. (equal "dummy\n"
  612. (org-test-with-temp-text "\\alpha"
  613. (org-export-as
  614. (org-export-create-backend
  615. :transcoders '((entity . (lambda (e c i) "dummy"))
  616. (paragraph . (lambda (p c i) c))
  617. (section . (lambda (s c i) c))))
  618. nil nil nil '(:with-entities t)))))
  619. (should
  620. (equal "\\alpha\n"
  621. (org-test-with-temp-text "\\alpha"
  622. (org-export-as
  623. (org-export-create-backend
  624. :transcoders '((entity . (lambda (e c i) "dummy"))
  625. (paragraph . (lambda (p c i) c))
  626. (section . (lambda (s c i) c))))
  627. nil nil nil '(:with-entities nil)))))
  628. ;; Emphasis.
  629. (should
  630. (equal "dummy\n"
  631. (org-test-with-temp-text "*bold*"
  632. (org-export-as
  633. (org-export-create-backend
  634. :transcoders '((bold . (lambda (b c i) "dummy"))
  635. (paragraph . (lambda (p c i) c))
  636. (section . (lambda (s c i) c))))
  637. nil nil nil '(:with-emphasize t)))))
  638. (should
  639. (equal "*bold*\n"
  640. (org-test-with-temp-text "*bold*"
  641. (org-export-as
  642. (org-export-create-backend
  643. :transcoders '((bold . (lambda (b c i) "dummy"))
  644. (paragraph . (lambda (p c i) c))
  645. (section . (lambda (s c i) c))))
  646. nil nil nil '(:with-emphasize nil)))))
  647. ;; LaTeX environment.
  648. (should
  649. (equal "dummy\n"
  650. (org-test-with-temp-text "\\begin{equation}\n1+1=2\n\\end{equation}"
  651. (org-export-as
  652. (org-export-create-backend
  653. :transcoders '((latex-environment . (lambda (l c i) "dummy"))
  654. (section . (lambda (s c i) c))))
  655. nil nil nil '(:with-latex t)))))
  656. (should
  657. (equal "\\begin{equation}\n1+1=2\n\\end{equation}\n"
  658. (org-test-with-temp-text "\\begin{equation}\n1+1=2\n\\end{equation}"
  659. (org-export-as
  660. (org-export-create-backend
  661. :transcoders '((latex-environment . (lambda (l c i) "dummy"))
  662. (section . (lambda (s c i) c))))
  663. nil nil nil '(:with-latex verbatim)))))
  664. ;; LaTeX fragment.
  665. (should
  666. (equal "dummy\n"
  667. (org-test-with-temp-text "$1$"
  668. (org-export-as
  669. (org-export-create-backend
  670. :transcoders '((latex-fragment . (lambda (l c i) "dummy"))
  671. (paragraph . (lambda (p c i) c))
  672. (section . (lambda (s c i) c))))
  673. nil nil nil '(:with-latex t)))))
  674. (should
  675. (equal "$1$\n"
  676. (org-test-with-temp-text "$1$"
  677. (org-export-as
  678. (org-export-create-backend
  679. :transcoders '((latex-fragment . (lambda (l c i) "dummy"))
  680. (paragraph . (lambda (p c i) c))
  681. (section . (lambda (s c i) c))))
  682. nil nil nil '(:with-latex verbatim)))))
  683. ;; Sub/superscript.
  684. (should
  685. (equal "adummy\n"
  686. (org-test-with-temp-text "a_b"
  687. (org-export-as
  688. (org-export-create-backend
  689. :transcoders '((subscript . (lambda (s c i) "dummy"))
  690. (paragraph . (lambda (p c i) c))
  691. (section . (lambda (s c i) c))))
  692. nil nil nil '(:with-sub-superscript t)))))
  693. (should
  694. (equal "a_b\n"
  695. (org-test-with-temp-text "a_b"
  696. (org-export-as
  697. (org-export-create-backend
  698. :transcoders '((subscript . (lambda (s c i) "dummy"))
  699. (paragraph . (lambda (p c i) c))
  700. (section . (lambda (s c i) c))))
  701. nil nil nil '(:with-sub-superscript nil)))))
  702. (should
  703. (equal "a_b\n"
  704. (org-test-with-temp-text "a_b"
  705. (org-export-as
  706. (org-export-create-backend
  707. :transcoders '((subscript . (lambda (s c i) "dummy"))
  708. (paragraph . (lambda (p c i) c))
  709. (section . (lambda (s c i) c))))
  710. nil nil nil '(:with-sub-superscript {})))))
  711. (should
  712. (equal "adummy\n"
  713. (org-test-with-temp-text "a_{b}"
  714. (org-export-as
  715. (org-export-create-backend
  716. :transcoders '((subscript . (lambda (s c i) "dummy"))
  717. (paragraph . (lambda (p c i) c))
  718. (section . (lambda (s c i) c))))
  719. nil nil nil '(:with-sub-superscript {})))))
  720. ;; Also handle uninterpreted objects in title.
  721. (should
  722. (equal "a_b"
  723. (org-test-with-temp-text "#+TITLE: a_b"
  724. (org-export-as
  725. (org-export-create-backend
  726. :transcoders
  727. '((subscript . (lambda (s c i) "dummy"))
  728. (template . (lambda (c i) (org-export-data
  729. (plist-get i :title) i)))
  730. (section . (lambda (s c i) c))))
  731. nil nil nil '(:with-sub-superscript nil)))))
  732. ;; Special case: multiples uninterpreted objects in a row.
  733. (should
  734. (equal "a_b_c_d\n"
  735. (org-test-with-temp-text "a_b_c_d"
  736. (org-export-as
  737. (org-export-create-backend
  738. :transcoders '((subscript . (lambda (s c i) "dummy"))
  739. (paragraph . (lambda (p c i) c))
  740. (section . (lambda (s c i) c))))
  741. nil nil nil '(:with-sub-superscript {}))))))
  742. (ert-deftest test-org-export/export-scope ()
  743. "Test all export scopes."
  744. (org-test-with-temp-text "
  745. * Head1
  746. ** Head2
  747. text
  748. *** Head3"
  749. ;; Subtree.
  750. (forward-line 3)
  751. (should (equal (org-export-as (org-test-default-backend) 'subtree)
  752. "text\n*** Head3\n"))
  753. ;; Visible.
  754. (goto-char (point-min))
  755. (forward-line)
  756. (org-cycle)
  757. (should (equal (org-export-as (org-test-default-backend) nil 'visible)
  758. "* Head1\n"))
  759. ;; Region.
  760. (goto-char (point-min))
  761. (forward-line 3)
  762. (transient-mark-mode 1)
  763. (push-mark (point) t t)
  764. (goto-char (point-at-eol))
  765. (should (equal (org-export-as (org-test-default-backend)) "text\n")))
  766. ;; Subtree with a code block calling another block outside.
  767. (let ((org-export-babel-evaluate t))
  768. (should
  769. (equal ": 3\n"
  770. (org-test-with-temp-text "
  771. * Head1
  772. #+BEGIN_SRC emacs-lisp :noweb yes :exports results
  773. <<test>>
  774. #+END_SRC
  775. * Head2
  776. #+NAME: test
  777. #+BEGIN_SRC emacs-lisp
  778. \(+ 1 2)
  779. #+END_SRC"
  780. (forward-line 1)
  781. (org-export-as (org-test-default-backend) 'subtree)))))
  782. ;; Body only.
  783. (let ((backend (org-test-default-backend)))
  784. (setf (org-export-backend-transcoders backend)
  785. (cons '(template . (lambda (body i)
  786. (format "BEGIN\n%sEND" body)))
  787. (org-export-backend-transcoders backend)))
  788. (org-test-with-temp-text "Text"
  789. (should (equal (org-export-as backend nil nil 'body-only)
  790. "Text\n"))
  791. (should (equal (org-export-as backend) "BEGIN\nText\nEND")))))
  792. (ert-deftest test-org-export/output-file-name ()
  793. "Test `org-export-output-file-name' specifications."
  794. ;; Export from a file: name is built from original file name.
  795. (should
  796. (org-test-with-temp-text-in-file "Test"
  797. (equal (concat (file-name-as-directory ".")
  798. (file-name-nondirectory
  799. (file-name-sans-extension (buffer-file-name))))
  800. (file-name-sans-extension (org-export-output-file-name ".ext")))))
  801. ;; When exporting to subtree, check EXPORT_FILE_NAME property first.
  802. (should
  803. (org-test-with-temp-text-in-file
  804. "* Test\n :PROPERTIES:\n :EXPORT_FILE_NAME: test\n :END:"
  805. (equal (org-export-output-file-name ".ext" t) "./test.ext")))
  806. ;; From a buffer not associated to a file, too.
  807. (should
  808. (org-test-with-temp-text
  809. "* Test\n :PROPERTIES:\n :EXPORT_FILE_NAME: test\n :END:"
  810. (equal (org-export-output-file-name ".ext" t) "./test.ext")))
  811. ;; When provided name is absolute, preserve it.
  812. (should
  813. (org-test-with-temp-text
  814. (format "* Test\n :PROPERTIES:\n :EXPORT_FILE_NAME: %s\n :END:"
  815. (expand-file-name "test"))
  816. (file-name-absolute-p (org-export-output-file-name ".ext" t))))
  817. ;; When PUB-DIR argument is provided, use it.
  818. (should
  819. (org-test-with-temp-text-in-file "Test"
  820. (equal (file-name-directory
  821. (org-export-output-file-name ".ext" nil "dir/"))
  822. "dir/")))
  823. ;; When returned name would overwrite original file, add EXTENSION
  824. ;; another time.
  825. (should
  826. (org-test-at-id "75282ba2-f77a-4309-a970-e87c149fe125"
  827. (equal (org-export-output-file-name ".org") "./normal.org.org"))))
  828. (ert-deftest test-org-export/expand-include ()
  829. "Test file inclusion in an Org buffer."
  830. ;; Error when file isn't specified.
  831. (should-error
  832. (org-test-with-temp-text "#+INCLUDE: dummy.org"
  833. (org-export-expand-include-keyword)))
  834. ;; Full insertion with recursive inclusion.
  835. (should
  836. (equal
  837. (with-temp-buffer
  838. (insert-file
  839. (expand-file-name "examples/include.org" org-test-dir))
  840. (replace-regexp-in-string
  841. (regexp-quote "#+INCLUDE: \"include2.org\"")
  842. "Success!" (buffer-string)))
  843. (org-test-with-temp-text
  844. (format "#+INCLUDE: \"%s/examples/include.org\"" org-test-dir)
  845. (org-export-expand-include-keyword)
  846. (buffer-string))))
  847. ;; Localized insertion.
  848. (org-test-with-temp-text
  849. (format "#+INCLUDE: \"%s/examples/include.org\" :lines \"1-2\""
  850. org-test-dir)
  851. (org-export-expand-include-keyword)
  852. (should (equal (buffer-string)
  853. "Small Org file with an include keyword.\n")))
  854. ;; Insertion with constraints on headlines level.
  855. (should
  856. (equal
  857. "* Top heading\n** Heading\nbody\n"
  858. (org-test-with-temp-text
  859. (format
  860. "* Top heading\n#+INCLUDE: \"%s/examples/include.org\" :lines \"9-11\""
  861. org-test-dir)
  862. (org-export-expand-include-keyword)
  863. (buffer-string))))
  864. (should
  865. (equal
  866. "* Top heading\n* Heading\nbody\n"
  867. (org-test-with-temp-text
  868. (format
  869. "* Top heading\n#+INCLUDE: \"%s/examples/include.org\" :lines \"9-11\" :minlevel 1"
  870. org-test-dir)
  871. (org-export-expand-include-keyword)
  872. (buffer-string))))
  873. ;; Inclusion within an example block.
  874. (should
  875. (equal
  876. "#+BEGIN_EXAMPLE\nSmall Org file with an include keyword.\n#+END_EXAMPLE\n"
  877. (org-test-with-temp-text
  878. (format "#+INCLUDE: \"%s/examples/include.org\" :lines \"1-2\" EXAMPLE"
  879. org-test-dir)
  880. (org-export-expand-include-keyword)
  881. (buffer-string))))
  882. ;; Inclusion within a src-block.
  883. (should
  884. (equal
  885. "#+BEGIN_SRC emacs-lisp\n(+ 2 1)\n#+END_SRC\n"
  886. (org-test-with-temp-text
  887. (format
  888. "#+INCLUDE: \"%s/examples/include.org\" :lines \"4-5\" SRC emacs-lisp"
  889. org-test-dir)
  890. (org-export-expand-include-keyword)
  891. (buffer-string))))
  892. ;; Inclusion within an html export-block.
  893. (should
  894. (equal
  895. "#+BEGIN_HTML\n<p>HTML!</p>\n#+END_HTML\n"
  896. (org-test-with-temp-text
  897. (format
  898. "#+INCLUDE: \"%s/examples/include.html\" HTML"
  899. org-test-dir)
  900. (org-export-expand-include-keyword)
  901. (buffer-string))))
  902. ;; Inclusion within an center paragraph
  903. (should
  904. (equal
  905. "#+BEGIN_CENTER\nSuccess!\n#+END_CENTER\n"
  906. (org-test-with-temp-text
  907. (format
  908. "#+INCLUDE: \"%s/examples/include2.org\" CENTER"
  909. org-test-dir)
  910. (org-export-expand-include-keyword)
  911. (buffer-string))))
  912. ;; Footnotes labels are local to each included file.
  913. (should
  914. (= 6
  915. (length
  916. (delete-dups
  917. (let ((contents "
  918. Footnotes[fn:1], [fn:test] and [fn:inline:anonymous footnote].
  919. \[fn:1] Footnote 1
  920. \[fn:test] Footnote \"test\""))
  921. (org-test-with-temp-text-in-file contents
  922. (let ((file1 (buffer-file-name)))
  923. (org-test-with-temp-text-in-file contents
  924. (let ((file2 (buffer-file-name)))
  925. (org-test-with-temp-text
  926. (format "#+INCLUDE: \"%s\"\n#+INCLUDE: \"%s\""
  927. file1 file2)
  928. (org-export-expand-include-keyword)
  929. (org-element-map (org-element-parse-buffer)
  930. 'footnote-reference
  931. (lambda (ref)
  932. (org-element-property :label ref)))))))))))))
  933. ;; Footnotes labels are not local to each include keyword.
  934. (should
  935. (= 4
  936. (length
  937. (delete-dups
  938. (let ((contents "
  939. Footnotes[fn:1], [fn:test], [2] and [fn:inline:anonymous footnote].
  940. \[fn:1] Footnote 1
  941. \[2] Footnote 2
  942. \[fn:test] Footnote \"test\""))
  943. (org-test-with-temp-text-in-file contents
  944. (let ((file (buffer-file-name)))
  945. (org-test-with-temp-text
  946. (format "#+INCLUDE: \"%s\"\n#+INCLUDE: \"%s\"" file file)
  947. (org-export-expand-include-keyword)
  948. (org-element-map (org-element-parse-buffer)
  949. 'footnote-reference
  950. (lambda (ref) (org-element-property :label ref)))))))))))
  951. ;; Footnotes are supported by :lines-like elements and unnecessary
  952. ;; footnotes are dropped.
  953. (should
  954. (= 4
  955. (length
  956. (delete-dups
  957. (let ((contents "
  958. * foo
  959. Footnotes[fn:1]
  960. * bar
  961. Footnotes[fn:2], foot[fn:test], digit only[3], and [fn:inline:anonymous footnote]
  962. \[fn:1] Footnote 1
  963. \[fn:2] Footnote 1
  964. * Footnotes
  965. \[fn:test] Footnote \"test\"
  966. \[3] Footnote 3
  967. "))
  968. (org-test-with-temp-text-in-file contents
  969. (let ((file (buffer-file-name)))
  970. (org-test-with-temp-text
  971. (format "#+INCLUDE: \"%s::*bar\"
  972. " file)
  973. (org-export-expand-include-keyword)
  974. (org-element-map (org-element-parse-buffer)
  975. 'footnote-definition
  976. (lambda (ref) (org-element-property :label ref)))))))))))
  977. ;; If only-contents is non-nil only include contents of element.
  978. (should
  979. (equal
  980. "body\n"
  981. (org-test-with-temp-text
  982. (concat
  983. (format "#+INCLUDE: \"%s/examples/include.org::*Heading\" " org-test-dir)
  984. ":only-contents t")
  985. (org-export-expand-include-keyword)
  986. (buffer-string))))
  987. ;; Headings can be included via CUSTOM_ID.
  988. (should
  989. (org-test-with-temp-text
  990. (format "#+INCLUDE: \"%s/examples/include.org::#ah\"" org-test-dir)
  991. (org-export-expand-include-keyword)
  992. (goto-char (point-min))
  993. (looking-at "* Another heading")))
  994. ;; Named objects can be included.
  995. (should
  996. (equal
  997. "| 1 |\n"
  998. (org-test-with-temp-text
  999. (format "#+INCLUDE: \"%s/examples/include.org::tbl\" :only-contents t" org-test-dir)
  1000. (org-export-expand-include-keyword)
  1001. (buffer-string))))
  1002. ;; Including non-existing elements should result in an error.
  1003. (should-error
  1004. (org-test-with-temp-text
  1005. (format "#+INCLUDE: \"%s/examples/include.org::*non-existing heading\"" org-test-dir)
  1006. (org-export-expand-include-keyword)))
  1007. ;; Lines work relatively to an included element.
  1008. (should
  1009. (equal
  1010. "2\n3\n"
  1011. (org-test-with-temp-text
  1012. (format "#+INCLUDE: \"%s/examples/include.org::#ah\" :only-contents t :lines \"2-3\"" org-test-dir)
  1013. (org-export-expand-include-keyword)
  1014. (buffer-string))))
  1015. ;; Properties should be dropped from headlines.
  1016. (should
  1017. (equal
  1018. (org-test-with-temp-text
  1019. (format "#+INCLUDE: \"%s/examples/include.org::#ht\" :only-contents t" org-test-dir)
  1020. (org-export-expand-include-keyword)
  1021. (buffer-string))
  1022. (org-test-with-temp-text
  1023. (format "#+INCLUDE: \"%s/examples/include.org::tbl\"" org-test-dir)
  1024. (org-export-expand-include-keyword)
  1025. (buffer-string))))
  1026. ;; Properties should be dropped, drawers should not be.
  1027. (should
  1028. (equal
  1029. ":LOGBOOK:\ndrawer\n:END:\ncontent\n"
  1030. (org-test-with-temp-text
  1031. (format "#+INCLUDE: \"%s/examples/include.org::#dh\" :only-contents t" org-test-dir)
  1032. (org-export-expand-include-keyword)
  1033. (buffer-string))))
  1034. ;; Adjacent INCLUDE-keywords should have the same :minlevel if unspecified.
  1035. (should
  1036. (org-every (lambda (level) (zerop (1- level)))
  1037. (org-test-with-temp-text
  1038. (concat
  1039. (format "#+INCLUDE: \"%s/examples/include.org::#ah\"\n" org-test-dir)
  1040. (format "#+INCLUDE: \"%s/examples/include.org::*Heading\"" org-test-dir))
  1041. (org-export-expand-include-keyword)
  1042. (org-element-map (org-element-parse-buffer) 'headline
  1043. (lambda (head) (org-element-property :level head))))))
  1044. ;; INCLUDE does not insert induced :minlevel for src-blocks.
  1045. (should-not
  1046. (equal
  1047. (org-test-with-temp-text
  1048. (format "#+INCLUDE: \"%s/examples/include2.org\" src emacs-lisp" org-test-dir)
  1049. (org-export-expand-include-keyword)
  1050. (buffer-string))
  1051. (org-test-with-temp-text
  1052. (format "#+INCLUDE: \"%s/examples/include2.org\" src emacs-lisp :minlevel 1" org-test-dir)
  1053. (org-export-expand-include-keyword)
  1054. (buffer-string))))
  1055. ;; INCLUDE assigns the relative :minlevel conditional on narrowing.
  1056. (should
  1057. (org-test-with-temp-text-in-file
  1058. (format "* h1\n<point>#+INCLUDE: \"%s/examples/include.org::#ah\"" org-test-dir)
  1059. (narrow-to-region (point) (point-max))
  1060. (org-export-expand-include-keyword)
  1061. (eq 1 (org-current-level))))
  1062. ;; If :minlevel is present do not alter it.
  1063. (should
  1064. (org-test-with-temp-text
  1065. (format "* h1\n<point>#+INCLUDE: \"%s/examples/include.org::#ah\" :minlevel 3" org-test-dir)
  1066. (narrow-to-region (point) (point-max))
  1067. (org-export-expand-include-keyword)
  1068. (eq 3 (org-current-level)))))
  1069. (ert-deftest test-org-export/expand-macro ()
  1070. "Test macro expansion in an Org buffer."
  1071. ;; Standard macro expansion.
  1072. (should
  1073. (equal "#+MACRO: macro1 value\nvalue\n"
  1074. (org-test-with-temp-text "#+MACRO: macro1 value\n{{{macro1}}}"
  1075. (org-export-as (org-test-default-backend)))))
  1076. ;; Allow macro in parsed keywords and associated properties.
  1077. ;; Standard macro expansion.
  1078. (should
  1079. (string-match
  1080. "#\\+K: value"
  1081. (let ((backend (org-export-create-backend
  1082. :parent 'org
  1083. :options '((:k "K" nil nil parse)))))
  1084. (org-test-with-temp-text "#+MACRO: macro value\n#+K: {{{macro}}}"
  1085. (org-export-as backend)))))
  1086. (should
  1087. (string-match
  1088. ":EXPORT_K: v"
  1089. (let ((backend (org-export-create-backend
  1090. :parent 'org
  1091. :options '((:k "K" nil nil parse)))))
  1092. (org-test-with-temp-text
  1093. "#+MACRO: m v\n* H\n:PROPERTIES:\n:EXPORT_K: {{{m}}}\n:END:"
  1094. (org-export-as backend nil nil nil '(:with-properties t))))))
  1095. ;; Expand specific macros.
  1096. (should
  1097. (equal "me 2012-03-29 me@here Title\n"
  1098. (org-test-with-temp-text
  1099. "
  1100. #+TITLE: Title
  1101. #+DATE: 2012-03-29
  1102. #+AUTHOR: me
  1103. #+EMAIL: me@here
  1104. {{{author}}} {{{date}}} {{{email}}} {{{title}}}"
  1105. (let ((output (org-export-as (org-test-default-backend))))
  1106. (substring output (string-match ".*\n\\'" output))))))
  1107. ;; Expand specific macros when property contained a regular macro
  1108. ;; already.
  1109. (should
  1110. (equal "value\n"
  1111. (org-test-with-temp-text "
  1112. #+MACRO: macro1 value
  1113. #+TITLE: {{{macro1}}}
  1114. {{{title}}}"
  1115. (let ((output (org-export-as (org-test-default-backend))))
  1116. (substring output (string-match ".*\n\\'" output))))))
  1117. ;; Expand macros with templates in included files.
  1118. (should
  1119. (equal "success\n"
  1120. (org-test-with-temp-text
  1121. (format "#+INCLUDE: \"%s/examples/macro-templates.org\"
  1122. {{{included-macro}}}" org-test-dir)
  1123. (let ((output (org-export-as (org-test-default-backend))))
  1124. (substring output (string-match ".*\n\\'" output))))))
  1125. ;; Date macro takes a optional formatting argument
  1126. (should
  1127. (equal "09-02-15\n"
  1128. (org-test-with-temp-text "{{{date(%d-%m-%y)}}}\n* d :noexport:\n#+DATE: <2015-02-09>"
  1129. (org-export-as (org-test-default-backend)))))
  1130. ;; Only single timestamps are formatted
  1131. (should
  1132. (equal "<2015-02x-09>\n"
  1133. (org-test-with-temp-text "{{{date(%d-%m-%y)}}}\n* d :noexport:\n#+DATE: <2015-02x-09>"
  1134. (org-export-as (org-test-default-backend)))))
  1135. ;; Throw an error when a macro definition is missing.
  1136. (should-error
  1137. (org-test-with-temp-text "{{{missing}}}"
  1138. (org-export-as (org-test-default-backend))))
  1139. ;; Macros defined in commented subtrees are ignored.
  1140. (should-error
  1141. (org-test-with-temp-text
  1142. "* COMMENT H\n#+MACRO: macro1\n* H2\nvalue\n{{{macro1}}}"
  1143. (org-export-as (org-test-default-backend))))
  1144. (should-error
  1145. (org-test-with-temp-text
  1146. "* COMMENT H\n** H2\n#+MACRO: macro1\n* H3\nvalue\n{{{macro1}}}"
  1147. (org-export-as (org-test-default-backend)))))
  1148. (ert-deftest test-org-export/before-processing-hook ()
  1149. "Test `org-export-before-processing-hook'."
  1150. (should
  1151. (equal
  1152. "#+MACRO: mac val\nTest\n"
  1153. (org-test-with-temp-text "#+MACRO: mac val\n{{{mac}}} Test"
  1154. (let ((org-export-before-processing-hook
  1155. '((lambda (backend)
  1156. (while (re-search-forward "{{{" nil t)
  1157. (let ((object (org-element-context)))
  1158. (when (eq (org-element-type object) 'macro)
  1159. (delete-region
  1160. (org-element-property :begin object)
  1161. (org-element-property :end object)))))))))
  1162. (org-export-as (org-test-default-backend)))))))
  1163. (ert-deftest test-org-export/before-parsing-hook ()
  1164. "Test `org-export-before-parsing-hook'."
  1165. (should
  1166. (equal "Body 1\nBody 2\n"
  1167. (org-test-with-temp-text "* Headline 1\nBody 1\n* Headline 2\nBody 2"
  1168. (let ((org-export-before-parsing-hook
  1169. '((lambda (backend)
  1170. (goto-char (point-min))
  1171. (while (re-search-forward org-outline-regexp-bol nil t)
  1172. (delete-region
  1173. (point-at-bol) (progn (forward-line) (point))))))))
  1174. (org-export-as (org-test-default-backend)))))))
  1175. ;;; Affiliated Keywords
  1176. (ert-deftest test-org-export/read-attribute ()
  1177. "Test `org-export-read-attribute' specifications."
  1178. ;; Standard test.
  1179. (should
  1180. (equal
  1181. (org-export-read-attribute
  1182. :attr_html
  1183. (org-test-with-temp-text "#+ATTR_HTML: :a 1 :b 2\nParagraph"
  1184. (org-element-at-point)))
  1185. '(:a "1" :b "2")))
  1186. ;; Return nil on empty attribute.
  1187. (should-not
  1188. (org-export-read-attribute
  1189. :attr_html
  1190. (org-test-with-temp-text "Paragraph" (org-element-at-point))))
  1191. ;; Return nil on "nil" string.
  1192. (should
  1193. (equal '(:a nil :b nil)
  1194. (org-export-read-attribute
  1195. :attr_html
  1196. (org-test-with-temp-text "#+ATTR_HTML: :a nil :b nil\nParagraph"
  1197. (org-element-at-point)))))
  1198. ;; Return nil on empty string.
  1199. (should
  1200. (equal '(:a nil :b nil)
  1201. (org-export-read-attribute
  1202. :attr_html
  1203. (org-test-with-temp-text "#+ATTR_HTML: :a :b\nParagraph"
  1204. (org-element-at-point)))))
  1205. ;; Return empty string when value is "".
  1206. (should
  1207. (equal '(:a "")
  1208. (org-export-read-attribute
  1209. :attr_html
  1210. (org-test-with-temp-text "#+ATTR_HTML: :a \"\"\nParagraph"
  1211. (org-element-at-point)))))
  1212. ;; Return \"\" when value is """".
  1213. (should
  1214. (equal '(:a "\"\"")
  1215. (org-export-read-attribute
  1216. :attr_html
  1217. (org-test-with-temp-text "#+ATTR_HTML: :a \"\"\"\"\nParagraph"
  1218. (org-element-at-point)))))
  1219. ;; Ignore text before first property.
  1220. (should-not
  1221. (member "ignore"
  1222. (org-export-read-attribute
  1223. :attr_html
  1224. (org-test-with-temp-text "#+ATTR_HTML: ignore :a 1\nParagraph"
  1225. (org-element-at-point))))))
  1226. (ert-deftest test-org-export/get-caption ()
  1227. "Test `org-export-get-caption' specifications."
  1228. ;; Without optional argument, return long caption
  1229. (should
  1230. (equal
  1231. '("l")
  1232. (org-test-with-temp-text "#+CAPTION[s]: l\nPara"
  1233. (org-export-get-caption (org-element-at-point)))))
  1234. ;; With optional argument, return short caption.
  1235. (should
  1236. (equal
  1237. '("s")
  1238. (org-test-with-temp-text "#+CAPTION[s]: l\nPara"
  1239. (org-export-get-caption (org-element-at-point) t))))
  1240. ;; Multiple lines are separated by white spaces.
  1241. (should
  1242. (equal
  1243. '("a" " " "b")
  1244. (org-test-with-temp-text "#+CAPTION: a\n#+CAPTION: b\nPara"
  1245. (org-export-get-caption (org-element-at-point))))))
  1246. ;;; Back-End Tools
  1247. (ert-deftest test-org-export/define-backend ()
  1248. "Test back-end definition and accessors."
  1249. ;; Translate table.
  1250. (should
  1251. (equal '((headline . my-headline-test))
  1252. (let (org-export--registered-backends)
  1253. (org-export-define-backend 'test '((headline . my-headline-test)))
  1254. (org-export-get-all-transcoders 'test))))
  1255. ;; Filters.
  1256. (should
  1257. (equal '((:filter-headline . my-filter))
  1258. (let (org-export--registered-backends)
  1259. (org-export-define-backend 'test
  1260. '((headline . my-headline-test))
  1261. :filters-alist '((:filter-headline . my-filter)))
  1262. (org-export-backend-filters (org-export-get-backend 'test)))))
  1263. ;; Options.
  1264. (should
  1265. (equal '((:prop value))
  1266. (let (org-export--registered-backends)
  1267. (org-export-define-backend 'test
  1268. '((headline . my-headline-test))
  1269. :options-alist '((:prop value)))
  1270. (org-export-backend-options (org-export-get-backend 'test)))))
  1271. ;; Menu.
  1272. (should
  1273. (equal '(?k "Test Export" test)
  1274. (let (org-export--registered-backends)
  1275. (org-export-define-backend 'test
  1276. '((headline . my-headline-test))
  1277. :menu-entry '(?k "Test Export" test))
  1278. (org-export-backend-menu (org-export-get-backend 'test)))))
  1279. ;; Export Blocks.
  1280. (should
  1281. (equal '(("TEST" . org-element-export-block-parser))
  1282. (let (org-export--registered-backends org-element-block-name-alist)
  1283. (org-export-define-backend 'test
  1284. '((headline . my-headline-test))
  1285. :export-block '("test"))
  1286. org-element-block-name-alist))))
  1287. (ert-deftest test-org-export/define-derived-backend ()
  1288. "Test `org-export-define-derived-backend' specifications."
  1289. ;; Error when parent back-end is not defined.
  1290. (should-error
  1291. (let (org-export--registered-backends)
  1292. (org-export-define-derived-backend 'test 'parent)))
  1293. ;; Append translation table to parent's.
  1294. (should
  1295. (equal '((:headline . test) (:headline . parent))
  1296. (let (org-export--registered-backends)
  1297. (org-export-define-backend 'parent '((:headline . parent)))
  1298. (org-export-define-derived-backend 'test 'parent
  1299. :translate-alist '((:headline . test)))
  1300. (org-export-get-all-transcoders 'test))))
  1301. ;; Options defined in the new back have priority over those defined
  1302. ;; in parent.
  1303. (should
  1304. (eq 'test
  1305. (let (org-export--registered-backends)
  1306. (org-export-define-backend 'parent
  1307. '((:headline . parent))
  1308. :options-alist '((:a nil nil 'parent)))
  1309. (org-export-define-derived-backend 'test 'parent
  1310. :options-alist '((:a nil nil 'test)))
  1311. (plist-get (org-export--get-global-options
  1312. (org-export-get-backend 'test))
  1313. :a)))))
  1314. (ert-deftest test-org-export/derived-backend-p ()
  1315. "Test `org-export-derived-backend-p' specifications."
  1316. ;; Non-nil with direct match.
  1317. (should
  1318. (let (org-export--registered-backends)
  1319. (org-export-define-backend 'test '((headline . test)))
  1320. (org-export-derived-backend-p 'test 'test)))
  1321. (should
  1322. (let (org-export--registered-backends)
  1323. (org-export-define-backend 'test '((headline . test)))
  1324. (org-export-define-derived-backend 'test2 'test)
  1325. (org-export-derived-backend-p 'test2 'test2)))
  1326. ;; Non-nil with a direct parent.
  1327. (should
  1328. (let (org-export--registered-backends)
  1329. (org-export-define-backend 'test '((headline . test)))
  1330. (org-export-define-derived-backend 'test2 'test)
  1331. (org-export-derived-backend-p 'test2 'test)))
  1332. ;; Non-nil with an indirect parent.
  1333. (should
  1334. (let (org-export--registered-backends)
  1335. (org-export-define-backend 'test '((headline . test)))
  1336. (org-export-define-derived-backend 'test2 'test)
  1337. (org-export-define-derived-backend 'test3 'test2)
  1338. (org-export-derived-backend-p 'test3 'test)))
  1339. ;; Nil otherwise.
  1340. (should-not
  1341. (let (org-export--registered-backends)
  1342. (org-export-define-backend 'test '((headline . test)))
  1343. (org-export-define-backend 'test2 '((headline . test2)))
  1344. (org-export-derived-backend-p 'test2 'test)))
  1345. (should-not
  1346. (let (org-export--registered-backends)
  1347. (org-export-define-backend 'test '((headline . test)))
  1348. (org-export-define-backend 'test2 '((headline . test2)))
  1349. (org-export-define-derived-backend 'test3 'test2)
  1350. (org-export-derived-backend-p 'test3 'test))))
  1351. (ert-deftest test-org-export/get-all-transcoders ()
  1352. "Test `org-export-get-all-transcoders' specifications."
  1353. ;; Return nil when back-end cannot be found.
  1354. (should-not (org-export-get-all-transcoders nil))
  1355. ;; Same as `org-export-transcoders' if no parent.
  1356. (should
  1357. (equal '((headline . ignore))
  1358. (org-export-get-all-transcoders
  1359. (org-export-create-backend
  1360. :transcoders '((headline . ignore))))))
  1361. ;; But inherit from all ancestors whenever possible.
  1362. (should
  1363. (equal '((section . ignore) (headline . ignore))
  1364. (let (org-export--registered-backends)
  1365. (org-export-define-backend 'b1 '((headline . ignore)))
  1366. (org-export-get-all-transcoders
  1367. (org-export-create-backend
  1368. :parent 'b1 :transcoders '((section . ignore)))))))
  1369. (should
  1370. (equal '((paragraph . ignore) (section . ignore) (headline . ignore))
  1371. (let (org-export--registered-backends)
  1372. (org-export-define-backend 'b1 '((headline . ignore)))
  1373. (org-export-define-derived-backend 'b2 'b1
  1374. :translate-alist '((section . ignore)))
  1375. (org-export-get-all-transcoders
  1376. (org-export-create-backend
  1377. :parent 'b2 :transcoders '((paragraph . ignore)))))))
  1378. ;; Back-end transcoders overrule inherited ones.
  1379. (should
  1380. (eq 'b
  1381. (let (org-export--registered-backends)
  1382. (org-export-define-backend 'b1 '((headline . a)))
  1383. (cdr (assq 'headline
  1384. (org-export-get-all-transcoders
  1385. (org-export-create-backend
  1386. :parent 'b1 :transcoders '((headline . b))))))))))
  1387. (ert-deftest test-org-export/get-all-options ()
  1388. "Test `org-export-get-all-options' specifications."
  1389. ;; Return nil when back-end cannot be found.
  1390. (should-not (org-export-get-all-options nil))
  1391. ;; Same as `org-export-options' if no parent.
  1392. (should
  1393. (equal '((headline . ignore))
  1394. (org-export-get-all-options
  1395. (org-export-create-backend
  1396. :options '((headline . ignore))))))
  1397. ;; But inherit from all ancestors whenever possible.
  1398. (should
  1399. (equal '((:key2 value2) (:key1 value1))
  1400. (let (org-export--registered-backends)
  1401. (org-export-define-backend 'b1 nil :options-alist '((:key1 value1)))
  1402. (org-export-get-all-options
  1403. (org-export-create-backend
  1404. :parent 'b1 :options '((:key2 value2)))))))
  1405. (should
  1406. (equal '((:key3 value3) (:key2 value2) (:key1 value1))
  1407. (let (org-export--registered-backends)
  1408. (org-export-define-backend 'b1 nil :options-alist '((:key1 value1)))
  1409. (org-export-define-derived-backend 'b2 'b1
  1410. :options-alist '((:key2 value2)))
  1411. (org-export-get-all-options
  1412. (org-export-create-backend
  1413. :parent 'b2 :options '((:key3 value3)))))))
  1414. ;; Back-end options overrule inherited ones.
  1415. (should
  1416. (eq 'b
  1417. (let (org-export--registered-backends)
  1418. (org-export-define-backend 'b1 nil :options-alist '((:key1 . a)))
  1419. (cdr (assq :key1
  1420. (org-export-get-all-options
  1421. (org-export-create-backend
  1422. :parent 'b1 :options '((:key1 . b))))))))))
  1423. (ert-deftest test-org-export/get-all-filters ()
  1424. "Test `org-export-get-all-filters' specifications."
  1425. ;; Return nil when back-end cannot be found.
  1426. (should-not (org-export-get-all-filters nil))
  1427. ;; Same as `org-export-filters' if no parent.
  1428. (should
  1429. (equal '((:filter-headline . ignore))
  1430. (org-export-get-all-filters
  1431. (org-export-create-backend
  1432. :filters '((:filter-headline . ignore))))))
  1433. ;; But inherit from all ancestors whenever possible.
  1434. (should
  1435. (equal '((:filter-section . ignore) (:filter-headline . ignore))
  1436. (let (org-export--registered-backends)
  1437. (org-export-define-backend 'b1
  1438. nil :filters-alist '((:filter-headline . ignore)))
  1439. (org-export-get-all-filters
  1440. (org-export-create-backend
  1441. :parent 'b1 :filters '((:filter-section . ignore)))))))
  1442. (should
  1443. (equal '((:filter-paragraph . ignore)
  1444. (:filter-section . ignore)
  1445. (:filter-headline . ignore))
  1446. (let (org-export--registered-backends)
  1447. (org-export-define-backend 'b1
  1448. nil :filters-alist '((:filter-headline . ignore)))
  1449. (org-export-define-derived-backend 'b2 'b1
  1450. :filters-alist '((:filter-section . ignore)))
  1451. (org-export-get-all-filters
  1452. (org-export-create-backend
  1453. :parent 'b2 :filters '((:filter-paragraph . ignore)))))))
  1454. ;; Back-end filters overrule inherited ones.
  1455. (should
  1456. (eq 'b
  1457. (let (org-export--registered-backends)
  1458. (org-export-define-backend 'b1 '((:filter-headline . a)))
  1459. (cdr (assq :filter-headline
  1460. (org-export-get-all-filters
  1461. (org-export-create-backend
  1462. :parent 'b1 :filters '((:filter-headline . b))))))))))
  1463. (ert-deftest test-org-export/with-backend ()
  1464. "Test `org-export-with-backend' definition."
  1465. ;; Error when calling an undefined back-end
  1466. (should-error (org-export-with-backend nil "Test"))
  1467. ;; Error when called back-end doesn't have an appropriate
  1468. ;; transcoder.
  1469. (should-error
  1470. (org-export-with-backend
  1471. (org-export-create-backend :transcoders '((headline . ignore)))
  1472. "Test"))
  1473. ;; Otherwise, export using correct transcoder
  1474. (should
  1475. (equal "Success"
  1476. (let (org-export--registered-backends)
  1477. (org-export-define-backend 'test
  1478. '((plain-text . (lambda (text contents info) "Failure"))))
  1479. (org-export-define-backend 'test2
  1480. '((plain-text . (lambda (text contents info) "Success"))))
  1481. (org-export-with-backend 'test2 "Test"))))
  1482. ;; Provide correct back-end if transcoder needs to use recursive
  1483. ;; calls anyway.
  1484. (should
  1485. (equal "Success\n"
  1486. (let ((test-back-end
  1487. (org-export-create-backend
  1488. :transcoders
  1489. '((headline . (lambda (headline contents info)
  1490. (org-export-data
  1491. (org-element-property :title headline)
  1492. info)))
  1493. (plain-text . (lambda (text info) "Success"))))))
  1494. (org-export-string-as
  1495. "* Test"
  1496. (org-export-create-backend
  1497. :transcoders
  1498. '((headline . (lambda (headline contents info)
  1499. (org-export-with-backend
  1500. test-back-end headline contents info))))))))))
  1501. (ert-deftest test-org-export/data-with-backend ()
  1502. "Test `org-export-data-with-backend' specifications."
  1503. ;; Error when calling an undefined back-end.
  1504. (should-error (org-export-data-with-backend nil "nil" nil))
  1505. ;; Otherwise, export data recursively, using correct back-end.
  1506. (should
  1507. (equal
  1508. "Success!"
  1509. (org-export-data-with-backend
  1510. '(bold nil "Test")
  1511. (org-export-create-backend
  1512. :transcoders
  1513. '((plain-text . (lambda (text info) "Success"))
  1514. (bold . (lambda (bold contents info) (concat contents "!")))))
  1515. '(:with-emphasize t)))))
  1516. ;;; Export Snippets
  1517. (ert-deftest test-org-export/export-snippet ()
  1518. "Test export snippets transcoding."
  1519. ;; Standard test.
  1520. (org-test-with-temp-text "@@test:A@@@@t:B@@"
  1521. (let ((backend (org-test-default-backend)))
  1522. (setf (org-export-backend-name backend) 'test)
  1523. (setf (org-export-backend-transcoders backend)
  1524. (cons (cons 'export-snippet
  1525. (lambda (snippet contents info)
  1526. (when (eq (org-export-snippet-backend snippet) 'test)
  1527. (org-element-property :value snippet))))
  1528. (org-export-backend-transcoders backend)))
  1529. (let ((org-export-snippet-translation-alist nil))
  1530. (should (equal (org-export-as backend) "A\n")))
  1531. (let ((org-export-snippet-translation-alist '(("t" . "test"))))
  1532. (should (equal (org-export-as backend) "AB\n")))))
  1533. ;; Ignored export snippets do not remove any blank.
  1534. (should
  1535. (equal "begin end\n"
  1536. (org-test-with-parsed-data "begin @@test:A@@ end"
  1537. (org-export-data-with-backend
  1538. tree
  1539. (org-export-create-backend
  1540. :transcoders
  1541. '((paragraph . (lambda (paragraph contents info) contents))
  1542. (section . (lambda (section contents info) contents))))
  1543. info)))))
  1544. ;;; Footnotes
  1545. (ert-deftest test-org-export/footnote-first-reference-p ()
  1546. "Test `org-export-footnote-first-reference-p' specifications."
  1547. (should
  1548. (equal
  1549. '(t nil)
  1550. (org-test-with-temp-text "Text[fn:1][fn:1]\n\n[fn:1] Definition"
  1551. (let (result)
  1552. (org-export-as
  1553. (org-export-create-backend
  1554. :transcoders
  1555. `(,(cons 'footnote-reference
  1556. (lambda (f c i)
  1557. (push (org-export-footnote-first-reference-p f info)
  1558. result)
  1559. ""))
  1560. (section . (lambda (s c i) c))
  1561. (paragraph . (lambda (p c i) c))))
  1562. nil nil nil '(:with-footnotes t))
  1563. (nreverse result)))))
  1564. ;; Limit check to DATA, when non-nil.
  1565. (should
  1566. (equal
  1567. '(nil t)
  1568. (org-test-with-parsed-data "Text[fn:1]\n* H\nText[fn:1]\n\n[fn:1] D1"
  1569. (let (result)
  1570. (org-element-map tree 'footnote-reference
  1571. (lambda (ref)
  1572. (push
  1573. (org-export-footnote-first-reference-p
  1574. ref info (org-element-map tree 'headline #'identity info t))
  1575. result))
  1576. info)
  1577. (nreverse result)))))
  1578. (should
  1579. (equal
  1580. '(t nil)
  1581. (org-test-with-parsed-data "Text[fn:1]\n* H\nText[fn:1]\n\n[fn:1] D1"
  1582. (let (result)
  1583. (org-element-map tree 'footnote-reference
  1584. (lambda (ref)
  1585. (push (org-export-footnote-first-reference-p ref info) result))
  1586. info)
  1587. (nreverse result)))))
  1588. ;; If optional argument BODY-FIRST is non-nil, first find footnote
  1589. ;; in the main body of the document. Otherwise, enter footnote
  1590. ;; definitions when they are encountered.
  1591. (should
  1592. (equal
  1593. '(t nil)
  1594. (org-test-with-temp-text
  1595. ":BODY:\nText[fn:1][fn:2]\n:END:\n\n[fn:1] Definition[fn:2]\n\n[fn:2] Inner"
  1596. (let (result)
  1597. (org-export-as
  1598. (org-export-create-backend
  1599. :transcoders
  1600. `(,(cons 'footnote-reference
  1601. (lambda (f c i)
  1602. (when (org-element-lineage f '(drawer))
  1603. (push (org-export-footnote-first-reference-p f info nil)
  1604. result))
  1605. ""))
  1606. (drawer . (lambda (d c i) c))
  1607. (footnote-definition . (lambda (d c i) c))
  1608. (section . (lambda (s c i) c))
  1609. (paragraph . (lambda (p c i) c))))
  1610. nil nil nil '(:with-footnotes t))
  1611. (nreverse result)))))
  1612. (should
  1613. (equal
  1614. '(t t)
  1615. (org-test-with-temp-text
  1616. ":BODY:\nText[fn:1][fn:2]\n:END:\n\n[fn:1] Definition[fn:2]\n\n[fn:2] Inner"
  1617. (let (result)
  1618. (org-export-as
  1619. (org-export-create-backend
  1620. :transcoders
  1621. `(,(cons 'footnote-reference
  1622. (lambda (f c i)
  1623. (when (org-element-lineage f '(drawer))
  1624. (push (org-export-footnote-first-reference-p f info nil t)
  1625. result))
  1626. ""))
  1627. (drawer . (lambda (d c i) c))
  1628. (footnote-definition . (lambda (d c i) c))
  1629. (section . (lambda (s c i) c))
  1630. (paragraph . (lambda (p c i) c))))
  1631. nil nil nil '(:with-footnotes t))
  1632. (nreverse result))))))
  1633. (ert-deftest test-org-export/get-footnote-number ()
  1634. "Test `org-export-get-footnote-number' specifications."
  1635. (should
  1636. (equal '(1 2 1)
  1637. (org-test-with-parsed-data
  1638. "Text[fn:1][fn:2][fn:1]\n\n[fn:1] Def\n[fn:2] Def"
  1639. (org-element-map tree 'footnote-reference
  1640. (lambda (ref) (org-export-get-footnote-number ref info))
  1641. info))))
  1642. ;; Anonymous footnotes all get a new number.
  1643. (should
  1644. (equal '(1 2)
  1645. (org-test-with-parsed-data
  1646. "Text[fn::anon1][fn::anon2]"
  1647. (org-element-map tree 'footnote-reference
  1648. (lambda (ref) (org-export-get-footnote-number ref info))
  1649. info))))
  1650. ;; Test nested footnotes order.
  1651. (should
  1652. (equal
  1653. '((1 . "fn:1") (2 . "fn:2") (3 . "fn:3") (3 . "fn:3") (4))
  1654. (org-test-with-parsed-data
  1655. "Text[fn:1:A[fn:2]] [fn:3].\n\n[fn:2] B [fn:3] [fn::D].\n\n[fn:3] C."
  1656. (org-element-map tree 'footnote-reference
  1657. (lambda (ref)
  1658. (cons (org-export-get-footnote-number ref info)
  1659. (org-element-property :label ref)))
  1660. info))))
  1661. ;; Limit number to provided DATA, when non-nil.
  1662. (should
  1663. (equal
  1664. '(1)
  1665. (org-test-with-parsed-data
  1666. "Text[fn:1]\n* H\nText[fn:2]\n\n[fn:1] D1\n[fn:2] D2"
  1667. (org-element-map tree 'footnote-reference
  1668. (lambda (ref)
  1669. (org-export-get-footnote-number
  1670. ref info (org-element-map tree 'headline #'identity info t)))
  1671. info))))
  1672. (should
  1673. (equal
  1674. '(1 2)
  1675. (org-test-with-parsed-data
  1676. "Text[fn:1]\n* H\nText[fn:2]\n\n[fn:1] D1\n[fn:2]"
  1677. (org-element-map tree 'footnote-reference
  1678. (lambda (ref) (org-export-get-footnote-number ref info))
  1679. info))))
  1680. ;; With a non-nil BODY-FIRST optional argument, first check body,
  1681. ;; then footnote definitions.
  1682. (should
  1683. (equal
  1684. '(("fn:1" . 1) ("fn:2" . 2) ("fn:3" . 3) ("fn:3" . 3))
  1685. (org-test-with-parsed-data
  1686. "Text[fn:1][fn:2][fn:3]\n\n[fn:1] Def[fn:3]\n[fn:2] Def\n[fn:3] Def"
  1687. (org-element-map tree 'footnote-reference
  1688. (lambda (ref)
  1689. (cons (org-element-property :label ref)
  1690. (org-export-get-footnote-number ref info nil t)))
  1691. info))))
  1692. (should
  1693. (equal
  1694. '(("fn:1" . 1) ("fn:2" . 3) ("fn:3" . 2) ("fn:3" . 2))
  1695. (org-test-with-parsed-data
  1696. "Text[fn:1][fn:2][fn:3]\n\n[fn:1] Def[fn:3]\n[fn:2] Def\n[fn:3] Def"
  1697. (org-element-map tree 'footnote-reference
  1698. (lambda (ref)
  1699. (cons (org-element-property :label ref)
  1700. (org-export-get-footnote-number ref info nil)))
  1701. info)))))
  1702. (ert-deftest test-org-export/collect-footnote-definitions ()
  1703. "Test `org-export-collect-footnote-definitions' specifications."
  1704. (should
  1705. (= 4
  1706. (org-test-with-parsed-data "Text[fn:1:A[fn:2]] [fn:3].
  1707. \[fn:2] B [fn:3] [fn::D].
  1708. \[fn:3] C."
  1709. (length (org-export-collect-footnote-definitions info)))))
  1710. ;; Limit number to provided DATA, when non-nil.
  1711. (should
  1712. (equal
  1713. '((1 "fn:2"))
  1714. (org-test-with-parsed-data
  1715. "Text[fn:1]\n* H\nText[fn:2]\n\n[fn:1] D1\n[fn:2] D2"
  1716. (mapcar #'butlast
  1717. (org-export-collect-footnote-definitions
  1718. info (org-element-map tree 'headline #'identity info t))))))
  1719. (should
  1720. (equal
  1721. '((1 "fn:1") (2 "fn:2"))
  1722. (org-test-with-parsed-data
  1723. "Text[fn:1]\n* H\nText[fn:2]\n\n[fn:1] D1\n[fn:2] D2"
  1724. (mapcar #'butlast (org-export-collect-footnote-definitions info)))))
  1725. ;; With optional argument BODY-FIRST, first check body, then
  1726. ;; footnote definitions.
  1727. (should
  1728. (equal '("fn:1" "fn:3" "fn:2" nil)
  1729. (org-test-with-parsed-data "Text[fn:1:A[fn:2]] [fn:3].
  1730. \[fn:2] B [fn:3] [fn::D].
  1731. \[fn:3] C."
  1732. (mapcar (lambda (e) (nth 1 e))
  1733. (org-export-collect-footnote-definitions info nil t)))))
  1734. (should-not
  1735. (equal '("fn:1" "fn:3" "fn:2" nil)
  1736. (org-test-with-parsed-data "Text[fn:1:A[fn:2]] [fn:3].
  1737. \[fn:2] B [fn:3] [fn::D].
  1738. \[fn:3] C."
  1739. (mapcar (lambda (e) (nth 1 e))
  1740. (org-export-collect-footnote-definitions info))))))
  1741. (ert-deftest test-org-export/footnotes ()
  1742. "Miscellaneous tests on footnotes."
  1743. (let ((org-footnote-section nil)
  1744. (org-export-with-footnotes t))
  1745. ;; Read every type of footnote.
  1746. (should
  1747. (equal
  1748. '((1 . "A\n") (2 . "B") (3 . "C") (4 . "D"))
  1749. (org-test-with-parsed-data
  1750. "Text[fn:1] [1] [fn:label:C] [fn::D]\n\n[fn:1] A\n\n[1] B"
  1751. (org-element-map tree 'footnote-reference
  1752. (lambda (ref)
  1753. (let ((def (org-export-get-footnote-definition ref info)))
  1754. (cons (org-export-get-footnote-number ref info)
  1755. (if (eq (org-element-property :type ref) 'inline) (car def)
  1756. (car (org-element-contents
  1757. (car (org-element-contents def))))))))
  1758. info))))
  1759. ;; Test nested footnote in invisible definitions.
  1760. (org-test-with-temp-text "Text[1]\n\n[1] B [2]\n\n[2] C."
  1761. ;; Hide definitions.
  1762. (narrow-to-region (point) (point-at-eol))
  1763. (let* ((tree (org-element-parse-buffer))
  1764. (info (org-combine-plists
  1765. `(:parse-tree ,tree)
  1766. (org-export-collect-tree-properties
  1767. tree (org-export-get-environment)))))
  1768. ;; Both footnotes should be seen.
  1769. (should
  1770. (= (length (org-export-collect-footnote-definitions info)) 2))))
  1771. ;; Test export of footnotes defined outside parsing scope.
  1772. (should
  1773. (equal
  1774. "ParagraphOut of scope\n"
  1775. (org-test-with-temp-text "[fn:1] Out of scope
  1776. * Title
  1777. Paragraph[fn:1]"
  1778. (let ((backend (org-test-default-backend)))
  1779. (setf (org-export-backend-transcoders backend)
  1780. (cons (cons 'footnote-reference
  1781. (lambda (fn contents info)
  1782. (org-element-interpret-data
  1783. (org-export-get-footnote-definition fn info))))
  1784. (org-export-backend-transcoders backend)))
  1785. (forward-line)
  1786. (org-export-as backend 'subtree)))))
  1787. ;; Footnotes without a definition should throw an error.
  1788. (should-error
  1789. (org-test-with-parsed-data "Text[fn:1]"
  1790. (org-export-get-footnote-definition
  1791. (org-element-map tree 'footnote-reference 'identity info t) info)))
  1792. ;; Footnote section should be ignored in TOC and in headlines
  1793. ;; numbering.
  1794. (should
  1795. (= 1 (let ((org-footnote-section "Footnotes"))
  1796. (length (org-test-with-parsed-data "* H1\n* Footnotes\n"
  1797. (org-export-collect-headlines info))))))
  1798. (should
  1799. (equal '(2)
  1800. (let ((org-footnote-section "Footnotes"))
  1801. (org-test-with-parsed-data "* H1\n* Footnotes\n* H2"
  1802. (org-element-map tree 'headline
  1803. (lambda (hl)
  1804. (when (equal (org-element-property :raw-value hl) "H2")
  1805. (org-export-get-headline-number hl info)))
  1806. info t)))))))
  1807. ;;; Headlines and Inlinetasks
  1808. (ert-deftest test-org-export/get-relative-level ()
  1809. "Test `org-export-get-relative-level' specifications."
  1810. ;; Standard test.
  1811. (should
  1812. (equal '(1 2)
  1813. (let ((org-odd-levels-only nil))
  1814. (org-test-with-parsed-data "* Headline 1\n** Headline 2"
  1815. (org-element-map tree 'headline
  1816. (lambda (h) (org-export-get-relative-level h info))
  1817. info)))))
  1818. ;; Missing levels
  1819. (should
  1820. (equal '(1 3)
  1821. (let ((org-odd-levels-only nil))
  1822. (org-test-with-parsed-data "** Headline 1\n**** Headline 2"
  1823. (org-element-map tree 'headline
  1824. (lambda (h) (org-export-get-relative-level h info))
  1825. info))))))
  1826. (ert-deftest test-org-export/low-level-p ()
  1827. "Test `org-export-low-level-p' specifications."
  1828. (should
  1829. (equal
  1830. '(no yes)
  1831. (let ((org-odd-levels-only nil))
  1832. (org-test-with-parsed-data "* Headline 1\n** Headline 2"
  1833. (org-element-map tree 'headline
  1834. (lambda (h) (if (org-export-low-level-p h info) 'yes 'no))
  1835. (plist-put info :headline-levels 1)))))))
  1836. (ert-deftest test-org-export/get-headline-number ()
  1837. "Test `org-export-get-headline-number' specifications."
  1838. ;; Standard test.
  1839. (should
  1840. (equal
  1841. '((1) (1 1))
  1842. (let ((org-odd-levels-only nil))
  1843. (org-test-with-parsed-data "* Headline 1\n** Headline 2"
  1844. (org-element-map tree 'headline
  1845. (lambda (h) (org-export-get-headline-number h info))
  1846. info)))))
  1847. ;; Missing levels are replaced with 0.
  1848. (should
  1849. (equal
  1850. '((1) (1 0 1))
  1851. (let ((org-odd-levels-only nil))
  1852. (org-test-with-parsed-data "* Headline 1\n*** Headline 2"
  1853. (org-element-map tree 'headline
  1854. (lambda (h) (org-export-get-headline-number h info))
  1855. info))))))
  1856. (ert-deftest test-org-export/numbered-headline-p ()
  1857. "Test `org-export-numbered-headline-p' specifications."
  1858. ;; If `:section-numbers' is nil, never number headlines.
  1859. (should-not
  1860. (org-test-with-parsed-data "* Headline"
  1861. (org-element-map tree 'headline
  1862. (lambda (h) (org-export-numbered-headline-p h info))
  1863. (plist-put info :section-numbers nil))))
  1864. ;; If `:section-numbers' is a number, only number headlines with
  1865. ;; a level greater that it.
  1866. (should
  1867. (equal
  1868. '(yes no)
  1869. (org-test-with-parsed-data "* Headline 1\n** Headline 2"
  1870. (org-element-map tree 'headline
  1871. (lambda (h) (if (org-export-numbered-headline-p h info) 'yes 'no))
  1872. (plist-put info :section-numbers 1)))))
  1873. ;; Otherwise, headlines are always numbered.
  1874. (should
  1875. (org-test-with-parsed-data "* Headline"
  1876. (org-element-map tree 'headline
  1877. (lambda (h) (org-export-numbered-headline-p h info))
  1878. (plist-put info :section-numbers t))))
  1879. ;; With #+OPTIONS: num:nil all headlines are unnumbered.
  1880. (should-not
  1881. (org-test-with-parsed-data "* H\n#+OPTIONS: num:nil"
  1882. (org-export-numbered-headline-p
  1883. (org-element-map tree 'headline 'identity info t)
  1884. info)))
  1885. ;; Headlines with a non-nil UNNUMBERED property are not numbered.
  1886. (should-not
  1887. (org-test-with-parsed-data "* H\n:PROPERTIES:\n:UNNUMBERED: t\n:END:"
  1888. (org-export-numbered-headline-p
  1889. (org-element-map tree 'headline #'identity info t)
  1890. info)))
  1891. ;; UNNUMBERED ignores inheritance. Any non-nil value among
  1892. ;; ancestors disables numbering.
  1893. (should
  1894. (org-test-with-parsed-data
  1895. "* H
  1896. :PROPERTIES:
  1897. :UNNUMBERED: t
  1898. :END:
  1899. ** H2
  1900. :PROPERTIES:
  1901. :UNNUMBERED: nil
  1902. :END:
  1903. *** H3"
  1904. (org-every
  1905. (lambda (h) (not (org-export-numbered-headline-p h info)))
  1906. (org-element-map tree 'headline #'identity info)))))
  1907. (ert-deftest test-org-export/number-to-roman ()
  1908. "Test `org-export-number-to-roman' specifications."
  1909. ;; If number is negative, return it as a string.
  1910. (should (equal (org-export-number-to-roman -1) "-1"))
  1911. ;; Otherwise, return it as a roman number.
  1912. (should (equal (org-export-number-to-roman 1449) "MCDXLIX")))
  1913. (ert-deftest test-org-export/get-optional-title ()
  1914. "Test `org-export-get-alt-title' specifications."
  1915. ;; If ALT_TITLE property is defined, use it.
  1916. (should
  1917. (equal '("opt")
  1918. (org-test-with-parsed-data
  1919. "* Headline\n:PROPERTIES:\n:ALT_TITLE: opt\n:END:"
  1920. (org-export-get-alt-title
  1921. (org-element-map tree 'headline 'identity info t)
  1922. info))))
  1923. ;; Otherwise, fall-back to regular title.
  1924. (should
  1925. (equal '("Headline")
  1926. (org-test-with-parsed-data "* Headline"
  1927. (org-export-get-alt-title
  1928. (org-element-map tree 'headline 'identity info t)
  1929. info)))))
  1930. (ert-deftest test-org-export/get-tags ()
  1931. "Test `org-export-get-tags' specifications."
  1932. (let ((org-export-exclude-tags '("noexport"))
  1933. (org-export-select-tags '("export")))
  1934. ;; Standard test: tags which are not a select tag, an exclude tag,
  1935. ;; or specified as optional argument shouldn't be ignored.
  1936. (should
  1937. (org-test-with-parsed-data "* Headline :tag:"
  1938. (org-export-get-tags (org-element-map tree 'headline 'identity info t)
  1939. info)))
  1940. ;; Exclude tags are removed.
  1941. (should-not
  1942. (org-test-with-parsed-data "* Headline :noexport:"
  1943. (org-export-get-tags (org-element-map tree 'headline 'identity info t)
  1944. info)))
  1945. ;; Select tags are removed.
  1946. (should-not
  1947. (org-test-with-parsed-data "* Headline :export:"
  1948. (org-export-get-tags (org-element-map tree 'headline 'identity info t)
  1949. info)))
  1950. (should
  1951. (equal
  1952. '("tag")
  1953. (org-test-with-parsed-data "* Headline :tag:export:"
  1954. (org-export-get-tags (org-element-map tree 'headline 'identity info t)
  1955. info))))
  1956. ;; Tags provided in the optional argument are also ignored.
  1957. (should-not
  1958. (org-test-with-parsed-data "* Headline :ignore:"
  1959. (org-export-get-tags (org-element-map tree 'headline 'identity info t)
  1960. info '("ignore"))))
  1961. ;; Allow tag inheritance.
  1962. (should
  1963. (equal
  1964. '(("tag") ("tag"))
  1965. (org-test-with-parsed-data "* Headline :tag:\n** Sub-heading"
  1966. (org-element-map tree 'headline
  1967. (lambda (hl) (org-export-get-tags hl info nil t)) info))))
  1968. ;; Tag inheritance checks FILETAGS keywords.
  1969. (should
  1970. (equal
  1971. '(("a" "b" "tag"))
  1972. (org-test-with-parsed-data "#+FILETAGS: :a:b:\n* Headline :tag:"
  1973. (org-element-map tree 'headline
  1974. (lambda (hl) (org-export-get-tags hl info nil t)) info))))))
  1975. (ert-deftest test-org-export/get-node-property ()
  1976. "Test`org-export-get-node-property' specifications."
  1977. ;; Standard test.
  1978. (should
  1979. (equal "value"
  1980. (org-test-with-parsed-data "* Headline
  1981. :PROPERTIES:
  1982. :prop: value
  1983. :END:"
  1984. (org-export-get-node-property
  1985. :PROP (org-element-map tree 'headline 'identity nil t)))))
  1986. ;; Test inheritance.
  1987. (should
  1988. (equal "value"
  1989. (org-test-with-parsed-data "* Parent
  1990. :PROPERTIES:
  1991. :prop: value
  1992. :END:
  1993. ** Headline
  1994. Paragraph"
  1995. (org-export-get-node-property
  1996. :PROP (org-element-map tree 'paragraph 'identity nil t) t))))
  1997. ;; Cannot return a value before the first headline.
  1998. (should-not
  1999. (org-test-with-parsed-data "Paragraph
  2000. * Headline
  2001. :PROPERTIES:
  2002. :prop: value
  2003. :END:"
  2004. (org-export-get-node-property
  2005. :PROP (org-element-map tree 'paragraph 'identity nil t)))))
  2006. (ert-deftest test-org-export/get-category ()
  2007. "Test `org-export-get-category' specifications."
  2008. ;; Standard test.
  2009. (should
  2010. (equal "value"
  2011. (org-test-with-parsed-data "* Headline
  2012. :PROPERTIES:
  2013. :CATEGORY: value
  2014. :END:"
  2015. (org-export-get-category
  2016. (org-element-map tree 'headline 'identity nil t) info))))
  2017. ;; Test inheritance from a parent headline.
  2018. (should
  2019. (equal '("value" "value")
  2020. (org-test-with-parsed-data "* Headline1
  2021. :PROPERTIES:
  2022. :CATEGORY: value
  2023. :END:
  2024. ** Headline2"
  2025. (org-element-map tree 'headline
  2026. (lambda (hl) (org-export-get-category hl info)) info))))
  2027. ;; Test inheritance from #+CATEGORY keyword
  2028. (should
  2029. (equal "value"
  2030. (org-test-with-parsed-data "#+CATEGORY: value
  2031. * Headline"
  2032. (org-export-get-category
  2033. (org-element-map tree 'headline 'identity nil t) info))))
  2034. ;; Test inheritance from file name.
  2035. (should
  2036. (equal "test"
  2037. (org-test-with-parsed-data "* Headline"
  2038. (let ((info (plist-put info :input-file "~/test.org")))
  2039. (org-export-get-category
  2040. (org-element-map tree 'headline 'identity nil t) info)))))
  2041. ;; Fall-back value.
  2042. (should
  2043. (equal "???"
  2044. (org-test-with-parsed-data "* Headline"
  2045. (org-export-get-category
  2046. (org-element-map tree 'headline 'identity nil t) info)))))
  2047. (ert-deftest test-org-export/first-sibling-p ()
  2048. "Test `org-export-first-sibling-p' specifications."
  2049. ;; Standard test.
  2050. (should
  2051. (equal
  2052. '(yes yes no)
  2053. (org-test-with-parsed-data "* H\n** H 2\n** H 3"
  2054. (org-element-map tree 'headline
  2055. (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
  2056. info))))
  2057. (should
  2058. (equal '(yes no)
  2059. (org-test-with-parsed-data "- item\n\n para"
  2060. (org-element-map tree 'paragraph
  2061. (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
  2062. info))))
  2063. ;; Ignore sections for headlines.
  2064. (should
  2065. (equal '(yes yes)
  2066. (org-test-with-parsed-data "* H\nSection\n** H 2"
  2067. (org-element-map tree 'headline
  2068. (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
  2069. info))))
  2070. ;; Ignore headlines not exported.
  2071. (should
  2072. (equal
  2073. '(yes)
  2074. (let ((org-export-exclude-tags '("ignore")))
  2075. (org-test-with-parsed-data "* Headline :ignore:\n* Headline 2"
  2076. (org-element-map tree 'headline
  2077. (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
  2078. info))))))
  2079. (ert-deftest test-org-export/last-sibling-p ()
  2080. "Test `org-export-last-sibling-p' specifications."
  2081. ;; Standard test.
  2082. (should
  2083. (equal
  2084. '(yes no yes)
  2085. (org-test-with-parsed-data "* Headline\n** Headline 2\n** Headline 3"
  2086. (org-element-map tree 'headline
  2087. (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
  2088. info))))
  2089. (should
  2090. (equal '(no yes)
  2091. (org-test-with-parsed-data "- item\n\n para"
  2092. (org-element-map tree 'paragraph
  2093. (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
  2094. info))))
  2095. ;; Ignore headlines not exported.
  2096. (should
  2097. (equal
  2098. '(yes)
  2099. (let ((org-export-exclude-tags '("ignore")))
  2100. (org-test-with-parsed-data "* Headline\n* Headline 2 :ignore:"
  2101. (org-element-map tree 'headline
  2102. (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
  2103. info))))))
  2104. (ert-deftest test-org-export/handle-inlinetasks ()
  2105. "Test inlinetask export."
  2106. ;; Inlinetask with an exclude tag.
  2107. (when (featurep 'org-inlinetask)
  2108. (should
  2109. (equal
  2110. ""
  2111. (let ((org-inlinetask-min-level 3)
  2112. org-export-filter-body-functions
  2113. org-export-filter-final-output-functions)
  2114. (org-test-with-temp-text "*** Inlinetask :noexp:\nContents\n*** end"
  2115. (org-export-as (org-test-default-backend)
  2116. nil nil nil '(:exclude-tags ("noexp")))))))
  2117. ;; Inlinetask with an include tag.
  2118. (should
  2119. (equal
  2120. "* H2\n*** Inline :exp:\n"
  2121. (let ((org-inlinetask-min-level 3)
  2122. (org-tags-column 0))
  2123. (org-test-with-temp-text "* H1\n* H2\n*** Inline :exp:"
  2124. (org-export-as (org-test-default-backend)
  2125. nil nil nil '(:select-tags ("exp")))))))
  2126. ;; Ignore inlinetask with a TODO keyword and tasks excluded.
  2127. (should
  2128. (equal ""
  2129. (let ((org-todo-keywords '((sequence "TODO" "DONE")))
  2130. (org-inlinetask-min-level 3)
  2131. org-export-filter-body-functions
  2132. org-export-filter-final-output-functions)
  2133. (org-test-with-temp-text "*** TODO Inline"
  2134. (org-export-as (org-test-default-backend)
  2135. nil nil nil '(:with-tasks nil))))))))
  2136. ;;; Keywords
  2137. (ert-deftest test-org-export/get-date ()
  2138. "Test `org-export-get-date' specifications."
  2139. ;; Return a properly formatted string when
  2140. ;; `org-export-date-timestamp-format' is non-nil and DATE keyword
  2141. ;; consists in a single timestamp.
  2142. (should
  2143. (equal "29 03 2012"
  2144. (let ((org-export-date-timestamp-format "%d %m %Y"))
  2145. (org-test-with-parsed-data "#+DATE: <2012-03-29 Thu>"
  2146. (org-export-get-date info)))))
  2147. ;; Return a secondary string otherwise.
  2148. (should-not
  2149. (stringp
  2150. (let ((org-export-date-timestamp-format nil))
  2151. (org-test-with-parsed-data "#+DATE: <2012-03-29 Thu>"
  2152. (org-export-get-date info)))))
  2153. (should
  2154. (equal '("Date")
  2155. (org-test-with-parsed-data "#+DATE: Date"
  2156. (org-export-get-date info))))
  2157. ;; Optional argument has precedence over
  2158. ;; `org-export-date-timestamp-format'.
  2159. (should
  2160. (equal "29 03"
  2161. (let ((org-export-date-timestamp-format "%d %m %Y"))
  2162. (org-test-with-parsed-data "#+DATE: <2012-03-29 Thu>"
  2163. (org-export-get-date info "%d %m"))))))
  2164. ;;; Links
  2165. (ert-deftest test-org-export/custom-protocol-maybe ()
  2166. "Test `org-export-custom-protocol-maybe' specifications."
  2167. (should
  2168. (string-match
  2169. "success"
  2170. (let ((org-link-types (copy-sequence org-link-types)))
  2171. (org-add-link-type "foo" nil (lambda (p d f) "success"))
  2172. (org-export-string-as
  2173. "[[foo:path]]"
  2174. (org-export-create-backend
  2175. :name 'test
  2176. :transcoders
  2177. '((section . (lambda (s c i) c))
  2178. (paragraph . (lambda (p c i) c))
  2179. (link . (lambda (l c i)
  2180. (or (org-export-custom-protocol-maybe l c 'test)
  2181. "failure")))))))))
  2182. (should-not
  2183. (string-match
  2184. "success"
  2185. (let ((org-link-types (copy-sequence org-link-types)))
  2186. (org-add-link-type
  2187. "foo" nil (lambda (p d f) (and (eq f 'test) "success")))
  2188. (org-export-string-as
  2189. "[[foo:path]]"
  2190. (org-export-create-backend
  2191. :name 'no-test
  2192. :transcoders
  2193. '((section . (lambda (s c i) c))
  2194. (paragraph . (lambda (p c i) c))
  2195. (link . (lambda (l c i)
  2196. (or (org-export-custom-protocol-maybe l c 'no-test)
  2197. "failure")))))))))
  2198. ;; Ignore anonymous back-ends.
  2199. (should-not
  2200. (string-match
  2201. "success"
  2202. (let ((org-link-types (copy-sequence org-link-types)))
  2203. (org-add-link-type
  2204. "foo" nil (lambda (p d f) (and (eq f 'test) "success")))
  2205. (org-export-string-as
  2206. "[[foo:path]]"
  2207. (org-export-create-backend
  2208. :transcoders
  2209. '((section . (lambda (s c i) c))
  2210. (paragraph . (lambda (p c i) c))
  2211. (link . (lambda (l c i)
  2212. (or (org-export-custom-protocol-maybe l c nil)
  2213. "failure"))))))))))
  2214. (ert-deftest test-org-export/get-coderef-format ()
  2215. "Test `org-export-get-coderef-format' specifications."
  2216. ;; A link without description returns "%s"
  2217. (should (equal (org-export-get-coderef-format "(ref:line)" nil)
  2218. "%s"))
  2219. ;; Return "%s" when path is matched within description.
  2220. (should (equal (org-export-get-coderef-format "path" "desc (path)")
  2221. "desc %s"))
  2222. ;; Otherwise return description.
  2223. (should (equal (org-export-get-coderef-format "path" "desc")
  2224. "desc")))
  2225. (ert-deftest test-org-export/inline-image-p ()
  2226. "Test `org-export-inline-image-p' specifications."
  2227. (should
  2228. (org-export-inline-image-p
  2229. (org-test-with-temp-text "[[#id]]"
  2230. (org-element-map (org-element-parse-buffer) 'link 'identity nil t))
  2231. '(("custom-id" . "id")))))
  2232. (ert-deftest test-org-export/fuzzy-link ()
  2233. "Test fuzzy links specifications."
  2234. ;; Link to an headline should return headline's number.
  2235. (org-test-with-parsed-data
  2236. "Paragraph.\n* Head1\n* Head2\n* Head3\n[[Head2]]"
  2237. (should
  2238. ;; Note: Headline's number is in fact a list of numbers.
  2239. (equal '(2)
  2240. (org-element-map tree 'link
  2241. (lambda (link)
  2242. (org-export-get-ordinal
  2243. (org-export-resolve-fuzzy-link link info) info)) info t))))
  2244. ;; Link to a target in an item should return item's number.
  2245. (org-test-with-parsed-data
  2246. "- Item1\n - Item11\n - <<test>>Item12\n- Item2\n\n\n[[test]]"
  2247. (should
  2248. ;; Note: Item's number is in fact a list of numbers.
  2249. (equal '(1 2)
  2250. (org-element-map tree 'link
  2251. (lambda (link)
  2252. (org-export-get-ordinal
  2253. (org-export-resolve-fuzzy-link link info) info)) info t))))
  2254. ;; Link to a target in a footnote should return footnote's number.
  2255. (org-test-with-parsed-data "
  2256. Paragraph[1][2][fn:lbl3:C<<target>>][[test]][[target]]\n[1] A\n\n[2] <<test>>B"
  2257. (should
  2258. (equal '(2 3)
  2259. (org-element-map tree 'link
  2260. (lambda (link)
  2261. (org-export-get-ordinal
  2262. (org-export-resolve-fuzzy-link link info) info)) info))))
  2263. ;; Link to a named element should return sequence number of that
  2264. ;; element.
  2265. (org-test-with-parsed-data
  2266. "#+NAME: tbl1\n|1|2|\n#+NAME: tbl2\n|3|4|\n#+NAME: tbl3\n|5|6|\n[[tbl2]]"
  2267. (should
  2268. (= 2
  2269. (org-element-map tree 'link
  2270. (lambda (link)
  2271. (org-export-get-ordinal
  2272. (org-export-resolve-fuzzy-link link info) info)) info t))))
  2273. ;; Link to a target not within an item, a table, a footnote
  2274. ;; reference or definition should return section number.
  2275. (org-test-with-parsed-data
  2276. "* Head1\n* Head2\nParagraph<<target>>\n* Head3\n[[target]]"
  2277. (should
  2278. (equal '(2)
  2279. (org-element-map tree 'link
  2280. (lambda (link)
  2281. (org-export-get-ordinal
  2282. (org-export-resolve-fuzzy-link link info) info)) info t))))
  2283. ;; Space are not significant when matching a fuzzy link.
  2284. (should
  2285. (org-test-with-parsed-data "* Head 1\n[[Head\n 1]]"
  2286. (org-element-map tree 'link
  2287. (lambda (link) (org-export-resolve-fuzzy-link link info))
  2288. info t)))
  2289. ;; Statistics cookies are ignored for headline match.
  2290. (should
  2291. (org-test-with-parsed-data "* Head [0/0]\n[[Head]]"
  2292. (org-element-map tree 'link
  2293. (lambda (link) (org-export-resolve-fuzzy-link link info))
  2294. info t)))
  2295. (should
  2296. (org-test-with-parsed-data "* Head [100%]\n[[Head]]"
  2297. (org-element-map tree 'link
  2298. (lambda (link) (org-export-resolve-fuzzy-link link info))
  2299. info t)))
  2300. ;; Headline match is position dependent.
  2301. (should-not
  2302. (apply
  2303. 'eq
  2304. (org-test-with-parsed-data "* H1\n[[*H1]]\n* H1\n[[*H1]]"
  2305. (org-element-map tree 'link
  2306. (lambda (link) (org-export-resolve-fuzzy-link link info)) info)))))
  2307. (ert-deftest test-org-export/resolve-coderef ()
  2308. "Test `org-export-resolve-coderef' specifications."
  2309. (let ((org-coderef-label-format "(ref:%s)"))
  2310. ;; A link to a "-n -k -r" block returns line number.
  2311. (should
  2312. (= 1
  2313. (org-test-with-parsed-data
  2314. "#+BEGIN_EXAMPLE -n -k -r\nText (ref:coderef)\n#+END_EXAMPLE"
  2315. (org-export-resolve-coderef "coderef" info))))
  2316. (should
  2317. (= 1
  2318. (org-test-with-parsed-data
  2319. "#+BEGIN_SRC emacs-lisp -n -k -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  2320. (org-export-resolve-coderef "coderef" info))))
  2321. ;; A link to a "-n -r" block returns line number.
  2322. (should
  2323. (= 1
  2324. (org-test-with-parsed-data
  2325. "#+BEGIN_EXAMPLE -n -r\nText (ref:coderef)\n#+END_EXAMPLE"
  2326. (org-export-resolve-coderef "coderef" info))))
  2327. (should
  2328. (= 1
  2329. (org-test-with-parsed-data
  2330. "#+BEGIN_SRC emacs-lisp -n -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  2331. (org-export-resolve-coderef "coderef" info))))
  2332. ;; A link to a "-n" block returns coderef.
  2333. (should
  2334. (equal "coderef"
  2335. (org-test-with-parsed-data
  2336. "#+BEGIN_SRC emacs-lisp -n\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  2337. (org-export-resolve-coderef "coderef" info))))
  2338. (should
  2339. (equal "coderef"
  2340. (org-test-with-parsed-data
  2341. "#+BEGIN_EXAMPLE -n\nText (ref:coderef)\n#+END_EXAMPLE"
  2342. (org-export-resolve-coderef "coderef" info))))
  2343. ;; A link to a "-r" block returns line number.
  2344. (should
  2345. (= 1
  2346. (org-test-with-parsed-data
  2347. "#+BEGIN_SRC emacs-lisp -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  2348. (org-export-resolve-coderef "coderef" info))))
  2349. (should
  2350. (= 1
  2351. (org-test-with-parsed-data
  2352. "#+BEGIN_EXAMPLE -r\nText (ref:coderef)\n#+END_EXAMPLE"
  2353. (org-export-resolve-coderef "coderef" info))))
  2354. ;; A link to a block without a switch returns coderef.
  2355. (should
  2356. (equal "coderef"
  2357. (org-test-with-parsed-data
  2358. "#+BEGIN_SRC emacs-lisp\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  2359. (org-export-resolve-coderef "coderef" info))))
  2360. (org-test-with-parsed-data
  2361. "#+BEGIN_EXAMPLE\nText (ref:coderef)\n#+END_EXAMPLE"
  2362. (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
  2363. ;; Correctly handle continued line numbers. A "+n" switch should
  2364. ;; resume numbering from previous block with numbered lines,
  2365. ;; ignoring blocks not numbering lines in the process. A "-n"
  2366. ;; switch resets count.
  2367. (should
  2368. (equal '(2 1)
  2369. (org-test-with-parsed-data "
  2370. #+BEGIN_EXAMPLE -n
  2371. Text.
  2372. #+END_EXAMPLE
  2373. #+BEGIN_SRC emacs-lisp
  2374. \(- 1 1)
  2375. #+END_SRC
  2376. #+BEGIN_SRC emacs-lisp +n -r
  2377. \(+ 1 1) (ref:addition)
  2378. #+END_SRC
  2379. #+BEGIN_EXAMPLE -n -r
  2380. Another text. (ref:text)
  2381. #+END_EXAMPLE"
  2382. (list (org-export-resolve-coderef "addition" info)
  2383. (org-export-resolve-coderef "text" info)))))
  2384. ;; Recognize coderef with user-specified syntax.
  2385. (should
  2386. (equal "text"
  2387. (org-test-with-parsed-data
  2388. "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\nText. [ref:text]\n#+END_EXAMPLE"
  2389. (org-export-resolve-coderef "text" info))))
  2390. ;; Unresolved coderefs throw an error.
  2391. (should-error
  2392. (org-test-with-parsed-data "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  2393. (org-export-resolve-coderef "unknown" info)))))
  2394. (ert-deftest test-org-export/resolve-fuzzy-link ()
  2395. "Test `org-export-resolve-fuzzy-link' specifications."
  2396. ;; Match target objects.
  2397. (should
  2398. (org-test-with-parsed-data "<<target>> [[target]]"
  2399. (org-export-resolve-fuzzy-link
  2400. (org-element-map tree 'link 'identity info t) info)))
  2401. ;; Match named elements.
  2402. (should
  2403. (org-test-with-parsed-data "#+NAME: target\nParagraph\n\n[[target]]"
  2404. (org-export-resolve-fuzzy-link
  2405. (org-element-map tree 'link 'identity info t) info)))
  2406. ;; Match exact headline's name.
  2407. (should
  2408. (org-test-with-parsed-data "* My headline\n[[My headline]]"
  2409. (org-export-resolve-fuzzy-link
  2410. (org-element-map tree 'link 'identity info t) info)))
  2411. ;; Targets objects have priority over named elements and headline
  2412. ;; titles.
  2413. (should
  2414. (eq 'target
  2415. (org-test-with-parsed-data
  2416. "* target\n#+NAME: target\n<<target>>\n\n[[target]]"
  2417. (org-element-type
  2418. (org-export-resolve-fuzzy-link
  2419. (org-element-map tree 'link 'identity info t) info)))))
  2420. ;; Named elements have priority over headline titles.
  2421. (should
  2422. (eq 'paragraph
  2423. (org-test-with-parsed-data
  2424. "* target\n#+NAME: target\nParagraph\n\n[[target]]"
  2425. (org-element-type
  2426. (org-export-resolve-fuzzy-link
  2427. (org-element-map tree 'link 'identity info t) info)))))
  2428. ;; If link's path starts with a "*", only match headline titles,
  2429. ;; though.
  2430. (should
  2431. (eq 'headline
  2432. (org-test-with-parsed-data
  2433. "* target\n#+NAME: target\n<<target>>\n\n[[*target]]"
  2434. (org-element-type
  2435. (org-export-resolve-fuzzy-link
  2436. (org-element-map tree 'link 'identity info t) info)))))
  2437. ;; Error if no match.
  2438. (should-error
  2439. (org-test-with-parsed-data "[[target]]"
  2440. (org-export-resolve-fuzzy-link
  2441. (org-element-map tree 'link 'identity info t) info)))
  2442. ;; Match fuzzy link even when before first headline.
  2443. (should
  2444. (eq 'headline
  2445. (org-test-with-parsed-data "[[hl]]\n* hl"
  2446. (org-element-type
  2447. (org-export-resolve-fuzzy-link
  2448. (org-element-map tree 'link 'identity info t) info))))))
  2449. (ert-deftest test-org-export/resolve-id-link ()
  2450. "Test `org-export-resolve-id-link' specifications."
  2451. ;; Regular test for custom-id link.
  2452. (should
  2453. (equal '("Headline1")
  2454. (org-test-with-parsed-data "* Headline1
  2455. :PROPERTIES:
  2456. :CUSTOM_ID: test
  2457. :END:
  2458. * Headline 2
  2459. \[[#test]]"
  2460. (org-element-property
  2461. :title
  2462. (org-export-resolve-id-link
  2463. (org-element-map tree 'link 'identity info t) info)))))
  2464. ;; Throw an error on failing searches.
  2465. (should-error
  2466. (org-test-with-parsed-data "* Headline1
  2467. :PROPERTIES:
  2468. :CUSTOM_ID: test
  2469. :END:
  2470. * Headline 2
  2471. \[[#no-match]]"
  2472. (org-export-resolve-id-link
  2473. (org-element-map tree 'link 'identity info t) info)))
  2474. ;; Test for internal id target.
  2475. (should
  2476. (equal '("Headline1")
  2477. (org-test-with-parsed-data "* Headline1
  2478. :PROPERTIES:
  2479. :ID: aaaa
  2480. :END:
  2481. * Headline 2
  2482. \[[id:aaaa]]"
  2483. (org-element-property
  2484. :title
  2485. (org-export-resolve-id-link
  2486. (org-element-map tree 'link 'identity info t) info)))))
  2487. ;; Test for external id target.
  2488. (should
  2489. (equal
  2490. "external-file"
  2491. (org-test-with-parsed-data "[[id:aaaa]]"
  2492. (org-export-resolve-id-link
  2493. (org-element-map tree 'link 'identity info t)
  2494. (org-combine-plists info '(:id-alist (("aaaa" . "external-file")))))))))
  2495. (ert-deftest test-org-export/resolve-radio-link ()
  2496. "Test `org-export-resolve-radio-link' specifications."
  2497. ;; Standard test.
  2498. (should
  2499. (org-test-with-temp-text "<<<radio>>> radio"
  2500. (org-update-radio-target-regexp)
  2501. (let* ((tree (org-element-parse-buffer))
  2502. (info `(:parse-tree ,tree)))
  2503. (org-export-resolve-radio-link
  2504. (org-element-map tree 'link 'identity info t)
  2505. info))))
  2506. ;; Radio targets are case-insensitive.
  2507. (should
  2508. (org-test-with-temp-text "<<<RADIO>>> radio"
  2509. (org-update-radio-target-regexp)
  2510. (let* ((tree (org-element-parse-buffer))
  2511. (info `(:parse-tree ,tree)))
  2512. (org-export-resolve-radio-link
  2513. (org-element-map tree 'link 'identity info t)
  2514. info))))
  2515. ;; Radio target with objects.
  2516. (should
  2517. (org-test-with-temp-text "<<<radio \\alpha>>> radio \\alpha"
  2518. (org-update-radio-target-regexp)
  2519. (let* ((tree (org-element-parse-buffer))
  2520. (info `(:parse-tree ,tree)))
  2521. (org-export-resolve-radio-link
  2522. (org-element-map tree 'link 'identity info t)
  2523. info))))
  2524. ;; Radio target with objects at its beginning.
  2525. (should
  2526. (org-test-with-temp-text "<<<\\alpha radio>>> \\alpha radio"
  2527. (org-update-radio-target-regexp)
  2528. (let* ((tree (org-element-parse-buffer))
  2529. (info `(:parse-tree ,tree)))
  2530. (org-export-resolve-radio-link
  2531. (org-element-map tree 'link 'identity info t)
  2532. info))))
  2533. ;; Radio link next to an apostrophe.
  2534. (should
  2535. (org-test-with-temp-text "<<<radio>>> radio's"
  2536. (org-update-radio-target-regexp)
  2537. (let* ((tree (org-element-parse-buffer))
  2538. (info `(:parse-tree ,tree)))
  2539. (org-export-resolve-radio-link
  2540. (org-element-map tree 'link 'identity info t)
  2541. info))))
  2542. ;; Multiple radio targets.
  2543. (should
  2544. (equal '("radio1" "radio2")
  2545. (org-test-with-temp-text "<<<radio1>>> <<<radio2>>> radio1 radio2"
  2546. (org-update-radio-target-regexp)
  2547. (let* ((tree (org-element-parse-buffer))
  2548. (info `(:parse-tree ,tree)))
  2549. (org-element-map tree 'link
  2550. (lambda (link)
  2551. (org-element-property
  2552. :value (org-export-resolve-radio-link link info)))
  2553. info)))))
  2554. ;; Radio target is whitespace insensitive.
  2555. (should
  2556. (org-test-with-temp-text "<<<a radio>>> a\n radio"
  2557. (org-update-radio-target-regexp)
  2558. (let* ((tree (org-element-parse-buffer))
  2559. (info `(:parse-tree ,tree)))
  2560. (org-element-map tree 'link
  2561. (lambda (link) (org-export-resolve-radio-link link info)) info t)))))
  2562. (ert-deftest test-org-export/file-uri ()
  2563. "Test `org-export-file-uri' specifications."
  2564. ;; Preserve relative filenames.
  2565. (should (equal "relative.org" (org-export-file-uri "relative.org")))
  2566. ;; Local files start with "file:///"
  2567. (should (equal "file:///local.org" (org-export-file-uri "/local.org")))
  2568. ;; Remote files start with "file://"
  2569. (should (equal "file://myself@some.where:papers/last.pdf"
  2570. (org-export-file-uri "/myself@some.where:papers/last.pdf")))
  2571. ;; Expand filename starting with "~".
  2572. (should (equal (org-export-file-uri "~/file.org")
  2573. (concat "file://" (expand-file-name "~/file.org")))))
  2574. ;;; Src-block and example-block
  2575. (ert-deftest test-org-export/unravel-code ()
  2576. "Test `org-export-unravel-code' function."
  2577. ;; Code without reference.
  2578. (should
  2579. (equal '("(+ 1 1)\n")
  2580. (org-test-with-temp-text "#+BEGIN_EXAMPLE\n(+ 1 1)\n#+END_EXAMPLE"
  2581. (org-export-unravel-code (org-element-at-point)))))
  2582. ;; Code with reference.
  2583. (should
  2584. (equal '("(+ 1 1)\n" (1 . "test"))
  2585. (org-test-with-temp-text
  2586. "#+BEGIN_EXAMPLE\n(+ 1 1) (ref:test)\n#+END_EXAMPLE"
  2587. (let ((org-coderef-label-format "(ref:%s)"))
  2588. (org-export-unravel-code (org-element-at-point))))))
  2589. ;; Code with user-defined reference.
  2590. (should
  2591. (equal
  2592. '("(+ 1 1)\n" (1 . "test"))
  2593. (org-test-with-temp-text
  2594. "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\n(+ 1 1) [ref:test]\n#+END_EXAMPLE"
  2595. (let ((org-coderef-label-format "(ref:%s)"))
  2596. (org-export-unravel-code (org-element-at-point))))))
  2597. ;; Code references keys are relative to the current block.
  2598. (should
  2599. (equal '("(+ 2 2)\n(+ 3 3)\n" (2 . "one"))
  2600. (org-test-with-temp-text "
  2601. #+BEGIN_EXAMPLE -n
  2602. \(+ 1 1)
  2603. #+END_EXAMPLE
  2604. #+BEGIN_EXAMPLE +n
  2605. \(+ 2 2)
  2606. \(+ 3 3) (ref:one)
  2607. #+END_EXAMPLE"
  2608. (goto-line 5)
  2609. (let ((org-coderef-label-format "(ref:%s)"))
  2610. (org-export-unravel-code (org-element-at-point)))))))
  2611. (ert-deftest test-org-export/format-code-default ()
  2612. "Test `org-export-format-code-default' specifications."
  2613. ;; Return the empty string when code is empty.
  2614. (should
  2615. (equal ""
  2616. (org-test-with-parsed-data "#+BEGIN_SRC emacs-lisp\n\n\n#+END_SRC"
  2617. (org-export-format-code-default
  2618. (org-element-map tree 'src-block 'identity info t) info))))
  2619. ;; Number lines, two whitespace characters before the actual loc.
  2620. (should
  2621. (equal "1 a\n2 b\n"
  2622. (org-test-with-parsed-data
  2623. "#+BEGIN_SRC emacs-lisp +n\na\nb\n#+END_SRC"
  2624. (org-export-format-code-default
  2625. (org-element-map tree 'src-block 'identity info t) info))))
  2626. ;; Put references 6 whitespace characters after the widest line,
  2627. ;; wrapped within parenthesis.
  2628. (should
  2629. (equal "123 (a)\n1 (b)\n"
  2630. (let ((org-coderef-label-format "(ref:%s)"))
  2631. (org-test-with-parsed-data
  2632. "#+BEGIN_SRC emacs-lisp\n123 (ref:a)\n1 (ref:b)\n#+END_SRC"
  2633. (org-export-format-code-default
  2634. (org-element-map tree 'src-block 'identity info t) info))))))
  2635. ;;; Smart Quotes
  2636. (ert-deftest test-org-export/activate-smart-quotes ()
  2637. "Test `org-export-activate-smart-quotes' specifications."
  2638. ;; Double quotes: standard test.
  2639. (should
  2640. (equal
  2641. '("some &ldquo;quoted&rdquo; text")
  2642. (let ((org-export-default-language "en"))
  2643. (org-test-with-parsed-data "some \"quoted\" text"
  2644. (org-element-map tree 'plain-text
  2645. (lambda (s) (org-export-activate-smart-quotes s :html info))
  2646. info)))))
  2647. ;; Opening quotes: at the beginning of a paragraph.
  2648. (should
  2649. (equal
  2650. '("&ldquo;begin")
  2651. (let ((org-export-default-language "en"))
  2652. (org-test-with-parsed-data "\"begin"
  2653. (org-element-map tree 'plain-text
  2654. (lambda (s) (org-export-activate-smart-quotes s :html info))
  2655. info)))))
  2656. ;; Opening quotes: after an object.
  2657. (should
  2658. (equal
  2659. '("&ldquo;quoted&rdquo; text")
  2660. (let ((org-export-default-language "en"))
  2661. (org-test-with-parsed-data "=verb= \"quoted\" text"
  2662. (org-element-map tree 'plain-text
  2663. (lambda (s) (org-export-activate-smart-quotes s :html info))
  2664. info)))))
  2665. ;; Closing quotes: at the end of a paragraph.
  2666. (should
  2667. (equal
  2668. '("Quoted &ldquo;text&rdquo;")
  2669. (let ((org-export-default-language "en"))
  2670. (org-test-with-parsed-data "Quoted \"text\""
  2671. (org-element-map tree 'plain-text
  2672. (lambda (s) (org-export-activate-smart-quotes s :html info))
  2673. info)))))
  2674. ;; Inner quotes: standard test.
  2675. (should
  2676. (equal '("« outer « inner » outer »")
  2677. (let ((org-export-default-language "fr"))
  2678. (org-test-with-parsed-data "\"outer 'inner' outer\""
  2679. (org-element-map tree 'plain-text
  2680. (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
  2681. info)))))
  2682. ;; Apostrophe: standard test.
  2683. (should
  2684. (equal '("It « shouldn’t » fail")
  2685. (let ((org-export-default-language "fr"))
  2686. (org-test-with-parsed-data "It \"shouldn't\" fail"
  2687. (org-element-map tree 'plain-text
  2688. (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
  2689. info)))))
  2690. (should
  2691. (equal '("It shouldn’t fail")
  2692. (let ((org-export-default-language "fr"))
  2693. (org-test-with-parsed-data "It shouldn't fail"
  2694. (org-element-map tree 'plain-text
  2695. (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
  2696. info)))))
  2697. ;; Apostrophe: before an object.
  2698. (should
  2699. (equal
  2700. '("« a’" " »")
  2701. (let ((org-export-default-language "fr"))
  2702. (org-test-with-parsed-data "\"a'=b=\""
  2703. (org-element-map tree 'plain-text
  2704. (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
  2705. info)))))
  2706. ;; Apostrophe: after an object.
  2707. (should
  2708. (equal '("« " "’s »")
  2709. (let ((org-export-default-language "fr"))
  2710. (org-test-with-parsed-data "\"=code='s\""
  2711. (org-element-map tree 'plain-text
  2712. (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
  2713. info)))))
  2714. ;; Special case: isolated quotes.
  2715. (should
  2716. (equal '("&ldquo;" "&rdquo;")
  2717. (let ((org-export-default-language "en"))
  2718. (org-test-with-parsed-data "\"$x$\""
  2719. (org-element-map tree 'plain-text
  2720. (lambda (s) (org-export-activate-smart-quotes s :html info))
  2721. info)))))
  2722. ;; Smart quotes in secondary strings.
  2723. (should
  2724. (equal '("&ldquo;" "&rdquo;")
  2725. (let ((org-export-default-language "en"))
  2726. (org-test-with-parsed-data "* \"$x$\""
  2727. (org-element-map tree 'plain-text
  2728. (lambda (s) (org-export-activate-smart-quotes s :html info))
  2729. info)))))
  2730. ;; Smart quotes in document keywords.
  2731. (should
  2732. (equal '("&ldquo;" "&rdquo;")
  2733. (let ((org-export-default-language "en"))
  2734. (org-test-with-parsed-data "#+TITLE: \"$x$\""
  2735. (org-element-map (plist-get info :title) 'plain-text
  2736. (lambda (s) (org-export-activate-smart-quotes s :html info))
  2737. info)))))
  2738. ;; Smart quotes in parsed affiliated keywords.
  2739. (should
  2740. (equal '("&ldquo;" "&rdquo;" "Paragraph")
  2741. (let ((org-export-default-language "en"))
  2742. (org-test-with-parsed-data "#+CAPTION: \"$x$\"\nParagraph"
  2743. (org-element-map tree 'plain-text
  2744. (lambda (s) (org-export-activate-smart-quotes s :html info))
  2745. info nil nil t))))))
  2746. ;;; Tables
  2747. (ert-deftest test-org-export/special-column ()
  2748. "Test if the table's special column is properly recognized."
  2749. ;; 1. First column is special if it contains only a special marking
  2750. ;; characters or empty cells.
  2751. (org-test-with-temp-text "
  2752. | ! | 1 |
  2753. | | 2 |"
  2754. (should
  2755. (org-export-table-has-special-column-p
  2756. (org-element-map
  2757. (org-element-parse-buffer) 'table 'identity nil 'first-match))))
  2758. ;; 2. If the column contains anything else, it isn't special.
  2759. (org-test-with-temp-text "
  2760. | ! | 1 |
  2761. | b | 2 |"
  2762. (should-not
  2763. (org-export-table-has-special-column-p
  2764. (org-element-map
  2765. (org-element-parse-buffer) 'table 'identity nil 'first-match))))
  2766. ;; 3. Special marking characters are "#", "^", "*", "_", "/", "$"
  2767. ;; and "!".
  2768. (org-test-with-temp-text "
  2769. | # | 1 |
  2770. | ^ | 2 |
  2771. | * | 3 |
  2772. | _ | 4 |
  2773. | / | 5 |
  2774. | $ | 6 |
  2775. | ! | 7 |"
  2776. (should
  2777. (org-export-table-has-special-column-p
  2778. (org-element-map
  2779. (org-element-parse-buffer) 'table 'identity nil 'first-match))))
  2780. ;; 4. A first column with only empty cells isn't considered as
  2781. ;; special.
  2782. (org-test-with-temp-text "
  2783. | | 1 |
  2784. | | 2 |"
  2785. (should-not
  2786. (org-export-table-has-special-column-p
  2787. (org-element-map
  2788. (org-element-parse-buffer) 'table 'identity nil 'first-match)))))
  2789. (ert-deftest test-org-export/table-row-is-special-p ()
  2790. "Test `org-export-table-row-is-special-p' specifications."
  2791. ;; 1. A row is special if it has a special marking character in the
  2792. ;; special column.
  2793. (org-test-with-parsed-data "| ! | 1 |"
  2794. (should
  2795. (org-export-table-row-is-special-p
  2796. (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  2797. ;; 2. A row is special when its first field is "/"
  2798. (org-test-with-parsed-data "
  2799. | / | 1 |
  2800. | a | b |"
  2801. (should
  2802. (org-export-table-row-is-special-p
  2803. (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  2804. ;; 3. A row only containing alignment cookies is also considered as
  2805. ;; special.
  2806. (org-test-with-parsed-data "| <5> | | <l> | <l22> |"
  2807. (should
  2808. (org-export-table-row-is-special-p
  2809. (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  2810. ;; 4. Everything else isn't considered as special.
  2811. (org-test-with-parsed-data "| \alpha | | c |"
  2812. (should-not
  2813. (org-export-table-row-is-special-p
  2814. (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  2815. ;; 5. Table's rules are never considered as special rows.
  2816. (org-test-with-parsed-data "|---+---|"
  2817. (should-not
  2818. (org-export-table-row-is-special-p
  2819. (org-element-map tree 'table-row 'identity nil 'first-match) info))))
  2820. (ert-deftest test-org-export/has-header-p ()
  2821. "Test `org-export-table-has-header-p' specifications."
  2822. ;; 1. With an header.
  2823. (org-test-with-parsed-data "
  2824. | a | b |
  2825. |---+---|
  2826. | c | d |"
  2827. (should
  2828. (org-export-table-has-header-p
  2829. (org-element-map tree 'table 'identity info 'first-match)
  2830. info)))
  2831. ;; 2. Without an header.
  2832. (org-test-with-parsed-data "
  2833. | a | b |
  2834. | c | d |"
  2835. (should-not
  2836. (org-export-table-has-header-p
  2837. (org-element-map tree 'table 'identity info 'first-match)
  2838. info)))
  2839. ;; 3. Don't get fooled with starting and ending rules.
  2840. (org-test-with-parsed-data "
  2841. |---+---|
  2842. | a | b |
  2843. | c | d |
  2844. |---+---|"
  2845. (should-not
  2846. (org-export-table-has-header-p
  2847. (org-element-map tree 'table 'identity info 'first-match)
  2848. info))))
  2849. (ert-deftest test-org-export/table-row-group ()
  2850. "Test `org-export-table-row-group' specifications."
  2851. ;; 1. A rule creates a new group.
  2852. (should
  2853. (equal '(1 rule 2)
  2854. (org-test-with-parsed-data "
  2855. | a | b |
  2856. |---+---|
  2857. | 1 | 2 |"
  2858. (org-element-map tree 'table-row
  2859. (lambda (row)
  2860. (if (eq (org-element-property :type row) 'rule) 'rule
  2861. (org-export-table-row-group row info)))))))
  2862. ;; 2. Special rows are ignored in count.
  2863. (should
  2864. (equal
  2865. '(rule 1)
  2866. (org-test-with-parsed-data "
  2867. | / | < | > |
  2868. |---|---+---|
  2869. | | 1 | 2 |"
  2870. (org-element-map tree 'table-row
  2871. (lambda (row)
  2872. (if (eq (org-element-property :type row) 'rule) 'rule
  2873. (org-export-table-row-group row info)))
  2874. info))))
  2875. ;; 3. Double rules also are ignored in count.
  2876. (should
  2877. (equal '(1 rule rule 2)
  2878. (org-test-with-parsed-data "
  2879. | a | b |
  2880. |---+---|
  2881. |---+---|
  2882. | 1 | 2 |"
  2883. (org-element-map tree 'table-row
  2884. (lambda (row)
  2885. (if (eq (org-element-property :type row) 'rule) 'rule
  2886. (org-export-table-row-group row info))))))))
  2887. (ert-deftest test-org-export/table-row-number ()
  2888. "Test `org-export-table-row-number' specifications."
  2889. ;; Standard test. Number is 0-indexed.
  2890. (should
  2891. (equal '(0 1)
  2892. (org-test-with-parsed-data "| a | b | c |\n| d | e | f |"
  2893. (org-element-map tree 'table-row
  2894. (lambda (row) (org-export-table-row-number row info)) info))))
  2895. ;; Number ignores separators.
  2896. (should
  2897. (equal '(0 1)
  2898. (org-test-with-parsed-data "
  2899. | a | b | c |
  2900. |---+---+---|
  2901. | d | e | f |"
  2902. (org-element-map tree 'table-row
  2903. (lambda (row) (org-export-table-row-number row info)) info))))
  2904. ;; Number ignores special rows.
  2905. (should
  2906. (equal '(0 1)
  2907. (org-test-with-parsed-data "
  2908. | / | < | > |
  2909. | | b | c |
  2910. |---+-----+-----|
  2911. | | <c> | <c> |
  2912. | | e | f |"
  2913. (org-element-map tree 'table-row
  2914. (lambda (row) (org-export-table-row-number row info)) info)))))
  2915. (ert-deftest test-org-export/table-cell-width ()
  2916. "Test `org-export-table-cell-width' specifications."
  2917. ;; 1. Width is primarily determined by width cookies. If no cookie
  2918. ;; is found, cell's width is nil.
  2919. (org-test-with-parsed-data "
  2920. | / | <l> | <6> | <l7> |
  2921. | | a | b | c |"
  2922. (should
  2923. (equal
  2924. '(nil 6 7)
  2925. (mapcar (lambda (cell) (org-export-table-cell-width cell info))
  2926. (org-element-map tree 'table-cell 'identity info)))))
  2927. ;; 2. The last width cookie has precedence.
  2928. (org-test-with-parsed-data "
  2929. | <6> |
  2930. | <7> |
  2931. | a |"
  2932. (should
  2933. (equal
  2934. '(7)
  2935. (mapcar (lambda (cell) (org-export-table-cell-width cell info))
  2936. (org-element-map tree 'table-cell 'identity info)))))
  2937. ;; 3. Valid width cookies must have a specific row.
  2938. (org-test-with-parsed-data "| <6> | cell |"
  2939. (should
  2940. (equal
  2941. '(nil nil)
  2942. (mapcar (lambda (cell) (org-export-table-cell-width cell info))
  2943. (org-element-map tree 'table-cell 'identity))))))
  2944. (ert-deftest test-org-export/table-cell-alignment ()
  2945. "Test `org-export-table-cell-alignment' specifications."
  2946. ;; 1. Alignment is primarily determined by alignment cookies.
  2947. (should
  2948. (equal '(left center right)
  2949. (let ((org-table-number-fraction 0.5)
  2950. (org-table-number-regexp "^[0-9]+$"))
  2951. (org-test-with-parsed-data "| <l> | <c> | <r> |"
  2952. (mapcar (lambda (cell)
  2953. (org-export-table-cell-alignment cell info))
  2954. (org-element-map tree 'table-cell 'identity))))))
  2955. ;; 2. The last alignment cookie has precedence.
  2956. (should
  2957. (equal '(right right right)
  2958. (org-test-with-parsed-data "
  2959. | <l8> |
  2960. | cell |
  2961. | <r9> |"
  2962. (mapcar (lambda (cell) (org-export-table-cell-alignment cell info))
  2963. (org-element-map tree 'table-cell 'identity)))))
  2964. ;; 3. If there's no cookie, cell's contents determine alignment.
  2965. ;; A column mostly made of cells containing numbers will align
  2966. ;; its cells to the right.
  2967. (should
  2968. (equal '(right right right)
  2969. (let ((org-table-number-fraction 0.5)
  2970. (org-table-number-regexp "^[0-9]+$"))
  2971. (org-test-with-parsed-data "
  2972. | 123 |
  2973. | some text |
  2974. | 12345 |"
  2975. (mapcar (lambda (cell)
  2976. (org-export-table-cell-alignment cell info))
  2977. (org-element-map tree 'table-cell 'identity))))))
  2978. ;; 4. Otherwise, they will be aligned to the left.
  2979. (should
  2980. (equal '(left left left)
  2981. (org-test-with-parsed-data "
  2982. | text |
  2983. | some text |
  2984. | \alpha |"
  2985. (mapcar (lambda (cell)
  2986. (org-export-table-cell-alignment cell info))
  2987. (org-element-map tree 'table-cell 'identity info))))))
  2988. (ert-deftest test-org-export/table-cell-borders ()
  2989. "Test `org-export-table-cell-borders' specifications."
  2990. ;; 1. Recognize various column groups indicators.
  2991. (org-test-with-parsed-data "| / | < | > | <> |"
  2992. (should
  2993. (equal
  2994. '((right bottom top) (left bottom top) (right bottom top)
  2995. (right left bottom top))
  2996. (mapcar (lambda (cell)
  2997. (org-export-table-cell-borders cell info))
  2998. (org-element-map tree 'table-cell 'identity)))))
  2999. ;; 2. Accept shortcuts to define column groups.
  3000. (org-test-with-parsed-data "| / | < | < |"
  3001. (should
  3002. (equal
  3003. '((right bottom top) (right left bottom top) (left bottom top))
  3004. (mapcar (lambda (cell)
  3005. (org-export-table-cell-borders cell info))
  3006. (org-element-map tree 'table-cell 'identity)))))
  3007. ;; 3. A valid column groups row must start with a "/".
  3008. (org-test-with-parsed-data "
  3009. | | < |
  3010. | a | b |"
  3011. (should
  3012. (equal '((top) (top) (bottom) (bottom))
  3013. (mapcar (lambda (cell)
  3014. (org-export-table-cell-borders cell info))
  3015. (org-element-map tree 'table-cell 'identity)))))
  3016. ;; 4. Take table rules into consideration.
  3017. (org-test-with-parsed-data "
  3018. | 1 |
  3019. |---|
  3020. | 2 |"
  3021. (should
  3022. (equal '((below top) (bottom above))
  3023. (mapcar (lambda (cell)
  3024. (org-export-table-cell-borders cell info))
  3025. (org-element-map tree 'table-cell 'identity)))))
  3026. ;; 5. Top and (resp. bottom) rules induce both `top' and `above'
  3027. ;; (resp. `bottom' and `below') borders. Any special row is
  3028. ;; ignored.
  3029. (org-test-with-parsed-data "
  3030. |---+----|
  3031. | / | |
  3032. | | 1 |
  3033. |---+----|"
  3034. (should
  3035. (equal '((bottom below top above))
  3036. (last
  3037. (mapcar (lambda (cell)
  3038. (org-export-table-cell-borders cell info))
  3039. (org-element-map tree 'table-cell 'identity)))))))
  3040. (ert-deftest test-org-export/table-dimensions ()
  3041. "Test `org-export-table-dimensions' specifications."
  3042. ;; 1. Standard test.
  3043. (org-test-with-parsed-data "
  3044. | 1 | 2 | 3 |
  3045. | 4 | 5 | 6 |"
  3046. (should
  3047. (equal '(2 . 3)
  3048. (org-export-table-dimensions
  3049. (org-element-map tree 'table 'identity info 'first-match) info))))
  3050. ;; 2. Ignore horizontal rules and special columns.
  3051. (org-test-with-parsed-data "
  3052. | / | < | > |
  3053. | 1 | 2 | 3 |
  3054. |---+---+---|
  3055. | 4 | 5 | 6 |"
  3056. (should
  3057. (equal '(2 . 3)
  3058. (org-export-table-dimensions
  3059. (org-element-map tree 'table 'identity info 'first-match) info)))))
  3060. (ert-deftest test-org-export/table-cell-address ()
  3061. "Test `org-export-table-cell-address' specifications."
  3062. ;; 1. Standard test: index is 0-based.
  3063. (org-test-with-parsed-data "| a | b |"
  3064. (should
  3065. (equal '((0 . 0) (0 . 1))
  3066. (org-element-map tree 'table-cell
  3067. (lambda (cell) (org-export-table-cell-address cell info))
  3068. info))))
  3069. ;; 2. Special column isn't counted, nor are special rows.
  3070. (org-test-with-parsed-data "
  3071. | / | <> |
  3072. | | c |"
  3073. (should
  3074. (equal '(0 . 0)
  3075. (org-export-table-cell-address
  3076. (car (last (org-element-map tree 'table-cell 'identity info)))
  3077. info))))
  3078. ;; 3. Tables rules do not count either.
  3079. (org-test-with-parsed-data "
  3080. | a |
  3081. |---|
  3082. | b |
  3083. |---|
  3084. | c |"
  3085. (should
  3086. (equal '(2 . 0)
  3087. (org-export-table-cell-address
  3088. (car (last (org-element-map tree 'table-cell 'identity info)))
  3089. info))))
  3090. ;; 4. Return nil for special cells.
  3091. (org-test-with-parsed-data "| / | a |"
  3092. (should-not
  3093. (org-export-table-cell-address
  3094. (org-element-map tree 'table-cell 'identity nil 'first-match)
  3095. info))))
  3096. (ert-deftest test-org-export/get-table-cell-at ()
  3097. "Test `org-export-get-table-cell-at' specifications."
  3098. ;; 1. Address ignores special columns, special rows and rules.
  3099. (org-test-with-parsed-data "
  3100. | / | <> |
  3101. | | a |
  3102. |---+----|
  3103. | | b |"
  3104. (should
  3105. (equal '("b")
  3106. (org-element-contents
  3107. (org-export-get-table-cell-at
  3108. '(1 . 0)
  3109. (org-element-map tree 'table 'identity info 'first-match)
  3110. info)))))
  3111. ;; 2. Return value for a non-existent address is nil.
  3112. (org-test-with-parsed-data "| a |"
  3113. (should-not
  3114. (org-export-get-table-cell-at
  3115. '(2 . 2)
  3116. (org-element-map tree 'table 'identity info 'first-match)
  3117. info)))
  3118. (org-test-with-parsed-data "| / |"
  3119. (should-not
  3120. (org-export-get-table-cell-at
  3121. '(0 . 0)
  3122. (org-element-map tree 'table 'identity info 'first-match)
  3123. info))))
  3124. (ert-deftest test-org-export/table-cell-starts-colgroup-p ()
  3125. "Test `org-export-table-cell-starts-colgroup-p' specifications."
  3126. ;; 1. A cell at a beginning of a row always starts a column group.
  3127. (org-test-with-parsed-data "| a |"
  3128. (should
  3129. (org-export-table-cell-starts-colgroup-p
  3130. (org-element-map tree 'table-cell 'identity info 'first-match)
  3131. info)))
  3132. ;; 2. Special column should be ignored when determining the
  3133. ;; beginning of the row.
  3134. (org-test-with-parsed-data "
  3135. | / | |
  3136. | | a |"
  3137. (should
  3138. (org-export-table-cell-starts-colgroup-p
  3139. (org-element-map tree 'table-cell 'identity info 'first-match)
  3140. info)))
  3141. ;; 2. Explicit column groups.
  3142. (org-test-with-parsed-data "
  3143. | / | | < |
  3144. | a | b | c |"
  3145. (should
  3146. (equal
  3147. '(yes no yes)
  3148. (org-element-map tree 'table-cell
  3149. (lambda (cell)
  3150. (if (org-export-table-cell-starts-colgroup-p cell info) 'yes 'no))
  3151. info)))))
  3152. (ert-deftest test-org-export/table-cell-ends-colgroup-p ()
  3153. "Test `org-export-table-cell-ends-colgroup-p' specifications."
  3154. ;; 1. A cell at the end of a row always ends a column group.
  3155. (org-test-with-parsed-data "| a |"
  3156. (should
  3157. (org-export-table-cell-ends-colgroup-p
  3158. (org-element-map tree 'table-cell 'identity info 'first-match)
  3159. info)))
  3160. ;; 2. Special column should be ignored when determining the
  3161. ;; beginning of the row.
  3162. (org-test-with-parsed-data "
  3163. | / | |
  3164. | | a |"
  3165. (should
  3166. (org-export-table-cell-ends-colgroup-p
  3167. (org-element-map tree 'table-cell 'identity info 'first-match)
  3168. info)))
  3169. ;; 3. Explicit column groups.
  3170. (org-test-with-parsed-data "
  3171. | / | < | |
  3172. | a | b | c |"
  3173. (should
  3174. (equal
  3175. '(yes no yes)
  3176. (org-element-map tree 'table-cell
  3177. (lambda (cell)
  3178. (if (org-export-table-cell-ends-colgroup-p cell info) 'yes 'no))
  3179. info)))))
  3180. (ert-deftest test-org-export/table-row-starts-rowgroup-p ()
  3181. "Test `org-export-table-row-starts-rowgroup-p' specifications."
  3182. ;; 1. A row at the beginning of a table always starts a row group.
  3183. ;; So does a row following a table rule.
  3184. (org-test-with-parsed-data "
  3185. | a |
  3186. |---|
  3187. | b |"
  3188. (should
  3189. (equal
  3190. '(yes no yes)
  3191. (org-element-map tree 'table-row
  3192. (lambda (row)
  3193. (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
  3194. info))))
  3195. ;; 2. Special rows should be ignored when determining the beginning
  3196. ;; of the row.
  3197. (org-test-with-parsed-data "
  3198. | / | < |
  3199. | | a |
  3200. |---+---|
  3201. | / | < |
  3202. | | b |"
  3203. (should
  3204. (equal
  3205. '(yes no yes)
  3206. (org-element-map tree 'table-row
  3207. (lambda (row)
  3208. (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
  3209. info)))))
  3210. (ert-deftest test-org-export/table-row-ends-rowgroup-p ()
  3211. "Test `org-export-table-row-ends-rowgroup-p' specifications."
  3212. ;; 1. A row at the end of a table always ends a row group. So does
  3213. ;; a row preceding a table rule.
  3214. (org-test-with-parsed-data "
  3215. | a |
  3216. |---|
  3217. | b |"
  3218. (should
  3219. (equal
  3220. '(yes no yes)
  3221. (org-element-map tree 'table-row
  3222. (lambda (row)
  3223. (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
  3224. info))))
  3225. ;; 2. Special rows should be ignored when determining the beginning
  3226. ;; of the row.
  3227. (org-test-with-parsed-data "
  3228. | | a |
  3229. | / | < |
  3230. |---+---|
  3231. | | b |
  3232. | / | < |"
  3233. (should
  3234. (equal
  3235. '(yes no yes)
  3236. (org-element-map tree 'table-row
  3237. (lambda (row)
  3238. (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
  3239. info)))))
  3240. (ert-deftest test-org-export/table-row-in-header-p ()
  3241. "Test `org-export-table-row-in-header-p' specifications."
  3242. ;; Standard test. Separators are always nil.
  3243. (should
  3244. (equal
  3245. '(yes no no)
  3246. (org-test-with-parsed-data "| a |\n|---|\n| b |"
  3247. (org-element-map tree 'table-row
  3248. (lambda (row)
  3249. (if (org-export-table-row-in-header-p row info) 'yes 'no)) info))))
  3250. ;; Nil when there is no header.
  3251. (should
  3252. (equal
  3253. '(no no)
  3254. (org-test-with-parsed-data "| a |\n| b |"
  3255. (org-element-map tree 'table-row
  3256. (lambda (row)
  3257. (if (org-export-table-row-in-header-p row info) 'yes 'no)) info)))))
  3258. (ert-deftest test-org-export/table-row-starts-header-p ()
  3259. "Test `org-export-table-row-starts-header-p' specifications."
  3260. ;; 1. Only the row starting the first row group starts the table
  3261. ;; header.
  3262. (org-test-with-parsed-data "
  3263. | a |
  3264. | b |
  3265. |---|
  3266. | c |"
  3267. (should
  3268. (equal
  3269. '(yes no no no)
  3270. (org-element-map tree 'table-row
  3271. (lambda (row)
  3272. (if (org-export-table-row-starts-header-p row info) 'yes 'no))
  3273. info))))
  3274. ;; 2. A row cannot start an header if there's no header in the
  3275. ;; table.
  3276. (org-test-with-parsed-data "
  3277. | a |
  3278. |---|"
  3279. (should-not
  3280. (org-export-table-row-starts-header-p
  3281. (org-element-map tree 'table-row 'identity info 'first-match)
  3282. info))))
  3283. (ert-deftest test-org-export/table-row-ends-header-p ()
  3284. "Test `org-export-table-row-ends-header-p' specifications."
  3285. ;; 1. Only the row starting the first row group starts the table
  3286. ;; header.
  3287. (org-test-with-parsed-data "
  3288. | a |
  3289. | b |
  3290. |---|
  3291. | c |"
  3292. (should
  3293. (equal
  3294. '(no yes no no)
  3295. (org-element-map tree 'table-row
  3296. (lambda (row)
  3297. (if (org-export-table-row-ends-header-p row info) 'yes 'no))
  3298. info))))
  3299. ;; 2. A row cannot start an header if there's no header in the
  3300. ;; table.
  3301. (org-test-with-parsed-data "
  3302. | a |
  3303. |---|"
  3304. (should-not
  3305. (org-export-table-row-ends-header-p
  3306. (org-element-map tree 'table-row 'identity info 'first-match)
  3307. info))))
  3308. ;;; Tables of Contents
  3309. (ert-deftest test-org-export/collect-headlines ()
  3310. "Test `org-export-collect-headlines' specifications."
  3311. ;; Standard test.
  3312. (should
  3313. (= 2
  3314. (length
  3315. (org-test-with-parsed-data "* H1\n** H2"
  3316. (org-export-collect-headlines info)))))
  3317. ;; Do not collect headlines below optional argument.
  3318. (should
  3319. (= 1
  3320. (length
  3321. (org-test-with-parsed-data "* H1\n** H2"
  3322. (org-export-collect-headlines info 1)))))
  3323. ;; Never collect headlines below maximum headline level.
  3324. (should
  3325. (= 1
  3326. (length
  3327. (org-test-with-parsed-data "#+OPTIONS: H:1\n* H1\n** H2"
  3328. (org-export-collect-headlines info)))))
  3329. (should
  3330. (= 1
  3331. (length
  3332. (org-test-with-parsed-data "#+OPTIONS: H:1\n* H1\n** H2"
  3333. (org-export-collect-headlines info 2)))))
  3334. ;; Collect headlines locally.
  3335. (should
  3336. (= 2
  3337. (org-test-with-parsed-data "* H1\n** H2\n** H3"
  3338. (let ((scope (org-element-map tree 'headline #'identity info t)))
  3339. (length (org-export-collect-headlines info nil scope))))))
  3340. ;; When collecting locally, optional level is relative.
  3341. (should
  3342. (= 1
  3343. (org-test-with-parsed-data "* H1\n** H2\n*** H3"
  3344. (let ((scope (org-element-map tree 'headline #'identity info t)))
  3345. (length (org-export-collect-headlines info 1 scope)))))))
  3346. ;;; Templates
  3347. (ert-deftest test-org-export/inner-template ()
  3348. "Test `inner-template' translator specifications."
  3349. (should
  3350. (equal "Success!"
  3351. (org-test-with-temp-text "* Headline"
  3352. (org-export-as
  3353. (org-export-create-backend
  3354. :transcoders
  3355. '((inner-template . (lambda (contents info) "Success!"))
  3356. (headline . (lambda (h c i) "Headline"))))))))
  3357. ;; Inner template is applied even in a "body-only" export.
  3358. (should
  3359. (equal "Success!"
  3360. (org-test-with-temp-text "* Headline"
  3361. (org-export-as
  3362. (org-export-create-backend
  3363. :transcoders '((inner-template . (lambda (c i) "Success!"))
  3364. (headline . (lambda (h c i) "Headline"))))
  3365. nil nil 'body-only)))))
  3366. (ert-deftest test-org-export/template ()
  3367. "Test `template' translator specifications."
  3368. (should
  3369. (equal "Success!"
  3370. (org-test-with-temp-text "* Headline"
  3371. (org-export-as
  3372. (org-export-create-backend
  3373. :transcoders '((template . (lambda (contents info) "Success!"))
  3374. (headline . (lambda (h c i) "Headline"))))))))
  3375. ;; Template is not applied in a "body-only" export.
  3376. (should-not
  3377. (equal "Success!"
  3378. (org-test-with-temp-text "* Headline"
  3379. (org-export-as
  3380. (org-export-create-backend
  3381. :transcoders '((template . (lambda (contents info) "Success!"))
  3382. (headline . (lambda (h c i) "Headline"))))
  3383. nil nil 'body-only)))))
  3384. ;;; Topology
  3385. (ert-deftest test-org-export/get-next-element ()
  3386. "Test `org-export-get-next-element' specifications."
  3387. ;; Standard test.
  3388. (should
  3389. (equal "b"
  3390. (org-test-with-parsed-data "* Headline\n*a* b"
  3391. (org-export-get-next-element
  3392. (org-element-map tree 'bold 'identity info t) info))))
  3393. ;; Return nil when no previous element.
  3394. (should-not
  3395. (org-test-with-parsed-data "* Headline\na *b*"
  3396. (org-export-get-next-element
  3397. (org-element-map tree 'bold 'identity info t) info)))
  3398. ;; Non-exportable elements are ignored.
  3399. (should-not
  3400. (let ((org-export-with-timestamps nil))
  3401. (org-test-with-parsed-data "\alpha <2012-03-29 Thu>"
  3402. (org-export-get-next-element
  3403. (org-element-map tree 'entity 'identity info t) info))))
  3404. ;; Find next element in secondary strings.
  3405. (should
  3406. (eq 'verbatim
  3407. (org-test-with-parsed-data "* a =verb="
  3408. (org-element-type
  3409. (org-export-get-next-element
  3410. (org-element-map tree 'plain-text 'identity info t) info)))))
  3411. (should
  3412. (eq 'verbatim
  3413. (org-test-with-parsed-data "* /italic/ =verb="
  3414. (org-element-type
  3415. (org-export-get-next-element
  3416. (org-element-map tree 'italic 'identity info t) info)))))
  3417. ;; Find next element in document keywords.
  3418. (should
  3419. (eq 'verbatim
  3420. (org-test-with-parsed-data "#+TITLE: a =verb="
  3421. (org-element-type
  3422. (org-export-get-next-element
  3423. (org-element-map
  3424. (plist-get info :title) 'plain-text 'identity info t) info)))))
  3425. ;; Find next element in parsed affiliated keywords.
  3426. (should
  3427. (eq 'verbatim
  3428. (org-test-with-parsed-data "#+CAPTION: a =verb=\nParagraph"
  3429. (org-element-type
  3430. (org-export-get-next-element
  3431. (org-element-map tree 'plain-text 'identity info t nil t) info)))))
  3432. ;; With optional argument N, return a list containing all the
  3433. ;; following elements.
  3434. (should
  3435. (equal
  3436. '(bold code underline)
  3437. (org-test-with-parsed-data "_a_ /b/ *c* ~d~ _e_"
  3438. (mapcar 'car
  3439. (org-export-get-next-element
  3440. (org-element-map tree 'italic 'identity info t) info t)))))
  3441. ;; When N is a positive integer, return a list containing up to
  3442. ;; N following elements.
  3443. (should
  3444. (equal
  3445. '(bold code)
  3446. (org-test-with-parsed-data "_a_ /b/ *c* ~d~ _e_"
  3447. (mapcar 'car
  3448. (org-export-get-next-element
  3449. (org-element-map tree 'italic 'identity info t) info 2))))))
  3450. (ert-deftest test-org-export/get-previous-element ()
  3451. "Test `org-export-get-previous-element' specifications."
  3452. ;; Standard test.
  3453. (should
  3454. (equal "a "
  3455. (org-test-with-parsed-data "* Headline\na *b*"
  3456. (org-export-get-previous-element
  3457. (org-element-map tree 'bold 'identity info t) info))))
  3458. ;; Return nil when no previous element.
  3459. (should-not
  3460. (org-test-with-parsed-data "* Headline\n*a* b"
  3461. (org-export-get-previous-element
  3462. (org-element-map tree 'bold 'identity info t) info)))
  3463. ;; Non-exportable elements are ignored.
  3464. (should-not
  3465. (let ((org-export-with-timestamps nil))
  3466. (org-test-with-parsed-data "<2012-03-29 Thu> \alpha"
  3467. (org-export-get-previous-element
  3468. (org-element-map tree 'entity 'identity info t) info))))
  3469. ;; Find previous element in secondary strings.
  3470. (should
  3471. (eq 'verbatim
  3472. (org-test-with-parsed-data "* =verb= a"
  3473. (org-element-type
  3474. (org-export-get-previous-element
  3475. (org-element-map tree 'plain-text 'identity info t) info)))))
  3476. (should
  3477. (eq 'verbatim
  3478. (org-test-with-parsed-data "* =verb= /italic/"
  3479. (org-element-type
  3480. (org-export-get-previous-element
  3481. (org-element-map tree 'italic 'identity info t) info)))))
  3482. ;; Find previous element in document keywords.
  3483. (should
  3484. (eq 'verbatim
  3485. (org-test-with-parsed-data "#+TITLE: =verb= a"
  3486. (org-element-type
  3487. (org-export-get-previous-element
  3488. (org-element-map
  3489. (plist-get info :title) 'plain-text 'identity info t) info)))))
  3490. ;; Find previous element in parsed affiliated keywords.
  3491. (should
  3492. (eq 'verbatim
  3493. (org-test-with-parsed-data "#+CAPTION: =verb= a\nParagraph"
  3494. (org-element-type
  3495. (org-export-get-previous-element
  3496. (org-element-map tree 'plain-text 'identity info t nil t) info)))))
  3497. ;; With optional argument N, return a list containing up to
  3498. ;; N previous elements.
  3499. (should
  3500. (equal '(underline italic bold)
  3501. (org-test-with-parsed-data "_a_ /b/ *c* ~d~"
  3502. (mapcar 'car
  3503. (org-export-get-previous-element
  3504. (org-element-map tree 'code 'identity info t) info t)))))
  3505. ;; When N is a positive integer, return a list containing up to
  3506. ;; N previous elements.
  3507. (should
  3508. (equal '(italic bold)
  3509. (org-test-with-parsed-data "_a_ /b/ *c* ~d~"
  3510. (mapcar 'car
  3511. (org-export-get-previous-element
  3512. (org-element-map tree 'code 'identity info t) info 2))))))
  3513. (provide 'test-ox)
  3514. ;;; test-org-export.el end here