test-ox.el 81 KB

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