test-ox.el 85 KB

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