test-org-export.el 63 KB

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