test-ox.el 121 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668
  1. ;;; test-ox.el --- Tests for ox.el
  2. ;; Copyright (C) 2012-2015 Nicolas Goaziou
  3. ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
  4. ;; This file is not part of GNU Emacs.
  5. ;; This program is free software; you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; This program is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Code:
  16. (unless (featurep 'ox)
  17. (signal 'missing-test-dependency "org-export"))
  18. (defun org-test-default-backend ()
  19. "Return a default export back-end.
  20. This back-end simply returns parsed data as Org syntax."
  21. (org-export-create-backend
  22. :transcoders (let (transcode-table)
  23. (dolist (type (append org-element-all-elements
  24. org-element-all-objects)
  25. transcode-table)
  26. (push
  27. (cons type
  28. (lambda (obj contents info)
  29. (funcall
  30. (intern (format "org-element-%s-interpreter"
  31. type))
  32. obj contents)))
  33. transcode-table)))))
  34. (defmacro org-test-with-parsed-data (data &rest body)
  35. "Execute body with parsed data available.
  36. DATA is a string containing the data to be parsed. BODY is the
  37. body to execute. Parse tree is available under the `tree'
  38. variable, and communication channel under `info'."
  39. (declare (debug (form body)) (indent 1))
  40. `(org-test-with-temp-text ,data
  41. (org-export--delete-comments)
  42. (let* ((tree (org-element-parse-buffer))
  43. (info (org-export-get-environment)))
  44. (org-export--prune-tree tree info)
  45. (org-export--remove-uninterpreted-data tree info)
  46. (let ((info (org-combine-plists
  47. info (org-export-collect-tree-properties tree info))))
  48. ,@body))))
  49. ;;; Internal Tests
  50. (ert-deftest test-org-export/bind-keyword ()
  51. "Test reading #+BIND: keywords."
  52. ;; Test with `org-export-allow-bind-keywords' set to t.
  53. (should
  54. (org-test-with-temp-text "#+BIND: test-ox-var value"
  55. (let ((org-export-allow-bind-keywords t))
  56. (org-export-get-environment)
  57. (eq test-ox-var 'value))))
  58. ;; Test with `org-export-allow-bind-keywords' set to nil.
  59. (should-not
  60. (org-test-with-temp-text "#+BIND: test-ox-var value"
  61. (let ((org-export-allow-bind-keywords nil))
  62. (org-export-get-environment)
  63. (boundp 'test-ox-var))))
  64. ;; BIND keywords are case-insensitive.
  65. (should
  66. (org-test-with-temp-text "#+bind: test-ox-var value"
  67. (let ((org-export-allow-bind-keywords t))
  68. (org-export-get-environment)
  69. (eq test-ox-var 'value))))
  70. ;; Preserve order of BIND keywords.
  71. (should
  72. (org-test-with-temp-text "#+BIND: test-ox-var 1\n#+BIND: test-ox-var 2"
  73. (let ((org-export-allow-bind-keywords t))
  74. (org-export-get-environment)
  75. (eq test-ox-var 2))))
  76. ;; Read BIND keywords in setup files.
  77. (should
  78. (org-test-with-temp-text
  79. (format "#+SETUPFILE: \"%s/examples/setupfile.org\"" org-test-dir)
  80. (let ((org-export-allow-bind-keywords t))
  81. (org-export-get-environment)
  82. (eq variable 'value))))
  83. ;; Verify that bound variables are seen during export.
  84. (should
  85. (equal "Yes\n"
  86. (org-test-with-temp-text "#+BIND: test-ox-var value"
  87. (let ((org-export-allow-bind-keywords t))
  88. (org-export-as
  89. (org-export-create-backend
  90. :transcoders
  91. '((section . (lambda (s c i)
  92. (if (eq test-ox-var 'value) "Yes" "No")))))))))))
  93. (ert-deftest test-org-export/parse-option-keyword ()
  94. "Test reading all standard #+OPTIONS: items."
  95. (should
  96. (equal
  97. (org-export--parse-option-keyword
  98. "H:1 num:t \\n:t timestamp:t arch:t author:t creator:t d:t email:t
  99. *:t e:t ::t f:t pri:t -:t ^:t toc:t |:t tags:t tasks:t <:t todo:t inline:nil
  100. stat:t title:t")
  101. '(:headline-levels
  102. 1 :preserve-breaks t :section-numbers t :time-stamp-file t
  103. :with-archived-trees t :with-author t :with-creator t :with-drawers t
  104. :with-email t :with-emphasize t :with-entities t :with-fixed-width t
  105. :with-footnotes t :with-inlinetasks nil :with-priority t
  106. :with-special-strings t :with-statistics-cookies t :with-sub-superscript t
  107. :with-toc t :with-tables t :with-tags t :with-tasks t :with-timestamps t
  108. :with-title t :with-todo-keywords t)))
  109. ;; Test some special values.
  110. (should
  111. (equal
  112. (org-export--parse-option-keyword
  113. "arch:headline d:(\"TEST\") ^:{} toc:1 tags:not-in-toc tasks:todo num:2 <:active")
  114. '( :section-numbers
  115. 2
  116. :with-archived-trees headline :with-drawers ("TEST")
  117. :with-sub-superscript {} :with-toc 1 :with-tags not-in-toc
  118. :with-tasks todo :with-timestamps active))))
  119. (ert-deftest test-org-export/get-inbuffer-options ()
  120. "Test reading all standard export keywords."
  121. ;; Properties should follow buffer order.
  122. (should
  123. (equal
  124. (org-test-with-temp-text "#+LANGUAGE: fr\n#+CREATOR: Me\n#+EMAIL: email"
  125. (org-export--get-inbuffer-options))
  126. '(:language "fr" :creator "Me" :email "email")))
  127. ;; Test `space' behaviour.
  128. (should
  129. (equal
  130. (let ((back-end (org-export-create-backend
  131. :options '((:keyword "KEYWORD" nil nil space)))))
  132. (org-test-with-temp-text "#+KEYWORD: With\n#+KEYWORD: spaces"
  133. (org-export--get-inbuffer-options back-end)))
  134. '(:keyword "With spaces")))
  135. ;; Test `newline' behaviour.
  136. (should
  137. (equal
  138. (let ((back-end (org-export-create-backend
  139. :options '((:keyword "KEYWORD" nil nil newline)))))
  140. (org-test-with-temp-text "#+KEYWORD: With\n#+KEYWORD: two lines"
  141. (org-export--get-inbuffer-options back-end)))
  142. '(:keyword "With\ntwo lines")))
  143. ;; Test `split' behaviour.
  144. (should
  145. (equal
  146. (org-test-with-temp-text "#+SELECT_TAGS: a\n#+SELECT_TAGS: b"
  147. (org-export--get-inbuffer-options))
  148. '(:select-tags ("a" "b"))))
  149. ;; Test `parse' behaviour.
  150. (should
  151. (org-element-map
  152. (org-test-with-temp-text "#+TITLE: *bold*"
  153. (plist-get (org-export--get-inbuffer-options) :title))
  154. 'bold #'identity nil t))
  155. (should
  156. (equal
  157. (org-test-with-temp-text "#+TITLE: Some title\n#+TITLE: with spaces"
  158. (plist-get (org-export--get-inbuffer-options) :title))
  159. '("Some title" " with spaces")))
  160. ;; Options set through SETUPFILE.
  161. (should
  162. (equal
  163. (org-test-with-temp-text
  164. (format "#+DESCRIPTION: l1
  165. #+LANGUAGE: es
  166. #+SELECT_TAGS: a
  167. #+TITLE: a
  168. #+SETUPFILE: \"%s/examples/setupfile.org\"
  169. #+LANGUAGE: fr
  170. #+SELECT_TAGS: c
  171. #+TITLE: c"
  172. org-test-dir)
  173. (org-export--get-inbuffer-options))
  174. '(:language "fr" :select-tags ("a" "b" "c") :title ("a" " b" " c"))))
  175. ;; More than one property can refer to the same buffer keyword.
  176. (should
  177. (equal '(:k2 "value" :k1 "value")
  178. (let ((backend (org-export-create-backend
  179. :options '((:k1 "KEYWORD")
  180. (:k2 "KEYWORD")))))
  181. (org-test-with-temp-text "#+KEYWORD: value"
  182. (org-export--get-inbuffer-options backend)))))
  183. ;; Keywords in commented subtrees are ignored.
  184. (should-not
  185. (equal "Me"
  186. (org-test-with-parsed-data "* COMMENT H1\n#+AUTHOR: Me"
  187. (plist-get info :author))))
  188. (should-not
  189. (equal "Mine"
  190. (org-test-with-parsed-data "* COMMENT H1\n** H2\n#+EMAIL: Mine"
  191. (plist-get info :email)))))
  192. (ert-deftest test-org-export/get-subtree-options ()
  193. "Test setting options from headline's properties."
  194. ;; EXPORT_TITLE.
  195. (org-test-with-temp-text "#+TITLE: Title
  196. * Headline
  197. :PROPERTIES:
  198. :EXPORT_TITLE: Subtree Title
  199. :END:
  200. Paragraph"
  201. (forward-line)
  202. (should (equal (plist-get (org-export-get-environment nil t) :title)
  203. '("Subtree Title"))))
  204. :title
  205. '("subtree-title")
  206. ;; EXPORT_OPTIONS.
  207. (org-test-with-temp-text "#+OPTIONS: H:1
  208. * Headline
  209. :PROPERTIES:
  210. :EXPORT_OPTIONS: H:2
  211. :END:
  212. Paragraph"
  213. (forward-line)
  214. (should
  215. (= 2 (plist-get (org-export-get-environment nil t) :headline-levels))))
  216. ;; EXPORT_DATE.
  217. (org-test-with-temp-text "#+DATE: today
  218. * Headline
  219. :PROPERTIES:
  220. :EXPORT_DATE: 29-03-2012
  221. :END:
  222. Paragraph"
  223. (forward-line)
  224. (should (equal (plist-get (org-export-get-environment nil t) :date)
  225. '("29-03-2012"))))
  226. ;; Properties with `split' behaviour are stored as a list of
  227. ;; strings.
  228. (should
  229. (equal '("a" "b")
  230. (org-test-with-temp-text "#+EXCLUDE_TAGS: noexport
  231. * Headline
  232. :PROPERTIES:
  233. :EXPORT_EXCLUDE_TAGS: a b
  234. :END:
  235. Paragraph"
  236. (progn
  237. (forward-line)
  238. (plist-get (org-export-get-environment nil t) :exclude-tags)))))
  239. ;; Handle :PROPERTY+: syntax.
  240. (should
  241. (equal '("a" "b")
  242. (org-test-with-temp-text "#+EXCLUDE_TAGS: noexport
  243. * Headline
  244. :PROPERTIES:
  245. :EXPORT_EXCLUDE_TAGS: a
  246. :EXPORT_EXCLUDE_TAGS+: b
  247. :END:
  248. Paragraph"
  249. (progn
  250. (forward-line)
  251. (plist-get (org-export-get-environment nil t) :exclude-tags)))))
  252. ;; Export properties are case-insensitive.
  253. (org-test-with-temp-text "* Headline
  254. :PROPERTIES:
  255. :EXPORT_Date: 29-03-2012
  256. :END:
  257. Paragraph"
  258. (should (equal (plist-get (org-export-get-environment nil t) :date)
  259. '("29-03-2012"))))
  260. ;; Still grab correct options when section above is empty.
  261. (should
  262. (equal '("H1")
  263. (org-test-with-temp-text "* H1\n** H11\n** H12"
  264. (progn (forward-line 2)
  265. (plist-get (org-export-get-environment nil t) :title))))))
  266. (ert-deftest test-org-export/set-title ()
  267. "Test title setting."
  268. ;; Without TITLE keyword.
  269. (should
  270. (equal
  271. ""
  272. (let (org-export-filter-body-functions
  273. org-export-filter-final-output-functions)
  274. (org-test-with-temp-text "Test"
  275. (org-export-as
  276. (org-export-create-backend
  277. :transcoders
  278. '((template . (lambda (text info)
  279. (org-element-interpret-data
  280. (plist-get info :title)))))))))))
  281. ;; With a blank TITLE keyword.
  282. (should
  283. (equal
  284. ""
  285. (let (org-export-filter-body-functions
  286. org-export-filter-final-output-functions)
  287. (org-test-with-temp-text "#+TITLE:\nTest"
  288. (org-export-as
  289. (org-export-create-backend
  290. :transcoders
  291. '((template . (lambda (text info)
  292. (org-element-interpret-data
  293. (plist-get info :title)))))))))))
  294. ;; With a non-empty TITLE keyword.
  295. (should
  296. (equal
  297. "Title"
  298. (org-test-with-temp-text "#+TITLE: Title\nTest"
  299. (org-export-as
  300. (org-export-create-backend
  301. :transcoders
  302. '((template . (lambda (text info)
  303. (org-element-interpret-data
  304. (plist-get info :title))))))))))
  305. ;; When exporting a subtree, its heading becomes the headline of the
  306. ;; document...
  307. (should
  308. (equal
  309. "Headline"
  310. (org-test-with-temp-text "* Headline\nBody"
  311. (org-export-as
  312. (org-export-create-backend
  313. :transcoders
  314. '((template . (lambda (text info)
  315. (org-element-interpret-data
  316. (plist-get info :title))))))
  317. 'subtree))))
  318. ;; ... unless there is an EXPORT_TITLE property at the root of the
  319. ;; subtree.
  320. (should
  321. (equal
  322. "B"
  323. (org-test-with-temp-text
  324. "* A\n :PROPERTIES:\n :EXPORT_TITLE: B\n :END:\nBody"
  325. (org-export-as
  326. (org-export-create-backend
  327. :transcoders
  328. '((template . (lambda (text info)
  329. (org-element-interpret-data
  330. (plist-get info :title))))))
  331. 'subtree)))))
  332. (ert-deftest test-org-export/handle-options ()
  333. "Test if export options have an impact on output."
  334. ;; Test exclude tags for headlines and inlinetasks.
  335. (should
  336. (equal ""
  337. (let (org-export-filter-body-functions
  338. org-export-filter-final-output-functions)
  339. (org-test-with-temp-text "* Head1 :noexp:"
  340. (org-export-as (org-test-default-backend)
  341. nil nil nil '(:exclude-tags ("noexp")))))))
  342. ;; Test include tags for headlines and inlinetasks.
  343. (should
  344. (equal "* H2\n** Sub :exp:\n*** Sub Sub\n"
  345. (org-test-with-temp-text "* H1\n* H2\n** Sub :exp:\n*** Sub Sub\n* H3"
  346. (let ((org-tags-column 0))
  347. (org-export-as (org-test-default-backend)
  348. nil nil nil '(:select-tags ("exp")))))))
  349. ;; If there is an include tag, ignore the section before the first
  350. ;; headline, if any.
  351. (should
  352. (equal "* H1 :exp:\nBody\n"
  353. (org-test-with-temp-text "First section\n* H1 :exp:\nBody"
  354. (let ((org-tags-column 0))
  355. (org-export-as (org-test-default-backend)
  356. nil nil nil '(:select-tags ("exp")))))))
  357. (should-not
  358. (equal "* H1 :exp:\n"
  359. (org-test-with-temp-text "* H1 :exp:\nBody"
  360. (let ((org-tags-column 0))
  361. (org-export-as (org-test-default-backend)
  362. nil nil nil '(:select-tags ("exp")))))))
  363. ;; Test mixing include tags and exclude tags.
  364. (should
  365. (string-match
  366. "\\* Head1[ \t]+:export:\n\\*\\* Sub-Head2\n"
  367. (org-test-with-temp-text "
  368. * Head1 :export:
  369. ** Sub-Head1 :noexport:
  370. ** Sub-Head2
  371. * Head2 :noexport:
  372. ** Sub-Head1 :export:"
  373. (org-export-as (org-test-default-backend) nil nil nil
  374. '(:select-tags ("export") :exclude-tags ("noexport"))))))
  375. ;; Ignore tasks.
  376. (should
  377. (equal ""
  378. (let ((org-todo-keywords '((sequence "TODO" "DONE")))
  379. org-export-filter-body-functions
  380. org-export-filter-final-output-functions)
  381. (org-test-with-temp-text "* TODO Head1"
  382. (org-export-as (org-test-default-backend)
  383. nil nil nil '(:with-tasks nil))))))
  384. (should
  385. (equal "* TODO Head1\n"
  386. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  387. (org-test-with-temp-text "* TODO Head1"
  388. (org-export-as (org-test-default-backend)
  389. nil nil nil '(:with-tasks t))))))
  390. ;; Archived tree.
  391. (should
  392. (equal ""
  393. (let (org-export-filter-body-functions
  394. org-export-filter-final-output-functions)
  395. (org-test-with-temp-text "* Head1 :archive:"
  396. (let ((org-archive-tag "archive"))
  397. (org-export-as (org-test-default-backend)
  398. nil nil nil '(:with-archived-trees nil)))))))
  399. (should
  400. (string-match
  401. "\\* Head1[ \t]+:archive:"
  402. (org-test-with-temp-text "* Head1 :archive:\nbody\n** Sub-head 2"
  403. (let ((org-archive-tag "archive"))
  404. (org-export-as (org-test-default-backend) nil nil nil
  405. '(:with-archived-trees headline))))))
  406. (should
  407. (string-match
  408. "\\`\\* Head1[ \t]+:archive:\n\\'"
  409. (org-test-with-temp-text "* Head1 :archive:"
  410. (let ((org-archive-tag "archive"))
  411. (org-export-as (org-test-default-backend)
  412. nil nil nil '(:with-archived-trees t))))))
  413. ;; Clocks.
  414. (should
  415. (string-match "CLOCK: \\[2012-04-29 .* 10:45\\]"
  416. (org-test-with-temp-text "CLOCK: [2012-04-29 sun. 10:45]"
  417. (org-export-as (org-test-default-backend)
  418. nil nil nil '(:with-clocks t)))))
  419. (should
  420. (equal ""
  421. (let (org-export-filter-body-functions
  422. org-export-filter-final-output-functions)
  423. (org-test-with-temp-text "CLOCK: [2012-04-29 sun. 10:45]"
  424. (org-export-as (org-test-default-backend)
  425. nil nil nil '(:with-clocks nil))))))
  426. ;; Drawers.
  427. (should
  428. (equal ""
  429. (let (org-export-filter-body-functions
  430. org-export-filter-final-output-functions)
  431. (org-test-with-temp-text ":TEST:\ncontents\n:END:"
  432. (org-export-as (org-test-default-backend)
  433. nil nil nil '(:with-drawers nil))))))
  434. (should
  435. (equal ":TEST:\ncontents\n:END:\n"
  436. (org-test-with-temp-text ":TEST:\ncontents\n:END:"
  437. (org-export-as (org-test-default-backend)
  438. nil nil nil '(:with-drawers t)))))
  439. (should
  440. (equal ":FOO:\nkeep\n:END:\n"
  441. (org-test-with-temp-text ":FOO:\nkeep\n:END:\n:BAR:\nremove\n:END:"
  442. (org-export-as (org-test-default-backend)
  443. nil nil nil '(:with-drawers ("FOO"))))))
  444. (should
  445. (equal ":FOO:\nkeep\n:END:\n"
  446. (org-test-with-temp-text ":FOO:\nkeep\n:END:\n:BAR:\nremove\n:END:"
  447. (org-export-as (org-test-default-backend)
  448. nil nil nil '(:with-drawers (not "BAR"))))))
  449. ;; Fixed-width.
  450. (should
  451. (equal ": A\n"
  452. (org-test-with-temp-text ": A"
  453. (org-export-as (org-test-default-backend) nil nil nil
  454. '(:with-fixed-width t)))))
  455. (should
  456. (equal ""
  457. (let (org-export-filter-body-functions
  458. org-export-filter-final-output-functions)
  459. (org-test-with-temp-text ": A"
  460. (org-export-as (org-test-default-backend) nil nil nil
  461. '(:with-fixed-width nil))))))
  462. ;; Footnotes.
  463. (should
  464. (equal "Footnote?"
  465. (let ((org-footnote-section nil))
  466. (org-test-with-temp-text "Footnote?[fn:1]\n\n[fn:1] Def"
  467. (org-trim (org-export-as (org-test-default-backend)
  468. nil nil nil '(:with-footnotes nil)))))))
  469. (should
  470. (equal "Footnote?[fn:1]\n\n[fn:1] Def"
  471. (let ((org-footnote-section nil))
  472. (org-test-with-temp-text "Footnote?[fn:1]\n\n[fn:1] Def"
  473. (org-trim (org-export-as (org-test-default-backend)
  474. nil nil nil '(:with-footnotes t)))))))
  475. ;; Inlinetasks.
  476. (when (featurep 'org-inlinetask)
  477. (should
  478. (equal
  479. ""
  480. (let ((org-inlinetask-min-level 15)
  481. org-export-filter-body-functions
  482. org-export-filter-final-output-functions)
  483. (org-test-with-temp-text "*************** Task"
  484. (org-export-as (org-test-default-backend)
  485. nil nil nil '(:with-inlinetasks nil))))))
  486. (should
  487. (equal
  488. ""
  489. (let ((org-inlinetask-min-level 15)
  490. org-export-filter-body-functions
  491. org-export-filter-final-output-functions)
  492. (org-test-with-temp-text
  493. "*************** Task\nContents\n*************** END"
  494. (org-export-as (org-test-default-backend)
  495. nil nil nil '(:with-inlinetasks nil)))))))
  496. ;; Plannings.
  497. (should
  498. (string-match
  499. "* H\nCLOSED: \\[2012-04-29 .* 10:45\\]"
  500. (let ((org-closed-string "CLOSED:"))
  501. (org-test-with-temp-text "* H\nCLOSED: [2012-04-29 sun. 10:45]"
  502. (org-export-as (org-test-default-backend)
  503. nil nil nil '(:with-planning t))))))
  504. (should
  505. (equal "* H\n"
  506. (let ((org-closed-string "CLOSED:"))
  507. (org-test-with-temp-text "* H\nCLOSED: [2012-04-29 sun. 10:45]"
  508. (org-export-as (org-test-default-backend)
  509. nil nil nil '(:with-planning nil))))))
  510. ;; Property Drawers.
  511. (should
  512. (equal "* H1\n"
  513. (org-test-with-temp-text
  514. "* H1\n :PROPERTIES:\n :PROP: value\n :END:"
  515. (org-export-as (org-test-default-backend)
  516. nil nil nil '(:with-properties nil)))))
  517. (should
  518. (equal "* H1\n:PROPERTIES:\n:PROP: value\n:END:\n"
  519. (org-test-with-temp-text
  520. "* H1\n :PROPERTIES:\n :PROP: value\n :END:"
  521. (org-export-as (org-test-default-backend)
  522. nil nil nil '(:with-properties t)))))
  523. (should
  524. (equal "* H1\n:PROPERTIES:\n:B: 2\n:END:\n"
  525. (org-test-with-temp-text
  526. "* H1\n :PROPERTIES:\n :A: 1\n :B: 2\n:END:"
  527. (org-export-as (org-test-default-backend)
  528. nil nil nil '(:with-properties ("B"))))))
  529. ;; Statistics cookies.
  530. (should
  531. (equal ""
  532. (let (org-export-filter-body-functions
  533. org-export-filter-final-output-functions)
  534. (org-trim
  535. (org-test-with-temp-text "[0/0]"
  536. (org-export-as (org-test-default-backend)
  537. nil nil nil '(:with-statistics-cookies nil)))))))
  538. ;; Tables.
  539. (should
  540. (equal "| A |\n"
  541. (org-test-with-temp-text "| A |"
  542. (org-export-as (org-test-default-backend) nil nil nil
  543. '(:with-tables t)))))
  544. (should
  545. (equal ""
  546. (let (org-export-filter-body-functions
  547. org-export-filter-final-output-functions)
  548. (org-test-with-temp-text "| A |"
  549. (org-export-as (org-test-default-backend) nil nil nil
  550. '(:with-tables nil)))))))
  551. (ert-deftest test-org-export/with-timestamps ()
  552. "Test `org-export-with-timestamps' specifications."
  553. ;; t value.
  554. (should
  555. (string-match
  556. "\\[2012-04-29 .*? 10:45\\]<2012-04-29 .*? 10:45>"
  557. (org-test-with-temp-text "[2012-04-29 sun. 10:45]<2012-04-29 sun. 10:45>"
  558. (org-export-as (org-test-default-backend)
  559. nil nil nil '(:with-timestamps t)))))
  560. ;; nil value.
  561. (should
  562. (equal
  563. ""
  564. (let (org-export-filter-body-functions
  565. org-export-filter-final-output-functions)
  566. (org-trim
  567. (org-test-with-temp-text "[2012-04-29 sun. 10:45]<2012-04-29 sun. 10:45>"
  568. (org-export-as (org-test-default-backend)
  569. nil nil nil '(:with-timestamps nil)))))))
  570. ;; `active' value.
  571. (should
  572. (string-match
  573. "<2012-03-29 .*?>\n\nParagraph <2012-03-29 .*?>\\[2012-03-29 .*?\\]"
  574. (org-test-with-temp-text
  575. "<2012-03-29 Thu>[2012-03-29 Thu]
  576. Paragraph <2012-03-29 Thu>[2012-03-29 Thu]"
  577. (org-export-as (org-test-default-backend)
  578. nil nil nil '(:with-timestamps active)))))
  579. ;; `inactive' value.
  580. (should
  581. (string-match
  582. "\\[2012-03-29 .*?\\]\n\nParagraph <2012-03-29 .*?>\\[2012-03-29 .*?\\]"
  583. (org-test-with-temp-text
  584. "<2012-03-29 Thu>[2012-03-29 Thu]
  585. Paragraph <2012-03-29 Thu>[2012-03-29 Thu]"
  586. (org-export-as (org-test-default-backend)
  587. nil nil nil '(:with-timestamps inactive))))))
  588. (ert-deftest test-org-export/comment-tree ()
  589. "Test if export process ignores commented trees."
  590. (should
  591. (equal ""
  592. (let (org-export-filter-body-functions
  593. org-export-filter-final-output-functions)
  594. (org-test-with-temp-text "* COMMENT Head1"
  595. (org-export-as (org-test-default-backend)))))))
  596. (ert-deftest test-org-export/uninterpreted ()
  597. "Test handling of uninterpreted elements."
  598. ;; Entities.
  599. (should
  600. (equal "dummy\n"
  601. (org-test-with-temp-text "\\alpha"
  602. (org-export-as
  603. (org-export-create-backend
  604. :transcoders '((entity . (lambda (e c i) "dummy"))
  605. (paragraph . (lambda (p c i) c))
  606. (section . (lambda (s c i) c))))
  607. nil nil nil '(:with-entities t)))))
  608. (should
  609. (equal "\\alpha\n"
  610. (org-test-with-temp-text "\\alpha"
  611. (org-export-as
  612. (org-export-create-backend
  613. :transcoders '((entity . (lambda (e c i) "dummy"))
  614. (paragraph . (lambda (p c i) c))
  615. (section . (lambda (s c i) c))))
  616. nil nil nil '(:with-entities nil)))))
  617. ;; Emphasis.
  618. (should
  619. (equal "dummy\n"
  620. (org-test-with-temp-text "*bold*"
  621. (org-export-as
  622. (org-export-create-backend
  623. :transcoders '((bold . (lambda (b c i) "dummy"))
  624. (paragraph . (lambda (p c i) c))
  625. (section . (lambda (s c i) c))))
  626. nil nil nil '(:with-emphasize t)))))
  627. (should
  628. (equal "*bold*\n"
  629. (org-test-with-temp-text "*bold*"
  630. (org-export-as
  631. (org-export-create-backend
  632. :transcoders '((bold . (lambda (b c i) "dummy"))
  633. (paragraph . (lambda (p c i) c))
  634. (section . (lambda (s c i) c))))
  635. nil nil nil '(:with-emphasize nil)))))
  636. ;; LaTeX environment.
  637. (should
  638. (equal "dummy\n"
  639. (org-test-with-temp-text "\\begin{equation}\n1+1=2\n\\end{equation}"
  640. (org-export-as
  641. (org-export-create-backend
  642. :transcoders '((latex-environment . (lambda (l c i) "dummy"))
  643. (section . (lambda (s c i) c))))
  644. nil nil nil '(:with-latex t)))))
  645. (should
  646. (equal "\\begin{equation}\n1+1=2\n\\end{equation}\n"
  647. (org-test-with-temp-text "\\begin{equation}\n1+1=2\n\\end{equation}"
  648. (org-export-as
  649. (org-export-create-backend
  650. :transcoders '((latex-environment . (lambda (l c i) "dummy"))
  651. (section . (lambda (s c i) c))))
  652. nil nil nil '(:with-latex verbatim)))))
  653. ;; LaTeX fragment.
  654. (should
  655. (equal "dummy\n"
  656. (org-test-with-temp-text "$1$"
  657. (org-export-as
  658. (org-export-create-backend
  659. :transcoders '((latex-fragment . (lambda (l c i) "dummy"))
  660. (paragraph . (lambda (p c i) c))
  661. (section . (lambda (s c i) c))))
  662. nil nil nil '(:with-latex t)))))
  663. (should
  664. (equal "$1$\n"
  665. (org-test-with-temp-text "$1$"
  666. (org-export-as
  667. (org-export-create-backend
  668. :transcoders '((latex-fragment . (lambda (l c i) "dummy"))
  669. (paragraph . (lambda (p c i) c))
  670. (section . (lambda (s c i) c))))
  671. nil nil nil '(:with-latex verbatim)))))
  672. ;; Sub/superscript.
  673. (should
  674. (equal "adummy\n"
  675. (org-test-with-temp-text "a_b"
  676. (org-export-as
  677. (org-export-create-backend
  678. :transcoders '((subscript . (lambda (s c i) "dummy"))
  679. (paragraph . (lambda (p c i) c))
  680. (section . (lambda (s c i) c))))
  681. nil nil nil '(:with-sub-superscript t)))))
  682. (should
  683. (equal "a_b\n"
  684. (org-test-with-temp-text "a_b"
  685. (org-export-as
  686. (org-export-create-backend
  687. :transcoders '((subscript . (lambda (s c i) "dummy"))
  688. (paragraph . (lambda (p c i) c))
  689. (section . (lambda (s c i) c))))
  690. nil nil nil '(:with-sub-superscript nil)))))
  691. (should
  692. (equal "a_b\n"
  693. (org-test-with-temp-text "a_b"
  694. (org-export-as
  695. (org-export-create-backend
  696. :transcoders '((subscript . (lambda (s c i) "dummy"))
  697. (paragraph . (lambda (p c i) c))
  698. (section . (lambda (s c i) c))))
  699. nil nil nil '(:with-sub-superscript {})))))
  700. (should
  701. (equal "adummy\n"
  702. (org-test-with-temp-text "a_{b}"
  703. (org-export-as
  704. (org-export-create-backend
  705. :transcoders '((subscript . (lambda (s c i) "dummy"))
  706. (paragraph . (lambda (p c i) c))
  707. (section . (lambda (s c i) c))))
  708. nil nil nil '(:with-sub-superscript {})))))
  709. ;; Also handle uninterpreted objects in title.
  710. (should
  711. (equal "a_b"
  712. (org-test-with-temp-text "#+TITLE: a_b"
  713. (org-export-as
  714. (org-export-create-backend
  715. :transcoders
  716. '((subscript . (lambda (s c i) "dummy"))
  717. (template . (lambda (c i) (org-export-data
  718. (plist-get i :title) i)))
  719. (section . (lambda (s c i) c))))
  720. nil nil nil '(:with-sub-superscript nil)))))
  721. ;; Special case: multiples uninterpreted objects in a row.
  722. (should
  723. (equal "a_b_c_d\n"
  724. (org-test-with-temp-text "a_b_c_d"
  725. (org-export-as
  726. (org-export-create-backend
  727. :transcoders '((subscript . (lambda (s c i) "dummy"))
  728. (paragraph . (lambda (p c i) c))
  729. (section . (lambda (s c i) c))))
  730. nil nil nil '(:with-sub-superscript {}))))))
  731. (ert-deftest test-org-export/export-scope ()
  732. "Test all export scopes."
  733. (org-test-with-temp-text "
  734. * Head1
  735. ** Head2
  736. text
  737. *** Head3"
  738. ;; Subtree.
  739. (forward-line 3)
  740. (should (equal (org-export-as (org-test-default-backend) 'subtree)
  741. "text\n*** Head3\n"))
  742. ;; Visible.
  743. (goto-char (point-min))
  744. (forward-line)
  745. (org-cycle)
  746. (should (equal (org-export-as (org-test-default-backend) nil 'visible)
  747. "* Head1\n"))
  748. ;; Region.
  749. (goto-char (point-min))
  750. (forward-line 3)
  751. (transient-mark-mode 1)
  752. (push-mark (point) t t)
  753. (goto-char (point-at-eol))
  754. (should (equal (org-export-as (org-test-default-backend)) "text\n")))
  755. ;; Subtree with a code block calling another block outside.
  756. (let ((org-export-babel-evaluate t))
  757. (should
  758. (equal ": 3\n"
  759. (org-test-with-temp-text "
  760. * Head1
  761. #+BEGIN_SRC emacs-lisp :noweb yes :exports results
  762. <<test>>
  763. #+END_SRC
  764. * Head2
  765. #+NAME: test
  766. #+BEGIN_SRC emacs-lisp
  767. \(+ 1 2)
  768. #+END_SRC"
  769. (forward-line 1)
  770. (org-export-as (org-test-default-backend) 'subtree)))))
  771. ;; Body only.
  772. (let ((backend (org-test-default-backend)))
  773. (setf (org-export-backend-transcoders backend)
  774. (cons '(template . (lambda (body i)
  775. (format "BEGIN\n%sEND" body)))
  776. (org-export-backend-transcoders backend)))
  777. (org-test-with-temp-text "Text"
  778. (should (equal (org-export-as backend nil nil 'body-only)
  779. "Text\n"))
  780. (should (equal (org-export-as backend) "BEGIN\nText\nEND")))))
  781. (ert-deftest test-org-export/output-file-name ()
  782. "Test `org-export-output-file-name' specifications."
  783. ;; Export from a file: name is built from original file name.
  784. (should
  785. (org-test-with-temp-text-in-file "Test"
  786. (equal (concat (file-name-as-directory ".")
  787. (file-name-nondirectory
  788. (file-name-sans-extension (buffer-file-name))))
  789. (file-name-sans-extension (org-export-output-file-name ".ext")))))
  790. ;; When exporting to subtree, check EXPORT_FILE_NAME property first.
  791. (should
  792. (org-test-with-temp-text-in-file
  793. "* Test\n :PROPERTIES:\n :EXPORT_FILE_NAME: test\n :END:"
  794. (equal (org-export-output-file-name ".ext" t) "./test.ext")))
  795. ;; From a buffer not associated to a file, too.
  796. (should
  797. (org-test-with-temp-text
  798. "* Test\n :PROPERTIES:\n :EXPORT_FILE_NAME: test\n :END:"
  799. (equal (org-export-output-file-name ".ext" t) "./test.ext")))
  800. ;; When provided name is absolute, preserve it.
  801. (should
  802. (org-test-with-temp-text
  803. (format "* Test\n :PROPERTIES:\n :EXPORT_FILE_NAME: %s\n :END:"
  804. (expand-file-name "test"))
  805. (file-name-absolute-p (org-export-output-file-name ".ext" t))))
  806. ;; When PUB-DIR argument is provided, use it.
  807. (should
  808. (org-test-with-temp-text-in-file "Test"
  809. (equal (file-name-directory
  810. (org-export-output-file-name ".ext" nil "dir/"))
  811. "dir/")))
  812. ;; When returned name would overwrite original file, add EXTENSION
  813. ;; another time.
  814. (should
  815. (org-test-at-id "75282ba2-f77a-4309-a970-e87c149fe125"
  816. (equal (org-export-output-file-name ".org") "./normal.org.org"))))
  817. (ert-deftest test-org-export/expand-include ()
  818. "Test file inclusion in an Org buffer."
  819. ;; Error when file isn't specified.
  820. (should-error
  821. (org-test-with-temp-text "#+INCLUDE: dummy.org"
  822. (org-export-expand-include-keyword)))
  823. ;; Full insertion with recursive inclusion.
  824. (should
  825. (equal
  826. (with-temp-buffer
  827. (insert-file
  828. (expand-file-name "examples/include.org" org-test-dir))
  829. (replace-regexp-in-string
  830. (regexp-quote "#+INCLUDE: \"include2.org\"")
  831. "Success!" (buffer-string)))
  832. (org-test-with-temp-text
  833. (format "#+INCLUDE: \"%s/examples/include.org\"" org-test-dir)
  834. (org-export-expand-include-keyword)
  835. (buffer-string))))
  836. ;; Localized insertion.
  837. (org-test-with-temp-text
  838. (format "#+INCLUDE: \"%s/examples/include.org\" :lines \"1-2\""
  839. org-test-dir)
  840. (org-export-expand-include-keyword)
  841. (should (equal (buffer-string)
  842. "Small Org file with an include keyword.\n")))
  843. ;; Insertion with constraints on headlines level.
  844. (should
  845. (equal
  846. "* Top heading\n** Heading\nbody\n"
  847. (org-test-with-temp-text
  848. (format
  849. "* Top heading\n#+INCLUDE: \"%s/examples/include.org\" :lines \"9-11\""
  850. org-test-dir)
  851. (org-export-expand-include-keyword)
  852. (buffer-string))))
  853. (should
  854. (equal
  855. "* Top heading\n* Heading\nbody\n"
  856. (org-test-with-temp-text
  857. (format
  858. "* Top heading\n#+INCLUDE: \"%s/examples/include.org\" :lines \"9-11\" :minlevel 1"
  859. org-test-dir)
  860. (org-export-expand-include-keyword)
  861. (buffer-string))))
  862. ;; Inclusion within an example block.
  863. (should
  864. (equal
  865. "#+BEGIN_EXAMPLE\nSmall Org file with an include keyword.\n#+END_EXAMPLE\n"
  866. (org-test-with-temp-text
  867. (format "#+INCLUDE: \"%s/examples/include.org\" :lines \"1-2\" EXAMPLE"
  868. org-test-dir)
  869. (org-export-expand-include-keyword)
  870. (buffer-string))))
  871. ;; Inclusion within a src-block.
  872. (should
  873. (equal
  874. "#+BEGIN_SRC emacs-lisp\n(+ 2 1)\n#+END_SRC\n"
  875. (org-test-with-temp-text
  876. (format
  877. "#+INCLUDE: \"%s/examples/include.org\" :lines \"4-5\" SRC emacs-lisp"
  878. org-test-dir)
  879. (org-export-expand-include-keyword)
  880. (buffer-string))))
  881. ;; Inclusion within an html export-block.
  882. (should
  883. (equal
  884. "#+BEGIN_HTML\n<p>HTML!</p>\n#+END_HTML\n"
  885. (org-test-with-temp-text
  886. (format
  887. "#+INCLUDE: \"%s/examples/include.html\" HTML"
  888. org-test-dir)
  889. (org-export-expand-include-keyword)
  890. (buffer-string))))
  891. ;; Inclusion within an center paragraph
  892. (should
  893. (equal
  894. "#+BEGIN_CENTER\nSuccess!\n#+END_CENTER\n"
  895. (org-test-with-temp-text
  896. (format
  897. "#+INCLUDE: \"%s/examples/include2.org\" CENTER"
  898. org-test-dir)
  899. (org-export-expand-include-keyword)
  900. (buffer-string))))
  901. ;; Footnotes labels are local to each included file.
  902. (should
  903. (= 6
  904. (length
  905. (delete-dups
  906. (let ((contents "
  907. Footnotes[fn:1], [fn:test] and [fn:inline:anonymous footnote].
  908. \[fn:1] Footnote 1
  909. \[fn:test] Footnote \"test\""))
  910. (org-test-with-temp-text-in-file contents
  911. (let ((file1 (buffer-file-name)))
  912. (org-test-with-temp-text-in-file contents
  913. (let ((file2 (buffer-file-name)))
  914. (org-test-with-temp-text
  915. (format "#+INCLUDE: \"%s\"\n#+INCLUDE: \"%s\""
  916. file1 file2)
  917. (org-export-expand-include-keyword)
  918. (org-element-map (org-element-parse-buffer)
  919. 'footnote-reference
  920. (lambda (ref)
  921. (org-element-property :label ref)))))))))))))
  922. ;; Footnotes labels are not local to each include keyword.
  923. (should
  924. (= 4
  925. (length
  926. (delete-dups
  927. (let ((contents "
  928. Footnotes[fn:1], [fn:test], [2] and [fn:inline:anonymous footnote].
  929. \[fn:1] Footnote 1
  930. \[2] Footnote 2
  931. \[fn:test] Footnote \"test\""))
  932. (org-test-with-temp-text-in-file contents
  933. (let ((file (buffer-file-name)))
  934. (org-test-with-temp-text
  935. (format "#+INCLUDE: \"%s\"\n#+INCLUDE: \"%s\"" file file)
  936. (org-export-expand-include-keyword)
  937. (org-element-map (org-element-parse-buffer)
  938. 'footnote-reference
  939. (lambda (ref) (org-element-property :label ref)))))))))))
  940. ;; Footnotes are supported by :lines-like elements and unnecessary
  941. ;; footnotes are dropped.
  942. (should
  943. (= 4
  944. (length
  945. (delete-dups
  946. (let ((contents "
  947. * foo
  948. Footnotes[fn:1]
  949. * bar
  950. Footnotes[fn:2], foot[fn:test], digit only[3], and [fn:inline:anonymous footnote]
  951. \[fn:1] Footnote 1
  952. \[fn:2] Footnote 1
  953. * Footnotes
  954. \[fn:test] Footnote \"test\"
  955. \[3] Footnote 3
  956. "))
  957. (org-test-with-temp-text-in-file contents
  958. (let ((file (buffer-file-name)))
  959. (org-test-with-temp-text
  960. (format "#+INCLUDE: \"%s::*bar\"
  961. " file)
  962. (org-export-expand-include-keyword)
  963. (org-element-map (org-element-parse-buffer)
  964. 'footnote-definition
  965. (lambda (ref) (org-element-property :label ref)))))))))))
  966. ;; If only-contents is non-nil only include contents of element.
  967. (should
  968. (equal
  969. "body\n"
  970. (org-test-with-temp-text
  971. (concat
  972. (format "#+INCLUDE: \"%s/examples/include.org::*Heading\" " org-test-dir)
  973. ":only-contents t")
  974. (org-export-expand-include-keyword)
  975. (buffer-string))))
  976. ;; Headings can be included via CUSTOM_ID.
  977. (should
  978. (org-test-with-temp-text
  979. (format "#+INCLUDE: \"%s/examples/include.org::#ah\"" org-test-dir)
  980. (org-export-expand-include-keyword)
  981. (goto-char (point-min))
  982. (looking-at "* Another heading")))
  983. ;; Named objects can be included.
  984. (should
  985. (equal
  986. "| 1 |\n"
  987. (org-test-with-temp-text
  988. (format "#+INCLUDE: \"%s/examples/include.org::tbl\" :only-contents t" org-test-dir)
  989. (org-export-expand-include-keyword)
  990. (buffer-string))))
  991. ;; Including non-existing elements should result in an error.
  992. (should-error
  993. (org-test-with-temp-text
  994. (format "#+INCLUDE: \"%s/examples/include.org::*non-existing heading\"" org-test-dir)
  995. (org-export-expand-include-keyword)))
  996. ;; Lines work relatively to an included element.
  997. (should
  998. (equal
  999. "2\n3\n"
  1000. (org-test-with-temp-text
  1001. (format "#+INCLUDE: \"%s/examples/include.org::#ah\" :only-contents t :lines \"2-3\"" org-test-dir)
  1002. (org-export-expand-include-keyword)
  1003. (buffer-string))))
  1004. ;; Properties should be dropped from headlines.
  1005. (should
  1006. (equal
  1007. (org-test-with-temp-text
  1008. (format "#+INCLUDE: \"%s/examples/include.org::#ht\" :only-contents t" org-test-dir)
  1009. (org-export-expand-include-keyword)
  1010. (buffer-string))
  1011. (org-test-with-temp-text
  1012. (format "#+INCLUDE: \"%s/examples/include.org::tbl\"" org-test-dir)
  1013. (org-export-expand-include-keyword)
  1014. (buffer-string))))
  1015. ;; Properties should be dropped, drawers should not be.
  1016. (should
  1017. (equal
  1018. ":LOGBOOK:\ndrawer\n:END:\ncontent\n"
  1019. (org-test-with-temp-text
  1020. (format "#+INCLUDE: \"%s/examples/include.org::#dh\" :only-contents t" org-test-dir)
  1021. (org-export-expand-include-keyword)
  1022. (buffer-string))))
  1023. ;; Adjacent INCLUDE-keywords should have the same :minlevel if unspecified.
  1024. (should
  1025. (org-every (lambda (level) (zerop (1- level)))
  1026. (org-test-with-temp-text
  1027. (concat
  1028. (format "#+INCLUDE: \"%s/examples/include.org::#ah\"\n" org-test-dir)
  1029. (format "#+INCLUDE: \"%s/examples/include.org::*Heading\"" org-test-dir))
  1030. (org-export-expand-include-keyword)
  1031. (org-element-map (org-element-parse-buffer) 'headline
  1032. (lambda (head) (org-element-property :level head))))))
  1033. ;; INCLUDE does not insert induced :minlevel for src-blocks.
  1034. (should-not
  1035. (equal
  1036. (org-test-with-temp-text
  1037. (format "#+INCLUDE: \"%s/examples/include2.org\" src emacs-lisp" org-test-dir)
  1038. (org-export-expand-include-keyword)
  1039. (buffer-string))
  1040. (org-test-with-temp-text
  1041. (format "#+INCLUDE: \"%s/examples/include2.org\" src emacs-lisp :minlevel 1" org-test-dir)
  1042. (org-export-expand-include-keyword)
  1043. (buffer-string))))
  1044. ;; INCLUDE assigns the relative :minlevel conditional on narrowing.
  1045. (should
  1046. (org-test-with-temp-text-in-file
  1047. (format "* h1\n<point>#+INCLUDE: \"%s/examples/include.org::#ah\"" org-test-dir)
  1048. (narrow-to-region (point) (point-max))
  1049. (org-export-expand-include-keyword)
  1050. (eq 1 (org-current-level))))
  1051. ;; If :minlevel is present do not alter it.
  1052. (should
  1053. (org-test-with-temp-text
  1054. (format "* h1\n<point>#+INCLUDE: \"%s/examples/include.org::#ah\" :minlevel 3" org-test-dir)
  1055. (narrow-to-region (point) (point-max))
  1056. (org-export-expand-include-keyword)
  1057. (eq 3 (org-current-level)))))
  1058. (ert-deftest test-org-export/expand-macro ()
  1059. "Test macro expansion in an Org buffer."
  1060. ;; Standard macro expansion.
  1061. (should
  1062. (equal "#+MACRO: macro1 value\nvalue\n"
  1063. (org-test-with-temp-text "#+MACRO: macro1 value\n{{{macro1}}}"
  1064. (org-export-as (org-test-default-backend)))))
  1065. ;; Allow macro in parsed keywords and associated properties.
  1066. ;; Standard macro expansion.
  1067. (should
  1068. (string-match
  1069. "#\\+K: value"
  1070. (let ((backend (org-export-create-backend
  1071. :parent 'org
  1072. :options '((:k "K" nil nil parse)))))
  1073. (org-test-with-temp-text "#+MACRO: macro value\n#+K: {{{macro}}}"
  1074. (org-export-as backend)))))
  1075. (should
  1076. (string-match
  1077. ":EXPORT_K: v"
  1078. (let ((backend (org-export-create-backend
  1079. :parent 'org
  1080. :options '((:k "K" nil nil parse)))))
  1081. (org-test-with-temp-text
  1082. "#+MACRO: m v\n* H\n:PROPERTIES:\n:EXPORT_K: {{{m}}}\n:END:"
  1083. (org-export-as backend nil nil nil '(:with-properties t))))))
  1084. ;; Expand specific macros.
  1085. (should
  1086. (equal "me 2012-03-29 me@here Title\n"
  1087. (org-test-with-temp-text
  1088. "
  1089. #+TITLE: Title
  1090. #+DATE: 2012-03-29
  1091. #+AUTHOR: me
  1092. #+EMAIL: me@here
  1093. {{{author}}} {{{date}}} {{{email}}} {{{title}}}"
  1094. (let ((output (org-export-as (org-test-default-backend))))
  1095. (substring output (string-match ".*\n\\'" output))))))
  1096. ;; Expand specific macros when property contained a regular macro
  1097. ;; already.
  1098. (should
  1099. (equal "value\n"
  1100. (org-test-with-temp-text "
  1101. #+MACRO: macro1 value
  1102. #+TITLE: {{{macro1}}}
  1103. {{{title}}}"
  1104. (let ((output (org-export-as (org-test-default-backend))))
  1105. (substring output (string-match ".*\n\\'" output))))))
  1106. ;; Expand macros with templates in included files.
  1107. (should
  1108. (equal "success\n"
  1109. (org-test-with-temp-text
  1110. (format "#+INCLUDE: \"%s/examples/macro-templates.org\"
  1111. {{{included-macro}}}" org-test-dir)
  1112. (let ((output (org-export-as (org-test-default-backend))))
  1113. (substring output (string-match ".*\n\\'" output))))))
  1114. ;; Date macro takes a optional formatting argument
  1115. (should
  1116. (equal "09-02-15\n"
  1117. (org-test-with-temp-text "{{{date(%d-%m-%y)}}}\n* d :noexport:\n#+DATE: <2015-02-09>"
  1118. (org-export-as (org-test-default-backend)))))
  1119. ;; Only single timestamps are formatted
  1120. (should
  1121. (equal "<2015-02x-09>\n"
  1122. (org-test-with-temp-text "{{{date(%d-%m-%y)}}}\n* d :noexport:\n#+DATE: <2015-02x-09>"
  1123. (org-export-as (org-test-default-backend)))))
  1124. ;; Throw an error when a macro definition is missing.
  1125. (should-error
  1126. (org-test-with-temp-text "{{{missing}}}"
  1127. (org-export-as (org-test-default-backend))))
  1128. ;; Macros defined in commented subtrees are ignored.
  1129. (should-error
  1130. (org-test-with-temp-text
  1131. "* COMMENT H\n#+MACRO: macro1\n* H2\nvalue\n{{{macro1}}}"
  1132. (org-export-as (org-test-default-backend))))
  1133. (should-error
  1134. (org-test-with-temp-text
  1135. "* COMMENT H\n** H2\n#+MACRO: macro1\n* H3\nvalue\n{{{macro1}}}"
  1136. (org-export-as (org-test-default-backend)))))
  1137. (ert-deftest test-org-export/before-processing-hook ()
  1138. "Test `org-export-before-processing-hook'."
  1139. (should
  1140. (equal
  1141. "#+MACRO: mac val\nTest\n"
  1142. (org-test-with-temp-text "#+MACRO: mac val\n{{{mac}}} Test"
  1143. (let ((org-export-before-processing-hook
  1144. '((lambda (backend)
  1145. (while (re-search-forward "{{{" nil t)
  1146. (let ((object (org-element-context)))
  1147. (when (eq (org-element-type object) 'macro)
  1148. (delete-region
  1149. (org-element-property :begin object)
  1150. (org-element-property :end object)))))))))
  1151. (org-export-as (org-test-default-backend)))))))
  1152. (ert-deftest test-org-export/before-parsing-hook ()
  1153. "Test `org-export-before-parsing-hook'."
  1154. (should
  1155. (equal "Body 1\nBody 2\n"
  1156. (org-test-with-temp-text "* Headline 1\nBody 1\n* Headline 2\nBody 2"
  1157. (let ((org-export-before-parsing-hook
  1158. '((lambda (backend)
  1159. (goto-char (point-min))
  1160. (while (re-search-forward org-outline-regexp-bol nil t)
  1161. (delete-region
  1162. (point-at-bol) (progn (forward-line) (point))))))))
  1163. (org-export-as (org-test-default-backend)))))))
  1164. ;;; Affiliated Keywords
  1165. (ert-deftest test-org-export/read-attribute ()
  1166. "Test `org-export-read-attribute' specifications."
  1167. ;; Standard test.
  1168. (should
  1169. (equal
  1170. (org-export-read-attribute
  1171. :attr_html
  1172. (org-test-with-temp-text "#+ATTR_HTML: :a 1 :b 2\nParagraph"
  1173. (org-element-at-point)))
  1174. '(:a "1" :b "2")))
  1175. ;; Return nil on empty attribute.
  1176. (should-not
  1177. (org-export-read-attribute
  1178. :attr_html
  1179. (org-test-with-temp-text "Paragraph" (org-element-at-point))))
  1180. ;; Return nil on "nil" string.
  1181. (should
  1182. (equal '(:a nil :b nil)
  1183. (org-export-read-attribute
  1184. :attr_html
  1185. (org-test-with-temp-text "#+ATTR_HTML: :a nil :b nil\nParagraph"
  1186. (org-element-at-point)))))
  1187. ;; Return nil on empty string.
  1188. (should
  1189. (equal '(:a nil :b nil)
  1190. (org-export-read-attribute
  1191. :attr_html
  1192. (org-test-with-temp-text "#+ATTR_HTML: :a :b\nParagraph"
  1193. (org-element-at-point)))))
  1194. ;; Return empty string when value is "".
  1195. (should
  1196. (equal '(:a "")
  1197. (org-export-read-attribute
  1198. :attr_html
  1199. (org-test-with-temp-text "#+ATTR_HTML: :a \"\"\nParagraph"
  1200. (org-element-at-point)))))
  1201. ;; Return \"\" when value is """".
  1202. (should
  1203. (equal '(:a "\"\"")
  1204. (org-export-read-attribute
  1205. :attr_html
  1206. (org-test-with-temp-text "#+ATTR_HTML: :a \"\"\"\"\nParagraph"
  1207. (org-element-at-point)))))
  1208. ;; Ignore text before first property.
  1209. (should-not
  1210. (member "ignore"
  1211. (org-export-read-attribute
  1212. :attr_html
  1213. (org-test-with-temp-text "#+ATTR_HTML: ignore :a 1\nParagraph"
  1214. (org-element-at-point))))))
  1215. (ert-deftest test-org-export/get-caption ()
  1216. "Test `org-export-get-caption' specifications."
  1217. ;; Without optional argument, return long caption
  1218. (should
  1219. (equal
  1220. '("l")
  1221. (org-test-with-temp-text "#+CAPTION[s]: l\nPara"
  1222. (org-export-get-caption (org-element-at-point)))))
  1223. ;; With optional argument, return short caption.
  1224. (should
  1225. (equal
  1226. '("s")
  1227. (org-test-with-temp-text "#+CAPTION[s]: l\nPara"
  1228. (org-export-get-caption (org-element-at-point) t))))
  1229. ;; Multiple lines are separated by white spaces.
  1230. (should
  1231. (equal
  1232. '("a" " " "b")
  1233. (org-test-with-temp-text "#+CAPTION: a\n#+CAPTION: b\nPara"
  1234. (org-export-get-caption (org-element-at-point))))))
  1235. ;;; Back-End Tools
  1236. (ert-deftest test-org-export/define-backend ()
  1237. "Test back-end definition and accessors."
  1238. ;; Translate table.
  1239. (should
  1240. (equal '((headline . my-headline-test))
  1241. (let (org-export--registered-backends)
  1242. (org-export-define-backend 'test '((headline . my-headline-test)))
  1243. (org-export-get-all-transcoders 'test))))
  1244. ;; Filters.
  1245. (should
  1246. (equal '((:filter-headline . my-filter))
  1247. (let (org-export--registered-backends)
  1248. (org-export-define-backend 'test
  1249. '((headline . my-headline-test))
  1250. :filters-alist '((:filter-headline . my-filter)))
  1251. (org-export-backend-filters (org-export-get-backend 'test)))))
  1252. ;; Options.
  1253. (should
  1254. (equal '((:prop value))
  1255. (let (org-export--registered-backends)
  1256. (org-export-define-backend 'test
  1257. '((headline . my-headline-test))
  1258. :options-alist '((:prop value)))
  1259. (org-export-backend-options (org-export-get-backend 'test)))))
  1260. ;; Menu.
  1261. (should
  1262. (equal '(?k "Test Export" test)
  1263. (let (org-export--registered-backends)
  1264. (org-export-define-backend 'test
  1265. '((headline . my-headline-test))
  1266. :menu-entry '(?k "Test Export" test))
  1267. (org-export-backend-menu (org-export-get-backend 'test)))))
  1268. ;; Export Blocks.
  1269. (should
  1270. (equal '(("TEST" . org-element-export-block-parser))
  1271. (let (org-export--registered-backends org-element-block-name-alist)
  1272. (org-export-define-backend 'test
  1273. '((headline . my-headline-test))
  1274. :export-block '("test"))
  1275. org-element-block-name-alist))))
  1276. (ert-deftest test-org-export/define-derived-backend ()
  1277. "Test `org-export-define-derived-backend' specifications."
  1278. ;; Error when parent back-end is not defined.
  1279. (should-error
  1280. (let (org-export--registered-backends)
  1281. (org-export-define-derived-backend 'test 'parent)))
  1282. ;; Append translation table to parent's.
  1283. (should
  1284. (equal '((:headline . test) (:headline . parent))
  1285. (let (org-export--registered-backends)
  1286. (org-export-define-backend 'parent '((:headline . parent)))
  1287. (org-export-define-derived-backend 'test 'parent
  1288. :translate-alist '((:headline . test)))
  1289. (org-export-get-all-transcoders 'test))))
  1290. ;; Options defined in the new back have priority over those defined
  1291. ;; in parent.
  1292. (should
  1293. (eq 'test
  1294. (let (org-export--registered-backends)
  1295. (org-export-define-backend 'parent
  1296. '((:headline . parent))
  1297. :options-alist '((:a nil nil 'parent)))
  1298. (org-export-define-derived-backend 'test 'parent
  1299. :options-alist '((:a nil nil 'test)))
  1300. (plist-get (org-export--get-global-options
  1301. (org-export-get-backend 'test))
  1302. :a)))))
  1303. (ert-deftest test-org-export/derived-backend-p ()
  1304. "Test `org-export-derived-backend-p' specifications."
  1305. ;; Non-nil with direct match.
  1306. (should
  1307. (let (org-export--registered-backends)
  1308. (org-export-define-backend 'test '((headline . test)))
  1309. (org-export-derived-backend-p 'test 'test)))
  1310. (should
  1311. (let (org-export--registered-backends)
  1312. (org-export-define-backend 'test '((headline . test)))
  1313. (org-export-define-derived-backend 'test2 'test)
  1314. (org-export-derived-backend-p 'test2 'test2)))
  1315. ;; Non-nil with a direct parent.
  1316. (should
  1317. (let (org-export--registered-backends)
  1318. (org-export-define-backend 'test '((headline . test)))
  1319. (org-export-define-derived-backend 'test2 'test)
  1320. (org-export-derived-backend-p 'test2 'test)))
  1321. ;; Non-nil with an indirect parent.
  1322. (should
  1323. (let (org-export--registered-backends)
  1324. (org-export-define-backend 'test '((headline . test)))
  1325. (org-export-define-derived-backend 'test2 'test)
  1326. (org-export-define-derived-backend 'test3 'test2)
  1327. (org-export-derived-backend-p 'test3 'test)))
  1328. ;; Nil otherwise.
  1329. (should-not
  1330. (let (org-export--registered-backends)
  1331. (org-export-define-backend 'test '((headline . test)))
  1332. (org-export-define-backend 'test2 '((headline . test2)))
  1333. (org-export-derived-backend-p 'test2 'test)))
  1334. (should-not
  1335. (let (org-export--registered-backends)
  1336. (org-export-define-backend 'test '((headline . test)))
  1337. (org-export-define-backend 'test2 '((headline . test2)))
  1338. (org-export-define-derived-backend 'test3 'test2)
  1339. (org-export-derived-backend-p 'test3 'test))))
  1340. (ert-deftest test-org-export/get-all-transcoders ()
  1341. "Test `org-export-get-all-transcoders' specifications."
  1342. ;; Return nil when back-end cannot be found.
  1343. (should-not (org-export-get-all-transcoders nil))
  1344. ;; Same as `org-export-transcoders' if no parent.
  1345. (should
  1346. (equal '((headline . ignore))
  1347. (org-export-get-all-transcoders
  1348. (org-export-create-backend
  1349. :transcoders '((headline . ignore))))))
  1350. ;; But inherit from all ancestors whenever possible.
  1351. (should
  1352. (equal '((section . ignore) (headline . ignore))
  1353. (let (org-export--registered-backends)
  1354. (org-export-define-backend 'b1 '((headline . ignore)))
  1355. (org-export-get-all-transcoders
  1356. (org-export-create-backend
  1357. :parent 'b1 :transcoders '((section . ignore)))))))
  1358. (should
  1359. (equal '((paragraph . ignore) (section . ignore) (headline . ignore))
  1360. (let (org-export--registered-backends)
  1361. (org-export-define-backend 'b1 '((headline . ignore)))
  1362. (org-export-define-derived-backend 'b2 'b1
  1363. :translate-alist '((section . ignore)))
  1364. (org-export-get-all-transcoders
  1365. (org-export-create-backend
  1366. :parent 'b2 :transcoders '((paragraph . ignore)))))))
  1367. ;; Back-end transcoders overrule inherited ones.
  1368. (should
  1369. (eq 'b
  1370. (let (org-export--registered-backends)
  1371. (org-export-define-backend 'b1 '((headline . a)))
  1372. (cdr (assq 'headline
  1373. (org-export-get-all-transcoders
  1374. (org-export-create-backend
  1375. :parent 'b1 :transcoders '((headline . b))))))))))
  1376. (ert-deftest test-org-export/get-all-options ()
  1377. "Test `org-export-get-all-options' specifications."
  1378. ;; Return nil when back-end cannot be found.
  1379. (should-not (org-export-get-all-options nil))
  1380. ;; Same as `org-export-options' if no parent.
  1381. (should
  1382. (equal '((headline . ignore))
  1383. (org-export-get-all-options
  1384. (org-export-create-backend
  1385. :options '((headline . ignore))))))
  1386. ;; But inherit from all ancestors whenever possible.
  1387. (should
  1388. (equal '((:key2 value2) (:key1 value1))
  1389. (let (org-export--registered-backends)
  1390. (org-export-define-backend 'b1 nil :options-alist '((:key1 value1)))
  1391. (org-export-get-all-options
  1392. (org-export-create-backend
  1393. :parent 'b1 :options '((:key2 value2)))))))
  1394. (should
  1395. (equal '((:key3 value3) (:key2 value2) (:key1 value1))
  1396. (let (org-export--registered-backends)
  1397. (org-export-define-backend 'b1 nil :options-alist '((:key1 value1)))
  1398. (org-export-define-derived-backend 'b2 'b1
  1399. :options-alist '((:key2 value2)))
  1400. (org-export-get-all-options
  1401. (org-export-create-backend
  1402. :parent 'b2 :options '((:key3 value3)))))))
  1403. ;; Back-end options overrule inherited ones.
  1404. (should
  1405. (eq 'b
  1406. (let (org-export--registered-backends)
  1407. (org-export-define-backend 'b1 nil :options-alist '((:key1 . a)))
  1408. (cdr (assq :key1
  1409. (org-export-get-all-options
  1410. (org-export-create-backend
  1411. :parent 'b1 :options '((:key1 . b))))))))))
  1412. (ert-deftest test-org-export/get-all-filters ()
  1413. "Test `org-export-get-all-filters' specifications."
  1414. ;; Return nil when back-end cannot be found.
  1415. (should-not (org-export-get-all-filters nil))
  1416. ;; Same as `org-export-filters' if no parent.
  1417. (should
  1418. (equal '((:filter-headline . ignore))
  1419. (org-export-get-all-filters
  1420. (org-export-create-backend
  1421. :filters '((:filter-headline . ignore))))))
  1422. ;; But inherit from all ancestors whenever possible.
  1423. (should
  1424. (equal '((:filter-section . ignore) (:filter-headline . ignore))
  1425. (let (org-export--registered-backends)
  1426. (org-export-define-backend 'b1
  1427. nil :filters-alist '((:filter-headline . ignore)))
  1428. (org-export-get-all-filters
  1429. (org-export-create-backend
  1430. :parent 'b1 :filters '((:filter-section . ignore)))))))
  1431. (should
  1432. (equal '((:filter-paragraph . ignore)
  1433. (:filter-section . ignore)
  1434. (:filter-headline . ignore))
  1435. (let (org-export--registered-backends)
  1436. (org-export-define-backend 'b1
  1437. nil :filters-alist '((:filter-headline . ignore)))
  1438. (org-export-define-derived-backend 'b2 'b1
  1439. :filters-alist '((:filter-section . ignore)))
  1440. (org-export-get-all-filters
  1441. (org-export-create-backend
  1442. :parent 'b2 :filters '((:filter-paragraph . ignore)))))))
  1443. ;; Back-end filters overrule inherited ones.
  1444. (should
  1445. (eq 'b
  1446. (let (org-export--registered-backends)
  1447. (org-export-define-backend 'b1 '((:filter-headline . a)))
  1448. (cdr (assq :filter-headline
  1449. (org-export-get-all-filters
  1450. (org-export-create-backend
  1451. :parent 'b1 :filters '((:filter-headline . b))))))))))
  1452. (ert-deftest test-org-export/with-backend ()
  1453. "Test `org-export-with-backend' definition."
  1454. ;; Error when calling an undefined back-end
  1455. (should-error (org-export-with-backend nil "Test"))
  1456. ;; Error when called back-end doesn't have an appropriate
  1457. ;; transcoder.
  1458. (should-error
  1459. (org-export-with-backend
  1460. (org-export-create-backend :transcoders '((headline . ignore)))
  1461. "Test"))
  1462. ;; Otherwise, export using correct transcoder
  1463. (should
  1464. (equal "Success"
  1465. (let (org-export--registered-backends)
  1466. (org-export-define-backend 'test
  1467. '((plain-text . (lambda (text contents info) "Failure"))))
  1468. (org-export-define-backend 'test2
  1469. '((plain-text . (lambda (text contents info) "Success"))))
  1470. (org-export-with-backend 'test2 "Test"))))
  1471. ;; Provide correct back-end if transcoder needs to use recursive
  1472. ;; calls anyway.
  1473. (should
  1474. (equal "Success\n"
  1475. (let ((test-back-end
  1476. (org-export-create-backend
  1477. :transcoders
  1478. '((headline . (lambda (headline contents info)
  1479. (org-export-data
  1480. (org-element-property :title headline)
  1481. info)))
  1482. (plain-text . (lambda (text info) "Success"))))))
  1483. (org-export-string-as
  1484. "* Test"
  1485. (org-export-create-backend
  1486. :transcoders
  1487. '((headline . (lambda (headline contents info)
  1488. (org-export-with-backend
  1489. test-back-end headline contents info))))))))))
  1490. (ert-deftest test-org-export/data-with-backend ()
  1491. "Test `org-export-data-with-backend' specifications."
  1492. ;; Error when calling an undefined back-end.
  1493. (should-error (org-export-data-with-backend nil "nil" nil))
  1494. ;; Otherwise, export data recursively, using correct back-end.
  1495. (should
  1496. (equal
  1497. "Success!"
  1498. (org-export-data-with-backend
  1499. '(bold nil "Test")
  1500. (org-export-create-backend
  1501. :transcoders
  1502. '((plain-text . (lambda (text info) "Success"))
  1503. (bold . (lambda (bold contents info) (concat contents "!")))))
  1504. '(:with-emphasize t)))))
  1505. ;;; Export Snippets
  1506. (ert-deftest test-org-export/export-snippet ()
  1507. "Test export snippets transcoding."
  1508. ;; Standard test.
  1509. (org-test-with-temp-text "@@test:A@@@@t:B@@"
  1510. (let ((backend (org-test-default-backend)))
  1511. (setf (org-export-backend-name backend) 'test)
  1512. (setf (org-export-backend-transcoders backend)
  1513. (cons (cons 'export-snippet
  1514. (lambda (snippet contents info)
  1515. (when (eq (org-export-snippet-backend snippet) 'test)
  1516. (org-element-property :value snippet))))
  1517. (org-export-backend-transcoders backend)))
  1518. (let ((org-export-snippet-translation-alist nil))
  1519. (should (equal (org-export-as backend) "A\n")))
  1520. (let ((org-export-snippet-translation-alist '(("t" . "test"))))
  1521. (should (equal (org-export-as backend) "AB\n")))))
  1522. ;; Ignored export snippets do not remove any blank.
  1523. (should
  1524. (equal "begin end\n"
  1525. (org-test-with-parsed-data "begin @@test:A@@ end"
  1526. (org-export-data-with-backend
  1527. tree
  1528. (org-export-create-backend
  1529. :transcoders
  1530. '((paragraph . (lambda (paragraph contents info) contents))
  1531. (section . (lambda (section contents info) contents))))
  1532. info)))))
  1533. ;;; Footnotes
  1534. (ert-deftest test-org-export/footnote-first-reference-p ()
  1535. "Test `org-export-footnote-first-reference-p' specifications."
  1536. (should
  1537. (equal
  1538. '(t nil)
  1539. (org-test-with-temp-text "Text[fn:1][fn:1]\n\n[fn:1] Definition"
  1540. (let (result)
  1541. (org-export-as
  1542. (org-export-create-backend
  1543. :transcoders
  1544. `(,(cons 'footnote-reference
  1545. (lambda (f c i)
  1546. (push (org-export-footnote-first-reference-p f info)
  1547. result)
  1548. ""))
  1549. (section . (lambda (s c i) c))
  1550. (paragraph . (lambda (p c i) c))))
  1551. nil nil nil '(:with-footnotes t))
  1552. (nreverse result)))))
  1553. ;; Limit check to DATA, when non-nil.
  1554. (should
  1555. (equal
  1556. '(nil t)
  1557. (org-test-with-parsed-data "Text[fn:1]\n* H\nText[fn:1]\n\n[fn:1] D1"
  1558. (let (result)
  1559. (org-element-map tree 'footnote-reference
  1560. (lambda (ref)
  1561. (push
  1562. (org-export-footnote-first-reference-p
  1563. ref info (org-element-map tree 'headline #'identity info t))
  1564. result))
  1565. info)
  1566. (nreverse result)))))
  1567. (should
  1568. (equal
  1569. '(t nil)
  1570. (org-test-with-parsed-data "Text[fn:1]\n* H\nText[fn:1]\n\n[fn:1] D1"
  1571. (let (result)
  1572. (org-element-map tree 'footnote-reference
  1573. (lambda (ref)
  1574. (push (org-export-footnote-first-reference-p ref info) result))
  1575. info)
  1576. (nreverse result)))))
  1577. ;; If optional argument BODY-FIRST is non-nil, first find footnote
  1578. ;; in the main body of the document. Otherwise, enter footnote
  1579. ;; definitions when they are encountered.
  1580. (should
  1581. (equal
  1582. '(t nil)
  1583. (org-test-with-temp-text
  1584. ":BODY:\nText[fn:1][fn:2]\n:END:\n\n[fn:1] Definition[fn:2]\n\n[fn:2] Inner"
  1585. (let (result)
  1586. (org-export-as
  1587. (org-export-create-backend
  1588. :transcoders
  1589. `(,(cons 'footnote-reference
  1590. (lambda (f c i)
  1591. (when (org-element-lineage f '(drawer))
  1592. (push (org-export-footnote-first-reference-p f info nil)
  1593. result))
  1594. ""))
  1595. (drawer . (lambda (d c i) c))
  1596. (footnote-definition . (lambda (d c i) c))
  1597. (section . (lambda (s c i) c))
  1598. (paragraph . (lambda (p c i) c))))
  1599. nil nil nil '(:with-footnotes t))
  1600. (nreverse result)))))
  1601. (should
  1602. (equal
  1603. '(t t)
  1604. (org-test-with-temp-text
  1605. ":BODY:\nText[fn:1][fn:2]\n:END:\n\n[fn:1] Definition[fn:2]\n\n[fn:2] Inner"
  1606. (let (result)
  1607. (org-export-as
  1608. (org-export-create-backend
  1609. :transcoders
  1610. `(,(cons 'footnote-reference
  1611. (lambda (f c i)
  1612. (when (org-element-lineage f '(drawer))
  1613. (push (org-export-footnote-first-reference-p f info nil t)
  1614. result))
  1615. ""))
  1616. (drawer . (lambda (d c i) c))
  1617. (footnote-definition . (lambda (d c i) c))
  1618. (section . (lambda (s c i) c))
  1619. (paragraph . (lambda (p c i) c))))
  1620. nil nil nil '(:with-footnotes t))
  1621. (nreverse result))))))
  1622. (ert-deftest test-org-export/get-footnote-number ()
  1623. "Test `org-export-get-footnote-number' specifications."
  1624. (should
  1625. (equal '(1 2 1)
  1626. (org-test-with-parsed-data
  1627. "Text[fn:1][fn:2][fn:1]\n\n[fn:1] Def\n[fn:2] Def"
  1628. (org-element-map tree 'footnote-reference
  1629. (lambda (ref) (org-export-get-footnote-number ref info))
  1630. info))))
  1631. ;; Anonymous footnotes all get a new number.
  1632. (should
  1633. (equal '(1 2)
  1634. (org-test-with-parsed-data
  1635. "Text[fn::anon1][fn::anon2]"
  1636. (org-element-map tree 'footnote-reference
  1637. (lambda (ref) (org-export-get-footnote-number ref info))
  1638. info))))
  1639. ;; Test nested footnotes order.
  1640. (should
  1641. (equal
  1642. '((1 . "fn:1") (2 . "fn:2") (3 . "fn:3") (3 . "fn:3") (4))
  1643. (org-test-with-parsed-data
  1644. "Text[fn:1:A[fn:2]] [fn:3].\n\n[fn:2] B [fn:3] [fn::D].\n\n[fn:3] C."
  1645. (org-element-map tree 'footnote-reference
  1646. (lambda (ref)
  1647. (cons (org-export-get-footnote-number ref info)
  1648. (org-element-property :label ref)))
  1649. info))))
  1650. ;; Limit number to provided DATA, when non-nil.
  1651. (should
  1652. (equal
  1653. '(1)
  1654. (org-test-with-parsed-data
  1655. "Text[fn:1]\n* H\nText[fn:2]\n\n[fn:1] D1\n[fn:2] D2"
  1656. (org-element-map tree 'footnote-reference
  1657. (lambda (ref)
  1658. (org-export-get-footnote-number
  1659. ref info (org-element-map tree 'headline #'identity info t)))
  1660. info))))
  1661. (should
  1662. (equal
  1663. '(1 2)
  1664. (org-test-with-parsed-data
  1665. "Text[fn:1]\n* H\nText[fn:2]\n\n[fn:1] D1\n[fn:2]"
  1666. (org-element-map tree 'footnote-reference
  1667. (lambda (ref) (org-export-get-footnote-number ref info))
  1668. info))))
  1669. ;; With a non-nil BODY-FIRST optional argument, first check body,
  1670. ;; then footnote definitions.
  1671. (should
  1672. (equal
  1673. '(("fn:1" . 1) ("fn:2" . 2) ("fn:3" . 3) ("fn:3" . 3))
  1674. (org-test-with-parsed-data
  1675. "Text[fn:1][fn:2][fn:3]\n\n[fn:1] Def[fn:3]\n[fn:2] Def\n[fn:3] Def"
  1676. (org-element-map tree 'footnote-reference
  1677. (lambda (ref)
  1678. (cons (org-element-property :label ref)
  1679. (org-export-get-footnote-number ref info nil t)))
  1680. info))))
  1681. (should
  1682. (equal
  1683. '(("fn:1" . 1) ("fn:2" . 3) ("fn:3" . 2) ("fn:3" . 2))
  1684. (org-test-with-parsed-data
  1685. "Text[fn:1][fn:2][fn:3]\n\n[fn:1] Def[fn:3]\n[fn:2] Def\n[fn:3] Def"
  1686. (org-element-map tree 'footnote-reference
  1687. (lambda (ref)
  1688. (cons (org-element-property :label ref)
  1689. (org-export-get-footnote-number ref info nil)))
  1690. info)))))
  1691. (ert-deftest test-org-export/collect-footnote-definitions ()
  1692. "Test `org-export-collect-footnote-definitions' specifications."
  1693. (should
  1694. (= 4
  1695. (org-test-with-parsed-data "Text[fn:1:A[fn:2]] [fn:3].
  1696. \[fn:2] B [fn:3] [fn::D].
  1697. \[fn:3] C."
  1698. (length (org-export-collect-footnote-definitions info)))))
  1699. ;; Limit number to provided DATA, when non-nil.
  1700. (should
  1701. (equal
  1702. '((1 "fn:2"))
  1703. (org-test-with-parsed-data
  1704. "Text[fn:1]\n* H\nText[fn:2]\n\n[fn:1] D1\n[fn:2] D2"
  1705. (mapcar #'butlast
  1706. (org-export-collect-footnote-definitions
  1707. info (org-element-map tree 'headline #'identity info t))))))
  1708. (should
  1709. (equal
  1710. '((1 "fn:1") (2 "fn:2"))
  1711. (org-test-with-parsed-data
  1712. "Text[fn:1]\n* H\nText[fn:2]\n\n[fn:1] D1\n[fn:2] D2"
  1713. (mapcar #'butlast (org-export-collect-footnote-definitions info)))))
  1714. ;; With optional argument BODY-FIRST, first check body, then
  1715. ;; footnote definitions.
  1716. (should
  1717. (equal '("fn:1" "fn:3" "fn:2" nil)
  1718. (org-test-with-parsed-data "Text[fn:1:A[fn:2]] [fn:3].
  1719. \[fn:2] B [fn:3] [fn::D].
  1720. \[fn:3] C."
  1721. (mapcar (lambda (e) (nth 1 e))
  1722. (org-export-collect-footnote-definitions info nil t)))))
  1723. (should-not
  1724. (equal '("fn:1" "fn:3" "fn:2" nil)
  1725. (org-test-with-parsed-data "Text[fn:1:A[fn:2]] [fn:3].
  1726. \[fn:2] B [fn:3] [fn::D].
  1727. \[fn:3] C."
  1728. (mapcar (lambda (e) (nth 1 e))
  1729. (org-export-collect-footnote-definitions info))))))
  1730. (ert-deftest test-org-export/footnotes ()
  1731. "Miscellaneous tests on footnotes."
  1732. (let ((org-footnote-section nil)
  1733. (org-export-with-footnotes t))
  1734. ;; Read every type of footnote.
  1735. (should
  1736. (equal
  1737. '((1 . "A\n") (2 . "B") (3 . "C") (4 . "D"))
  1738. (org-test-with-parsed-data
  1739. "Text[fn:1] [1] [fn:label:C] [fn::D]\n\n[fn:1] A\n\n[1] B"
  1740. (org-element-map tree 'footnote-reference
  1741. (lambda (ref)
  1742. (let ((def (org-export-get-footnote-definition ref info)))
  1743. (cons (org-export-get-footnote-number ref info)
  1744. (if (eq (org-element-property :type ref) 'inline) (car def)
  1745. (car (org-element-contents
  1746. (car (org-element-contents def))))))))
  1747. info))))
  1748. ;; Test nested footnote in invisible definitions.
  1749. (org-test-with-temp-text "Text[1]\n\n[1] B [2]\n\n[2] C."
  1750. ;; Hide definitions.
  1751. (narrow-to-region (point) (point-at-eol))
  1752. (let* ((tree (org-element-parse-buffer))
  1753. (info (org-combine-plists
  1754. `(:parse-tree ,tree)
  1755. (org-export-collect-tree-properties
  1756. tree (org-export-get-environment)))))
  1757. ;; Both footnotes should be seen.
  1758. (should
  1759. (= (length (org-export-collect-footnote-definitions info)) 2))))
  1760. ;; Test export of footnotes defined outside parsing scope.
  1761. (should
  1762. (equal
  1763. "ParagraphOut of scope\n"
  1764. (org-test-with-temp-text "[fn:1] Out of scope
  1765. * Title
  1766. Paragraph[fn:1]"
  1767. (let ((backend (org-test-default-backend)))
  1768. (setf (org-export-backend-transcoders backend)
  1769. (cons (cons 'footnote-reference
  1770. (lambda (fn contents info)
  1771. (org-element-interpret-data
  1772. (org-export-get-footnote-definition fn info))))
  1773. (org-export-backend-transcoders backend)))
  1774. (forward-line)
  1775. (org-export-as backend 'subtree)))))
  1776. ;; Footnotes without a definition should throw an error.
  1777. (should-error
  1778. (org-test-with-parsed-data "Text[fn:1]"
  1779. (org-export-get-footnote-definition
  1780. (org-element-map tree 'footnote-reference 'identity info t) info)))
  1781. ;; Footnote section should be ignored in TOC and in headlines
  1782. ;; numbering.
  1783. (should
  1784. (= 1 (let ((org-footnote-section "Footnotes"))
  1785. (length (org-test-with-parsed-data "* H1\n* Footnotes\n"
  1786. (org-export-collect-headlines info))))))
  1787. (should
  1788. (equal '(2)
  1789. (let ((org-footnote-section "Footnotes"))
  1790. (org-test-with-parsed-data "* H1\n* Footnotes\n* H2"
  1791. (org-element-map tree 'headline
  1792. (lambda (hl)
  1793. (when (equal (org-element-property :raw-value hl) "H2")
  1794. (org-export-get-headline-number hl info)))
  1795. info t)))))))
  1796. ;;; Headlines and Inlinetasks
  1797. (ert-deftest test-org-export/get-relative-level ()
  1798. "Test `org-export-get-relative-level' specifications."
  1799. ;; Standard test.
  1800. (should
  1801. (equal '(1 2)
  1802. (let ((org-odd-levels-only nil))
  1803. (org-test-with-parsed-data "* Headline 1\n** Headline 2"
  1804. (org-element-map tree 'headline
  1805. (lambda (h) (org-export-get-relative-level h info))
  1806. info)))))
  1807. ;; Missing levels
  1808. (should
  1809. (equal '(1 3)
  1810. (let ((org-odd-levels-only nil))
  1811. (org-test-with-parsed-data "** Headline 1\n**** Headline 2"
  1812. (org-element-map tree 'headline
  1813. (lambda (h) (org-export-get-relative-level h info))
  1814. info))))))
  1815. (ert-deftest test-org-export/low-level-p ()
  1816. "Test `org-export-low-level-p' specifications."
  1817. (should
  1818. (equal
  1819. '(no yes)
  1820. (let ((org-odd-levels-only nil))
  1821. (org-test-with-parsed-data "* Headline 1\n** Headline 2"
  1822. (org-element-map tree 'headline
  1823. (lambda (h) (if (org-export-low-level-p h info) 'yes 'no))
  1824. (plist-put info :headline-levels 1)))))))
  1825. (ert-deftest test-org-export/get-headline-number ()
  1826. "Test `org-export-get-headline-number' specifications."
  1827. ;; Standard test.
  1828. (should
  1829. (equal
  1830. '((1) (1 1))
  1831. (let ((org-odd-levels-only nil))
  1832. (org-test-with-parsed-data "* Headline 1\n** Headline 2"
  1833. (org-element-map tree 'headline
  1834. (lambda (h) (org-export-get-headline-number h info))
  1835. info)))))
  1836. ;; Missing levels are replaced with 0.
  1837. (should
  1838. (equal
  1839. '((1) (1 0 1))
  1840. (let ((org-odd-levels-only nil))
  1841. (org-test-with-parsed-data "* Headline 1\n*** Headline 2"
  1842. (org-element-map tree 'headline
  1843. (lambda (h) (org-export-get-headline-number h info))
  1844. info))))))
  1845. (ert-deftest test-org-export/numbered-headline-p ()
  1846. "Test `org-export-numbered-headline-p' specifications."
  1847. ;; If `:section-numbers' is nil, never number headlines.
  1848. (should-not
  1849. (org-test-with-parsed-data "* Headline"
  1850. (org-element-map tree 'headline
  1851. (lambda (h) (org-export-numbered-headline-p h info))
  1852. (plist-put info :section-numbers nil))))
  1853. ;; If `:section-numbers' is a number, only number headlines with
  1854. ;; a level greater that it.
  1855. (should
  1856. (equal
  1857. '(yes no)
  1858. (org-test-with-parsed-data "* Headline 1\n** Headline 2"
  1859. (org-element-map tree 'headline
  1860. (lambda (h) (if (org-export-numbered-headline-p h info) 'yes 'no))
  1861. (plist-put info :section-numbers 1)))))
  1862. ;; Otherwise, headlines are always numbered.
  1863. (should
  1864. (org-test-with-parsed-data "* Headline"
  1865. (org-element-map tree 'headline
  1866. (lambda (h) (org-export-numbered-headline-p h info))
  1867. (plist-put info :section-numbers t)))))
  1868. (ert-deftest test-org-export/org-export-get-headline-id ()
  1869. "Test `org-export-get-headline-id' specifications."
  1870. ;; Numbered headlines have IDs akin to "sec-N".
  1871. (should
  1872. (equal "sec-1"
  1873. (org-test-with-parsed-data "* H"
  1874. (org-export-get-headline-id
  1875. (org-element-map tree 'headline #'identity info t)
  1876. info))))
  1877. ;; The ID of numbered headlines reflect the hierarchy.
  1878. (should
  1879. (equal "sec-1-1"
  1880. (org-test-with-parsed-data "* H1\n** H2"
  1881. (org-export-get-headline-id
  1882. (org-element-map tree 'headline
  1883. (lambda (h)
  1884. (and (equal "H2" (org-element-property :raw-value h)) h))
  1885. info t)
  1886. info))))
  1887. ;; Unnumbered headlines have IDs akin to "unnumbered-N".
  1888. (should
  1889. (equal "unnumbered-1"
  1890. (org-test-with-parsed-data
  1891. "* H\n:PROPERTIES:\n:UNNUMBERED: t\n:END:"
  1892. (org-export-get-headline-id
  1893. (org-element-map tree 'headline #'identity info t)
  1894. info))))
  1895. ;; The ID of Unnumbered headlines do not reflect the hierarchy.
  1896. (should
  1897. (equal "unnumbered-2"
  1898. (org-test-with-parsed-data
  1899. "* H1\n:PROPERTIES:\n:UNNUMBERED: t\n:END:\n** H2"
  1900. (org-export-get-headline-id
  1901. (org-element-map tree 'headline
  1902. (lambda (h)
  1903. (and (equal "H2" (org-element-property :raw-value h)) h))
  1904. info t)
  1905. info))))
  1906. ;; When #+OPTIONS: num:nil all headlines are unnumbered.
  1907. (should
  1908. (equal "unnumbered-1"
  1909. (org-test-with-parsed-data "* H\n#+OPTIONS: num:nil"
  1910. (org-export-get-headline-id
  1911. (org-element-map tree 'headline 'identity info t)
  1912. info))))
  1913. ;; UNNUMBERED ignores inheritance. Any non-nil value among
  1914. ;; ancestors disables numbering.
  1915. (should
  1916. (org-test-with-parsed-data
  1917. "* H
  1918. :PROPERTIES:
  1919. :UNNUMBERED: t
  1920. :END:
  1921. ** H2
  1922. :PROPERTIES:
  1923. :UNNUMBERED: nil
  1924. :END:
  1925. *** H3"
  1926. (org-every
  1927. (lambda (h) (not (org-export-numbered-headline-p h info)))
  1928. (org-element-map tree 'headline #'identity info)))))
  1929. (ert-deftest test-org-export/number-to-roman ()
  1930. "Test `org-export-number-to-roman' specifications."
  1931. ;; If number is negative, return it as a string.
  1932. (should (equal (org-export-number-to-roman -1) "-1"))
  1933. ;; Otherwise, return it as a roman number.
  1934. (should (equal (org-export-number-to-roman 1449) "MCDXLIX")))
  1935. (ert-deftest test-org-export/get-optional-title ()
  1936. "Test `org-export-get-alt-title' specifications."
  1937. ;; If ALT_TITLE property is defined, use it.
  1938. (should
  1939. (equal '("opt")
  1940. (org-test-with-parsed-data
  1941. "* Headline\n:PROPERTIES:\n:ALT_TITLE: opt\n:END:"
  1942. (org-export-get-alt-title
  1943. (org-element-map tree 'headline 'identity info t)
  1944. info))))
  1945. ;; Otherwise, fall-back to regular title.
  1946. (should
  1947. (equal '("Headline")
  1948. (org-test-with-parsed-data "* Headline"
  1949. (org-export-get-alt-title
  1950. (org-element-map tree 'headline 'identity info t)
  1951. info)))))
  1952. (ert-deftest test-org-export/get-tags ()
  1953. "Test `org-export-get-tags' specifications."
  1954. (let ((org-export-exclude-tags '("noexport"))
  1955. (org-export-select-tags '("export")))
  1956. ;; Standard test: tags which are not a select tag, an exclude tag,
  1957. ;; or specified as optional argument shouldn't be ignored.
  1958. (should
  1959. (org-test-with-parsed-data "* Headline :tag:"
  1960. (org-export-get-tags (org-element-map tree 'headline 'identity info t)
  1961. info)))
  1962. ;; Exclude tags are removed.
  1963. (should-not
  1964. (org-test-with-parsed-data "* Headline :noexport:"
  1965. (org-export-get-tags (org-element-map tree 'headline 'identity info t)
  1966. info)))
  1967. ;; Select tags are removed.
  1968. (should-not
  1969. (org-test-with-parsed-data "* Headline :export:"
  1970. (org-export-get-tags (org-element-map tree 'headline 'identity info t)
  1971. info)))
  1972. (should
  1973. (equal
  1974. '("tag")
  1975. (org-test-with-parsed-data "* Headline :tag:export:"
  1976. (org-export-get-tags (org-element-map tree 'headline 'identity info t)
  1977. info))))
  1978. ;; Tags provided in the optional argument are also ignored.
  1979. (should-not
  1980. (org-test-with-parsed-data "* Headline :ignore:"
  1981. (org-export-get-tags (org-element-map tree 'headline 'identity info t)
  1982. info '("ignore"))))
  1983. ;; Allow tag inheritance.
  1984. (should
  1985. (equal
  1986. '(("tag") ("tag"))
  1987. (org-test-with-parsed-data "* Headline :tag:\n** Sub-heading"
  1988. (org-element-map tree 'headline
  1989. (lambda (hl) (org-export-get-tags hl info nil t)) info))))
  1990. ;; Tag inheritance checks FILETAGS keywords.
  1991. (should
  1992. (equal
  1993. '(("a" "b" "tag"))
  1994. (org-test-with-parsed-data "#+FILETAGS: :a:b:\n* Headline :tag:"
  1995. (org-element-map tree 'headline
  1996. (lambda (hl) (org-export-get-tags hl info nil t)) info))))))
  1997. (ert-deftest test-org-export/get-node-property ()
  1998. "Test`org-export-get-node-property' specifications."
  1999. ;; Standard test.
  2000. (should
  2001. (equal "value"
  2002. (org-test-with-parsed-data "* Headline
  2003. :PROPERTIES:
  2004. :prop: value
  2005. :END:"
  2006. (org-export-get-node-property
  2007. :PROP (org-element-map tree 'headline 'identity nil t)))))
  2008. ;; Test inheritance.
  2009. (should
  2010. (equal "value"
  2011. (org-test-with-parsed-data "* Parent
  2012. :PROPERTIES:
  2013. :prop: value
  2014. :END:
  2015. ** Headline
  2016. Paragraph"
  2017. (org-export-get-node-property
  2018. :PROP (org-element-map tree 'paragraph 'identity nil t) t))))
  2019. ;; Cannot return a value before the first headline.
  2020. (should-not
  2021. (org-test-with-parsed-data "Paragraph
  2022. * Headline
  2023. :PROPERTIES:
  2024. :prop: value
  2025. :END:"
  2026. (org-export-get-node-property
  2027. :PROP (org-element-map tree 'paragraph 'identity nil t)))))
  2028. (ert-deftest test-org-export/get-category ()
  2029. "Test `org-export-get-category' specifications."
  2030. ;; Standard test.
  2031. (should
  2032. (equal "value"
  2033. (org-test-with-parsed-data "* Headline
  2034. :PROPERTIES:
  2035. :CATEGORY: value
  2036. :END:"
  2037. (org-export-get-category
  2038. (org-element-map tree 'headline 'identity nil t) info))))
  2039. ;; Test inheritance from a parent headline.
  2040. (should
  2041. (equal '("value" "value")
  2042. (org-test-with-parsed-data "* Headline1
  2043. :PROPERTIES:
  2044. :CATEGORY: value
  2045. :END:
  2046. ** Headline2"
  2047. (org-element-map tree 'headline
  2048. (lambda (hl) (org-export-get-category hl info)) info))))
  2049. ;; Test inheritance from #+CATEGORY keyword
  2050. (should
  2051. (equal "value"
  2052. (org-test-with-parsed-data "#+CATEGORY: value
  2053. * Headline"
  2054. (org-export-get-category
  2055. (org-element-map tree 'headline 'identity nil t) info))))
  2056. ;; Test inheritance from file name.
  2057. (should
  2058. (equal "test"
  2059. (org-test-with-parsed-data "* Headline"
  2060. (let ((info (plist-put info :input-file "~/test.org")))
  2061. (org-export-get-category
  2062. (org-element-map tree 'headline 'identity nil t) info)))))
  2063. ;; Fall-back value.
  2064. (should
  2065. (equal "???"
  2066. (org-test-with-parsed-data "* Headline"
  2067. (org-export-get-category
  2068. (org-element-map tree 'headline 'identity nil t) info)))))
  2069. (ert-deftest test-org-export/first-sibling-p ()
  2070. "Test `org-export-first-sibling-p' specifications."
  2071. ;; Standard test.
  2072. (should
  2073. (equal
  2074. '(yes yes no)
  2075. (org-test-with-parsed-data "* H\n** H 2\n** H 3"
  2076. (org-element-map tree 'headline
  2077. (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
  2078. info))))
  2079. (should
  2080. (equal '(yes no)
  2081. (org-test-with-parsed-data "- item\n\n para"
  2082. (org-element-map tree 'paragraph
  2083. (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
  2084. info))))
  2085. ;; Ignore sections for headlines.
  2086. (should
  2087. (equal '(yes yes)
  2088. (org-test-with-parsed-data "* H\nSection\n** H 2"
  2089. (org-element-map tree 'headline
  2090. (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
  2091. info))))
  2092. ;; Ignore headlines not exported.
  2093. (should
  2094. (equal
  2095. '(yes)
  2096. (let ((org-export-exclude-tags '("ignore")))
  2097. (org-test-with-parsed-data "* Headline :ignore:\n* Headline 2"
  2098. (org-element-map tree 'headline
  2099. (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
  2100. info))))))
  2101. (ert-deftest test-org-export/last-sibling-p ()
  2102. "Test `org-export-last-sibling-p' specifications."
  2103. ;; Standard test.
  2104. (should
  2105. (equal
  2106. '(yes no yes)
  2107. (org-test-with-parsed-data "* Headline\n** Headline 2\n** Headline 3"
  2108. (org-element-map tree 'headline
  2109. (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
  2110. info))))
  2111. (should
  2112. (equal '(no yes)
  2113. (org-test-with-parsed-data "- item\n\n para"
  2114. (org-element-map tree 'paragraph
  2115. (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
  2116. info))))
  2117. ;; Ignore headlines not exported.
  2118. (should
  2119. (equal
  2120. '(yes)
  2121. (let ((org-export-exclude-tags '("ignore")))
  2122. (org-test-with-parsed-data "* Headline\n* Headline 2 :ignore:"
  2123. (org-element-map tree 'headline
  2124. (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
  2125. info))))))
  2126. (ert-deftest test-org-export/handle-inlinetasks ()
  2127. "Test inlinetask export."
  2128. ;; Inlinetask with an exclude tag.
  2129. (when (featurep 'org-inlinetask)
  2130. (should
  2131. (equal
  2132. ""
  2133. (let ((org-inlinetask-min-level 3)
  2134. org-export-filter-body-functions
  2135. org-export-filter-final-output-functions)
  2136. (org-test-with-temp-text "*** Inlinetask :noexp:\nContents\n*** end"
  2137. (org-export-as (org-test-default-backend)
  2138. nil nil nil '(:exclude-tags ("noexp")))))))
  2139. ;; Inlinetask with an include tag.
  2140. (should
  2141. (equal
  2142. "* H2\n*** Inline :exp:\n"
  2143. (let ((org-inlinetask-min-level 3)
  2144. (org-tags-column 0))
  2145. (org-test-with-temp-text "* H1\n* H2\n*** Inline :exp:"
  2146. (org-export-as (org-test-default-backend)
  2147. nil nil nil '(:select-tags ("exp")))))))
  2148. ;; Ignore inlinetask with a TODO keyword and tasks excluded.
  2149. (should
  2150. (equal ""
  2151. (let ((org-todo-keywords '((sequence "TODO" "DONE")))
  2152. (org-inlinetask-min-level 3)
  2153. org-export-filter-body-functions
  2154. org-export-filter-final-output-functions)
  2155. (org-test-with-temp-text "*** TODO Inline"
  2156. (org-export-as (org-test-default-backend)
  2157. nil nil nil '(:with-tasks nil))))))))
  2158. ;;; Keywords
  2159. (ert-deftest test-org-export/get-date ()
  2160. "Test `org-export-get-date' specifications."
  2161. ;; Return a properly formatted string when
  2162. ;; `org-export-date-timestamp-format' is non-nil and DATE keyword
  2163. ;; consists in a single timestamp.
  2164. (should
  2165. (equal "29 03 2012"
  2166. (let ((org-export-date-timestamp-format "%d %m %Y"))
  2167. (org-test-with-parsed-data "#+DATE: <2012-03-29 Thu>"
  2168. (org-export-get-date info)))))
  2169. ;; Return a secondary string otherwise.
  2170. (should-not
  2171. (stringp
  2172. (let ((org-export-date-timestamp-format nil))
  2173. (org-test-with-parsed-data "#+DATE: <2012-03-29 Thu>"
  2174. (org-export-get-date info)))))
  2175. (should
  2176. (equal '("Date")
  2177. (org-test-with-parsed-data "#+DATE: Date"
  2178. (org-export-get-date info))))
  2179. ;; Optional argument has precedence over
  2180. ;; `org-export-date-timestamp-format'.
  2181. (should
  2182. (equal "29 03"
  2183. (let ((org-export-date-timestamp-format "%d %m %Y"))
  2184. (org-test-with-parsed-data "#+DATE: <2012-03-29 Thu>"
  2185. (org-export-get-date info "%d %m"))))))
  2186. ;;; Links
  2187. (ert-deftest test-org-export/custom-protocol-maybe ()
  2188. "Test `org-export-custom-protocol-maybe' specifications."
  2189. (should
  2190. (string-match
  2191. "success"
  2192. (let ((org-link-types (copy-sequence org-link-types)))
  2193. (org-add-link-type "foo" nil (lambda (p d f) "success"))
  2194. (org-export-string-as
  2195. "[[foo:path]]"
  2196. (org-export-create-backend
  2197. :name 'test
  2198. :transcoders
  2199. '((section . (lambda (s c i) c))
  2200. (paragraph . (lambda (p c i) c))
  2201. (link . (lambda (l c i)
  2202. (or (org-export-custom-protocol-maybe l c 'test)
  2203. "failure")))))))))
  2204. (should-not
  2205. (string-match
  2206. "success"
  2207. (let ((org-link-types (copy-sequence org-link-types)))
  2208. (org-add-link-type
  2209. "foo" nil (lambda (p d f) (and (eq f 'test) "success")))
  2210. (org-export-string-as
  2211. "[[foo:path]]"
  2212. (org-export-create-backend
  2213. :name 'no-test
  2214. :transcoders
  2215. '((section . (lambda (s c i) c))
  2216. (paragraph . (lambda (p c i) c))
  2217. (link . (lambda (l c i)
  2218. (or (org-export-custom-protocol-maybe l c 'no-test)
  2219. "failure")))))))))
  2220. ;; Ignore anonymous back-ends.
  2221. (should-not
  2222. (string-match
  2223. "success"
  2224. (let ((org-link-types (copy-sequence org-link-types)))
  2225. (org-add-link-type
  2226. "foo" nil (lambda (p d f) (and (eq f 'test) "success")))
  2227. (org-export-string-as
  2228. "[[foo:path]]"
  2229. (org-export-create-backend
  2230. :transcoders
  2231. '((section . (lambda (s c i) c))
  2232. (paragraph . (lambda (p c i) c))
  2233. (link . (lambda (l c i)
  2234. (or (org-export-custom-protocol-maybe l c nil)
  2235. "failure"))))))))))
  2236. (ert-deftest test-org-export/get-coderef-format ()
  2237. "Test `org-export-get-coderef-format' specifications."
  2238. ;; A link without description returns "%s"
  2239. (should (equal (org-export-get-coderef-format "(ref:line)" nil)
  2240. "%s"))
  2241. ;; Return "%s" when path is matched within description.
  2242. (should (equal (org-export-get-coderef-format "path" "desc (path)")
  2243. "desc %s"))
  2244. ;; Otherwise return description.
  2245. (should (equal (org-export-get-coderef-format "path" "desc")
  2246. "desc")))
  2247. (ert-deftest test-org-export/inline-image-p ()
  2248. "Test `org-export-inline-image-p' specifications."
  2249. (should
  2250. (org-export-inline-image-p
  2251. (org-test-with-temp-text "[[#id]]"
  2252. (org-element-map (org-element-parse-buffer) 'link 'identity nil t))
  2253. '(("custom-id" . "id")))))
  2254. (ert-deftest test-org-export/fuzzy-link ()
  2255. "Test fuzzy links specifications."
  2256. ;; Link to an headline should return headline's number.
  2257. (org-test-with-parsed-data
  2258. "Paragraph.\n* Head1\n* Head2\n* Head3\n[[Head2]]"
  2259. (should
  2260. ;; Note: Headline's number is in fact a list of numbers.
  2261. (equal '(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 an item should return item's number.
  2267. (org-test-with-parsed-data
  2268. "- Item1\n - Item11\n - <<test>>Item12\n- Item2\n\n\n[[test]]"
  2269. (should
  2270. ;; Note: Item's number is in fact a list of numbers.
  2271. (equal '(1 2)
  2272. (org-element-map tree 'link
  2273. (lambda (link)
  2274. (org-export-get-ordinal
  2275. (org-export-resolve-fuzzy-link link info) info)) info t))))
  2276. ;; Link to a target in a footnote should return footnote's number.
  2277. (org-test-with-parsed-data "
  2278. Paragraph[1][2][fn:lbl3:C<<target>>][[test]][[target]]\n[1] A\n\n[2] <<test>>B"
  2279. (should
  2280. (equal '(2 3)
  2281. (org-element-map tree 'link
  2282. (lambda (link)
  2283. (org-export-get-ordinal
  2284. (org-export-resolve-fuzzy-link link info) info)) info))))
  2285. ;; Link to a named element should return sequence number of that
  2286. ;; element.
  2287. (org-test-with-parsed-data
  2288. "#+NAME: tbl1\n|1|2|\n#+NAME: tbl2\n|3|4|\n#+NAME: tbl3\n|5|6|\n[[tbl2]]"
  2289. (should
  2290. (= 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. ;; Link to a target not within an item, a table, a footnote
  2296. ;; reference or definition should return section number.
  2297. (org-test-with-parsed-data
  2298. "* Head1\n* Head2\nParagraph<<target>>\n* Head3\n[[target]]"
  2299. (should
  2300. (equal '(2)
  2301. (org-element-map tree 'link
  2302. (lambda (link)
  2303. (org-export-get-ordinal
  2304. (org-export-resolve-fuzzy-link link info) info)) info t))))
  2305. ;; Space are not significant when matching a fuzzy link.
  2306. (should
  2307. (org-test-with-parsed-data "* Head 1\n[[Head\n 1]]"
  2308. (org-element-map tree 'link
  2309. (lambda (link) (org-export-resolve-fuzzy-link link info))
  2310. info t)))
  2311. ;; Statistics cookies are ignored for headline match.
  2312. (should
  2313. (org-test-with-parsed-data "* Head [0/0]\n[[Head]]"
  2314. (org-element-map tree 'link
  2315. (lambda (link) (org-export-resolve-fuzzy-link link info))
  2316. info t)))
  2317. (should
  2318. (org-test-with-parsed-data "* Head [100%]\n[[Head]]"
  2319. (org-element-map tree 'link
  2320. (lambda (link) (org-export-resolve-fuzzy-link link info))
  2321. info t)))
  2322. ;; Headline match is position dependent.
  2323. (should-not
  2324. (apply
  2325. 'eq
  2326. (org-test-with-parsed-data "* H1\n[[*H1]]\n* H1\n[[*H1]]"
  2327. (org-element-map tree 'link
  2328. (lambda (link) (org-export-resolve-fuzzy-link link info)) info)))))
  2329. (ert-deftest test-org-export/resolve-coderef ()
  2330. "Test `org-export-resolve-coderef' specifications."
  2331. (let ((org-coderef-label-format "(ref:%s)"))
  2332. ;; A link to a "-n -k -r" block returns line number.
  2333. (should
  2334. (= 1
  2335. (org-test-with-parsed-data
  2336. "#+BEGIN_EXAMPLE -n -k -r\nText (ref:coderef)\n#+END_EXAMPLE"
  2337. (org-export-resolve-coderef "coderef" info))))
  2338. (should
  2339. (= 1
  2340. (org-test-with-parsed-data
  2341. "#+BEGIN_SRC emacs-lisp -n -k -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  2342. (org-export-resolve-coderef "coderef" info))))
  2343. ;; A link to a "-n -r" block returns line number.
  2344. (should
  2345. (= 1
  2346. (org-test-with-parsed-data
  2347. "#+BEGIN_EXAMPLE -n -r\nText (ref:coderef)\n#+END_EXAMPLE"
  2348. (org-export-resolve-coderef "coderef" info))))
  2349. (should
  2350. (= 1
  2351. (org-test-with-parsed-data
  2352. "#+BEGIN_SRC emacs-lisp -n -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  2353. (org-export-resolve-coderef "coderef" info))))
  2354. ;; A link to a "-n" block returns coderef.
  2355. (should
  2356. (equal "coderef"
  2357. (org-test-with-parsed-data
  2358. "#+BEGIN_SRC emacs-lisp -n\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  2359. (org-export-resolve-coderef "coderef" info))))
  2360. (should
  2361. (equal "coderef"
  2362. (org-test-with-parsed-data
  2363. "#+BEGIN_EXAMPLE -n\nText (ref:coderef)\n#+END_EXAMPLE"
  2364. (org-export-resolve-coderef "coderef" info))))
  2365. ;; A link to a "-r" block returns line number.
  2366. (should
  2367. (= 1
  2368. (org-test-with-parsed-data
  2369. "#+BEGIN_SRC emacs-lisp -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  2370. (org-export-resolve-coderef "coderef" info))))
  2371. (should
  2372. (= 1
  2373. (org-test-with-parsed-data
  2374. "#+BEGIN_EXAMPLE -r\nText (ref:coderef)\n#+END_EXAMPLE"
  2375. (org-export-resolve-coderef "coderef" info))))
  2376. ;; A link to a block without a switch returns coderef.
  2377. (should
  2378. (equal "coderef"
  2379. (org-test-with-parsed-data
  2380. "#+BEGIN_SRC emacs-lisp\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  2381. (org-export-resolve-coderef "coderef" info))))
  2382. (org-test-with-parsed-data
  2383. "#+BEGIN_EXAMPLE\nText (ref:coderef)\n#+END_EXAMPLE"
  2384. (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
  2385. ;; Correctly handle continued line numbers. A "+n" switch should
  2386. ;; resume numbering from previous block with numbered lines,
  2387. ;; ignoring blocks not numbering lines in the process. A "-n"
  2388. ;; switch resets count.
  2389. (should
  2390. (equal '(2 1)
  2391. (org-test-with-parsed-data "
  2392. #+BEGIN_EXAMPLE -n
  2393. Text.
  2394. #+END_EXAMPLE
  2395. #+BEGIN_SRC emacs-lisp
  2396. \(- 1 1)
  2397. #+END_SRC
  2398. #+BEGIN_SRC emacs-lisp +n -r
  2399. \(+ 1 1) (ref:addition)
  2400. #+END_SRC
  2401. #+BEGIN_EXAMPLE -n -r
  2402. Another text. (ref:text)
  2403. #+END_EXAMPLE"
  2404. (list (org-export-resolve-coderef "addition" info)
  2405. (org-export-resolve-coderef "text" info)))))
  2406. ;; Recognize coderef with user-specified syntax.
  2407. (should
  2408. (equal "text"
  2409. (org-test-with-parsed-data
  2410. "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\nText. [ref:text]\n#+END_EXAMPLE"
  2411. (org-export-resolve-coderef "text" info))))
  2412. ;; Unresolved coderefs throw an error.
  2413. (should-error
  2414. (org-test-with-parsed-data "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  2415. (org-export-resolve-coderef "unknown" info)))))
  2416. (ert-deftest test-org-export/resolve-fuzzy-link ()
  2417. "Test `org-export-resolve-fuzzy-link' specifications."
  2418. ;; Match target objects.
  2419. (should
  2420. (org-test-with-parsed-data "<<target>> [[target]]"
  2421. (org-export-resolve-fuzzy-link
  2422. (org-element-map tree 'link 'identity info t) info)))
  2423. ;; Match named elements.
  2424. (should
  2425. (org-test-with-parsed-data "#+NAME: target\nParagraph\n\n[[target]]"
  2426. (org-export-resolve-fuzzy-link
  2427. (org-element-map tree 'link 'identity info t) info)))
  2428. ;; Match exact headline's name.
  2429. (should
  2430. (org-test-with-parsed-data "* My headline\n[[My headline]]"
  2431. (org-export-resolve-fuzzy-link
  2432. (org-element-map tree 'link 'identity info t) info)))
  2433. ;; Targets objects have priority over named elements and headline
  2434. ;; titles.
  2435. (should
  2436. (eq 'target
  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. ;; Named elements have priority over headline titles.
  2443. (should
  2444. (eq 'paragraph
  2445. (org-test-with-parsed-data
  2446. "* target\n#+NAME: target\nParagraph\n\n[[target]]"
  2447. (org-element-type
  2448. (org-export-resolve-fuzzy-link
  2449. (org-element-map tree 'link 'identity info t) info)))))
  2450. ;; If link's path starts with a "*", only match headline titles,
  2451. ;; though.
  2452. (should
  2453. (eq 'headline
  2454. (org-test-with-parsed-data
  2455. "* target\n#+NAME: target\n<<target>>\n\n[[*target]]"
  2456. (org-element-type
  2457. (org-export-resolve-fuzzy-link
  2458. (org-element-map tree 'link 'identity info t) info)))))
  2459. ;; Error if no match.
  2460. (should-error
  2461. (org-test-with-parsed-data "[[target]]"
  2462. (org-export-resolve-fuzzy-link
  2463. (org-element-map tree 'link 'identity info t) info)))
  2464. ;; Match fuzzy link even when before first headline.
  2465. (should
  2466. (eq 'headline
  2467. (org-test-with-parsed-data "[[hl]]\n* hl"
  2468. (org-element-type
  2469. (org-export-resolve-fuzzy-link
  2470. (org-element-map tree 'link 'identity info t) info))))))
  2471. (ert-deftest test-org-export/resolve-id-link ()
  2472. "Test `org-export-resolve-id-link' specifications."
  2473. ;; Regular test for custom-id link.
  2474. (should
  2475. (equal '("Headline1")
  2476. (org-test-with-parsed-data "* Headline1
  2477. :PROPERTIES:
  2478. :CUSTOM_ID: test
  2479. :END:
  2480. * Headline 2
  2481. \[[#test]]"
  2482. (org-element-property
  2483. :title
  2484. (org-export-resolve-id-link
  2485. (org-element-map tree 'link 'identity info t) info)))))
  2486. ;; Throw an error on failing searches.
  2487. (should-error
  2488. (org-test-with-parsed-data "* Headline1
  2489. :PROPERTIES:
  2490. :CUSTOM_ID: test
  2491. :END:
  2492. * Headline 2
  2493. \[[#no-match]]"
  2494. (org-export-resolve-id-link
  2495. (org-element-map tree 'link 'identity info t) info)))
  2496. ;; Test for internal id target.
  2497. (should
  2498. (equal '("Headline1")
  2499. (org-test-with-parsed-data "* Headline1
  2500. :PROPERTIES:
  2501. :ID: aaaa
  2502. :END:
  2503. * Headline 2
  2504. \[[id:aaaa]]"
  2505. (org-element-property
  2506. :title
  2507. (org-export-resolve-id-link
  2508. (org-element-map tree 'link 'identity info t) info)))))
  2509. ;; Test for external id target.
  2510. (should
  2511. (equal
  2512. "external-file"
  2513. (org-test-with-parsed-data "[[id:aaaa]]"
  2514. (org-export-resolve-id-link
  2515. (org-element-map tree 'link 'identity info t)
  2516. (org-combine-plists info '(:id-alist (("aaaa" . "external-file")))))))))
  2517. (ert-deftest test-org-export/resolve-radio-link ()
  2518. "Test `org-export-resolve-radio-link' specifications."
  2519. ;; Standard test.
  2520. (should
  2521. (org-test-with-temp-text "<<<radio>>> radio"
  2522. (org-update-radio-target-regexp)
  2523. (let* ((tree (org-element-parse-buffer))
  2524. (info `(:parse-tree ,tree)))
  2525. (org-export-resolve-radio-link
  2526. (org-element-map tree 'link 'identity info t)
  2527. info))))
  2528. ;; Radio targets are case-insensitive.
  2529. (should
  2530. (org-test-with-temp-text "<<<RADIO>>> radio"
  2531. (org-update-radio-target-regexp)
  2532. (let* ((tree (org-element-parse-buffer))
  2533. (info `(:parse-tree ,tree)))
  2534. (org-export-resolve-radio-link
  2535. (org-element-map tree 'link 'identity info t)
  2536. info))))
  2537. ;; Radio target with objects.
  2538. (should
  2539. (org-test-with-temp-text "<<<radio \\alpha>>> radio \\alpha"
  2540. (org-update-radio-target-regexp)
  2541. (let* ((tree (org-element-parse-buffer))
  2542. (info `(:parse-tree ,tree)))
  2543. (org-export-resolve-radio-link
  2544. (org-element-map tree 'link 'identity info t)
  2545. info))))
  2546. ;; Radio target with objects at its beginning.
  2547. (should
  2548. (org-test-with-temp-text "<<<\\alpha radio>>> \\alpha radio"
  2549. (org-update-radio-target-regexp)
  2550. (let* ((tree (org-element-parse-buffer))
  2551. (info `(:parse-tree ,tree)))
  2552. (org-export-resolve-radio-link
  2553. (org-element-map tree 'link 'identity info t)
  2554. info))))
  2555. ;; Radio link next to an apostrophe.
  2556. (should
  2557. (org-test-with-temp-text "<<<radio>>> radio's"
  2558. (org-update-radio-target-regexp)
  2559. (let* ((tree (org-element-parse-buffer))
  2560. (info `(:parse-tree ,tree)))
  2561. (org-export-resolve-radio-link
  2562. (org-element-map tree 'link 'identity info t)
  2563. info))))
  2564. ;; Multiple radio targets.
  2565. (should
  2566. (equal '("radio1" "radio2")
  2567. (org-test-with-temp-text "<<<radio1>>> <<<radio2>>> radio1 radio2"
  2568. (org-update-radio-target-regexp)
  2569. (let* ((tree (org-element-parse-buffer))
  2570. (info `(:parse-tree ,tree)))
  2571. (org-element-map tree 'link
  2572. (lambda (link)
  2573. (org-element-property
  2574. :value (org-export-resolve-radio-link link info)))
  2575. info)))))
  2576. ;; Radio target is whitespace insensitive.
  2577. (should
  2578. (org-test-with-temp-text "<<<a radio>>> a\n radio"
  2579. (org-update-radio-target-regexp)
  2580. (let* ((tree (org-element-parse-buffer))
  2581. (info `(:parse-tree ,tree)))
  2582. (org-element-map tree 'link
  2583. (lambda (link) (org-export-resolve-radio-link link info)) info t)))))
  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