test-ox.el 73 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262
  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. ;;; Headlines and Inlinetasks
  808. (ert-deftest test-org-export/get-relative-level ()
  809. "Test `org-export-get-relative-level' specifications."
  810. ;; Standard test.
  811. (should
  812. (equal '(1 2)
  813. (let ((org-odd-levels-only nil))
  814. (org-test-with-parsed-data "* Headline 1\n** Headline 2"
  815. (org-element-map
  816. tree 'headline
  817. (lambda (h) (org-export-get-relative-level h info))
  818. info)))))
  819. ;; Missing levels
  820. (should
  821. (equal '(1 3)
  822. (let ((org-odd-levels-only nil))
  823. (org-test-with-parsed-data "** Headline 1\n**** Headline 2"
  824. (org-element-map
  825. tree 'headline
  826. (lambda (h) (org-export-get-relative-level h info))
  827. info))))))
  828. (ert-deftest test-org-export/low-level-p ()
  829. "Test `org-export-low-level-p' specifications."
  830. (should
  831. (equal
  832. '(no yes)
  833. (let ((org-odd-levels-only nil))
  834. (org-test-with-parsed-data "* Headline 1\n** Headline 2"
  835. (org-element-map
  836. tree 'headline
  837. (lambda (h) (if (org-export-low-level-p h info) 'yes 'no))
  838. (plist-put info :headline-levels 1)))))))
  839. (ert-deftest test-org-export/get-headline-number ()
  840. "Test `org-export-get-headline-number' specifications."
  841. ;; Standard test.
  842. (should
  843. (equal
  844. '((1) (1 1))
  845. (let ((org-odd-levels-only nil))
  846. (org-test-with-parsed-data "* Headline 1\n** Headline 2"
  847. (org-element-map
  848. tree 'headline
  849. (lambda (h) (org-export-get-headline-number h info))
  850. info)))))
  851. ;; Missing levels are replaced with 0.
  852. (should
  853. (equal
  854. '((1) (1 0 1))
  855. (let ((org-odd-levels-only nil))
  856. (org-test-with-parsed-data "* Headline 1\n*** Headline 2"
  857. (org-element-map
  858. tree 'headline
  859. (lambda (h) (org-export-get-headline-number h info))
  860. info))))))
  861. (ert-deftest test-org-export/numbered-headline-p ()
  862. "Test `org-export-numbered-headline-p' specifications."
  863. ;; If `:section-numbers' is nil, never number headlines.
  864. (should-not
  865. (org-test-with-parsed-data "* Headline"
  866. (org-element-map
  867. tree 'headline
  868. (lambda (h) (org-export-numbered-headline-p h info))
  869. (plist-put info :section-numbers nil))))
  870. ;; If `:section-numbers' is a number, only number headlines with
  871. ;; a level greater that it.
  872. (should
  873. (equal
  874. '(yes no)
  875. (org-test-with-parsed-data "* Headline 1\n** Headline 2"
  876. (org-element-map
  877. tree 'headline
  878. (lambda (h) (if (org-export-numbered-headline-p h info) 'yes 'no))
  879. (plist-put info :section-numbers 1)))))
  880. ;; Otherwise, headlines are always numbered.
  881. (should
  882. (org-test-with-parsed-data "* Headline"
  883. (org-element-map
  884. tree 'headline
  885. (lambda (h) (org-export-numbered-headline-p h info))
  886. (plist-put info :section-numbers t)))))
  887. (ert-deftest test-org-export/number-to-roman ()
  888. "Test `org-export-number-to-roman' specifications."
  889. ;; If number is negative, return it as a string.
  890. (should (equal (org-export-number-to-roman -1) "-1"))
  891. ;; Otherwise, return it as a roman number.
  892. (should (equal (org-export-number-to-roman 1449) "MCDXLIX")))
  893. (ert-deftest test-org-export/get-tags ()
  894. "Test `org-export-get-tags' specifications."
  895. (let ((org-export-exclude-tags '("noexport"))
  896. (org-export-select-tags '("export")))
  897. ;; Standard test: tags which are not a select tag, an exclude tag,
  898. ;; or specified as optional argument shouldn't be ignored.
  899. (should
  900. (org-test-with-parsed-data "* Headline :tag:"
  901. (org-export-get-tags (org-element-map tree 'headline 'identity info t)
  902. info)))
  903. ;; Exclude tags are removed.
  904. (should-not
  905. (org-test-with-parsed-data "* Headline :noexport:"
  906. (org-export-get-tags (org-element-map tree 'headline 'identity info t)
  907. info)))
  908. ;; Select tags are removed.
  909. (should-not
  910. (org-test-with-parsed-data "* Headline :export:"
  911. (org-export-get-tags (org-element-map tree 'headline 'identity info t)
  912. info)))
  913. (should
  914. (equal
  915. '("tag")
  916. (org-test-with-parsed-data "* Headline :tag:export:"
  917. (org-export-get-tags (org-element-map tree 'headline 'identity info t)
  918. info))))
  919. ;; Tags provided in the optional argument are also ignored.
  920. (should-not
  921. (org-test-with-parsed-data "* Headline :ignore:"
  922. (org-export-get-tags (org-element-map tree 'headline 'identity info t)
  923. info '("ignore"))))
  924. ;; Allow tag inheritance.
  925. (should
  926. (equal
  927. '(("tag") ("tag"))
  928. (org-test-with-parsed-data "* Headline :tag:\n** Sub-heading"
  929. (org-element-map
  930. tree 'headline
  931. (lambda (hl) (org-export-get-tags hl info nil t)) info))))
  932. ;; Tag inheritance checks FILETAGS keywords.
  933. (should
  934. (equal
  935. '(("a" "b" "tag"))
  936. (org-test-with-parsed-data "#+FILETAGS: :a:b:\n* Headline :tag:"
  937. (org-element-map
  938. tree 'headline
  939. (lambda (hl) (org-export-get-tags hl info nil t)) info))))))
  940. (ert-deftest test-org-export/get-node-property ()
  941. "Test`org-export-get-node-property' specifications."
  942. ;; Standard test.
  943. (should
  944. (equal "value"
  945. (org-test-with-parsed-data "* Headline
  946. :PROPERTIES:
  947. :prop: value
  948. :END:"
  949. (org-export-get-node-property
  950. :prop (org-element-map tree 'headline 'identity nil t)))))
  951. ;; Test inheritance.
  952. (should
  953. (equal "value"
  954. (org-test-with-parsed-data "* Parent
  955. :PROPERTIES:
  956. :prop: value
  957. :END:
  958. ** Headline
  959. Paragraph"
  960. (org-export-get-node-property
  961. :prop (org-element-map tree 'paragraph 'identity nil t) t))))
  962. ;; Cannot return a value before the first headline.
  963. (should-not
  964. (org-test-with-parsed-data "Paragraph
  965. * Headline
  966. :PROPERTIES:
  967. :prop: value
  968. :END:"
  969. (org-export-get-node-property
  970. :prop (org-element-map tree 'paragraph 'identity nil t)))))
  971. (ert-deftest test-org-export/get-category ()
  972. "Test `org-export-get-category' specifications."
  973. ;; Standard test.
  974. (should
  975. (equal "value"
  976. (org-test-with-parsed-data "* Headline
  977. :PROPERTIES:
  978. :CATEGORY: value
  979. :END:"
  980. (org-export-get-category
  981. (org-element-map tree 'headline 'identity nil t) info))))
  982. ;; Test inheritance from a parent headline.
  983. (should
  984. (equal '("value" "value")
  985. (org-test-with-parsed-data "* Headline1
  986. :PROPERTIES:
  987. :CATEGORY: value
  988. :END:
  989. ** Headline2"
  990. (org-element-map
  991. tree 'headline
  992. (lambda (hl) (org-export-get-category hl info)) info))))
  993. ;; Test inheritance from #+CATEGORY keyword
  994. (should
  995. (equal "value"
  996. (org-test-with-parsed-data "#+CATEGORY: value
  997. * Headline"
  998. (org-export-get-category
  999. (org-element-map tree 'headline 'identity nil t) info))))
  1000. ;; Test inheritance from file name.
  1001. (should
  1002. (equal "test"
  1003. (org-test-with-parsed-data "* Headline"
  1004. (let ((info (plist-put info :input-file "~/test.org")))
  1005. (org-export-get-category
  1006. (org-element-map tree 'headline 'identity nil t) info)))))
  1007. ;; Fall-back value.
  1008. (should
  1009. (equal "???"
  1010. (org-test-with-parsed-data "* Headline"
  1011. (org-export-get-category
  1012. (org-element-map tree 'headline 'identity nil t) info)))))
  1013. (ert-deftest test-org-export/first-sibling-p ()
  1014. "Test `org-export-first-sibling-p' specifications."
  1015. ;; Standard test.
  1016. (should
  1017. (equal
  1018. '(yes yes no)
  1019. (org-test-with-parsed-data "* Headline\n** Headline 2\n** Headline 3"
  1020. (org-element-map
  1021. tree 'headline
  1022. (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
  1023. info))))
  1024. ;; Ignore headlines not exported.
  1025. (should
  1026. (equal
  1027. '(yes)
  1028. (let ((org-export-exclude-tags '("ignore")))
  1029. (org-test-with-parsed-data "* Headline :ignore:\n* Headline 2"
  1030. (org-element-map
  1031. tree 'headline
  1032. (lambda (h) (if (org-export-first-sibling-p h info) 'yes 'no))
  1033. info))))))
  1034. (ert-deftest test-org-export/last-sibling-p ()
  1035. "Test `org-export-last-sibling-p' specifications."
  1036. ;; Standard test.
  1037. (should
  1038. (equal
  1039. '(yes no yes)
  1040. (org-test-with-parsed-data "* Headline\n** Headline 2\n** Headline 3"
  1041. (org-element-map
  1042. tree 'headline
  1043. (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
  1044. info))))
  1045. ;; Ignore headlines not exported.
  1046. (should
  1047. (equal
  1048. '(yes)
  1049. (let ((org-export-exclude-tags '("ignore")))
  1050. (org-test-with-parsed-data "* Headline\n* Headline 2 :ignore:"
  1051. (org-element-map
  1052. tree 'headline
  1053. (lambda (h) (if (org-export-last-sibling-p h info) 'yes 'no))
  1054. info))))))
  1055. ;;; Links
  1056. (ert-deftest test-org-export/get-coderef-format ()
  1057. "Test `org-export-get-coderef-format' specifications."
  1058. ;; A link without description returns "%s"
  1059. (should (equal (org-export-get-coderef-format "(ref:line)" nil)
  1060. "%s"))
  1061. ;; Return "%s" when path is matched within description.
  1062. (should (equal (org-export-get-coderef-format "path" "desc (path)")
  1063. "desc %s"))
  1064. ;; Otherwise return description.
  1065. (should (equal (org-export-get-coderef-format "path" "desc")
  1066. "desc")))
  1067. (ert-deftest test-org-export/inline-image-p ()
  1068. "Test `org-export-inline-image-p' specifications."
  1069. (should
  1070. (org-export-inline-image-p
  1071. (org-test-with-temp-text "[[#id]]"
  1072. (org-element-map
  1073. (org-element-parse-buffer) 'link 'identity nil t))
  1074. '(("custom-id" . "id")))))
  1075. (ert-deftest test-org-export/fuzzy-link ()
  1076. "Test fuzzy links specifications."
  1077. ;; 1. Links to invisible (keyword) targets should be ignored.
  1078. (org-test-with-parsed-data
  1079. "Paragraph.\n#+TARGET: Test\n[[Test]]"
  1080. (should-not
  1081. (org-element-map
  1082. tree 'link
  1083. (lambda (link)
  1084. (org-export-get-ordinal
  1085. (org-export-resolve-fuzzy-link link info) info)) info)))
  1086. ;; 2. Link to an headline should return headline's number.
  1087. (org-test-with-parsed-data
  1088. "Paragraph.\n* Head1\n* Head2\n* Head3\n[[Head2]]"
  1089. (should
  1090. ;; Note: Headline's number is in fact a list of numbers.
  1091. (equal '(2)
  1092. (org-element-map
  1093. tree 'link
  1094. (lambda (link)
  1095. (org-export-get-ordinal
  1096. (org-export-resolve-fuzzy-link link info) info)) info t))))
  1097. ;; 3. Link to a target in an item should return item's number.
  1098. (org-test-with-parsed-data
  1099. "- Item1\n - Item11\n - <<test>>Item12\n- Item2\n\n\n[[test]]"
  1100. (should
  1101. ;; Note: Item's number is in fact a list of numbers.
  1102. (equal '(1 2)
  1103. (org-element-map
  1104. tree 'link
  1105. (lambda (link)
  1106. (org-export-get-ordinal
  1107. (org-export-resolve-fuzzy-link link info) info)) info t))))
  1108. ;; 4. Link to a target in a footnote should return footnote's
  1109. ;; number.
  1110. (org-test-with-parsed-data "
  1111. Paragraph[1][2][fn:lbl3:C<<target>>][[test]][[target]]\n[1] A\n\n[2] <<test>>B"
  1112. (should
  1113. (equal '(2 3)
  1114. (org-element-map
  1115. tree 'link
  1116. (lambda (link)
  1117. (org-export-get-ordinal
  1118. (org-export-resolve-fuzzy-link link info) info)) info))))
  1119. ;; 5. Link to a named element should return sequence number of that
  1120. ;; element.
  1121. (org-test-with-parsed-data
  1122. "#+NAME: tbl1\n|1|2|\n#+NAME: tbl2\n|3|4|\n#+NAME: tbl3\n|5|6|\n[[tbl2]]"
  1123. (should
  1124. (= 2
  1125. (org-element-map
  1126. tree 'link
  1127. (lambda (link)
  1128. (org-export-get-ordinal
  1129. (org-export-resolve-fuzzy-link link info) info)) info t))))
  1130. ;; 6. Link to a target not within an item, a table, a footnote
  1131. ;; reference or definition should return section number.
  1132. (org-test-with-parsed-data
  1133. "* Head1\n* Head2\nParagraph<<target>>\n* Head3\n[[target]]"
  1134. (should
  1135. (equal '(2)
  1136. (org-element-map
  1137. tree 'link
  1138. (lambda (link)
  1139. (org-export-get-ordinal
  1140. (org-export-resolve-fuzzy-link link info) info)) info t)))))
  1141. (ert-deftest test-org-export/resolve-coderef ()
  1142. "Test `org-export-resolve-coderef' specifications."
  1143. (let ((org-coderef-label-format "(ref:%s)"))
  1144. ;; 1. A link to a "-n -k -r" block returns line number.
  1145. (org-test-with-parsed-data
  1146. "#+BEGIN_EXAMPLE -n -k -r\nText (ref:coderef)\n#+END_EXAMPLE"
  1147. (should (= (org-export-resolve-coderef "coderef" info) 1)))
  1148. (org-test-with-parsed-data
  1149. "#+BEGIN_SRC emacs-lisp -n -k -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  1150. (should (= (org-export-resolve-coderef "coderef" info) 1)))
  1151. ;; 2. A link to a "-n -r" block returns line number.
  1152. (org-test-with-parsed-data
  1153. "#+BEGIN_EXAMPLE -n -r\nText (ref:coderef)\n#+END_EXAMPLE"
  1154. (should (= (org-export-resolve-coderef "coderef" info) 1)))
  1155. (org-test-with-parsed-data
  1156. "#+BEGIN_SRC emacs-lisp -n -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  1157. (should (= (org-export-resolve-coderef "coderef" info) 1)))
  1158. ;; 3. A link to a "-n" block returns coderef.
  1159. (org-test-with-parsed-data
  1160. "#+BEGIN_SRC emacs-lisp -n\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  1161. (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
  1162. (org-test-with-parsed-data
  1163. "#+BEGIN_EXAMPLE -n\nText (ref:coderef)\n#+END_EXAMPLE"
  1164. (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
  1165. ;; 4. A link to a "-r" block returns line number.
  1166. (org-test-with-parsed-data
  1167. "#+BEGIN_SRC emacs-lisp -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  1168. (should (= (org-export-resolve-coderef "coderef" info) 1)))
  1169. (org-test-with-parsed-data
  1170. "#+BEGIN_EXAMPLE -r\nText (ref:coderef)\n#+END_EXAMPLE"
  1171. (should (= (org-export-resolve-coderef "coderef" info) 1)))
  1172. ;; 5. A link to a block without a switch returns coderef.
  1173. (org-test-with-parsed-data
  1174. "#+BEGIN_SRC emacs-lisp\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  1175. (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
  1176. (org-test-with-parsed-data
  1177. "#+BEGIN_EXAMPLE\nText (ref:coderef)\n#+END_EXAMPLE"
  1178. (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
  1179. ;; 6. Correctly handle continued line numbers. A "+n" switch
  1180. ;; should resume numbering from previous block with numbered
  1181. ;; lines, ignoring blocks not numbering lines in the process.
  1182. ;; A "-n" switch resets count.
  1183. (org-test-with-parsed-data "
  1184. #+BEGIN_EXAMPLE -n
  1185. Text.
  1186. #+END_EXAMPLE
  1187. #+BEGIN_SRC emacs-lisp
  1188. \(- 1 1)
  1189. #+END_SRC
  1190. #+BEGIN_SRC emacs-lisp +n -r
  1191. \(+ 1 1) (ref:addition)
  1192. #+END_SRC
  1193. #+BEGIN_EXAMPLE -n -r
  1194. Another text. (ref:text)
  1195. #+END_EXAMPLE"
  1196. (should (= (org-export-resolve-coderef "addition" info) 2))
  1197. (should (= (org-export-resolve-coderef "text" info) 1)))
  1198. ;; 7. Recognize coderef with user-specified syntax.
  1199. (org-test-with-parsed-data
  1200. "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\nText. [ref:text]\n#+END_EXAMPLE"
  1201. (should (equal (org-export-resolve-coderef "text" info) "text")))))
  1202. (ert-deftest test-org-export/resolve-fuzzy-link ()
  1203. "Test `org-export-resolve-fuzzy-link' specifications."
  1204. ;; 1. Match target objects.
  1205. (org-test-with-parsed-data "<<target>> [[target]]"
  1206. (should
  1207. (org-export-resolve-fuzzy-link
  1208. (org-element-map tree 'link 'identity info t) info)))
  1209. ;; 2. Match target elements.
  1210. (org-test-with-parsed-data "#+TARGET: target\n[[target]]"
  1211. (should
  1212. (org-export-resolve-fuzzy-link
  1213. (org-element-map tree 'link 'identity info t) info)))
  1214. ;; 3. Match named elements.
  1215. (org-test-with-parsed-data "#+NAME: target\nParagraph\n\n[[target]]"
  1216. (should
  1217. (org-export-resolve-fuzzy-link
  1218. (org-element-map tree 'link 'identity info t) info)))
  1219. ;; 4. Match exact headline's name.
  1220. (org-test-with-parsed-data "* My headline\n[[My headline]]"
  1221. (should
  1222. (org-export-resolve-fuzzy-link
  1223. (org-element-map tree 'link 'identity info t) info)))
  1224. ;; 5. Targets objects have priority over named elements and headline
  1225. ;; titles.
  1226. (org-test-with-parsed-data
  1227. "* target\n#+NAME: target\n<<target>>\n\n[[target]]"
  1228. (should
  1229. (eq 'target
  1230. (org-element-type
  1231. (org-export-resolve-fuzzy-link
  1232. (org-element-map tree 'link 'identity info t) info)))))
  1233. ;; 6. Named elements have priority over headline titles.
  1234. (org-test-with-parsed-data
  1235. "* target\n#+NAME: target\nParagraph\n\n[[target]]"
  1236. (should
  1237. (eq 'paragraph
  1238. (org-element-type
  1239. (org-export-resolve-fuzzy-link
  1240. (org-element-map tree 'link 'identity info t) info)))))
  1241. ;; 7. If link's path starts with a "*", only match headline titles,
  1242. ;; though.
  1243. (org-test-with-parsed-data
  1244. "* target\n#+NAME: target\n<<target>>\n\n[[*target]]"
  1245. (should
  1246. (eq 'headline
  1247. (org-element-type
  1248. (org-export-resolve-fuzzy-link
  1249. (org-element-map tree 'link 'identity info t) info)))))
  1250. ;; 8. Return nil if no match.
  1251. (org-test-with-parsed-data "[[target]]"
  1252. (should-not
  1253. (org-export-resolve-fuzzy-link
  1254. (org-element-map tree 'link 'identity info t) info))))
  1255. (ert-deftest test-org-export/resolve-id-link ()
  1256. "Test `org-export-resolve-id-link' specifications."
  1257. ;; 1. Regular test for custom-id link.
  1258. (org-test-with-parsed-data "* Headline1
  1259. :PROPERTIES:
  1260. :CUSTOM-ID: test
  1261. :END:
  1262. * Headline 2
  1263. \[[#test]]"
  1264. (should
  1265. (org-export-resolve-id-link
  1266. (org-element-map tree 'link 'identity info t) info)))
  1267. ;; 2. Failing test for custom-id link.
  1268. (org-test-with-parsed-data "* Headline1
  1269. :PROPERTIES:
  1270. :CUSTOM-ID: test
  1271. :END:
  1272. * Headline 2
  1273. \[[#no-match]]"
  1274. (should-not
  1275. (org-export-resolve-id-link
  1276. (org-element-map tree 'link 'identity info t) info)))
  1277. ;; 3. Test for internal id target.
  1278. (org-test-with-parsed-data "* Headline1
  1279. :PROPERTIES:
  1280. :ID: aaaa
  1281. :END:
  1282. * Headline 2
  1283. \[[id:aaaa]]"
  1284. (should
  1285. (org-export-resolve-id-link
  1286. (org-element-map tree 'link 'identity info t) info)))
  1287. ;; 4. Test for external id target.
  1288. (org-test-with-parsed-data "[[id:aaaa]]"
  1289. (should
  1290. (org-export-resolve-id-link
  1291. (org-element-map tree 'link 'identity info t)
  1292. (org-combine-plists info '(:id-alist (("aaaa" . "external-file"))))))))
  1293. (ert-deftest test-org-export/resolve-radio-link ()
  1294. "Test `org-export-resolve-radio-link' specifications."
  1295. ;; Standard test.
  1296. (org-test-with-temp-text "<<<radio>>> radio"
  1297. (org-update-radio-target-regexp)
  1298. (should
  1299. (let* ((tree (org-element-parse-buffer))
  1300. (info `(:parse-tree ,tree)))
  1301. (org-export-resolve-radio-link
  1302. (org-element-map tree 'link 'identity info t)
  1303. info))))
  1304. ;; Radio target with objects.
  1305. (org-test-with-temp-text "<<<radio \\alpha>>> radio \\alpha"
  1306. (org-update-radio-target-regexp)
  1307. (should
  1308. (let* ((tree (org-element-parse-buffer))
  1309. (info `(:parse-tree ,tree)))
  1310. (org-export-resolve-radio-link
  1311. (org-element-map tree 'link 'identity info t)
  1312. info)))))
  1313. ;;; Src-block and example-block
  1314. (ert-deftest test-org-export/unravel-code ()
  1315. "Test `org-export-unravel-code' function."
  1316. (let ((org-coderef-label-format "(ref:%s)"))
  1317. ;; 1. Code without reference.
  1318. (org-test-with-temp-text "#+BEGIN_EXAMPLE\n(+ 1 1)\n#+END_EXAMPLE"
  1319. (should (equal (org-export-unravel-code (org-element-at-point))
  1320. '("(+ 1 1)\n"))))
  1321. ;; 2. Code with reference.
  1322. (org-test-with-temp-text
  1323. "#+BEGIN_EXAMPLE\n(+ 1 1) (ref:test)\n#+END_EXAMPLE"
  1324. (should (equal (org-export-unravel-code (org-element-at-point))
  1325. '("(+ 1 1)\n" (1 . "test")))))
  1326. ;; 3. Code with user-defined reference.
  1327. (org-test-with-temp-text
  1328. "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\n(+ 1 1) [ref:test]\n#+END_EXAMPLE"
  1329. (should (equal (org-export-unravel-code (org-element-at-point))
  1330. '("(+ 1 1)\n" (1 . "test")))))
  1331. ;; 4. Code references keys are relative to the current block.
  1332. (org-test-with-temp-text "
  1333. #+BEGIN_EXAMPLE -n
  1334. \(+ 1 1)
  1335. #+END_EXAMPLE
  1336. #+BEGIN_EXAMPLE +n
  1337. \(+ 2 2)
  1338. \(+ 3 3) (ref:one)
  1339. #+END_EXAMPLE"
  1340. (goto-line 5)
  1341. (should (equal (org-export-unravel-code (org-element-at-point))
  1342. '("(+ 2 2)\n(+ 3 3)\n" (2 . "one")))))))
  1343. ;;; Smart Quotes
  1344. (ert-deftest test-org-export/activate-smart-quotes ()
  1345. "Test `org-export-activate-smart-quotes' specifications."
  1346. ;; Opening double quotes: standard test.
  1347. (should
  1348. (equal
  1349. '("some &ldquo;paragraph")
  1350. (let ((org-export-default-language "en"))
  1351. (org-test-with-parsed-data "some \"paragraph"
  1352. (org-element-map
  1353. tree 'plain-text
  1354. (lambda (s) (org-export-activate-smart-quotes s :html info))
  1355. info)))))
  1356. ;; Opening quotes: at the beginning of a paragraph.
  1357. (should
  1358. (equal
  1359. '("&ldquo;begin")
  1360. (let ((org-export-default-language "en"))
  1361. (org-test-with-parsed-data "\"begin"
  1362. (org-element-map
  1363. tree 'plain-text
  1364. (lambda (s) (org-export-activate-smart-quotes s :html info))
  1365. info)))))
  1366. ;; Opening quotes: after an object.
  1367. (should
  1368. (equal
  1369. '("&ldquo;begin")
  1370. (let ((org-export-default-language "en"))
  1371. (org-test-with-parsed-data "=verb= \"begin"
  1372. (org-element-map
  1373. tree 'plain-text
  1374. (lambda (s) (org-export-activate-smart-quotes s :html info))
  1375. info)))))
  1376. ;; Closing quotes: standard test.
  1377. (should
  1378. (equal
  1379. '("some&rdquo; paragraph")
  1380. (let ((org-export-default-language "en"))
  1381. (org-test-with-parsed-data "some\" paragraph"
  1382. (org-element-map
  1383. tree 'plain-text
  1384. (lambda (s) (org-export-activate-smart-quotes s :html info))
  1385. info)))))
  1386. ;; Closing quotes: at the end of a paragraph.
  1387. (should
  1388. (equal
  1389. '("end&rdquo;")
  1390. (let ((org-export-default-language "en"))
  1391. (org-test-with-parsed-data "end\""
  1392. (org-element-map
  1393. tree 'plain-text
  1394. (lambda (s) (org-export-activate-smart-quotes s :html info))
  1395. info)))))
  1396. ;; Apostrophe: standard test.
  1397. (should
  1398. (equal
  1399. '("It shouldn&rsquo;t fail")
  1400. (let ((org-export-default-language "en"))
  1401. (org-test-with-parsed-data "It shouldn't fail"
  1402. (org-element-map
  1403. tree 'plain-text
  1404. (lambda (s) (org-export-activate-smart-quotes s :html info))
  1405. info)))))
  1406. ;; Apostrophe: before an object.
  1407. (should
  1408. (equal
  1409. '("a&rsquo;")
  1410. (let ((org-export-default-language "en"))
  1411. (org-test-with-parsed-data "a'=b="
  1412. (org-element-map
  1413. tree 'plain-text
  1414. (lambda (s) (org-export-activate-smart-quotes s :html info))
  1415. info)))))
  1416. ;; Apostrophe: after an object.
  1417. (should
  1418. (equal
  1419. '("&rsquo;s")
  1420. (let ((org-export-default-language "en"))
  1421. (org-test-with-parsed-data "=code='s"
  1422. (org-element-map
  1423. tree 'plain-text
  1424. (lambda (s) (org-export-activate-smart-quotes s :html info))
  1425. info)))))
  1426. ;; Special case: isolated quotes.
  1427. (should
  1428. (equal '("&ldquo;" "&rdquo;")
  1429. (let ((org-export-default-language "en"))
  1430. (org-test-with-parsed-data "\"$x$\""
  1431. (org-element-map
  1432. tree 'plain-text
  1433. (lambda (s) (org-export-activate-smart-quotes s :html info))
  1434. info)))))
  1435. ;; Smart quotes in secondary strings.
  1436. (should
  1437. (equal '("&ldquo;" "&rdquo;")
  1438. (let ((org-export-default-language "en"))
  1439. (org-test-with-parsed-data "* \"$x$\""
  1440. (org-element-map
  1441. tree 'plain-text
  1442. (lambda (s) (org-export-activate-smart-quotes s :html info))
  1443. info)))))
  1444. ;; Smart quotes in document keywords.
  1445. (should
  1446. (equal '("&ldquo;" "&rdquo;")
  1447. (let ((org-export-default-language "en"))
  1448. (org-test-with-parsed-data "#+TITLE: \"$x$\""
  1449. (org-element-map
  1450. (plist-get info :title) 'plain-text
  1451. (lambda (s) (org-export-activate-smart-quotes s :html info))
  1452. info)))))
  1453. ;; Smart quotes in parsed affiliated keywords.
  1454. (should
  1455. (equal '("&ldquo;" "&rdquo;" "Paragraph")
  1456. (let ((org-export-default-language "en"))
  1457. (org-test-with-parsed-data "#+CAPTION: \"$x$\"\nParagraph"
  1458. (org-element-map
  1459. tree 'plain-text
  1460. (lambda (s) (org-export-activate-smart-quotes s :html info))
  1461. info nil nil t))))))
  1462. ;;; Tables
  1463. (ert-deftest test-org-export/special-column ()
  1464. "Test if the table's special column is properly recognized."
  1465. ;; 1. First column is special if it contains only a special marking
  1466. ;; characters or empty cells.
  1467. (org-test-with-temp-text "
  1468. | ! | 1 |
  1469. | | 2 |"
  1470. (should
  1471. (org-export-table-has-special-column-p
  1472. (org-element-map
  1473. (org-element-parse-buffer) 'table 'identity nil 'first-match))))
  1474. ;; 2. If the column contains anything else, it isn't special.
  1475. (org-test-with-temp-text "
  1476. | ! | 1 |
  1477. | b | 2 |"
  1478. (should-not
  1479. (org-export-table-has-special-column-p
  1480. (org-element-map
  1481. (org-element-parse-buffer) 'table 'identity nil 'first-match))))
  1482. ;; 3. Special marking characters are "#", "^", "*", "_", "/", "$"
  1483. ;; and "!".
  1484. (org-test-with-temp-text "
  1485. | # | 1 |
  1486. | ^ | 2 |
  1487. | * | 3 |
  1488. | _ | 4 |
  1489. | / | 5 |
  1490. | $ | 6 |
  1491. | ! | 7 |"
  1492. (should
  1493. (org-export-table-has-special-column-p
  1494. (org-element-map
  1495. (org-element-parse-buffer) 'table 'identity nil 'first-match))))
  1496. ;; 4. A first column with only empty cells isn't considered as
  1497. ;; special.
  1498. (org-test-with-temp-text "
  1499. | | 1 |
  1500. | | 2 |"
  1501. (should-not
  1502. (org-export-table-has-special-column-p
  1503. (org-element-map
  1504. (org-element-parse-buffer) 'table 'identity nil 'first-match)))))
  1505. (ert-deftest test-org-export/table-row-is-special-p ()
  1506. "Test `org-export-table-row-is-special-p' specifications."
  1507. ;; 1. A row is special if it has a special marking character in the
  1508. ;; special column.
  1509. (org-test-with-parsed-data "| ! | 1 |"
  1510. (should
  1511. (org-export-table-row-is-special-p
  1512. (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  1513. ;; 2. A row is special when its first field is "/"
  1514. (org-test-with-parsed-data "
  1515. | / | 1 |
  1516. | a | b |"
  1517. (should
  1518. (org-export-table-row-is-special-p
  1519. (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  1520. ;; 3. A row only containing alignment cookies is also considered as
  1521. ;; special.
  1522. (org-test-with-parsed-data "| <5> | | <l> | <l22> |"
  1523. (should
  1524. (org-export-table-row-is-special-p
  1525. (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  1526. ;; 4. Everything else isn't considered as special.
  1527. (org-test-with-parsed-data "| \alpha | | c |"
  1528. (should-not
  1529. (org-export-table-row-is-special-p
  1530. (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  1531. ;; 5. Table's rules are never considered as special rows.
  1532. (org-test-with-parsed-data "|---+---|"
  1533. (should-not
  1534. (org-export-table-row-is-special-p
  1535. (org-element-map tree 'table-row 'identity nil 'first-match) info))))
  1536. (ert-deftest test-org-export/has-header-p ()
  1537. "Test `org-export-table-has-header-p' specifications."
  1538. ;; 1. With an header.
  1539. (org-test-with-parsed-data "
  1540. | a | b |
  1541. |---+---|
  1542. | c | d |"
  1543. (should
  1544. (org-export-table-has-header-p
  1545. (org-element-map tree 'table 'identity info 'first-match)
  1546. info)))
  1547. ;; 2. Without an header.
  1548. (org-test-with-parsed-data "
  1549. | a | b |
  1550. | c | d |"
  1551. (should-not
  1552. (org-export-table-has-header-p
  1553. (org-element-map tree 'table 'identity info 'first-match)
  1554. info)))
  1555. ;; 3. Don't get fooled with starting and ending rules.
  1556. (org-test-with-parsed-data "
  1557. |---+---|
  1558. | a | b |
  1559. | c | d |
  1560. |---+---|"
  1561. (should-not
  1562. (org-export-table-has-header-p
  1563. (org-element-map tree 'table 'identity info 'first-match)
  1564. info))))
  1565. (ert-deftest test-org-export/table-row-group ()
  1566. "Test `org-export-table-row-group' specifications."
  1567. ;; 1. A rule creates a new group.
  1568. (org-test-with-parsed-data "
  1569. | a | b |
  1570. |---+---|
  1571. | 1 | 2 |"
  1572. (should
  1573. (equal
  1574. '(1 nil 2)
  1575. (mapcar (lambda (row) (org-export-table-row-group row info))
  1576. (org-element-map tree 'table-row 'identity)))))
  1577. ;; 2. Special rows are ignored in count.
  1578. (org-test-with-parsed-data "
  1579. | / | < | > |
  1580. |---|---+---|
  1581. | | 1 | 2 |"
  1582. (should
  1583. (equal
  1584. '(nil nil 1)
  1585. (mapcar (lambda (row) (org-export-table-row-group row info))
  1586. (org-element-map tree 'table-row 'identity)))))
  1587. ;; 3. Double rules also are ignored in count.
  1588. (org-test-with-parsed-data "
  1589. | a | b |
  1590. |---+---|
  1591. |---+---|
  1592. | 1 | 2 |"
  1593. (should
  1594. (equal
  1595. '(1 nil nil 2)
  1596. (mapcar (lambda (row) (org-export-table-row-group row info))
  1597. (org-element-map tree 'table-row 'identity))))))
  1598. (ert-deftest test-org-export/table-cell-width ()
  1599. "Test `org-export-table-cell-width' specifications."
  1600. ;; 1. Width is primarily determined by width cookies. If no cookie
  1601. ;; is found, cell's width is nil.
  1602. (org-test-with-parsed-data "
  1603. | / | <l> | <6> | <l7> |
  1604. | | a | b | c |"
  1605. (should
  1606. (equal
  1607. '(nil 6 7)
  1608. (mapcar (lambda (cell) (org-export-table-cell-width cell info))
  1609. (org-element-map tree 'table-cell 'identity info)))))
  1610. ;; 2. The last width cookie has precedence.
  1611. (org-test-with-parsed-data "
  1612. | <6> |
  1613. | <7> |
  1614. | a |"
  1615. (should
  1616. (equal
  1617. '(7)
  1618. (mapcar (lambda (cell) (org-export-table-cell-width cell info))
  1619. (org-element-map tree 'table-cell 'identity info)))))
  1620. ;; 3. Valid width cookies must have a specific row.
  1621. (org-test-with-parsed-data "| <6> | cell |"
  1622. (should
  1623. (equal
  1624. '(nil nil)
  1625. (mapcar (lambda (cell) (org-export-table-cell-width cell info))
  1626. (org-element-map tree 'table-cell 'identity))))))
  1627. (ert-deftest test-org-export/table-cell-alignment ()
  1628. "Test `org-export-table-cell-alignment' specifications."
  1629. (let ((org-table-number-fraction 0.5)
  1630. (org-table-number-regexp "^[0-9]+$"))
  1631. ;; 1. Alignment is primarily determined by alignment cookies.
  1632. (org-test-with-temp-text "| <l> | <c> | <r> |"
  1633. (let* ((tree (org-element-parse-buffer))
  1634. (info `(:parse-tree ,tree)))
  1635. (should
  1636. (equal
  1637. '(left center right)
  1638. (mapcar (lambda (cell) (org-export-table-cell-alignment cell info))
  1639. (org-element-map tree 'table-cell 'identity))))))
  1640. ;; 2. The last alignment cookie has precedence.
  1641. (org-test-with-parsed-data "
  1642. | <l8> |
  1643. | cell |
  1644. | <r9> |"
  1645. (should
  1646. (equal
  1647. '(right right right)
  1648. (mapcar (lambda (cell) (org-export-table-cell-alignment cell info))
  1649. (org-element-map tree 'table-cell 'identity)))))
  1650. ;; 3. If there's no cookie, cell's contents determine alignment.
  1651. ;; A column mostly made of cells containing numbers will align
  1652. ;; its cells to the right.
  1653. (org-test-with-parsed-data "
  1654. | 123 |
  1655. | some text |
  1656. | 12345 |"
  1657. (should
  1658. (equal
  1659. '(right right right)
  1660. (mapcar (lambda (cell)
  1661. (org-export-table-cell-alignment cell info))
  1662. (org-element-map tree 'table-cell 'identity)))))
  1663. ;; 4. Otherwise, they will be aligned to the left.
  1664. (org-test-with-parsed-data "
  1665. | text |
  1666. | some text |
  1667. | \alpha |"
  1668. (should
  1669. (equal
  1670. '(left left left)
  1671. (mapcar (lambda (cell)
  1672. (org-export-table-cell-alignment cell info))
  1673. (org-element-map tree 'table-cell 'identity)))))))
  1674. (ert-deftest test-org-export/table-cell-borders ()
  1675. "Test `org-export-table-cell-borders' specifications."
  1676. ;; 1. Recognize various column groups indicators.
  1677. (org-test-with-parsed-data "| / | < | > | <> |"
  1678. (should
  1679. (equal
  1680. '((right bottom top) (left bottom top) (right bottom top)
  1681. (right left bottom top))
  1682. (mapcar (lambda (cell)
  1683. (org-export-table-cell-borders cell info))
  1684. (org-element-map tree 'table-cell 'identity)))))
  1685. ;; 2. Accept shortcuts to define column groups.
  1686. (org-test-with-parsed-data "| / | < | < |"
  1687. (should
  1688. (equal
  1689. '((right bottom top) (right left bottom top) (left bottom top))
  1690. (mapcar (lambda (cell)
  1691. (org-export-table-cell-borders cell info))
  1692. (org-element-map tree 'table-cell 'identity)))))
  1693. ;; 3. A valid column groups row must start with a "/".
  1694. (org-test-with-parsed-data "
  1695. | | < |
  1696. | a | b |"
  1697. (should
  1698. (equal '((top) (top) (bottom) (bottom))
  1699. (mapcar (lambda (cell)
  1700. (org-export-table-cell-borders cell info))
  1701. (org-element-map tree 'table-cell 'identity)))))
  1702. ;; 4. Take table rules into consideration.
  1703. (org-test-with-parsed-data "
  1704. | 1 |
  1705. |---|
  1706. | 2 |"
  1707. (should
  1708. (equal '((below top) (bottom above))
  1709. (mapcar (lambda (cell)
  1710. (org-export-table-cell-borders cell info))
  1711. (org-element-map tree 'table-cell 'identity)))))
  1712. ;; 5. Top and (resp. bottom) rules induce both `top' and `above'
  1713. ;; (resp. `bottom' and `below') borders. Any special row is
  1714. ;; ignored.
  1715. (org-test-with-parsed-data "
  1716. |---+----|
  1717. | / | |
  1718. | | 1 |
  1719. |---+----|"
  1720. (should
  1721. (equal '((bottom below top above))
  1722. (last
  1723. (mapcar (lambda (cell)
  1724. (org-export-table-cell-borders cell info))
  1725. (org-element-map tree 'table-cell 'identity)))))))
  1726. (ert-deftest test-org-export/table-dimensions ()
  1727. "Test `org-export-table-dimensions' specifications."
  1728. ;; 1. Standard test.
  1729. (org-test-with-parsed-data "
  1730. | 1 | 2 | 3 |
  1731. | 4 | 5 | 6 |"
  1732. (should
  1733. (equal '(2 . 3)
  1734. (org-export-table-dimensions
  1735. (org-element-map tree 'table 'identity info 'first-match) info))))
  1736. ;; 2. Ignore horizontal rules and special columns.
  1737. (org-test-with-parsed-data "
  1738. | / | < | > |
  1739. | 1 | 2 | 3 |
  1740. |---+---+---|
  1741. | 4 | 5 | 6 |"
  1742. (should
  1743. (equal '(2 . 3)
  1744. (org-export-table-dimensions
  1745. (org-element-map tree 'table 'identity info 'first-match) info)))))
  1746. (ert-deftest test-org-export/table-cell-address ()
  1747. "Test `org-export-table-cell-address' specifications."
  1748. ;; 1. Standard test: index is 0-based.
  1749. (org-test-with-parsed-data "| a | b |"
  1750. (should
  1751. (equal '((0 . 0) (0 . 1))
  1752. (org-element-map
  1753. tree 'table-cell
  1754. (lambda (cell) (org-export-table-cell-address cell info))
  1755. info))))
  1756. ;; 2. Special column isn't counted, nor are special rows.
  1757. (org-test-with-parsed-data "
  1758. | / | <> |
  1759. | | c |"
  1760. (should
  1761. (equal '(0 . 0)
  1762. (org-export-table-cell-address
  1763. (car (last (org-element-map tree 'table-cell 'identity info)))
  1764. info))))
  1765. ;; 3. Tables rules do not count either.
  1766. (org-test-with-parsed-data "
  1767. | a |
  1768. |---|
  1769. | b |
  1770. |---|
  1771. | c |"
  1772. (should
  1773. (equal '(2 . 0)
  1774. (org-export-table-cell-address
  1775. (car (last (org-element-map tree 'table-cell 'identity info)))
  1776. info))))
  1777. ;; 4. Return nil for special cells.
  1778. (org-test-with-parsed-data "| / | a |"
  1779. (should-not
  1780. (org-export-table-cell-address
  1781. (org-element-map tree 'table-cell 'identity nil 'first-match)
  1782. info))))
  1783. (ert-deftest test-org-export/get-table-cell-at ()
  1784. "Test `org-export-get-table-cell-at' specifications."
  1785. ;; 1. Address ignores special columns, special rows and rules.
  1786. (org-test-with-parsed-data "
  1787. | / | <> |
  1788. | | a |
  1789. |---+----|
  1790. | | b |"
  1791. (should
  1792. (equal '("b")
  1793. (org-element-contents
  1794. (org-export-get-table-cell-at
  1795. '(1 . 0)
  1796. (org-element-map tree 'table 'identity info 'first-match)
  1797. info)))))
  1798. ;; 2. Return value for a non-existent address is nil.
  1799. (org-test-with-parsed-data "| a |"
  1800. (should-not
  1801. (org-export-get-table-cell-at
  1802. '(2 . 2)
  1803. (org-element-map tree 'table 'identity info 'first-match)
  1804. info)))
  1805. (org-test-with-parsed-data "| / |"
  1806. (should-not
  1807. (org-export-get-table-cell-at
  1808. '(0 . 0)
  1809. (org-element-map tree 'table 'identity info 'first-match)
  1810. info))))
  1811. (ert-deftest test-org-export/table-cell-starts-colgroup-p ()
  1812. "Test `org-export-table-cell-starts-colgroup-p' specifications."
  1813. ;; 1. A cell at a beginning of a row always starts a column group.
  1814. (org-test-with-parsed-data "| a |"
  1815. (should
  1816. (org-export-table-cell-starts-colgroup-p
  1817. (org-element-map tree 'table-cell 'identity info 'first-match)
  1818. info)))
  1819. ;; 2. Special column should be ignored when determining the
  1820. ;; beginning of the row.
  1821. (org-test-with-parsed-data "
  1822. | / | |
  1823. | | a |"
  1824. (should
  1825. (org-export-table-cell-starts-colgroup-p
  1826. (org-element-map tree 'table-cell 'identity info 'first-match)
  1827. info)))
  1828. ;; 2. Explicit column groups.
  1829. (org-test-with-parsed-data "
  1830. | / | | < |
  1831. | a | b | c |"
  1832. (should
  1833. (equal
  1834. '(yes no yes)
  1835. (org-element-map
  1836. tree 'table-cell
  1837. (lambda (cell)
  1838. (if (org-export-table-cell-starts-colgroup-p cell info) 'yes 'no))
  1839. info)))))
  1840. (ert-deftest test-org-export/table-cell-ends-colgroup-p ()
  1841. "Test `org-export-table-cell-ends-colgroup-p' specifications."
  1842. ;; 1. A cell at the end of a row always ends a column group.
  1843. (org-test-with-parsed-data "| a |"
  1844. (should
  1845. (org-export-table-cell-ends-colgroup-p
  1846. (org-element-map tree 'table-cell 'identity info 'first-match)
  1847. info)))
  1848. ;; 2. Special column should be ignored when determining the
  1849. ;; beginning of the row.
  1850. (org-test-with-parsed-data "
  1851. | / | |
  1852. | | a |"
  1853. (should
  1854. (org-export-table-cell-ends-colgroup-p
  1855. (org-element-map tree 'table-cell 'identity info 'first-match)
  1856. info)))
  1857. ;; 3. Explicit column groups.
  1858. (org-test-with-parsed-data "
  1859. | / | < | |
  1860. | a | b | c |"
  1861. (should
  1862. (equal
  1863. '(yes no yes)
  1864. (org-element-map
  1865. tree 'table-cell
  1866. (lambda (cell)
  1867. (if (org-export-table-cell-ends-colgroup-p cell info) 'yes 'no))
  1868. info)))))
  1869. (ert-deftest test-org-export/table-row-starts-rowgroup-p ()
  1870. "Test `org-export-table-row-starts-rowgroup-p' specifications."
  1871. ;; 1. A row at the beginning of a table always starts a row group.
  1872. ;; So does a row following a table rule.
  1873. (org-test-with-parsed-data "
  1874. | a |
  1875. |---|
  1876. | b |"
  1877. (should
  1878. (equal
  1879. '(yes no yes)
  1880. (org-element-map
  1881. tree 'table-row
  1882. (lambda (row)
  1883. (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
  1884. info))))
  1885. ;; 2. Special rows should be ignored when determining the beginning
  1886. ;; of the row.
  1887. (org-test-with-parsed-data "
  1888. | / | < |
  1889. | | a |
  1890. |---+---|
  1891. | / | < |
  1892. | | b |"
  1893. (should
  1894. (equal
  1895. '(yes no yes)
  1896. (org-element-map
  1897. tree 'table-row
  1898. (lambda (row)
  1899. (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
  1900. info)))))
  1901. (ert-deftest test-org-export/table-row-ends-rowgroup-p ()
  1902. "Test `org-export-table-row-ends-rowgroup-p' specifications."
  1903. ;; 1. A row at the end of a table always ends a row group. So does
  1904. ;; a row preceding a table rule.
  1905. (org-test-with-parsed-data "
  1906. | a |
  1907. |---|
  1908. | b |"
  1909. (should
  1910. (equal
  1911. '(yes no yes)
  1912. (org-element-map
  1913. tree 'table-row
  1914. (lambda (row)
  1915. (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
  1916. info))))
  1917. ;; 2. Special rows should be ignored when determining the beginning
  1918. ;; of the row.
  1919. (org-test-with-parsed-data "
  1920. | | a |
  1921. | / | < |
  1922. |---+---|
  1923. | | b |
  1924. | / | < |"
  1925. (should
  1926. (equal
  1927. '(yes no yes)
  1928. (org-element-map
  1929. tree 'table-row
  1930. (lambda (row)
  1931. (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
  1932. info)))))
  1933. (ert-deftest test-org-export/table-row-starts-header-p ()
  1934. "Test `org-export-table-row-starts-header-p' specifications."
  1935. ;; 1. Only the row starting the first row group starts the table
  1936. ;; header.
  1937. (org-test-with-parsed-data "
  1938. | a |
  1939. | b |
  1940. |---|
  1941. | c |"
  1942. (should
  1943. (equal
  1944. '(yes no no no)
  1945. (org-element-map
  1946. tree 'table-row
  1947. (lambda (row)
  1948. (if (org-export-table-row-starts-header-p row info) 'yes 'no))
  1949. info))))
  1950. ;; 2. A row cannot start an header if there's no header in the
  1951. ;; table.
  1952. (org-test-with-parsed-data "
  1953. | a |
  1954. |---|"
  1955. (should-not
  1956. (org-export-table-row-starts-header-p
  1957. (org-element-map tree 'table-row 'identity info 'first-match)
  1958. info))))
  1959. (ert-deftest test-org-export/table-row-ends-header-p ()
  1960. "Test `org-export-table-row-ends-header-p' specifications."
  1961. ;; 1. Only the row starting the first row group starts the table
  1962. ;; header.
  1963. (org-test-with-parsed-data "
  1964. | a |
  1965. | b |
  1966. |---|
  1967. | c |"
  1968. (should
  1969. (equal
  1970. '(no yes no no)
  1971. (org-element-map
  1972. tree 'table-row
  1973. (lambda (row)
  1974. (if (org-export-table-row-ends-header-p row info) 'yes 'no))
  1975. info))))
  1976. ;; 2. A row cannot start an header if there's no header in the
  1977. ;; table.
  1978. (org-test-with-parsed-data "
  1979. | a |
  1980. |---|"
  1981. (should-not
  1982. (org-export-table-row-ends-header-p
  1983. (org-element-map tree 'table-row 'identity info 'first-match)
  1984. info))))
  1985. ;;; Templates
  1986. (ert-deftest test-org-export/inner-template ()
  1987. "Test `inner-template' translator specifications."
  1988. (should
  1989. (equal "Success!"
  1990. (let (org-export-registered-backends)
  1991. (org-export-define-backend test
  1992. ((inner-template . (lambda (contents info) "Success!"))
  1993. (headline . (lambda (h c i) "Headline"))))
  1994. (org-test-with-temp-text "* Headline"
  1995. (org-export-as 'test)))))
  1996. ;; Inner template is applied even in a "body-only" export.
  1997. (should
  1998. (equal "Success!"
  1999. (let (org-export-registered-backends)
  2000. (org-export-define-backend test
  2001. ((inner-template . (lambda (contents info) "Success!"))
  2002. (headline . (lambda (h c i) "Headline"))))
  2003. (org-test-with-temp-text "* Headline"
  2004. (org-export-as 'test nil nil 'body-only))))))
  2005. (ert-deftest test-org-export/template ()
  2006. "Test `template' translator specifications."
  2007. (should
  2008. (equal "Success!"
  2009. (let (org-export-registered-backends)
  2010. (org-export-define-backend test
  2011. ((template . (lambda (contents info) "Success!"))
  2012. (headline . (lambda (h c i) "Headline"))))
  2013. (org-test-with-temp-text "* Headline"
  2014. (org-export-as 'test)))))
  2015. ;; Template is not applied in a "body-only" export.
  2016. (should-not
  2017. (equal "Success!"
  2018. (let (org-export-registered-backends)
  2019. (org-export-define-backend test
  2020. ((template . (lambda (contents info) "Success!"))
  2021. (headline . (lambda (h c i) "Headline"))))
  2022. (org-test-with-temp-text "* Headline"
  2023. (org-export-as 'test nil nil 'body-only))))))
  2024. ;;; Topology
  2025. (ert-deftest test-org-export/get-next-element ()
  2026. "Test `org-export-get-next-element' specifications."
  2027. ;; Standard test.
  2028. (should
  2029. (equal "b"
  2030. (org-test-with-parsed-data "* Headline\n*a* b"
  2031. (org-export-get-next-element
  2032. (org-element-map tree 'bold 'identity info t) info))))
  2033. ;; Return nil when no previous element.
  2034. (should-not
  2035. (org-test-with-parsed-data "* Headline\na *b*"
  2036. (org-export-get-next-element
  2037. (org-element-map tree 'bold 'identity info t) info)))
  2038. ;; Non-exportable elements are ignored.
  2039. (should-not
  2040. (let ((org-export-with-timestamps nil))
  2041. (org-test-with-parsed-data "\alpha <2012-03-29 Thu>"
  2042. (org-export-get-next-element
  2043. (org-element-map tree 'entity 'identity info t) info))))
  2044. ;; Find next element in secondary strings.
  2045. (should
  2046. (eq 'verbatim
  2047. (org-test-with-parsed-data "* a =verb="
  2048. (org-element-type
  2049. (org-export-get-next-element
  2050. (org-element-map tree 'plain-text 'identity info t) info)))))
  2051. ;; Find next element in document keywords.
  2052. (should
  2053. (eq 'verbatim
  2054. (org-test-with-parsed-data "#+TITLE: a =verb="
  2055. (org-element-type
  2056. (org-export-get-next-element
  2057. (org-element-map
  2058. (plist-get info :title) 'plain-text 'identity info t) info)))))
  2059. ;; Find next element in parsed affiliated keywords.
  2060. (should
  2061. (eq 'verbatim
  2062. (org-test-with-parsed-data "#+CAPTION: a =verb=\nParagraph"
  2063. (org-element-type
  2064. (org-export-get-next-element
  2065. (org-element-map tree 'plain-text 'identity info t nil t) info)))))
  2066. ;; With optional argument N, return a list containing all the
  2067. ;; following elements.
  2068. (should
  2069. (equal
  2070. '(bold code underline)
  2071. (org-test-with-parsed-data "_a_ /b/ *c* ~d~ _e_"
  2072. (mapcar 'car
  2073. (org-export-get-next-element
  2074. (org-element-map tree 'italic 'identity info t) info t)))))
  2075. ;; When N is a positive integer, return a list containing up to
  2076. ;; N following elements.
  2077. (should
  2078. (equal
  2079. '(bold code)
  2080. (org-test-with-parsed-data "_a_ /b/ *c* ~d~ _e_"
  2081. (mapcar 'car
  2082. (org-export-get-next-element
  2083. (org-element-map tree 'italic 'identity info t) info 2))))))
  2084. (ert-deftest test-org-export/get-previous-element ()
  2085. "Test `org-export-get-previous-element' specifications."
  2086. ;; Standard test.
  2087. (should
  2088. (equal "a "
  2089. (org-test-with-parsed-data "* Headline\na *b*"
  2090. (org-export-get-previous-element
  2091. (org-element-map tree 'bold 'identity info t) info))))
  2092. ;; Return nil when no previous element.
  2093. (should-not
  2094. (org-test-with-parsed-data "* Headline\n*a* b"
  2095. (org-export-get-previous-element
  2096. (org-element-map tree 'bold 'identity info t) info)))
  2097. ;; Non-exportable elements are ignored.
  2098. (should-not
  2099. (let ((org-export-with-timestamps nil))
  2100. (org-test-with-parsed-data "<2012-03-29 Thu> \alpha"
  2101. (org-export-get-previous-element
  2102. (org-element-map tree 'entity 'identity info t) info))))
  2103. ;; Find previous element in secondary strings.
  2104. (should
  2105. (eq 'verbatim
  2106. (org-test-with-parsed-data "* =verb= a"
  2107. (org-element-type
  2108. (org-export-get-previous-element
  2109. (org-element-map tree 'plain-text 'identity info t) info)))))
  2110. ;; Find previous element in document keywords.
  2111. (should
  2112. (eq 'verbatim
  2113. (org-test-with-parsed-data "#+TITLE: =verb= a"
  2114. (org-element-type
  2115. (org-export-get-previous-element
  2116. (org-element-map
  2117. (plist-get info :title) 'plain-text 'identity info t) info)))))
  2118. ;; Find previous element in parsed affiliated keywords.
  2119. (should
  2120. (eq 'verbatim
  2121. (org-test-with-parsed-data "#+CAPTION: =verb= a\nParagraph"
  2122. (org-element-type
  2123. (org-export-get-previous-element
  2124. (org-element-map tree 'plain-text 'identity info t nil t) info)))))
  2125. ;; With optional argument N, return a list containing up to
  2126. ;; N previous elements.
  2127. (should
  2128. (equal '(bold italic underline)
  2129. (org-test-with-parsed-data "_a_ /b/ *c* ~d~"
  2130. (mapcar 'car
  2131. (org-export-get-previous-element
  2132. (org-element-map tree 'code 'identity info t) info t)))))
  2133. ;; When N is a positive integer, return a list containing up to
  2134. ;; N previous elements.
  2135. (should
  2136. (equal '(bold italic)
  2137. (org-test-with-parsed-data "_a_ /b/ *c* ~d~"
  2138. (mapcar 'car
  2139. (org-export-get-previous-element
  2140. (org-element-map tree 'code 'identity info t) info 2))))))
  2141. (provide 'test-ox)
  2142. ;;; test-org-export.el end here