test-ox.el 125 KB

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