test-ox.el 81 KB

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