test-ox.el 94 KB

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