test-ox.el 106 KB

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