test-ox.el 80 KB

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