test-ox.el 74 KB

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