test-org-export.el 75 KB

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