test-ox.el 126 KB

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