test-ox.el 121 KB

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