test-ox.el 144 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441
  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. ;; Handle uninterpreted objects in parsed keywords.
  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. ;; Objects in parsed keywords are "uninterpreted" before filters are
  830. ;; applied.
  831. (should
  832. (org-test-with-temp-text "#+TITLE: a_b"
  833. (org-export-as
  834. (org-export-create-backend
  835. :filters
  836. '((:filter-options
  837. (lambda (i _)
  838. (org-element-map (plist-get i :title) 'subscript
  839. (lambda (_) (error "There should be no subscript here")))))))
  840. nil nil nil '(:with-sub-superscript nil))))
  841. ;; Handle uninterpreted objects in captions.
  842. (should
  843. (equal "adummy\n"
  844. (org-test-with-temp-text "#+CAPTION: a_b\nParagraph"
  845. (org-export-as
  846. (org-export-create-backend
  847. :transcoders
  848. '((subscript . (lambda (s c i) "dummy"))
  849. (paragraph . (lambda (p c i)
  850. (org-export-data (org-export-get-caption p) i)))
  851. (section . (lambda (s c i) c))))
  852. nil nil nil '(:with-sub-superscript t)))))
  853. (should
  854. (equal "a_b\n"
  855. (org-test-with-temp-text "#+CAPTION: a_b\nParagraph"
  856. (org-export-as
  857. (org-export-create-backend
  858. :transcoders
  859. '((subscript . (lambda (s c i) "dummy"))
  860. (paragraph . (lambda (p c i)
  861. (org-export-data (org-export-get-caption p) i)))
  862. (section . (lambda (s c i) c))))
  863. nil nil nil '(:with-sub-superscript nil)))))
  864. ;; Special case: multiples uninterpreted objects in a row.
  865. (should
  866. (equal "a_b_c_d\n"
  867. (org-test-with-temp-text "a_b_c_d"
  868. (org-export-as
  869. (org-export-create-backend
  870. :transcoders '((subscript . (lambda (s c i) "dummy"))
  871. (paragraph . (lambda (p c i) c))
  872. (section . (lambda (s c i) c))))
  873. nil nil nil '(:with-sub-superscript {}))))))
  874. (ert-deftest test-org-export/export-scope ()
  875. "Test all export scopes."
  876. ;; Subtree.
  877. (should
  878. (equal "text\n*** H3\n"
  879. (org-test-with-temp-text "* H1\n<point>** H2\ntext\n*** H3"
  880. (org-export-as (org-test-default-backend) 'subtree))))
  881. (should
  882. (equal "text\n*** H3\n"
  883. (org-test-with-temp-text "* H1\n** H2\n<point>text\n*** H3"
  884. (org-export-as (org-test-default-backend) 'subtree))))
  885. ;; Subtree with a code block calling another block outside.
  886. (should
  887. (equal ": 3\n"
  888. (org-test-with-temp-text "
  889. <point>* Head1
  890. #+BEGIN_SRC emacs-lisp :noweb yes :exports results
  891. <<test>>
  892. #+END_SRC
  893. * Head2
  894. #+NAME: test
  895. #+BEGIN_SRC emacs-lisp
  896. \(+ 1 2)
  897. #+END_SRC"
  898. (let ((org-export-use-babel t))
  899. (org-export-as (org-test-default-backend) 'subtree)))))
  900. ;; Subtree export should ignore leading planning line and property
  901. ;; drawer.
  902. (should
  903. (equal "Text\n"
  904. (org-test-with-temp-text "
  905. <point>* H
  906. SCHEDULED: <2012-03-29 Thu>
  907. :PROPERTIES:
  908. :A: 1
  909. :END:
  910. Text"
  911. (org-export-as (org-test-default-backend)
  912. 'subtree nil nil
  913. '(:with-planning t :with-properties t)))))
  914. ;; Visible.
  915. (should
  916. (equal "* H1\n"
  917. (org-test-with-temp-text "* H1\n** H2\ntext\n*** H3"
  918. (org-cycle)
  919. (org-export-as (org-test-default-backend) nil 'visible))))
  920. ;; Region.
  921. (should
  922. (equal "text\n"
  923. (org-test-with-temp-text "* H1\n** H2\n<point>text\n*** H3"
  924. (transient-mark-mode 1)
  925. (push-mark (point) t t)
  926. (end-of-line)
  927. (org-export-as (org-test-default-backend)))))
  928. ;; Body only.
  929. (should
  930. (equal "Text\n"
  931. (org-test-with-temp-text "Text"
  932. (org-export-as
  933. (org-export-create-backend
  934. :transcoders
  935. '((template . (lambda (b _i) (format "BEGIN\n%sEND" b)))
  936. (section . (lambda (_s c _i) c))
  937. (paragraph . (lambda (_p c _i) c))))
  938. nil nil 'body-only))))
  939. (should
  940. (equal "BEGIN\nText\nEND"
  941. (org-test-with-temp-text "Text"
  942. (org-export-as
  943. (org-export-create-backend
  944. :transcoders
  945. '((template . (lambda (b _i) (format "BEGIN\n%sEND" b)))
  946. (section . (lambda (_s c _i) c))
  947. (paragraph . (lambda (_p c _i) c))))))))
  948. ;; Pathological case: Body only on an empty buffer is expected to
  949. ;; return an empty string, not nil.
  950. (should
  951. (org-test-with-temp-text ""
  952. (org-export-as (org-test-default-backend) nil nil t))))
  953. (ert-deftest test-org-export/output-file-name ()
  954. "Test `org-export-output-file-name' specifications."
  955. ;; Export from a file: name is built from original file name.
  956. (should
  957. (org-test-with-temp-text-in-file "Test"
  958. (equal (file-name-base (buffer-file-name))
  959. (file-name-base (org-export-output-file-name ".ext")))))
  960. ;; When #+EXPORT_FILE_NAME is defined, use it.
  961. (should
  962. (equal "test.ext"
  963. (org-test-with-temp-text-in-file "#+EXPORT_FILE_NAME: test"
  964. (org-export-output-file-name ".ext" t))))
  965. ;; When exporting to subtree, check EXPORT_FILE_NAME property first.
  966. (should
  967. (equal "test.ext"
  968. (org-test-with-temp-text-in-file
  969. "* Test\n :PROPERTIES:\n :EXPORT_FILE_NAME: test\n :END:"
  970. (org-export-output-file-name ".ext" t))))
  971. (should
  972. (equal "property.ext"
  973. (org-test-with-temp-text
  974. "#+EXPORT_FILE_NAME: keyword
  975. * Test<point>
  976. :PROPERTIES:
  977. :EXPORT_FILE_NAME: property
  978. :END:"
  979. (org-export-output-file-name ".ext" t))))
  980. ;; From a buffer not associated to a file, too.
  981. (should
  982. (equal "test.ext"
  983. (org-test-with-temp-text
  984. "* Test\n :PROPERTIES:\n :EXPORT_FILE_NAME: test\n :END:"
  985. (org-export-output-file-name ".ext" t))))
  986. ;; When provided name is absolute, preserve it.
  987. (should
  988. (org-test-with-temp-text
  989. (format "* Test\n :PROPERTIES:\n :EXPORT_FILE_NAME: %s\n :END:"
  990. (expand-file-name "test"))
  991. (file-name-absolute-p (org-export-output-file-name ".ext" t))))
  992. ;; When PUB-DIR argument is provided, use it.
  993. (should
  994. (equal "dir/"
  995. (org-test-with-temp-text-in-file "Test"
  996. (file-name-directory
  997. (org-export-output-file-name ".ext" nil "dir/")))))
  998. ;; PUB-DIR has precedence over EXPORT_FILE_NAME keyword or property.
  999. (should
  1000. (equal "pub-dir/"
  1001. (org-test-with-temp-text-in-file
  1002. "#+EXPORT_FILE_NAME: /dir/keyword\nTest"
  1003. (file-name-directory
  1004. (org-export-output-file-name ".ext" nil "pub-dir/")))))
  1005. ;; When returned name would overwrite original file, add EXTENSION
  1006. ;; another time.
  1007. (should
  1008. (equal "normal.org.org"
  1009. (org-test-at-id "75282ba2-f77a-4309-a970-e87c149fe125"
  1010. (org-export-output-file-name ".org")))))
  1011. (ert-deftest test-org-export/expand-include ()
  1012. "Test file inclusion in an Org buffer."
  1013. ;; Error when file isn't specified.
  1014. (should-error
  1015. (org-test-with-temp-text "#+INCLUDE: dummy.org"
  1016. (org-export-expand-include-keyword)))
  1017. ;; Full insertion with recursive inclusion.
  1018. (should
  1019. (equal
  1020. (with-temp-buffer
  1021. (insert-file
  1022. (expand-file-name "examples/include.org" org-test-dir))
  1023. (replace-regexp-in-string
  1024. (regexp-quote "#+INCLUDE: \"include2.org\"")
  1025. "Success!" (buffer-string)))
  1026. (org-test-with-temp-text
  1027. (format "#+INCLUDE: \"%s/examples/include.org\"" org-test-dir)
  1028. (org-export-expand-include-keyword)
  1029. (buffer-string))))
  1030. ;; Localized insertion.
  1031. (org-test-with-temp-text
  1032. (format "#+INCLUDE: \"%s/examples/include.org\" :lines \"1-2\""
  1033. org-test-dir)
  1034. (org-export-expand-include-keyword)
  1035. (should (equal (buffer-string)
  1036. "Small Org file with an include keyword.\n")))
  1037. ;; Insertion with constraints on headlines level.
  1038. (should
  1039. (equal
  1040. "* Top heading\n** Heading\nbody\n"
  1041. (org-test-with-temp-text
  1042. (format
  1043. "* Top heading\n#+INCLUDE: \"%s/examples/include.org\" :lines \"9-11\""
  1044. org-test-dir)
  1045. (org-export-expand-include-keyword)
  1046. (buffer-string))))
  1047. (should
  1048. (equal
  1049. "* Top heading\n* Heading\nbody\n"
  1050. (org-test-with-temp-text
  1051. (format
  1052. "* Top heading\n#+INCLUDE: \"%s/examples/include.org\" :lines \"9-11\" :minlevel 1"
  1053. org-test-dir)
  1054. (org-export-expand-include-keyword)
  1055. (buffer-string))))
  1056. ;; Inclusion within an example block.
  1057. (should
  1058. (equal
  1059. "#+BEGIN_EXAMPLE\nSmall Org file with an include keyword.\n#+END_EXAMPLE\n"
  1060. (org-test-with-temp-text
  1061. (format "#+INCLUDE: \"%s/examples/include.org\" :lines \"1-2\" EXAMPLE"
  1062. org-test-dir)
  1063. (org-export-expand-include-keyword)
  1064. (buffer-string))))
  1065. ;; Inclusion within a src-block.
  1066. (should
  1067. (equal
  1068. "#+BEGIN_SRC emacs-lisp\n(+ 2 1)\n#+END_SRC\n"
  1069. (org-test-with-temp-text
  1070. (format
  1071. "#+INCLUDE: \"%s/examples/include.org\" :lines \"4-5\" SRC emacs-lisp"
  1072. org-test-dir)
  1073. (org-export-expand-include-keyword)
  1074. (buffer-string))))
  1075. ;; Inclusion within an html export-block.
  1076. (should
  1077. (equal
  1078. "#+BEGIN_EXPORT html\n<p>HTML!</p>\n#+END_EXPORT\n"
  1079. (org-test-with-temp-text
  1080. (format
  1081. "#+INCLUDE: \"%s/examples/include.html\" EXPORT html"
  1082. org-test-dir)
  1083. (org-export-expand-include-keyword)
  1084. (buffer-string))))
  1085. ;; Inclusion within an center paragraph
  1086. (should
  1087. (equal
  1088. "#+BEGIN_CENTER\nSuccess!\n#+END_CENTER\n"
  1089. (org-test-with-temp-text
  1090. (format
  1091. "#+INCLUDE: \"%s/examples/include2.org\" CENTER"
  1092. org-test-dir)
  1093. (org-export-expand-include-keyword)
  1094. (buffer-string))))
  1095. ;; Footnotes labels are local to each included file.
  1096. (should
  1097. (= 6
  1098. (length
  1099. (delete-dups
  1100. (let ((contents "
  1101. Footnotes[fn:1], [fn:test], [fn:test] and [fn:inline:inline footnote].
  1102. \[fn:1] Footnote 1
  1103. \[fn:test] Footnote \"test\""))
  1104. (org-test-with-temp-text-in-file contents
  1105. (let ((file1 (buffer-file-name)))
  1106. (org-test-with-temp-text-in-file contents
  1107. (let ((file2 (buffer-file-name)))
  1108. (org-test-with-temp-text
  1109. (format "#+INCLUDE: \"%s\"\n#+INCLUDE: \"%s\""
  1110. file1 file2)
  1111. (org-export-expand-include-keyword)
  1112. (org-element-map (org-element-parse-buffer)
  1113. 'footnote-reference
  1114. (lambda (r) (org-element-property :label r)))))))))))))
  1115. ;; Footnotes labels are not local to each include keyword.
  1116. (should
  1117. (= 3
  1118. (length
  1119. (delete-dups
  1120. (let ((contents "
  1121. Footnotes[fn:1], [fn:test] and [fn:inline:inline footnote].
  1122. \[fn:1] Footnote 1
  1123. \[fn:test] Footnote \"test\""))
  1124. (org-test-with-temp-text-in-file contents
  1125. (let ((file (buffer-file-name)))
  1126. (org-test-with-temp-text
  1127. (format "#+INCLUDE: \"%s\"\n#+INCLUDE: \"%s\"" file file)
  1128. (org-export-expand-include-keyword)
  1129. (org-element-map (org-element-parse-buffer)
  1130. 'footnote-reference
  1131. (lambda (ref) (org-element-property :label ref)))))))))))
  1132. ;; Footnotes are supported by :lines-like elements and unnecessary
  1133. ;; footnotes are dropped.
  1134. (should
  1135. (= 3
  1136. (length
  1137. (delete-dups
  1138. (let ((contents "
  1139. * foo
  1140. Footnotes[fn:1]
  1141. * bar
  1142. Footnotes[fn:2], foot[fn:test] and [fn:inline:inline footnote]
  1143. \[fn:1] Footnote 1
  1144. \[fn:2] Footnote 1
  1145. * Footnotes
  1146. \[fn:test] Footnote \"test\"
  1147. "))
  1148. (org-test-with-temp-text-in-file contents
  1149. (let ((file (buffer-file-name)))
  1150. (org-test-with-temp-text
  1151. (format "#+INCLUDE: \"%s::*bar\"\n" file)
  1152. (org-export-expand-include-keyword)
  1153. (org-element-map (org-element-parse-buffer)
  1154. 'footnote-definition
  1155. (lambda (ref) (org-element-property :label ref)))))))))))
  1156. ;; If only-contents is non-nil only include contents of element.
  1157. (should
  1158. (equal
  1159. "body\n"
  1160. (org-test-with-temp-text
  1161. (concat
  1162. (format "#+INCLUDE: \"%s/examples/include.org::*Heading\" "
  1163. org-test-dir)
  1164. ":only-contents t")
  1165. (org-export-expand-include-keyword)
  1166. (buffer-string))))
  1167. ;; Headings can be included via CUSTOM_ID.
  1168. (should
  1169. (org-test-with-temp-text
  1170. (format "#+INCLUDE: \"%s/examples/include.org::#ah\"" org-test-dir)
  1171. (org-export-expand-include-keyword)
  1172. (goto-char (point-min))
  1173. (looking-at "* Another heading")))
  1174. ;; Named objects can be included.
  1175. (should
  1176. (equal
  1177. "| 1 |\n"
  1178. (org-test-with-temp-text
  1179. (format "#+INCLUDE: \"%s/examples/include.org::tbl\" :only-contents t"
  1180. org-test-dir)
  1181. (org-export-expand-include-keyword)
  1182. (buffer-string))))
  1183. ;; Including non-existing elements should result in an error.
  1184. (should-error
  1185. (org-test-with-temp-text
  1186. (format "#+INCLUDE: \"%s/examples/include.org::*non-existing heading\""
  1187. org-test-dir)
  1188. (org-export-expand-include-keyword)))
  1189. ;; Lines work relatively to an included element.
  1190. (should
  1191. (equal
  1192. "2\n3\n"
  1193. (org-test-with-temp-text
  1194. (format "#+INCLUDE: \"%s/examples/include.org::#ah\" :only-contents t \
  1195. :lines \"2-3\""
  1196. org-test-dir)
  1197. (org-export-expand-include-keyword)
  1198. (buffer-string))))
  1199. ;; Properties should be dropped from headlines.
  1200. (should
  1201. (equal
  1202. (org-test-with-temp-text
  1203. (format "#+INCLUDE: \"%s/examples/include.org::#ht\" :only-contents t"
  1204. org-test-dir)
  1205. (org-export-expand-include-keyword)
  1206. (buffer-string))
  1207. (org-test-with-temp-text
  1208. (format "#+INCLUDE: \"%s/examples/include.org::tbl\"" org-test-dir)
  1209. (org-export-expand-include-keyword)
  1210. (buffer-string))))
  1211. ;; Properties should be dropped, drawers should not be.
  1212. (should
  1213. (equal
  1214. ":LOGBOOK:\ndrawer\n:END:\ncontent\n"
  1215. (org-test-with-temp-text
  1216. (format "#+INCLUDE: \"%s/examples/include.org::#dh\" :only-contents t"
  1217. org-test-dir)
  1218. (org-export-expand-include-keyword)
  1219. (buffer-string))))
  1220. ;; Adjacent INCLUDE-keywords should have the same :minlevel if unspecified.
  1221. (should
  1222. (cl-every (lambda (level) (zerop (1- level)))
  1223. (org-test-with-temp-text
  1224. (concat
  1225. (format "#+INCLUDE: \"%s/examples/include.org::#ah\"\n"
  1226. org-test-dir)
  1227. (format "#+INCLUDE: \"%s/examples/include.org::*Heading\""
  1228. org-test-dir))
  1229. (org-export-expand-include-keyword)
  1230. (org-element-map (org-element-parse-buffer) 'headline
  1231. (lambda (head) (org-element-property :level head))))))
  1232. ;; INCLUDE does not insert induced :minlevel for src-blocks.
  1233. (should-not
  1234. (equal
  1235. (org-test-with-temp-text
  1236. (format "#+INCLUDE: \"%s/examples/include2.org\" src emacs-lisp"
  1237. org-test-dir)
  1238. (org-export-expand-include-keyword)
  1239. (buffer-string))
  1240. (org-test-with-temp-text
  1241. (format
  1242. "#+INCLUDE: \"%s/examples/include2.org\" src emacs-lisp :minlevel 1"
  1243. org-test-dir)
  1244. (org-export-expand-include-keyword)
  1245. (buffer-string))))
  1246. ;; INCLUDE assigns the relative :minlevel conditional on narrowing.
  1247. (should
  1248. (org-test-with-temp-text-in-file
  1249. (format "* h1\n<point>#+INCLUDE: \"%s/examples/include.org::#ah\""
  1250. org-test-dir)
  1251. (narrow-to-region (point) (point-max))
  1252. (org-export-expand-include-keyword)
  1253. (eq 1 (org-current-level))))
  1254. ;; If :minlevel is present do not alter it.
  1255. (should
  1256. (org-test-with-temp-text
  1257. (format
  1258. "* h1\n<point>#+INCLUDE: \"%s/examples/include.org::#ah\" :minlevel 3"
  1259. org-test-dir)
  1260. (narrow-to-region (point) (point-max))
  1261. (org-export-expand-include-keyword)
  1262. (eq 3 (org-current-level)))))
  1263. (ert-deftest test-org-export/expand-macro ()
  1264. "Test macro expansion in an Org buffer."
  1265. (require 'ox-org)
  1266. ;; Standard macro expansion.
  1267. (should
  1268. (equal "#+MACRO: macro1 value\nvalue\n"
  1269. (org-test-with-temp-text "#+MACRO: macro1 value\n{{{macro1}}}"
  1270. (org-export-as (org-test-default-backend)))))
  1271. ;; Include global macros. However, local macros override them.
  1272. (should
  1273. (equal "global\n"
  1274. (org-test-with-temp-text "{{{M}}}"
  1275. (let ((org-export-global-macros '(("M" . "global"))))
  1276. (org-export-as (org-test-default-backend))))))
  1277. (should
  1278. (equal "global arg\n"
  1279. (org-test-with-temp-text "{{{M(arg)}}}"
  1280. (let ((org-export-global-macros '(("M" . "global $1"))))
  1281. (org-export-as (org-test-default-backend))))))
  1282. (should
  1283. (equal "2\n"
  1284. (org-test-with-temp-text "{{{M}}}"
  1285. (let ((org-export-global-macros '(("M" . "(eval (+ 1 1))"))))
  1286. (org-export-as (org-test-default-backend))))))
  1287. (should
  1288. (equal "#+MACRO: M local\nlocal\n"
  1289. (org-test-with-temp-text "#+macro: M local\n{{{M}}}"
  1290. (let ((org-export-global-macros '(("M" . "global"))))
  1291. (org-export-as (org-test-default-backend))))))
  1292. ;; Allow macro in parsed keywords and associated properties.
  1293. ;; Standard macro expansion.
  1294. (should
  1295. (string-match
  1296. "#\\+K: value"
  1297. (let ((backend (org-export-create-backend
  1298. :parent 'org
  1299. :options '((:k "K" nil nil parse)))))
  1300. (org-test-with-temp-text "#+MACRO: macro value\n#+K: {{{macro}}}"
  1301. (org-export-as backend)))))
  1302. (should
  1303. (string-match
  1304. ":EXPORT_K: v"
  1305. (let ((backend (org-export-create-backend
  1306. :parent 'org
  1307. :options '((:k "K" nil nil parse)))))
  1308. (org-test-with-temp-text
  1309. "#+MACRO: m v\n* H\n:PROPERTIES:\n:EXPORT_K: {{{m}}}\n:END:"
  1310. (org-export-as backend nil nil nil '(:with-properties t))))))
  1311. ;; Expand specific macros.
  1312. (should
  1313. (equal "me 2012-03-29 me@here Title\n"
  1314. (org-test-with-temp-text
  1315. "
  1316. #+TITLE: Title
  1317. #+DATE: 2012-03-29
  1318. #+AUTHOR: me
  1319. #+EMAIL: me@here
  1320. {{{author}}} {{{date}}} {{{email}}} {{{title}}}"
  1321. (let ((output (org-export-as (org-test-default-backend))))
  1322. (substring output (string-match ".*\n\\'" output))))))
  1323. ;; Expand specific macros when property contained a regular macro
  1324. ;; already.
  1325. (should
  1326. (equal "value\n"
  1327. (org-test-with-temp-text "
  1328. #+MACRO: macro1 value
  1329. #+TITLE: {{{macro1}}}
  1330. {{{title}}}"
  1331. (let ((output (org-export-as (org-test-default-backend))))
  1332. (substring output (string-match ".*\n\\'" output))))))
  1333. ;; Expand macros with templates in included files.
  1334. (should
  1335. (equal "success\n"
  1336. (org-test-with-temp-text
  1337. (format "#+INCLUDE: \"%s/examples/macro-templates.org\"
  1338. {{{included-macro}}}" org-test-dir)
  1339. (let ((output (org-export-as (org-test-default-backend))))
  1340. (substring output (string-match ".*\n\\'" output))))))
  1341. ;; Date macro takes a optional formatting argument
  1342. (should
  1343. (equal "09-02-15\n"
  1344. (org-test-with-temp-text "{{{date(%d-%m-%y)}}}\n* d :noexport:\n#+DATE: <2015-02-09>"
  1345. (org-export-as (org-test-default-backend)))))
  1346. ;; Only single timestamps are formatted
  1347. (should
  1348. (equal "<2015-02x-09>\n"
  1349. (org-test-with-temp-text "{{{date(%d-%m-%y)}}}\n* d :noexport:\n#+DATE: <2015-02x-09>"
  1350. (org-export-as (org-test-default-backend)))))
  1351. ;; Throw an error when a macro definition is missing.
  1352. (should-error
  1353. (org-test-with-temp-text "{{{missing}}}"
  1354. (org-export-as (org-test-default-backend))))
  1355. ;; Macros defined in commented subtrees are ignored.
  1356. (should-error
  1357. (org-test-with-temp-text
  1358. "* COMMENT H\n#+MACRO: macro1\n* H2\nvalue\n{{{macro1}}}"
  1359. (org-export-as (org-test-default-backend))))
  1360. (should-error
  1361. (org-test-with-temp-text
  1362. "* COMMENT H\n** H2\n#+MACRO: macro1\n* H3\nvalue\n{{{macro1}}}"
  1363. (org-export-as (org-test-default-backend)))))
  1364. (ert-deftest test-org-export/before-processing-hook ()
  1365. "Test `org-export-before-processing-hook'."
  1366. (should
  1367. (equal
  1368. "#+MACRO: mac val\nTest\n"
  1369. (org-test-with-temp-text "#+MACRO: mac val\n{{{mac}}} Test"
  1370. (let ((org-export-before-processing-hook
  1371. '((lambda (backend)
  1372. (while (re-search-forward "{{{" nil t)
  1373. (let ((object (org-element-context)))
  1374. (when (eq (org-element-type object) 'macro)
  1375. (delete-region
  1376. (org-element-property :begin object)
  1377. (org-element-property :end object)))))))))
  1378. (org-export-as (org-test-default-backend)))))))
  1379. (ert-deftest test-org-export/before-parsing-hook ()
  1380. "Test `org-export-before-parsing-hook'."
  1381. (should
  1382. (equal "Body 1\nBody 2\n"
  1383. (org-test-with-temp-text "* Headline 1\nBody 1\n* Headline 2\nBody 2"
  1384. (let ((org-export-before-parsing-hook
  1385. '((lambda (backend)
  1386. (goto-char (point-min))
  1387. (while (re-search-forward org-outline-regexp-bol nil t)
  1388. (delete-region
  1389. (point-at-bol) (progn (forward-line) (point))))))))
  1390. (org-export-as (org-test-default-backend)))))))
  1391. ;;; Affiliated Keywords
  1392. (ert-deftest test-org-export/read-attribute ()
  1393. "Test `org-export-read-attribute' specifications."
  1394. ;; Standard test.
  1395. (should
  1396. (equal
  1397. (org-export-read-attribute
  1398. :attr_html
  1399. (org-test-with-temp-text "#+ATTR_HTML: :a 1 :b 2\nParagraph"
  1400. (org-element-at-point)))
  1401. '(:a "1" :b "2")))
  1402. ;; Return nil on empty attribute.
  1403. (should-not
  1404. (org-export-read-attribute
  1405. :attr_html
  1406. (org-test-with-temp-text "Paragraph" (org-element-at-point))))
  1407. ;; Return nil on "nil" string.
  1408. (should
  1409. (equal '(:a nil :b nil)
  1410. (org-export-read-attribute
  1411. :attr_html
  1412. (org-test-with-temp-text "#+ATTR_HTML: :a nil :b nil\nParagraph"
  1413. (org-element-at-point)))))
  1414. ;; Return nil on empty string.
  1415. (should
  1416. (equal '(:a nil :b nil)
  1417. (org-export-read-attribute
  1418. :attr_html
  1419. (org-test-with-temp-text "#+ATTR_HTML: :a :b\nParagraph"
  1420. (org-element-at-point)))))
  1421. ;; Return empty string when value is "".
  1422. (should
  1423. (equal '(:a "")
  1424. (org-export-read-attribute
  1425. :attr_html
  1426. (org-test-with-temp-text "#+ATTR_HTML: :a \"\"\nParagraph"
  1427. (org-element-at-point)))))
  1428. ;; Return \"\" when value is """".
  1429. (should
  1430. (equal '(:a "\"\"")
  1431. (org-export-read-attribute
  1432. :attr_html
  1433. (org-test-with-temp-text "#+ATTR_HTML: :a \"\"\"\"\nParagraph"
  1434. (org-element-at-point)))))
  1435. ;; Ignore text before first property.
  1436. (should-not
  1437. (member "ignore"
  1438. (org-export-read-attribute
  1439. :attr_html
  1440. (org-test-with-temp-text "#+ATTR_HTML: ignore :a 1\nParagraph"
  1441. (org-element-at-point))))))
  1442. (ert-deftest test-org-export/get-caption ()
  1443. "Test `org-export-get-caption' specifications."
  1444. ;; Without optional argument, return long caption
  1445. (should
  1446. (equal
  1447. '("l")
  1448. (org-test-with-temp-text "#+CAPTION[s]: l\nPara"
  1449. (org-export-get-caption (org-element-at-point)))))
  1450. ;; With optional argument, return short caption.
  1451. (should
  1452. (equal
  1453. '("s")
  1454. (org-test-with-temp-text "#+CAPTION[s]: l\nPara"
  1455. (org-export-get-caption (org-element-at-point) t))))
  1456. ;; Multiple lines are separated by white spaces.
  1457. (should
  1458. (equal
  1459. '("a" " " "b")
  1460. (org-test-with-temp-text "#+CAPTION: a\n#+CAPTION: b\nPara"
  1461. (org-export-get-caption (org-element-at-point))))))
  1462. ;;; Back-End Tools
  1463. (ert-deftest test-org-export/define-backend ()
  1464. "Test back-end definition and accessors."
  1465. ;; Translate table.
  1466. (should
  1467. (equal '((headline . my-headline-test))
  1468. (let (org-export-registered-backends)
  1469. (org-export-define-backend 'test '((headline . my-headline-test)))
  1470. (org-export-get-all-transcoders 'test))))
  1471. ;; Filters.
  1472. (should
  1473. (equal '((:filter-headline . my-filter))
  1474. (let (org-export-registered-backends)
  1475. (org-export-define-backend 'test
  1476. '((headline . my-headline-test))
  1477. :filters-alist '((:filter-headline . my-filter)))
  1478. (org-export-backend-filters (org-export-get-backend 'test)))))
  1479. ;; Options.
  1480. (should
  1481. (equal '((:prop value))
  1482. (let (org-export-registered-backends)
  1483. (org-export-define-backend 'test
  1484. '((headline . my-headline-test))
  1485. :options-alist '((:prop value)))
  1486. (org-export-backend-options (org-export-get-backend 'test)))))
  1487. ;; Menu.
  1488. (should
  1489. (equal '(?k "Test Export" test)
  1490. (let (org-export-registered-backends)
  1491. (org-export-define-backend 'test
  1492. '((headline . my-headline-test))
  1493. :menu-entry '(?k "Test Export" test))
  1494. (org-export-backend-menu (org-export-get-backend 'test))))))
  1495. (ert-deftest test-org-export/define-derived-backend ()
  1496. "Test `org-export-define-derived-backend' specifications."
  1497. ;; Error when parent back-end is not defined.
  1498. (should-error
  1499. (let (org-export-registered-backends)
  1500. (org-export-define-derived-backend 'test 'parent)))
  1501. ;; Append translation table to parent's.
  1502. (should
  1503. (equal '((:headline . test) (:headline . parent))
  1504. (let (org-export-registered-backends)
  1505. (org-export-define-backend 'parent '((:headline . parent)))
  1506. (org-export-define-derived-backend 'test 'parent
  1507. :translate-alist '((:headline . test)))
  1508. (org-export-get-all-transcoders 'test))))
  1509. ;; Options defined in the new back have priority over those defined
  1510. ;; in parent.
  1511. (should
  1512. (eq 'test
  1513. (let (org-export-registered-backends)
  1514. (org-export-define-backend 'parent
  1515. '((:headline . parent))
  1516. :options-alist '((:a nil nil 'parent)))
  1517. (org-export-define-derived-backend 'test 'parent
  1518. :options-alist '((:a nil nil 'test)))
  1519. (plist-get (org-export--get-global-options
  1520. (org-export-get-backend 'test))
  1521. :a)))))
  1522. (ert-deftest test-org-export/derived-backend-p ()
  1523. "Test `org-export-derived-backend-p' specifications."
  1524. ;; Non-nil with direct match.
  1525. (should
  1526. (let (org-export-registered-backends)
  1527. (org-export-define-backend 'test '((headline . test)))
  1528. (org-export-derived-backend-p 'test 'test)))
  1529. (should
  1530. (let (org-export-registered-backends)
  1531. (org-export-define-backend 'test '((headline . test)))
  1532. (org-export-define-derived-backend 'test2 'test)
  1533. (org-export-derived-backend-p 'test2 'test2)))
  1534. ;; Non-nil with a direct parent.
  1535. (should
  1536. (let (org-export-registered-backends)
  1537. (org-export-define-backend 'test '((headline . test)))
  1538. (org-export-define-derived-backend 'test2 'test)
  1539. (org-export-derived-backend-p 'test2 'test)))
  1540. ;; Non-nil with an indirect parent.
  1541. (should
  1542. (let (org-export-registered-backends)
  1543. (org-export-define-backend 'test '((headline . test)))
  1544. (org-export-define-derived-backend 'test2 'test)
  1545. (org-export-define-derived-backend 'test3 'test2)
  1546. (org-export-derived-backend-p 'test3 'test)))
  1547. ;; Nil otherwise.
  1548. (should-not
  1549. (let (org-export-registered-backends)
  1550. (org-export-define-backend 'test '((headline . test)))
  1551. (org-export-define-backend 'test2 '((headline . test2)))
  1552. (org-export-derived-backend-p 'test2 'test)))
  1553. (should-not
  1554. (let (org-export-registered-backends)
  1555. (org-export-define-backend 'test '((headline . test)))
  1556. (org-export-define-backend 'test2 '((headline . test2)))
  1557. (org-export-define-derived-backend 'test3 'test2)
  1558. (org-export-derived-backend-p 'test3 'test))))
  1559. (ert-deftest test-org-export/get-all-transcoders ()
  1560. "Test `org-export-get-all-transcoders' specifications."
  1561. ;; Return nil when back-end cannot be found.
  1562. (should-not (org-export-get-all-transcoders nil))
  1563. ;; Same as `org-export-transcoders' if no parent.
  1564. (should
  1565. (equal '((headline . ignore))
  1566. (org-export-get-all-transcoders
  1567. (org-export-create-backend
  1568. :transcoders '((headline . ignore))))))
  1569. ;; But inherit from all ancestors whenever possible.
  1570. (should
  1571. (equal '((section . ignore) (headline . ignore))
  1572. (let (org-export-registered-backends)
  1573. (org-export-define-backend 'b1 '((headline . ignore)))
  1574. (org-export-get-all-transcoders
  1575. (org-export-create-backend
  1576. :parent 'b1 :transcoders '((section . ignore)))))))
  1577. (should
  1578. (equal '((paragraph . ignore) (section . ignore) (headline . ignore))
  1579. (let (org-export-registered-backends)
  1580. (org-export-define-backend 'b1 '((headline . ignore)))
  1581. (org-export-define-derived-backend 'b2 'b1
  1582. :translate-alist '((section . ignore)))
  1583. (org-export-get-all-transcoders
  1584. (org-export-create-backend
  1585. :parent 'b2 :transcoders '((paragraph . ignore)))))))
  1586. ;; Back-end transcoders overrule inherited ones.
  1587. (should
  1588. (eq 'b
  1589. (let (org-export-registered-backends)
  1590. (org-export-define-backend 'b1 '((headline . a)))
  1591. (cdr (assq 'headline
  1592. (org-export-get-all-transcoders
  1593. (org-export-create-backend
  1594. :parent 'b1 :transcoders '((headline . b))))))))))
  1595. (ert-deftest test-org-export/get-all-options ()
  1596. "Test `org-export-get-all-options' specifications."
  1597. ;; Return nil when back-end cannot be found.
  1598. (should-not (org-export-get-all-options nil))
  1599. ;; Same as `org-export-options' if no parent.
  1600. (should
  1601. (equal '((headline . ignore))
  1602. (org-export-get-all-options
  1603. (org-export-create-backend
  1604. :options '((headline . ignore))))))
  1605. ;; But inherit from all ancestors whenever possible.
  1606. (should
  1607. (equal '((:key2 value2) (:key1 value1))
  1608. (let (org-export-registered-backends)
  1609. (org-export-define-backend 'b1 nil :options-alist '((:key1 value1)))
  1610. (org-export-get-all-options
  1611. (org-export-create-backend
  1612. :parent 'b1 :options '((:key2 value2)))))))
  1613. (should
  1614. (equal '((:key3 value3) (:key2 value2) (:key1 value1))
  1615. (let (org-export-registered-backends)
  1616. (org-export-define-backend 'b1 nil :options-alist '((:key1 value1)))
  1617. (org-export-define-derived-backend 'b2 'b1
  1618. :options-alist '((:key2 value2)))
  1619. (org-export-get-all-options
  1620. (org-export-create-backend
  1621. :parent 'b2 :options '((:key3 value3)))))))
  1622. ;; Back-end options overrule inherited ones.
  1623. (should
  1624. (eq 'b
  1625. (let (org-export-registered-backends)
  1626. (org-export-define-backend 'b1 nil :options-alist '((:key1 . a)))
  1627. (cdr (assq :key1
  1628. (org-export-get-all-options
  1629. (org-export-create-backend
  1630. :parent 'b1 :options '((:key1 . b))))))))))
  1631. (ert-deftest test-org-export/get-all-filters ()
  1632. "Test `org-export-get-all-filters' specifications."
  1633. ;; Return nil when back-end cannot be found.
  1634. (should-not (org-export-get-all-filters nil))
  1635. ;; Same as `org-export-filters' if no parent.
  1636. (should
  1637. (equal '((:filter-headline . ignore))
  1638. (org-export-get-all-filters
  1639. (org-export-create-backend
  1640. :filters '((:filter-headline . ignore))))))
  1641. ;; But inherit from all ancestors whenever possible.
  1642. (should
  1643. (equal '((:filter-section . ignore) (:filter-headline . ignore))
  1644. (let (org-export-registered-backends)
  1645. (org-export-define-backend 'b1
  1646. nil :filters-alist '((:filter-headline . ignore)))
  1647. (org-export-get-all-filters
  1648. (org-export-create-backend
  1649. :parent 'b1 :filters '((:filter-section . ignore)))))))
  1650. (should
  1651. (equal '((:filter-paragraph . ignore)
  1652. (:filter-section . ignore)
  1653. (:filter-headline . ignore))
  1654. (let (org-export-registered-backends)
  1655. (org-export-define-backend 'b1
  1656. nil :filters-alist '((:filter-headline . ignore)))
  1657. (org-export-define-derived-backend 'b2 'b1
  1658. :filters-alist '((:filter-section . ignore)))
  1659. (org-export-get-all-filters
  1660. (org-export-create-backend
  1661. :parent 'b2 :filters '((:filter-paragraph . ignore)))))))
  1662. ;; Back-end filters overrule inherited ones.
  1663. (should
  1664. (eq 'b
  1665. (let (org-export-registered-backends)
  1666. (org-export-define-backend 'b1 '((:filter-headline . a)))
  1667. (cdr (assq :filter-headline
  1668. (org-export-get-all-filters
  1669. (org-export-create-backend
  1670. :parent 'b1 :filters '((:filter-headline . b))))))))))
  1671. (ert-deftest test-org-export/with-backend ()
  1672. "Test `org-export-with-backend' definition."
  1673. ;; Error when calling an undefined back-end
  1674. (should-error (org-export-with-backend nil "Test"))
  1675. ;; Error when called back-end doesn't have an appropriate
  1676. ;; transcoder.
  1677. (should-error
  1678. (org-export-with-backend
  1679. (org-export-create-backend :transcoders '((headline . ignore)))
  1680. "Test"))
  1681. ;; Otherwise, export using correct transcoder
  1682. (should
  1683. (equal "Success"
  1684. (let (org-export-registered-backends)
  1685. (org-export-define-backend 'test
  1686. '((plain-text . (lambda (text contents info) "Failure"))))
  1687. (org-export-define-backend 'test2
  1688. '((plain-text . (lambda (text contents info) "Success"))))
  1689. (org-export-with-backend 'test2 "Test"))))
  1690. ;; Provide correct back-end if transcoder needs to use recursive
  1691. ;; calls anyway.
  1692. (should
  1693. (equal "Success\n"
  1694. (let ((test-back-end
  1695. (org-export-create-backend
  1696. :transcoders
  1697. (list (cons 'headline
  1698. (lambda (headline contents info)
  1699. (org-export-data
  1700. (org-element-property :title headline)
  1701. info)))
  1702. (cons 'plain-text (lambda (text info) "Success"))))))
  1703. (org-export-string-as
  1704. "* Test"
  1705. (org-export-create-backend
  1706. :transcoders
  1707. (list (cons 'headline
  1708. (lambda (headline contents info)
  1709. (org-export-with-backend
  1710. test-back-end headline contents info))))))))))
  1711. (ert-deftest test-org-export/data-with-backend ()
  1712. "Test `org-export-data-with-backend' specifications."
  1713. ;; Error when calling an undefined back-end.
  1714. (should-error (org-export-data-with-backend nil "nil" nil))
  1715. ;; Otherwise, export data recursively, using correct back-end.
  1716. (should
  1717. (equal
  1718. "Success!"
  1719. (org-export-data-with-backend
  1720. '(bold nil "Test")
  1721. (org-export-create-backend
  1722. :transcoders
  1723. '((plain-text . (lambda (text info) "Success"))
  1724. (bold . (lambda (bold contents info) (concat contents "!")))))
  1725. '(:with-emphasize t)))))
  1726. ;;; Comments
  1727. (ert-deftest test-org-export/comments ()
  1728. "Test comments handling during export.
  1729. In particular, structure of the document mustn't be altered after
  1730. comments removal."
  1731. (should
  1732. (equal "Para1\n\nPara2\n"
  1733. (org-test-with-temp-text "
  1734. Para1
  1735. # Comment
  1736. # Comment
  1737. Para2"
  1738. (org-export-as (org-test-default-backend)))))
  1739. (should
  1740. (equal "Para1\n\nPara2\n"
  1741. (org-test-with-temp-text "
  1742. Para1
  1743. # Comment
  1744. Para2"
  1745. (org-export-as (org-test-default-backend)))))
  1746. (should
  1747. (equal "[fn:1] Para1\n\n\nPara2\n"
  1748. (org-test-with-temp-text "
  1749. \[fn:1] Para1
  1750. # Inside definition
  1751. # Outside definition
  1752. Para2"
  1753. (org-export-as (org-test-default-backend)))))
  1754. (should
  1755. (equal "[fn:1] Para1\n\nPara2\n"
  1756. (org-test-with-temp-text "
  1757. \[fn:1] Para1
  1758. # Inside definition
  1759. # Inside definition
  1760. Para2"
  1761. (org-export-as (org-test-default-backend)))))
  1762. (should
  1763. (equal "[fn:1] Para1\n\nPara2\n"
  1764. (org-test-with-temp-text "
  1765. \[fn:1] Para1
  1766. # Inside definition
  1767. Para2"
  1768. (org-export-as (org-test-default-backend)))))
  1769. (should
  1770. (equal "[fn:1] Para1\n\nPara2\n"
  1771. (org-test-with-temp-text "
  1772. \[fn:1] Para1
  1773. # Inside definition
  1774. Para2"
  1775. (org-export-as (org-test-default-backend)))))
  1776. (should
  1777. (equal "- item 1\n\n- item 2\n"
  1778. (org-test-with-temp-text "
  1779. - item 1
  1780. # Comment
  1781. - item 2"
  1782. (org-export-as (org-test-default-backend))))))
  1783. ;;; Export blocks
  1784. (ert-deftest test-org-export/export-block ()
  1785. "Test export blocks transcoding."
  1786. (should
  1787. (equal "Success!\n"
  1788. (org-test-with-temp-text
  1789. "#+BEGIN_EXPORT backend\nSuccess!\n#+END_EXPORT"
  1790. (org-export-as
  1791. (org-export-create-backend
  1792. :transcoders '((export-block . (lambda (b _c _i)
  1793. (org-element-property :value b)))
  1794. (section . (lambda (_s c _i) c))))))))
  1795. (should
  1796. (equal "Success!\n"
  1797. (org-test-with-temp-text
  1798. "#+BEGIN_EXPORT backend\nSuccess!\n#+END_EXPORT"
  1799. (org-export-as
  1800. (org-export-create-backend
  1801. :transcoders
  1802. (list
  1803. (cons 'export-block
  1804. (lambda (b _c _i)
  1805. (and (equal (org-element-property :type b) "BACKEND")
  1806. (org-element-property :value b))))
  1807. (cons 'section (lambda (_s c _i) c)))))))))
  1808. ;;; Export Snippets
  1809. (ert-deftest test-org-export/export-snippet ()
  1810. "Test export snippets transcoding."
  1811. ;; Standard test.
  1812. (org-test-with-temp-text "@@test:A@@@@t:B@@"
  1813. (let ((backend (org-test-default-backend)))
  1814. (setf (org-export-backend-name backend) 'test)
  1815. (setf (org-export-backend-transcoders backend)
  1816. (cons (cons 'export-snippet
  1817. (lambda (snippet contents info)
  1818. (when (eq (org-export-snippet-backend snippet) 'test)
  1819. (org-element-property :value snippet))))
  1820. (org-export-backend-transcoders backend)))
  1821. (let ((org-export-snippet-translation-alist nil))
  1822. (should (equal (org-export-as backend) "A\n")))
  1823. (let ((org-export-snippet-translation-alist '(("t" . "test"))))
  1824. (should (equal (org-export-as backend) "AB\n")))))
  1825. ;; Ignored export snippets do not remove any blank.
  1826. (should
  1827. (equal "begin end\n"
  1828. (org-test-with-parsed-data "begin @@test:A@@ end"
  1829. (org-export-data-with-backend
  1830. tree
  1831. (org-export-create-backend
  1832. :transcoders
  1833. '((paragraph . (lambda (paragraph contents info) contents))
  1834. (section . (lambda (section contents info) contents))))
  1835. info)))))
  1836. ;;; Filters
  1837. (ert-deftest test-org-export/filter-apply-functions ()
  1838. "Test `org-export-filter-apply-functions' specifications."
  1839. ;; Functions are applied in order and return values are reduced.
  1840. (should
  1841. (equal "210"
  1842. (org-export-filter-apply-functions
  1843. (list (lambda (value &rest _) (concat "1" value))
  1844. (lambda (value &rest _) (concat "2" value)))
  1845. "0" nil)))
  1846. ;; Functions returning nil are skipped.
  1847. (should
  1848. (equal "20"
  1849. (org-export-filter-apply-functions
  1850. (list #'ignore (lambda (value &rest _) (concat "2" value)))
  1851. "0" nil)))
  1852. ;; If all functions are skipped, return the initial value.
  1853. (should
  1854. (equal "0"
  1855. (org-export-filter-apply-functions (list #'ignore) "0" nil)))
  1856. ;; If any function returns the empty string, final value is the
  1857. ;; empty string.
  1858. (should
  1859. (equal ""
  1860. (org-export-filter-apply-functions
  1861. (list (lambda (value &rest _) "")
  1862. (lambda (value &rest _) (concat "2" value)))
  1863. "0" nil)))
  1864. ;; Any function returning the empty string short-circuits the
  1865. ;; process.
  1866. (should
  1867. (org-export-filter-apply-functions
  1868. (list (lambda (value &rest _) "")
  1869. (lambda (value &rest _) (error "This shouldn't happen")))
  1870. "0" nil)))
  1871. ;;; Footnotes
  1872. (ert-deftest test-org-export/footnote-first-reference-p ()
  1873. "Test `org-export-footnote-first-reference-p' specifications."
  1874. (should
  1875. (equal
  1876. '(t nil)
  1877. (org-test-with-temp-text "Text[fn:1][fn:1]\n\n[fn:1] Definition"
  1878. (let (result)
  1879. (org-export-as
  1880. (org-export-create-backend
  1881. :transcoders
  1882. `(,(cons 'footnote-reference
  1883. (lambda (f c i)
  1884. (push (org-export-footnote-first-reference-p f i)
  1885. result)
  1886. ""))
  1887. (section . (lambda (s c i) c))
  1888. (paragraph . (lambda (p c i) c))))
  1889. nil nil nil '(:with-footnotes t))
  1890. (nreverse result)))))
  1891. ;; Limit check to DATA, when non-nil.
  1892. (should
  1893. (equal
  1894. '(nil t)
  1895. (org-test-with-parsed-data "Text[fn:1]\n* H\nText[fn:1]\n\n[fn:1] D1"
  1896. (let (result)
  1897. (org-element-map tree 'footnote-reference
  1898. (lambda (ref)
  1899. (push
  1900. (org-export-footnote-first-reference-p
  1901. ref info (org-element-map tree 'headline #'identity info t))
  1902. result))
  1903. info)
  1904. (nreverse result)))))
  1905. (should
  1906. (equal
  1907. '(t nil)
  1908. (org-test-with-parsed-data "Text[fn:1]\n* H\nText[fn:1]\n\n[fn:1] D1"
  1909. (let (result)
  1910. (org-element-map tree 'footnote-reference
  1911. (lambda (ref)
  1912. (push (org-export-footnote-first-reference-p ref info) result))
  1913. info)
  1914. (nreverse result)))))
  1915. ;; If optional argument BODY-FIRST is non-nil, first find footnote
  1916. ;; in the main body of the document. Otherwise, enter footnote
  1917. ;; definitions when they are encountered.
  1918. (should
  1919. (equal
  1920. '(t nil)
  1921. (org-test-with-temp-text
  1922. ":BODY:\nText[fn:1][fn:2]\n:END:\n\n[fn:1] Definition[fn:2]\n\n[fn:2] Inner"
  1923. (let (result)
  1924. (org-export-as
  1925. (org-export-create-backend
  1926. :transcoders
  1927. `(,(cons 'footnote-reference
  1928. (lambda (f c i)
  1929. (when (org-element-lineage f '(drawer))
  1930. (push (org-export-footnote-first-reference-p f i nil)
  1931. result))
  1932. ""))
  1933. (drawer . (lambda (d c i) c))
  1934. (footnote-definition . (lambda (d c i) c))
  1935. (section . (lambda (s c i) c))
  1936. (paragraph . (lambda (p c i) c))))
  1937. nil nil nil '(:with-footnotes t))
  1938. (nreverse result)))))
  1939. (should
  1940. (equal
  1941. '(t t)
  1942. (org-test-with-temp-text
  1943. ":BODY:\nText[fn:1][fn:2]\n:END:\n\n[fn:1] Definition[fn:2]\n\n[fn:2] Inner"
  1944. (let (result)
  1945. (org-export-as
  1946. (org-export-create-backend
  1947. :transcoders
  1948. `(,(cons 'footnote-reference
  1949. (lambda (f c i)
  1950. (when (org-element-lineage f '(drawer))
  1951. (push (org-export-footnote-first-reference-p f i nil t)
  1952. result))
  1953. ""))
  1954. (drawer . (lambda (d c i) c))
  1955. (footnote-definition . (lambda (d c i) c))
  1956. (section . (lambda (s c i) c))
  1957. (paragraph . (lambda (p c i) c))))
  1958. nil nil nil '(:with-footnotes t))
  1959. (nreverse result))))))
  1960. (ert-deftest test-org-export/get-footnote-number ()
  1961. "Test `org-export-get-footnote-number' specifications."
  1962. (should
  1963. (equal '(1 2 1)
  1964. (org-test-with-parsed-data
  1965. "Text[fn:1][fn:2][fn:1]\n\n[fn:1] Def\n[fn:2] Def"
  1966. (org-element-map tree 'footnote-reference
  1967. (lambda (ref) (org-export-get-footnote-number ref info))
  1968. info))))
  1969. ;; Anonymous footnotes all get a new number.
  1970. (should
  1971. (equal '(1 2)
  1972. (org-test-with-parsed-data
  1973. "Text[fn::anon1][fn::anon2]"
  1974. (org-element-map tree 'footnote-reference
  1975. (lambda (ref) (org-export-get-footnote-number ref info))
  1976. info))))
  1977. ;; Test nested footnotes order.
  1978. (should
  1979. (equal
  1980. '((1 . "1") (2 . "2") (3 . "3") (3 . "3") (4))
  1981. (org-test-with-parsed-data
  1982. "Text[fn:1:A[fn:2]] [fn:3].\n\n[fn:2] B [fn:3] [fn::D].\n\n[fn:3] C."
  1983. (org-element-map tree 'footnote-reference
  1984. (lambda (ref)
  1985. (cons (org-export-get-footnote-number ref info)
  1986. (org-element-property :label ref)))
  1987. info))))
  1988. ;; Limit number to provided DATA, when non-nil.
  1989. (should
  1990. (equal
  1991. '(1)
  1992. (org-test-with-parsed-data
  1993. "Text[fn:1]\n* H\nText[fn:2]\n\n[fn:1] D1\n[fn:2] D2"
  1994. (org-element-map tree 'footnote-reference
  1995. (lambda (ref)
  1996. (org-export-get-footnote-number
  1997. ref info (org-element-map tree 'headline #'identity info t)))
  1998. info))))
  1999. (should
  2000. (equal
  2001. '(1 2)
  2002. (org-test-with-parsed-data
  2003. "Text[fn:1]\n* H\nText[fn:2]\n\n[fn:1] D1\n[fn:2]"
  2004. (org-element-map tree 'footnote-reference
  2005. (lambda (ref) (org-export-get-footnote-number ref info))
  2006. info))))
  2007. ;; With a non-nil BODY-FIRST optional argument, first check body,
  2008. ;; then footnote definitions.
  2009. (should
  2010. (equal
  2011. '(("1" . 1) ("2" . 2) ("3" . 3) ("3" . 3))
  2012. (org-test-with-parsed-data
  2013. "Text[fn:1][fn:2][fn:3]\n\n[fn:1] Def[fn:3]\n[fn:2] Def\n[fn:3] Def"
  2014. (org-element-map tree 'footnote-reference
  2015. (lambda (ref)
  2016. (cons (org-element-property :label ref)
  2017. (org-export-get-footnote-number ref info nil t)))
  2018. info))))
  2019. (should
  2020. (equal
  2021. '(("1" . 1) ("2" . 3) ("3" . 2) ("3" . 2))
  2022. (org-test-with-parsed-data
  2023. "Text[fn:1][fn:2][fn:3]\n\n[fn:1] Def[fn:3]\n[fn:2] Def\n[fn:3] Def"
  2024. (org-element-map tree 'footnote-reference
  2025. (lambda (ref)
  2026. (cons (org-element-property :label ref)
  2027. (org-export-get-footnote-number ref info nil)))
  2028. info)))))
  2029. (ert-deftest test-org-export/get-footnote-definition ()
  2030. "Test `org-export-get-footnote-definition' specifications."
  2031. ;; Standard test.
  2032. (should
  2033. (equal "A\n"
  2034. (org-element-interpret-data
  2035. (org-test-with-parsed-data "Text[fn:1]\n\n[fn:1] A"
  2036. (org-export-get-footnote-definition
  2037. (org-element-map tree 'footnote-reference #'identity nil t)
  2038. info)))))
  2039. ;; Raise an error if no definition is found.
  2040. (should-error
  2041. (org-test-with-parsed-data "Text[fn:1]"
  2042. (org-export-get-footnote-definition
  2043. (org-element-map tree 'footnote-reference #'identity nil t)
  2044. info)))
  2045. ;; Find inline definitions.
  2046. (should
  2047. (equal "A"
  2048. (org-element-interpret-data
  2049. (org-test-with-parsed-data "Text[fn:1:A]"
  2050. (org-export-get-footnote-definition
  2051. (org-element-map tree 'footnote-reference #'identity nil t)
  2052. info)))))
  2053. ;; Find anonymous definitions.
  2054. (should
  2055. (equal "A"
  2056. (org-element-interpret-data
  2057. (org-test-with-parsed-data "Text[fn::A]"
  2058. (org-export-get-footnote-definition
  2059. (org-element-map tree 'footnote-reference #'identity nil t)
  2060. info)))))
  2061. ;; Find empty definitions.
  2062. (should
  2063. (equal ""
  2064. (org-element-interpret-data
  2065. (org-test-with-parsed-data "Text[fn:1]\n\n[fn:1]"
  2066. (org-export-get-footnote-definition
  2067. (org-element-map tree 'footnote-reference #'identity nil t)
  2068. info)))))
  2069. (should
  2070. (equal ""
  2071. (org-element-interpret-data
  2072. (org-test-with-parsed-data "Text[fn:1:]"
  2073. (org-export-get-footnote-definition
  2074. (org-element-map tree 'footnote-reference #'identity nil t)
  2075. info)))))
  2076. (should
  2077. (equal ""
  2078. (org-element-interpret-data
  2079. (org-test-with-parsed-data "Text[fn::]"
  2080. (org-export-get-footnote-definition
  2081. (org-element-map tree 'footnote-reference #'identity nil t)
  2082. info))))))
  2083. (ert-deftest test-org-export/collect-footnote-definitions ()
  2084. "Test `org-export-collect-footnote-definitions' specifications."
  2085. (should
  2086. (= 4
  2087. (org-test-with-parsed-data "Text[fn:1:A[fn:2]] [fn:3].
  2088. \[fn:2] B [fn:3] [fn::D].
  2089. \[fn:3] C."
  2090. (length (org-export-collect-footnote-definitions info)))))
  2091. ;; Limit number to provided DATA, when non-nil.
  2092. (should
  2093. (equal
  2094. '((1 "2"))
  2095. (org-test-with-parsed-data
  2096. "Text[fn:1]\n* H\nText[fn:2]\n\n[fn:1] D1\n[fn:2] D2"
  2097. (mapcar #'butlast
  2098. (org-export-collect-footnote-definitions
  2099. info (org-element-map tree 'headline #'identity info t))))))
  2100. (should
  2101. (equal
  2102. '((1 "1") (2 "2"))
  2103. (org-test-with-parsed-data
  2104. "Text[fn:1]\n* H\nText[fn:2]\n\n[fn:1] D1\n[fn:2] D2"
  2105. (mapcar #'butlast (org-export-collect-footnote-definitions info)))))
  2106. ;; With optional argument BODY-FIRST, first check body, then
  2107. ;; footnote definitions.
  2108. (should
  2109. (equal '("1" "3" "2" nil)
  2110. (org-test-with-parsed-data "Text[fn:1:A[fn:2]] [fn:3].
  2111. \[fn:2] B [fn:3] [fn::D].
  2112. \[fn:3] C."
  2113. (mapcar (lambda (e) (nth 1 e))
  2114. (org-export-collect-footnote-definitions info nil t)))))
  2115. (should-not
  2116. (equal '("1" "3" "2" nil)
  2117. (org-test-with-parsed-data "Text[fn:1:A[fn:2]] [fn:3].
  2118. \[fn:2] B [fn:3] [fn::D].
  2119. \[fn:3] C."
  2120. (mapcar (lambda (e) (nth 1 e))
  2121. (org-export-collect-footnote-definitions info))))))
  2122. (ert-deftest test-org-export/footnotes ()
  2123. "Miscellaneous tests on footnotes."
  2124. (let ((org-footnote-section nil)
  2125. (org-export-with-footnotes t))
  2126. ;; Read every type of footnote.
  2127. (should
  2128. (equal
  2129. '((1 . "A\n") (2 . "C") (3 . "D"))
  2130. (org-test-with-parsed-data
  2131. "Text[fn:1] [fn:label:C] [fn::D]\n\n[fn:1] A\n"
  2132. (org-element-map tree 'footnote-reference
  2133. (lambda (ref)
  2134. (let ((def (org-export-get-footnote-definition ref info)))
  2135. (cons (org-export-get-footnote-number ref info)
  2136. (if (eq (org-element-property :type ref) 'inline) (car def)
  2137. (car (org-element-contents
  2138. (car (org-element-contents def))))))))
  2139. info))))
  2140. ;; Export nested footnote in invisible definitions.
  2141. (should
  2142. (= 2
  2143. (org-test-with-temp-text "Text[fn:1]\n\n[fn:1] B [fn:2]\n\n[fn:2] C."
  2144. (narrow-to-region (point) (line-end-position))
  2145. (catch 'exit
  2146. (org-export-as
  2147. (org-export-create-backend
  2148. :transcoders
  2149. '((section
  2150. .
  2151. (lambda (s c i)
  2152. (throw 'exit (length
  2153. (org-export-collect-footnote-definitions
  2154. i))))))))))))
  2155. ;; Export footnotes defined outside parsing scope.
  2156. (should
  2157. (string-match
  2158. "Out of scope"
  2159. (org-test-with-temp-text "[fn:1] Out of scope
  2160. * Title
  2161. <point>Paragraph[fn:1]"
  2162. (org-export-as (org-test-default-backend) 'subtree))))
  2163. (should
  2164. (string-match
  2165. "Out of scope"
  2166. (org-test-with-temp-text "[fn:1] Out of scope
  2167. * Title
  2168. <point>Paragraph[fn:1]"
  2169. (narrow-to-region (point) (point-max))
  2170. (org-export-as (org-test-default-backend)))))
  2171. ;; Export nested footnotes defined outside parsing scope.
  2172. (should
  2173. (string-match
  2174. "Very out of scope"
  2175. (org-test-with-temp-text "
  2176. \[fn:1] Out of scope[fn:2]
  2177. \[fn:2] Very out of scope
  2178. * Title
  2179. <point>Paragraph[fn:1]"
  2180. (org-export-as (org-test-default-backend) 'subtree))))
  2181. (should
  2182. (string-match
  2183. "Very out of scope"
  2184. (org-test-with-temp-text "
  2185. \[fn:1] Out of scope[fn:2]
  2186. \[fn:2] Very out of scope
  2187. * Title
  2188. <point>Paragraph[fn:1]"
  2189. (narrow-to-region (point) (point-max))
  2190. (org-export-as (org-test-default-backend)))))
  2191. (should
  2192. (string-match
  2193. "D2"
  2194. (org-test-with-temp-text "
  2195. \[fn:1] Out of scope[fn:2:D2]
  2196. * Title
  2197. <point>Paragraph[fn:1]"
  2198. (narrow-to-region (point) (point-max))
  2199. (org-export-as (org-test-default-backend)))))
  2200. ;; Export footnotes in pruned parts of tree.
  2201. (should
  2202. (string-match
  2203. "Definition"
  2204. (let ((org-export-exclude-tags '("noexport")))
  2205. (org-test-with-temp-text
  2206. "* H\nText[fn:1]\n* H2 :noexport:\n[fn:1] Definition"
  2207. (org-export-as (org-test-default-backend))))))
  2208. (should
  2209. (string-match
  2210. "Definition"
  2211. (let ((org-export-select-tags '("export")))
  2212. (org-test-with-temp-text
  2213. "* H :export:\nText[fn:1]\n* H2\n[fn:1] Definition"
  2214. (org-export-as (org-test-default-backend))))))
  2215. ;; Export nested footnotes in pruned parts of tree.
  2216. (should
  2217. (string-match
  2218. "D2"
  2219. (let ((org-export-exclude-tags '("noexport")))
  2220. (org-test-with-temp-text
  2221. "* H\nText[fn:1]\n* H2 :noexport:\n[fn:1] D1[fn:2]\n\n[fn:2] D2"
  2222. (org-export-as (org-test-default-backend))))))
  2223. (should
  2224. (string-match
  2225. "D2"
  2226. (let ((org-export-select-tags '("export")))
  2227. (org-test-with-temp-text
  2228. "* H :export:\nText[fn:1]\n* H2\n[fn:1] D1[fn:2]\n\n[fn:2] D2"
  2229. (org-export-as (org-test-default-backend))))))
  2230. ;; Handle uninterpreted data in pruned footnote definitions.
  2231. (should-not
  2232. (string-match
  2233. "|"
  2234. (let ((org-export-with-tables nil))
  2235. (org-test-with-temp-text
  2236. "* H\nText[fn:1]\n* H2 :noexport:\n[fn:1]\n| a |"
  2237. (org-export-as (org-test-default-backend))))))
  2238. ;; Footnotes without a definition should throw an error.
  2239. (should-error
  2240. (org-test-with-parsed-data "Text[fn:1]"
  2241. (org-export-get-footnote-definition
  2242. (org-element-map tree 'footnote-reference #'identity info t) info)))
  2243. ;; Footnote section should be ignored in TOC and in headlines
  2244. ;; numbering.
  2245. (should
  2246. (= 1 (let ((org-footnote-section "Footnotes"))
  2247. (length (org-test-with-parsed-data "* H1\n* Footnotes\n"
  2248. (org-export-collect-headlines info))))))
  2249. (should
  2250. (equal '(2)
  2251. (let ((org-footnote-section "Footnotes"))
  2252. (org-test-with-parsed-data "* H1\n* Footnotes\n* H2"
  2253. (org-element-map tree 'headline
  2254. (lambda (hl)
  2255. (when (equal (org-element-property :raw-value hl) "H2")
  2256. (org-export-get-headline-number hl info)))
  2257. info t)))))))
  2258. ;;; Headlines and Inlinetasks
  2259. (ert-deftest test-org-export/get-relative-level ()
  2260. "Test `org-export-get-relative-level' specifications."
  2261. ;; Standard test.
  2262. (should
  2263. (equal '(1 2)
  2264. (let ((org-odd-levels-only nil))
  2265. (org-test-with-parsed-data "* Headline 1\n** Headline 2"
  2266. (org-element-map tree 'headline
  2267. (lambda (h) (org-export-get-relative-level h info))
  2268. info)))))
  2269. ;; Missing levels
  2270. (should
  2271. (equal '(1 3)
  2272. (let ((org-odd-levels-only nil))
  2273. (org-test-with-parsed-data "** Headline 1\n**** Headline 2"
  2274. (org-element-map tree 'headline
  2275. (lambda (h) (org-export-get-relative-level h info))
  2276. info))))))
  2277. (ert-deftest test-org-export/low-level-p ()
  2278. "Test `org-export-low-level-p' specifications."
  2279. (should
  2280. (equal
  2281. '(no yes)
  2282. (let ((org-odd-levels-only nil))
  2283. (org-test-with-parsed-data "* Headline 1\n** Headline 2"
  2284. (org-element-map tree 'headline
  2285. (lambda (h) (if (org-export-low-level-p h info) 'yes 'no))
  2286. (plist-put info :headline-levels 1)))))))
  2287. (ert-deftest test-org-export/get-headline-number ()
  2288. "Test `org-export-get-headline-number' specifications."
  2289. ;; Standard test.
  2290. (should
  2291. (equal
  2292. '((1) (1 1))
  2293. (let ((org-odd-levels-only nil))
  2294. (org-test-with-parsed-data "* Headline 1\n** Headline 2"
  2295. (org-element-map tree 'headline
  2296. (lambda (h) (org-export-get-headline-number h info))
  2297. info)))))
  2298. ;; Missing levels are replaced with 0.
  2299. (should
  2300. (equal
  2301. '((1) (1 0 1))
  2302. (let ((org-odd-levels-only nil))
  2303. (org-test-with-parsed-data "* Headline 1\n*** Headline 2"
  2304. (org-element-map tree 'headline
  2305. (lambda (h) (org-export-get-headline-number h info))
  2306. info))))))
  2307. (ert-deftest test-org-export/numbered-headline-p ()
  2308. "Test `org-export-numbered-headline-p' specifications."
  2309. ;; If `:section-numbers' is nil, never number headlines.
  2310. (should-not
  2311. (org-test-with-parsed-data "* Headline"
  2312. (org-element-map tree 'headline
  2313. (lambda (h) (org-export-numbered-headline-p h info))
  2314. (plist-put info :section-numbers nil))))
  2315. ;; If `:section-numbers' is a number, only number headlines with
  2316. ;; a level greater that it.
  2317. (should
  2318. (equal
  2319. '(yes no)
  2320. (org-test-with-parsed-data "* Headline 1\n** Headline 2"
  2321. (org-element-map tree 'headline
  2322. (lambda (h) (if (org-export-numbered-headline-p h info) 'yes 'no))
  2323. (plist-put info :section-numbers 1)))))
  2324. ;; Otherwise, headlines are always numbered.
  2325. (should
  2326. (org-test-with-parsed-data "* Headline"
  2327. (org-element-map tree 'headline
  2328. (lambda (h) (org-export-numbered-headline-p h info))
  2329. (plist-put info :section-numbers t))))
  2330. ;; With #+OPTIONS: num:nil all headlines are unnumbered.
  2331. (should-not
  2332. (org-test-with-parsed-data "* H\n#+OPTIONS: num:nil"
  2333. (org-export-numbered-headline-p
  2334. (org-element-map tree 'headline 'identity info t)
  2335. info)))
  2336. ;; Headlines with a non-nil UNNUMBERED property are not numbered.
  2337. (should-not
  2338. (org-test-with-parsed-data "* H\n:PROPERTIES:\n:UNNUMBERED: t\n:END:"
  2339. (org-export-numbered-headline-p
  2340. (org-element-map tree 'headline #'identity info t)
  2341. info)))
  2342. ;; UNNUMBERED ignores inheritance. Any non-nil value among
  2343. ;; ancestors disables numbering.
  2344. (should
  2345. (org-test-with-parsed-data
  2346. "* H
  2347. :PROPERTIES:
  2348. :UNNUMBERED: t
  2349. :END:
  2350. ** H2
  2351. :PROPERTIES:
  2352. :UNNUMBERED: nil
  2353. :END:
  2354. *** H3"
  2355. (cl-every
  2356. (lambda (h) (not (org-export-numbered-headline-p h info)))
  2357. (org-element-map tree 'headline #'identity info)))))
  2358. (ert-deftest test-org-export/number-to-roman ()
  2359. "Test `org-export-number-to-roman' specifications."
  2360. ;; If number is negative, return it as a string.
  2361. (should (equal (org-export-number-to-roman -1) "-1"))
  2362. ;; Otherwise, return it as a roman number.
  2363. (should (equal (org-export-number-to-roman 1449) "MCDXLIX")))
  2364. (ert-deftest test-org-export/get-optional-title ()
  2365. "Test `org-export-get-alt-title' specifications."
  2366. ;; If ALT_TITLE property is defined, use it.
  2367. (should
  2368. (equal '("opt")
  2369. (org-test-with-parsed-data
  2370. "* Headline\n:PROPERTIES:\n:ALT_TITLE: opt\n:END:"
  2371. (org-export-get-alt-title
  2372. (org-element-map tree 'headline 'identity info t)
  2373. info))))
  2374. ;; Otherwise, fall-back to regular title.
  2375. (should
  2376. (equal '("Headline")
  2377. (org-test-with-parsed-data "* Headline"
  2378. (org-export-get-alt-title
  2379. (org-element-map tree 'headline 'identity info t)
  2380. info)))))
  2381. (ert-deftest test-org-export/get-tags ()
  2382. "Test `org-export-get-tags' specifications."
  2383. ;; Standard test: tags which are not a select tag, an exclude tag,
  2384. ;; or specified as optional argument shouldn't be ignored.
  2385. (should
  2386. (org-test-with-parsed-data "* Headline :tag:"
  2387. (org-export-get-tags (org-element-map tree 'headline 'identity info t)
  2388. info)))
  2389. ;; Tags provided in the optional argument are ignored.
  2390. (should-not
  2391. (org-test-with-parsed-data "* Headline :ignore:"
  2392. (org-export-get-tags (org-element-map tree 'headline 'identity info t)
  2393. info '("ignore"))))
  2394. ;; Allow tag inheritance.
  2395. (should
  2396. (equal
  2397. '(("tag") ("tag"))
  2398. (org-test-with-parsed-data "* Headline :tag:\n** Sub-heading"
  2399. (org-element-map tree 'headline
  2400. (lambda (hl) (org-export-get-tags hl info nil t)) info))))
  2401. ;; Tag inheritance checks FILETAGS keywords.
  2402. (should
  2403. (equal
  2404. '(("a" "b" "tag"))
  2405. (org-test-with-parsed-data "#+FILETAGS: :a:b:\n* Headline :tag:"
  2406. (org-element-map tree 'headline
  2407. (lambda (hl) (org-export-get-tags hl info nil t)) info)))))
  2408. (ert-deftest test-org-export/get-node-property ()
  2409. "Test`org-export-get-node-property' specifications."
  2410. ;; Standard test.
  2411. (should
  2412. (equal "value"
  2413. (org-test-with-parsed-data "* Headline
  2414. :PROPERTIES:
  2415. :prop: value
  2416. :END:"
  2417. (org-export-get-node-property
  2418. :PROP (org-element-map tree 'headline 'identity nil t)))))
  2419. ;; Test inheritance.
  2420. (should
  2421. (equal "value"
  2422. (org-test-with-parsed-data "* Parent
  2423. :PROPERTIES:
  2424. :prop: value
  2425. :END:
  2426. ** Headline
  2427. Paragraph"
  2428. (org-export-get-node-property
  2429. :PROP (org-element-map tree 'paragraph 'identity nil t) t))))
  2430. ;; Cannot return a value before the first headline.
  2431. (should-not
  2432. (org-test-with-parsed-data "Paragraph
  2433. * Headline
  2434. :PROPERTIES:
  2435. :prop: value
  2436. :END:"
  2437. (org-export-get-node-property
  2438. :PROP (org-element-map tree 'paragraph 'identity nil t)))))
  2439. (ert-deftest test-org-export/get-category ()
  2440. "Test `org-export-get-category' specifications."
  2441. ;; Standard test.
  2442. (should
  2443. (equal "value"
  2444. (org-test-with-parsed-data "* Headline
  2445. :PROPERTIES:
  2446. :CATEGORY: value
  2447. :END:"
  2448. (org-export-get-category
  2449. (org-element-map tree 'headline 'identity nil t) info))))
  2450. ;; Test inheritance from a parent headline.
  2451. (should
  2452. (equal '("value" "value")
  2453. (org-test-with-parsed-data "* Headline1
  2454. :PROPERTIES:
  2455. :CATEGORY: value
  2456. :END:
  2457. ** Headline2"
  2458. (org-element-map tree 'headline
  2459. (lambda (hl) (org-export-get-category hl info)) info))))
  2460. ;; Test inheritance from #+CATEGORY keyword
  2461. (should
  2462. (equal "value"
  2463. (org-test-with-parsed-data "#+CATEGORY: value
  2464. * Headline"
  2465. (org-export-get-category
  2466. (org-element-map tree 'headline 'identity nil t) info))))
  2467. ;; Test inheritance from file name.
  2468. (should
  2469. (equal "test"
  2470. (org-test-with-parsed-data "* Headline"
  2471. (let ((info (plist-put info :input-file "~/test.org")))
  2472. (org-export-get-category
  2473. (org-element-map tree 'headline 'identity nil t) info)))))
  2474. ;; Fall-back value.
  2475. (should
  2476. (equal "???"
  2477. (org-test-with-parsed-data "* Headline"
  2478. (org-export-get-category
  2479. (org-element-map tree 'headline 'identity nil t) info)))))
  2480. (ert-deftest test-org-export/first-sibling-p ()
  2481. "Test `org-export-first-sibling-p' specifications."
  2482. ;; Standard test.
  2483. (should
  2484. (equal
  2485. '(yes yes no)
  2486. (org-test-with-parsed-data "* H\n** H 2\n** H 3"
  2487. (org-element-map tree 'headline
  2488. (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
  2489. info))))
  2490. (should
  2491. (equal '(yes no)
  2492. (org-test-with-parsed-data "- item\n\n para"
  2493. (org-element-map tree 'paragraph
  2494. (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
  2495. info))))
  2496. ;; Ignore sections for headlines.
  2497. (should
  2498. (equal '(yes yes)
  2499. (org-test-with-parsed-data "* H\nSection\n** H 2"
  2500. (org-element-map tree 'headline
  2501. (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
  2502. info))))
  2503. ;; Ignore headlines not exported.
  2504. (should
  2505. (equal
  2506. '(yes)
  2507. (let ((org-export-exclude-tags '("ignore")))
  2508. (org-test-with-parsed-data "* Headline :ignore:\n* Headline 2"
  2509. (org-element-map tree 'headline
  2510. (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
  2511. info))))))
  2512. (ert-deftest test-org-export/last-sibling-p ()
  2513. "Test `org-export-last-sibling-p' specifications."
  2514. ;; Standard test.
  2515. (should
  2516. (equal
  2517. '(yes no yes)
  2518. (org-test-with-parsed-data "* Headline\n** Headline 2\n** Headline 3"
  2519. (org-element-map tree 'headline
  2520. (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
  2521. info))))
  2522. (should
  2523. (equal '(no yes)
  2524. (org-test-with-parsed-data "- item\n\n para"
  2525. (org-element-map tree 'paragraph
  2526. (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
  2527. info))))
  2528. ;; Ignore headlines not exported.
  2529. (should
  2530. (equal
  2531. '(yes)
  2532. (let ((org-export-exclude-tags '("ignore")))
  2533. (org-test-with-parsed-data "* Headline\n* Headline 2 :ignore:"
  2534. (org-element-map tree 'headline
  2535. (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
  2536. info))))))
  2537. (ert-deftest test-org-export/handle-inlinetasks ()
  2538. "Test inlinetask export."
  2539. ;; Inlinetask with an exclude tag.
  2540. (when (featurep 'org-inlinetask)
  2541. (should
  2542. (equal
  2543. ""
  2544. (let ((org-inlinetask-min-level 3)
  2545. org-export-filter-body-functions
  2546. org-export-filter-final-output-functions)
  2547. (org-test-with-temp-text "*** Inlinetask :noexp:\nContents\n*** end"
  2548. (org-export-as (org-test-default-backend)
  2549. nil nil nil '(:exclude-tags ("noexp")))))))
  2550. ;; Inlinetask with an include tag.
  2551. (should
  2552. (equal
  2553. "* H2\n*** Inline :exp:\n"
  2554. (let ((org-inlinetask-min-level 3)
  2555. (org-tags-column 0))
  2556. (org-test-with-temp-text "* H1\n* H2\n*** Inline :exp:"
  2557. (org-export-as (org-test-default-backend)
  2558. nil nil nil '(:select-tags ("exp")))))))
  2559. ;; Ignore inlinetask with a TODO keyword and tasks excluded.
  2560. (should
  2561. (equal ""
  2562. (let ((org-todo-keywords '((sequence "TODO" "DONE")))
  2563. (org-inlinetask-min-level 3)
  2564. org-export-filter-body-functions
  2565. org-export-filter-final-output-functions)
  2566. (org-test-with-temp-text "*** TODO Inline"
  2567. (org-export-as (org-test-default-backend)
  2568. nil nil nil '(:with-tasks nil))))))))
  2569. ;;; Keywords
  2570. (ert-deftest test-org-export/get-date ()
  2571. "Test `org-export-get-date' specifications."
  2572. ;; Return a properly formatted string when
  2573. ;; `org-export-date-timestamp-format' is non-nil and DATE keyword
  2574. ;; consists in a single timestamp.
  2575. (should
  2576. (equal "29 03 2012"
  2577. (let ((org-export-date-timestamp-format "%d %m %Y"))
  2578. (org-test-with-parsed-data "#+DATE: <2012-03-29 Thu>"
  2579. (org-export-get-date info)))))
  2580. ;; Return a secondary string otherwise.
  2581. (should-not
  2582. (stringp
  2583. (let ((org-export-date-timestamp-format nil))
  2584. (org-test-with-parsed-data "#+DATE: <2012-03-29 Thu>"
  2585. (org-export-get-date info)))))
  2586. (should
  2587. (equal '("Date")
  2588. (org-test-with-parsed-data "#+DATE: Date"
  2589. (org-export-get-date info))))
  2590. ;; Optional argument has precedence over
  2591. ;; `org-export-date-timestamp-format'.
  2592. (should
  2593. (equal "29 03"
  2594. (let ((org-export-date-timestamp-format "%d %m %Y"))
  2595. (org-test-with-parsed-data "#+DATE: <2012-03-29 Thu>"
  2596. (org-export-get-date info "%d %m"))))))
  2597. ;;; Links
  2598. (ert-deftest test-org-export/custom-protocol-maybe ()
  2599. "Test `org-export-custom-protocol-maybe' specifications."
  2600. (should
  2601. (string-match
  2602. "success"
  2603. (progn
  2604. (org-link-set-parameters "foo" :export (lambda (p d f) "success"))
  2605. (org-export-string-as
  2606. "[[foo:path]]"
  2607. (org-export-create-backend
  2608. :name 'test
  2609. :transcoders
  2610. '((section . (lambda (s c i) c))
  2611. (paragraph . (lambda (p c i) c))
  2612. (link . (lambda (l c i)
  2613. (or (org-export-custom-protocol-maybe l c 'test)
  2614. "failure")))))))))
  2615. (should-not
  2616. (string-match
  2617. "success"
  2618. (progn
  2619. (org-link-set-parameters
  2620. "foo" :export (lambda (p d f) (and (eq f 'test) "success")))
  2621. (org-export-string-as
  2622. "[[foo:path]]"
  2623. (org-export-create-backend
  2624. :name 'no-test
  2625. :transcoders
  2626. '((section . (lambda (s c i) c))
  2627. (paragraph . (lambda (p c i) c))
  2628. (link . (lambda (l c i)
  2629. (or (org-export-custom-protocol-maybe l c 'no-test)
  2630. "failure")))))))))
  2631. ;; Ignore anonymous back-ends.
  2632. (should-not
  2633. (string-match
  2634. "success"
  2635. (progn
  2636. (org-link-set-parameters
  2637. "foo" :export (lambda (p d f) (and (eq f 'test) "success")))
  2638. (org-export-string-as
  2639. "[[foo:path]]"
  2640. (org-export-create-backend
  2641. :transcoders
  2642. '((section . (lambda (s c i) c))
  2643. (paragraph . (lambda (p c i) c))
  2644. (link . (lambda (l c i)
  2645. (or (org-export-custom-protocol-maybe l c nil)
  2646. "failure"))))))))))
  2647. (ert-deftest test-org-export/get-coderef-format ()
  2648. "Test `org-export-get-coderef-format' specifications."
  2649. ;; A link without description returns "%s"
  2650. (should (equal (org-export-get-coderef-format "(ref:line)" nil)
  2651. "%s"))
  2652. ;; Return "%s" when path is matched within description.
  2653. (should (equal (org-export-get-coderef-format "path" "desc (path)")
  2654. "desc %s"))
  2655. ;; Otherwise return description.
  2656. (should (equal (org-export-get-coderef-format "path" "desc")
  2657. "desc")))
  2658. (ert-deftest test-org-export/inline-image-p ()
  2659. "Test `org-export-inline-image-p' specifications."
  2660. (should
  2661. (org-export-inline-image-p
  2662. (org-test-with-temp-text "[[#id]]"
  2663. (org-element-map (org-element-parse-buffer) 'link 'identity nil t))
  2664. '(("custom-id" . "id")))))
  2665. (ert-deftest test-org-export/insert-image-links ()
  2666. "Test `org-export-insert-image-links' specifications."
  2667. (should-not
  2668. (member "file"
  2669. (org-test-with-parsed-data "[[http://orgmode.org][file:image.png]]"
  2670. (org-element-map tree 'link
  2671. (lambda (l) (org-element-property :type l))))))
  2672. (should
  2673. (member "file"
  2674. (org-test-with-parsed-data "[[http://orgmode.org][file:image.png]]"
  2675. (org-element-map (org-export-insert-image-links tree info) 'link
  2676. (lambda (l) (org-element-property :type l))))))
  2677. ;; Properly set `:parent' property when replace contents with image
  2678. ;; link.
  2679. (should
  2680. (memq 'link
  2681. (org-test-with-parsed-data "[[http://orgmode.org][file:image.png]]"
  2682. (org-element-map (org-export-insert-image-links tree info) 'link
  2683. (lambda (l)
  2684. (org-element-type (org-element-property :parent l)))))))
  2685. ;; With optional argument RULES, recognize different links as
  2686. ;; images.
  2687. (should-not
  2688. (member "file"
  2689. (org-test-with-parsed-data "[[http://orgmode.org][file:image.xxx]]"
  2690. (org-element-map (org-export-insert-image-links tree info) 'link
  2691. (lambda (l) (org-element-property :type l))))))
  2692. (should
  2693. (member "file"
  2694. (org-test-with-parsed-data "[[http://orgmode.org][file:image.xxx]]"
  2695. (org-element-map
  2696. (org-export-insert-image-links tree info '(("file" . "xxx")))
  2697. 'link
  2698. (lambda (l) (org-element-property :type l)))))))
  2699. (ert-deftest test-org-export/fuzzy-link ()
  2700. "Test fuzzy links specifications."
  2701. ;; Link to an headline should return headline's number.
  2702. (should
  2703. ;; Note: Headline's number is in fact a list of numbers.
  2704. (equal '(2)
  2705. (org-test-with-parsed-data
  2706. "Paragraph.\n* Head1\n* Head2\n* Head3\n[[Head2]]"
  2707. (org-element-map tree 'link
  2708. (lambda (link)
  2709. (org-export-get-ordinal
  2710. (org-export-resolve-fuzzy-link link info) info)) info t))))
  2711. ;; Link to a target in an item should return item's number.
  2712. (should
  2713. ;; Note: Item's number is in fact a list of numbers.
  2714. (equal '(1 2)
  2715. (org-test-with-parsed-data
  2716. "- Item1\n - Item11\n - <<test>>Item12\n- Item2\n\n\n[[test]]"
  2717. (org-element-map tree 'link
  2718. (lambda (link)
  2719. (org-export-get-ordinal
  2720. (org-export-resolve-fuzzy-link link info) info)) info t))))
  2721. ;; Link to a target in a footnote should return footnote's number.
  2722. (should
  2723. (equal '(2 3)
  2724. (org-test-with-parsed-data "
  2725. Paragraph[fn:1][fn:2][fn:lbl3:C<<target>>][[test]][[target]]
  2726. \[fn:1] A
  2727. \[fn:2] <<test>>B"
  2728. (org-element-map tree 'link
  2729. (lambda (link)
  2730. (org-export-get-ordinal
  2731. (org-export-resolve-fuzzy-link link info) info)) info))))
  2732. ;; Link to a named element should return sequence number of that
  2733. ;; element.
  2734. (should
  2735. (= 2
  2736. (org-test-with-parsed-data
  2737. "#+NAME: tbl1\n|1|2|\n#+NAME: tbl2\n|3|4|\n#+NAME: tbl3\n|5|6|\n[[tbl2]]"
  2738. (org-element-map tree 'link
  2739. (lambda (link)
  2740. (org-export-get-ordinal
  2741. (org-export-resolve-fuzzy-link link info) info)) info t))))
  2742. ;; Link to a target not within an item, a table, a footnote
  2743. ;; reference or definition should return section number.
  2744. (should
  2745. (equal '(2)
  2746. (org-test-with-parsed-data
  2747. "* Head1\n* Head2\nParagraph<<target>>\n* Head3\n[[target]]"
  2748. (org-element-map tree 'link
  2749. (lambda (link)
  2750. (org-export-get-ordinal
  2751. (org-export-resolve-fuzzy-link link info) info)) info t))))
  2752. ;; Space are not significant when matching a fuzzy link.
  2753. (should
  2754. (org-test-with-parsed-data "* Head 1\n[[Head\n 1]]"
  2755. (org-element-map tree 'link
  2756. (lambda (link) (org-export-resolve-fuzzy-link link info))
  2757. info t)))
  2758. ;; Statistics cookies are ignored for headline match.
  2759. (should
  2760. (org-test-with-parsed-data "* Head [0/0]\n[[Head]]"
  2761. (org-element-map tree 'link
  2762. (lambda (link) (org-export-resolve-fuzzy-link link info))
  2763. info t)))
  2764. (should
  2765. (org-test-with-parsed-data "* Head [100%]\n[[Head]]"
  2766. (org-element-map tree 'link
  2767. (lambda (link) (org-export-resolve-fuzzy-link link info))
  2768. info t))))
  2769. (defun test-org-gen-loc-list(text type)
  2770. (org-test-with-parsed-data text
  2771. (org-element-map tree type
  2772. (lambda (el) (or (org-export-get-loc el info) 'no-loc)))))
  2773. (ert-deftest test-org-export/get-loc ()
  2774. "Test `org-export-get-loc' specifications."
  2775. (should
  2776. ;; "-n" resets line number.
  2777. (equal '(0)
  2778. (test-org-gen-loc-list "#+BEGIN_EXAMPLE -n\n Text\n#+END_EXAMPLE"
  2779. 'example-block)))
  2780. ;; The first "+n" has 0 lines before it
  2781. (should
  2782. (equal '(0)
  2783. (test-org-gen-loc-list "#+BEGIN_EXAMPLE +n\n Text\n#+END_EXAMPLE"
  2784. 'example-block)))
  2785. ;; "-n 10" resets line number but has "9 lines" before it.
  2786. (should
  2787. (equal '(9)
  2788. (test-org-gen-loc-list "#+BEGIN_EXAMPLE -n 10\n Text\n#+END_EXAMPLE"
  2789. 'example-block)))
  2790. ;; -n10 with two lines then +n 15
  2791. (should
  2792. (equal '(9 25)
  2793. (test-org-gen-loc-list "
  2794. #+BEGIN_EXAMPLE -n 10
  2795. Text_10
  2796. Second line(11)
  2797. #+END_EXAMPLE
  2798. #+BEGIN_EXAMPLE +n 15
  2799. Text line (11 + 15)
  2800. #+END_EXAMPLE"
  2801. 'example-block)))
  2802. (should
  2803. (equal '(9 19 0)
  2804. (test-org-gen-loc-list "
  2805. #+BEGIN_EXAMPLE -n 10
  2806. Text
  2807. #+END_EXAMPLE
  2808. #+BEGIN_EXAMPLE +n 10
  2809. Text
  2810. #+END_EXAMPLE
  2811. #+BEGIN_EXAMPLE -n
  2812. Text
  2813. #+END_EXAMPLE"
  2814. 'example-block)))
  2815. ;; an Example Block without -n does not add to the line count.
  2816. (should
  2817. (equal '(9 no-loc 19)
  2818. (test-org-gen-loc-list "
  2819. #+BEGIN_EXAMPLE -n 10
  2820. Text
  2821. #+END_EXAMPLE
  2822. #+BEGIN_EXAMPLE
  2823. Text
  2824. #+END_EXAMPLE
  2825. #+BEGIN_EXAMPLE +n 10
  2826. Text
  2827. #+END_EXAMPLE"
  2828. 'example-block)))
  2829. ;; "-n" resets line number.
  2830. (should
  2831. (equal
  2832. '(0)
  2833. (test-org-gen-loc-list "#+BEGIN_SRC emacs-lisp -n \n (- 1 1) \n#+END_SRC"
  2834. 'src-block)))
  2835. ;; The first "+n" has 0 lines before it.
  2836. (should
  2837. (equal '(0)
  2838. (test-org-gen-loc-list
  2839. "#+BEGIN_SRC emacs-lisp +n \n (+ 0 (- 1 1))\n#+END_SRC"
  2840. 'src-block)))
  2841. ;; "-n 10" resets line number but has "9 lines" before it.
  2842. (should
  2843. (equal '(9)
  2844. (test-org-gen-loc-list
  2845. "#+BEGIN_SRC emacs-lisp -n 10\n (- 10 1)\n#+END_SRC"
  2846. 'src-block)))
  2847. (should
  2848. (equal '(9 25)
  2849. (test-org-gen-loc-list "
  2850. #+BEGIN_SRC emacs-lisp -n 10
  2851. (- 10 1)
  2852. (+ (- 10 1) 1)
  2853. #+END_SRC
  2854. #+BEGIN_SRC emacs-lisp +n 15
  2855. (+ (- 10 1) 2 (- 15 1))
  2856. #+END_SRC"
  2857. 'src-block)))
  2858. (should
  2859. (equal '(9 19 0)
  2860. (test-org-gen-loc-list "
  2861. #+BEGIN_SRC emacs-lisp -n 10
  2862. (- 10 1)
  2863. #+END_SRC
  2864. #+BEGIN_SRC emacs-lisp +n 10
  2865. (+ (- 10 1) 1 (- 10 1))
  2866. #+END_SRC
  2867. #+BEGIN_SRC emacs-lisp -n
  2868. (- 1 1)
  2869. #+END_SRC"
  2870. 'src-block)))
  2871. ;; A SRC Block without -n does not add to the line count.
  2872. (should
  2873. (equal '(9 no-loc 19)
  2874. (test-org-gen-loc-list
  2875. "#+BEGIN_SRC emacs-lisp -n 10
  2876. (+ (-10 1) 1)
  2877. #+END_SRC
  2878. #+BEGIN_SRC emacs-lisp
  2879. (+ 2 2)
  2880. #+END_SRC
  2881. #+BEGIN_SRC emacs-lisp +n 10
  2882. (+ (- 10 1) 1 (- 10 1))
  2883. #+END_SRC"
  2884. 'src-block))))
  2885. (ert-deftest test-org-export/resolve-coderef ()
  2886. "Test `org-export-resolve-coderef' specifications."
  2887. (let ((org-coderef-label-format "(ref:%s)"))
  2888. ;; A link to a "-n -k -r" block returns line number.
  2889. (should
  2890. (= 1
  2891. (org-test-with-parsed-data
  2892. "#+BEGIN_EXAMPLE -n -k -r\nText (ref:coderef)\n#+END_EXAMPLE"
  2893. (org-export-resolve-coderef "coderef" info))))
  2894. (should
  2895. (= 10
  2896. (org-test-with-parsed-data
  2897. "#+BEGIN_EXAMPLE -n 10 -k -r\nText (ref:coderef)\n#+END_EXAMPLE"
  2898. (org-export-resolve-coderef "coderef" info))))
  2899. (should
  2900. (= 135
  2901. (org-test-with-parsed-data
  2902. "#+BEGIN_EXAMPLE -n 10 -k -r\nText \n#+END_EXAMPLE\n
  2903. #+BEGIN_EXAMPLE +n 125 -k -r\nText (ref:coderef)\n#+END_EXAMPLE"
  2904. (org-export-resolve-coderef "coderef" info))))
  2905. (should
  2906. (= 1
  2907. (org-test-with-parsed-data
  2908. "#+BEGIN_SRC emacs-lisp -n -k -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  2909. (org-export-resolve-coderef "coderef" info))))
  2910. (should
  2911. (= 10
  2912. (org-test-with-parsed-data
  2913. "#+BEGIN_SRC emacs-lisp -n 10 -k -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  2914. (org-export-resolve-coderef "coderef" info))))
  2915. (should
  2916. (= 135
  2917. (org-test-with-parsed-data
  2918. "#+BEGIN_SRC emacs-lisp -n 10 -k -r\n(+ 1 1) \n#+END_SRC\n
  2919. #+BEGIN_SRC emacs-lisp +n 125 -k -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  2920. (org-export-resolve-coderef "coderef" info))))
  2921. ;; A link to a "-n -r" block returns line number.
  2922. (should
  2923. (= 1
  2924. (org-test-with-parsed-data
  2925. "#+BEGIN_EXAMPLE -n -r\nText (ref:coderef)\n#+END_EXAMPLE"
  2926. (org-export-resolve-coderef "coderef" info))))
  2927. (should
  2928. (= 10
  2929. (org-test-with-parsed-data
  2930. "#+BEGIN_EXAMPLE -n 10 -r\nText (ref:coderef)\n#+END_EXAMPLE"
  2931. (org-export-resolve-coderef "coderef" info))))
  2932. (should
  2933. (= 135
  2934. (org-test-with-parsed-data
  2935. "#+BEGIN_EXAMPLE +n 10 -r\nText \n#+END_EXAMPLE
  2936. #+BEGIN_EXAMPLE +n 125 -r\nText (ref:coderef)\n#+END_EXAMPLE"
  2937. (org-export-resolve-coderef "coderef" info))))
  2938. (should
  2939. (= 1
  2940. (org-test-with-parsed-data
  2941. "#+BEGIN_SRC emacs-lisp -n -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  2942. (org-export-resolve-coderef "coderef" info))))
  2943. (should
  2944. (= 10
  2945. (org-test-with-parsed-data
  2946. "#+BEGIN_SRC emacs-lisp -n10 -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  2947. (org-export-resolve-coderef "coderef" info))))
  2948. (should
  2949. (= 135
  2950. (org-test-with-parsed-data
  2951. "#+BEGIN_SRC emacs-lisp -n10 -r\n(+ 1 1) \n#+END_SRC
  2952. #+BEGIN_SRC emacs-lisp +n125 -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  2953. (org-export-resolve-coderef "coderef" info))))
  2954. ;; A link to a "-n" block returns coderef.
  2955. (should
  2956. (equal "coderef"
  2957. (org-test-with-parsed-data
  2958. "#+BEGIN_SRC emacs-lisp -n\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  2959. (org-export-resolve-coderef "coderef" info))))
  2960. (should
  2961. (equal "coderef"
  2962. (org-test-with-parsed-data
  2963. "#+BEGIN_EXAMPLE -n\nText (ref:coderef)\n#+END_EXAMPLE"
  2964. (org-export-resolve-coderef "coderef" info))))
  2965. ;; A link to a "-r" block returns line number.
  2966. (should
  2967. (= 1
  2968. (org-test-with-parsed-data
  2969. "#+BEGIN_SRC emacs-lisp -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  2970. (org-export-resolve-coderef "coderef" info))))
  2971. (should
  2972. (= 1
  2973. (org-test-with-parsed-data
  2974. "#+BEGIN_EXAMPLE -r\nText (ref:coderef)\n#+END_EXAMPLE"
  2975. (org-export-resolve-coderef "coderef" info))))
  2976. ;; A link to a block without a switch returns coderef.
  2977. (should
  2978. (equal "coderef"
  2979. (org-test-with-parsed-data
  2980. "#+BEGIN_SRC emacs-lisp\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  2981. (org-export-resolve-coderef "coderef" info))))
  2982. (org-test-with-parsed-data
  2983. "#+BEGIN_EXAMPLE\nText (ref:coderef)\n#+END_EXAMPLE"
  2984. (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
  2985. ;; Correctly handle continued line numbers. A "+n" switch should
  2986. ;; resume numbering from previous block with numbered lines,
  2987. ;; ignoring blocks not numbering lines in the process. A "-n"
  2988. ;; switch resets count.
  2989. (should
  2990. (equal '(2 1)
  2991. (org-test-with-parsed-data "
  2992. #+BEGIN_EXAMPLE -n
  2993. Text.
  2994. #+END_EXAMPLE
  2995. #+BEGIN_SRC emacs-lisp
  2996. \(- 1 1)
  2997. #+END_SRC
  2998. #+BEGIN_SRC emacs-lisp +n -r
  2999. \(+ 1 1) (ref:addition)
  3000. #+END_SRC
  3001. #+BEGIN_EXAMPLE -n -r
  3002. Another text. (ref:text)
  3003. #+END_EXAMPLE"
  3004. (list (org-export-resolve-coderef "addition" info)
  3005. (org-export-resolve-coderef "text" info)))))
  3006. ;; Recognize coderef with user-specified syntax.
  3007. (should
  3008. (equal
  3009. "text"
  3010. (org-test-with-parsed-data
  3011. "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\nText. [ref:text]\n#+END_EXAMPLE"
  3012. (org-export-resolve-coderef "text" info))))
  3013. ;; Unresolved coderefs raise a `org-link-broken' signal.
  3014. (should
  3015. (condition-case nil
  3016. (org-test-with-parsed-data "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  3017. (org-export-resolve-coderef "unknown" info))
  3018. (org-link-broken t)))))
  3019. (ert-deftest test-org-export/resolve-fuzzy-link ()
  3020. "Test `org-export-resolve-fuzzy-link' specifications."
  3021. ;; Match target objects.
  3022. (should
  3023. (org-test-with-parsed-data "<<target>> [[target]]"
  3024. (org-export-resolve-fuzzy-link
  3025. (org-element-map tree 'link 'identity info t) info)))
  3026. ;; Match named elements.
  3027. (should
  3028. (org-test-with-parsed-data "#+NAME: target\nParagraph\n\n[[target]]"
  3029. (org-export-resolve-fuzzy-link
  3030. (org-element-map tree 'link 'identity info t) info)))
  3031. ;; Match exact headline's name.
  3032. (should
  3033. (org-test-with-parsed-data "* My headline\n[[My headline]]"
  3034. (org-export-resolve-fuzzy-link
  3035. (org-element-map tree 'link 'identity info t) info)))
  3036. ;; Targets objects have priority over headline titles.
  3037. (should
  3038. (eq 'target
  3039. (org-test-with-parsed-data "* target\n<<target>>[[target]]"
  3040. (org-element-type
  3041. (org-export-resolve-fuzzy-link
  3042. (org-element-map tree 'link 'identity info t) info)))))
  3043. ;; Named elements have priority over headline titles.
  3044. (should
  3045. (eq 'paragraph
  3046. (org-test-with-parsed-data
  3047. "* target\n#+NAME: target\nParagraph\n\n[[target]]"
  3048. (org-element-type
  3049. (org-export-resolve-fuzzy-link
  3050. (org-element-map tree 'link 'identity info t) info)))))
  3051. ;; If link's path starts with a "*", only match headline titles,
  3052. ;; though.
  3053. (should
  3054. (eq 'headline
  3055. (org-test-with-parsed-data
  3056. "* target\n#+NAME: target\n<<target>>\n\n[[*target]]"
  3057. (org-element-type
  3058. (org-export-resolve-fuzzy-link
  3059. (org-element-map tree 'link 'identity info t) info)))))
  3060. ;; Raise a `org-link-broken' signal if no match.
  3061. (should
  3062. (org-test-with-parsed-data "[[target]]"
  3063. (condition-case nil
  3064. (org-export-resolve-fuzzy-link
  3065. (org-element-map tree 'link #'identity info t) info)
  3066. (org-link-broken t))))
  3067. ;; Match fuzzy link even when before first headline.
  3068. (should
  3069. (eq 'headline
  3070. (org-test-with-parsed-data "[[hl]]\n* hl"
  3071. (org-element-type
  3072. (org-export-resolve-fuzzy-link
  3073. (org-element-map tree 'link 'identity info t) info)))))
  3074. ;; Handle url-encoded fuzzy links.
  3075. (should
  3076. (org-test-with-parsed-data "* A B\n[[A%20B]]"
  3077. (org-export-resolve-fuzzy-link
  3078. (org-element-map tree 'link #'identity info t) info))))
  3079. (ert-deftest test-org-export/resolve-id-link ()
  3080. "Test `org-export-resolve-id-link' specifications."
  3081. ;; Regular test for custom-id link.
  3082. (should
  3083. (equal '("Headline1")
  3084. (org-test-with-parsed-data "* Headline1
  3085. :PROPERTIES:
  3086. :CUSTOM_ID: test
  3087. :END:
  3088. * Headline 2
  3089. \[[#test]]"
  3090. (org-element-property
  3091. :title
  3092. (org-export-resolve-id-link
  3093. (org-element-map tree 'link 'identity info t) info)))))
  3094. ;; Raise a `org-link-broken' signal on failing searches.
  3095. (should
  3096. (org-test-with-parsed-data "* Headline1
  3097. :PROPERTIES:
  3098. :CUSTOM_ID: test
  3099. :END:
  3100. * Headline 2
  3101. \[[#no-match]]"
  3102. (condition-case nil
  3103. (org-export-resolve-id-link
  3104. (org-element-map tree 'link #'identity info t) info)
  3105. (org-link-broken t))))
  3106. ;; Test for internal id target.
  3107. (should
  3108. (equal '("Headline1")
  3109. (org-test-with-parsed-data "* Headline1
  3110. :PROPERTIES:
  3111. :ID: aaaa
  3112. :END:
  3113. * Headline 2
  3114. \[[id:aaaa]]"
  3115. (org-element-property
  3116. :title
  3117. (org-export-resolve-id-link
  3118. (org-element-map tree 'link 'identity info t) info)))))
  3119. ;; Test for external id target.
  3120. (should
  3121. (equal
  3122. "external-file"
  3123. (org-test-with-parsed-data "[[id:aaaa]]"
  3124. (org-export-resolve-id-link
  3125. (org-element-map tree 'link 'identity info t)
  3126. (org-combine-plists info '(:id-alist (("aaaa" . "external-file")))))))))
  3127. (ert-deftest test-org-export/resolve-radio-link ()
  3128. "Test `org-export-resolve-radio-link' specifications."
  3129. ;; Standard test.
  3130. (should
  3131. (org-test-with-temp-text "<<<radio>>> radio"
  3132. (org-update-radio-target-regexp)
  3133. (let* ((tree (org-element-parse-buffer))
  3134. (info `(:parse-tree ,tree)))
  3135. (org-export-resolve-radio-link
  3136. (org-element-map tree 'link 'identity info t)
  3137. info))))
  3138. ;; Radio targets are case-insensitive.
  3139. (should
  3140. (org-test-with-temp-text "<<<RADIO>>> radio"
  3141. (org-update-radio-target-regexp)
  3142. (let* ((tree (org-element-parse-buffer))
  3143. (info `(:parse-tree ,tree)))
  3144. (org-export-resolve-radio-link
  3145. (org-element-map tree 'link 'identity info t)
  3146. info))))
  3147. ;; Radio target with objects.
  3148. (should
  3149. (org-test-with-temp-text "<<<radio \\alpha>>> radio \\alpha"
  3150. (org-update-radio-target-regexp)
  3151. (let* ((tree (org-element-parse-buffer))
  3152. (info `(:parse-tree ,tree)))
  3153. (org-export-resolve-radio-link
  3154. (org-element-map tree 'link 'identity info t)
  3155. info))))
  3156. ;; Radio target with objects at its beginning.
  3157. (should
  3158. (org-test-with-temp-text "<<<\\alpha radio>>> \\alpha radio"
  3159. (org-update-radio-target-regexp)
  3160. (let* ((tree (org-element-parse-buffer))
  3161. (info `(:parse-tree ,tree)))
  3162. (org-export-resolve-radio-link
  3163. (org-element-map tree 'link 'identity info t)
  3164. info))))
  3165. ;; Radio link next to an apostrophe.
  3166. (should
  3167. (org-test-with-temp-text "<<<radio>>> radio's"
  3168. (org-update-radio-target-regexp)
  3169. (let* ((tree (org-element-parse-buffer))
  3170. (info `(:parse-tree ,tree)))
  3171. (org-export-resolve-radio-link
  3172. (org-element-map tree 'link 'identity info t)
  3173. info))))
  3174. ;; Multiple radio targets.
  3175. (should
  3176. (equal '("radio1" "radio2")
  3177. (org-test-with-temp-text "<<<radio1>>> <<<radio2>>> radio1 radio2"
  3178. (org-update-radio-target-regexp)
  3179. (let* ((tree (org-element-parse-buffer))
  3180. (info `(:parse-tree ,tree)))
  3181. (org-element-map tree 'link
  3182. (lambda (link)
  3183. (org-element-property
  3184. :value (org-export-resolve-radio-link link info)))
  3185. info)))))
  3186. ;; Radio target is whitespace insensitive.
  3187. (should
  3188. (org-test-with-temp-text "<<<a radio>>> a\n radio"
  3189. (org-update-radio-target-regexp)
  3190. (let* ((tree (org-element-parse-buffer))
  3191. (info `(:parse-tree ,tree)))
  3192. (org-element-map tree 'link
  3193. (lambda (link) (org-export-resolve-radio-link link info)) info t)))))
  3194. (ert-deftest test-org-export/file-uri ()
  3195. "Test `org-export-file-uri' specifications."
  3196. ;; Preserve relative filenames.
  3197. (should (equal "relative.org" (org-export-file-uri "relative.org")))
  3198. ;; Local files start with "file://"
  3199. (should (equal (concat (if (memq system-type '(windows-nt cygwin)) "file:///" "file://") (expand-file-name "/local.org"))
  3200. (org-export-file-uri "/local.org")))
  3201. ;; Remote files start with "file://"
  3202. (should (equal "file://myself@some.where:papers/last.pdf"
  3203. (org-export-file-uri "/myself@some.where:papers/last.pdf")))
  3204. (should (equal "file://localhost/etc/fstab"
  3205. (org-export-file-uri "//localhost/etc/fstab")))
  3206. ;; Expand filename starting with "~".
  3207. (should (equal (org-export-file-uri "~/file.org")
  3208. (concat (if (memq system-type '(windows-nt cygwin)) "file:///" "file://") (expand-file-name "~/file.org")))))
  3209. (ert-deftest test-org-export/get-reference ()
  3210. "Test `org-export-get-reference' specifications."
  3211. (should
  3212. (org-test-with-parsed-data "* Headline"
  3213. (org-export-get-reference (org-element-map tree 'headline #'identity nil t)
  3214. info)))
  3215. ;; For a given element always return the same reference.
  3216. (should
  3217. (org-test-with-parsed-data "* Headline"
  3218. (let ((headline (org-element-map tree 'headline #'identity nil t)))
  3219. (equal (org-export-get-reference headline info)
  3220. (org-export-get-reference headline info)))))
  3221. ;; References get through local export back-ends.
  3222. (should
  3223. (org-test-with-parsed-data "* Headline"
  3224. (let ((headline (org-element-map tree 'headline #'identity nil t))
  3225. (backend
  3226. (org-export-create-backend
  3227. :transcoders
  3228. '((headline . (lambda (h _c i) (org-export-get-reference h i)))))))
  3229. (equal (org-trim (org-export-data-with-backend headline backend info))
  3230. (org-export-get-reference headline info)))))
  3231. (should
  3232. (org-test-with-parsed-data "* Headline"
  3233. (let ((headline (org-element-map tree 'headline #'identity nil t))
  3234. (backend
  3235. (org-export-create-backend
  3236. :transcoders
  3237. '((headline . (lambda (h _c i) (org-export-get-reference h i)))))))
  3238. (equal (org-export-with-backend backend headline nil info)
  3239. (org-export-get-reference headline info)))))
  3240. ;; Use search cells defined in `:crossrefs'. However, handle
  3241. ;; duplicate search cells.
  3242. (should
  3243. (equal "org0000001"
  3244. (org-test-with-parsed-data "* Headline"
  3245. (let* ((headline (org-element-map tree 'headline #'identity nil t))
  3246. (search-cell (car (org-export-search-cells headline))))
  3247. (setq info
  3248. (plist-put info :crossrefs (list (cons search-cell 1))))
  3249. (org-export-get-reference headline info)))))
  3250. (should-not
  3251. (equal '("org0000001" "org0000001")
  3252. (org-test-with-parsed-data "* H\n** H"
  3253. (org-element-map tree 'headline
  3254. (lambda (h)
  3255. (let* ((search-cell (car (org-export-search-cells h)))
  3256. (info (plist-put info :crossrefs
  3257. (list (cons search-cell 1)))))
  3258. (org-export-get-reference h info))))))))
  3259. ;;; Pseudo objects and pseudo elements
  3260. (ert-deftest test-org-export/pseudo-elements ()
  3261. "Test exporting pseudo-elements."
  3262. ;; Handle blank lines after pseudo-elements. In particular, do not
  3263. ;; replace them with white spaces.
  3264. (should
  3265. (equal "contents\n\nparagraph\n"
  3266. (let ((backend (org-export-create-backend
  3267. :transcoders
  3268. '((pseudo-element . (lambda (_p c _i) c))
  3269. (paragraph . (lambda (_p c _i) c))
  3270. (plain-text . (lambda (c _i) c)))))
  3271. (element '(pseudo-element (:post-blank 1) "contents"))
  3272. (paragraph '(paragraph nil "paragraph"))
  3273. (data '(org-data nil)))
  3274. (org-element-adopt-elements data element paragraph)
  3275. (org-export-data-with-backend data backend nil)))))
  3276. (ert-deftest test-org-export/pseudo-objects ()
  3277. "Test exporting pseudo-objects."
  3278. ;; Handle blank spaces after pseudo-objects. In particular, do not
  3279. ;; replace them with newlines.
  3280. (should
  3281. (equal "begin x end\n"
  3282. (let ((backend (org-export-create-backend
  3283. :transcoders
  3284. '((pseudo-object . (lambda (_p c _i) c))
  3285. (paragraph . (lambda (_p c _i) c))
  3286. (plain-text . (lambda (c _i) c)))))
  3287. (object '(pseudo-object (:post-blank 1) "x"))
  3288. (paragraph '(paragraph nil)))
  3289. (org-element-adopt-elements paragraph "begin " object "end")
  3290. (org-export-data-with-backend paragraph backend nil)))))
  3291. ;;; Src-block and example-block
  3292. (ert-deftest test-org-export/unravel-code ()
  3293. "Test `org-export-unravel-code' function."
  3294. ;; Code without reference.
  3295. (should
  3296. (equal '("(+ 1 1)\n")
  3297. (org-test-with-temp-text "#+BEGIN_EXAMPLE\n(+ 1 1)\n#+END_EXAMPLE"
  3298. (org-export-unravel-code (org-element-at-point)))))
  3299. ;; Code with reference.
  3300. (should
  3301. (equal '("(+ 1 1)\n" (1 . "test"))
  3302. (org-test-with-temp-text
  3303. "#+BEGIN_EXAMPLE\n(+ 1 1) (ref:test)\n#+END_EXAMPLE"
  3304. (let ((org-coderef-label-format "(ref:%s)"))
  3305. (org-export-unravel-code (org-element-at-point))))))
  3306. ;; Code with user-defined reference.
  3307. (should
  3308. (equal
  3309. '("(+ 1 1)\n" (1 . "test"))
  3310. (org-test-with-temp-text
  3311. "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\n(+ 1 1) [ref:test]\n#+END_EXAMPLE"
  3312. (let ((org-coderef-label-format "(ref:%s)"))
  3313. (org-export-unravel-code (org-element-at-point))))))
  3314. ;; Code references keys are relative to the current block.
  3315. (should
  3316. (equal '("(+ 2 2)\n(+ 3 3)\n" (2 . "one"))
  3317. (org-test-with-temp-text "
  3318. #+BEGIN_EXAMPLE -n
  3319. \(+ 1 1)
  3320. #+END_EXAMPLE
  3321. #+BEGIN_EXAMPLE +n
  3322. \(+ 2 2)
  3323. \(+ 3 3) (ref:one)
  3324. #+END_EXAMPLE"
  3325. (goto-line 5)
  3326. (let ((org-coderef-label-format "(ref:%s)"))
  3327. (org-export-unravel-code (org-element-at-point)))))))
  3328. (ert-deftest test-org-export/format-code-default ()
  3329. "Test `org-export-format-code-default' specifications."
  3330. ;; Return the empty string when code is empty.
  3331. (should
  3332. (equal ""
  3333. (org-test-with-parsed-data "#+BEGIN_SRC emacs-lisp\n\n\n#+END_SRC"
  3334. (org-export-format-code-default
  3335. (org-element-map tree 'src-block 'identity info t) info))))
  3336. ;; Number lines, two whitespace characters before the actual loc.
  3337. (should
  3338. (equal "1 a\n2 b\n"
  3339. (org-test-with-parsed-data
  3340. "#+BEGIN_SRC emacs-lisp +n\na\nb\n#+END_SRC"
  3341. (org-export-format-code-default
  3342. (org-element-map tree 'src-block 'identity info t) info))))
  3343. ;; Put references 6 whitespace characters after the widest line,
  3344. ;; wrapped within parenthesis.
  3345. (should
  3346. (equal "123 (a)\n1 (b)\n"
  3347. (let ((org-coderef-label-format "(ref:%s)"))
  3348. (org-test-with-parsed-data
  3349. "#+BEGIN_SRC emacs-lisp\n123 (ref:a)\n1 (ref:b)\n#+END_SRC"
  3350. (org-export-format-code-default
  3351. (org-element-map tree 'src-block 'identity info t) info))))))
  3352. ;;; Smart Quotes
  3353. (ert-deftest test-org-export/activate-smart-quotes ()
  3354. "Test `org-export-activate-smart-quotes' specifications."
  3355. ;; Double quotes: standard test.
  3356. (should
  3357. (equal
  3358. '("some &ldquo;quoted&rdquo; text")
  3359. (let ((org-export-default-language "en"))
  3360. (org-test-with-parsed-data "some \"quoted\" text"
  3361. (org-element-map tree 'plain-text
  3362. (lambda (s) (org-export-activate-smart-quotes s :html info))
  3363. info)))))
  3364. ;; Opening quotes: at the beginning of a paragraph.
  3365. (should
  3366. (equal
  3367. '("&ldquo;begin")
  3368. (let ((org-export-default-language "en"))
  3369. (org-test-with-parsed-data "\"begin"
  3370. (org-element-map tree 'plain-text
  3371. (lambda (s) (org-export-activate-smart-quotes s :html info))
  3372. info)))))
  3373. ;; Opening quotes: after an object.
  3374. (should
  3375. (equal
  3376. '("&ldquo;quoted&rdquo; text")
  3377. (let ((org-export-default-language "en"))
  3378. (org-test-with-parsed-data "=verb= \"quoted\" text"
  3379. (org-element-map tree 'plain-text
  3380. (lambda (s) (org-export-activate-smart-quotes s :html info))
  3381. info)))))
  3382. ;; Closing quotes: at the end of a paragraph.
  3383. (should
  3384. (equal
  3385. '("Quoted &ldquo;text&rdquo;")
  3386. (let ((org-export-default-language "en"))
  3387. (org-test-with-parsed-data "Quoted \"text\""
  3388. (org-element-map tree 'plain-text
  3389. (lambda (s) (org-export-activate-smart-quotes s :html info))
  3390. info)))))
  3391. ;; Inner quotes: standard test.
  3392. (should
  3393. (equal '("« outer « inner » outer »")
  3394. (let ((org-export-default-language "fr"))
  3395. (org-test-with-parsed-data "\"outer 'inner' outer\""
  3396. (org-element-map tree 'plain-text
  3397. (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
  3398. info)))))
  3399. ;; Inner quotes: close to special symbols.
  3400. (should
  3401. (equal '("« outer (« inner ») outer »")
  3402. (let ((org-export-default-language "fr"))
  3403. (org-test-with-parsed-data "\"outer ('inner') outer\""
  3404. (org-element-map tree 'plain-text
  3405. (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
  3406. info)))))
  3407. (should
  3408. (equal '("« « inner » »")
  3409. (let ((org-export-default-language "fr"))
  3410. (org-test-with-parsed-data "\"'inner'\""
  3411. (org-element-map tree 'plain-text
  3412. (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
  3413. info)))))
  3414. ;; Apostrophe: standard test.
  3415. (should
  3416. (equal '("It « shouldn’t » fail")
  3417. (let ((org-export-default-language "fr"))
  3418. (org-test-with-parsed-data "It \"shouldn't\" fail"
  3419. (org-element-map tree 'plain-text
  3420. (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
  3421. info)))))
  3422. (should
  3423. (equal '("It shouldn’t fail")
  3424. (let ((org-export-default-language "fr"))
  3425. (org-test-with-parsed-data "It shouldn't fail"
  3426. (org-element-map tree 'plain-text
  3427. (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
  3428. info)))))
  3429. ;; Apostrophe: before an object.
  3430. (should
  3431. (equal
  3432. '("« a’" " »")
  3433. (let ((org-export-default-language "fr"))
  3434. (org-test-with-parsed-data "\"a'=b=\""
  3435. (org-element-map tree 'plain-text
  3436. (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
  3437. info)))))
  3438. ;; Apostrophe: after an object.
  3439. (should
  3440. (equal '("« " "’s »")
  3441. (let ((org-export-default-language "fr"))
  3442. (org-test-with-parsed-data "\"=code='s\""
  3443. (org-element-map tree 'plain-text
  3444. (lambda (s) (org-export-activate-smart-quotes s :utf-8 info))
  3445. info)))))
  3446. ;; Special case: isolated quotes.
  3447. (should
  3448. (equal '("&ldquo;" "&rdquo;")
  3449. (let ((org-export-default-language "en"))
  3450. (org-test-with-parsed-data "\"$x$\""
  3451. (org-element-map tree 'plain-text
  3452. (lambda (s) (org-export-activate-smart-quotes s :html info))
  3453. info)))))
  3454. ;; Smart quotes in secondary strings.
  3455. (should
  3456. (equal '("&ldquo;" "&rdquo;")
  3457. (let ((org-export-default-language "en"))
  3458. (org-test-with-parsed-data "* \"$x$\""
  3459. (org-element-map tree 'plain-text
  3460. (lambda (s) (org-export-activate-smart-quotes s :html info))
  3461. info)))))
  3462. ;; Smart quotes in document keywords.
  3463. (should
  3464. (equal '("&ldquo;" "&rdquo;")
  3465. (let ((org-export-default-language "en"))
  3466. (org-test-with-parsed-data "#+TITLE: \"$x$\""
  3467. (org-element-map (plist-get info :title) 'plain-text
  3468. (lambda (s) (org-export-activate-smart-quotes s :html info))
  3469. info)))))
  3470. ;; Smart quotes in parsed affiliated keywords.
  3471. (should
  3472. (equal '("&ldquo;" "&rdquo;" "Paragraph")
  3473. (let ((org-export-default-language "en"))
  3474. (org-test-with-parsed-data "#+CAPTION: \"$x$\"\nParagraph"
  3475. (org-element-map tree 'plain-text
  3476. (lambda (s) (org-export-activate-smart-quotes s :html info))
  3477. info nil nil t)))))
  3478. ;; Smart quotes within objects.
  3479. (should
  3480. (equal '("&ldquo;foo&rdquo;")
  3481. (let ((org-export-default-language "en"))
  3482. (org-test-with-parsed-data "| \"foo\" |"
  3483. (org-element-map tree 'plain-text
  3484. (lambda (s) (org-export-activate-smart-quotes s :html info))
  3485. info nil nil t)))))
  3486. ;; FIXME: Test failing non-interactively.
  3487. ;;
  3488. ;; (should
  3489. ;; (equal '("&ldquo;foo&rdquo;")
  3490. ;; (let ((org-export-default-language "en"))
  3491. ;; (org-test-with-parsed-data "*\"foo\"*"
  3492. ;; (org-element-map tree 'plain-text
  3493. ;; (lambda (s) (org-export-activate-smart-quotes s :html info))
  3494. ;; info nil nil t)))))
  3495. )
  3496. ;;; Tables
  3497. (ert-deftest test-org-export/special-column ()
  3498. "Test if the table's special column is properly recognized."
  3499. ;; 1. First column is special if it contains only a special marking
  3500. ;; characters or empty cells.
  3501. (org-test-with-temp-text "
  3502. | ! | 1 |
  3503. | | 2 |"
  3504. (should
  3505. (org-export-table-has-special-column-p
  3506. (org-element-map
  3507. (org-element-parse-buffer) 'table 'identity nil 'first-match))))
  3508. ;; 2. If the column contains anything else, it isn't special.
  3509. (org-test-with-temp-text "
  3510. | ! | 1 |
  3511. | b | 2 |"
  3512. (should-not
  3513. (org-export-table-has-special-column-p
  3514. (org-element-map
  3515. (org-element-parse-buffer) 'table 'identity nil 'first-match))))
  3516. ;; 3. Special marking characters are "#", "^", "*", "_", "/", "$"
  3517. ;; and "!".
  3518. (org-test-with-temp-text "
  3519. | # | 1 |
  3520. | ^ | 2 |
  3521. | * | 3 |
  3522. | _ | 4 |
  3523. | / | 5 |
  3524. | $ | 6 |
  3525. | ! | 7 |"
  3526. (should
  3527. (org-export-table-has-special-column-p
  3528. (org-element-map
  3529. (org-element-parse-buffer) 'table 'identity nil 'first-match))))
  3530. ;; 4. A first column with only empty cells isn't considered as
  3531. ;; special.
  3532. (org-test-with-temp-text "
  3533. | | 1 |
  3534. | | 2 |"
  3535. (should-not
  3536. (org-export-table-has-special-column-p
  3537. (org-element-map
  3538. (org-element-parse-buffer) 'table 'identity nil 'first-match)))))
  3539. (ert-deftest test-org-export/table-row-is-special-p ()
  3540. "Test `org-export-table-row-is-special-p' specifications."
  3541. ;; 1. A row is special if it has a special marking character in the
  3542. ;; special column.
  3543. (org-test-with-parsed-data "| ! | 1 |"
  3544. (should
  3545. (org-export-table-row-is-special-p
  3546. (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  3547. ;; 2. A row is special when its first field is "/"
  3548. (org-test-with-parsed-data "
  3549. | / | 1 |
  3550. | a | b |"
  3551. (should
  3552. (org-export-table-row-is-special-p
  3553. (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  3554. ;; 3. A row only containing alignment cookies is also considered as
  3555. ;; special.
  3556. (org-test-with-parsed-data "| <5> | | <l> | <l22> |"
  3557. (should
  3558. (org-export-table-row-is-special-p
  3559. (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  3560. ;; 4. Everything else isn't considered as special.
  3561. (org-test-with-parsed-data "| \alpha | | c |"
  3562. (should-not
  3563. (org-export-table-row-is-special-p
  3564. (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  3565. ;; 5. Table's rules are never considered as special rows.
  3566. (org-test-with-parsed-data "|---+---|"
  3567. (should-not
  3568. (org-export-table-row-is-special-p
  3569. (org-element-map tree 'table-row 'identity nil 'first-match) info))))
  3570. (ert-deftest test-org-export/has-header-p ()
  3571. "Test `org-export-table-has-header-p' specifications."
  3572. ;; With an header.
  3573. (should
  3574. (org-test-with-parsed-data "
  3575. | a | b |
  3576. |---+---|
  3577. | c | d |"
  3578. (org-export-table-has-header-p
  3579. (org-element-map tree 'table 'identity info 'first-match)
  3580. info)))
  3581. ;; Without an header.
  3582. (should-not
  3583. (org-test-with-parsed-data "
  3584. | a | b |
  3585. | c | d |"
  3586. (org-export-table-has-header-p
  3587. (org-element-map tree 'table 'identity info 'first-match)
  3588. info)))
  3589. ;; Don't get fooled with starting and ending rules.
  3590. (should-not
  3591. (org-test-with-parsed-data "
  3592. |---+---|
  3593. | a | b |
  3594. | c | d |
  3595. |---+---|"
  3596. (org-export-table-has-header-p
  3597. (org-element-map tree 'table 'identity info 'first-match)
  3598. info))))
  3599. (ert-deftest test-org-export/table-row-group ()
  3600. "Test `org-export-table-row-group' specifications."
  3601. ;; A rule creates a new group.
  3602. (should
  3603. (equal '(1 rule 2)
  3604. (org-test-with-parsed-data "
  3605. | a | b |
  3606. |---+---|
  3607. | 1 | 2 |"
  3608. (org-element-map tree 'table-row
  3609. (lambda (row)
  3610. (if (eq (org-element-property :type row) 'rule) 'rule
  3611. (org-export-table-row-group row info)))))))
  3612. ;; Special rows are ignored in count.
  3613. (should
  3614. (equal
  3615. '(rule 1)
  3616. (org-test-with-parsed-data "
  3617. | / | < | > |
  3618. |---|---+---|
  3619. | | 1 | 2 |"
  3620. (org-element-map tree 'table-row
  3621. (lambda (row)
  3622. (if (eq (org-element-property :type row) 'rule) 'rule
  3623. (org-export-table-row-group row info)))
  3624. info))))
  3625. ;; Double rules also are ignored in count.
  3626. (should
  3627. (equal '(1 rule rule 2)
  3628. (org-test-with-parsed-data "
  3629. | a | b |
  3630. |---+---|
  3631. |---+---|
  3632. | 1 | 2 |"
  3633. (org-element-map tree 'table-row
  3634. (lambda (row)
  3635. (if (eq (org-element-property :type row) 'rule) 'rule
  3636. (org-export-table-row-group row info))))))))
  3637. (ert-deftest test-org-export/table-row-number ()
  3638. "Test `org-export-table-row-number' specifications."
  3639. ;; Standard test. Number is 0-indexed.
  3640. (should
  3641. (equal '(0 1)
  3642. (org-test-with-parsed-data "| a | b | c |\n| d | e | f |"
  3643. (org-element-map tree 'table-row
  3644. (lambda (row) (org-export-table-row-number row info)) info))))
  3645. ;; Number ignores separators.
  3646. (should
  3647. (equal '(0 1)
  3648. (org-test-with-parsed-data "
  3649. | a | b | c |
  3650. |---+---+---|
  3651. | d | e | f |"
  3652. (org-element-map tree 'table-row
  3653. (lambda (row) (org-export-table-row-number row info)) info))))
  3654. ;; Number ignores special rows.
  3655. (should
  3656. (equal '(0 1)
  3657. (org-test-with-parsed-data "
  3658. | / | < | > |
  3659. | | b | c |
  3660. |---+-----+-----|
  3661. | | <c> | <c> |
  3662. | | e | f |"
  3663. (org-element-map tree 'table-row
  3664. (lambda (row) (org-export-table-row-number row info)) info)))))
  3665. (ert-deftest test-org-export/table-cell-width ()
  3666. "Test `org-export-table-cell-width' specifications."
  3667. ;; 1. Width is primarily determined by width cookies. If no cookie
  3668. ;; is found, cell's width is nil.
  3669. (org-test-with-parsed-data "
  3670. | / | <l> | <6> | <l7> |
  3671. | | a | b | c |"
  3672. (should
  3673. (equal
  3674. '(nil 6 7)
  3675. (mapcar (lambda (cell) (org-export-table-cell-width cell info))
  3676. (org-element-map tree 'table-cell 'identity info)))))
  3677. ;; 2. The last width cookie has precedence.
  3678. (org-test-with-parsed-data "
  3679. | <6> |
  3680. | <7> |
  3681. | a |"
  3682. (should
  3683. (equal
  3684. '(7)
  3685. (mapcar (lambda (cell) (org-export-table-cell-width cell info))
  3686. (org-element-map tree 'table-cell 'identity info)))))
  3687. ;; 3. Valid width cookies must have a specific row.
  3688. (org-test-with-parsed-data "| <6> | cell |"
  3689. (should
  3690. (equal
  3691. '(nil nil)
  3692. (mapcar (lambda (cell) (org-export-table-cell-width cell info))
  3693. (org-element-map tree 'table-cell 'identity))))))
  3694. (ert-deftest test-org-export/table-cell-alignment ()
  3695. "Test `org-export-table-cell-alignment' specifications."
  3696. ;; 1. Alignment is primarily determined by alignment cookies.
  3697. (should
  3698. (equal '(left center right)
  3699. (let ((org-table-number-fraction 0.5)
  3700. (org-table-number-regexp "^[0-9]+$"))
  3701. (org-test-with-parsed-data "| <l> | <c> | <r> |"
  3702. (mapcar (lambda (cell)
  3703. (org-export-table-cell-alignment cell info))
  3704. (org-element-map tree 'table-cell 'identity))))))
  3705. ;; 2. The last alignment cookie has precedence.
  3706. (should
  3707. (equal '(right right right)
  3708. (org-test-with-parsed-data "
  3709. | <l8> |
  3710. | cell |
  3711. | <r9> |"
  3712. (mapcar (lambda (cell) (org-export-table-cell-alignment cell info))
  3713. (org-element-map tree 'table-cell 'identity)))))
  3714. ;; 3. If there's no cookie, cell's contents determine alignment.
  3715. ;; A column mostly made of cells containing numbers will align
  3716. ;; its cells to the right.
  3717. (should
  3718. (equal '(right right right)
  3719. (let ((org-table-number-fraction 0.5)
  3720. (org-table-number-regexp "^[0-9]+$"))
  3721. (org-test-with-parsed-data "
  3722. | 123 |
  3723. | some text |
  3724. | 12345 |"
  3725. (mapcar (lambda (cell)
  3726. (org-export-table-cell-alignment cell info))
  3727. (org-element-map tree 'table-cell 'identity))))))
  3728. ;; 4. Otherwise, they will be aligned to the left.
  3729. (should
  3730. (equal '(left left left)
  3731. (org-test-with-parsed-data "
  3732. | text |
  3733. | some text |
  3734. | \alpha |"
  3735. (mapcar (lambda (cell)
  3736. (org-export-table-cell-alignment cell info))
  3737. (org-element-map tree 'table-cell 'identity info))))))
  3738. (ert-deftest test-org-export/table-cell-borders ()
  3739. "Test `org-export-table-cell-borders' specifications."
  3740. ;; 1. Recognize various column groups indicators.
  3741. (org-test-with-parsed-data "| / | < | > | <> |"
  3742. (should
  3743. (equal
  3744. '((right bottom top) (left bottom top) (right bottom top)
  3745. (right left bottom top))
  3746. (mapcar (lambda (cell)
  3747. (org-export-table-cell-borders cell info))
  3748. (org-element-map tree 'table-cell 'identity)))))
  3749. ;; 2. Accept shortcuts to define column groups.
  3750. (org-test-with-parsed-data "| / | < | < |"
  3751. (should
  3752. (equal
  3753. '((right bottom top) (right left bottom top) (left bottom top))
  3754. (mapcar (lambda (cell)
  3755. (org-export-table-cell-borders cell info))
  3756. (org-element-map tree 'table-cell 'identity)))))
  3757. ;; 3. A valid column groups row must start with a "/".
  3758. (org-test-with-parsed-data "
  3759. | | < |
  3760. | a | b |"
  3761. (should
  3762. (equal '((top) (top) (bottom) (bottom))
  3763. (mapcar (lambda (cell)
  3764. (org-export-table-cell-borders cell info))
  3765. (org-element-map tree 'table-cell 'identity)))))
  3766. ;; 4. Take table rules into consideration.
  3767. (org-test-with-parsed-data "
  3768. | 1 |
  3769. |---|
  3770. | 2 |"
  3771. (should
  3772. (equal '((below top) (bottom above))
  3773. (mapcar (lambda (cell)
  3774. (org-export-table-cell-borders cell info))
  3775. (org-element-map tree 'table-cell 'identity)))))
  3776. ;; 5. Top and (resp. bottom) rules induce both `top' and `above'
  3777. ;; (resp. `bottom' and `below') borders. Any special row is
  3778. ;; ignored.
  3779. (org-test-with-parsed-data "
  3780. |---+----|
  3781. | / | |
  3782. | | 1 |
  3783. |---+----|"
  3784. (should
  3785. (equal '((bottom below top above))
  3786. (last
  3787. (mapcar (lambda (cell)
  3788. (org-export-table-cell-borders cell info))
  3789. (org-element-map tree 'table-cell 'identity)))))))
  3790. (ert-deftest test-org-export/table-dimensions ()
  3791. "Test `org-export-table-dimensions' specifications."
  3792. ;; 1. Standard test.
  3793. (org-test-with-parsed-data "
  3794. | 1 | 2 | 3 |
  3795. | 4 | 5 | 6 |"
  3796. (should
  3797. (equal '(2 . 3)
  3798. (org-export-table-dimensions
  3799. (org-element-map tree 'table 'identity info 'first-match) info))))
  3800. ;; 2. Ignore horizontal rules and special columns.
  3801. (org-test-with-parsed-data "
  3802. | / | < | > |
  3803. | 1 | 2 | 3 |
  3804. |---+---+---|
  3805. | 4 | 5 | 6 |"
  3806. (should
  3807. (equal '(2 . 3)
  3808. (org-export-table-dimensions
  3809. (org-element-map tree 'table 'identity info 'first-match) info)))))
  3810. (ert-deftest test-org-export/table-cell-address ()
  3811. "Test `org-export-table-cell-address' specifications."
  3812. ;; 1. Standard test: index is 0-based.
  3813. (org-test-with-parsed-data "| a | b |"
  3814. (should
  3815. (equal '((0 . 0) (0 . 1))
  3816. (org-element-map tree 'table-cell
  3817. (lambda (cell) (org-export-table-cell-address cell info))
  3818. info))))
  3819. ;; 2. Special column isn't counted, nor are special rows.
  3820. (org-test-with-parsed-data "
  3821. | / | <> |
  3822. | | c |"
  3823. (should
  3824. (equal '(0 . 0)
  3825. (org-export-table-cell-address
  3826. (car (last (org-element-map tree 'table-cell 'identity info)))
  3827. info))))
  3828. ;; 3. Tables rules do not count either.
  3829. (org-test-with-parsed-data "
  3830. | a |
  3831. |---|
  3832. | b |
  3833. |---|
  3834. | c |"
  3835. (should
  3836. (equal '(2 . 0)
  3837. (org-export-table-cell-address
  3838. (car (last (org-element-map tree 'table-cell 'identity info)))
  3839. info))))
  3840. ;; 4. Return nil for special cells.
  3841. (org-test-with-parsed-data "| / | a |"
  3842. (should-not
  3843. (org-export-table-cell-address
  3844. (org-element-map tree 'table-cell 'identity nil 'first-match)
  3845. info))))
  3846. (ert-deftest test-org-export/get-table-cell-at ()
  3847. "Test `org-export-get-table-cell-at' specifications."
  3848. ;; 1. Address ignores special columns, special rows and rules.
  3849. (org-test-with-parsed-data "
  3850. | / | <> |
  3851. | | a |
  3852. |---+----|
  3853. | | b |"
  3854. (should
  3855. (equal '("b")
  3856. (org-element-contents
  3857. (org-export-get-table-cell-at
  3858. '(1 . 0)
  3859. (org-element-map tree 'table 'identity info 'first-match)
  3860. info)))))
  3861. ;; 2. Return value for a non-existent address is nil.
  3862. (org-test-with-parsed-data "| a |"
  3863. (should-not
  3864. (org-export-get-table-cell-at
  3865. '(2 . 2)
  3866. (org-element-map tree 'table 'identity info 'first-match)
  3867. info)))
  3868. (org-test-with-parsed-data "| / |"
  3869. (should-not
  3870. (org-export-get-table-cell-at
  3871. '(0 . 0)
  3872. (org-element-map tree 'table 'identity info 'first-match)
  3873. info))))
  3874. (ert-deftest test-org-export/table-cell-starts-colgroup-p ()
  3875. "Test `org-export-table-cell-starts-colgroup-p' specifications."
  3876. ;; 1. A cell at a beginning of a row always starts a column group.
  3877. (org-test-with-parsed-data "| a |"
  3878. (should
  3879. (org-export-table-cell-starts-colgroup-p
  3880. (org-element-map tree 'table-cell 'identity info 'first-match)
  3881. info)))
  3882. ;; 2. Special column should be ignored when determining the
  3883. ;; beginning of the row.
  3884. (org-test-with-parsed-data "
  3885. | / | |
  3886. | | a |"
  3887. (should
  3888. (org-export-table-cell-starts-colgroup-p
  3889. (org-element-map tree 'table-cell 'identity info 'first-match)
  3890. info)))
  3891. ;; 2. Explicit column groups.
  3892. (org-test-with-parsed-data "
  3893. | / | | < |
  3894. | a | b | c |"
  3895. (should
  3896. (equal
  3897. '(yes no yes)
  3898. (org-element-map tree 'table-cell
  3899. (lambda (cell)
  3900. (if (org-export-table-cell-starts-colgroup-p cell info) 'yes 'no))
  3901. info)))))
  3902. (ert-deftest test-org-export/table-cell-ends-colgroup-p ()
  3903. "Test `org-export-table-cell-ends-colgroup-p' specifications."
  3904. ;; 1. A cell at the end of a row always ends a column group.
  3905. (org-test-with-parsed-data "| a |"
  3906. (should
  3907. (org-export-table-cell-ends-colgroup-p
  3908. (org-element-map tree 'table-cell 'identity info 'first-match)
  3909. info)))
  3910. ;; 2. Special column should be ignored when determining the
  3911. ;; beginning of the row.
  3912. (org-test-with-parsed-data "
  3913. | / | |
  3914. | | a |"
  3915. (should
  3916. (org-export-table-cell-ends-colgroup-p
  3917. (org-element-map tree 'table-cell 'identity info 'first-match)
  3918. info)))
  3919. ;; 3. Explicit column groups.
  3920. (org-test-with-parsed-data "
  3921. | / | < | |
  3922. | a | b | c |"
  3923. (should
  3924. (equal
  3925. '(yes no yes)
  3926. (org-element-map tree 'table-cell
  3927. (lambda (cell)
  3928. (if (org-export-table-cell-ends-colgroup-p cell info) 'yes 'no))
  3929. info)))))
  3930. (ert-deftest test-org-export/table-row-starts-rowgroup-p ()
  3931. "Test `org-export-table-row-starts-rowgroup-p' specifications."
  3932. ;; 1. A row at the beginning of a table always starts a row group.
  3933. ;; So does a row following a table rule.
  3934. (org-test-with-parsed-data "
  3935. | a |
  3936. |---|
  3937. | b |"
  3938. (should
  3939. (equal
  3940. '(yes no yes)
  3941. (org-element-map tree 'table-row
  3942. (lambda (row)
  3943. (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
  3944. info))))
  3945. ;; 2. Special rows should be ignored when determining the beginning
  3946. ;; of the row.
  3947. (org-test-with-parsed-data "
  3948. | / | < |
  3949. | | a |
  3950. |---+---|
  3951. | / | < |
  3952. | | b |"
  3953. (should
  3954. (equal
  3955. '(yes no yes)
  3956. (org-element-map tree 'table-row
  3957. (lambda (row)
  3958. (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
  3959. info)))))
  3960. (ert-deftest test-org-export/table-row-ends-rowgroup-p ()
  3961. "Test `org-export-table-row-ends-rowgroup-p' specifications."
  3962. ;; 1. A row at the end of a table always ends a row group. So does
  3963. ;; a row preceding a table rule.
  3964. (org-test-with-parsed-data "
  3965. | a |
  3966. |---|
  3967. | b |"
  3968. (should
  3969. (equal
  3970. '(yes no yes)
  3971. (org-element-map tree 'table-row
  3972. (lambda (row)
  3973. (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
  3974. info))))
  3975. ;; 2. Special rows should be ignored when determining the beginning
  3976. ;; of the row.
  3977. (org-test-with-parsed-data "
  3978. | | a |
  3979. | / | < |
  3980. |---+---|
  3981. | | b |
  3982. | / | < |"
  3983. (should
  3984. (equal
  3985. '(yes no yes)
  3986. (org-element-map tree 'table-row
  3987. (lambda (row)
  3988. (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
  3989. info)))))
  3990. (ert-deftest test-org-export/table-row-in-header-p ()
  3991. "Test `org-export-table-row-in-header-p' specifications."
  3992. ;; Standard test. Separators are always nil.
  3993. (should
  3994. (equal
  3995. '(yes no no)
  3996. (org-test-with-parsed-data "| a |\n|---|\n| b |"
  3997. (org-element-map tree 'table-row
  3998. (lambda (row)
  3999. (if (org-export-table-row-in-header-p row info) 'yes 'no)) info))))
  4000. ;; Nil when there is no header.
  4001. (should
  4002. (equal
  4003. '(no no)
  4004. (org-test-with-parsed-data "| a |\n| b |"
  4005. (org-element-map tree 'table-row
  4006. (lambda (row)
  4007. (if (org-export-table-row-in-header-p row info) 'yes 'no)) info)))))
  4008. (ert-deftest test-org-export/table-row-starts-header-p ()
  4009. "Test `org-export-table-row-starts-header-p' specifications."
  4010. ;; 1. Only the row starting the first row group starts the table
  4011. ;; header.
  4012. (org-test-with-parsed-data "
  4013. | a |
  4014. | b |
  4015. |---|
  4016. | c |"
  4017. (should
  4018. (equal
  4019. '(yes no no no)
  4020. (org-element-map tree 'table-row
  4021. (lambda (row)
  4022. (if (org-export-table-row-starts-header-p row info) 'yes 'no))
  4023. info))))
  4024. ;; 2. A row cannot start an header if there's no header in the
  4025. ;; table.
  4026. (org-test-with-parsed-data "
  4027. | a |
  4028. |---|"
  4029. (should-not
  4030. (org-export-table-row-starts-header-p
  4031. (org-element-map tree 'table-row 'identity info 'first-match)
  4032. info))))
  4033. (ert-deftest test-org-export/table-row-ends-header-p ()
  4034. "Test `org-export-table-row-ends-header-p' specifications."
  4035. ;; 1. Only the row starting the first row group starts the table
  4036. ;; header.
  4037. (org-test-with-parsed-data "
  4038. | a |
  4039. | b |
  4040. |---|
  4041. | c |"
  4042. (should
  4043. (equal
  4044. '(no yes no no)
  4045. (org-element-map tree 'table-row
  4046. (lambda (row)
  4047. (if (org-export-table-row-ends-header-p row info) 'yes 'no))
  4048. info))))
  4049. ;; 2. A row cannot start an header if there's no header in the
  4050. ;; table.
  4051. (org-test-with-parsed-data "
  4052. | a |
  4053. |---|"
  4054. (should-not
  4055. (org-export-table-row-ends-header-p
  4056. (org-element-map tree 'table-row 'identity info 'first-match)
  4057. info))))
  4058. ;;; Tables of Contents
  4059. (ert-deftest test-org-export/collect-headlines ()
  4060. "Test `org-export-collect-headlines' specifications."
  4061. ;; Standard test.
  4062. (should
  4063. (= 2
  4064. (length
  4065. (org-test-with-parsed-data "* H1\n** H2"
  4066. (org-export-collect-headlines info)))))
  4067. ;; Do not collect headlines below optional argument.
  4068. (should
  4069. (= 1
  4070. (length
  4071. (org-test-with-parsed-data "* H1\n** H2"
  4072. (org-export-collect-headlines info 1)))))
  4073. ;; Never collect headlines below maximum headline level.
  4074. (should
  4075. (= 1
  4076. (length
  4077. (org-test-with-parsed-data "#+OPTIONS: H:1\n* H1\n** H2"
  4078. (org-export-collect-headlines info)))))
  4079. (should
  4080. (= 1
  4081. (length
  4082. (org-test-with-parsed-data "#+OPTIONS: H:1\n* H1\n** H2"
  4083. (org-export-collect-headlines info 2)))))
  4084. ;; Collect headlines locally.
  4085. (should
  4086. (= 2
  4087. (org-test-with-parsed-data "* H1\n** H2\n** H3"
  4088. (let ((scope (org-element-map tree 'headline #'identity info t)))
  4089. (length (org-export-collect-headlines info nil scope))))))
  4090. ;; When collecting locally, optional level is relative.
  4091. (should
  4092. (= 1
  4093. (org-test-with-parsed-data "* H1\n** H2\n*** H3"
  4094. (let ((scope (org-element-map tree 'headline #'identity info t)))
  4095. (length (org-export-collect-headlines info 1 scope)))))))
  4096. ;;; Templates
  4097. (ert-deftest test-org-export/inner-template ()
  4098. "Test `inner-template' translator specifications."
  4099. (should
  4100. (equal "Success!"
  4101. (org-test-with-temp-text "* Headline"
  4102. (org-export-as
  4103. (org-export-create-backend
  4104. :transcoders
  4105. '((inner-template . (lambda (contents info) "Success!"))
  4106. (headline . (lambda (h c i) "Headline"))))))))
  4107. ;; Inner template is applied even in a "body-only" export.
  4108. (should
  4109. (equal "Success!"
  4110. (org-test-with-temp-text "* Headline"
  4111. (org-export-as
  4112. (org-export-create-backend
  4113. :transcoders '((inner-template . (lambda (c i) "Success!"))
  4114. (headline . (lambda (h c i) "Headline"))))
  4115. nil nil 'body-only)))))
  4116. (ert-deftest test-org-export/template ()
  4117. "Test `template' translator specifications."
  4118. (should
  4119. (equal "Success!"
  4120. (org-test-with-temp-text "* Headline"
  4121. (org-export-as
  4122. (org-export-create-backend
  4123. :transcoders '((template . (lambda (contents info) "Success!"))
  4124. (headline . (lambda (h c i) "Headline"))))))))
  4125. ;; Template is not applied in a "body-only" export.
  4126. (should-not
  4127. (equal "Success!"
  4128. (org-test-with-temp-text "* Headline"
  4129. (org-export-as
  4130. (org-export-create-backend
  4131. :transcoders '((template . (lambda (contents info) "Success!"))
  4132. (headline . (lambda (h c i) "Headline"))))
  4133. nil nil 'body-only)))))
  4134. ;;; Topology
  4135. (ert-deftest test-org-export/get-next-element ()
  4136. "Test `org-export-get-next-element' specifications."
  4137. ;; Standard test.
  4138. (should
  4139. (equal "b"
  4140. (org-test-with-parsed-data "* Headline\n*a* b"
  4141. (org-export-get-next-element
  4142. (org-element-map tree 'bold 'identity info t) info))))
  4143. ;; Return nil when no previous element.
  4144. (should-not
  4145. (org-test-with-parsed-data "* Headline\na *b*"
  4146. (org-export-get-next-element
  4147. (org-element-map tree 'bold 'identity info t) info)))
  4148. ;; Non-exportable elements are ignored.
  4149. (should-not
  4150. (let ((org-export-with-timestamps nil))
  4151. (org-test-with-parsed-data "\alpha <2012-03-29 Thu>"
  4152. (org-export-get-next-element
  4153. (org-element-map tree 'entity 'identity info t) info))))
  4154. ;; Find next element in secondary strings.
  4155. (should
  4156. (eq 'verbatim
  4157. (org-test-with-parsed-data "* a =verb="
  4158. (org-element-type
  4159. (org-export-get-next-element
  4160. (org-element-map tree 'plain-text 'identity info t) info)))))
  4161. (should
  4162. (eq 'verbatim
  4163. (org-test-with-parsed-data "* /italic/ =verb="
  4164. (org-element-type
  4165. (org-export-get-next-element
  4166. (org-element-map tree 'italic 'identity info t) info)))))
  4167. ;; Find next element in document keywords.
  4168. (should
  4169. (eq 'verbatim
  4170. (org-test-with-parsed-data "#+TITLE: a =verb="
  4171. (org-element-type
  4172. (org-export-get-next-element
  4173. (org-element-map
  4174. (plist-get info :title) 'plain-text 'identity info t) info)))))
  4175. ;; Find next element in parsed affiliated keywords.
  4176. (should
  4177. (eq 'verbatim
  4178. (org-test-with-parsed-data "#+CAPTION: a =verb=\nParagraph"
  4179. (org-element-type
  4180. (org-export-get-next-element
  4181. (org-element-map tree 'plain-text 'identity info t nil t) info)))))
  4182. ;; With optional argument N, return a list containing all the
  4183. ;; following elements.
  4184. (should
  4185. (equal
  4186. '(bold code underline)
  4187. (org-test-with-parsed-data "_a_ /b/ *c* ~d~ _e_"
  4188. (mapcar 'car
  4189. (org-export-get-next-element
  4190. (org-element-map tree 'italic 'identity info t) info t)))))
  4191. ;; When N is a positive integer, return a list containing up to
  4192. ;; N following elements.
  4193. (should
  4194. (equal
  4195. '(bold code)
  4196. (org-test-with-parsed-data "_a_ /b/ *c* ~d~ _e_"
  4197. (mapcar 'car
  4198. (org-export-get-next-element
  4199. (org-element-map tree 'italic 'identity info t) info 2))))))
  4200. (ert-deftest test-org-export/get-previous-element ()
  4201. "Test `org-export-get-previous-element' specifications."
  4202. ;; Standard test.
  4203. (should
  4204. (equal "a "
  4205. (org-test-with-parsed-data "* Headline\na *b*"
  4206. (org-export-get-previous-element
  4207. (org-element-map tree 'bold 'identity info t) info))))
  4208. ;; Return nil when no previous element.
  4209. (should-not
  4210. (org-test-with-parsed-data "* Headline\n*a* b"
  4211. (org-export-get-previous-element
  4212. (org-element-map tree 'bold 'identity info t) info)))
  4213. ;; Non-exportable elements are ignored.
  4214. (should-not
  4215. (let ((org-export-with-timestamps nil))
  4216. (org-test-with-parsed-data "<2012-03-29 Thu> \alpha"
  4217. (org-export-get-previous-element
  4218. (org-element-map tree 'entity 'identity info t) info))))
  4219. ;; Find previous element in secondary strings.
  4220. (should
  4221. (eq 'verbatim
  4222. (org-test-with-parsed-data "* =verb= a"
  4223. (org-element-type
  4224. (org-export-get-previous-element
  4225. (org-element-map tree 'plain-text 'identity info t) info)))))
  4226. (should
  4227. (eq 'verbatim
  4228. (org-test-with-parsed-data "* =verb= /italic/"
  4229. (org-element-type
  4230. (org-export-get-previous-element
  4231. (org-element-map tree 'italic 'identity info t) info)))))
  4232. ;; Find previous element in document keywords.
  4233. (should
  4234. (eq 'verbatim
  4235. (org-test-with-parsed-data "#+TITLE: =verb= a"
  4236. (org-element-type
  4237. (org-export-get-previous-element
  4238. (org-element-map
  4239. (plist-get info :title) 'plain-text 'identity info t) info)))))
  4240. ;; Find previous element in parsed affiliated keywords.
  4241. (should
  4242. (eq 'verbatim
  4243. (org-test-with-parsed-data "#+CAPTION: =verb= a\nParagraph"
  4244. (org-element-type
  4245. (org-export-get-previous-element
  4246. (org-element-map tree 'plain-text 'identity info t nil t) info)))))
  4247. ;; With optional argument N, return a list containing up to
  4248. ;; N previous elements.
  4249. (should
  4250. (equal '(underline italic bold)
  4251. (org-test-with-parsed-data "_a_ /b/ *c* ~d~"
  4252. (mapcar 'car
  4253. (org-export-get-previous-element
  4254. (org-element-map tree 'code 'identity info t) info t)))))
  4255. ;; When N is a positive integer, return a list containing up to
  4256. ;; N previous elements.
  4257. (should
  4258. (equal '(italic bold)
  4259. (org-test-with-parsed-data "_a_ /b/ *c* ~d~"
  4260. (mapcar 'car
  4261. (org-export-get-previous-element
  4262. (org-element-map tree 'code 'identity info t) info 2))))))
  4263. (provide 'test-ox)
  4264. ;;; test-org-export.el end here