test-org-export.el 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611
  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 ((,(intern (format "org-%s-translate-alist" backend))
  17. ',(let (transcode-table)
  18. (dolist (type (append org-element-all-elements
  19. org-element-all-objects)
  20. transcode-table)
  21. (push
  22. (cons type
  23. (lambda (obj contents info)
  24. (funcall
  25. (intern (format "org-element-%s-interpreter" type))
  26. obj contents)))
  27. transcode-table)))))
  28. (progn ,@body)))
  29. (defmacro org-test-with-parsed-data (data &rest body)
  30. "Execute body with parsed data available.
  31. DATA is a string containing the data to be parsed. BODY is the
  32. body to execute. Parse tree is available under the `tree'
  33. variable, and communication channel under `info'.
  34. This function calls `org-export-collect-tree-properties'. As
  35. such, `:ignore-list' (for `org-element-map') and
  36. `:parse-tree' (for `org-export-get-genealogy') properties are
  37. already filled in `info'."
  38. (declare (debug (form body)) (indent 1))
  39. `(org-test-with-temp-text ,data
  40. (let* ((tree (org-element-parse-buffer))
  41. (info (org-export-collect-tree-properties
  42. tree (org-export-get-environment))))
  43. ,@body)))
  44. ;;; Tests
  45. (ert-deftest test-org-export/parse-option-keyword ()
  46. "Test reading all standard #+OPTIONS: items."
  47. (should
  48. (equal
  49. (org-export--parse-option-keyword
  50. "H:1 num:t \\n:t timestamp:t arch:t author:t creator:t d:t email:t
  51. *:t e:t ::t f:t pri:t -:t ^:t toc:t |:t tags:t tasks:t <:t todo:t inline:nil
  52. stat:t")
  53. '(:headline-levels
  54. 1 :preserve-breaks t :section-numbers t :time-stamp-file t
  55. :with-archived-trees t :with-author t :with-creator t :with-drawers t
  56. :with-email t :with-emphasize t :with-entities t :with-fixed-width t
  57. :with-footnotes t :with-inlinetasks nil :with-priority t
  58. :with-special-strings t :with-statistics-cookies t :with-sub-superscript t
  59. :with-toc t :with-tables t :with-tags t :with-tasks t :with-timestamps t
  60. :with-todo-keywords t)))
  61. ;; Test some special values.
  62. (should
  63. (equal
  64. (org-export--parse-option-keyword
  65. "arch:headline creator:comment d:(\"TEST\")
  66. ^:{} toc:1 tags:not-in-toc tasks:todo num:2 <:active")
  67. '( :section-numbers
  68. 2
  69. :with-archived-trees headline :with-creator comment
  70. :with-drawers ("TEST") :with-sub-superscript {} :with-toc 1
  71. :with-tags not-in-toc :with-tasks todo :with-timestamps active))))
  72. (ert-deftest test-org-export/get-inbuffer-options ()
  73. "Test reading all standard export keywords."
  74. (should
  75. (equal
  76. (org-test-with-temp-text "#+AUTHOR: Me, Myself and I
  77. #+CREATOR: Idem
  78. #+DATE: Today
  79. #+DESCRIPTION: Testing
  80. #+DESCRIPTION: with two lines
  81. #+EMAIL: some@email.org
  82. #+EXCLUDE_TAGS: noexport invisible
  83. #+KEYWORDS: test
  84. #+LANGUAGE: en
  85. #+SELECT_TAGS: export
  86. #+TITLE: Some title
  87. #+TITLE: with spaces"
  88. (org-export--get-inbuffer-options))
  89. '(:author
  90. ("Me, Myself and I") :creator "Idem" :date ("Today")
  91. :description "Testing\nwith two lines" :email "some@email.org"
  92. :exclude-tags ("noexport" "invisible") :keywords "test" :language "en"
  93. :select-tags ("export") :title ("Some title with spaces")))))
  94. (ert-deftest test-org-export/get-subtree-options ()
  95. "Test setting options from headline's properties."
  96. ;; EXPORT_TITLE.
  97. (org-test-with-temp-text "#+TITLE: Title
  98. * Headline
  99. :PROPERTIES:
  100. :EXPORT_TITLE: Subtree Title
  101. :END:
  102. Paragraph"
  103. (forward-line)
  104. (should (equal (plist-get (org-export-get-environment nil t) :title)
  105. '("Subtree Title"))))
  106. :title
  107. '("subtree-title")
  108. ;; EXPORT_OPTIONS.
  109. (org-test-with-temp-text "#+OPTIONS: H:1
  110. * Headline
  111. :PROPERTIES:
  112. :EXPORT_OPTIONS: H:2
  113. :END:
  114. Paragraph"
  115. (forward-line)
  116. (should
  117. (= 2 (plist-get (org-export-get-environment nil t) :headline-levels))))
  118. ;; EXPORT_DATE.
  119. (org-test-with-temp-text "#+DATE: today
  120. * Headline
  121. :PROPERTIES:
  122. :EXPORT_DATE: 29-03-2012
  123. :END:
  124. Paragraph"
  125. (forward-line)
  126. (should (equal (plist-get (org-export-get-environment nil t) :date)
  127. '("29-03-2012"))))
  128. ;; Export properties are case-insensitive.
  129. (org-test-with-temp-text "* Headline
  130. :PROPERTIES:
  131. :EXPORT_Date: 29-03-2012
  132. :END:
  133. Paragraph"
  134. (should (equal (plist-get (org-export-get-environment nil t) :date)
  135. '("29-03-2012")))))
  136. (ert-deftest test-org-export/handle-options ()
  137. "Test if export options have an impact on output."
  138. ;; Test exclude tags.
  139. (org-test-with-temp-text "* Head1 :noexport:"
  140. (org-test-with-backend test
  141. (should
  142. (equal (org-export-as 'test nil nil nil '(:exclude-tags ("noexport")))
  143. ""))))
  144. ;; Test include tags.
  145. (org-test-with-temp-text "
  146. * Head1
  147. * Head2
  148. ** Sub-Head2.1 :export:
  149. *** Sub-Head2.1.1
  150. * Head2"
  151. (org-test-with-backend test
  152. (should
  153. (equal
  154. "* Head2\n** Sub-Head2.1 :export:\n*** Sub-Head2.1.1\n"
  155. (let ((org-tags-column 0))
  156. (org-export-as 'test nil nil nil '(:select-tags ("export"))))))))
  157. ;; Test mixing include tags and exclude tags.
  158. (org-test-with-temp-text "
  159. * Head1 :export:
  160. ** Sub-Head1 :noexport:
  161. ** Sub-Head2
  162. * Head2 :noexport:
  163. ** Sub-Head1 :export:"
  164. (org-test-with-backend test
  165. (should
  166. (string-match
  167. "\\* Head1[ \t]+:export:\n\\*\\* Sub-Head2\n"
  168. (org-export-as
  169. 'test nil nil nil
  170. '(:select-tags ("export") :exclude-tags ("noexport")))))))
  171. ;; Ignore tasks.
  172. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  173. (org-test-with-temp-text "* TODO Head1"
  174. (org-test-with-backend test
  175. (should (equal (org-export-as 'test nil nil nil '(:with-tasks nil))
  176. "")))))
  177. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  178. (org-test-with-temp-text "* TODO Head1"
  179. (org-test-with-backend test
  180. (should (equal (org-export-as 'test nil nil nil '(:with-tasks t))
  181. "* TODO Head1\n")))))
  182. ;; Archived tree.
  183. (org-test-with-temp-text "* Head1 :archive:"
  184. (let ((org-archive-tag "archive"))
  185. (org-test-with-backend test
  186. (should
  187. (equal (org-export-as 'test nil nil nil '(:with-archived-trees nil))
  188. "")))))
  189. (org-test-with-temp-text "* Head1 :archive:\nbody\n** Sub-head 2"
  190. (let ((org-archive-tag "archive"))
  191. (org-test-with-backend test
  192. (should
  193. (string-match
  194. "\\* Head1[ \t]+:archive:"
  195. (org-export-as 'test nil nil nil
  196. '(:with-archived-trees headline)))))))
  197. (org-test-with-temp-text "* Head1 :archive:"
  198. (let ((org-archive-tag "archive"))
  199. (org-test-with-backend test
  200. (should
  201. (string-match
  202. "\\`\\* Head1[ \t]+:archive:\n\\'"
  203. (org-export-as 'test nil nil nil '(:with-archived-trees t)))))))
  204. ;; Drawers.
  205. (let ((org-drawers '("TEST")))
  206. (org-test-with-temp-text ":TEST:\ncontents\n:END:"
  207. (org-test-with-backend test
  208. (should (equal (org-export-as 'test nil nil nil '(:with-drawers nil))
  209. ""))
  210. (should (equal (org-export-as 'test nil nil nil '(:with-drawers t))
  211. ":TEST:\ncontents\n:END:\n")))))
  212. (let ((org-drawers '("FOO" "BAR")))
  213. (org-test-with-temp-text ":FOO:\nkeep\n:END:\n:BAR:\nremove\n:END:"
  214. (org-test-with-backend test
  215. (should
  216. (equal (org-export-as 'test nil nil nil '(:with-drawers ("FOO")))
  217. ":FOO:\nkeep\n:END:\n")))))
  218. ;; Timestamps.
  219. (org-test-with-temp-text "[2012-04-29 sun. 10:45]<2012-04-29 sun. 10:45>"
  220. (org-test-with-backend test
  221. (should
  222. (equal (org-export-as 'test nil nil nil '(:with-timestamps t))
  223. "[2012-04-29 sun. 10:45]<2012-04-29 sun. 10:45>\n"))
  224. (should
  225. (equal (org-export-as 'test nil nil nil '(:with-timestamps nil)) ""))
  226. (should
  227. (equal (org-export-as 'test nil nil nil '(:with-timestamps active))
  228. "<2012-04-29 sun. 10:45>\n"))
  229. (should
  230. (equal (org-export-as 'test nil nil nil '(:with-timestamps inactive))
  231. "[2012-04-29 sun. 10:45]\n"))))
  232. ;; Clocks.
  233. (let ((org-clock-string "CLOCK:"))
  234. (org-test-with-temp-text "CLOCK: [2012-04-29 sun. 10:45]"
  235. (org-test-with-backend test
  236. (should
  237. (equal (org-export-as 'test nil nil nil '(:with-clocks t))
  238. "CLOCK: [2012-04-29 sun. 10:45]\n"))
  239. (should
  240. (equal (org-export-as 'test nil nil nil '(:with-clocks nil)) "")))))
  241. ;; Plannings.
  242. (let ((org-closed-string "CLOSED:"))
  243. (org-test-with-temp-text "CLOSED: [2012-04-29 sun. 10:45]"
  244. (org-test-with-backend test
  245. (should
  246. (equal (org-export-as 'test nil nil nil '(:with-plannings t))
  247. "CLOSED: [2012-04-29 sun. 10:45]\n"))
  248. (should
  249. (equal (org-export-as 'test nil nil nil '(:with-plannings nil))
  250. "")))))
  251. ;; Inlinetasks.
  252. (when (featurep 'org-inlinetask)
  253. (should
  254. (equal
  255. (let ((org-inlinetask-min-level 15))
  256. (org-test-with-temp-text "*************** Task"
  257. (org-test-with-backend test
  258. (org-export-as 'test nil nil nil '(:with-inlinetasks nil)))))
  259. ""))
  260. (should
  261. (equal
  262. (let ((org-inlinetask-min-level 15))
  263. (org-test-with-temp-text
  264. "*************** Task\nContents\n*************** END"
  265. (org-test-with-backend test
  266. (org-export-as 'test nil nil nil '(:with-inlinetasks nil)))))
  267. "")))
  268. ;; Statistics cookies.
  269. (should
  270. (equal ""
  271. (org-test-with-temp-text "[0/0]"
  272. (org-test-with-backend test
  273. (org-export-as
  274. 'test nil nil nil '(:with-statistics-cookies nil)))))))
  275. (ert-deftest test-org-export/comment-tree ()
  276. "Test if export process ignores commented trees."
  277. (let ((org-comment-string "COMMENT"))
  278. (org-test-with-temp-text "* COMMENT Head1"
  279. (org-test-with-backend test
  280. (should (equal (org-export-as 'test) ""))))))
  281. (ert-deftest test-org-export/export-scope ()
  282. "Test all export scopes."
  283. (org-test-with-temp-text "
  284. * Head1
  285. ** Head2
  286. text
  287. *** Head3"
  288. (org-test-with-backend test
  289. ;; Subtree.
  290. (forward-line 3)
  291. (should (equal (org-export-as 'test 'subtree) "text\n*** Head3\n"))
  292. ;; Visible.
  293. (goto-char (point-min))
  294. (forward-line)
  295. (org-cycle)
  296. (should (equal (org-export-as 'test nil 'visible) "* Head1\n"))
  297. ;; Body only.
  298. (flet ((org-test-template (body info) (format "BEGIN\n%sEND" body)))
  299. (push '(template . org-test-template) org-test-translate-alist)
  300. (should (equal (org-export-as 'test nil nil 'body-only)
  301. "* Head1\n** Head2\ntext\n*** Head3\n"))
  302. (should (equal (org-export-as 'test)
  303. "BEGIN\n* Head1\n** Head2\ntext\n*** Head3\nEND")))
  304. ;; Region.
  305. (goto-char (point-min))
  306. (forward-line 3)
  307. (transient-mark-mode 1)
  308. (push-mark (point) t t)
  309. (goto-char (point-at-eol))
  310. (should (equal (org-export-as 'test) "text\n"))))
  311. ;; Subtree with a code block calling another block outside.
  312. (org-test-with-temp-text "
  313. * Head1
  314. #+BEGIN_SRC emacs-lisp :noweb yes :exports results
  315. <<test>>
  316. #+END_SRC
  317. * Head2
  318. #+NAME: test
  319. #+BEGIN_SRC emacs-lisp
  320. \(+ 1 2)
  321. #+END_SRC"
  322. (org-test-with-backend test
  323. (forward-line 1)
  324. (should (equal (org-export-as 'test 'subtree) ": 3\n")))))
  325. (ert-deftest test-org-export/expand-include ()
  326. "Test file inclusion in an Org buffer."
  327. ;; Full insertion with recursive inclusion.
  328. (org-test-with-temp-text
  329. (format "#+INCLUDE: \"%s/examples/include.org\"" org-test-dir)
  330. (org-export-expand-include-keyword)
  331. (should (equal (buffer-string)
  332. "Small Org file with an include keyword.
  333. #+BEGIN_SRC emacs-lisp :exports results\n(+ 2 1)\n#+END_SRC
  334. Success!
  335. * Heading
  336. body\n")))
  337. ;; Localized insertion.
  338. (org-test-with-temp-text
  339. (format "#+INCLUDE: \"%s/examples/include.org\" :lines \"1-2\""
  340. org-test-dir)
  341. (org-export-expand-include-keyword)
  342. (should (equal (buffer-string)
  343. "Small Org file with an include keyword.\n")))
  344. ;; Insertion with constraints on headlines level.
  345. (org-test-with-temp-text
  346. (format
  347. "* Top heading\n#+INCLUDE: \"%s/examples/include.org\" :lines \"9-\""
  348. org-test-dir)
  349. (org-export-expand-include-keyword)
  350. (should (equal (buffer-string) "* Top heading\n** Heading\nbody\n")))
  351. ;; Inclusion within an example block.
  352. (org-test-with-temp-text
  353. (format "#+INCLUDE: \"%s/examples/include.org\" :lines \"1-2\" example"
  354. org-test-dir)
  355. (org-export-expand-include-keyword)
  356. (should
  357. (equal
  358. (buffer-string)
  359. "#+BEGIN_EXAMPLE\nSmall Org file with an include keyword.\n#+END_EXAMPLE\n")))
  360. ;; Inclusion within a src-block.
  361. (org-test-with-temp-text
  362. (format
  363. "#+INCLUDE: \"%s/examples/include.org\" :lines \"4-5\" src emacs-lisp"
  364. org-test-dir)
  365. (org-export-expand-include-keyword)
  366. (should (equal (buffer-string)
  367. "#+BEGIN_SRC emacs-lisp\n(+ 2 1)\n#+END_SRC\n"))))
  368. (ert-deftest test-org-export/user-ignore-list ()
  369. "Test if `:ignore-list' accepts user input."
  370. (org-test-with-backend test
  371. (flet ((skip-note-head
  372. (data backend info)
  373. ;; Ignore headlines with the word "note" in their title.
  374. (org-element-map
  375. data 'headline
  376. (lambda (headline)
  377. (when (string-match "\\<note\\>"
  378. (org-element-property :raw-value headline))
  379. (org-export-ignore-element headline info)))
  380. info)
  381. data))
  382. ;; Install function in parse tree filters.
  383. (let ((org-export-filter-parse-tree-functions '(skip-note-head)))
  384. (org-test-with-temp-text "* Head1\n* Head2 (note)\n"
  385. (should (equal (org-export-as 'test) "* Head1\n")))))))
  386. (ert-deftest test-org-export/before-parsing-hook ()
  387. "Test `org-export-before-parsing-hook'."
  388. (org-test-with-backend test
  389. (org-test-with-temp-text "* Headline 1\nBody 1\n* Headline 2\nBody 2"
  390. (let ((org-export-before-parsing-hook
  391. '((lambda (backend)
  392. (org-map-entries
  393. (lambda ()
  394. (delete-region (point) (progn (forward-line) (point)))))))))
  395. (should (equal (org-export-as 'test) "Body 1\nBody 2\n"))))))
  396. ;;; Affiliated Keywords
  397. (ert-deftest test-org-export/read-attribute ()
  398. "Test `org-export-read-attribute' specifications."
  399. ;; Standard test.
  400. (should
  401. (equal
  402. (org-export-read-attribute
  403. :attr_html
  404. (org-test-with-temp-text "#+ATTR_HTML: :a 1 :b 2\nParagraph"
  405. (org-element-at-point)))
  406. '(:a 1 :b 2)))
  407. ;; Return nil on empty attribute.
  408. (should-not
  409. (org-export-read-attribute
  410. :attr_html
  411. (org-test-with-temp-text "Paragraph" (org-element-at-point)))))
  412. ;;; Export Snippets
  413. (ert-deftest test-org-export/export-snippet ()
  414. "Test export snippets transcoding."
  415. (org-test-with-temp-text "@@test:A@@@@t:B@@"
  416. (org-test-with-backend test
  417. (let ((org-test-translate-alist
  418. (cons (cons 'export-snippet
  419. (lambda (snippet contents info)
  420. (when (eq (org-export-snippet-backend snippet) 'test)
  421. (org-element-property :value snippet))))
  422. org-test-translate-alist)))
  423. (let ((org-export-snippet-translation-alist nil))
  424. (should (equal (org-export-as 'test) "A\n")))
  425. (let ((org-export-snippet-translation-alist '(("t" . "test"))))
  426. (should (equal (org-export-as 'test) "AB\n")))))))
  427. ;;; Footnotes
  428. (ert-deftest test-org-export/footnotes ()
  429. "Test footnotes specifications."
  430. (let ((org-footnote-section nil)
  431. (org-export-with-footnotes t))
  432. ;; 1. Read every type of footnote.
  433. (should
  434. (equal
  435. '((1 . "A\n") (2 . "B") (3 . "C") (4 . "D"))
  436. (org-test-with-parsed-data
  437. "Text[fn:1] [1] [fn:label:C] [fn::D]\n\n[fn:1] A\n\n[1] B"
  438. (org-element-map
  439. tree 'footnote-reference
  440. (lambda (ref)
  441. (let ((def (org-export-get-footnote-definition ref info)))
  442. (cons (org-export-get-footnote-number ref info)
  443. (if (eq (org-element-property :type ref) 'inline) (car def)
  444. (car (org-element-contents
  445. (car (org-element-contents def))))))))
  446. info))))
  447. ;; 2. Test nested footnotes order.
  448. (org-test-with-parsed-data
  449. "Text[fn:1:A[fn:2]] [fn:3].\n\n[fn:2] B [fn:3] [fn::D].\n\n[fn:3] C."
  450. (should
  451. (equal
  452. '((1 . "fn:1") (2 . "fn:2") (3 . "fn:3") (4))
  453. (org-element-map
  454. tree 'footnote-reference
  455. (lambda (ref)
  456. (when (org-export-footnote-first-reference-p ref info)
  457. (cons (org-export-get-footnote-number ref info)
  458. (org-element-property :label ref))))
  459. info))))
  460. ;; 3. Test nested footnote in invisible definitions.
  461. (org-test-with-temp-text "Text[1]\n\n[1] B [2]\n\n[2] C."
  462. ;; Hide definitions.
  463. (narrow-to-region (point) (point-at-eol))
  464. (let* ((tree (org-element-parse-buffer))
  465. (info (org-combine-plists
  466. `(:parse-tree ,tree)
  467. (org-export-collect-tree-properties
  468. tree (org-export-get-environment)))))
  469. ;; Both footnotes should be seen.
  470. (should
  471. (= (length (org-export-collect-footnote-definitions tree info)) 2))))
  472. ;; 4. Test footnotes definitions collection.
  473. (org-test-with-parsed-data "Text[fn:1:A[fn:2]] [fn:3].
  474. \[fn:2] B [fn:3] [fn::D].
  475. \[fn:3] C."
  476. (should (= (length (org-export-collect-footnote-definitions tree info))
  477. 4)))
  478. ;; 5. Test export of footnotes defined outside parsing scope.
  479. (org-test-with-temp-text "[fn:1] Out of scope
  480. * Title
  481. Paragraph[fn:1]"
  482. (org-test-with-backend test
  483. (let ((org-test-translate-alist
  484. (cons (cons 'footnote-reference
  485. (lambda (fn contents info)
  486. (org-element-interpret-data
  487. (org-export-get-footnote-definition fn info))))
  488. org-test-translate-alist)))
  489. (forward-line)
  490. (should (equal "ParagraphOut of scope\n"
  491. (org-export-as 'test 'subtree))))))))
  492. ;;; Headlines and Inlinetasks
  493. (ert-deftest test-org-export/get-relative-level ()
  494. "Test `org-export-get-relative-level' specifications."
  495. ;; Standard test.
  496. (should
  497. (equal '(1 2)
  498. (let ((org-odd-levels-only nil))
  499. (org-test-with-parsed-data "* Headline 1\n** Headline 2"
  500. (org-element-map
  501. tree 'headline
  502. (lambda (h) (org-export-get-relative-level h info))
  503. info)))))
  504. ;; Missing levels
  505. (should
  506. (equal '(1 3)
  507. (let ((org-odd-levels-only nil))
  508. (org-test-with-parsed-data "** Headline 1\n**** Headline 2"
  509. (org-element-map
  510. tree 'headline
  511. (lambda (h) (org-export-get-relative-level h info))
  512. info))))))
  513. (ert-deftest test-org-export/low-level-p ()
  514. "Test `org-export-low-level-p' specifications."
  515. (should
  516. (equal
  517. '(no yes)
  518. (let ((org-odd-levels-only nil))
  519. (org-test-with-parsed-data "* Headline 1\n** Headline 2"
  520. (org-element-map
  521. tree 'headline
  522. (lambda (h) (if (org-export-low-level-p h info) 'yes 'no))
  523. (plist-put info :headline-levels 1)))))))
  524. (ert-deftest test-org-export/get-headline-number ()
  525. "Test `org-export-get-headline-number' specifications."
  526. ;; Standard test.
  527. (should
  528. (equal
  529. '((1) (1 1))
  530. (let ((org-odd-levels-only nil))
  531. (org-test-with-parsed-data "* Headline 1\n** Headline 2"
  532. (org-element-map
  533. tree 'headline
  534. (lambda (h) (org-export-get-headline-number h info))
  535. info)))))
  536. ;; Missing levels are replaced with 0.
  537. (should
  538. (equal
  539. '((1) (1 0 1))
  540. (let ((org-odd-levels-only nil))
  541. (org-test-with-parsed-data "* Headline 1\n*** Headline 2"
  542. (org-element-map
  543. tree 'headline
  544. (lambda (h) (org-export-get-headline-number h info))
  545. info))))))
  546. (ert-deftest test-org-export/numbered-headline-p ()
  547. "Test `org-export-numbered-headline-p' specifications."
  548. ;; If `:section-numbers' is nil, never number headlines.
  549. (should-not
  550. (org-test-with-parsed-data "* Headline"
  551. (org-element-map
  552. tree 'headline
  553. (lambda (h) (org-export-numbered-headline-p h info))
  554. (plist-put info :section-numbers nil))))
  555. ;; If `:section-numbers' is a number, only number headlines with
  556. ;; a level greater that it.
  557. (should
  558. (equal
  559. '(yes no)
  560. (org-test-with-parsed-data "* Headline 1\n** Headline 2"
  561. (org-element-map
  562. tree 'headline
  563. (lambda (h) (if (org-export-numbered-headline-p h info) 'yes 'no))
  564. (plist-put info :section-numbers 1)))))
  565. ;; Otherwise, headlines are always numbered.
  566. (should
  567. (org-test-with-parsed-data "* Headline"
  568. (org-element-map
  569. tree 'headline
  570. (lambda (h) (org-export-numbered-headline-p h info))
  571. (plist-put info :section-numbers t)))))
  572. (ert-deftest test-org-export/number-to-roman ()
  573. "Test `org-export-number-to-roman' specifications."
  574. ;; If number is negative, return it as a string.
  575. (should (equal (org-export-number-to-roman -1) "-1"))
  576. ;; Otherwise, return it as a roman number.
  577. (should (equal (org-export-number-to-roman 1449) "MCDXLIX")))
  578. (ert-deftest test-org-export/get-tags ()
  579. "Test `org-export-get-tags' specifications."
  580. (let ((org-export-exclude-tags '("noexport"))
  581. (org-export-select-tags '("export")))
  582. ;; Standard test: tags which are not a select tag, an exclude tag,
  583. ;; or specified as optional argument shouldn't be ignored.
  584. (should
  585. (org-test-with-parsed-data "* Headline :tag:"
  586. (org-export-get-tags (org-element-map tree 'headline 'identity info t)
  587. info)))
  588. ;; Exclude tags are removed.
  589. (should-not
  590. (org-test-with-parsed-data "* Headline :noexport:"
  591. (org-export-get-tags (org-element-map tree 'headline 'identity info t)
  592. info)))
  593. ;; Select tags are removed.
  594. (should-not
  595. (org-test-with-parsed-data "* Headline :export:"
  596. (org-export-get-tags (org-element-map tree 'headline 'identity info t)
  597. info)))
  598. (should
  599. (equal
  600. '("tag")
  601. (org-test-with-parsed-data "* Headline :tag:export:"
  602. (org-export-get-tags (org-element-map tree 'headline 'identity info t)
  603. info))))
  604. ;; Tags provided in the optional argument are also ignored.
  605. (should-not
  606. (org-test-with-parsed-data "* Headline :ignore:"
  607. (org-export-get-tags (org-element-map tree 'headline 'identity info t)
  608. info '("ignore"))))))
  609. (ert-deftest test-org-export/first-sibling-p ()
  610. "Test `org-export-first-sibling-p' specifications."
  611. ;; Standard test.
  612. (should
  613. (equal
  614. '(yes yes no)
  615. (org-test-with-parsed-data "* Headline\n** Headline 2\n** Headline 3"
  616. (org-element-map
  617. tree 'headline
  618. (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
  619. info))))
  620. ;; Ignore headlines not exported.
  621. (should
  622. (equal
  623. '(yes)
  624. (let ((org-export-exclude-tags '("ignore")))
  625. (org-test-with-parsed-data "* Headline :ignore:\n* Headline 2"
  626. (org-element-map
  627. tree 'headline
  628. (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
  629. info))))))
  630. (ert-deftest test-org-export/last-sibling-p ()
  631. "Test `org-export-last-sibling-p' specifications."
  632. ;; Standard test.
  633. (should
  634. (equal
  635. '(yes no yes)
  636. (org-test-with-parsed-data "* Headline\n** Headline 2\n** Headline 3"
  637. (org-element-map
  638. tree 'headline
  639. (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
  640. info))))
  641. ;; Ignore headlines not exported.
  642. (should
  643. (equal
  644. '(yes)
  645. (let ((org-export-exclude-tags '("ignore")))
  646. (org-test-with-parsed-data "* Headline\n* Headline 2 :ignore:"
  647. (org-element-map
  648. tree 'headline
  649. (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
  650. info))))))
  651. ;;; Links
  652. (ert-deftest test-org-export/get-coderef-format ()
  653. "Test `org-export-get-coderef-format' specifications."
  654. ;; A link without description returns "%s"
  655. (should (equal (org-export-get-coderef-format "(ref:line)" nil)
  656. "%s"))
  657. ;; Return "%s" when path is matched within description.
  658. (should (equal (org-export-get-coderef-format "path" "desc (path)")
  659. "desc %s"))
  660. ;; Otherwise return description.
  661. (should (equal (org-export-get-coderef-format "path" "desc")
  662. "desc")))
  663. (ert-deftest test-org-export/inline-image-p ()
  664. "Test `org-export-inline-image-p' specifications."
  665. (should
  666. (org-export-inline-image-p
  667. (org-test-with-temp-text "[[#id]]"
  668. (org-element-map
  669. (org-element-parse-buffer) 'link 'identity nil t))
  670. '(("custom-id" . "id")))))
  671. (ert-deftest test-org-export/fuzzy-link ()
  672. "Test fuzzy links specifications."
  673. ;; 1. Links to invisible (keyword) targets should be ignored.
  674. (org-test-with-parsed-data
  675. "Paragraph.\n#+TARGET: Test\n[[Test]]"
  676. (should-not
  677. (org-element-map
  678. tree 'link
  679. (lambda (link)
  680. (org-export-get-ordinal
  681. (org-export-resolve-fuzzy-link link info) info)) info)))
  682. ;; 2. Link to an headline should return headline's number.
  683. (org-test-with-parsed-data
  684. "Paragraph.\n* Head1\n* Head2\n* Head3\n[[Head2]]"
  685. (should
  686. ;; Note: Headline's number is in fact a list of numbers.
  687. (equal '(2)
  688. (org-element-map
  689. tree 'link
  690. (lambda (link)
  691. (org-export-get-ordinal
  692. (org-export-resolve-fuzzy-link link info) info)) info t))))
  693. ;; 3. Link to a target in an item should return item's number.
  694. (org-test-with-parsed-data
  695. "- Item1\n - Item11\n - <<test>>Item12\n- Item2\n\n\n[[test]]"
  696. (should
  697. ;; Note: Item's number is in fact a list of numbers.
  698. (equal '(1 2)
  699. (org-element-map
  700. tree 'link
  701. (lambda (link)
  702. (org-export-get-ordinal
  703. (org-export-resolve-fuzzy-link link info) info)) info t))))
  704. ;; 4. Link to a target in a footnote should return footnote's
  705. ;; number.
  706. (org-test-with-parsed-data "
  707. Paragraph[1][2][fn:lbl3:C<<target>>][[test]][[target]]\n[1] A\n\n[2] <<test>>B"
  708. (should
  709. (equal '(2 3)
  710. (org-element-map
  711. tree 'link
  712. (lambda (link)
  713. (org-export-get-ordinal
  714. (org-export-resolve-fuzzy-link link info) info)) info))))
  715. ;; 5. Link to a named element should return sequence number of that
  716. ;; element.
  717. (org-test-with-parsed-data
  718. "#+NAME: tbl1\n|1|2|\n#+NAME: tbl2\n|3|4|\n#+NAME: tbl3\n|5|6|\n[[tbl2]]"
  719. (should
  720. (= 2
  721. (org-element-map
  722. tree 'link
  723. (lambda (link)
  724. (org-export-get-ordinal
  725. (org-export-resolve-fuzzy-link link info) info)) info t))))
  726. ;; 6. Link to a target not within an item, a table, a footnote
  727. ;; reference or definition should return section number.
  728. (org-test-with-parsed-data
  729. "* Head1\n* Head2\nParagraph<<target>>\n* Head3\n[[target]]"
  730. (should
  731. (equal '(2)
  732. (org-element-map
  733. tree 'link
  734. (lambda (link)
  735. (org-export-get-ordinal
  736. (org-export-resolve-fuzzy-link link info) info)) info t)))))
  737. (ert-deftest test-org-export/resolve-coderef ()
  738. "Test `org-export-resolve-coderef' specifications."
  739. (let ((org-coderef-label-format "(ref:%s)"))
  740. ;; 1. A link to a "-n -k -r" block returns line number.
  741. (org-test-with-parsed-data
  742. "#+BEGIN_EXAMPLE -n -k -r\nText (ref:coderef)\n#+END_EXAMPLE"
  743. (should (= (org-export-resolve-coderef "coderef" info) 1)))
  744. (org-test-with-parsed-data
  745. "#+BEGIN_SRC emacs-lisp -n -k -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  746. (should (= (org-export-resolve-coderef "coderef" info) 1)))
  747. ;; 2. A link to a "-n -r" block returns line number.
  748. (org-test-with-parsed-data
  749. "#+BEGIN_EXAMPLE -n -r\nText (ref:coderef)\n#+END_EXAMPLE"
  750. (should (= (org-export-resolve-coderef "coderef" info) 1)))
  751. (org-test-with-parsed-data
  752. "#+BEGIN_SRC emacs-lisp -n -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  753. (should (= (org-export-resolve-coderef "coderef" info) 1)))
  754. ;; 3. A link to a "-n" block returns coderef.
  755. (org-test-with-parsed-data
  756. "#+BEGIN_SRC emacs-lisp -n\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  757. (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
  758. (org-test-with-parsed-data
  759. "#+BEGIN_EXAMPLE -n\nText (ref:coderef)\n#+END_EXAMPLE"
  760. (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
  761. ;; 4. A link to a "-r" block returns line number.
  762. (org-test-with-parsed-data
  763. "#+BEGIN_SRC emacs-lisp -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  764. (should (= (org-export-resolve-coderef "coderef" info) 1)))
  765. (org-test-with-parsed-data
  766. "#+BEGIN_EXAMPLE -r\nText (ref:coderef)\n#+END_EXAMPLE"
  767. (should (= (org-export-resolve-coderef "coderef" info) 1)))
  768. ;; 5. A link to a block without a switch returns coderef.
  769. (org-test-with-parsed-data
  770. "#+BEGIN_SRC emacs-lisp\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  771. (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
  772. (org-test-with-parsed-data
  773. "#+BEGIN_EXAMPLE\nText (ref:coderef)\n#+END_EXAMPLE"
  774. (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
  775. ;; 6. Correctly handle continued line numbers. A "+n" switch
  776. ;; should resume numbering from previous block with numbered
  777. ;; lines, ignoring blocks not numbering lines in the process.
  778. ;; A "-n" switch resets count.
  779. (org-test-with-parsed-data "
  780. #+BEGIN_EXAMPLE -n
  781. Text.
  782. #+END_EXAMPLE
  783. #+BEGIN_SRC emacs-lisp
  784. \(- 1 1)
  785. #+END_SRC
  786. #+BEGIN_SRC emacs-lisp +n -r
  787. \(+ 1 1) (ref:addition)
  788. #+END_SRC
  789. #+BEGIN_EXAMPLE -n -r
  790. Another text. (ref:text)
  791. #+END_EXAMPLE"
  792. (should (= (org-export-resolve-coderef "addition" info) 2))
  793. (should (= (org-export-resolve-coderef "text" info) 1)))
  794. ;; 7. Recognize coderef with user-specified syntax.
  795. (org-test-with-parsed-data
  796. "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\nText. [ref:text]\n#+END_EXAMPLE"
  797. (should (equal (org-export-resolve-coderef "text" info) "text")))))
  798. (ert-deftest test-org-export/resolve-fuzzy-link ()
  799. "Test `org-export-resolve-fuzzy-link' specifications."
  800. ;; 1. Match target objects.
  801. (org-test-with-parsed-data "<<target>> [[target]]"
  802. (should
  803. (org-export-resolve-fuzzy-link
  804. (org-element-map tree 'link 'identity info t) info)))
  805. ;; 2. Match target elements.
  806. (org-test-with-parsed-data "#+TARGET: target\n[[target]]"
  807. (should
  808. (org-export-resolve-fuzzy-link
  809. (org-element-map tree 'link 'identity info t) info)))
  810. ;; 3. Match named elements.
  811. (org-test-with-parsed-data "#+NAME: target\nParagraph\n\n[[target]]"
  812. (should
  813. (org-export-resolve-fuzzy-link
  814. (org-element-map tree 'link 'identity info t) info)))
  815. ;; 4. Match exact headline's name.
  816. (org-test-with-parsed-data "* My headline\n[[My headline]]"
  817. (should
  818. (org-export-resolve-fuzzy-link
  819. (org-element-map tree 'link 'identity info t) info)))
  820. ;; 5. Targets objects have priority over named elements and headline
  821. ;; titles.
  822. (org-test-with-parsed-data
  823. "* target\n#+NAME: target\n<<target>>\n\n[[target]]"
  824. (should
  825. (eq 'target
  826. (org-element-type
  827. (org-export-resolve-fuzzy-link
  828. (org-element-map tree 'link 'identity info t) info)))))
  829. ;; 6. Named elements have priority over headline titles.
  830. (org-test-with-parsed-data
  831. "* target\n#+NAME: target\nParagraph\n\n[[target]]"
  832. (should
  833. (eq 'paragraph
  834. (org-element-type
  835. (org-export-resolve-fuzzy-link
  836. (org-element-map tree 'link 'identity info t) info)))))
  837. ;; 7. If link's path starts with a "*", only match headline titles,
  838. ;; though.
  839. (org-test-with-parsed-data
  840. "* target\n#+NAME: target\n<<target>>\n\n[[*target]]"
  841. (should
  842. (eq 'headline
  843. (org-element-type
  844. (org-export-resolve-fuzzy-link
  845. (org-element-map tree 'link 'identity info t) info)))))
  846. ;; 8. Return nil if no match.
  847. (org-test-with-parsed-data "[[target]]"
  848. (should-not
  849. (org-export-resolve-fuzzy-link
  850. (org-element-map tree 'link 'identity info t) info))))
  851. (ert-deftest test-org-export/resolve-id-link ()
  852. "Test `org-export-resolve-id-link' specifications."
  853. ;; 1. Regular test for custom-id link.
  854. (org-test-with-parsed-data "* Headline1
  855. :PROPERTIES:
  856. :CUSTOM-ID: test
  857. :END:
  858. * Headline 2
  859. \[[#test]]"
  860. (should
  861. (org-export-resolve-id-link
  862. (org-element-map tree 'link 'identity info t) info)))
  863. ;; 2. Failing test for custom-id link.
  864. (org-test-with-parsed-data "* Headline1
  865. :PROPERTIES:
  866. :CUSTOM-ID: test
  867. :END:
  868. * Headline 2
  869. \[[#no-match]]"
  870. (should-not
  871. (org-export-resolve-id-link
  872. (org-element-map tree 'link 'identity info t) info)))
  873. ;; 3. Test for internal id target.
  874. (org-test-with-parsed-data "* Headline1
  875. :PROPERTIES:
  876. :ID: aaaa
  877. :END:
  878. * Headline 2
  879. \[[id:aaaa]]"
  880. (should
  881. (org-export-resolve-id-link
  882. (org-element-map tree 'link 'identity info t) info)))
  883. ;; 4. Test for external id target.
  884. (org-test-with-parsed-data "[[id:aaaa]]"
  885. (should
  886. (org-export-resolve-id-link
  887. (org-element-map tree 'link 'identity info t)
  888. (org-combine-plists info '(:id-alist (("aaaa" . "external-file"))))))))
  889. (ert-deftest test-org-export/resolve-radio-link ()
  890. "Test `org-export-resolve-radio-link' specifications."
  891. ;; Standard test.
  892. (org-test-with-temp-text "<<<radio>>> radio"
  893. (org-update-radio-target-regexp)
  894. (should
  895. (let* ((tree (org-element-parse-buffer))
  896. (info `(:parse-tree ,tree)))
  897. (org-export-resolve-radio-link
  898. (org-element-map tree 'link 'identity info t)
  899. info))))
  900. ;; Radio target with objects.
  901. (org-test-with-temp-text "<<<radio \\alpha>>> radio \\alpha"
  902. (org-update-radio-target-regexp)
  903. (should
  904. (let* ((tree (org-element-parse-buffer))
  905. (info `(:parse-tree ,tree)))
  906. (org-export-resolve-radio-link
  907. (org-element-map tree 'link 'identity info t)
  908. info)))))
  909. ;;; Src-block and example-block
  910. (ert-deftest test-org-export/unravel-code ()
  911. "Test `org-export-unravel-code' function."
  912. (let ((org-coderef-label-format "(ref:%s)"))
  913. ;; 1. Code without reference.
  914. (org-test-with-temp-text "#+BEGIN_EXAMPLE\n(+ 1 1)\n#+END_EXAMPLE"
  915. (should (equal (org-export-unravel-code (org-element-at-point))
  916. '("(+ 1 1)\n"))))
  917. ;; 2. Code with reference.
  918. (org-test-with-temp-text
  919. "#+BEGIN_EXAMPLE\n(+ 1 1) (ref:test)\n#+END_EXAMPLE"
  920. (should (equal (org-export-unravel-code (org-element-at-point))
  921. '("(+ 1 1)\n" (1 . "test")))))
  922. ;; 3. Code with user-defined reference.
  923. (org-test-with-temp-text
  924. "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\n(+ 1 1) [ref:test]\n#+END_EXAMPLE"
  925. (should (equal (org-export-unravel-code (org-element-at-point))
  926. '("(+ 1 1)\n" (1 . "test")))))
  927. ;; 4. Code references keys are relative to the current block.
  928. (org-test-with-temp-text "
  929. #+BEGIN_EXAMPLE -n
  930. \(+ 1 1)
  931. #+END_EXAMPLE
  932. #+BEGIN_EXAMPLE +n
  933. \(+ 2 2)
  934. \(+ 3 3) (ref:one)
  935. #+END_EXAMPLE"
  936. (goto-line 5)
  937. (should (equal (org-export-unravel-code (org-element-at-point))
  938. '("(+ 2 2)\n(+ 3 3)\n" (2 . "one")))))
  939. ;; 5. Free up comma-protected lines.
  940. ;;
  941. ;; 5.1. In an Org source block, every line is protected.
  942. (org-test-with-temp-text
  943. "#+BEGIN_SRC org\n,* Test\n,# comment\n,Text\n#+END_SRC"
  944. (should (equal (org-export-unravel-code (org-element-at-point))
  945. '("* Test\n# comment\nText\n"))))
  946. ;; 5.2. In other blocks, only headlines, comments and keywords are
  947. ;; protected.
  948. (org-test-with-temp-text
  949. "#+BEGIN_EXAMPLE\n,* Headline\n, * Not headline\n,Keep\n#+END_EXAMPLE"
  950. (should (equal (org-export-unravel-code (org-element-at-point))
  951. '("* Headline\n, * Not headline\n,Keep\n"))))))
  952. ;;; Tables
  953. (ert-deftest test-org-export/special-column ()
  954. "Test if the table's special column is properly recognized."
  955. ;; 1. First column is special if it contains only a special marking
  956. ;; characters or empty cells.
  957. (org-test-with-temp-text "
  958. | ! | 1 |
  959. | | 2 |"
  960. (should
  961. (org-export-table-has-special-column-p
  962. (org-element-map
  963. (org-element-parse-buffer) 'table 'identity nil 'first-match))))
  964. ;; 2. If the column contains anything else, it isn't special.
  965. (org-test-with-temp-text "
  966. | ! | 1 |
  967. | b | 2 |"
  968. (should-not
  969. (org-export-table-has-special-column-p
  970. (org-element-map
  971. (org-element-parse-buffer) 'table 'identity nil 'first-match))))
  972. ;; 3. Special marking characters are "#", "^", "*", "_", "/", "$"
  973. ;; and "!".
  974. (org-test-with-temp-text "
  975. | # | 1 |
  976. | ^ | 2 |
  977. | * | 3 |
  978. | _ | 4 |
  979. | / | 5 |
  980. | $ | 6 |
  981. | ! | 7 |"
  982. (should
  983. (org-export-table-has-special-column-p
  984. (org-element-map
  985. (org-element-parse-buffer) 'table 'identity nil 'first-match))))
  986. ;; 4. A first column with only empty cells isn't considered as
  987. ;; special.
  988. (org-test-with-temp-text "
  989. | | 1 |
  990. | | 2 |"
  991. (should-not
  992. (org-export-table-has-special-column-p
  993. (org-element-map
  994. (org-element-parse-buffer) 'table 'identity nil 'first-match)))))
  995. (ert-deftest test-org-export/table-row-is-special-p ()
  996. "Test `org-export-table-row-is-special-p' specifications."
  997. ;; 1. A row is special if it has a special marking character in the
  998. ;; special column.
  999. (org-test-with-parsed-data "| ! | 1 |"
  1000. (should
  1001. (org-export-table-row-is-special-p
  1002. (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  1003. ;; 2. A row is special when its first field is "/"
  1004. (org-test-with-parsed-data "
  1005. | / | 1 |
  1006. | a | b |"
  1007. (should
  1008. (org-export-table-row-is-special-p
  1009. (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  1010. ;; 3. A row only containing alignment cookies is also considered as
  1011. ;; special.
  1012. (org-test-with-parsed-data "| <5> | | <l> | <l22> |"
  1013. (should
  1014. (org-export-table-row-is-special-p
  1015. (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  1016. ;; 4. Everything else isn't considered as special.
  1017. (org-test-with-parsed-data "| \alpha | | c |"
  1018. (should-not
  1019. (org-export-table-row-is-special-p
  1020. (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  1021. ;; 5. Table's rules are never considered as special rows.
  1022. (org-test-with-parsed-data "|---+---|"
  1023. (should-not
  1024. (org-export-table-row-is-special-p
  1025. (org-element-map tree 'table-row 'identity nil 'first-match) info))))
  1026. (ert-deftest test-org-export/has-header-p ()
  1027. "Test `org-export-table-has-header-p' specifications."
  1028. ;; 1. With an header.
  1029. (org-test-with-parsed-data "
  1030. | a | b |
  1031. |---+---|
  1032. | c | d |"
  1033. (should
  1034. (org-export-table-has-header-p
  1035. (org-element-map tree 'table 'identity info 'first-match)
  1036. info)))
  1037. ;; 2. Without an header.
  1038. (org-test-with-parsed-data "
  1039. | a | b |
  1040. | c | d |"
  1041. (should-not
  1042. (org-export-table-has-header-p
  1043. (org-element-map tree 'table 'identity info 'first-match)
  1044. info)))
  1045. ;; 3. Don't get fooled with starting and ending rules.
  1046. (org-test-with-parsed-data "
  1047. |---+---|
  1048. | a | b |
  1049. | c | d |
  1050. |---+---|"
  1051. (should-not
  1052. (org-export-table-has-header-p
  1053. (org-element-map tree 'table 'identity info 'first-match)
  1054. info))))
  1055. (ert-deftest test-org-export/table-row-group ()
  1056. "Test `org-export-table-row-group' specifications."
  1057. ;; 1. A rule creates a new group.
  1058. (org-test-with-parsed-data "
  1059. | a | b |
  1060. |---+---|
  1061. | 1 | 2 |"
  1062. (should
  1063. (equal
  1064. '(1 nil 2)
  1065. (mapcar (lambda (row) (org-export-table-row-group row info))
  1066. (org-element-map tree 'table-row 'identity)))))
  1067. ;; 2. Special rows are ignored in count.
  1068. (org-test-with-parsed-data "
  1069. | / | < | > |
  1070. |---|---+---|
  1071. | | 1 | 2 |"
  1072. (should
  1073. (equal
  1074. '(nil nil 1)
  1075. (mapcar (lambda (row) (org-export-table-row-group row info))
  1076. (org-element-map tree 'table-row 'identity)))))
  1077. ;; 3. Double rules also are ignored in count.
  1078. (org-test-with-parsed-data "
  1079. | a | b |
  1080. |---+---|
  1081. |---+---|
  1082. | 1 | 2 |"
  1083. (should
  1084. (equal
  1085. '(1 nil nil 2)
  1086. (mapcar (lambda (row) (org-export-table-row-group row info))
  1087. (org-element-map tree 'table-row 'identity))))))
  1088. (ert-deftest test-org-export/table-cell-width ()
  1089. "Test `org-export-table-cell-width' specifications."
  1090. ;; 1. Width is primarily determined by width cookies. If no cookie
  1091. ;; is found, cell's width is nil.
  1092. (org-test-with-parsed-data "
  1093. | / | <l> | <6> | <l7> |
  1094. | | a | b | c |"
  1095. (should
  1096. (equal
  1097. '(nil 6 7)
  1098. (mapcar (lambda (cell) (org-export-table-cell-width cell info))
  1099. (org-element-map tree 'table-cell 'identity info)))))
  1100. ;; 2. The last width cookie has precedence.
  1101. (org-test-with-parsed-data "
  1102. | <6> |
  1103. | <7> |
  1104. | a |"
  1105. (should
  1106. (equal
  1107. '(7)
  1108. (mapcar (lambda (cell) (org-export-table-cell-width cell info))
  1109. (org-element-map tree 'table-cell 'identity info)))))
  1110. ;; 3. Valid width cookies must have a specific row.
  1111. (org-test-with-parsed-data "| <6> | cell |"
  1112. (should
  1113. (equal
  1114. '(nil nil)
  1115. (mapcar (lambda (cell) (org-export-table-cell-width cell info))
  1116. (org-element-map tree 'table-cell 'identity))))))
  1117. (ert-deftest test-org-export/table-cell-alignment ()
  1118. "Test `org-export-table-cell-alignment' specifications."
  1119. (let ((org-table-number-fraction 0.5)
  1120. (org-table-number-regexp "^[0-9]+$"))
  1121. ;; 1. Alignment is primarily determined by alignment cookies.
  1122. (org-test-with-temp-text "| <l> | <c> | <r> |"
  1123. (let* ((tree (org-element-parse-buffer))
  1124. (info `(:parse-tree ,tree)))
  1125. (should
  1126. (equal
  1127. '(left center right)
  1128. (mapcar (lambda (cell) (org-export-table-cell-alignment cell info))
  1129. (org-element-map tree 'table-cell 'identity))))))
  1130. ;; 2. The last alignment cookie has precedence.
  1131. (org-test-with-parsed-data "
  1132. | <l8> |
  1133. | cell |
  1134. | <r9> |"
  1135. (should
  1136. (equal
  1137. '(right right right)
  1138. (mapcar (lambda (cell) (org-export-table-cell-alignment cell info))
  1139. (org-element-map tree 'table-cell 'identity)))))
  1140. ;; 3. If there's no cookie, cell's contents determine alignment.
  1141. ;; A column mostly made of cells containing numbers will align
  1142. ;; its cells to the right.
  1143. (org-test-with-parsed-data "
  1144. | 123 |
  1145. | some text |
  1146. | 12345 |"
  1147. (should
  1148. (equal
  1149. '(right right right)
  1150. (mapcar (lambda (cell)
  1151. (org-export-table-cell-alignment cell info))
  1152. (org-element-map tree 'table-cell 'identity)))))
  1153. ;; 4. Otherwise, they will be aligned to the left.
  1154. (org-test-with-parsed-data "
  1155. | text |
  1156. | some text |
  1157. | \alpha |"
  1158. (should
  1159. (equal
  1160. '(left left left)
  1161. (mapcar (lambda (cell)
  1162. (org-export-table-cell-alignment cell info))
  1163. (org-element-map tree 'table-cell 'identity)))))))
  1164. (ert-deftest test-org-export/table-cell-borders ()
  1165. "Test `org-export-table-cell-borders' specifications."
  1166. ;; 1. Recognize various column groups indicators.
  1167. (org-test-with-parsed-data "| / | < | > | <> |"
  1168. (should
  1169. (equal
  1170. '((right bottom top) (left bottom top) (right bottom top)
  1171. (right left bottom top))
  1172. (mapcar (lambda (cell)
  1173. (org-export-table-cell-borders cell info))
  1174. (org-element-map tree 'table-cell 'identity)))))
  1175. ;; 2. Accept shortcuts to define column groups.
  1176. (org-test-with-parsed-data "| / | < | < |"
  1177. (should
  1178. (equal
  1179. '((right bottom top) (right left bottom top) (left bottom top))
  1180. (mapcar (lambda (cell)
  1181. (org-export-table-cell-borders cell info))
  1182. (org-element-map tree 'table-cell 'identity)))))
  1183. ;; 3. A valid column groups row must start with a "/".
  1184. (org-test-with-parsed-data "
  1185. | | < |
  1186. | a | b |"
  1187. (should
  1188. (equal '((top) (top) (bottom) (bottom))
  1189. (mapcar (lambda (cell)
  1190. (org-export-table-cell-borders cell info))
  1191. (org-element-map tree 'table-cell 'identity)))))
  1192. ;; 4. Take table rules into consideration.
  1193. (org-test-with-parsed-data "
  1194. | 1 |
  1195. |---|
  1196. | 2 |"
  1197. (should
  1198. (equal '((below top) (bottom above))
  1199. (mapcar (lambda (cell)
  1200. (org-export-table-cell-borders cell info))
  1201. (org-element-map tree 'table-cell 'identity)))))
  1202. ;; 5. Top and (resp. bottom) rules induce both `top' and `above'
  1203. ;; (resp. `bottom' and `below') borders. Any special row is
  1204. ;; ignored.
  1205. (org-test-with-parsed-data "
  1206. |---+----|
  1207. | / | |
  1208. | | 1 |
  1209. |---+----|"
  1210. (should
  1211. (equal '((bottom below top above))
  1212. (last
  1213. (mapcar (lambda (cell)
  1214. (org-export-table-cell-borders cell info))
  1215. (org-element-map tree 'table-cell 'identity)))))))
  1216. (ert-deftest test-org-export/table-dimensions ()
  1217. "Test `org-export-table-dimensions' specifications."
  1218. ;; 1. Standard test.
  1219. (org-test-with-parsed-data "
  1220. | 1 | 2 | 3 |
  1221. | 4 | 5 | 6 |"
  1222. (should
  1223. (equal '(2 . 3)
  1224. (org-export-table-dimensions
  1225. (org-element-map tree 'table 'identity info 'first-match) info))))
  1226. ;; 2. Ignore horizontal rules and special columns.
  1227. (org-test-with-parsed-data "
  1228. | / | < | > |
  1229. | 1 | 2 | 3 |
  1230. |---+---+---|
  1231. | 4 | 5 | 6 |"
  1232. (should
  1233. (equal '(2 . 3)
  1234. (org-export-table-dimensions
  1235. (org-element-map tree 'table 'identity info 'first-match) info)))))
  1236. (ert-deftest test-org-export/table-cell-address ()
  1237. "Test `org-export-table-cell-address' specifications."
  1238. ;; 1. Standard test: index is 0-based.
  1239. (org-test-with-parsed-data "| a | b |"
  1240. (should
  1241. (equal '((0 . 0) (0 . 1))
  1242. (org-element-map
  1243. tree 'table-cell
  1244. (lambda (cell) (org-export-table-cell-address cell info))
  1245. info))))
  1246. ;; 2. Special column isn't counted, nor are special rows.
  1247. (org-test-with-parsed-data "
  1248. | / | <> |
  1249. | | c |"
  1250. (should
  1251. (equal '(0 . 0)
  1252. (org-export-table-cell-address
  1253. (car (last (org-element-map tree 'table-cell 'identity info)))
  1254. info))))
  1255. ;; 3. Tables rules do not count either.
  1256. (org-test-with-parsed-data "
  1257. | a |
  1258. |---|
  1259. | b |
  1260. |---|
  1261. | c |"
  1262. (should
  1263. (equal '(2 . 0)
  1264. (org-export-table-cell-address
  1265. (car (last (org-element-map tree 'table-cell 'identity info)))
  1266. info))))
  1267. ;; 4. Return nil for special cells.
  1268. (org-test-with-parsed-data "| / | a |"
  1269. (should-not
  1270. (org-export-table-cell-address
  1271. (org-element-map tree 'table-cell 'identity nil 'first-match)
  1272. info))))
  1273. (ert-deftest test-org-export/get-table-cell-at ()
  1274. "Test `org-export-get-table-cell-at' specifications."
  1275. ;; 1. Address ignores special columns, special rows and rules.
  1276. (org-test-with-parsed-data "
  1277. | / | <> |
  1278. | | a |
  1279. |---+----|
  1280. | | b |"
  1281. (should
  1282. (equal '("b")
  1283. (org-element-contents
  1284. (org-export-get-table-cell-at
  1285. '(1 . 0)
  1286. (org-element-map tree 'table 'identity info 'first-match)
  1287. info)))))
  1288. ;; 2. Return value for a non-existent address is nil.
  1289. (org-test-with-parsed-data "| a |"
  1290. (should-not
  1291. (org-export-get-table-cell-at
  1292. '(2 . 2)
  1293. (org-element-map tree 'table 'identity info 'first-match)
  1294. info)))
  1295. (org-test-with-parsed-data "| / |"
  1296. (should-not
  1297. (org-export-get-table-cell-at
  1298. '(0 . 0)
  1299. (org-element-map tree 'table 'identity info 'first-match)
  1300. info))))
  1301. (ert-deftest test-org-export/table-cell-starts-colgroup-p ()
  1302. "Test `org-export-table-cell-starts-colgroup-p' specifications."
  1303. ;; 1. A cell at a beginning of a row always starts a column group.
  1304. (org-test-with-parsed-data "| a |"
  1305. (should
  1306. (org-export-table-cell-starts-colgroup-p
  1307. (org-element-map tree 'table-cell 'identity info 'first-match)
  1308. info)))
  1309. ;; 2. Special column should be ignored when determining the
  1310. ;; beginning of the row.
  1311. (org-test-with-parsed-data "
  1312. | / | |
  1313. | | a |"
  1314. (should
  1315. (org-export-table-cell-starts-colgroup-p
  1316. (org-element-map tree 'table-cell 'identity info 'first-match)
  1317. info)))
  1318. ;; 2. Explicit column groups.
  1319. (org-test-with-parsed-data "
  1320. | / | | < |
  1321. | a | b | c |"
  1322. (should
  1323. (equal
  1324. '(yes no yes)
  1325. (org-element-map
  1326. tree 'table-cell
  1327. (lambda (cell)
  1328. (if (org-export-table-cell-starts-colgroup-p cell info) 'yes 'no))
  1329. info)))))
  1330. (ert-deftest test-org-export/table-cell-ends-colgroup-p ()
  1331. "Test `org-export-table-cell-ends-colgroup-p' specifications."
  1332. ;; 1. A cell at the end of a row always ends a column group.
  1333. (org-test-with-parsed-data "| a |"
  1334. (should
  1335. (org-export-table-cell-ends-colgroup-p
  1336. (org-element-map tree 'table-cell 'identity info 'first-match)
  1337. info)))
  1338. ;; 2. Special column should be ignored when determining the
  1339. ;; beginning of the row.
  1340. (org-test-with-parsed-data "
  1341. | / | |
  1342. | | a |"
  1343. (should
  1344. (org-export-table-cell-ends-colgroup-p
  1345. (org-element-map tree 'table-cell 'identity info 'first-match)
  1346. info)))
  1347. ;; 3. Explicit column groups.
  1348. (org-test-with-parsed-data "
  1349. | / | < | |
  1350. | a | b | c |"
  1351. (should
  1352. (equal
  1353. '(yes no yes)
  1354. (org-element-map
  1355. tree 'table-cell
  1356. (lambda (cell)
  1357. (if (org-export-table-cell-ends-colgroup-p cell info) 'yes 'no))
  1358. info)))))
  1359. (ert-deftest test-org-export/table-row-starts-rowgroup-p ()
  1360. "Test `org-export-table-row-starts-rowgroup-p' specifications."
  1361. ;; 1. A row at the beginning of a table always starts a row group.
  1362. ;; So does a row following a table rule.
  1363. (org-test-with-parsed-data "
  1364. | a |
  1365. |---|
  1366. | b |"
  1367. (should
  1368. (equal
  1369. '(yes no yes)
  1370. (org-element-map
  1371. tree 'table-row
  1372. (lambda (row)
  1373. (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
  1374. info))))
  1375. ;; 2. Special rows should be ignored when determining the beginning
  1376. ;; of the row.
  1377. (org-test-with-parsed-data "
  1378. | / | < |
  1379. | | a |
  1380. |---+---|
  1381. | / | < |
  1382. | | b |"
  1383. (should
  1384. (equal
  1385. '(yes no yes)
  1386. (org-element-map
  1387. tree 'table-row
  1388. (lambda (row)
  1389. (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
  1390. info)))))
  1391. (ert-deftest test-org-export/table-row-ends-rowgroup-p ()
  1392. "Test `org-export-table-row-ends-rowgroup-p' specifications."
  1393. ;; 1. A row at the end of a table always ends a row group. So does
  1394. ;; a row preceding a table rule.
  1395. (org-test-with-parsed-data "
  1396. | a |
  1397. |---|
  1398. | b |"
  1399. (should
  1400. (equal
  1401. '(yes no yes)
  1402. (org-element-map
  1403. tree 'table-row
  1404. (lambda (row)
  1405. (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
  1406. info))))
  1407. ;; 2. Special rows should be ignored when determining the beginning
  1408. ;; of the row.
  1409. (org-test-with-parsed-data "
  1410. | | a |
  1411. | / | < |
  1412. |---+---|
  1413. | | b |
  1414. | / | < |"
  1415. (should
  1416. (equal
  1417. '(yes no yes)
  1418. (org-element-map
  1419. tree 'table-row
  1420. (lambda (row)
  1421. (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
  1422. info)))))
  1423. (ert-deftest test-org-export/table-row-starts-header-p ()
  1424. "Test `org-export-table-row-starts-header-p' specifications."
  1425. ;; 1. Only the row starting the first row group starts the table
  1426. ;; header.
  1427. (org-test-with-parsed-data "
  1428. | a |
  1429. | b |
  1430. |---|
  1431. | c |"
  1432. (should
  1433. (equal
  1434. '(yes no no no)
  1435. (org-element-map
  1436. tree 'table-row
  1437. (lambda (row)
  1438. (if (org-export-table-row-starts-header-p row info) 'yes 'no))
  1439. info))))
  1440. ;; 2. A row cannot start an header if there's no header in the
  1441. ;; table.
  1442. (org-test-with-parsed-data "
  1443. | a |
  1444. |---|"
  1445. (should-not
  1446. (org-export-table-row-starts-header-p
  1447. (org-element-map tree 'table-row 'identity info 'first-match)
  1448. info))))
  1449. (ert-deftest test-org-export/table-row-ends-header-p ()
  1450. "Test `org-export-table-row-ends-header-p' specifications."
  1451. ;; 1. Only the row starting the first row group starts the table
  1452. ;; header.
  1453. (org-test-with-parsed-data "
  1454. | a |
  1455. | b |
  1456. |---|
  1457. | c |"
  1458. (should
  1459. (equal
  1460. '(no yes no no)
  1461. (org-element-map
  1462. tree 'table-row
  1463. (lambda (row)
  1464. (if (org-export-table-row-ends-header-p row info) 'yes 'no))
  1465. info))))
  1466. ;; 2. A row cannot start an header if there's no header in the
  1467. ;; table.
  1468. (org-test-with-parsed-data "
  1469. | a |
  1470. |---|"
  1471. (should-not
  1472. (org-export-table-row-ends-header-p
  1473. (org-element-map tree 'table-row 'identity info 'first-match)
  1474. info))))
  1475. ;;; Topology
  1476. (ert-deftest test-org-export/get-next-element ()
  1477. "Test `org-export-get-next-element' specifications."
  1478. ;; Standard test.
  1479. (should
  1480. (equal "b"
  1481. (org-test-with-parsed-data "* Headline\n*a* b"
  1482. (org-export-get-next-element
  1483. (org-element-map tree 'bold 'identity info t) info))))
  1484. ;; Return nil when no previous element.
  1485. (should-not
  1486. (org-test-with-parsed-data "* Headline\na *b*"
  1487. (org-export-get-next-element
  1488. (org-element-map tree 'bold 'identity info t) info)))
  1489. ;; Non-exportable elements are ignored.
  1490. (should-not
  1491. (let ((org-export-with-timestamps nil))
  1492. (org-test-with-parsed-data "\alpha <2012-03-29 Thu>"
  1493. (org-export-get-next-element
  1494. (org-element-map tree 'entity 'identity info t) info)))))
  1495. (ert-deftest test-org-export/get-previous-element ()
  1496. "Test `org-export-get-previous-element' specifications."
  1497. ;; Standard test.
  1498. (should
  1499. (equal "a "
  1500. (org-test-with-parsed-data "* Headline\na *b*"
  1501. (org-export-get-previous-element
  1502. (org-element-map tree 'bold 'identity info t) info))))
  1503. ;; Return nil when no previous element.
  1504. (should-not
  1505. (org-test-with-parsed-data "* Headline\n*a* b"
  1506. (org-export-get-previous-element
  1507. (org-element-map tree 'bold 'identity info t) info)))
  1508. ;; Non-exportable elements are ignored.
  1509. (should-not
  1510. (let ((org-export-with-timestamps nil))
  1511. (org-test-with-parsed-data "<2012-03-29 Thu> \alpha"
  1512. (org-export-get-previous-element
  1513. (org-element-map tree 'entity 'identity info t) info)))))
  1514. (provide 'test-org-export)
  1515. ;;; test-org-export.el end here