test-ox.el 125 KB

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