test-ox.el 118 KB

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