test-ox.el 125 KB

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