test-ox.el 141 KB

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