test-ox.el 133 KB

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