test-ox.el 77 KB

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