test-org-export.el 67 KB

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