test-org-export.el 74 KB

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