test-ox.el 121 KB

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