test-ox.el 99 KB

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