test-ox.el 95 KB

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