test-ox.el 113 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395
  1. ;;; test-ox.el --- Tests for ox.el
  2. ;; Copyright (C) 2012, 2013, 2014 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/footnotes ()
  1487. "Test footnotes specifications."
  1488. (let ((org-footnote-section nil)
  1489. (org-export-with-footnotes t))
  1490. ;; 1. Read every type of footnote.
  1491. (should
  1492. (equal
  1493. '((1 . "A\n") (2 . "B") (3 . "C") (4 . "D"))
  1494. (org-test-with-parsed-data
  1495. "Text[fn:1] [1] [fn:label:C] [fn::D]\n\n[fn:1] A\n\n[1] B"
  1496. (org-element-map tree 'footnote-reference
  1497. (lambda (ref)
  1498. (let ((def (org-export-get-footnote-definition ref info)))
  1499. (cons (org-export-get-footnote-number ref info)
  1500. (if (eq (org-element-property :type ref) 'inline) (car def)
  1501. (car (org-element-contents
  1502. (car (org-element-contents def))))))))
  1503. info))))
  1504. ;; 2. Test nested footnotes order.
  1505. (should
  1506. (equal
  1507. '((1 . "fn:1") (2 . "fn:2") (3 . "fn:3") (4))
  1508. (org-test-with-parsed-data
  1509. "Text[fn:1:A[fn:2]] [fn:3].\n\n[fn:2] B [fn:3] [fn::D].\n\n[fn:3] C."
  1510. (org-element-map tree 'footnote-reference
  1511. (lambda (ref)
  1512. (when (org-export-footnote-first-reference-p ref info)
  1513. (cons (org-export-get-footnote-number ref info)
  1514. (org-element-property :label ref))))
  1515. info))))
  1516. ;; 3. Test nested footnote in invisible definitions.
  1517. (org-test-with-temp-text "Text[1]\n\n[1] B [2]\n\n[2] C."
  1518. ;; Hide definitions.
  1519. (narrow-to-region (point) (point-at-eol))
  1520. (let* ((tree (org-element-parse-buffer))
  1521. (info (org-combine-plists
  1522. `(:parse-tree ,tree)
  1523. (org-export-collect-tree-properties
  1524. tree (org-export-get-environment)))))
  1525. ;; Both footnotes should be seen.
  1526. (should
  1527. (= (length (org-export-collect-footnote-definitions tree info)) 2))))
  1528. ;; 4. Test footnotes definitions collection.
  1529. (should
  1530. (= 4
  1531. (org-test-with-parsed-data "Text[fn:1:A[fn:2]] [fn:3].
  1532. \[fn:2] B [fn:3] [fn::D].
  1533. \[fn:3] C."
  1534. (length (org-export-collect-footnote-definitions tree info)))))
  1535. ;; 5. Test export of footnotes defined outside parsing scope.
  1536. (should
  1537. (equal
  1538. "ParagraphOut of scope\n"
  1539. (org-test-with-temp-text "[fn:1] Out of scope
  1540. * Title
  1541. Paragraph[fn:1]"
  1542. (let ((backend (org-test-default-backend)))
  1543. (setf (org-export-backend-transcoders backend)
  1544. (cons (cons 'footnote-reference
  1545. (lambda (fn contents info)
  1546. (org-element-interpret-data
  1547. (org-export-get-footnote-definition fn info))))
  1548. (org-export-backend-transcoders backend)))
  1549. (forward-line)
  1550. (org-export-as backend 'subtree)))))
  1551. ;; 6. Footnotes without a definition should throw an error.
  1552. (should-error
  1553. (org-test-with-parsed-data "Text[fn:1]"
  1554. (org-export-get-footnote-definition
  1555. (org-element-map tree 'footnote-reference 'identity info t) info)))
  1556. ;; 7. Footnote section should be ignored in TOC and in headlines
  1557. ;; numbering.
  1558. (should
  1559. (= 1 (let ((org-footnote-section "Footnotes"))
  1560. (length (org-test-with-parsed-data "* H1\n* Footnotes\n"
  1561. (org-export-collect-headlines info))))))
  1562. (should
  1563. (equal '(2)
  1564. (let ((org-footnote-section "Footnotes"))
  1565. (org-test-with-parsed-data "* H1\n* Footnotes\n* H2"
  1566. (org-element-map tree 'headline
  1567. (lambda (hl)
  1568. (when (equal (org-element-property :raw-value hl) "H2")
  1569. (org-export-get-headline-number hl info)))
  1570. info t)))))))
  1571. ;;; Headlines and Inlinetasks
  1572. (ert-deftest test-org-export/get-relative-level ()
  1573. "Test `org-export-get-relative-level' specifications."
  1574. ;; Standard test.
  1575. (should
  1576. (equal '(1 2)
  1577. (let ((org-odd-levels-only nil))
  1578. (org-test-with-parsed-data "* Headline 1\n** Headline 2"
  1579. (org-element-map tree 'headline
  1580. (lambda (h) (org-export-get-relative-level h info))
  1581. info)))))
  1582. ;; Missing levels
  1583. (should
  1584. (equal '(1 3)
  1585. (let ((org-odd-levels-only nil))
  1586. (org-test-with-parsed-data "** Headline 1\n**** Headline 2"
  1587. (org-element-map tree 'headline
  1588. (lambda (h) (org-export-get-relative-level h info))
  1589. info))))))
  1590. (ert-deftest test-org-export/low-level-p ()
  1591. "Test `org-export-low-level-p' specifications."
  1592. (should
  1593. (equal
  1594. '(no yes)
  1595. (let ((org-odd-levels-only nil))
  1596. (org-test-with-parsed-data "* Headline 1\n** Headline 2"
  1597. (org-element-map tree 'headline
  1598. (lambda (h) (if (org-export-low-level-p h info) 'yes 'no))
  1599. (plist-put info :headline-levels 1)))))))
  1600. (ert-deftest test-org-export/get-headline-number ()
  1601. "Test `org-export-get-headline-number' specifications."
  1602. ;; Standard test.
  1603. (should
  1604. (equal
  1605. '((1) (1 1))
  1606. (let ((org-odd-levels-only nil))
  1607. (org-test-with-parsed-data "* Headline 1\n** Headline 2"
  1608. (org-element-map tree 'headline
  1609. (lambda (h) (org-export-get-headline-number h info))
  1610. info)))))
  1611. ;; Missing levels are replaced with 0.
  1612. (should
  1613. (equal
  1614. '((1) (1 0 1))
  1615. (let ((org-odd-levels-only nil))
  1616. (org-test-with-parsed-data "* Headline 1\n*** Headline 2"
  1617. (org-element-map tree 'headline
  1618. (lambda (h) (org-export-get-headline-number h info))
  1619. info))))))
  1620. (ert-deftest test-org-export/numbered-headline-p ()
  1621. "Test `org-export-numbered-headline-p' specifications."
  1622. ;; If `:section-numbers' is nil, never number headlines.
  1623. (should-not
  1624. (org-test-with-parsed-data "* Headline"
  1625. (org-element-map tree 'headline
  1626. (lambda (h) (org-export-numbered-headline-p h info))
  1627. (plist-put info :section-numbers nil))))
  1628. ;; If `:section-numbers' is a number, only number headlines with
  1629. ;; a level greater that it.
  1630. (should
  1631. (equal
  1632. '(yes no)
  1633. (org-test-with-parsed-data "* Headline 1\n** Headline 2"
  1634. (org-element-map tree 'headline
  1635. (lambda (h) (if (org-export-numbered-headline-p h info) 'yes 'no))
  1636. (plist-put info :section-numbers 1)))))
  1637. ;; Otherwise, headlines are always numbered.
  1638. (should
  1639. (org-test-with-parsed-data "* Headline"
  1640. (org-element-map tree 'headline
  1641. (lambda (h) (org-export-numbered-headline-p h info))
  1642. (plist-put info :section-numbers t)))))
  1643. (ert-deftest test-org-export/org-export-get-headline-id ()
  1644. "Test `org-export-get-headline-id' specifications."
  1645. ;; Numbered headlines have IDs akin to "sec-N".
  1646. (should
  1647. (equal "sec-1"
  1648. (org-test-with-parsed-data "* H"
  1649. (org-export-get-headline-id
  1650. (org-element-map tree 'headline #'identity info t)
  1651. info))))
  1652. ;; The ID of numbered headlines reflect the hierarchy.
  1653. (should
  1654. (equal "sec-1-1"
  1655. (org-test-with-parsed-data "* H1\n** H2"
  1656. (org-export-get-headline-id
  1657. (org-element-map tree 'headline
  1658. (lambda (h)
  1659. (and (equal "H2" (org-element-property :raw-value h)) h))
  1660. info t)
  1661. info))))
  1662. ;; Unnumbered headlines have IDs akin to "unnumbered-N".
  1663. (should
  1664. (equal "unnumbered-1"
  1665. (org-test-with-parsed-data
  1666. "* H\n:PROPERTIES:\n:UNNUMBERED: t\n:END:"
  1667. (org-export-get-headline-id
  1668. (org-element-map tree 'headline #'identity info t)
  1669. info))))
  1670. ;; The ID of Unnumbered headlines do not reflect the hierarchy.
  1671. (should
  1672. (equal "unnumbered-2"
  1673. (org-test-with-parsed-data
  1674. "* H1\n:PROPERTIES:\n:UNNUMBERED: t\n:END:\n** H2"
  1675. (org-export-get-headline-id
  1676. (org-element-map tree 'headline
  1677. (lambda (h)
  1678. (and (equal "H2" (org-element-property :raw-value h)) h))
  1679. info t)
  1680. info))))
  1681. ;; When #+OPTIONS: num:nil all headlines are unnumbered.
  1682. (should
  1683. (equal "unnumbered-1"
  1684. (org-test-with-parsed-data "* H\n#+OPTIONS: num:nil"
  1685. (org-export-get-headline-id
  1686. (org-element-map tree 'headline 'identity info t)
  1687. info))))
  1688. ;; UNNUMBERED ignores inheritance. Any non-nil value among
  1689. ;; ancestors disables numbering.
  1690. (should
  1691. (org-test-with-parsed-data
  1692. "* H
  1693. :PROPERTIES:
  1694. :UNNUMBERED: t
  1695. :END:
  1696. ** H2
  1697. :PROPERTIES:
  1698. :UNNUMBERED: nil
  1699. :END:
  1700. *** H3"
  1701. (org-every
  1702. (lambda (h) (not (org-export-numbered-headline-p h info)))
  1703. (org-element-map tree 'headline #'identity info)))))
  1704. (ert-deftest test-org-export/number-to-roman ()
  1705. "Test `org-export-number-to-roman' specifications."
  1706. ;; If number is negative, return it as a string.
  1707. (should (equal (org-export-number-to-roman -1) "-1"))
  1708. ;; Otherwise, return it as a roman number.
  1709. (should (equal (org-export-number-to-roman 1449) "MCDXLIX")))
  1710. (ert-deftest test-org-export/get-optional-title ()
  1711. "Test `org-export-get-alt-title' specifications."
  1712. ;; If ALT_TITLE property is defined, use it.
  1713. (should
  1714. (equal '("opt")
  1715. (org-test-with-parsed-data
  1716. "* Headline\n:PROPERTIES:\n:ALT_TITLE: opt\n:END:"
  1717. (org-export-get-alt-title
  1718. (org-element-map tree 'headline 'identity info t)
  1719. info))))
  1720. ;; Otherwise, fall-back to regular title.
  1721. (should
  1722. (equal '("Headline")
  1723. (org-test-with-parsed-data "* Headline"
  1724. (org-export-get-alt-title
  1725. (org-element-map tree 'headline 'identity info t)
  1726. info)))))
  1727. (ert-deftest test-org-export/get-tags ()
  1728. "Test `org-export-get-tags' specifications."
  1729. (let ((org-export-exclude-tags '("noexport"))
  1730. (org-export-select-tags '("export")))
  1731. ;; Standard test: tags which are not a select tag, an exclude tag,
  1732. ;; or specified as optional argument shouldn't be ignored.
  1733. (should
  1734. (org-test-with-parsed-data "* Headline :tag:"
  1735. (org-export-get-tags (org-element-map tree 'headline 'identity info t)
  1736. info)))
  1737. ;; Exclude tags are removed.
  1738. (should-not
  1739. (org-test-with-parsed-data "* Headline :noexport:"
  1740. (org-export-get-tags (org-element-map tree 'headline 'identity info t)
  1741. info)))
  1742. ;; Select tags are removed.
  1743. (should-not
  1744. (org-test-with-parsed-data "* Headline :export:"
  1745. (org-export-get-tags (org-element-map tree 'headline 'identity info t)
  1746. info)))
  1747. (should
  1748. (equal
  1749. '("tag")
  1750. (org-test-with-parsed-data "* Headline :tag:export:"
  1751. (org-export-get-tags (org-element-map tree 'headline 'identity info t)
  1752. info))))
  1753. ;; Tags provided in the optional argument are also ignored.
  1754. (should-not
  1755. (org-test-with-parsed-data "* Headline :ignore:"
  1756. (org-export-get-tags (org-element-map tree 'headline 'identity info t)
  1757. info '("ignore"))))
  1758. ;; Allow tag inheritance.
  1759. (should
  1760. (equal
  1761. '(("tag") ("tag"))
  1762. (org-test-with-parsed-data "* Headline :tag:\n** Sub-heading"
  1763. (org-element-map tree 'headline
  1764. (lambda (hl) (org-export-get-tags hl info nil t)) info))))
  1765. ;; Tag inheritance checks FILETAGS keywords.
  1766. (should
  1767. (equal
  1768. '(("a" "b" "tag"))
  1769. (org-test-with-parsed-data "#+FILETAGS: :a:b:\n* Headline :tag:"
  1770. (org-element-map tree 'headline
  1771. (lambda (hl) (org-export-get-tags hl info nil t)) info))))))
  1772. (ert-deftest test-org-export/get-node-property ()
  1773. "Test`org-export-get-node-property' specifications."
  1774. ;; Standard test.
  1775. (should
  1776. (equal "value"
  1777. (org-test-with-parsed-data "* Headline
  1778. :PROPERTIES:
  1779. :prop: value
  1780. :END:"
  1781. (org-export-get-node-property
  1782. :PROP (org-element-map tree 'headline 'identity nil t)))))
  1783. ;; Test inheritance.
  1784. (should
  1785. (equal "value"
  1786. (org-test-with-parsed-data "* Parent
  1787. :PROPERTIES:
  1788. :prop: value
  1789. :END:
  1790. ** Headline
  1791. Paragraph"
  1792. (org-export-get-node-property
  1793. :PROP (org-element-map tree 'paragraph 'identity nil t) t))))
  1794. ;; Cannot return a value before the first headline.
  1795. (should-not
  1796. (org-test-with-parsed-data "Paragraph
  1797. * Headline
  1798. :PROPERTIES:
  1799. :prop: value
  1800. :END:"
  1801. (org-export-get-node-property
  1802. :PROP (org-element-map tree 'paragraph 'identity nil t)))))
  1803. (ert-deftest test-org-export/get-category ()
  1804. "Test `org-export-get-category' specifications."
  1805. ;; Standard test.
  1806. (should
  1807. (equal "value"
  1808. (org-test-with-parsed-data "* Headline
  1809. :PROPERTIES:
  1810. :CATEGORY: value
  1811. :END:"
  1812. (org-export-get-category
  1813. (org-element-map tree 'headline 'identity nil t) info))))
  1814. ;; Test inheritance from a parent headline.
  1815. (should
  1816. (equal '("value" "value")
  1817. (org-test-with-parsed-data "* Headline1
  1818. :PROPERTIES:
  1819. :CATEGORY: value
  1820. :END:
  1821. ** Headline2"
  1822. (org-element-map tree 'headline
  1823. (lambda (hl) (org-export-get-category hl info)) info))))
  1824. ;; Test inheritance from #+CATEGORY keyword
  1825. (should
  1826. (equal "value"
  1827. (org-test-with-parsed-data "#+CATEGORY: value
  1828. * Headline"
  1829. (org-export-get-category
  1830. (org-element-map tree 'headline 'identity nil t) info))))
  1831. ;; Test inheritance from file name.
  1832. (should
  1833. (equal "test"
  1834. (org-test-with-parsed-data "* Headline"
  1835. (let ((info (plist-put info :input-file "~/test.org")))
  1836. (org-export-get-category
  1837. (org-element-map tree 'headline 'identity nil t) info)))))
  1838. ;; Fall-back value.
  1839. (should
  1840. (equal "???"
  1841. (org-test-with-parsed-data "* Headline"
  1842. (org-export-get-category
  1843. (org-element-map tree 'headline 'identity nil t) info)))))
  1844. (ert-deftest test-org-export/first-sibling-p ()
  1845. "Test `org-export-first-sibling-p' specifications."
  1846. ;; Standard test.
  1847. (should
  1848. (equal
  1849. '(yes yes no)
  1850. (org-test-with-parsed-data "* H\n** H 2\n** H 3"
  1851. (org-element-map tree 'headline
  1852. (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
  1853. info))))
  1854. (should
  1855. (equal '(yes no)
  1856. (org-test-with-parsed-data "- item\n\n para"
  1857. (org-element-map tree 'paragraph
  1858. (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
  1859. info))))
  1860. ;; Ignore sections for headlines.
  1861. (should
  1862. (equal '(yes yes)
  1863. (org-test-with-parsed-data "* H\nSection\n** H 2"
  1864. (org-element-map tree 'headline
  1865. (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
  1866. info))))
  1867. ;; Ignore headlines not exported.
  1868. (should
  1869. (equal
  1870. '(yes)
  1871. (let ((org-export-exclude-tags '("ignore")))
  1872. (org-test-with-parsed-data "* Headline :ignore:\n* Headline 2"
  1873. (org-element-map tree 'headline
  1874. (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
  1875. info))))))
  1876. (ert-deftest test-org-export/last-sibling-p ()
  1877. "Test `org-export-last-sibling-p' specifications."
  1878. ;; Standard test.
  1879. (should
  1880. (equal
  1881. '(yes no yes)
  1882. (org-test-with-parsed-data "* Headline\n** Headline 2\n** Headline 3"
  1883. (org-element-map tree 'headline
  1884. (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
  1885. info))))
  1886. (should
  1887. (equal '(no yes)
  1888. (org-test-with-parsed-data "- item\n\n para"
  1889. (org-element-map tree 'paragraph
  1890. (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
  1891. info))))
  1892. ;; Ignore headlines not exported.
  1893. (should
  1894. (equal
  1895. '(yes)
  1896. (let ((org-export-exclude-tags '("ignore")))
  1897. (org-test-with-parsed-data "* Headline\n* Headline 2 :ignore:"
  1898. (org-element-map tree 'headline
  1899. (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
  1900. info))))))
  1901. (ert-deftest test-org-export/handle-inlinetasks ()
  1902. "Test inlinetask export."
  1903. ;; Inlinetask with an exclude tag.
  1904. (when (featurep 'org-inlinetask)
  1905. (should
  1906. (equal
  1907. ""
  1908. (let ((org-inlinetask-min-level 3)
  1909. org-export-filter-body-functions
  1910. org-export-filter-final-output-functions)
  1911. (org-test-with-temp-text "*** Inlinetask :noexp:\nContents\n*** end"
  1912. (org-export-as (org-test-default-backend)
  1913. nil nil nil '(:exclude-tags ("noexp")))))))
  1914. ;; Inlinetask with an include tag.
  1915. (should
  1916. (equal
  1917. "* H2\n*** Inline :exp:\n"
  1918. (let ((org-inlinetask-min-level 3)
  1919. (org-tags-column 0))
  1920. (org-test-with-temp-text "* H1\n* H2\n*** Inline :exp:"
  1921. (org-export-as (org-test-default-backend)
  1922. nil nil nil '(:select-tags ("exp")))))))
  1923. ;; Ignore inlinetask with a TODO keyword and tasks excluded.
  1924. (should
  1925. (equal ""
  1926. (let ((org-todo-keywords '((sequence "TODO" "DONE")))
  1927. (org-inlinetask-min-level 3)
  1928. org-export-filter-body-functions
  1929. org-export-filter-final-output-functions)
  1930. (org-test-with-temp-text "*** TODO Inline"
  1931. (org-export-as (org-test-default-backend)
  1932. nil nil nil '(:with-tasks nil))))))))
  1933. ;;; Keywords
  1934. (ert-deftest test-org-export/get-date ()
  1935. "Test `org-export-get-date' specifications."
  1936. ;; Return a properly formatted string when
  1937. ;; `org-export-date-timestamp-format' is non-nil and DATE keyword
  1938. ;; consists in a single timestamp.
  1939. (should
  1940. (equal "29 03 2012"
  1941. (let ((org-export-date-timestamp-format "%d %m %Y"))
  1942. (org-test-with-parsed-data "#+DATE: <2012-03-29 Thu>"
  1943. (org-export-get-date info)))))
  1944. ;; Return a secondary string otherwise.
  1945. (should-not
  1946. (stringp
  1947. (let ((org-export-date-timestamp-format nil))
  1948. (org-test-with-parsed-data "#+DATE: <2012-03-29 Thu>"
  1949. (org-export-get-date info)))))
  1950. (should
  1951. (equal '("Date")
  1952. (org-test-with-parsed-data "#+DATE: Date"
  1953. (org-export-get-date info))))
  1954. ;; Optional argument has precedence over
  1955. ;; `org-export-date-timestamp-format'.
  1956. (should
  1957. (equal "29 03"
  1958. (let ((org-export-date-timestamp-format "%d %m %Y"))
  1959. (org-test-with-parsed-data "#+DATE: <2012-03-29 Thu>"
  1960. (org-export-get-date info "%d %m"))))))
  1961. ;;; Links
  1962. (ert-deftest test-org-export/custom-protocol-maybe ()
  1963. "Test `org-export-custom-protocol-maybe' specifications."
  1964. (should
  1965. (string-match
  1966. "success"
  1967. (let ((org-link-types (copy-sequence org-link-types)))
  1968. (org-add-link-type "foo" nil (lambda (p d f) "success"))
  1969. (org-export-string-as
  1970. "[[foo:path]]"
  1971. (org-export-create-backend
  1972. :name 'test
  1973. :transcoders '((section . (lambda (s c i) c))
  1974. (paragraph . (lambda (p c i) c))
  1975. (link . (lambda (l c i)
  1976. (or (org-export-custom-protocol-maybe l c i)
  1977. "failure")))))))))
  1978. (should-not
  1979. (string-match
  1980. "success"
  1981. (let ((org-link-types (copy-sequence org-link-types)))
  1982. (org-add-link-type
  1983. "foo" nil (lambda (p d f) (and (eq f 'test) "success")))
  1984. (org-export-string-as
  1985. "[[foo:path]]"
  1986. (org-export-create-backend
  1987. :name 'no-test
  1988. :transcoders '((section . (lambda (s c i) c))
  1989. (paragraph . (lambda (p c i) c))
  1990. (link . (lambda (l c i)
  1991. (or (org-export-custom-protocol-maybe l c i)
  1992. "failure")))))))))
  1993. ;; Ignore anonymous back-ends.
  1994. (should-not
  1995. (string-match
  1996. "success"
  1997. (let ((org-link-types (copy-sequence org-link-types)))
  1998. (org-add-link-type
  1999. "foo" nil (lambda (p d f) (and (eq f 'test) "success")))
  2000. (org-export-string-as
  2001. "[[foo:path]]"
  2002. (org-export-create-backend
  2003. :transcoders '((section . (lambda (s c i) c))
  2004. (paragraph . (lambda (p c i) c))
  2005. (link . (lambda (l c i)
  2006. (or (org-export-custom-protocol-maybe l c i)
  2007. "failure"))))))))))
  2008. (ert-deftest test-org-export/get-coderef-format ()
  2009. "Test `org-export-get-coderef-format' specifications."
  2010. ;; A link without description returns "%s"
  2011. (should (equal (org-export-get-coderef-format "(ref:line)" nil)
  2012. "%s"))
  2013. ;; Return "%s" when path is matched within description.
  2014. (should (equal (org-export-get-coderef-format "path" "desc (path)")
  2015. "desc %s"))
  2016. ;; Otherwise return description.
  2017. (should (equal (org-export-get-coderef-format "path" "desc")
  2018. "desc")))
  2019. (ert-deftest test-org-export/inline-image-p ()
  2020. "Test `org-export-inline-image-p' specifications."
  2021. (should
  2022. (org-export-inline-image-p
  2023. (org-test-with-temp-text "[[#id]]"
  2024. (org-element-map (org-element-parse-buffer) 'link 'identity nil t))
  2025. '(("custom-id" . "id")))))
  2026. (ert-deftest test-org-export/fuzzy-link ()
  2027. "Test fuzzy links specifications."
  2028. ;; Link to an headline should return headline's number.
  2029. (org-test-with-parsed-data
  2030. "Paragraph.\n* Head1\n* Head2\n* Head3\n[[Head2]]"
  2031. (should
  2032. ;; Note: Headline's number is in fact a list of numbers.
  2033. (equal '(2)
  2034. (org-element-map tree 'link
  2035. (lambda (link)
  2036. (org-export-get-ordinal
  2037. (org-export-resolve-fuzzy-link link info) info)) info t))))
  2038. ;; Link to a target in an item should return item's number.
  2039. (org-test-with-parsed-data
  2040. "- Item1\n - Item11\n - <<test>>Item12\n- Item2\n\n\n[[test]]"
  2041. (should
  2042. ;; Note: Item's number is in fact a list of numbers.
  2043. (equal '(1 2)
  2044. (org-element-map tree 'link
  2045. (lambda (link)
  2046. (org-export-get-ordinal
  2047. (org-export-resolve-fuzzy-link link info) info)) info t))))
  2048. ;; Link to a target in a footnote should return footnote's number.
  2049. (org-test-with-parsed-data "
  2050. Paragraph[1][2][fn:lbl3:C<<target>>][[test]][[target]]\n[1] A\n\n[2] <<test>>B"
  2051. (should
  2052. (equal '(2 3)
  2053. (org-element-map tree 'link
  2054. (lambda (link)
  2055. (org-export-get-ordinal
  2056. (org-export-resolve-fuzzy-link link info) info)) info))))
  2057. ;; Link to a named element should return sequence number of that
  2058. ;; element.
  2059. (org-test-with-parsed-data
  2060. "#+NAME: tbl1\n|1|2|\n#+NAME: tbl2\n|3|4|\n#+NAME: tbl3\n|5|6|\n[[tbl2]]"
  2061. (should
  2062. (= 2
  2063. (org-element-map tree 'link
  2064. (lambda (link)
  2065. (org-export-get-ordinal
  2066. (org-export-resolve-fuzzy-link link info) info)) info t))))
  2067. ;; Link to a target not within an item, a table, a footnote
  2068. ;; reference or definition should return section number.
  2069. (org-test-with-parsed-data
  2070. "* Head1\n* Head2\nParagraph<<target>>\n* Head3\n[[target]]"
  2071. (should
  2072. (equal '(2)
  2073. (org-element-map tree 'link
  2074. (lambda (link)
  2075. (org-export-get-ordinal
  2076. (org-export-resolve-fuzzy-link link info) info)) info t))))
  2077. ;; Space are not significant when matching a fuzzy link.
  2078. (should
  2079. (org-test-with-parsed-data "* Head 1\n[[Head\n 1]]"
  2080. (org-element-map tree 'link
  2081. (lambda (link) (org-export-resolve-fuzzy-link link info))
  2082. info t)))
  2083. ;; Statistics cookies are ignored for headline match.
  2084. (should
  2085. (org-test-with-parsed-data "* Head [0/0]\n[[Head]]"
  2086. (org-element-map tree 'link
  2087. (lambda (link) (org-export-resolve-fuzzy-link link info))
  2088. info t)))
  2089. (should
  2090. (org-test-with-parsed-data "* Head [100%]\n[[Head]]"
  2091. (org-element-map tree 'link
  2092. (lambda (link) (org-export-resolve-fuzzy-link link info))
  2093. info t)))
  2094. ;; Headline match is position dependent.
  2095. (should-not
  2096. (apply
  2097. 'eq
  2098. (org-test-with-parsed-data "* H1\n[[*H1]]\n* H1\n[[*H1]]"
  2099. (org-element-map tree 'link
  2100. (lambda (link) (org-export-resolve-fuzzy-link link info)) info)))))
  2101. (ert-deftest test-org-export/resolve-coderef ()
  2102. "Test `org-export-resolve-coderef' specifications."
  2103. (let ((org-coderef-label-format "(ref:%s)"))
  2104. ;; 1. A link to a "-n -k -r" block returns line number.
  2105. (org-test-with-parsed-data
  2106. "#+BEGIN_EXAMPLE -n -k -r\nText (ref:coderef)\n#+END_EXAMPLE"
  2107. (should (= (org-export-resolve-coderef "coderef" info) 1)))
  2108. (org-test-with-parsed-data
  2109. "#+BEGIN_SRC emacs-lisp -n -k -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  2110. (should (= (org-export-resolve-coderef "coderef" info) 1)))
  2111. ;; 2. A link to a "-n -r" block returns line number.
  2112. (org-test-with-parsed-data
  2113. "#+BEGIN_EXAMPLE -n -r\nText (ref:coderef)\n#+END_EXAMPLE"
  2114. (should (= (org-export-resolve-coderef "coderef" info) 1)))
  2115. (org-test-with-parsed-data
  2116. "#+BEGIN_SRC emacs-lisp -n -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  2117. (should (= (org-export-resolve-coderef "coderef" info) 1)))
  2118. ;; 3. A link to a "-n" block returns coderef.
  2119. (org-test-with-parsed-data
  2120. "#+BEGIN_SRC emacs-lisp -n\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  2121. (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
  2122. (org-test-with-parsed-data
  2123. "#+BEGIN_EXAMPLE -n\nText (ref:coderef)\n#+END_EXAMPLE"
  2124. (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
  2125. ;; 4. A link to a "-r" block returns line number.
  2126. (org-test-with-parsed-data
  2127. "#+BEGIN_SRC emacs-lisp -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  2128. (should (= (org-export-resolve-coderef "coderef" info) 1)))
  2129. (org-test-with-parsed-data
  2130. "#+BEGIN_EXAMPLE -r\nText (ref:coderef)\n#+END_EXAMPLE"
  2131. (should (= (org-export-resolve-coderef "coderef" info) 1)))
  2132. ;; 5. A link to a block without a switch returns coderef.
  2133. (org-test-with-parsed-data
  2134. "#+BEGIN_SRC emacs-lisp\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  2135. (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
  2136. (org-test-with-parsed-data
  2137. "#+BEGIN_EXAMPLE\nText (ref:coderef)\n#+END_EXAMPLE"
  2138. (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
  2139. ;; 6. Correctly handle continued line numbers. A "+n" switch
  2140. ;; should resume numbering from previous block with numbered
  2141. ;; lines, ignoring blocks not numbering lines in the process.
  2142. ;; A "-n" switch resets count.
  2143. (org-test-with-parsed-data "
  2144. #+BEGIN_EXAMPLE -n
  2145. Text.
  2146. #+END_EXAMPLE
  2147. #+BEGIN_SRC emacs-lisp
  2148. \(- 1 1)
  2149. #+END_SRC
  2150. #+BEGIN_SRC emacs-lisp +n -r
  2151. \(+ 1 1) (ref:addition)
  2152. #+END_SRC
  2153. #+BEGIN_EXAMPLE -n -r
  2154. Another text. (ref:text)
  2155. #+END_EXAMPLE"
  2156. (should (= (org-export-resolve-coderef "addition" info) 2))
  2157. (should (= (org-export-resolve-coderef "text" info) 1)))
  2158. ;; 7. Recognize coderef with user-specified syntax.
  2159. (org-test-with-parsed-data
  2160. "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\nText. [ref:text]\n#+END_EXAMPLE"
  2161. (should (equal (org-export-resolve-coderef "text" info) "text")))))
  2162. (ert-deftest test-org-export/resolve-fuzzy-link ()
  2163. "Test `org-export-resolve-fuzzy-link' specifications."
  2164. ;; Match target objects.
  2165. (should
  2166. (org-test-with-parsed-data "<<target>> [[target]]"
  2167. (org-export-resolve-fuzzy-link
  2168. (org-element-map tree 'link 'identity info t) info)))
  2169. ;; Match named elements.
  2170. (should
  2171. (org-test-with-parsed-data "#+NAME: target\nParagraph\n\n[[target]]"
  2172. (org-export-resolve-fuzzy-link
  2173. (org-element-map tree 'link 'identity info t) info)))
  2174. ;; Match exact headline's name.
  2175. (should
  2176. (org-test-with-parsed-data "* My headline\n[[My headline]]"
  2177. (org-export-resolve-fuzzy-link
  2178. (org-element-map tree 'link 'identity info t) info)))
  2179. ;; Targets objects have priority over named elements and headline
  2180. ;; titles.
  2181. (should
  2182. (eq 'target
  2183. (org-test-with-parsed-data
  2184. "* target\n#+NAME: target\n<<target>>\n\n[[target]]"
  2185. (org-element-type
  2186. (org-export-resolve-fuzzy-link
  2187. (org-element-map tree 'link 'identity info t) info)))))
  2188. ;; Named elements have priority over headline titles.
  2189. (should
  2190. (eq 'paragraph
  2191. (org-test-with-parsed-data
  2192. "* target\n#+NAME: target\nParagraph\n\n[[target]]"
  2193. (org-element-type
  2194. (org-export-resolve-fuzzy-link
  2195. (org-element-map tree 'link 'identity info t) info)))))
  2196. ;; If link's path starts with a "*", only match headline titles,
  2197. ;; though.
  2198. (should
  2199. (eq 'headline
  2200. (org-test-with-parsed-data
  2201. "* target\n#+NAME: target\n<<target>>\n\n[[*target]]"
  2202. (org-element-type
  2203. (org-export-resolve-fuzzy-link
  2204. (org-element-map tree 'link 'identity info t) info)))))
  2205. ;; Return nil if no match.
  2206. (should-not
  2207. (org-test-with-parsed-data "[[target]]"
  2208. (org-export-resolve-fuzzy-link
  2209. (org-element-map tree 'link 'identity info t) info)))
  2210. ;; Match fuzzy link even when before first headline.
  2211. (should
  2212. (eq 'headline
  2213. (org-test-with-parsed-data "[[hl]]\n* hl"
  2214. (org-element-type
  2215. (org-export-resolve-fuzzy-link
  2216. (org-element-map tree 'link 'identity info t) info))))))
  2217. (ert-deftest test-org-export/resolve-id-link ()
  2218. "Test `org-export-resolve-id-link' specifications."
  2219. ;; 1. Regular test for custom-id link.
  2220. (org-test-with-parsed-data "* Headline1
  2221. :PROPERTIES:
  2222. :CUSTOM_ID: test
  2223. :END:
  2224. * Headline 2
  2225. \[[#test]]"
  2226. (should
  2227. (org-export-resolve-id-link
  2228. (org-element-map tree 'link 'identity info t) info)))
  2229. ;; 2. Failing test for custom-id link.
  2230. (org-test-with-parsed-data "* Headline1
  2231. :PROPERTIES:
  2232. :CUSTOM_ID: test
  2233. :END:
  2234. * Headline 2
  2235. \[[#no-match]]"
  2236. (should-not
  2237. (org-export-resolve-id-link
  2238. (org-element-map tree 'link 'identity info t) info)))
  2239. ;; 3. Test for internal id target.
  2240. (org-test-with-parsed-data "* Headline1
  2241. :PROPERTIES:
  2242. :ID: aaaa
  2243. :END:
  2244. * Headline 2
  2245. \[[id:aaaa]]"
  2246. (should
  2247. (org-export-resolve-id-link
  2248. (org-element-map tree 'link 'identity info t) info)))
  2249. ;; 4. Test for external id target.
  2250. (org-test-with-parsed-data "[[id:aaaa]]"
  2251. (should
  2252. (org-export-resolve-id-link
  2253. (org-element-map tree 'link 'identity info t)
  2254. (org-combine-plists info '(:id-alist (("aaaa" . "external-file"))))))))
  2255. (ert-deftest test-org-export/resolve-radio-link ()
  2256. "Test `org-export-resolve-radio-link' specifications."
  2257. ;; Standard test.
  2258. (should
  2259. (org-test-with-temp-text "<<<radio>>> radio"
  2260. (org-update-radio-target-regexp)
  2261. (let* ((tree (org-element-parse-buffer))
  2262. (info `(:parse-tree ,tree)))
  2263. (org-export-resolve-radio-link
  2264. (org-element-map tree 'link 'identity info t)
  2265. info))))
  2266. ;; Radio targets are case-insensitive.
  2267. (should
  2268. (org-test-with-temp-text "<<<RADIO>>> radio"
  2269. (org-update-radio-target-regexp)
  2270. (let* ((tree (org-element-parse-buffer))
  2271. (info `(:parse-tree ,tree)))
  2272. (org-export-resolve-radio-link
  2273. (org-element-map tree 'link 'identity info t)
  2274. info))))
  2275. ;; Radio target with objects.
  2276. (should
  2277. (org-test-with-temp-text "<<<radio \\alpha>>> radio \\alpha"
  2278. (org-update-radio-target-regexp)
  2279. (let* ((tree (org-element-parse-buffer))
  2280. (info `(:parse-tree ,tree)))
  2281. (org-export-resolve-radio-link
  2282. (org-element-map tree 'link 'identity info t)
  2283. info))))
  2284. ;; Radio target with objects at its beginning.
  2285. (should
  2286. (org-test-with-temp-text "<<<\\alpha radio>>> \\alpha radio"
  2287. (org-update-radio-target-regexp)
  2288. (let* ((tree (org-element-parse-buffer))
  2289. (info `(:parse-tree ,tree)))
  2290. (org-export-resolve-radio-link
  2291. (org-element-map tree 'link 'identity info t)
  2292. info))))
  2293. ;; Radio link next to an apostrophe.
  2294. (should
  2295. (org-test-with-temp-text "<<<radio>>> radio's"
  2296. (org-update-radio-target-regexp)
  2297. (let* ((tree (org-element-parse-buffer))
  2298. (info `(:parse-tree ,tree)))
  2299. (org-export-resolve-radio-link
  2300. (org-element-map tree 'link 'identity info t)
  2301. info))))
  2302. ;; Multiple radio targets.
  2303. (should
  2304. (equal '("radio1" "radio2")
  2305. (org-test-with-temp-text "<<<radio1>>> <<<radio2>>> radio1 radio2"
  2306. (org-update-radio-target-regexp)
  2307. (let* ((tree (org-element-parse-buffer))
  2308. (info `(:parse-tree ,tree)))
  2309. (org-element-map tree 'link
  2310. (lambda (link)
  2311. (org-element-property
  2312. :value (org-export-resolve-radio-link link info)))
  2313. info)))))
  2314. ;; Radio target is whitespace insensitive.
  2315. (should
  2316. (org-test-with-temp-text "<<<a radio>>> a\n radio"
  2317. (org-update-radio-target-regexp)
  2318. (let* ((tree (org-element-parse-buffer))
  2319. (info `(:parse-tree ,tree)))
  2320. (org-element-map tree 'link
  2321. (lambda (link) (org-export-resolve-radio-link link info)) info t)))))
  2322. ;;; Src-block and example-block
  2323. (ert-deftest test-org-export/unravel-code ()
  2324. "Test `org-export-unravel-code' function."
  2325. ;; Code without reference.
  2326. (should
  2327. (equal '("(+ 1 1)\n")
  2328. (org-test-with-temp-text "#+BEGIN_EXAMPLE\n(+ 1 1)\n#+END_EXAMPLE"
  2329. (org-export-unravel-code (org-element-at-point)))))
  2330. ;; Code with reference.
  2331. (should
  2332. (equal '("(+ 1 1)\n" (1 . "test"))
  2333. (org-test-with-temp-text
  2334. "#+BEGIN_EXAMPLE\n(+ 1 1) (ref:test)\n#+END_EXAMPLE"
  2335. (let ((org-coderef-label-format "(ref:%s)"))
  2336. (org-export-unravel-code (org-element-at-point))))))
  2337. ;; Code with user-defined reference.
  2338. (should
  2339. (equal
  2340. '("(+ 1 1)\n" (1 . "test"))
  2341. (org-test-with-temp-text
  2342. "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\n(+ 1 1) [ref:test]\n#+END_EXAMPLE"
  2343. (let ((org-coderef-label-format "(ref:%s)"))
  2344. (org-export-unravel-code (org-element-at-point))))))
  2345. ;; Code references keys are relative to the current block.
  2346. (should
  2347. (equal '("(+ 2 2)\n(+ 3 3)\n" (2 . "one"))
  2348. (org-test-with-temp-text "
  2349. #+BEGIN_EXAMPLE -n
  2350. \(+ 1 1)
  2351. #+END_EXAMPLE
  2352. #+BEGIN_EXAMPLE +n
  2353. \(+ 2 2)
  2354. \(+ 3 3) (ref:one)
  2355. #+END_EXAMPLE"
  2356. (goto-line 5)
  2357. (let ((org-coderef-label-format "(ref:%s)"))
  2358. (org-export-unravel-code (org-element-at-point)))))))
  2359. (ert-deftest test-org-export/format-code-default ()
  2360. "Test `org-export-format-code-default' specifications."
  2361. ;; Return the empty string when code is empty.
  2362. (should
  2363. (equal ""
  2364. (org-test-with-parsed-data "#+BEGIN_SRC emacs-lisp\n\n\n#+END_SRC"
  2365. (org-export-format-code-default
  2366. (org-element-map tree 'src-block 'identity info t) info))))
  2367. ;; Number lines, two whitespace characters before the actual loc.
  2368. (should
  2369. (equal "1 a\n2 b\n"
  2370. (org-test-with-parsed-data
  2371. "#+BEGIN_SRC emacs-lisp +n\na\nb\n#+END_SRC"
  2372. (org-export-format-code-default
  2373. (org-element-map tree 'src-block 'identity info t) info))))
  2374. ;; Put references 6 whitespace characters after the widest line,
  2375. ;; wrapped within parenthesis.
  2376. (should
  2377. (equal "123 (a)\n1 (b)\n"
  2378. (let ((org-coderef-label-format "(ref:%s)"))
  2379. (org-test-with-parsed-data
  2380. "#+BEGIN_SRC emacs-lisp\n123 (ref:a)\n1 (ref:b)\n#+END_SRC"
  2381. (org-export-format-code-default
  2382. (org-element-map tree 'src-block 'identity info t) info))))))
  2383. ;;; Smart Quotes
  2384. (ert-deftest test-org-export/activate-smart-quotes ()
  2385. "Test `org-export-activate-smart-quotes' specifications."
  2386. ;; Opening double quotes: standard test.
  2387. (should
  2388. (equal
  2389. '("some &ldquo;paragraph")
  2390. (let ((org-export-default-language "en"))
  2391. (org-test-with-parsed-data "some \"paragraph"
  2392. (org-element-map tree 'plain-text
  2393. (lambda (s) (org-export-activate-smart-quotes s :html info))
  2394. info)))))
  2395. ;; Opening quotes: at the beginning of a paragraph.
  2396. (should
  2397. (equal
  2398. '("&ldquo;begin")
  2399. (let ((org-export-default-language "en"))
  2400. (org-test-with-parsed-data "\"begin"
  2401. (org-element-map tree 'plain-text
  2402. (lambda (s) (org-export-activate-smart-quotes s :html info))
  2403. info)))))
  2404. ;; Opening quotes: after an object.
  2405. (should
  2406. (equal
  2407. '("&ldquo;begin")
  2408. (let ((org-export-default-language "en"))
  2409. (org-test-with-parsed-data "=verb= \"begin"
  2410. (org-element-map tree 'plain-text
  2411. (lambda (s) (org-export-activate-smart-quotes s :html info))
  2412. info)))))
  2413. ;; Closing quotes: standard test.
  2414. (should
  2415. (equal
  2416. '("some&rdquo; paragraph")
  2417. (let ((org-export-default-language "en"))
  2418. (org-test-with-parsed-data "some\" paragraph"
  2419. (org-element-map tree 'plain-text
  2420. (lambda (s) (org-export-activate-smart-quotes s :html info))
  2421. info)))))
  2422. ;; Closing quotes: at the end of a paragraph.
  2423. (should
  2424. (equal
  2425. '("end&rdquo;")
  2426. (let ((org-export-default-language "en"))
  2427. (org-test-with-parsed-data "end\""
  2428. (org-element-map tree 'plain-text
  2429. (lambda (s) (org-export-activate-smart-quotes s :html info))
  2430. info)))))
  2431. ;; Apostrophe: standard test.
  2432. (should
  2433. (equal
  2434. '("It shouldn&rsquo;t fail")
  2435. (let ((org-export-default-language "en"))
  2436. (org-test-with-parsed-data "It shouldn't fail"
  2437. (org-element-map tree 'plain-text
  2438. (lambda (s) (org-export-activate-smart-quotes s :html info))
  2439. info)))))
  2440. ;; Apostrophe: before an object.
  2441. (should
  2442. (equal
  2443. '("a&rsquo;")
  2444. (let ((org-export-default-language "en"))
  2445. (org-test-with-parsed-data "a'=b="
  2446. (org-element-map tree 'plain-text
  2447. (lambda (s) (org-export-activate-smart-quotes s :html info))
  2448. info)))))
  2449. ;; Apostrophe: after an object.
  2450. (should
  2451. (equal
  2452. '("&rsquo;s")
  2453. (let ((org-export-default-language "en"))
  2454. (org-test-with-parsed-data "=code='s"
  2455. (org-element-map tree 'plain-text
  2456. (lambda (s) (org-export-activate-smart-quotes s :html info))
  2457. info)))))
  2458. ;; Special case: isolated quotes.
  2459. (should
  2460. (equal '("&ldquo;" "&rdquo;")
  2461. (let ((org-export-default-language "en"))
  2462. (org-test-with-parsed-data "\"$x$\""
  2463. (org-element-map tree 'plain-text
  2464. (lambda (s) (org-export-activate-smart-quotes s :html info))
  2465. info)))))
  2466. ;; Smart quotes in secondary strings.
  2467. (should
  2468. (equal '("&ldquo;" "&rdquo;")
  2469. (let ((org-export-default-language "en"))
  2470. (org-test-with-parsed-data "* \"$x$\""
  2471. (org-element-map tree 'plain-text
  2472. (lambda (s) (org-export-activate-smart-quotes s :html info))
  2473. info)))))
  2474. ;; Smart quotes in document keywords.
  2475. (should
  2476. (equal '("&ldquo;" "&rdquo;")
  2477. (let ((org-export-default-language "en"))
  2478. (org-test-with-parsed-data "#+TITLE: \"$x$\""
  2479. (org-element-map (plist-get info :title) 'plain-text
  2480. (lambda (s) (org-export-activate-smart-quotes s :html info))
  2481. info)))))
  2482. ;; Smart quotes in parsed affiliated keywords.
  2483. (should
  2484. (equal '("&ldquo;" "&rdquo;" "Paragraph")
  2485. (let ((org-export-default-language "en"))
  2486. (org-test-with-parsed-data "#+CAPTION: \"$x$\"\nParagraph"
  2487. (org-element-map tree 'plain-text
  2488. (lambda (s) (org-export-activate-smart-quotes s :html info))
  2489. info nil nil t))))))
  2490. ;;; Tables
  2491. (ert-deftest test-org-export/special-column ()
  2492. "Test if the table's special column is properly recognized."
  2493. ;; 1. First column is special if it contains only a special marking
  2494. ;; characters or empty cells.
  2495. (org-test-with-temp-text "
  2496. | ! | 1 |
  2497. | | 2 |"
  2498. (should
  2499. (org-export-table-has-special-column-p
  2500. (org-element-map
  2501. (org-element-parse-buffer) 'table 'identity nil 'first-match))))
  2502. ;; 2. If the column contains anything else, it isn't special.
  2503. (org-test-with-temp-text "
  2504. | ! | 1 |
  2505. | b | 2 |"
  2506. (should-not
  2507. (org-export-table-has-special-column-p
  2508. (org-element-map
  2509. (org-element-parse-buffer) 'table 'identity nil 'first-match))))
  2510. ;; 3. Special marking characters are "#", "^", "*", "_", "/", "$"
  2511. ;; and "!".
  2512. (org-test-with-temp-text "
  2513. | # | 1 |
  2514. | ^ | 2 |
  2515. | * | 3 |
  2516. | _ | 4 |
  2517. | / | 5 |
  2518. | $ | 6 |
  2519. | ! | 7 |"
  2520. (should
  2521. (org-export-table-has-special-column-p
  2522. (org-element-map
  2523. (org-element-parse-buffer) 'table 'identity nil 'first-match))))
  2524. ;; 4. A first column with only empty cells isn't considered as
  2525. ;; special.
  2526. (org-test-with-temp-text "
  2527. | | 1 |
  2528. | | 2 |"
  2529. (should-not
  2530. (org-export-table-has-special-column-p
  2531. (org-element-map
  2532. (org-element-parse-buffer) 'table 'identity nil 'first-match)))))
  2533. (ert-deftest test-org-export/table-row-is-special-p ()
  2534. "Test `org-export-table-row-is-special-p' specifications."
  2535. ;; 1. A row is special if it has a special marking character in the
  2536. ;; special column.
  2537. (org-test-with-parsed-data "| ! | 1 |"
  2538. (should
  2539. (org-export-table-row-is-special-p
  2540. (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  2541. ;; 2. A row is special when its first field is "/"
  2542. (org-test-with-parsed-data "
  2543. | / | 1 |
  2544. | a | b |"
  2545. (should
  2546. (org-export-table-row-is-special-p
  2547. (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  2548. ;; 3. A row only containing alignment cookies is also considered as
  2549. ;; special.
  2550. (org-test-with-parsed-data "| <5> | | <l> | <l22> |"
  2551. (should
  2552. (org-export-table-row-is-special-p
  2553. (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  2554. ;; 4. Everything else isn't considered as special.
  2555. (org-test-with-parsed-data "| \alpha | | c |"
  2556. (should-not
  2557. (org-export-table-row-is-special-p
  2558. (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  2559. ;; 5. Table's rules are never considered as special rows.
  2560. (org-test-with-parsed-data "|---+---|"
  2561. (should-not
  2562. (org-export-table-row-is-special-p
  2563. (org-element-map tree 'table-row 'identity nil 'first-match) info))))
  2564. (ert-deftest test-org-export/has-header-p ()
  2565. "Test `org-export-table-has-header-p' specifications."
  2566. ;; 1. With an header.
  2567. (org-test-with-parsed-data "
  2568. | a | b |
  2569. |---+---|
  2570. | c | d |"
  2571. (should
  2572. (org-export-table-has-header-p
  2573. (org-element-map tree 'table 'identity info 'first-match)
  2574. info)))
  2575. ;; 2. Without an header.
  2576. (org-test-with-parsed-data "
  2577. | a | b |
  2578. | c | d |"
  2579. (should-not
  2580. (org-export-table-has-header-p
  2581. (org-element-map tree 'table 'identity info 'first-match)
  2582. info)))
  2583. ;; 3. Don't get fooled with starting and ending rules.
  2584. (org-test-with-parsed-data "
  2585. |---+---|
  2586. | a | b |
  2587. | c | d |
  2588. |---+---|"
  2589. (should-not
  2590. (org-export-table-has-header-p
  2591. (org-element-map tree 'table 'identity info 'first-match)
  2592. info))))
  2593. (ert-deftest test-org-export/table-row-group ()
  2594. "Test `org-export-table-row-group' specifications."
  2595. ;; 1. A rule creates a new group.
  2596. (should
  2597. (equal '(1 rule 2)
  2598. (org-test-with-parsed-data "
  2599. | a | b |
  2600. |---+---|
  2601. | 1 | 2 |"
  2602. (org-element-map tree 'table-row
  2603. (lambda (row)
  2604. (if (eq (org-element-property :type row) 'rule) 'rule
  2605. (org-export-table-row-group row info)))))))
  2606. ;; 2. Special rows are ignored in count.
  2607. (should
  2608. (equal
  2609. '(rule 1)
  2610. (org-test-with-parsed-data "
  2611. | / | < | > |
  2612. |---|---+---|
  2613. | | 1 | 2 |"
  2614. (org-element-map tree 'table-row
  2615. (lambda (row)
  2616. (if (eq (org-element-property :type row) 'rule) 'rule
  2617. (org-export-table-row-group row info)))
  2618. info))))
  2619. ;; 3. Double rules also are ignored in count.
  2620. (should
  2621. (equal '(1 rule rule 2)
  2622. (org-test-with-parsed-data "
  2623. | a | b |
  2624. |---+---|
  2625. |---+---|
  2626. | 1 | 2 |"
  2627. (org-element-map tree 'table-row
  2628. (lambda (row)
  2629. (if (eq (org-element-property :type row) 'rule) 'rule
  2630. (org-export-table-row-group row info))))))))
  2631. (ert-deftest test-org-export/table-row-number ()
  2632. "Test `org-export-table-row-number' specifications."
  2633. ;; Standard test. Number is 0-indexed.
  2634. (should
  2635. (equal '(0 1)
  2636. (org-test-with-parsed-data "| a | b | c |\n| d | e | f |"
  2637. (org-element-map tree 'table-row
  2638. (lambda (row) (org-export-table-row-number row info)) info))))
  2639. ;; Number ignores separators.
  2640. (should
  2641. (equal '(0 1)
  2642. (org-test-with-parsed-data "
  2643. | a | b | c |
  2644. |---+---+---|
  2645. | d | e | f |"
  2646. (org-element-map tree 'table-row
  2647. (lambda (row) (org-export-table-row-number row info)) info))))
  2648. ;; Number ignores special rows.
  2649. (should
  2650. (equal '(0 1)
  2651. (org-test-with-parsed-data "
  2652. | / | < | > |
  2653. | | b | c |
  2654. |---+-----+-----|
  2655. | | <c> | <c> |
  2656. | | e | f |"
  2657. (org-element-map tree 'table-row
  2658. (lambda (row) (org-export-table-row-number row info)) info)))))
  2659. (ert-deftest test-org-export/table-cell-width ()
  2660. "Test `org-export-table-cell-width' specifications."
  2661. ;; 1. Width is primarily determined by width cookies. If no cookie
  2662. ;; is found, cell's width is nil.
  2663. (org-test-with-parsed-data "
  2664. | / | <l> | <6> | <l7> |
  2665. | | a | b | c |"
  2666. (should
  2667. (equal
  2668. '(nil 6 7)
  2669. (mapcar (lambda (cell) (org-export-table-cell-width cell info))
  2670. (org-element-map tree 'table-cell 'identity info)))))
  2671. ;; 2. The last width cookie has precedence.
  2672. (org-test-with-parsed-data "
  2673. | <6> |
  2674. | <7> |
  2675. | a |"
  2676. (should
  2677. (equal
  2678. '(7)
  2679. (mapcar (lambda (cell) (org-export-table-cell-width cell info))
  2680. (org-element-map tree 'table-cell 'identity info)))))
  2681. ;; 3. Valid width cookies must have a specific row.
  2682. (org-test-with-parsed-data "| <6> | cell |"
  2683. (should
  2684. (equal
  2685. '(nil nil)
  2686. (mapcar (lambda (cell) (org-export-table-cell-width cell info))
  2687. (org-element-map tree 'table-cell 'identity))))))
  2688. (ert-deftest test-org-export/table-cell-alignment ()
  2689. "Test `org-export-table-cell-alignment' specifications."
  2690. ;; 1. Alignment is primarily determined by alignment cookies.
  2691. (should
  2692. (equal '(left center right)
  2693. (let ((org-table-number-fraction 0.5)
  2694. (org-table-number-regexp "^[0-9]+$"))
  2695. (org-test-with-parsed-data "| <l> | <c> | <r> |"
  2696. (mapcar (lambda (cell)
  2697. (org-export-table-cell-alignment cell info))
  2698. (org-element-map tree 'table-cell 'identity))))))
  2699. ;; 2. The last alignment cookie has precedence.
  2700. (should
  2701. (equal '(right right right)
  2702. (org-test-with-parsed-data "
  2703. | <l8> |
  2704. | cell |
  2705. | <r9> |"
  2706. (mapcar (lambda (cell) (org-export-table-cell-alignment cell info))
  2707. (org-element-map tree 'table-cell 'identity)))))
  2708. ;; 3. If there's no cookie, cell's contents determine alignment.
  2709. ;; A column mostly made of cells containing numbers will align
  2710. ;; its cells to the right.
  2711. (should
  2712. (equal '(right right right)
  2713. (let ((org-table-number-fraction 0.5)
  2714. (org-table-number-regexp "^[0-9]+$"))
  2715. (org-test-with-parsed-data "
  2716. | 123 |
  2717. | some text |
  2718. | 12345 |"
  2719. (mapcar (lambda (cell)
  2720. (org-export-table-cell-alignment cell info))
  2721. (org-element-map tree 'table-cell 'identity))))))
  2722. ;; 4. Otherwise, they will be aligned to the left.
  2723. (should
  2724. (equal '(left left left)
  2725. (org-test-with-parsed-data "
  2726. | text |
  2727. | some text |
  2728. | \alpha |"
  2729. (mapcar (lambda (cell)
  2730. (org-export-table-cell-alignment cell info))
  2731. (org-element-map tree 'table-cell 'identity info))))))
  2732. (ert-deftest test-org-export/table-cell-borders ()
  2733. "Test `org-export-table-cell-borders' specifications."
  2734. ;; 1. Recognize various column groups indicators.
  2735. (org-test-with-parsed-data "| / | < | > | <> |"
  2736. (should
  2737. (equal
  2738. '((right bottom top) (left bottom top) (right bottom top)
  2739. (right left bottom top))
  2740. (mapcar (lambda (cell)
  2741. (org-export-table-cell-borders cell info))
  2742. (org-element-map tree 'table-cell 'identity)))))
  2743. ;; 2. Accept shortcuts to define column groups.
  2744. (org-test-with-parsed-data "| / | < | < |"
  2745. (should
  2746. (equal
  2747. '((right bottom top) (right left bottom top) (left bottom top))
  2748. (mapcar (lambda (cell)
  2749. (org-export-table-cell-borders cell info))
  2750. (org-element-map tree 'table-cell 'identity)))))
  2751. ;; 3. A valid column groups row must start with a "/".
  2752. (org-test-with-parsed-data "
  2753. | | < |
  2754. | a | b |"
  2755. (should
  2756. (equal '((top) (top) (bottom) (bottom))
  2757. (mapcar (lambda (cell)
  2758. (org-export-table-cell-borders cell info))
  2759. (org-element-map tree 'table-cell 'identity)))))
  2760. ;; 4. Take table rules into consideration.
  2761. (org-test-with-parsed-data "
  2762. | 1 |
  2763. |---|
  2764. | 2 |"
  2765. (should
  2766. (equal '((below top) (bottom above))
  2767. (mapcar (lambda (cell)
  2768. (org-export-table-cell-borders cell info))
  2769. (org-element-map tree 'table-cell 'identity)))))
  2770. ;; 5. Top and (resp. bottom) rules induce both `top' and `above'
  2771. ;; (resp. `bottom' and `below') borders. Any special row is
  2772. ;; ignored.
  2773. (org-test-with-parsed-data "
  2774. |---+----|
  2775. | / | |
  2776. | | 1 |
  2777. |---+----|"
  2778. (should
  2779. (equal '((bottom below top above))
  2780. (last
  2781. (mapcar (lambda (cell)
  2782. (org-export-table-cell-borders cell info))
  2783. (org-element-map tree 'table-cell 'identity)))))))
  2784. (ert-deftest test-org-export/table-dimensions ()
  2785. "Test `org-export-table-dimensions' specifications."
  2786. ;; 1. Standard test.
  2787. (org-test-with-parsed-data "
  2788. | 1 | 2 | 3 |
  2789. | 4 | 5 | 6 |"
  2790. (should
  2791. (equal '(2 . 3)
  2792. (org-export-table-dimensions
  2793. (org-element-map tree 'table 'identity info 'first-match) info))))
  2794. ;; 2. Ignore horizontal rules and special columns.
  2795. (org-test-with-parsed-data "
  2796. | / | < | > |
  2797. | 1 | 2 | 3 |
  2798. |---+---+---|
  2799. | 4 | 5 | 6 |"
  2800. (should
  2801. (equal '(2 . 3)
  2802. (org-export-table-dimensions
  2803. (org-element-map tree 'table 'identity info 'first-match) info)))))
  2804. (ert-deftest test-org-export/table-cell-address ()
  2805. "Test `org-export-table-cell-address' specifications."
  2806. ;; 1. Standard test: index is 0-based.
  2807. (org-test-with-parsed-data "| a | b |"
  2808. (should
  2809. (equal '((0 . 0) (0 . 1))
  2810. (org-element-map tree 'table-cell
  2811. (lambda (cell) (org-export-table-cell-address cell info))
  2812. info))))
  2813. ;; 2. Special column isn't counted, nor are special rows.
  2814. (org-test-with-parsed-data "
  2815. | / | <> |
  2816. | | c |"
  2817. (should
  2818. (equal '(0 . 0)
  2819. (org-export-table-cell-address
  2820. (car (last (org-element-map tree 'table-cell 'identity info)))
  2821. info))))
  2822. ;; 3. Tables rules do not count either.
  2823. (org-test-with-parsed-data "
  2824. | a |
  2825. |---|
  2826. | b |
  2827. |---|
  2828. | c |"
  2829. (should
  2830. (equal '(2 . 0)
  2831. (org-export-table-cell-address
  2832. (car (last (org-element-map tree 'table-cell 'identity info)))
  2833. info))))
  2834. ;; 4. Return nil for special cells.
  2835. (org-test-with-parsed-data "| / | a |"
  2836. (should-not
  2837. (org-export-table-cell-address
  2838. (org-element-map tree 'table-cell 'identity nil 'first-match)
  2839. info))))
  2840. (ert-deftest test-org-export/get-table-cell-at ()
  2841. "Test `org-export-get-table-cell-at' specifications."
  2842. ;; 1. Address ignores special columns, special rows and rules.
  2843. (org-test-with-parsed-data "
  2844. | / | <> |
  2845. | | a |
  2846. |---+----|
  2847. | | b |"
  2848. (should
  2849. (equal '("b")
  2850. (org-element-contents
  2851. (org-export-get-table-cell-at
  2852. '(1 . 0)
  2853. (org-element-map tree 'table 'identity info 'first-match)
  2854. info)))))
  2855. ;; 2. Return value for a non-existent address is nil.
  2856. (org-test-with-parsed-data "| a |"
  2857. (should-not
  2858. (org-export-get-table-cell-at
  2859. '(2 . 2)
  2860. (org-element-map tree 'table 'identity info 'first-match)
  2861. info)))
  2862. (org-test-with-parsed-data "| / |"
  2863. (should-not
  2864. (org-export-get-table-cell-at
  2865. '(0 . 0)
  2866. (org-element-map tree 'table 'identity info 'first-match)
  2867. info))))
  2868. (ert-deftest test-org-export/table-cell-starts-colgroup-p ()
  2869. "Test `org-export-table-cell-starts-colgroup-p' specifications."
  2870. ;; 1. A cell at a beginning of a row always starts a column group.
  2871. (org-test-with-parsed-data "| a |"
  2872. (should
  2873. (org-export-table-cell-starts-colgroup-p
  2874. (org-element-map tree 'table-cell 'identity info 'first-match)
  2875. info)))
  2876. ;; 2. Special column should be ignored when determining the
  2877. ;; beginning of the row.
  2878. (org-test-with-parsed-data "
  2879. | / | |
  2880. | | a |"
  2881. (should
  2882. (org-export-table-cell-starts-colgroup-p
  2883. (org-element-map tree 'table-cell 'identity info 'first-match)
  2884. info)))
  2885. ;; 2. Explicit column groups.
  2886. (org-test-with-parsed-data "
  2887. | / | | < |
  2888. | a | b | c |"
  2889. (should
  2890. (equal
  2891. '(yes no yes)
  2892. (org-element-map tree 'table-cell
  2893. (lambda (cell)
  2894. (if (org-export-table-cell-starts-colgroup-p cell info) 'yes 'no))
  2895. info)))))
  2896. (ert-deftest test-org-export/table-cell-ends-colgroup-p ()
  2897. "Test `org-export-table-cell-ends-colgroup-p' specifications."
  2898. ;; 1. A cell at the end of a row always ends a column group.
  2899. (org-test-with-parsed-data "| a |"
  2900. (should
  2901. (org-export-table-cell-ends-colgroup-p
  2902. (org-element-map tree 'table-cell 'identity info 'first-match)
  2903. info)))
  2904. ;; 2. Special column should be ignored when determining the
  2905. ;; beginning of the row.
  2906. (org-test-with-parsed-data "
  2907. | / | |
  2908. | | a |"
  2909. (should
  2910. (org-export-table-cell-ends-colgroup-p
  2911. (org-element-map tree 'table-cell 'identity info 'first-match)
  2912. info)))
  2913. ;; 3. Explicit column groups.
  2914. (org-test-with-parsed-data "
  2915. | / | < | |
  2916. | a | b | c |"
  2917. (should
  2918. (equal
  2919. '(yes no yes)
  2920. (org-element-map tree 'table-cell
  2921. (lambda (cell)
  2922. (if (org-export-table-cell-ends-colgroup-p cell info) 'yes 'no))
  2923. info)))))
  2924. (ert-deftest test-org-export/table-row-starts-rowgroup-p ()
  2925. "Test `org-export-table-row-starts-rowgroup-p' specifications."
  2926. ;; 1. A row at the beginning of a table always starts a row group.
  2927. ;; So does a row following a table rule.
  2928. (org-test-with-parsed-data "
  2929. | a |
  2930. |---|
  2931. | b |"
  2932. (should
  2933. (equal
  2934. '(yes no yes)
  2935. (org-element-map tree 'table-row
  2936. (lambda (row)
  2937. (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
  2938. info))))
  2939. ;; 2. Special rows should be ignored when determining the beginning
  2940. ;; of the row.
  2941. (org-test-with-parsed-data "
  2942. | / | < |
  2943. | | a |
  2944. |---+---|
  2945. | / | < |
  2946. | | b |"
  2947. (should
  2948. (equal
  2949. '(yes no yes)
  2950. (org-element-map tree 'table-row
  2951. (lambda (row)
  2952. (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
  2953. info)))))
  2954. (ert-deftest test-org-export/table-row-ends-rowgroup-p ()
  2955. "Test `org-export-table-row-ends-rowgroup-p' specifications."
  2956. ;; 1. A row at the end of a table always ends a row group. So does
  2957. ;; a row preceding a table rule.
  2958. (org-test-with-parsed-data "
  2959. | a |
  2960. |---|
  2961. | b |"
  2962. (should
  2963. (equal
  2964. '(yes no yes)
  2965. (org-element-map tree 'table-row
  2966. (lambda (row)
  2967. (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
  2968. info))))
  2969. ;; 2. Special rows should be ignored when determining the beginning
  2970. ;; of the row.
  2971. (org-test-with-parsed-data "
  2972. | | a |
  2973. | / | < |
  2974. |---+---|
  2975. | | b |
  2976. | / | < |"
  2977. (should
  2978. (equal
  2979. '(yes no yes)
  2980. (org-element-map tree 'table-row
  2981. (lambda (row)
  2982. (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
  2983. info)))))
  2984. (ert-deftest test-org-export/table-row-in-header-p ()
  2985. "Test `org-export-table-row-in-header-p' specifications."
  2986. ;; Standard test. Separators are always nil.
  2987. (should
  2988. (equal
  2989. '(yes no no)
  2990. (org-test-with-parsed-data "| a |\n|---|\n| b |"
  2991. (org-element-map tree 'table-row
  2992. (lambda (row)
  2993. (if (org-export-table-row-in-header-p row info) 'yes 'no)) info))))
  2994. ;; Nil when there is no header.
  2995. (should
  2996. (equal
  2997. '(no no)
  2998. (org-test-with-parsed-data "| a |\n| b |"
  2999. (org-element-map tree 'table-row
  3000. (lambda (row)
  3001. (if (org-export-table-row-in-header-p row info) 'yes 'no)) info)))))
  3002. (ert-deftest test-org-export/table-row-starts-header-p ()
  3003. "Test `org-export-table-row-starts-header-p' specifications."
  3004. ;; 1. Only the row starting the first row group starts the table
  3005. ;; header.
  3006. (org-test-with-parsed-data "
  3007. | a |
  3008. | b |
  3009. |---|
  3010. | c |"
  3011. (should
  3012. (equal
  3013. '(yes no no no)
  3014. (org-element-map tree 'table-row
  3015. (lambda (row)
  3016. (if (org-export-table-row-starts-header-p row info) 'yes 'no))
  3017. info))))
  3018. ;; 2. A row cannot start an header if there's no header in the
  3019. ;; table.
  3020. (org-test-with-parsed-data "
  3021. | a |
  3022. |---|"
  3023. (should-not
  3024. (org-export-table-row-starts-header-p
  3025. (org-element-map tree 'table-row 'identity info 'first-match)
  3026. info))))
  3027. (ert-deftest test-org-export/table-row-ends-header-p ()
  3028. "Test `org-export-table-row-ends-header-p' specifications."
  3029. ;; 1. Only the row starting the first row group starts the table
  3030. ;; header.
  3031. (org-test-with-parsed-data "
  3032. | a |
  3033. | b |
  3034. |---|
  3035. | c |"
  3036. (should
  3037. (equal
  3038. '(no yes no no)
  3039. (org-element-map tree 'table-row
  3040. (lambda (row)
  3041. (if (org-export-table-row-ends-header-p row info) 'yes 'no))
  3042. info))))
  3043. ;; 2. A row cannot start an header if there's no header in the
  3044. ;; table.
  3045. (org-test-with-parsed-data "
  3046. | a |
  3047. |---|"
  3048. (should-not
  3049. (org-export-table-row-ends-header-p
  3050. (org-element-map tree 'table-row 'identity info 'first-match)
  3051. info))))
  3052. ;;; Tables of Contents
  3053. (ert-deftest test-org-export/collect-headlines ()
  3054. "Test `org-export-collect-headlines' specifications."
  3055. ;; Standard test.
  3056. (should
  3057. (= 2
  3058. (length
  3059. (org-test-with-parsed-data "* H1\n** H2"
  3060. (org-export-collect-headlines info)))))
  3061. ;; Do not collect headlines below optional argument.
  3062. (should
  3063. (= 1
  3064. (length
  3065. (org-test-with-parsed-data "* H1\n** H2"
  3066. (org-export-collect-headlines info 1)))))
  3067. ;; Never collect headlines below maximum headline level.
  3068. (should
  3069. (= 1
  3070. (length
  3071. (org-test-with-parsed-data "#+OPTIONS: H:1\n* H1\n** H2"
  3072. (org-export-collect-headlines info)))))
  3073. (should
  3074. (= 1
  3075. (length
  3076. (org-test-with-parsed-data "#+OPTIONS: H:1\n* H1\n** H2"
  3077. (org-export-collect-headlines info 2)))))
  3078. ;; Collect headlines locally.
  3079. (should
  3080. (= 2
  3081. (org-test-with-parsed-data "* H1\n** H2\n** H3"
  3082. (let ((scope (org-element-map tree 'headline #'identity info t)))
  3083. (length (org-export-collect-headlines info nil scope))))))
  3084. ;; When collecting locally, optional level is relative.
  3085. (should
  3086. (= 1
  3087. (org-test-with-parsed-data "* H1\n** H2\n*** H3"
  3088. (let ((scope (org-element-map tree 'headline #'identity info t)))
  3089. (length (org-export-collect-headlines info 1 scope)))))))
  3090. ;;; Templates
  3091. (ert-deftest test-org-export/inner-template ()
  3092. "Test `inner-template' translator specifications."
  3093. (should
  3094. (equal "Success!"
  3095. (org-test-with-temp-text "* Headline"
  3096. (org-export-as
  3097. (org-export-create-backend
  3098. :transcoders
  3099. '((inner-template . (lambda (contents info) "Success!"))
  3100. (headline . (lambda (h c i) "Headline"))))))))
  3101. ;; Inner template is applied even in a "body-only" export.
  3102. (should
  3103. (equal "Success!"
  3104. (org-test-with-temp-text "* Headline"
  3105. (org-export-as
  3106. (org-export-create-backend
  3107. :transcoders '((inner-template . (lambda (c i) "Success!"))
  3108. (headline . (lambda (h c i) "Headline"))))
  3109. nil nil 'body-only)))))
  3110. (ert-deftest test-org-export/template ()
  3111. "Test `template' translator specifications."
  3112. (should
  3113. (equal "Success!"
  3114. (org-test-with-temp-text "* Headline"
  3115. (org-export-as
  3116. (org-export-create-backend
  3117. :transcoders '((template . (lambda (contents info) "Success!"))
  3118. (headline . (lambda (h c i) "Headline"))))))))
  3119. ;; Template is not applied in a "body-only" export.
  3120. (should-not
  3121. (equal "Success!"
  3122. (org-test-with-temp-text "* Headline"
  3123. (org-export-as
  3124. (org-export-create-backend
  3125. :transcoders '((template . (lambda (contents info) "Success!"))
  3126. (headline . (lambda (h c i) "Headline"))))
  3127. nil nil 'body-only)))))
  3128. ;;; Topology
  3129. (ert-deftest test-org-export/get-next-element ()
  3130. "Test `org-export-get-next-element' specifications."
  3131. ;; Standard test.
  3132. (should
  3133. (equal "b"
  3134. (org-test-with-parsed-data "* Headline\n*a* b"
  3135. (org-export-get-next-element
  3136. (org-element-map tree 'bold 'identity info t) info))))
  3137. ;; Return nil when no previous element.
  3138. (should-not
  3139. (org-test-with-parsed-data "* Headline\na *b*"
  3140. (org-export-get-next-element
  3141. (org-element-map tree 'bold 'identity info t) info)))
  3142. ;; Non-exportable elements are ignored.
  3143. (should-not
  3144. (let ((org-export-with-timestamps nil))
  3145. (org-test-with-parsed-data "\alpha <2012-03-29 Thu>"
  3146. (org-export-get-next-element
  3147. (org-element-map tree 'entity 'identity info t) info))))
  3148. ;; Find next element in secondary strings.
  3149. (should
  3150. (eq 'verbatim
  3151. (org-test-with-parsed-data "* a =verb="
  3152. (org-element-type
  3153. (org-export-get-next-element
  3154. (org-element-map tree 'plain-text 'identity info t) info)))))
  3155. (should
  3156. (eq 'verbatim
  3157. (org-test-with-parsed-data "* /italic/ =verb="
  3158. (org-element-type
  3159. (org-export-get-next-element
  3160. (org-element-map tree 'italic 'identity info t) info)))))
  3161. ;; Find next element in document keywords.
  3162. (should
  3163. (eq 'verbatim
  3164. (org-test-with-parsed-data "#+TITLE: a =verb="
  3165. (org-element-type
  3166. (org-export-get-next-element
  3167. (org-element-map
  3168. (plist-get info :title) 'plain-text 'identity info t) info)))))
  3169. ;; Find next element in parsed affiliated keywords.
  3170. (should
  3171. (eq 'verbatim
  3172. (org-test-with-parsed-data "#+CAPTION: a =verb=\nParagraph"
  3173. (org-element-type
  3174. (org-export-get-next-element
  3175. (org-element-map tree 'plain-text 'identity info t nil t) info)))))
  3176. ;; With optional argument N, return a list containing all the
  3177. ;; following elements.
  3178. (should
  3179. (equal
  3180. '(bold code underline)
  3181. (org-test-with-parsed-data "_a_ /b/ *c* ~d~ _e_"
  3182. (mapcar 'car
  3183. (org-export-get-next-element
  3184. (org-element-map tree 'italic 'identity info t) info t)))))
  3185. ;; When N is a positive integer, return a list containing up to
  3186. ;; N following elements.
  3187. (should
  3188. (equal
  3189. '(bold code)
  3190. (org-test-with-parsed-data "_a_ /b/ *c* ~d~ _e_"
  3191. (mapcar 'car
  3192. (org-export-get-next-element
  3193. (org-element-map tree 'italic 'identity info t) info 2))))))
  3194. (ert-deftest test-org-export/get-previous-element ()
  3195. "Test `org-export-get-previous-element' specifications."
  3196. ;; Standard test.
  3197. (should
  3198. (equal "a "
  3199. (org-test-with-parsed-data "* Headline\na *b*"
  3200. (org-export-get-previous-element
  3201. (org-element-map tree 'bold 'identity info t) info))))
  3202. ;; Return nil when no previous element.
  3203. (should-not
  3204. (org-test-with-parsed-data "* Headline\n*a* b"
  3205. (org-export-get-previous-element
  3206. (org-element-map tree 'bold 'identity info t) info)))
  3207. ;; Non-exportable elements are ignored.
  3208. (should-not
  3209. (let ((org-export-with-timestamps nil))
  3210. (org-test-with-parsed-data "<2012-03-29 Thu> \alpha"
  3211. (org-export-get-previous-element
  3212. (org-element-map tree 'entity 'identity info t) info))))
  3213. ;; Find previous element in secondary strings.
  3214. (should
  3215. (eq 'verbatim
  3216. (org-test-with-parsed-data "* =verb= a"
  3217. (org-element-type
  3218. (org-export-get-previous-element
  3219. (org-element-map tree 'plain-text 'identity info t) info)))))
  3220. (should
  3221. (eq 'verbatim
  3222. (org-test-with-parsed-data "* =verb= /italic/"
  3223. (org-element-type
  3224. (org-export-get-previous-element
  3225. (org-element-map tree 'italic 'identity info t) info)))))
  3226. ;; Find previous element in document keywords.
  3227. (should
  3228. (eq 'verbatim
  3229. (org-test-with-parsed-data "#+TITLE: =verb= a"
  3230. (org-element-type
  3231. (org-export-get-previous-element
  3232. (org-element-map
  3233. (plist-get info :title) 'plain-text 'identity info t) info)))))
  3234. ;; Find previous element in parsed affiliated keywords.
  3235. (should
  3236. (eq 'verbatim
  3237. (org-test-with-parsed-data "#+CAPTION: =verb= a\nParagraph"
  3238. (org-element-type
  3239. (org-export-get-previous-element
  3240. (org-element-map tree 'plain-text 'identity info t nil t) info)))))
  3241. ;; With optional argument N, return a list containing up to
  3242. ;; N previous elements.
  3243. (should
  3244. (equal '(underline italic bold)
  3245. (org-test-with-parsed-data "_a_ /b/ *c* ~d~"
  3246. (mapcar 'car
  3247. (org-export-get-previous-element
  3248. (org-element-map tree 'code 'identity info t) info t)))))
  3249. ;; When N is a positive integer, return a list containing up to
  3250. ;; N previous elements.
  3251. (should
  3252. (equal '(italic bold)
  3253. (org-test-with-parsed-data "_a_ /b/ *c* ~d~"
  3254. (mapcar 'car
  3255. (org-export-get-previous-element
  3256. (org-element-map tree 'code 'identity info t) info 2))))))
  3257. (provide 'test-ox)
  3258. ;;; test-org-export.el end here