test-ox.el 78 KB

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