test-ox.el 79 KB

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