test-ox.el 92 KB

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