test-ox.el 119 KB

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