test-org-export.el 76 KB

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