test-ox.el 87 KB

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