test-ox.el 98 KB

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