test-ox.el 116 KB

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