test-ox.el 82 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472
  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. ;;; Keywords
  1222. (ert-deftest test-org-export/get-date ()
  1223. "Test `org-export-get-date' specifications."
  1224. ;; Return a properly formatted string when
  1225. ;; `org-export-date-timestamp-format' is non-nil and DATE keyword
  1226. ;; consists in a single timestamp.
  1227. (should
  1228. (equal "29 03 2012"
  1229. (let ((org-export-date-timestamp-format "%d %m %Y"))
  1230. (org-test-with-parsed-data "#+DATE: <2012-03-29 Thu>"
  1231. (org-export-get-date info)))))
  1232. ;; Return a secondary string otherwise.
  1233. (should-not
  1234. (stringp
  1235. (let ((org-export-date-timestamp-format nil))
  1236. (org-test-with-parsed-data "#+DATE: <2012-03-29 Thu>"
  1237. (org-export-get-date info)))))
  1238. (should
  1239. (equal '("Date")
  1240. (org-test-with-parsed-data "#+DATE: Date"
  1241. (org-export-get-date info))))
  1242. ;; Optional argument has precedence over
  1243. ;; `org-export-date-timestamp-format'.
  1244. (should
  1245. (equal "29 03"
  1246. (let ((org-export-date-timestamp-format "%d %m %Y"))
  1247. (org-test-with-parsed-data "#+DATE: <2012-03-29 Thu>"
  1248. (org-export-get-date info "%d %m"))))))
  1249. ;;; Links
  1250. (ert-deftest test-org-export/get-coderef-format ()
  1251. "Test `org-export-get-coderef-format' specifications."
  1252. ;; A link without description returns "%s"
  1253. (should (equal (org-export-get-coderef-format "(ref:line)" nil)
  1254. "%s"))
  1255. ;; Return "%s" when path is matched within description.
  1256. (should (equal (org-export-get-coderef-format "path" "desc (path)")
  1257. "desc %s"))
  1258. ;; Otherwise return description.
  1259. (should (equal (org-export-get-coderef-format "path" "desc")
  1260. "desc")))
  1261. (ert-deftest test-org-export/inline-image-p ()
  1262. "Test `org-export-inline-image-p' specifications."
  1263. (should
  1264. (org-export-inline-image-p
  1265. (org-test-with-temp-text "[[#id]]"
  1266. (org-element-map (org-element-parse-buffer) 'link 'identity nil t))
  1267. '(("custom-id" . "id")))))
  1268. (ert-deftest test-org-export/fuzzy-link ()
  1269. "Test fuzzy links specifications."
  1270. ;; Link to an headline should return headline's number.
  1271. (org-test-with-parsed-data
  1272. "Paragraph.\n* Head1\n* Head2\n* Head3\n[[Head2]]"
  1273. (should
  1274. ;; Note: Headline's number is in fact a list of numbers.
  1275. (equal '(2)
  1276. (org-element-map tree 'link
  1277. (lambda (link)
  1278. (org-export-get-ordinal
  1279. (org-export-resolve-fuzzy-link link info) info)) info t))))
  1280. ;; Link to a target in an item should return item's number.
  1281. (org-test-with-parsed-data
  1282. "- Item1\n - Item11\n - <<test>>Item12\n- Item2\n\n\n[[test]]"
  1283. (should
  1284. ;; Note: Item's number is in fact a list of numbers.
  1285. (equal '(1 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. ;; Link to a target in a footnote should return footnote's number.
  1291. (org-test-with-parsed-data "
  1292. Paragraph[1][2][fn:lbl3:C<<target>>][[test]][[target]]\n[1] A\n\n[2] <<test>>B"
  1293. (should
  1294. (equal '(2 3)
  1295. (org-element-map tree 'link
  1296. (lambda (link)
  1297. (org-export-get-ordinal
  1298. (org-export-resolve-fuzzy-link link info) info)) info))))
  1299. ;; Link to a named element should return sequence number of that
  1300. ;; element.
  1301. (org-test-with-parsed-data
  1302. "#+NAME: tbl1\n|1|2|\n#+NAME: tbl2\n|3|4|\n#+NAME: tbl3\n|5|6|\n[[tbl2]]"
  1303. (should
  1304. (= 2
  1305. (org-element-map tree 'link
  1306. (lambda (link)
  1307. (org-export-get-ordinal
  1308. (org-export-resolve-fuzzy-link link info) info)) info t))))
  1309. ;; Link to a target not within an item, a table, a footnote
  1310. ;; reference or definition should return section number.
  1311. (org-test-with-parsed-data
  1312. "* Head1\n* Head2\nParagraph<<target>>\n* Head3\n[[target]]"
  1313. (should
  1314. (equal '(2)
  1315. (org-element-map tree 'link
  1316. (lambda (link)
  1317. (org-export-get-ordinal
  1318. (org-export-resolve-fuzzy-link link info) info)) info t))))
  1319. ;; Space are not significant when matching a fuzzy link.
  1320. (should
  1321. (org-test-with-parsed-data "* Head 1\n[[Head\n 1]]"
  1322. (org-element-map tree 'link
  1323. (lambda (link) (org-export-resolve-fuzzy-link link info))
  1324. info t)))
  1325. ;; Statistics cookies are ignored for headline match.
  1326. (should
  1327. (org-test-with-parsed-data "* Head [0/0]\n[[Head]]"
  1328. (org-element-map tree 'link
  1329. (lambda (link) (org-export-resolve-fuzzy-link link info))
  1330. info t)))
  1331. (should
  1332. (org-test-with-parsed-data "* Head [100%]\n[[Head]]"
  1333. (org-element-map tree 'link
  1334. (lambda (link) (org-export-resolve-fuzzy-link link info))
  1335. info t))))
  1336. (ert-deftest test-org-export/resolve-coderef ()
  1337. "Test `org-export-resolve-coderef' specifications."
  1338. (let ((org-coderef-label-format "(ref:%s)"))
  1339. ;; 1. A link to a "-n -k -r" block returns line number.
  1340. (org-test-with-parsed-data
  1341. "#+BEGIN_EXAMPLE -n -k -r\nText (ref:coderef)\n#+END_EXAMPLE"
  1342. (should (= (org-export-resolve-coderef "coderef" info) 1)))
  1343. (org-test-with-parsed-data
  1344. "#+BEGIN_SRC emacs-lisp -n -k -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  1345. (should (= (org-export-resolve-coderef "coderef" info) 1)))
  1346. ;; 2. A link to a "-n -r" block returns line number.
  1347. (org-test-with-parsed-data
  1348. "#+BEGIN_EXAMPLE -n -r\nText (ref:coderef)\n#+END_EXAMPLE"
  1349. (should (= (org-export-resolve-coderef "coderef" info) 1)))
  1350. (org-test-with-parsed-data
  1351. "#+BEGIN_SRC emacs-lisp -n -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  1352. (should (= (org-export-resolve-coderef "coderef" info) 1)))
  1353. ;; 3. A link to a "-n" block returns coderef.
  1354. (org-test-with-parsed-data
  1355. "#+BEGIN_SRC emacs-lisp -n\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  1356. (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
  1357. (org-test-with-parsed-data
  1358. "#+BEGIN_EXAMPLE -n\nText (ref:coderef)\n#+END_EXAMPLE"
  1359. (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
  1360. ;; 4. A link to a "-r" block returns line number.
  1361. (org-test-with-parsed-data
  1362. "#+BEGIN_SRC emacs-lisp -r\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  1363. (should (= (org-export-resolve-coderef "coderef" info) 1)))
  1364. (org-test-with-parsed-data
  1365. "#+BEGIN_EXAMPLE -r\nText (ref:coderef)\n#+END_EXAMPLE"
  1366. (should (= (org-export-resolve-coderef "coderef" info) 1)))
  1367. ;; 5. A link to a block without a switch returns coderef.
  1368. (org-test-with-parsed-data
  1369. "#+BEGIN_SRC emacs-lisp\n(+ 1 1) (ref:coderef)\n#+END_SRC"
  1370. (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
  1371. (org-test-with-parsed-data
  1372. "#+BEGIN_EXAMPLE\nText (ref:coderef)\n#+END_EXAMPLE"
  1373. (should (equal (org-export-resolve-coderef "coderef" info) "coderef")))
  1374. ;; 6. Correctly handle continued line numbers. A "+n" switch
  1375. ;; should resume numbering from previous block with numbered
  1376. ;; lines, ignoring blocks not numbering lines in the process.
  1377. ;; A "-n" switch resets count.
  1378. (org-test-with-parsed-data "
  1379. #+BEGIN_EXAMPLE -n
  1380. Text.
  1381. #+END_EXAMPLE
  1382. #+BEGIN_SRC emacs-lisp
  1383. \(- 1 1)
  1384. #+END_SRC
  1385. #+BEGIN_SRC emacs-lisp +n -r
  1386. \(+ 1 1) (ref:addition)
  1387. #+END_SRC
  1388. #+BEGIN_EXAMPLE -n -r
  1389. Another text. (ref:text)
  1390. #+END_EXAMPLE"
  1391. (should (= (org-export-resolve-coderef "addition" info) 2))
  1392. (should (= (org-export-resolve-coderef "text" info) 1)))
  1393. ;; 7. Recognize coderef with user-specified syntax.
  1394. (org-test-with-parsed-data
  1395. "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\nText. [ref:text]\n#+END_EXAMPLE"
  1396. (should (equal (org-export-resolve-coderef "text" info) "text")))))
  1397. (ert-deftest test-org-export/resolve-fuzzy-link ()
  1398. "Test `org-export-resolve-fuzzy-link' specifications."
  1399. ;; Match target objects.
  1400. (should
  1401. (org-test-with-parsed-data "<<target>> [[target]]"
  1402. (org-export-resolve-fuzzy-link
  1403. (org-element-map tree 'link 'identity info t) info)))
  1404. ;; Match named elements.
  1405. (should
  1406. (org-test-with-parsed-data "#+NAME: target\nParagraph\n\n[[target]]"
  1407. (org-export-resolve-fuzzy-link
  1408. (org-element-map tree 'link 'identity info t) info)))
  1409. ;; Match exact headline's name.
  1410. (should
  1411. (org-test-with-parsed-data "* My headline\n[[My headline]]"
  1412. (org-export-resolve-fuzzy-link
  1413. (org-element-map tree 'link 'identity info t) info)))
  1414. ;; Targets objects have priority over named elements and headline
  1415. ;; titles.
  1416. (should
  1417. (eq 'target
  1418. (org-test-with-parsed-data
  1419. "* target\n#+NAME: target\n<<target>>\n\n[[target]]"
  1420. (org-element-type
  1421. (org-export-resolve-fuzzy-link
  1422. (org-element-map tree 'link 'identity info t) info)))))
  1423. ;; Named elements have priority over headline titles.
  1424. (should
  1425. (eq 'paragraph
  1426. (org-test-with-parsed-data
  1427. "* target\n#+NAME: target\nParagraph\n\n[[target]]"
  1428. (org-element-type
  1429. (org-export-resolve-fuzzy-link
  1430. (org-element-map tree 'link 'identity info t) info)))))
  1431. ;; If link's path starts with a "*", only match headline titles,
  1432. ;; though.
  1433. (should
  1434. (eq 'headline
  1435. (org-test-with-parsed-data
  1436. "* target\n#+NAME: target\n<<target>>\n\n[[*target]]"
  1437. (org-element-type
  1438. (org-export-resolve-fuzzy-link
  1439. (org-element-map tree 'link 'identity info t) info)))))
  1440. ;; Return nil if no match.
  1441. (should-not
  1442. (org-test-with-parsed-data "[[target]]"
  1443. (org-export-resolve-fuzzy-link
  1444. (org-element-map tree 'link 'identity info t) info))))
  1445. (ert-deftest test-org-export/resolve-id-link ()
  1446. "Test `org-export-resolve-id-link' specifications."
  1447. ;; 1. Regular test for custom-id link.
  1448. (org-test-with-parsed-data "* Headline1
  1449. :PROPERTIES:
  1450. :CUSTOM_ID: test
  1451. :END:
  1452. * Headline 2
  1453. \[[#test]]"
  1454. (should
  1455. (org-export-resolve-id-link
  1456. (org-element-map tree 'link 'identity info t) info)))
  1457. ;; 2. Failing test for custom-id link.
  1458. (org-test-with-parsed-data "* Headline1
  1459. :PROPERTIES:
  1460. :CUSTOM_ID: test
  1461. :END:
  1462. * Headline 2
  1463. \[[#no-match]]"
  1464. (should-not
  1465. (org-export-resolve-id-link
  1466. (org-element-map tree 'link 'identity info t) info)))
  1467. ;; 3. Test for internal id target.
  1468. (org-test-with-parsed-data "* Headline1
  1469. :PROPERTIES:
  1470. :ID: aaaa
  1471. :END:
  1472. * Headline 2
  1473. \[[id:aaaa]]"
  1474. (should
  1475. (org-export-resolve-id-link
  1476. (org-element-map tree 'link 'identity info t) info)))
  1477. ;; 4. Test for external id target.
  1478. (org-test-with-parsed-data "[[id:aaaa]]"
  1479. (should
  1480. (org-export-resolve-id-link
  1481. (org-element-map tree 'link 'identity info t)
  1482. (org-combine-plists info '(:id-alist (("aaaa" . "external-file"))))))))
  1483. (ert-deftest test-org-export/resolve-radio-link ()
  1484. "Test `org-export-resolve-radio-link' specifications."
  1485. ;; Standard test.
  1486. (should
  1487. (org-test-with-temp-text "<<<radio>>> radio"
  1488. (org-update-radio-target-regexp)
  1489. (let* ((tree (org-element-parse-buffer))
  1490. (info `(:parse-tree ,tree)))
  1491. (org-export-resolve-radio-link
  1492. (org-element-map tree 'link 'identity info t)
  1493. info))))
  1494. ;; Radio targets are case-insensitive.
  1495. (should
  1496. (org-test-with-temp-text "<<<RADIO>>> radio"
  1497. (org-update-radio-target-regexp)
  1498. (let* ((tree (org-element-parse-buffer))
  1499. (info `(:parse-tree ,tree)))
  1500. (org-export-resolve-radio-link
  1501. (org-element-map tree 'link 'identity info t)
  1502. info))))
  1503. ;; Radio target with objects.
  1504. (should
  1505. (org-test-with-temp-text "<<<radio \\alpha>>> radio \\alpha"
  1506. (org-update-radio-target-regexp)
  1507. (let* ((tree (org-element-parse-buffer))
  1508. (info `(:parse-tree ,tree)))
  1509. (org-export-resolve-radio-link
  1510. (org-element-map tree 'link 'identity info t)
  1511. info)))))
  1512. ;;; Src-block and example-block
  1513. (ert-deftest test-org-export/unravel-code ()
  1514. "Test `org-export-unravel-code' function."
  1515. (let ((org-coderef-label-format "(ref:%s)"))
  1516. ;; 1. Code without reference.
  1517. (org-test-with-temp-text "#+BEGIN_EXAMPLE\n(+ 1 1)\n#+END_EXAMPLE"
  1518. (should (equal (org-export-unravel-code (org-element-at-point))
  1519. '("(+ 1 1)\n"))))
  1520. ;; 2. Code with reference.
  1521. (org-test-with-temp-text
  1522. "#+BEGIN_EXAMPLE\n(+ 1 1) (ref:test)\n#+END_EXAMPLE"
  1523. (should (equal (org-export-unravel-code (org-element-at-point))
  1524. '("(+ 1 1)\n" (1 . "test")))))
  1525. ;; 3. Code with user-defined reference.
  1526. (org-test-with-temp-text
  1527. "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\n(+ 1 1) [ref:test]\n#+END_EXAMPLE"
  1528. (should (equal (org-export-unravel-code (org-element-at-point))
  1529. '("(+ 1 1)\n" (1 . "test")))))
  1530. ;; 4. Code references keys are relative to the current block.
  1531. (org-test-with-temp-text "
  1532. #+BEGIN_EXAMPLE -n
  1533. \(+ 1 1)
  1534. #+END_EXAMPLE
  1535. #+BEGIN_EXAMPLE +n
  1536. \(+ 2 2)
  1537. \(+ 3 3) (ref:one)
  1538. #+END_EXAMPLE"
  1539. (goto-line 5)
  1540. (should (equal (org-export-unravel-code (org-element-at-point))
  1541. '("(+ 2 2)\n(+ 3 3)\n" (2 . "one")))))))
  1542. (ert-deftest test-org-export/format-code-default ()
  1543. "Test `org-export-format-code-default' specifications."
  1544. ;; Return the empty string when code is empty.
  1545. (should
  1546. (equal ""
  1547. (org-test-with-parsed-data "#+BEGIN_SRC emacs-lisp\n\n\n#+END_SRC"
  1548. (org-export-format-code-default
  1549. (org-element-map tree 'src-block 'identity info t) info))))
  1550. ;; Number lines, two whitespace characters before the actual loc.
  1551. (should
  1552. (equal "1 a\n2 b\n"
  1553. (org-test-with-parsed-data
  1554. "#+BEGIN_SRC emacs-lisp +n\na\nb\n#+END_SRC"
  1555. (org-export-format-code-default
  1556. (org-element-map tree 'src-block 'identity info t) info))))
  1557. ;; Put references 6 whitespace characters after the widest line,
  1558. ;; wrapped within parenthesis.
  1559. (should
  1560. (equal "123 (a)\n1 (b)\n"
  1561. (let ((org-coderef-label-format "(ref:%s)"))
  1562. (org-test-with-parsed-data
  1563. "#+BEGIN_SRC emacs-lisp\n123 (ref:a)\n1 (ref:b)\n#+END_SRC"
  1564. (org-export-format-code-default
  1565. (org-element-map tree 'src-block 'identity info t) info))))))
  1566. ;;; Smart Quotes
  1567. (ert-deftest test-org-export/activate-smart-quotes ()
  1568. "Test `org-export-activate-smart-quotes' specifications."
  1569. ;; Opening double quotes: standard test.
  1570. (should
  1571. (equal
  1572. '("some &ldquo;paragraph")
  1573. (let ((org-export-default-language "en"))
  1574. (org-test-with-parsed-data "some \"paragraph"
  1575. (org-element-map tree 'plain-text
  1576. (lambda (s) (org-export-activate-smart-quotes s :html info))
  1577. info)))))
  1578. ;; Opening quotes: at the beginning of a paragraph.
  1579. (should
  1580. (equal
  1581. '("&ldquo;begin")
  1582. (let ((org-export-default-language "en"))
  1583. (org-test-with-parsed-data "\"begin"
  1584. (org-element-map tree 'plain-text
  1585. (lambda (s) (org-export-activate-smart-quotes s :html info))
  1586. info)))))
  1587. ;; Opening quotes: after an object.
  1588. (should
  1589. (equal
  1590. '("&ldquo;begin")
  1591. (let ((org-export-default-language "en"))
  1592. (org-test-with-parsed-data "=verb= \"begin"
  1593. (org-element-map tree 'plain-text
  1594. (lambda (s) (org-export-activate-smart-quotes s :html info))
  1595. info)))))
  1596. ;; Closing quotes: standard test.
  1597. (should
  1598. (equal
  1599. '("some&rdquo; paragraph")
  1600. (let ((org-export-default-language "en"))
  1601. (org-test-with-parsed-data "some\" paragraph"
  1602. (org-element-map tree 'plain-text
  1603. (lambda (s) (org-export-activate-smart-quotes s :html info))
  1604. info)))))
  1605. ;; Closing quotes: at the end of a paragraph.
  1606. (should
  1607. (equal
  1608. '("end&rdquo;")
  1609. (let ((org-export-default-language "en"))
  1610. (org-test-with-parsed-data "end\""
  1611. (org-element-map tree 'plain-text
  1612. (lambda (s) (org-export-activate-smart-quotes s :html info))
  1613. info)))))
  1614. ;; Apostrophe: standard test.
  1615. (should
  1616. (equal
  1617. '("It shouldn&rsquo;t fail")
  1618. (let ((org-export-default-language "en"))
  1619. (org-test-with-parsed-data "It shouldn't fail"
  1620. (org-element-map tree 'plain-text
  1621. (lambda (s) (org-export-activate-smart-quotes s :html info))
  1622. info)))))
  1623. ;; Apostrophe: before an object.
  1624. (should
  1625. (equal
  1626. '("a&rsquo;")
  1627. (let ((org-export-default-language "en"))
  1628. (org-test-with-parsed-data "a'=b="
  1629. (org-element-map tree 'plain-text
  1630. (lambda (s) (org-export-activate-smart-quotes s :html info))
  1631. info)))))
  1632. ;; Apostrophe: after an object.
  1633. (should
  1634. (equal
  1635. '("&rsquo;s")
  1636. (let ((org-export-default-language "en"))
  1637. (org-test-with-parsed-data "=code='s"
  1638. (org-element-map tree 'plain-text
  1639. (lambda (s) (org-export-activate-smart-quotes s :html info))
  1640. info)))))
  1641. ;; Special case: isolated quotes.
  1642. (should
  1643. (equal '("&ldquo;" "&rdquo;")
  1644. (let ((org-export-default-language "en"))
  1645. (org-test-with-parsed-data "\"$x$\""
  1646. (org-element-map tree 'plain-text
  1647. (lambda (s) (org-export-activate-smart-quotes s :html info))
  1648. info)))))
  1649. ;; Smart quotes in secondary strings.
  1650. (should
  1651. (equal '("&ldquo;" "&rdquo;")
  1652. (let ((org-export-default-language "en"))
  1653. (org-test-with-parsed-data "* \"$x$\""
  1654. (org-element-map tree 'plain-text
  1655. (lambda (s) (org-export-activate-smart-quotes s :html info))
  1656. info)))))
  1657. ;; Smart quotes in document keywords.
  1658. (should
  1659. (equal '("&ldquo;" "&rdquo;")
  1660. (let ((org-export-default-language "en"))
  1661. (org-test-with-parsed-data "#+TITLE: \"$x$\""
  1662. (org-element-map (plist-get info :title) 'plain-text
  1663. (lambda (s) (org-export-activate-smart-quotes s :html info))
  1664. info)))))
  1665. ;; Smart quotes in parsed affiliated keywords.
  1666. (should
  1667. (equal '("&ldquo;" "&rdquo;" "Paragraph")
  1668. (let ((org-export-default-language "en"))
  1669. (org-test-with-parsed-data "#+CAPTION: \"$x$\"\nParagraph"
  1670. (org-element-map tree 'plain-text
  1671. (lambda (s) (org-export-activate-smart-quotes s :html info))
  1672. info nil nil t))))))
  1673. ;;; Tables
  1674. (ert-deftest test-org-export/special-column ()
  1675. "Test if the table's special column is properly recognized."
  1676. ;; 1. First column is special if it contains only a special marking
  1677. ;; characters or empty cells.
  1678. (org-test-with-temp-text "
  1679. | ! | 1 |
  1680. | | 2 |"
  1681. (should
  1682. (org-export-table-has-special-column-p
  1683. (org-element-map
  1684. (org-element-parse-buffer) 'table 'identity nil 'first-match))))
  1685. ;; 2. If the column contains anything else, it isn't special.
  1686. (org-test-with-temp-text "
  1687. | ! | 1 |
  1688. | b | 2 |"
  1689. (should-not
  1690. (org-export-table-has-special-column-p
  1691. (org-element-map
  1692. (org-element-parse-buffer) 'table 'identity nil 'first-match))))
  1693. ;; 3. Special marking characters are "#", "^", "*", "_", "/", "$"
  1694. ;; and "!".
  1695. (org-test-with-temp-text "
  1696. | # | 1 |
  1697. | ^ | 2 |
  1698. | * | 3 |
  1699. | _ | 4 |
  1700. | / | 5 |
  1701. | $ | 6 |
  1702. | ! | 7 |"
  1703. (should
  1704. (org-export-table-has-special-column-p
  1705. (org-element-map
  1706. (org-element-parse-buffer) 'table 'identity nil 'first-match))))
  1707. ;; 4. A first column with only empty cells isn't considered as
  1708. ;; special.
  1709. (org-test-with-temp-text "
  1710. | | 1 |
  1711. | | 2 |"
  1712. (should-not
  1713. (org-export-table-has-special-column-p
  1714. (org-element-map
  1715. (org-element-parse-buffer) 'table 'identity nil 'first-match)))))
  1716. (ert-deftest test-org-export/table-row-is-special-p ()
  1717. "Test `org-export-table-row-is-special-p' specifications."
  1718. ;; 1. A row is special if it has a special marking character in the
  1719. ;; special column.
  1720. (org-test-with-parsed-data "| ! | 1 |"
  1721. (should
  1722. (org-export-table-row-is-special-p
  1723. (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  1724. ;; 2. A row is special when its first field is "/"
  1725. (org-test-with-parsed-data "
  1726. | / | 1 |
  1727. | a | b |"
  1728. (should
  1729. (org-export-table-row-is-special-p
  1730. (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  1731. ;; 3. A row only containing alignment cookies is also considered as
  1732. ;; special.
  1733. (org-test-with-parsed-data "| <5> | | <l> | <l22> |"
  1734. (should
  1735. (org-export-table-row-is-special-p
  1736. (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  1737. ;; 4. Everything else isn't considered as special.
  1738. (org-test-with-parsed-data "| \alpha | | c |"
  1739. (should-not
  1740. (org-export-table-row-is-special-p
  1741. (org-element-map tree 'table-row 'identity nil 'first-match) info)))
  1742. ;; 5. Table's rules are never considered as special rows.
  1743. (org-test-with-parsed-data "|---+---|"
  1744. (should-not
  1745. (org-export-table-row-is-special-p
  1746. (org-element-map tree 'table-row 'identity nil 'first-match) info))))
  1747. (ert-deftest test-org-export/has-header-p ()
  1748. "Test `org-export-table-has-header-p' specifications."
  1749. ;; 1. With an header.
  1750. (org-test-with-parsed-data "
  1751. | a | b |
  1752. |---+---|
  1753. | c | d |"
  1754. (should
  1755. (org-export-table-has-header-p
  1756. (org-element-map tree 'table 'identity info 'first-match)
  1757. info)))
  1758. ;; 2. Without an header.
  1759. (org-test-with-parsed-data "
  1760. | a | b |
  1761. | c | d |"
  1762. (should-not
  1763. (org-export-table-has-header-p
  1764. (org-element-map tree 'table 'identity info 'first-match)
  1765. info)))
  1766. ;; 3. Don't get fooled with starting and ending rules.
  1767. (org-test-with-parsed-data "
  1768. |---+---|
  1769. | a | b |
  1770. | c | d |
  1771. |---+---|"
  1772. (should-not
  1773. (org-export-table-has-header-p
  1774. (org-element-map tree 'table 'identity info 'first-match)
  1775. info))))
  1776. (ert-deftest test-org-export/table-row-group ()
  1777. "Test `org-export-table-row-group' specifications."
  1778. ;; 1. A rule creates a new group.
  1779. (org-test-with-parsed-data "
  1780. | a | b |
  1781. |---+---|
  1782. | 1 | 2 |"
  1783. (should
  1784. (equal
  1785. '(1 nil 2)
  1786. (mapcar (lambda (row) (org-export-table-row-group row info))
  1787. (org-element-map tree 'table-row 'identity)))))
  1788. ;; 2. Special rows are ignored in count.
  1789. (org-test-with-parsed-data "
  1790. | / | < | > |
  1791. |---|---+---|
  1792. | | 1 | 2 |"
  1793. (should
  1794. (equal
  1795. '(nil nil 1)
  1796. (mapcar (lambda (row) (org-export-table-row-group row info))
  1797. (org-element-map tree 'table-row 'identity)))))
  1798. ;; 3. Double rules also are ignored in count.
  1799. (org-test-with-parsed-data "
  1800. | a | b |
  1801. |---+---|
  1802. |---+---|
  1803. | 1 | 2 |"
  1804. (should
  1805. (equal
  1806. '(1 nil nil 2)
  1807. (mapcar (lambda (row) (org-export-table-row-group row info))
  1808. (org-element-map tree 'table-row 'identity))))))
  1809. (ert-deftest test-org-export/table-cell-width ()
  1810. "Test `org-export-table-cell-width' specifications."
  1811. ;; 1. Width is primarily determined by width cookies. If no cookie
  1812. ;; is found, cell's width is nil.
  1813. (org-test-with-parsed-data "
  1814. | / | <l> | <6> | <l7> |
  1815. | | a | b | c |"
  1816. (should
  1817. (equal
  1818. '(nil 6 7)
  1819. (mapcar (lambda (cell) (org-export-table-cell-width cell info))
  1820. (org-element-map tree 'table-cell 'identity info)))))
  1821. ;; 2. The last width cookie has precedence.
  1822. (org-test-with-parsed-data "
  1823. | <6> |
  1824. | <7> |
  1825. | a |"
  1826. (should
  1827. (equal
  1828. '(7)
  1829. (mapcar (lambda (cell) (org-export-table-cell-width cell info))
  1830. (org-element-map tree 'table-cell 'identity info)))))
  1831. ;; 3. Valid width cookies must have a specific row.
  1832. (org-test-with-parsed-data "| <6> | cell |"
  1833. (should
  1834. (equal
  1835. '(nil nil)
  1836. (mapcar (lambda (cell) (org-export-table-cell-width cell info))
  1837. (org-element-map tree 'table-cell 'identity))))))
  1838. (ert-deftest test-org-export/table-cell-alignment ()
  1839. "Test `org-export-table-cell-alignment' specifications."
  1840. (let ((org-table-number-fraction 0.5)
  1841. (org-table-number-regexp "^[0-9]+$"))
  1842. ;; 1. Alignment is primarily determined by alignment cookies.
  1843. (org-test-with-temp-text "| <l> | <c> | <r> |"
  1844. (let* ((tree (org-element-parse-buffer))
  1845. (info `(:parse-tree ,tree)))
  1846. (should
  1847. (equal
  1848. '(left center right)
  1849. (mapcar (lambda (cell) (org-export-table-cell-alignment cell info))
  1850. (org-element-map tree 'table-cell 'identity))))))
  1851. ;; 2. The last alignment cookie has precedence.
  1852. (org-test-with-parsed-data "
  1853. | <l8> |
  1854. | cell |
  1855. | <r9> |"
  1856. (should
  1857. (equal
  1858. '(right right right)
  1859. (mapcar (lambda (cell) (org-export-table-cell-alignment cell info))
  1860. (org-element-map tree 'table-cell 'identity)))))
  1861. ;; 3. If there's no cookie, cell's contents determine alignment.
  1862. ;; A column mostly made of cells containing numbers will align
  1863. ;; its cells to the right.
  1864. (org-test-with-parsed-data "
  1865. | 123 |
  1866. | some text |
  1867. | 12345 |"
  1868. (should
  1869. (equal
  1870. '(right right right)
  1871. (mapcar (lambda (cell)
  1872. (org-export-table-cell-alignment cell info))
  1873. (org-element-map tree 'table-cell 'identity)))))
  1874. ;; 4. Otherwise, they will be aligned to the left.
  1875. (org-test-with-parsed-data "
  1876. | text |
  1877. | some text |
  1878. | \alpha |"
  1879. (should
  1880. (equal
  1881. '(left left left)
  1882. (mapcar (lambda (cell)
  1883. (org-export-table-cell-alignment cell info))
  1884. (org-element-map tree 'table-cell 'identity)))))))
  1885. (ert-deftest test-org-export/table-cell-borders ()
  1886. "Test `org-export-table-cell-borders' specifications."
  1887. ;; 1. Recognize various column groups indicators.
  1888. (org-test-with-parsed-data "| / | < | > | <> |"
  1889. (should
  1890. (equal
  1891. '((right bottom top) (left bottom top) (right bottom top)
  1892. (right left bottom top))
  1893. (mapcar (lambda (cell)
  1894. (org-export-table-cell-borders cell info))
  1895. (org-element-map tree 'table-cell 'identity)))))
  1896. ;; 2. Accept shortcuts to define column groups.
  1897. (org-test-with-parsed-data "| / | < | < |"
  1898. (should
  1899. (equal
  1900. '((right bottom top) (right left bottom top) (left bottom top))
  1901. (mapcar (lambda (cell)
  1902. (org-export-table-cell-borders cell info))
  1903. (org-element-map tree 'table-cell 'identity)))))
  1904. ;; 3. A valid column groups row must start with a "/".
  1905. (org-test-with-parsed-data "
  1906. | | < |
  1907. | a | b |"
  1908. (should
  1909. (equal '((top) (top) (bottom) (bottom))
  1910. (mapcar (lambda (cell)
  1911. (org-export-table-cell-borders cell info))
  1912. (org-element-map tree 'table-cell 'identity)))))
  1913. ;; 4. Take table rules into consideration.
  1914. (org-test-with-parsed-data "
  1915. | 1 |
  1916. |---|
  1917. | 2 |"
  1918. (should
  1919. (equal '((below top) (bottom above))
  1920. (mapcar (lambda (cell)
  1921. (org-export-table-cell-borders cell info))
  1922. (org-element-map tree 'table-cell 'identity)))))
  1923. ;; 5. Top and (resp. bottom) rules induce both `top' and `above'
  1924. ;; (resp. `bottom' and `below') borders. Any special row is
  1925. ;; ignored.
  1926. (org-test-with-parsed-data "
  1927. |---+----|
  1928. | / | |
  1929. | | 1 |
  1930. |---+----|"
  1931. (should
  1932. (equal '((bottom below top above))
  1933. (last
  1934. (mapcar (lambda (cell)
  1935. (org-export-table-cell-borders cell info))
  1936. (org-element-map tree 'table-cell 'identity)))))))
  1937. (ert-deftest test-org-export/table-dimensions ()
  1938. "Test `org-export-table-dimensions' specifications."
  1939. ;; 1. Standard test.
  1940. (org-test-with-parsed-data "
  1941. | 1 | 2 | 3 |
  1942. | 4 | 5 | 6 |"
  1943. (should
  1944. (equal '(2 . 3)
  1945. (org-export-table-dimensions
  1946. (org-element-map tree 'table 'identity info 'first-match) info))))
  1947. ;; 2. Ignore horizontal rules and special columns.
  1948. (org-test-with-parsed-data "
  1949. | / | < | > |
  1950. | 1 | 2 | 3 |
  1951. |---+---+---|
  1952. | 4 | 5 | 6 |"
  1953. (should
  1954. (equal '(2 . 3)
  1955. (org-export-table-dimensions
  1956. (org-element-map tree 'table 'identity info 'first-match) info)))))
  1957. (ert-deftest test-org-export/table-cell-address ()
  1958. "Test `org-export-table-cell-address' specifications."
  1959. ;; 1. Standard test: index is 0-based.
  1960. (org-test-with-parsed-data "| a | b |"
  1961. (should
  1962. (equal '((0 . 0) (0 . 1))
  1963. (org-element-map tree 'table-cell
  1964. (lambda (cell) (org-export-table-cell-address cell info))
  1965. info))))
  1966. ;; 2. Special column isn't counted, nor are special rows.
  1967. (org-test-with-parsed-data "
  1968. | / | <> |
  1969. | | c |"
  1970. (should
  1971. (equal '(0 . 0)
  1972. (org-export-table-cell-address
  1973. (car (last (org-element-map tree 'table-cell 'identity info)))
  1974. info))))
  1975. ;; 3. Tables rules do not count either.
  1976. (org-test-with-parsed-data "
  1977. | a |
  1978. |---|
  1979. | b |
  1980. |---|
  1981. | c |"
  1982. (should
  1983. (equal '(2 . 0)
  1984. (org-export-table-cell-address
  1985. (car (last (org-element-map tree 'table-cell 'identity info)))
  1986. info))))
  1987. ;; 4. Return nil for special cells.
  1988. (org-test-with-parsed-data "| / | a |"
  1989. (should-not
  1990. (org-export-table-cell-address
  1991. (org-element-map tree 'table-cell 'identity nil 'first-match)
  1992. info))))
  1993. (ert-deftest test-org-export/get-table-cell-at ()
  1994. "Test `org-export-get-table-cell-at' specifications."
  1995. ;; 1. Address ignores special columns, special rows and rules.
  1996. (org-test-with-parsed-data "
  1997. | / | <> |
  1998. | | a |
  1999. |---+----|
  2000. | | b |"
  2001. (should
  2002. (equal '("b")
  2003. (org-element-contents
  2004. (org-export-get-table-cell-at
  2005. '(1 . 0)
  2006. (org-element-map tree 'table 'identity info 'first-match)
  2007. info)))))
  2008. ;; 2. Return value for a non-existent address is nil.
  2009. (org-test-with-parsed-data "| a |"
  2010. (should-not
  2011. (org-export-get-table-cell-at
  2012. '(2 . 2)
  2013. (org-element-map tree 'table 'identity info 'first-match)
  2014. info)))
  2015. (org-test-with-parsed-data "| / |"
  2016. (should-not
  2017. (org-export-get-table-cell-at
  2018. '(0 . 0)
  2019. (org-element-map tree 'table 'identity info 'first-match)
  2020. info))))
  2021. (ert-deftest test-org-export/table-cell-starts-colgroup-p ()
  2022. "Test `org-export-table-cell-starts-colgroup-p' specifications."
  2023. ;; 1. A cell at a beginning of a row always starts a column group.
  2024. (org-test-with-parsed-data "| a |"
  2025. (should
  2026. (org-export-table-cell-starts-colgroup-p
  2027. (org-element-map tree 'table-cell 'identity info 'first-match)
  2028. info)))
  2029. ;; 2. Special column should be ignored when determining the
  2030. ;; beginning of the row.
  2031. (org-test-with-parsed-data "
  2032. | / | |
  2033. | | a |"
  2034. (should
  2035. (org-export-table-cell-starts-colgroup-p
  2036. (org-element-map tree 'table-cell 'identity info 'first-match)
  2037. info)))
  2038. ;; 2. Explicit column groups.
  2039. (org-test-with-parsed-data "
  2040. | / | | < |
  2041. | a | b | c |"
  2042. (should
  2043. (equal
  2044. '(yes no yes)
  2045. (org-element-map tree 'table-cell
  2046. (lambda (cell)
  2047. (if (org-export-table-cell-starts-colgroup-p cell info) 'yes 'no))
  2048. info)))))
  2049. (ert-deftest test-org-export/table-cell-ends-colgroup-p ()
  2050. "Test `org-export-table-cell-ends-colgroup-p' specifications."
  2051. ;; 1. A cell at the end of a row always ends a column group.
  2052. (org-test-with-parsed-data "| a |"
  2053. (should
  2054. (org-export-table-cell-ends-colgroup-p
  2055. (org-element-map tree 'table-cell 'identity info 'first-match)
  2056. info)))
  2057. ;; 2. Special column should be ignored when determining the
  2058. ;; beginning of the row.
  2059. (org-test-with-parsed-data "
  2060. | / | |
  2061. | | a |"
  2062. (should
  2063. (org-export-table-cell-ends-colgroup-p
  2064. (org-element-map tree 'table-cell 'identity info 'first-match)
  2065. info)))
  2066. ;; 3. Explicit column groups.
  2067. (org-test-with-parsed-data "
  2068. | / | < | |
  2069. | a | b | c |"
  2070. (should
  2071. (equal
  2072. '(yes no yes)
  2073. (org-element-map tree 'table-cell
  2074. (lambda (cell)
  2075. (if (org-export-table-cell-ends-colgroup-p cell info) 'yes 'no))
  2076. info)))))
  2077. (ert-deftest test-org-export/table-row-starts-rowgroup-p ()
  2078. "Test `org-export-table-row-starts-rowgroup-p' specifications."
  2079. ;; 1. A row at the beginning of a table always starts a row group.
  2080. ;; So does a row following a table rule.
  2081. (org-test-with-parsed-data "
  2082. | a |
  2083. |---|
  2084. | b |"
  2085. (should
  2086. (equal
  2087. '(yes no yes)
  2088. (org-element-map tree 'table-row
  2089. (lambda (row)
  2090. (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
  2091. info))))
  2092. ;; 2. Special rows should be ignored when determining the beginning
  2093. ;; of the row.
  2094. (org-test-with-parsed-data "
  2095. | / | < |
  2096. | | a |
  2097. |---+---|
  2098. | / | < |
  2099. | | b |"
  2100. (should
  2101. (equal
  2102. '(yes no yes)
  2103. (org-element-map tree 'table-row
  2104. (lambda (row)
  2105. (if (org-export-table-row-starts-rowgroup-p row info) 'yes 'no))
  2106. info)))))
  2107. (ert-deftest test-org-export/table-row-ends-rowgroup-p ()
  2108. "Test `org-export-table-row-ends-rowgroup-p' specifications."
  2109. ;; 1. A row at the end of a table always ends a row group. So does
  2110. ;; a row preceding a table rule.
  2111. (org-test-with-parsed-data "
  2112. | a |
  2113. |---|
  2114. | b |"
  2115. (should
  2116. (equal
  2117. '(yes no yes)
  2118. (org-element-map tree 'table-row
  2119. (lambda (row)
  2120. (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
  2121. info))))
  2122. ;; 2. Special rows should be ignored when determining the beginning
  2123. ;; of the row.
  2124. (org-test-with-parsed-data "
  2125. | | a |
  2126. | / | < |
  2127. |---+---|
  2128. | | b |
  2129. | / | < |"
  2130. (should
  2131. (equal
  2132. '(yes no yes)
  2133. (org-element-map tree 'table-row
  2134. (lambda (row)
  2135. (if (org-export-table-row-ends-rowgroup-p row info) 'yes 'no))
  2136. info)))))
  2137. (ert-deftest test-org-export/table-row-starts-header-p ()
  2138. "Test `org-export-table-row-starts-header-p' specifications."
  2139. ;; 1. Only the row starting the first row group starts the table
  2140. ;; header.
  2141. (org-test-with-parsed-data "
  2142. | a |
  2143. | b |
  2144. |---|
  2145. | c |"
  2146. (should
  2147. (equal
  2148. '(yes no no no)
  2149. (org-element-map tree 'table-row
  2150. (lambda (row)
  2151. (if (org-export-table-row-starts-header-p row info) 'yes 'no))
  2152. info))))
  2153. ;; 2. A row cannot start an header if there's no header in the
  2154. ;; table.
  2155. (org-test-with-parsed-data "
  2156. | a |
  2157. |---|"
  2158. (should-not
  2159. (org-export-table-row-starts-header-p
  2160. (org-element-map tree 'table-row 'identity info 'first-match)
  2161. info))))
  2162. (ert-deftest test-org-export/table-row-ends-header-p ()
  2163. "Test `org-export-table-row-ends-header-p' specifications."
  2164. ;; 1. Only the row starting the first row group starts the table
  2165. ;; header.
  2166. (org-test-with-parsed-data "
  2167. | a |
  2168. | b |
  2169. |---|
  2170. | c |"
  2171. (should
  2172. (equal
  2173. '(no yes no no)
  2174. (org-element-map tree 'table-row
  2175. (lambda (row)
  2176. (if (org-export-table-row-ends-header-p row info) 'yes 'no))
  2177. info))))
  2178. ;; 2. A row cannot start an header if there's no header in the
  2179. ;; table.
  2180. (org-test-with-parsed-data "
  2181. | a |
  2182. |---|"
  2183. (should-not
  2184. (org-export-table-row-ends-header-p
  2185. (org-element-map tree 'table-row 'identity info 'first-match)
  2186. info))))
  2187. ;;; Templates
  2188. (ert-deftest test-org-export/inner-template ()
  2189. "Test `inner-template' translator specifications."
  2190. (should
  2191. (equal "Success!"
  2192. (let (org-export-registered-backends)
  2193. (org-export-define-backend 'test
  2194. '((inner-template . (lambda (contents info) "Success!"))
  2195. (headline . (lambda (h c i) "Headline"))))
  2196. (org-test-with-temp-text "* Headline"
  2197. (org-export-as 'test)))))
  2198. ;; Inner template is applied even in a "body-only" export.
  2199. (should
  2200. (equal "Success!"
  2201. (let (org-export-registered-backends)
  2202. (org-export-define-backend 'test
  2203. '((inner-template . (lambda (contents info) "Success!"))
  2204. (headline . (lambda (h c i) "Headline"))))
  2205. (org-test-with-temp-text "* Headline"
  2206. (org-export-as 'test nil nil 'body-only))))))
  2207. (ert-deftest test-org-export/template ()
  2208. "Test `template' translator specifications."
  2209. (should
  2210. (equal "Success!"
  2211. (let (org-export-registered-backends)
  2212. (org-export-define-backend 'test
  2213. '((template . (lambda (contents info) "Success!"))
  2214. (headline . (lambda (h c i) "Headline"))))
  2215. (org-test-with-temp-text "* Headline"
  2216. (org-export-as 'test)))))
  2217. ;; Template is not applied in a "body-only" export.
  2218. (should-not
  2219. (equal "Success!"
  2220. (let (org-export-registered-backends)
  2221. (org-export-define-backend 'test
  2222. '((template . (lambda (contents info) "Success!"))
  2223. (headline . (lambda (h c i) "Headline"))))
  2224. (org-test-with-temp-text "* Headline"
  2225. (org-export-as 'test nil nil 'body-only))))))
  2226. ;;; Topology
  2227. (ert-deftest test-org-export/get-next-element ()
  2228. "Test `org-export-get-next-element' specifications."
  2229. ;; Standard test.
  2230. (should
  2231. (equal "b"
  2232. (org-test-with-parsed-data "* Headline\n*a* b"
  2233. (org-export-get-next-element
  2234. (org-element-map tree 'bold 'identity info t) info))))
  2235. ;; Return nil when no previous element.
  2236. (should-not
  2237. (org-test-with-parsed-data "* Headline\na *b*"
  2238. (org-export-get-next-element
  2239. (org-element-map tree 'bold 'identity info t) info)))
  2240. ;; Non-exportable elements are ignored.
  2241. (should-not
  2242. (let ((org-export-with-timestamps nil))
  2243. (org-test-with-parsed-data "\alpha <2012-03-29 Thu>"
  2244. (org-export-get-next-element
  2245. (org-element-map tree 'entity 'identity info t) info))))
  2246. ;; Find next element in secondary strings.
  2247. (should
  2248. (eq 'verbatim
  2249. (org-test-with-parsed-data "* a =verb="
  2250. (org-element-type
  2251. (org-export-get-next-element
  2252. (org-element-map tree 'plain-text 'identity info t) info)))))
  2253. ;; Find next element in document keywords.
  2254. (should
  2255. (eq 'verbatim
  2256. (org-test-with-parsed-data "#+TITLE: a =verb="
  2257. (org-element-type
  2258. (org-export-get-next-element
  2259. (org-element-map
  2260. (plist-get info :title) 'plain-text 'identity info t) info)))))
  2261. ;; Find next element in parsed affiliated keywords.
  2262. (should
  2263. (eq 'verbatim
  2264. (org-test-with-parsed-data "#+CAPTION: a =verb=\nParagraph"
  2265. (org-element-type
  2266. (org-export-get-next-element
  2267. (org-element-map tree 'plain-text 'identity info t nil t) info)))))
  2268. ;; With optional argument N, return a list containing all the
  2269. ;; following elements.
  2270. (should
  2271. (equal
  2272. '(bold code underline)
  2273. (org-test-with-parsed-data "_a_ /b/ *c* ~d~ _e_"
  2274. (mapcar 'car
  2275. (org-export-get-next-element
  2276. (org-element-map tree 'italic 'identity info t) info t)))))
  2277. ;; When N is a positive integer, return a list containing up to
  2278. ;; N following elements.
  2279. (should
  2280. (equal
  2281. '(bold code)
  2282. (org-test-with-parsed-data "_a_ /b/ *c* ~d~ _e_"
  2283. (mapcar 'car
  2284. (org-export-get-next-element
  2285. (org-element-map tree 'italic 'identity info t) info 2))))))
  2286. (ert-deftest test-org-export/get-previous-element ()
  2287. "Test `org-export-get-previous-element' specifications."
  2288. ;; Standard test.
  2289. (should
  2290. (equal "a "
  2291. (org-test-with-parsed-data "* Headline\na *b*"
  2292. (org-export-get-previous-element
  2293. (org-element-map tree 'bold 'identity info t) info))))
  2294. ;; Return nil when no previous element.
  2295. (should-not
  2296. (org-test-with-parsed-data "* Headline\n*a* b"
  2297. (org-export-get-previous-element
  2298. (org-element-map tree 'bold 'identity info t) info)))
  2299. ;; Non-exportable elements are ignored.
  2300. (should-not
  2301. (let ((org-export-with-timestamps nil))
  2302. (org-test-with-parsed-data "<2012-03-29 Thu> \alpha"
  2303. (org-export-get-previous-element
  2304. (org-element-map tree 'entity 'identity info t) info))))
  2305. ;; Find previous element in secondary strings.
  2306. (should
  2307. (eq 'verbatim
  2308. (org-test-with-parsed-data "* =verb= a"
  2309. (org-element-type
  2310. (org-export-get-previous-element
  2311. (org-element-map tree 'plain-text 'identity info t) info)))))
  2312. ;; Find previous element in document keywords.
  2313. (should
  2314. (eq 'verbatim
  2315. (org-test-with-parsed-data "#+TITLE: =verb= a"
  2316. (org-element-type
  2317. (org-export-get-previous-element
  2318. (org-element-map
  2319. (plist-get info :title) 'plain-text 'identity info t) info)))))
  2320. ;; Find previous element in parsed affiliated keywords.
  2321. (should
  2322. (eq 'verbatim
  2323. (org-test-with-parsed-data "#+CAPTION: =verb= a\nParagraph"
  2324. (org-element-type
  2325. (org-export-get-previous-element
  2326. (org-element-map tree 'plain-text 'identity info t nil t) info)))))
  2327. ;; With optional argument N, return a list containing up to
  2328. ;; N previous elements.
  2329. (should
  2330. (equal '(underline italic bold)
  2331. (org-test-with-parsed-data "_a_ /b/ *c* ~d~"
  2332. (mapcar 'car
  2333. (org-export-get-previous-element
  2334. (org-element-map tree 'code 'identity info t) info t)))))
  2335. ;; When N is a positive integer, return a list containing up to
  2336. ;; N previous elements.
  2337. (should
  2338. (equal '(italic bold)
  2339. (org-test-with-parsed-data "_a_ /b/ *c* ~d~"
  2340. (mapcar 'car
  2341. (org-export-get-previous-element
  2342. (org-element-map tree 'code 'identity info t) info 2))))))
  2343. (provide 'test-ox)
  2344. ;;; test-org-export.el end here