test-ox.el 81 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468
  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. ;; Return nil on "nil" string.
  619. (should
  620. (equal '(:a nil :b nil)
  621. (org-export-read-attribute
  622. :attr_html
  623. (org-test-with-temp-text "#+ATTR_HTML: :a nil :b nil\nParagraph"
  624. (org-element-at-point)))))
  625. ;; Return nil on empty string.
  626. (should
  627. (equal '(:a nil :b nil)
  628. (org-export-read-attribute
  629. :attr_html
  630. (org-test-with-temp-text "#+ATTR_HTML: :a :b\nParagraph"
  631. (org-element-at-point)))))
  632. ;; Ignore text before first property.
  633. (should-not
  634. (member "ignore"
  635. (org-export-read-attribute
  636. :attr_html
  637. (org-test-with-temp-text "#+ATTR_HTML: ignore :a 1\nParagraph"
  638. (org-element-at-point))))))
  639. (ert-deftest test-org-export/get-caption ()
  640. "Test `org-export-get-caption' specifications."
  641. ;; Without optional argument, return long caption
  642. (should
  643. (equal
  644. '("l")
  645. (org-test-with-temp-text "#+CAPTION[s]: l\nPara"
  646. (org-export-get-caption (org-element-at-point)))))
  647. ;; With optional argument, return short caption.
  648. (should
  649. (equal
  650. '("s")
  651. (org-test-with-temp-text "#+CAPTION[s]: l\nPara"
  652. (org-export-get-caption (org-element-at-point) t))))
  653. ;; Multiple lines are separated by white spaces.
  654. (should
  655. (equal
  656. '("a" " " "b")
  657. (org-test-with-temp-text "#+CAPTION: a\n#+CAPTION: b\nPara"
  658. (org-export-get-caption (org-element-at-point))))))
  659. ;;; Back-End Tools
  660. (ert-deftest test-org-export/define-backend ()
  661. "Test back-end definition and accessors."
  662. ;; Translate table.
  663. (should
  664. (equal '((headline . my-headline-test))
  665. (let (org-export-registered-backends)
  666. (org-export-define-backend test ((headline . my-headline-test)))
  667. (org-export-backend-translate-table 'test))))
  668. ;; Filters.
  669. (should
  670. (equal '((:filter-headline . my-filter))
  671. (let (org-export-registered-backends)
  672. (org-export-define-backend test
  673. ((headline . my-headline-test))
  674. :filters-alist ((:filter-headline . my-filter)))
  675. (org-export-backend-filters 'test))))
  676. ;; Options.
  677. (should
  678. (equal '((:prop value))
  679. (let (org-export-registered-backends)
  680. (org-export-define-backend test
  681. ((headline . my-headline-test))
  682. :options-alist ((:prop value)))
  683. (org-export-backend-options 'test))))
  684. ;; Menu.
  685. (should
  686. (equal '(?k "Test Export" test)
  687. (let (org-export-registered-backends)
  688. (org-export-define-backend test
  689. ((headline . my-headline-test))
  690. :menu-entry (?k "Test Export" test))
  691. (org-export-backend-menu 'test))))
  692. ;; Export Blocks.
  693. (should
  694. (equal '(("TEST" . org-element-export-block-parser))
  695. (let (org-export-registered-backends org-element-block-name-alist)
  696. (org-export-define-backend test
  697. ((headline . my-headline-test))
  698. :export-block ("test"))
  699. org-element-block-name-alist))))
  700. (ert-deftest test-org-export/define-derived-backend ()
  701. "Test `org-export-define-derived-backend' specifications."
  702. ;; Error when parent back-end is not defined.
  703. (should-error
  704. (let (org-export-registered-backends)
  705. (org-export-define-derived-backend test parent)))
  706. ;; Append translation table to parent's.
  707. (should
  708. (equal '((:headline . test) (:headline . parent))
  709. (let (org-export-registered-backends)
  710. (org-export-define-backend parent ((:headline . parent)))
  711. (org-export-define-derived-backend test parent
  712. :translate-alist ((:headline . test)))
  713. (org-export-backend-translate-table 'test))))
  714. ;; Options defined in the new back have priority over those defined
  715. ;; in parent.
  716. (should
  717. (eq 'test
  718. (let (org-export-registered-backends)
  719. (org-export-define-backend parent
  720. ((:headline . parent))
  721. :options-alist ((:a nil nil 'parent)))
  722. (org-export-define-derived-backend test parent
  723. :options-alist ((:a nil nil 'test)))
  724. (plist-get (org-export--get-global-options 'test) :a)))))
  725. (ert-deftest test-org-export/derived-backend-p ()
  726. "Test `org-export-derived-backend-p' specifications."
  727. ;; Non-nil with direct match.
  728. (should
  729. (let (org-export-registered-backends)
  730. (org-export-define-backend test ((headline . test)))
  731. (org-export-derived-backend-p 'test 'test)))
  732. (should
  733. (let (org-export-registered-backends)
  734. (org-export-define-backend test ((headline . test)))
  735. (org-export-define-derived-backend test2 test)
  736. (org-export-derived-backend-p 'test2 'test2)))
  737. ;; Non-nil with a direct parent.
  738. (should
  739. (let (org-export-registered-backends)
  740. (org-export-define-backend test ((headline . test)))
  741. (org-export-define-derived-backend test2 test)
  742. (org-export-derived-backend-p 'test2 'test)))
  743. ;; Non-nil with an indirect parent.
  744. (should
  745. (let (org-export-registered-backends)
  746. (org-export-define-backend test ((headline . test)))
  747. (org-export-define-derived-backend test2 test)
  748. (org-export-define-derived-backend test3 test2)
  749. (org-export-derived-backend-p 'test3 'test)))
  750. ;; Nil otherwise.
  751. (should-not
  752. (let (org-export-registered-backends)
  753. (org-export-define-backend test ((headline . test)))
  754. (org-export-define-backend test2 ((headline . test2)))
  755. (org-export-derived-backend-p 'test2 'test)))
  756. (should-not
  757. (let (org-export-registered-backends)
  758. (org-export-define-backend test ((headline . test)))
  759. (org-export-define-backend test2 ((headline . test2)))
  760. (org-export-define-derived-backend test3 test2)
  761. (org-export-derived-backend-p 'test3 'test))))
  762. (ert-deftest test-org-export/with-backend ()
  763. "Test `org-export-with-backend' definition."
  764. ;; Error when calling an undefined back-end
  765. (should-error
  766. (let (org-export-registered-backends)
  767. (org-export-with-backend 'test "Test")))
  768. ;; Error when called back-end doesn't have an appropriate
  769. ;; transcoder.
  770. (should-error
  771. (let (org-export-registered-backends)
  772. (org-export-define-backend test ((headline . ignore)))
  773. (org-export-with-backend 'test "Test")))
  774. ;; Otherwise, export using correct transcoder
  775. (should
  776. (equal "Success"
  777. (let (org-export-registered-backends)
  778. (org-export-define-backend test
  779. ((plain-text . (lambda (text contents info) "Failure"))))
  780. (org-export-define-backend test2
  781. ((plain-text . (lambda (text contents info) "Success"))))
  782. (org-export-with-backend 'test2 "Test")))))
  783. (ert-deftest test-org-export/data-with-translations ()
  784. "Test `org-export-data-with-translations' specifications."
  785. (should
  786. (equal
  787. "Success!"
  788. (org-export-data-with-translations
  789. '(bold nil "Test")
  790. '((plain-text . (lambda (text info) "Success"))
  791. (bold . (lambda (bold contents info) (concat contents "!"))))
  792. '(:with-emphasize t)))))
  793. (ert-deftest test-org-export/data-with-backend ()
  794. "Test `org-export-data-with-backend' specifications."
  795. ;; Error when calling an undefined back-end.
  796. (should-error
  797. (let (org-export-registered-backends)
  798. (org-export-data-with-backend 'test "Test" nil)))
  799. ;; Otherwise, export data recursively, using correct back-end.
  800. (should
  801. (equal
  802. "Success!"
  803. (let (org-export-registered-backends)
  804. (org-export-define-backend test
  805. ((plain-text . (lambda (text info) "Success"))
  806. (bold . (lambda (bold contents info) (concat contents "!")))))
  807. (org-export-data-with-backend
  808. '(bold nil "Test") 'test '(:with-emphasize t))))))
  809. ;;; Export Snippets
  810. (ert-deftest test-org-export/export-snippet ()
  811. "Test export snippets transcoding."
  812. (org-test-with-temp-text "@@test:A@@@@t:B@@"
  813. (org-test-with-backend test
  814. (plist-put
  815. (cdr (assq 'test org-export-registered-backends))
  816. :translate-alist
  817. (cons (cons 'export-snippet
  818. (lambda (snippet contents info)
  819. (when (eq (org-export-snippet-backend snippet) 'test)
  820. (org-element-property :value snippet))))
  821. (org-export-backend-translate-table 'test)))
  822. (let ((org-export-snippet-translation-alist nil))
  823. (should (equal (org-export-as 'test) "A\n")))
  824. (let ((org-export-snippet-translation-alist '(("t" . "test"))))
  825. (should (equal (org-export-as 'test) "AB\n"))))))
  826. ;;; Footnotes
  827. (ert-deftest test-org-export/footnotes ()
  828. "Test footnotes specifications."
  829. (let ((org-footnote-section nil)
  830. (org-export-with-footnotes t))
  831. ;; 1. Read every type of footnote.
  832. (should
  833. (equal
  834. '((1 . "A\n") (2 . "B") (3 . "C") (4 . "D"))
  835. (org-test-with-parsed-data
  836. "Text[fn:1] [1] [fn:label:C] [fn::D]\n\n[fn:1] A\n\n[1] B"
  837. (org-element-map
  838. tree 'footnote-reference
  839. (lambda (ref)
  840. (let ((def (org-export-get-footnote-definition ref info)))
  841. (cons (org-export-get-footnote-number ref info)
  842. (if (eq (org-element-property :type ref) 'inline) (car def)
  843. (car (org-element-contents
  844. (car (org-element-contents def))))))))
  845. info))))
  846. ;; 2. Test nested footnotes order.
  847. (org-test-with-parsed-data
  848. "Text[fn:1:A[fn:2]] [fn:3].\n\n[fn:2] B [fn:3] [fn::D].\n\n[fn:3] C."
  849. (should
  850. (equal
  851. '((1 . "fn:1") (2 . "fn:2") (3 . "fn:3") (4))
  852. (org-element-map
  853. tree 'footnote-reference
  854. (lambda (ref)
  855. (when (org-export-footnote-first-reference-p ref info)
  856. (cons (org-export-get-footnote-number ref info)
  857. (org-element-property :label ref))))
  858. info))))
  859. ;; 3. Test nested footnote in invisible definitions.
  860. (org-test-with-temp-text "Text[1]\n\n[1] B [2]\n\n[2] C."
  861. ;; Hide definitions.
  862. (narrow-to-region (point) (point-at-eol))
  863. (let* ((tree (org-element-parse-buffer))
  864. (info (org-combine-plists
  865. `(:parse-tree ,tree)
  866. (org-export-collect-tree-properties
  867. tree (org-export-get-environment)))))
  868. ;; Both footnotes should be seen.
  869. (should
  870. (= (length (org-export-collect-footnote-definitions tree info)) 2))))
  871. ;; 4. Test footnotes definitions collection.
  872. (org-test-with-parsed-data "Text[fn:1:A[fn:2]] [fn:3].
  873. \[fn:2] B [fn:3] [fn::D].
  874. \[fn:3] C."
  875. (should (= (length (org-export-collect-footnote-definitions tree info))
  876. 4)))
  877. ;; 5. Test export of footnotes defined outside parsing scope.
  878. (org-test-with-temp-text "[fn:1] Out of scope
  879. * Title
  880. Paragraph[fn:1]"
  881. (org-test-with-backend test
  882. (plist-put
  883. (cdr (assq 'test org-export-registered-backends))
  884. :translate-alist
  885. (cons (cons 'footnote-reference
  886. (lambda (fn contents info)
  887. (org-element-interpret-data
  888. (org-export-get-footnote-definition fn info))))
  889. (org-export-backend-translate-table 'test)))
  890. (forward-line)
  891. (should (equal "ParagraphOut of scope\n"
  892. (org-export-as 'test 'subtree)))))
  893. ;; 6. Footnotes without a definition should be provided a fallback
  894. ;; definition.
  895. (should
  896. (org-test-with-parsed-data "[fn:1]"
  897. (org-export-get-footnote-definition
  898. (org-element-map tree 'footnote-reference 'identity info t) info)))
  899. ;; 7. Footnote section should be ignored in TOC and in headlines
  900. ;; numbering.
  901. (should
  902. (= 1 (let ((org-footnote-section "Footnotes"))
  903. (length (org-test-with-parsed-data "* H1\n* Footnotes\n"
  904. (org-export-collect-headlines info))))))
  905. (should
  906. (equal '(2)
  907. (let ((org-footnote-section "Footnotes"))
  908. (org-test-with-parsed-data "* H1\n* Footnotes\n* H2"
  909. (org-element-map tree 'headline
  910. (lambda (hl)
  911. (when (equal (org-element-property :raw-value hl) "H2")
  912. (org-export-get-headline-number hl info)))
  913. info t)))))))
  914. ;;; Headlines and Inlinetasks
  915. (ert-deftest test-org-export/get-relative-level ()
  916. "Test `org-export-get-relative-level' specifications."
  917. ;; Standard test.
  918. (should
  919. (equal '(1 2)
  920. (let ((org-odd-levels-only nil))
  921. (org-test-with-parsed-data "* Headline 1\n** Headline 2"
  922. (org-element-map
  923. tree 'headline
  924. (lambda (h) (org-export-get-relative-level h info))
  925. info)))))
  926. ;; Missing levels
  927. (should
  928. (equal '(1 3)
  929. (let ((org-odd-levels-only nil))
  930. (org-test-with-parsed-data "** Headline 1\n**** Headline 2"
  931. (org-element-map
  932. tree 'headline
  933. (lambda (h) (org-export-get-relative-level h info))
  934. info))))))
  935. (ert-deftest test-org-export/low-level-p ()
  936. "Test `org-export-low-level-p' specifications."
  937. (should
  938. (equal
  939. '(no yes)
  940. (let ((org-odd-levels-only nil))
  941. (org-test-with-parsed-data "* Headline 1\n** Headline 2"
  942. (org-element-map
  943. tree 'headline
  944. (lambda (h) (if (org-export-low-level-p h info) 'yes 'no))
  945. (plist-put info :headline-levels 1)))))))
  946. (ert-deftest test-org-export/get-headline-number ()
  947. "Test `org-export-get-headline-number' specifications."
  948. ;; Standard test.
  949. (should
  950. (equal
  951. '((1) (1 1))
  952. (let ((org-odd-levels-only nil))
  953. (org-test-with-parsed-data "* Headline 1\n** Headline 2"
  954. (org-element-map
  955. tree 'headline
  956. (lambda (h) (org-export-get-headline-number h info))
  957. info)))))
  958. ;; Missing levels are replaced with 0.
  959. (should
  960. (equal
  961. '((1) (1 0 1))
  962. (let ((org-odd-levels-only nil))
  963. (org-test-with-parsed-data "* Headline 1\n*** Headline 2"
  964. (org-element-map
  965. tree 'headline
  966. (lambda (h) (org-export-get-headline-number h info))
  967. info))))))
  968. (ert-deftest test-org-export/numbered-headline-p ()
  969. "Test `org-export-numbered-headline-p' specifications."
  970. ;; If `:section-numbers' is nil, never number headlines.
  971. (should-not
  972. (org-test-with-parsed-data "* Headline"
  973. (org-element-map
  974. tree 'headline
  975. (lambda (h) (org-export-numbered-headline-p h info))
  976. (plist-put info :section-numbers nil))))
  977. ;; If `:section-numbers' is a number, only number headlines with
  978. ;; a level greater that it.
  979. (should
  980. (equal
  981. '(yes no)
  982. (org-test-with-parsed-data "* Headline 1\n** Headline 2"
  983. (org-element-map
  984. tree 'headline
  985. (lambda (h) (if (org-export-numbered-headline-p h info) 'yes 'no))
  986. (plist-put info :section-numbers 1)))))
  987. ;; Otherwise, headlines are always numbered.
  988. (should
  989. (org-test-with-parsed-data "* Headline"
  990. (org-element-map
  991. tree 'headline
  992. (lambda (h) (org-export-numbered-headline-p h info))
  993. (plist-put info :section-numbers t)))))
  994. (ert-deftest test-org-export/number-to-roman ()
  995. "Test `org-export-number-to-roman' specifications."
  996. ;; If number is negative, return it as a string.
  997. (should (equal (org-export-number-to-roman -1) "-1"))
  998. ;; Otherwise, return it as a roman number.
  999. (should (equal (org-export-number-to-roman 1449) "MCDXLIX")))
  1000. (ert-deftest test-org-export/get-optional-title ()
  1001. "Test `org-export-get-alt-title' specifications."
  1002. ;; If ALT_TITLE property is defined, use it.
  1003. (should
  1004. (equal '("opt")
  1005. (org-test-with-parsed-data
  1006. "* Headline\n:PROPERTIES:\n:ALT_TITLE: opt\n:END:"
  1007. (org-export-get-alt-title
  1008. (org-element-map tree 'headline 'identity info t)
  1009. info))))
  1010. ;; Otherwise, fall-back to regular title.
  1011. (should
  1012. (equal '("Headline")
  1013. (org-test-with-parsed-data "* Headline"
  1014. (org-export-get-alt-title
  1015. (org-element-map tree 'headline 'identity info t)
  1016. info)))))
  1017. (ert-deftest test-org-export/get-tags ()
  1018. "Test `org-export-get-tags' specifications."
  1019. (let ((org-export-exclude-tags '("noexport"))
  1020. (org-export-select-tags '("export")))
  1021. ;; Standard test: tags which are not a select tag, an exclude tag,
  1022. ;; or specified as optional argument shouldn't be ignored.
  1023. (should
  1024. (org-test-with-parsed-data "* Headline :tag:"
  1025. (org-export-get-tags (org-element-map tree 'headline 'identity info t)
  1026. info)))
  1027. ;; Exclude tags are removed.
  1028. (should-not
  1029. (org-test-with-parsed-data "* Headline :noexport:"
  1030. (org-export-get-tags (org-element-map tree 'headline 'identity info t)
  1031. info)))
  1032. ;; Select tags are removed.
  1033. (should-not
  1034. (org-test-with-parsed-data "* Headline :export:"
  1035. (org-export-get-tags (org-element-map tree 'headline 'identity info t)
  1036. info)))
  1037. (should
  1038. (equal
  1039. '("tag")
  1040. (org-test-with-parsed-data "* Headline :tag:export:"
  1041. (org-export-get-tags (org-element-map tree 'headline 'identity info t)
  1042. info))))
  1043. ;; Tags provided in the optional argument are also ignored.
  1044. (should-not
  1045. (org-test-with-parsed-data "* Headline :ignore:"
  1046. (org-export-get-tags (org-element-map tree 'headline 'identity info t)
  1047. info '("ignore"))))
  1048. ;; Allow tag inheritance.
  1049. (should
  1050. (equal
  1051. '(("tag") ("tag"))
  1052. (org-test-with-parsed-data "* Headline :tag:\n** Sub-heading"
  1053. (org-element-map
  1054. tree 'headline
  1055. (lambda (hl) (org-export-get-tags hl info nil t)) info))))
  1056. ;; Tag inheritance checks FILETAGS keywords.
  1057. (should
  1058. (equal
  1059. '(("a" "b" "tag"))
  1060. (org-test-with-parsed-data "#+FILETAGS: :a:b:\n* Headline :tag:"
  1061. (org-element-map
  1062. tree 'headline
  1063. (lambda (hl) (org-export-get-tags hl info nil t)) info))))))
  1064. (ert-deftest test-org-export/get-node-property ()
  1065. "Test`org-export-get-node-property' specifications."
  1066. ;; Standard test.
  1067. (should
  1068. (equal "value"
  1069. (org-test-with-parsed-data "* Headline
  1070. :PROPERTIES:
  1071. :prop: value
  1072. :END:"
  1073. (org-export-get-node-property
  1074. :PROP (org-element-map tree 'headline 'identity nil t)))))
  1075. ;; Test inheritance.
  1076. (should
  1077. (equal "value"
  1078. (org-test-with-parsed-data "* Parent
  1079. :PROPERTIES:
  1080. :prop: value
  1081. :END:
  1082. ** Headline
  1083. Paragraph"
  1084. (org-export-get-node-property
  1085. :PROP (org-element-map tree 'paragraph 'identity nil t) t))))
  1086. ;; Cannot return a value before the first headline.
  1087. (should-not
  1088. (org-test-with-parsed-data "Paragraph
  1089. * Headline
  1090. :PROPERTIES:
  1091. :prop: value
  1092. :END:"
  1093. (org-export-get-node-property
  1094. :PROP (org-element-map tree 'paragraph 'identity nil t)))))
  1095. (ert-deftest test-org-export/get-category ()
  1096. "Test `org-export-get-category' specifications."
  1097. ;; Standard test.
  1098. (should
  1099. (equal "value"
  1100. (org-test-with-parsed-data "* Headline
  1101. :PROPERTIES:
  1102. :CATEGORY: value
  1103. :END:"
  1104. (org-export-get-category
  1105. (org-element-map tree 'headline 'identity nil t) info))))
  1106. ;; Test inheritance from a parent headline.
  1107. (should
  1108. (equal '("value" "value")
  1109. (org-test-with-parsed-data "* Headline1
  1110. :PROPERTIES:
  1111. :CATEGORY: value
  1112. :END:
  1113. ** Headline2"
  1114. (org-element-map
  1115. tree 'headline
  1116. (lambda (hl) (org-export-get-category hl info)) info))))
  1117. ;; Test inheritance from #+CATEGORY keyword
  1118. (should
  1119. (equal "value"
  1120. (org-test-with-parsed-data "#+CATEGORY: value
  1121. * Headline"
  1122. (org-export-get-category
  1123. (org-element-map tree 'headline 'identity nil t) info))))
  1124. ;; Test inheritance from file name.
  1125. (should
  1126. (equal "test"
  1127. (org-test-with-parsed-data "* Headline"
  1128. (let ((info (plist-put info :input-file "~/test.org")))
  1129. (org-export-get-category
  1130. (org-element-map tree 'headline 'identity nil t) info)))))
  1131. ;; Fall-back value.
  1132. (should
  1133. (equal "???"
  1134. (org-test-with-parsed-data "* Headline"
  1135. (org-export-get-category
  1136. (org-element-map tree 'headline 'identity nil t) info)))))
  1137. (ert-deftest test-org-export/first-sibling-p ()
  1138. "Test `org-export-first-sibling-p' specifications."
  1139. ;; Standard test.
  1140. (should
  1141. (equal
  1142. '(yes yes no)
  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-first-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 :ignore:\n* Headline 2"
  1154. (org-element-map
  1155. tree 'headline
  1156. (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
  1157. info))))))
  1158. (ert-deftest test-org-export/last-sibling-p ()
  1159. "Test `org-export-last-sibling-p' specifications."
  1160. ;; Standard test.
  1161. (should
  1162. (equal
  1163. '(yes no yes)
  1164. (org-test-with-parsed-data "* Headline\n** Headline 2\n** Headline 3"
  1165. (org-element-map
  1166. tree 'headline
  1167. (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
  1168. info))))
  1169. ;; Ignore headlines not exported.
  1170. (should
  1171. (equal
  1172. '(yes)
  1173. (let ((org-export-exclude-tags '("ignore")))
  1174. (org-test-with-parsed-data "* Headline\n* Headline 2 :ignore:"
  1175. (org-element-map
  1176. tree 'headline
  1177. (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
  1178. info))))))
  1179. (ert-deftest test-org-export/handle-inlinetasks ()
  1180. "Test inlinetask export."
  1181. ;; Inlinetask with an exclude tag.
  1182. (when (featurep 'org-inlinetask)
  1183. (should
  1184. (equal
  1185. ""
  1186. (let ((org-inlinetask-min-level 3))
  1187. (org-test-with-temp-text "*** Inlinetask :noexp:\nContents\n*** end"
  1188. (org-test-with-backend test
  1189. (org-export-as 'test nil nil nil '(:exclude-tags ("noexp"))))))))
  1190. ;; Inlinetask with an include tag.
  1191. (should
  1192. (equal
  1193. "* H2\n*** Inline :exp:\n"
  1194. (let ((org-inlinetask-min-level 3)
  1195. (org-tags-column 0))
  1196. (org-test-with-temp-text "* H1\n* H2\n*** Inline :exp:"
  1197. (org-test-with-backend test
  1198. (org-export-as 'test nil nil nil '(:select-tags ("exp"))))))))
  1199. ;; Ignore inlinetask with a TODO keyword and tasks excluded.
  1200. (should
  1201. (equal ""
  1202. (let ((org-todo-keywords '((sequence "TODO" "DONE")))
  1203. (org-inlinetask-min-level 3))
  1204. (org-test-with-temp-text "*** TODO Inline"
  1205. (org-test-with-backend test
  1206. (org-export-as 'test nil nil nil '(:with-tasks nil)))))))))
  1207. ;;; Links
  1208. (ert-deftest test-org-export/get-coderef-format ()
  1209. "Test `org-export-get-coderef-format' specifications."
  1210. ;; A link without description returns "%s"
  1211. (should (equal (org-export-get-coderef-format "(ref:line)" nil)
  1212. "%s"))
  1213. ;; Return "%s" when path is matched within description.
  1214. (should (equal (org-export-get-coderef-format "path" "desc (path)")
  1215. "desc %s"))
  1216. ;; Otherwise return description.
  1217. (should (equal (org-export-get-coderef-format "path" "desc")
  1218. "desc")))
  1219. (ert-deftest test-org-export/inline-image-p ()
  1220. "Test `org-export-inline-image-p' specifications."
  1221. (should
  1222. (org-export-inline-image-p
  1223. (org-test-with-temp-text "[[#id]]"
  1224. (org-element-map
  1225. (org-element-parse-buffer) 'link 'identity nil t))
  1226. '(("custom-id" . "id")))))
  1227. (ert-deftest test-org-export/fuzzy-link ()
  1228. "Test fuzzy links specifications."
  1229. ;; 1. Links to invisible (keyword) targets should be ignored.
  1230. (org-test-with-parsed-data
  1231. "Paragraph.\n#+TARGET: Test\n[[Test]]"
  1232. (should-not
  1233. (org-element-map
  1234. tree 'link
  1235. (lambda (link)
  1236. (org-export-get-ordinal
  1237. (org-export-resolve-fuzzy-link link info) info)) info)))
  1238. ;; 2. Link to an headline should return headline's number.
  1239. (org-test-with-parsed-data
  1240. "Paragraph.\n* Head1\n* Head2\n* Head3\n[[Head2]]"
  1241. (should
  1242. ;; Note: Headline's number is in fact a list of numbers.
  1243. (equal '(2)
  1244. (org-element-map
  1245. tree 'link
  1246. (lambda (link)
  1247. (org-export-get-ordinal
  1248. (org-export-resolve-fuzzy-link link info) info)) info t))))
  1249. ;; 3. Link to a target in an item should return item's number.
  1250. (org-test-with-parsed-data
  1251. "- Item1\n - Item11\n - <<test>>Item12\n- Item2\n\n\n[[test]]"
  1252. (should
  1253. ;; Note: Item's number is in fact a list of numbers.
  1254. (equal '(1 2)
  1255. (org-element-map
  1256. tree 'link
  1257. (lambda (link)
  1258. (org-export-get-ordinal
  1259. (org-export-resolve-fuzzy-link link info) info)) info t))))
  1260. ;; 4. Link to a target in a footnote should return footnote's
  1261. ;; number.
  1262. (org-test-with-parsed-data "
  1263. Paragraph[1][2][fn:lbl3:C<<target>>][[test]][[target]]\n[1] A\n\n[2] <<test>>B"
  1264. (should
  1265. (equal '(2 3)
  1266. (org-element-map
  1267. tree 'link
  1268. (lambda (link)
  1269. (org-export-get-ordinal
  1270. (org-export-resolve-fuzzy-link link info) info)) info))))
  1271. ;; 5. Link to a named element should return sequence number of that
  1272. ;; element.
  1273. (org-test-with-parsed-data
  1274. "#+NAME: tbl1\n|1|2|\n#+NAME: tbl2\n|3|4|\n#+NAME: tbl3\n|5|6|\n[[tbl2]]"
  1275. (should
  1276. (= 2
  1277. (org-element-map
  1278. tree 'link
  1279. (lambda (link)
  1280. (org-export-get-ordinal
  1281. (org-export-resolve-fuzzy-link link info) info)) info t))))
  1282. ;; 6. Link to a target not within an item, a table, a footnote
  1283. ;; reference or definition should return section number.
  1284. (org-test-with-parsed-data
  1285. "* Head1\n* Head2\nParagraph<<target>>\n* Head3\n[[target]]"
  1286. (should
  1287. (equal '(2)
  1288. (org-element-map
  1289. tree 'link
  1290. (lambda (link)
  1291. (org-export-get-ordinal
  1292. (org-export-resolve-fuzzy-link link info) info)) info t))))
  1293. ;; 7. Space are not significant when matching a fuzzy link.
  1294. (should
  1295. (org-test-with-parsed-data "* Head 1\n[[Head\n 1]]"
  1296. (org-element-map tree 'link
  1297. (lambda (link) (org-export-resolve-fuzzy-link link info))
  1298. info t)))
  1299. ;; 8. Statistics cookies are ignored for headline match.
  1300. (should
  1301. (org-test-with-parsed-data "* Head [0/0]\n[[Head]]"
  1302. (org-element-map tree 'link
  1303. (lambda (link) (org-export-resolve-fuzzy-link link info))
  1304. info t)))
  1305. (should
  1306. (org-test-with-parsed-data "* Head [100%]\n[[Head]]"
  1307. (org-element-map tree 'link
  1308. (lambda (link) (org-export-resolve-fuzzy-link link info))
  1309. info t))))
  1310. (ert-deftest test-org-export/resolve-coderef ()
  1311. "Test `org-export-resolve-coderef' specifications."
  1312. (let ((org-coderef-label-format "(ref:%s)"))
  1313. ;; 1. A link to a "-n -k -r" block returns line number.
  1314. (org-test-with-parsed-data
  1315. "#+BEGIN_EXAMPLE -n -k -r\nText (ref:coderef)\n#+END_EXAMPLE"
  1316. (should (= (org-export-resolve-coderef "coderef" info) 1)))
  1317. (org-test-with-parsed-data
  1318. "#+BEGIN_SRC emacs-lisp -n -k -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  1319. (should (= (org-export-resolve-coderef "coderef" info) 1)))
  1320. ;; 2. A link to a "-n -r" block returns line number.
  1321. (org-test-with-parsed-data
  1322. "#+BEGIN_EXAMPLE -n -r\nText (ref:coderef)\n#+END_EXAMPLE"
  1323. (should (= (org-export-resolve-coderef "coderef" info) 1)))
  1324. (org-test-with-parsed-data
  1325. "#+BEGIN_SRC emacs-lisp -n -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  1326. (should (= (org-export-resolve-coderef "coderef" info) 1)))
  1327. ;; 3. A link to a "-n" block returns coderef.
  1328. (org-test-with-parsed-data
  1329. "#+BEGIN_SRC emacs-lisp -n\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  1330. (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
  1331. (org-test-with-parsed-data
  1332. "#+BEGIN_EXAMPLE -n\nText (ref:coderef)\n#+END_EXAMPLE"
  1333. (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
  1334. ;; 4. A link to a "-r" block returns line number.
  1335. (org-test-with-parsed-data
  1336. "#+BEGIN_SRC emacs-lisp -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  1337. (should (= (org-export-resolve-coderef "coderef" info) 1)))
  1338. (org-test-with-parsed-data
  1339. "#+BEGIN_EXAMPLE -r\nText (ref:coderef)\n#+END_EXAMPLE"
  1340. (should (= (org-export-resolve-coderef "coderef" info) 1)))
  1341. ;; 5. A link to a block without a switch returns coderef.
  1342. (org-test-with-parsed-data
  1343. "#+BEGIN_SRC emacs-lisp\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  1344. (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
  1345. (org-test-with-parsed-data
  1346. "#+BEGIN_EXAMPLE\nText (ref:coderef)\n#+END_EXAMPLE"
  1347. (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
  1348. ;; 6. Correctly handle continued line numbers. A "+n" switch
  1349. ;; should resume numbering from previous block with numbered
  1350. ;; lines, ignoring blocks not numbering lines in the process.
  1351. ;; A "-n" switch resets count.
  1352. (org-test-with-parsed-data "
  1353. #+BEGIN_EXAMPLE -n
  1354. Text.
  1355. #+END_EXAMPLE
  1356. #+BEGIN_SRC emacs-lisp
  1357. \(- 1 1)
  1358. #+END_SRC
  1359. #+BEGIN_SRC emacs-lisp +n -r
  1360. \(+ 1 1) (ref:addition)
  1361. #+END_SRC
  1362. #+BEGIN_EXAMPLE -n -r
  1363. Another text. (ref:text)
  1364. #+END_EXAMPLE"
  1365. (should (= (org-export-resolve-coderef "addition" info) 2))
  1366. (should (= (org-export-resolve-coderef "text" info) 1)))
  1367. ;; 7. Recognize coderef with user-specified syntax.
  1368. (org-test-with-parsed-data
  1369. "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\nText. [ref:text]\n#+END_EXAMPLE"
  1370. (should (equal (org-export-resolve-coderef "text" info) "text")))))
  1371. (ert-deftest test-org-export/resolve-fuzzy-link ()
  1372. "Test `org-export-resolve-fuzzy-link' specifications."
  1373. ;; 1. Match target objects.
  1374. (org-test-with-parsed-data "<<target>> [[target]]"
  1375. (should
  1376. (org-export-resolve-fuzzy-link
  1377. (org-element-map tree 'link 'identity info t) info)))
  1378. ;; 2. Match target elements.
  1379. (org-test-with-parsed-data "#+TARGET: target\n[[target]]"
  1380. (should
  1381. (org-export-resolve-fuzzy-link
  1382. (org-element-map tree 'link 'identity info t) info)))
  1383. ;; 3. Match named elements.
  1384. (org-test-with-parsed-data "#+NAME: target\nParagraph\n\n[[target]]"
  1385. (should
  1386. (org-export-resolve-fuzzy-link
  1387. (org-element-map tree 'link 'identity info t) info)))
  1388. ;; 4. Match exact headline's name.
  1389. (org-test-with-parsed-data "* My headline\n[[My headline]]"
  1390. (should
  1391. (org-export-resolve-fuzzy-link
  1392. (org-element-map tree 'link 'identity info t) info)))
  1393. ;; 5. Targets objects have priority over named elements and headline
  1394. ;; titles.
  1395. (org-test-with-parsed-data
  1396. "* target\n#+NAME: target\n<<target>>\n\n[[target]]"
  1397. (should
  1398. (eq 'target
  1399. (org-element-type
  1400. (org-export-resolve-fuzzy-link
  1401. (org-element-map tree 'link 'identity info t) info)))))
  1402. ;; 6. Named elements have priority over headline titles.
  1403. (org-test-with-parsed-data
  1404. "* target\n#+NAME: target\nParagraph\n\n[[target]]"
  1405. (should
  1406. (eq 'paragraph
  1407. (org-element-type
  1408. (org-export-resolve-fuzzy-link
  1409. (org-element-map tree 'link 'identity info t) info)))))
  1410. ;; 7. If link's path starts with a "*", only match headline titles,
  1411. ;; though.
  1412. (org-test-with-parsed-data
  1413. "* target\n#+NAME: target\n<<target>>\n\n[[*target]]"
  1414. (should
  1415. (eq 'headline
  1416. (org-element-type
  1417. (org-export-resolve-fuzzy-link
  1418. (org-element-map tree 'link 'identity info t) info)))))
  1419. ;; 8. Return nil if no match.
  1420. (org-test-with-parsed-data "[[target]]"
  1421. (should-not
  1422. (org-export-resolve-fuzzy-link
  1423. (org-element-map tree 'link 'identity info t) info))))
  1424. (ert-deftest test-org-export/resolve-id-link ()
  1425. "Test `org-export-resolve-id-link' specifications."
  1426. ;; 1. Regular test for custom-id link.
  1427. (org-test-with-parsed-data "* Headline1
  1428. :PROPERTIES:
  1429. :CUSTOM_ID: test
  1430. :END:
  1431. * Headline 2
  1432. \[[#test]]"
  1433. (should
  1434. (org-export-resolve-id-link
  1435. (org-element-map tree 'link 'identity info t) info)))
  1436. ;; 2. Failing test for custom-id link.
  1437. (org-test-with-parsed-data "* Headline1
  1438. :PROPERTIES:
  1439. :CUSTOM_ID: test
  1440. :END:
  1441. * Headline 2
  1442. \[[#no-match]]"
  1443. (should-not
  1444. (org-export-resolve-id-link
  1445. (org-element-map tree 'link 'identity info t) info)))
  1446. ;; 3. Test for internal id target.
  1447. (org-test-with-parsed-data "* Headline1
  1448. :PROPERTIES:
  1449. :ID: aaaa
  1450. :END:
  1451. * Headline 2
  1452. \[[id:aaaa]]"
  1453. (should
  1454. (org-export-resolve-id-link
  1455. (org-element-map tree 'link 'identity info t) info)))
  1456. ;; 4. Test for external id target.
  1457. (org-test-with-parsed-data "[[id:aaaa]]"
  1458. (should
  1459. (org-export-resolve-id-link
  1460. (org-element-map tree 'link 'identity info t)
  1461. (org-combine-plists info '(:id-alist (("aaaa" . "external-file"))))))))
  1462. (ert-deftest test-org-export/resolve-radio-link ()
  1463. "Test `org-export-resolve-radio-link' specifications."
  1464. ;; Standard test.
  1465. (should
  1466. (org-test-with-temp-text "<<<radio>>> radio"
  1467. (org-update-radio-target-regexp)
  1468. (let* ((tree (org-element-parse-buffer))
  1469. (info `(:parse-tree ,tree)))
  1470. (org-export-resolve-radio-link
  1471. (org-element-map tree 'link 'identity info t)
  1472. info))))
  1473. ;; Radio targets are case-insensitive.
  1474. (should
  1475. (org-test-with-temp-text "<<<RADIO>>> radio"
  1476. (org-update-radio-target-regexp)
  1477. (let* ((tree (org-element-parse-buffer))
  1478. (info `(:parse-tree ,tree)))
  1479. (org-export-resolve-radio-link
  1480. (org-element-map tree 'link 'identity info t)
  1481. info))))
  1482. ;; Radio target with objects.
  1483. (should
  1484. (org-test-with-temp-text "<<<radio \\alpha>>> radio \\alpha"
  1485. (org-update-radio-target-regexp)
  1486. (let* ((tree (org-element-parse-buffer))
  1487. (info `(:parse-tree ,tree)))
  1488. (org-export-resolve-radio-link
  1489. (org-element-map tree 'link 'identity info t)
  1490. info)))))
  1491. ;;; Src-block and example-block
  1492. (ert-deftest test-org-export/unravel-code ()
  1493. "Test `org-export-unravel-code' function."
  1494. (let ((org-coderef-label-format "(ref:%s)"))
  1495. ;; 1. Code without reference.
  1496. (org-test-with-temp-text "#+BEGIN_EXAMPLE\n(+ 1 1)\n#+END_EXAMPLE"
  1497. (should (equal (org-export-unravel-code (org-element-at-point))
  1498. '("(+ 1 1)\n"))))
  1499. ;; 2. Code with reference.
  1500. (org-test-with-temp-text
  1501. "#+BEGIN_EXAMPLE\n(+ 1 1) (ref:test)\n#+END_EXAMPLE"
  1502. (should (equal (org-export-unravel-code (org-element-at-point))
  1503. '("(+ 1 1)\n" (1 . "test")))))
  1504. ;; 3. Code with user-defined reference.
  1505. (org-test-with-temp-text
  1506. "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\n(+ 1 1) [ref:test]\n#+END_EXAMPLE"
  1507. (should (equal (org-export-unravel-code (org-element-at-point))
  1508. '("(+ 1 1)\n" (1 . "test")))))
  1509. ;; 4. Code references keys are relative to the current block.
  1510. (org-test-with-temp-text "
  1511. #+BEGIN_EXAMPLE -n
  1512. \(+ 1 1)
  1513. #+END_EXAMPLE
  1514. #+BEGIN_EXAMPLE +n
  1515. \(+ 2 2)
  1516. \(+ 3 3) (ref:one)
  1517. #+END_EXAMPLE"
  1518. (goto-line 5)
  1519. (should (equal (org-export-unravel-code (org-element-at-point))
  1520. '("(+ 2 2)\n(+ 3 3)\n" (2 . "one")))))))
  1521. (ert-deftest test-org-export/format-code-default ()
  1522. "Test `org-export-format-code-default' specifications."
  1523. ;; Return the empty string when code is empty.
  1524. (should
  1525. (equal ""
  1526. (org-test-with-parsed-data "#+BEGIN_SRC emacs-lisp\n\n\n#+END_SRC"
  1527. (org-export-format-code-default
  1528. (org-element-map tree 'src-block 'identity info t) info))))
  1529. ;; Number lines, two whitespace characters before the actual loc.
  1530. (should
  1531. (equal "1 a\n2 b\n"
  1532. (org-test-with-parsed-data
  1533. "#+BEGIN_SRC emacs-lisp +n\na\nb\n#+END_SRC"
  1534. (org-export-format-code-default
  1535. (org-element-map tree 'src-block 'identity info t) info))))
  1536. ;; Put references 6 whitespace characters after the widest line,
  1537. ;; wrapped within parenthesis.
  1538. (should
  1539. (equal "123 (a)\n1 (b)\n"
  1540. (let ((org-coderef-label-format "(ref:%s)"))
  1541. (org-test-with-parsed-data
  1542. "#+BEGIN_SRC emacs-lisp\n123 (ref:a)\n1 (ref:b)\n#+END_SRC"
  1543. (org-export-format-code-default
  1544. (org-element-map tree 'src-block 'identity info t) info))))))
  1545. ;;; Smart Quotes
  1546. (ert-deftest test-org-export/activate-smart-quotes ()
  1547. "Test `org-export-activate-smart-quotes' specifications."
  1548. ;; Opening double quotes: standard test.
  1549. (should
  1550. (equal
  1551. '("some &ldquo;paragraph")
  1552. (let ((org-export-default-language "en"))
  1553. (org-test-with-parsed-data "some \"paragraph"
  1554. (org-element-map
  1555. tree 'plain-text
  1556. (lambda (s) (org-export-activate-smart-quotes s :html info))
  1557. info)))))
  1558. ;; Opening quotes: at the beginning of a paragraph.
  1559. (should
  1560. (equal
  1561. '("&ldquo;begin")
  1562. (let ((org-export-default-language "en"))
  1563. (org-test-with-parsed-data "\"begin"
  1564. (org-element-map
  1565. tree 'plain-text
  1566. (lambda (s) (org-export-activate-smart-quotes s :html info))
  1567. info)))))
  1568. ;; Opening quotes: after an object.
  1569. (should
  1570. (equal
  1571. '("&ldquo;begin")
  1572. (let ((org-export-default-language "en"))
  1573. (org-test-with-parsed-data "=verb= \"begin"
  1574. (org-element-map
  1575. tree 'plain-text
  1576. (lambda (s) (org-export-activate-smart-quotes s :html info))
  1577. info)))))
  1578. ;; Closing quotes: standard test.
  1579. (should
  1580. (equal
  1581. '("some&rdquo; paragraph")
  1582. (let ((org-export-default-language "en"))
  1583. (org-test-with-parsed-data "some\" paragraph"
  1584. (org-element-map
  1585. tree 'plain-text
  1586. (lambda (s) (org-export-activate-smart-quotes s :html info))
  1587. info)))))
  1588. ;; Closing quotes: at the end of a paragraph.
  1589. (should
  1590. (equal
  1591. '("end&rdquo;")
  1592. (let ((org-export-default-language "en"))
  1593. (org-test-with-parsed-data "end\""
  1594. (org-element-map
  1595. tree 'plain-text
  1596. (lambda (s) (org-export-activate-smart-quotes s :html info))
  1597. info)))))
  1598. ;; Apostrophe: standard test.
  1599. (should
  1600. (equal
  1601. '("It shouldn&rsquo;t fail")
  1602. (let ((org-export-default-language "en"))
  1603. (org-test-with-parsed-data "It shouldn't fail"
  1604. (org-element-map
  1605. tree 'plain-text
  1606. (lambda (s) (org-export-activate-smart-quotes s :html info))
  1607. info)))))
  1608. ;; Apostrophe: before an object.
  1609. (should
  1610. (equal
  1611. '("a&rsquo;")
  1612. (let ((org-export-default-language "en"))
  1613. (org-test-with-parsed-data "a'=b="
  1614. (org-element-map
  1615. tree 'plain-text
  1616. (lambda (s) (org-export-activate-smart-quotes s :html info))
  1617. info)))))
  1618. ;; Apostrophe: after an object.
  1619. (should
  1620. (equal
  1621. '("&rsquo;s")
  1622. (let ((org-export-default-language "en"))
  1623. (org-test-with-parsed-data "=code='s"
  1624. (org-element-map
  1625. tree 'plain-text
  1626. (lambda (s) (org-export-activate-smart-quotes s :html info))
  1627. info)))))
  1628. ;; Special case: isolated quotes.
  1629. (should
  1630. (equal '("&ldquo;" "&rdquo;")
  1631. (let ((org-export-default-language "en"))
  1632. (org-test-with-parsed-data "\"$x$\""
  1633. (org-element-map
  1634. tree 'plain-text
  1635. (lambda (s) (org-export-activate-smart-quotes s :html info))
  1636. info)))))
  1637. ;; Smart quotes in secondary strings.
  1638. (should
  1639. (equal '("&ldquo;" "&rdquo;")
  1640. (let ((org-export-default-language "en"))
  1641. (org-test-with-parsed-data "* \"$x$\""
  1642. (org-element-map
  1643. tree 'plain-text
  1644. (lambda (s) (org-export-activate-smart-quotes s :html info))
  1645. info)))))
  1646. ;; Smart quotes in document keywords.
  1647. (should
  1648. (equal '("&ldquo;" "&rdquo;")
  1649. (let ((org-export-default-language "en"))
  1650. (org-test-with-parsed-data "#+TITLE: \"$x$\""
  1651. (org-element-map
  1652. (plist-get info :title) 'plain-text
  1653. (lambda (s) (org-export-activate-smart-quotes s :html info))
  1654. info)))))
  1655. ;; Smart quotes in parsed affiliated keywords.
  1656. (should
  1657. (equal '("&ldquo;" "&rdquo;" "Paragraph")
  1658. (let ((org-export-default-language "en"))
  1659. (org-test-with-parsed-data "#+CAPTION: \"$x$\"\nParagraph"
  1660. (org-element-map
  1661. tree 'plain-text
  1662. (lambda (s) (org-export-activate-smart-quotes s :html info))
  1663. info nil nil t))))))
  1664. ;;; Tables
  1665. (ert-deftest test-org-export/special-column ()
  1666. "Test if the table's special column is properly recognized."
  1667. ;; 1. First column is special if it contains only a special marking
  1668. ;; characters or empty cells.
  1669. (org-test-with-temp-text "
  1670. | ! | 1 |
  1671. | | 2 |"
  1672. (should
  1673. (org-export-table-has-special-column-p
  1674. (org-element-map
  1675. (org-element-parse-buffer) 'table 'identity nil 'first-match))))
  1676. ;; 2. If the column contains anything else, it isn't special.
  1677. (org-test-with-temp-text "
  1678. | ! | 1 |
  1679. | b | 2 |"
  1680. (should-not
  1681. (org-export-table-has-special-column-p
  1682. (org-element-map
  1683. (org-element-parse-buffer) 'table 'identity nil 'first-match))))
  1684. ;; 3. Special marking characters are "#", "^", "*", "_", "/", "$"
  1685. ;; and "!".
  1686. (org-test-with-temp-text "
  1687. | # | 1 |
  1688. | ^ | 2 |
  1689. | * | 3 |
  1690. | _ | 4 |
  1691. | / | 5 |
  1692. | $ | 6 |
  1693. | ! | 7 |"
  1694. (should
  1695. (org-export-table-has-special-column-p
  1696. (org-element-map
  1697. (org-element-parse-buffer) 'table 'identity nil 'first-match))))
  1698. ;; 4. A first column with only empty cells isn't considered as
  1699. ;; special.
  1700. (org-test-with-temp-text "
  1701. | | 1 |
  1702. | | 2 |"
  1703. (should-not
  1704. (org-export-table-has-special-column-p
  1705. (org-element-map
  1706. (org-element-parse-buffer) 'table 'identity nil 'first-match)))))
  1707. (ert-deftest test-org-export/table-row-is-special-p ()
  1708. "Test `org-export-table-row-is-special-p' specifications."
  1709. ;; 1. A row is special if it has a special marking character in the
  1710. ;; special column.
  1711. (org-test-with-parsed-data "| ! | 1 |"
  1712. (should
  1713. (org-export-table-row-is-special-p
  1714. (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  1715. ;; 2. A row is special when its first field is "/"
  1716. (org-test-with-parsed-data "
  1717. | / | 1 |
  1718. | a | b |"
  1719. (should
  1720. (org-export-table-row-is-special-p
  1721. (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  1722. ;; 3. A row only containing alignment cookies is also considered as
  1723. ;; special.
  1724. (org-test-with-parsed-data "| <5> | | <l> | <l22> |"
  1725. (should
  1726. (org-export-table-row-is-special-p
  1727. (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  1728. ;; 4. Everything else isn't considered as special.
  1729. (org-test-with-parsed-data "| \alpha | | c |"
  1730. (should-not
  1731. (org-export-table-row-is-special-p
  1732. (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  1733. ;; 5. Table's rules are never considered as special rows.
  1734. (org-test-with-parsed-data "|---+---|"
  1735. (should-not
  1736. (org-export-table-row-is-special-p
  1737. (org-element-map tree 'table-row 'identity nil 'first-match) info))))
  1738. (ert-deftest test-org-export/has-header-p ()
  1739. "Test `org-export-table-has-header-p' specifications."
  1740. ;; 1. With an header.
  1741. (org-test-with-parsed-data "
  1742. | a | b |
  1743. |---+---|
  1744. | c | d |"
  1745. (should
  1746. (org-export-table-has-header-p
  1747. (org-element-map tree 'table 'identity info 'first-match)
  1748. info)))
  1749. ;; 2. Without an header.
  1750. (org-test-with-parsed-data "
  1751. | a | b |
  1752. | c | d |"
  1753. (should-not
  1754. (org-export-table-has-header-p
  1755. (org-element-map tree 'table 'identity info 'first-match)
  1756. info)))
  1757. ;; 3. Don't get fooled with starting and ending rules.
  1758. (org-test-with-parsed-data "
  1759. |---+---|
  1760. | a | b |
  1761. | c | d |
  1762. |---+---|"
  1763. (should-not
  1764. (org-export-table-has-header-p
  1765. (org-element-map tree 'table 'identity info 'first-match)
  1766. info))))
  1767. (ert-deftest test-org-export/table-row-group ()
  1768. "Test `org-export-table-row-group' specifications."
  1769. ;; 1. A rule creates a new group.
  1770. (org-test-with-parsed-data "
  1771. | a | b |
  1772. |---+---|
  1773. | 1 | 2 |"
  1774. (should
  1775. (equal
  1776. '(1 nil 2)
  1777. (mapcar (lambda (row) (org-export-table-row-group row info))
  1778. (org-element-map tree 'table-row 'identity)))))
  1779. ;; 2. Special rows are ignored in count.
  1780. (org-test-with-parsed-data "
  1781. | / | < | > |
  1782. |---|---+---|
  1783. | | 1 | 2 |"
  1784. (should
  1785. (equal
  1786. '(nil nil 1)
  1787. (mapcar (lambda (row) (org-export-table-row-group row info))
  1788. (org-element-map tree 'table-row 'identity)))))
  1789. ;; 3. Double rules also are ignored in count.
  1790. (org-test-with-parsed-data "
  1791. | a | b |
  1792. |---+---|
  1793. |---+---|
  1794. | 1 | 2 |"
  1795. (should
  1796. (equal
  1797. '(1 nil nil 2)
  1798. (mapcar (lambda (row) (org-export-table-row-group row info))
  1799. (org-element-map tree 'table-row 'identity))))))
  1800. (ert-deftest test-org-export/table-cell-width ()
  1801. "Test `org-export-table-cell-width' specifications."
  1802. ;; 1. Width is primarily determined by width cookies. If no cookie
  1803. ;; is found, cell's width is nil.
  1804. (org-test-with-parsed-data "
  1805. | / | <l> | <6> | <l7> |
  1806. | | a | b | c |"
  1807. (should
  1808. (equal
  1809. '(nil 6 7)
  1810. (mapcar (lambda (cell) (org-export-table-cell-width cell info))
  1811. (org-element-map tree 'table-cell 'identity info)))))
  1812. ;; 2. The last width cookie has precedence.
  1813. (org-test-with-parsed-data "
  1814. | <6> |
  1815. | <7> |
  1816. | a |"
  1817. (should
  1818. (equal
  1819. '(7)
  1820. (mapcar (lambda (cell) (org-export-table-cell-width cell info))
  1821. (org-element-map tree 'table-cell 'identity info)))))
  1822. ;; 3. Valid width cookies must have a specific row.
  1823. (org-test-with-parsed-data "| <6> | cell |"
  1824. (should
  1825. (equal
  1826. '(nil nil)
  1827. (mapcar (lambda (cell) (org-export-table-cell-width cell info))
  1828. (org-element-map tree 'table-cell 'identity))))))
  1829. (ert-deftest test-org-export/table-cell-alignment ()
  1830. "Test `org-export-table-cell-alignment' specifications."
  1831. (let ((org-table-number-fraction 0.5)
  1832. (org-table-number-regexp "^[0-9]+$"))
  1833. ;; 1. Alignment is primarily determined by alignment cookies.
  1834. (org-test-with-temp-text "| <l> | <c> | <r> |"
  1835. (let* ((tree (org-element-parse-buffer))
  1836. (info `(:parse-tree ,tree)))
  1837. (should
  1838. (equal
  1839. '(left center right)
  1840. (mapcar (lambda (cell) (org-export-table-cell-alignment cell info))
  1841. (org-element-map tree 'table-cell 'identity))))))
  1842. ;; 2. The last alignment cookie has precedence.
  1843. (org-test-with-parsed-data "
  1844. | <l8> |
  1845. | cell |
  1846. | <r9> |"
  1847. (should
  1848. (equal
  1849. '(right right right)
  1850. (mapcar (lambda (cell) (org-export-table-cell-alignment cell info))
  1851. (org-element-map tree 'table-cell 'identity)))))
  1852. ;; 3. If there's no cookie, cell's contents determine alignment.
  1853. ;; A column mostly made of cells containing numbers will align
  1854. ;; its cells to the right.
  1855. (org-test-with-parsed-data "
  1856. | 123 |
  1857. | some text |
  1858. | 12345 |"
  1859. (should
  1860. (equal
  1861. '(right right right)
  1862. (mapcar (lambda (cell)
  1863. (org-export-table-cell-alignment cell info))
  1864. (org-element-map tree 'table-cell 'identity)))))
  1865. ;; 4. Otherwise, they will be aligned to the left.
  1866. (org-test-with-parsed-data "
  1867. | text |
  1868. | some text |
  1869. | \alpha |"
  1870. (should
  1871. (equal
  1872. '(left left left)
  1873. (mapcar (lambda (cell)
  1874. (org-export-table-cell-alignment cell info))
  1875. (org-element-map tree 'table-cell 'identity)))))))
  1876. (ert-deftest test-org-export/table-cell-borders ()
  1877. "Test `org-export-table-cell-borders' specifications."
  1878. ;; 1. Recognize various column groups indicators.
  1879. (org-test-with-parsed-data "| / | < | > | <> |"
  1880. (should
  1881. (equal
  1882. '((right bottom top) (left bottom top) (right bottom top)
  1883. (right left bottom top))
  1884. (mapcar (lambda (cell)
  1885. (org-export-table-cell-borders cell info))
  1886. (org-element-map tree 'table-cell 'identity)))))
  1887. ;; 2. Accept shortcuts to define column groups.
  1888. (org-test-with-parsed-data "| / | < | < |"
  1889. (should
  1890. (equal
  1891. '((right bottom top) (right left bottom top) (left bottom top))
  1892. (mapcar (lambda (cell)
  1893. (org-export-table-cell-borders cell info))
  1894. (org-element-map tree 'table-cell 'identity)))))
  1895. ;; 3. A valid column groups row must start with a "/".
  1896. (org-test-with-parsed-data "
  1897. | | < |
  1898. | a | b |"
  1899. (should
  1900. (equal '((top) (top) (bottom) (bottom))
  1901. (mapcar (lambda (cell)
  1902. (org-export-table-cell-borders cell info))
  1903. (org-element-map tree 'table-cell 'identity)))))
  1904. ;; 4. Take table rules into consideration.
  1905. (org-test-with-parsed-data "
  1906. | 1 |
  1907. |---|
  1908. | 2 |"
  1909. (should
  1910. (equal '((below top) (bottom above))
  1911. (mapcar (lambda (cell)
  1912. (org-export-table-cell-borders cell info))
  1913. (org-element-map tree 'table-cell 'identity)))))
  1914. ;; 5. Top and (resp. bottom) rules induce both `top' and `above'
  1915. ;; (resp. `bottom' and `below') borders. Any special row is
  1916. ;; ignored.
  1917. (org-test-with-parsed-data "
  1918. |---+----|
  1919. | / | |
  1920. | | 1 |
  1921. |---+----|"
  1922. (should
  1923. (equal '((bottom below top above))
  1924. (last
  1925. (mapcar (lambda (cell)
  1926. (org-export-table-cell-borders cell info))
  1927. (org-element-map tree 'table-cell 'identity)))))))
  1928. (ert-deftest test-org-export/table-dimensions ()
  1929. "Test `org-export-table-dimensions' specifications."
  1930. ;; 1. Standard test.
  1931. (org-test-with-parsed-data "
  1932. | 1 | 2 | 3 |
  1933. | 4 | 5 | 6 |"
  1934. (should
  1935. (equal '(2 . 3)
  1936. (org-export-table-dimensions
  1937. (org-element-map tree 'table 'identity info 'first-match) info))))
  1938. ;; 2. Ignore horizontal rules and special columns.
  1939. (org-test-with-parsed-data "
  1940. | / | < | > |
  1941. | 1 | 2 | 3 |
  1942. |---+---+---|
  1943. | 4 | 5 | 6 |"
  1944. (should
  1945. (equal '(2 . 3)
  1946. (org-export-table-dimensions
  1947. (org-element-map tree 'table 'identity info 'first-match) info)))))
  1948. (ert-deftest test-org-export/table-cell-address ()
  1949. "Test `org-export-table-cell-address' specifications."
  1950. ;; 1. Standard test: index is 0-based.
  1951. (org-test-with-parsed-data "| a | b |"
  1952. (should
  1953. (equal '((0 . 0) (0 . 1))
  1954. (org-element-map
  1955. tree 'table-cell
  1956. (lambda (cell) (org-export-table-cell-address cell info))
  1957. info))))
  1958. ;; 2. Special column isn't counted, nor are special rows.
  1959. (org-test-with-parsed-data "
  1960. | / | <> |
  1961. | | c |"
  1962. (should
  1963. (equal '(0 . 0)
  1964. (org-export-table-cell-address
  1965. (car (last (org-element-map tree 'table-cell 'identity info)))
  1966. info))))
  1967. ;; 3. Tables rules do not count either.
  1968. (org-test-with-parsed-data "
  1969. | a |
  1970. |---|
  1971. | b |
  1972. |---|
  1973. | c |"
  1974. (should
  1975. (equal '(2 . 0)
  1976. (org-export-table-cell-address
  1977. (car (last (org-element-map tree 'table-cell 'identity info)))
  1978. info))))
  1979. ;; 4. Return nil for special cells.
  1980. (org-test-with-parsed-data "| / | a |"
  1981. (should-not
  1982. (org-export-table-cell-address
  1983. (org-element-map tree 'table-cell 'identity nil 'first-match)
  1984. info))))
  1985. (ert-deftest test-org-export/get-table-cell-at ()
  1986. "Test `org-export-get-table-cell-at' specifications."
  1987. ;; 1. Address ignores special columns, special rows and rules.
  1988. (org-test-with-parsed-data "
  1989. | / | <> |
  1990. | | a |
  1991. |---+----|
  1992. | | b |"
  1993. (should
  1994. (equal '("b")
  1995. (org-element-contents
  1996. (org-export-get-table-cell-at
  1997. '(1 . 0)
  1998. (org-element-map tree 'table 'identity info 'first-match)
  1999. info)))))
  2000. ;; 2. Return value for a non-existent address is nil.
  2001. (org-test-with-parsed-data "| a |"
  2002. (should-not
  2003. (org-export-get-table-cell-at
  2004. '(2 . 2)
  2005. (org-element-map tree 'table 'identity info 'first-match)
  2006. info)))
  2007. (org-test-with-parsed-data "| / |"
  2008. (should-not
  2009. (org-export-get-table-cell-at
  2010. '(0 . 0)
  2011. (org-element-map tree 'table 'identity info 'first-match)
  2012. info))))
  2013. (ert-deftest test-org-export/table-cell-starts-colgroup-p ()
  2014. "Test `org-export-table-cell-starts-colgroup-p' specifications."
  2015. ;; 1. A cell at a beginning of a row always starts a column group.
  2016. (org-test-with-parsed-data "| a |"
  2017. (should
  2018. (org-export-table-cell-starts-colgroup-p
  2019. (org-element-map tree 'table-cell 'identity info 'first-match)
  2020. info)))
  2021. ;; 2. Special column should be ignored when determining the
  2022. ;; beginning of the row.
  2023. (org-test-with-parsed-data "
  2024. | / | |
  2025. | | a |"
  2026. (should
  2027. (org-export-table-cell-starts-colgroup-p
  2028. (org-element-map tree 'table-cell 'identity info 'first-match)
  2029. info)))
  2030. ;; 2. Explicit column groups.
  2031. (org-test-with-parsed-data "
  2032. | / | | < |
  2033. | a | b | c |"
  2034. (should
  2035. (equal
  2036. '(yes no yes)
  2037. (org-element-map
  2038. tree 'table-cell
  2039. (lambda (cell)
  2040. (if (org-export-table-cell-starts-colgroup-p cell info) 'yes 'no))
  2041. info)))))
  2042. (ert-deftest test-org-export/table-cell-ends-colgroup-p ()
  2043. "Test `org-export-table-cell-ends-colgroup-p' specifications."
  2044. ;; 1. A cell at the end of a row always ends a column group.
  2045. (org-test-with-parsed-data "| a |"
  2046. (should
  2047. (org-export-table-cell-ends-colgroup-p
  2048. (org-element-map tree 'table-cell 'identity info 'first-match)
  2049. info)))
  2050. ;; 2. Special column should be ignored when determining the
  2051. ;; beginning of the row.
  2052. (org-test-with-parsed-data "
  2053. | / | |
  2054. | | a |"
  2055. (should
  2056. (org-export-table-cell-ends-colgroup-p
  2057. (org-element-map tree 'table-cell 'identity info 'first-match)
  2058. info)))
  2059. ;; 3. Explicit column groups.
  2060. (org-test-with-parsed-data "
  2061. | / | < | |
  2062. | a | b | c |"
  2063. (should
  2064. (equal
  2065. '(yes no yes)
  2066. (org-element-map
  2067. tree 'table-cell
  2068. (lambda (cell)
  2069. (if (org-export-table-cell-ends-colgroup-p cell info) 'yes 'no))
  2070. info)))))
  2071. (ert-deftest test-org-export/table-row-starts-rowgroup-p ()
  2072. "Test `org-export-table-row-starts-rowgroup-p' specifications."
  2073. ;; 1. A row at the beginning of a table always starts a row group.
  2074. ;; So does a row following a table rule.
  2075. (org-test-with-parsed-data "
  2076. | a |
  2077. |---|
  2078. | b |"
  2079. (should
  2080. (equal
  2081. '(yes no yes)
  2082. (org-element-map
  2083. tree 'table-row
  2084. (lambda (row)
  2085. (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
  2086. info))))
  2087. ;; 2. Special rows should be ignored when determining the beginning
  2088. ;; of the row.
  2089. (org-test-with-parsed-data "
  2090. | / | < |
  2091. | | a |
  2092. |---+---|
  2093. | / | < |
  2094. | | b |"
  2095. (should
  2096. (equal
  2097. '(yes no yes)
  2098. (org-element-map
  2099. tree 'table-row
  2100. (lambda (row)
  2101. (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
  2102. info)))))
  2103. (ert-deftest test-org-export/table-row-ends-rowgroup-p ()
  2104. "Test `org-export-table-row-ends-rowgroup-p' specifications."
  2105. ;; 1. A row at the end of a table always ends a row group. So does
  2106. ;; a row preceding a table rule.
  2107. (org-test-with-parsed-data "
  2108. | a |
  2109. |---|
  2110. | b |"
  2111. (should
  2112. (equal
  2113. '(yes no yes)
  2114. (org-element-map
  2115. tree 'table-row
  2116. (lambda (row)
  2117. (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
  2118. info))))
  2119. ;; 2. Special rows should be ignored when determining the beginning
  2120. ;; of the row.
  2121. (org-test-with-parsed-data "
  2122. | | a |
  2123. | / | < |
  2124. |---+---|
  2125. | | b |
  2126. | / | < |"
  2127. (should
  2128. (equal
  2129. '(yes no yes)
  2130. (org-element-map
  2131. tree 'table-row
  2132. (lambda (row)
  2133. (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
  2134. info)))))
  2135. (ert-deftest test-org-export/table-row-starts-header-p ()
  2136. "Test `org-export-table-row-starts-header-p' specifications."
  2137. ;; 1. Only the row starting the first row group starts the table
  2138. ;; header.
  2139. (org-test-with-parsed-data "
  2140. | a |
  2141. | b |
  2142. |---|
  2143. | c |"
  2144. (should
  2145. (equal
  2146. '(yes no no no)
  2147. (org-element-map
  2148. tree 'table-row
  2149. (lambda (row)
  2150. (if (org-export-table-row-starts-header-p row info) 'yes 'no))
  2151. info))))
  2152. ;; 2. A row cannot start an header if there's no header in the
  2153. ;; table.
  2154. (org-test-with-parsed-data "
  2155. | a |
  2156. |---|"
  2157. (should-not
  2158. (org-export-table-row-starts-header-p
  2159. (org-element-map tree 'table-row 'identity info 'first-match)
  2160. info))))
  2161. (ert-deftest test-org-export/table-row-ends-header-p ()
  2162. "Test `org-export-table-row-ends-header-p' specifications."
  2163. ;; 1. Only the row starting the first row group starts the table
  2164. ;; header.
  2165. (org-test-with-parsed-data "
  2166. | a |
  2167. | b |
  2168. |---|
  2169. | c |"
  2170. (should
  2171. (equal
  2172. '(no yes no no)
  2173. (org-element-map
  2174. tree 'table-row
  2175. (lambda (row)
  2176. (if (org-export-table-row-ends-header-p row info) 'yes 'no))
  2177. info))))
  2178. ;; 2. A row cannot start an header if there's no header in the
  2179. ;; table.
  2180. (org-test-with-parsed-data "
  2181. | a |
  2182. |---|"
  2183. (should-not
  2184. (org-export-table-row-ends-header-p
  2185. (org-element-map tree 'table-row 'identity info 'first-match)
  2186. info))))
  2187. ;;; Templates
  2188. (ert-deftest test-org-export/inner-template ()
  2189. "Test `inner-template' translator specifications."
  2190. (should
  2191. (equal "Success!"
  2192. (let (org-export-registered-backends)
  2193. (org-export-define-backend test
  2194. ((inner-template . (lambda (contents info) "Success!"))
  2195. (headline . (lambda (h c i) "Headline"))))
  2196. (org-test-with-temp-text "* Headline"
  2197. (org-export-as 'test)))))
  2198. ;; Inner template is applied even in a "body-only" export.
  2199. (should
  2200. (equal "Success!"
  2201. (let (org-export-registered-backends)
  2202. (org-export-define-backend test
  2203. ((inner-template . (lambda (contents info) "Success!"))
  2204. (headline . (lambda (h c i) "Headline"))))
  2205. (org-test-with-temp-text "* Headline"
  2206. (org-export-as 'test nil nil 'body-only))))))
  2207. (ert-deftest test-org-export/template ()
  2208. "Test `template' translator specifications."
  2209. (should
  2210. (equal "Success!"
  2211. (let (org-export-registered-backends)
  2212. (org-export-define-backend test
  2213. ((template . (lambda (contents info) "Success!"))
  2214. (headline . (lambda (h c i) "Headline"))))
  2215. (org-test-with-temp-text "* Headline"
  2216. (org-export-as 'test)))))
  2217. ;; Template is not applied in a "body-only" export.
  2218. (should-not
  2219. (equal "Success!"
  2220. (let (org-export-registered-backends)
  2221. (org-export-define-backend test
  2222. ((template . (lambda (contents info) "Success!"))
  2223. (headline . (lambda (h c i) "Headline"))))
  2224. (org-test-with-temp-text "* Headline"
  2225. (org-export-as 'test nil nil 'body-only))))))
  2226. ;;; Topology
  2227. (ert-deftest test-org-export/get-next-element ()
  2228. "Test `org-export-get-next-element' specifications."
  2229. ;; Standard test.
  2230. (should
  2231. (equal "b"
  2232. (org-test-with-parsed-data "* Headline\n*a* b"
  2233. (org-export-get-next-element
  2234. (org-element-map tree 'bold 'identity info t) info))))
  2235. ;; Return nil when no previous element.
  2236. (should-not
  2237. (org-test-with-parsed-data "* Headline\na *b*"
  2238. (org-export-get-next-element
  2239. (org-element-map tree 'bold 'identity info t) info)))
  2240. ;; Non-exportable elements are ignored.
  2241. (should-not
  2242. (let ((org-export-with-timestamps nil))
  2243. (org-test-with-parsed-data "\alpha <2012-03-29 Thu>"
  2244. (org-export-get-next-element
  2245. (org-element-map tree 'entity 'identity info t) info))))
  2246. ;; Find next element in secondary strings.
  2247. (should
  2248. (eq 'verbatim
  2249. (org-test-with-parsed-data "* a =verb="
  2250. (org-element-type
  2251. (org-export-get-next-element
  2252. (org-element-map tree 'plain-text 'identity info t) info)))))
  2253. ;; Find next element in document keywords.
  2254. (should
  2255. (eq 'verbatim
  2256. (org-test-with-parsed-data "#+TITLE: a =verb="
  2257. (org-element-type
  2258. (org-export-get-next-element
  2259. (org-element-map
  2260. (plist-get info :title) 'plain-text 'identity info t) info)))))
  2261. ;; Find next element in parsed affiliated keywords.
  2262. (should
  2263. (eq 'verbatim
  2264. (org-test-with-parsed-data "#+CAPTION: a =verb=\nParagraph"
  2265. (org-element-type
  2266. (org-export-get-next-element
  2267. (org-element-map tree 'plain-text 'identity info t nil t) info)))))
  2268. ;; With optional argument N, return a list containing all the
  2269. ;; following elements.
  2270. (should
  2271. (equal
  2272. '(bold code underline)
  2273. (org-test-with-parsed-data "_a_ /b/ *c* ~d~ _e_"
  2274. (mapcar 'car
  2275. (org-export-get-next-element
  2276. (org-element-map tree 'italic 'identity info t) info t)))))
  2277. ;; When N is a positive integer, return a list containing up to
  2278. ;; N following elements.
  2279. (should
  2280. (equal
  2281. '(bold code)
  2282. (org-test-with-parsed-data "_a_ /b/ *c* ~d~ _e_"
  2283. (mapcar 'car
  2284. (org-export-get-next-element
  2285. (org-element-map tree 'italic 'identity info t) info 2))))))
  2286. (ert-deftest test-org-export/get-previous-element ()
  2287. "Test `org-export-get-previous-element' specifications."
  2288. ;; Standard test.
  2289. (should
  2290. (equal "a "
  2291. (org-test-with-parsed-data "* Headline\na *b*"
  2292. (org-export-get-previous-element
  2293. (org-element-map tree 'bold 'identity info t) info))))
  2294. ;; Return nil when no previous element.
  2295. (should-not
  2296. (org-test-with-parsed-data "* Headline\n*a* b"
  2297. (org-export-get-previous-element
  2298. (org-element-map tree 'bold 'identity info t) info)))
  2299. ;; Non-exportable elements are ignored.
  2300. (should-not
  2301. (let ((org-export-with-timestamps nil))
  2302. (org-test-with-parsed-data "<2012-03-29 Thu> \alpha"
  2303. (org-export-get-previous-element
  2304. (org-element-map tree 'entity 'identity info t) info))))
  2305. ;; Find previous element in secondary strings.
  2306. (should
  2307. (eq 'verbatim
  2308. (org-test-with-parsed-data "* =verb= a"
  2309. (org-element-type
  2310. (org-export-get-previous-element
  2311. (org-element-map tree 'plain-text 'identity info t) info)))))
  2312. ;; Find previous element in document keywords.
  2313. (should
  2314. (eq 'verbatim
  2315. (org-test-with-parsed-data "#+TITLE: =verb= a"
  2316. (org-element-type
  2317. (org-export-get-previous-element
  2318. (org-element-map
  2319. (plist-get info :title) 'plain-text 'identity info t) info)))))
  2320. ;; Find previous element in parsed affiliated keywords.
  2321. (should
  2322. (eq 'verbatim
  2323. (org-test-with-parsed-data "#+CAPTION: =verb= a\nParagraph"
  2324. (org-element-type
  2325. (org-export-get-previous-element
  2326. (org-element-map tree 'plain-text 'identity info t nil t) info)))))
  2327. ;; With optional argument N, return a list containing up to
  2328. ;; N previous elements.
  2329. (should
  2330. (equal '(underline italic bold)
  2331. (org-test-with-parsed-data "_a_ /b/ *c* ~d~"
  2332. (mapcar 'car
  2333. (org-export-get-previous-element
  2334. (org-element-map tree 'code 'identity info t) info t)))))
  2335. ;; When N is a positive integer, return a list containing up to
  2336. ;; N previous elements.
  2337. (should
  2338. (equal '(italic bold)
  2339. (org-test-with-parsed-data "_a_ /b/ *c* ~d~"
  2340. (mapcar 'car
  2341. (org-export-get-previous-element
  2342. (org-element-map tree 'code 'identity info t) info 2))))))
  2343. (provide 'test-ox)
  2344. ;;; test-org-export.el end here