test-ox.el 121 KB

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