test-ox.el 82 KB

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