test-org-element.el 117 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352
  1. ;;; test-org-element.el --- Tests for org-element.el
  2. ;; Copyright (C) 2012, 2013, 2014 Nicolas Goaziou
  3. ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
  4. ;; This program is free software; you can redistribute it and/or modify
  5. ;; it under the terms of the GNU General Public License as published by
  6. ;; the Free Software Foundation, either version 3 of the License, or
  7. ;; (at your option) any later version.
  8. ;; This program is distributed in the hope that it will be useful,
  9. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. ;; GNU General Public License for more details.
  12. ;; You should have received a copy of the GNU General Public License
  13. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. ;;; Code:
  15. (unless (featurep 'org-element)
  16. (signal 'missing-test-dependency "org-element"))
  17. (defun org-test-parse-and-interpret (text)
  18. "Parse TEXT as Org syntax and interpret it.
  19. Return interpreted string."
  20. (with-temp-buffer
  21. (org-mode)
  22. (insert text)
  23. (org-element-interpret-data (org-element-parse-buffer))))
  24. ;;; Test `org-element-map'
  25. (ert-deftest test-org-element/map ()
  26. "Test `org-element-map'."
  27. ;; Can map to `plain-text' objects.
  28. (should
  29. (= 2
  30. (org-test-with-temp-text "Some text \alpha
  31. #+BEGIN_CENTER
  32. Some other text
  33. #+END_CENTER"
  34. (let ((count 0))
  35. (org-element-map
  36. (org-element-parse-buffer) 'plain-text
  37. (lambda (s) (when (string-match "text" s) (incf count))))
  38. count))))
  39. ;; Applies to secondary strings
  40. (should
  41. (org-element-map '("some " (bold nil "bold") "text") 'bold 'identity))
  42. ;; Enter secondary strings before entering contents.
  43. (should
  44. (equal
  45. "alpha"
  46. (org-element-property
  47. :name
  48. (org-test-with-temp-text "* Some \\alpha headline\n\\beta entity."
  49. (org-element-map (org-element-parse-buffer) 'entity 'identity nil t)))))
  50. ;; Apply NO-RECURSION argument.
  51. (should-not
  52. (org-test-with-temp-text "#+BEGIN_CENTER\n\\alpha\n#+END_CENTER"
  53. (org-element-map
  54. (org-element-parse-buffer) 'entity 'identity nil nil 'center-block)))
  55. ;; Use WITH-AFFILIATED argument.
  56. (should
  57. (equal
  58. '("a" "1" "b" "2")
  59. (org-test-with-temp-text "#+CAPTION[a]: 1\n#+CAPTION[b]: 2\nParagraph"
  60. (org-element-map
  61. (org-element-at-point) 'plain-text 'identity nil nil nil t)))))
  62. ;;; Test Setters
  63. (ert-deftest test-org-element/put-property ()
  64. "Test `org-element-put-property' specifications."
  65. ;; Standard test.
  66. (org-test-with-temp-text "* Headline\n *a*"
  67. (let ((tree (org-element-parse-buffer)))
  68. (org-element-put-property
  69. (org-element-map tree 'bold 'identity nil t) :test 1)
  70. (should (org-element-property
  71. :test (org-element-map tree 'bold 'identity nil t)))))
  72. ;; Put property on a string.
  73. (should
  74. (org-element-property :test (org-element-put-property "Paragraph" :test t))))
  75. (ert-deftest test-org-element/set-contents ()
  76. "Test `org-element-set-contents' specifications."
  77. ;; Accept multiple entries.
  78. (should
  79. (equal '("b" (italic nil "a"))
  80. (org-test-with-temp-text "* Headline\n *a*"
  81. (let ((tree (org-element-parse-buffer)))
  82. (org-element-set-contents
  83. (org-element-map tree 'bold 'identity nil t) "b" '(italic nil "a"))
  84. (org-element-contents
  85. (org-element-map tree 'bold 'identity nil t))))))
  86. ;; Accept atoms and elements.
  87. (should
  88. (equal '("b")
  89. (org-test-with-temp-text "* Headline\n *a*"
  90. (let ((tree (org-element-parse-buffer)))
  91. (org-element-set-contents
  92. (org-element-map tree 'bold 'identity nil t) "b")
  93. (org-element-contents
  94. (org-element-map tree 'bold 'identity nil t))))))
  95. (should
  96. (equal '((italic nil "b"))
  97. (org-test-with-temp-text "* Headline\n *a*"
  98. (let ((tree (org-element-parse-buffer)))
  99. (org-element-set-contents
  100. (org-element-map tree 'bold 'identity nil t) '(italic nil "b"))
  101. (org-element-contents
  102. (org-element-map tree 'bold 'identity nil t))))))
  103. ;; Allow nil contents.
  104. (should-not
  105. (org-test-with-temp-text "* Headline\n *a*"
  106. (let ((tree (org-element-parse-buffer)))
  107. (org-element-set-contents (org-element-map tree 'bold 'identity nil t))
  108. (org-element-contents (org-element-map tree 'bold 'identity nil t))))))
  109. (ert-deftest test-org-element/secondary-p ()
  110. "Test `org-element-secondary-p' specifications."
  111. ;; In a secondary string, return property name.
  112. (should
  113. (eq :title
  114. (org-test-with-temp-text "* Headline *object*"
  115. (org-element-map (org-element-parse-buffer) 'bold
  116. (lambda (object) (org-element-secondary-p object))
  117. nil t))))
  118. ;; Outside a secondary string, return nil.
  119. (should-not
  120. (org-test-with-temp-text "Paragraph *object*"
  121. (org-element-map (org-element-parse-buffer) 'bold
  122. (lambda (object) (org-element-type (org-element-secondary-p object)))
  123. nil t))))
  124. (ert-deftest test-org-element/adopt-elements ()
  125. "Test `org-element-adopt-elements' specifications."
  126. ;; Adopt an element.
  127. (should
  128. (equal '(plain-text italic)
  129. (org-test-with-temp-text "* Headline\n *a*"
  130. (let ((tree (org-element-parse-buffer)))
  131. (org-element-adopt-elements
  132. (org-element-map tree 'bold 'identity nil t) '(italic nil "a"))
  133. (mapcar (lambda (blob) (org-element-type blob))
  134. (org-element-contents
  135. (org-element-map tree 'bold 'identity nil t)))))))
  136. ;; Adopt a string.
  137. (should
  138. (equal '("a" "b")
  139. (org-test-with-temp-text "* Headline\n *a*"
  140. (let ((tree (org-element-parse-buffer)))
  141. (org-element-adopt-elements
  142. (org-element-map tree 'bold 'identity nil t) "b")
  143. (org-element-contents
  144. (org-element-map tree 'bold 'identity nil t)))))))
  145. (ert-deftest test-org-element/extract-element ()
  146. "Test `org-element-extract-element' specifications."
  147. ;; Extract a greater element.
  148. (should
  149. (equal '(org-data nil)
  150. (org-test-with-temp-text "* Headline"
  151. (let* ((tree (org-element-parse-buffer))
  152. (element (org-element-map tree 'headline 'identity nil t)))
  153. (org-element-extract-element element)
  154. tree))))
  155. ;; Extract an element.
  156. (should-not
  157. (org-element-map
  158. (org-test-with-temp-text "Paragraph"
  159. (let* ((tree (org-element-parse-buffer))
  160. (element (org-element-map tree 'paragraph 'identity nil t)))
  161. (org-element-extract-element element)
  162. tree))
  163. 'paragraph
  164. 'identity))
  165. ;; Extract an object, even in a secondary string.
  166. (should-not
  167. (org-element-map
  168. (org-test-with-temp-text "*bold*"
  169. (let* ((tree (org-element-parse-buffer))
  170. (element (org-element-map tree 'bold 'identity nil t)))
  171. (org-element-extract-element element)
  172. tree))
  173. 'bold
  174. 'identity))
  175. (should-not
  176. (org-element-map
  177. (org-test-with-temp-text "* Headline *bold*"
  178. (let* ((tree (org-element-parse-buffer))
  179. (element (org-element-map tree 'bold 'identity nil t)))
  180. (org-element-extract-element element)
  181. tree))
  182. 'bold
  183. 'identity))
  184. ;; Return value doesn't have any :parent set.
  185. (should-not
  186. (org-element-property
  187. :parent
  188. (org-test-with-temp-text "* Headline\n Paragraph with *bold* text."
  189. (let* ((tree (org-element-parse-buffer))
  190. (element (org-element-map tree 'bold 'identity nil t)))
  191. (org-element-extract-element element))))))
  192. (ert-deftest test-org-element/insert-before ()
  193. "Test `org-element-insert-before' specifications."
  194. ;; Standard test.
  195. (should
  196. (equal
  197. '(italic entity bold)
  198. (org-test-with-temp-text "/some/ *paragraph*"
  199. (let* ((tree (org-element-parse-buffer))
  200. (paragraph (org-element-map tree 'paragraph 'identity nil t))
  201. (bold (org-element-map tree 'bold 'identity nil t)))
  202. (org-element-insert-before '(entity (:name "\\alpha")) bold)
  203. (org-element-map tree '(bold entity italic) #'org-element-type nil)))))
  204. ;; Insert an object in a secondary string.
  205. (should
  206. (equal
  207. '(entity italic)
  208. (org-test-with-temp-text "* /A/\n Paragraph."
  209. (let* ((tree (org-element-parse-buffer))
  210. (headline (org-element-map tree 'headline 'identity nil t))
  211. (italic (org-element-map tree 'italic 'identity nil t)))
  212. (org-element-insert-before '(entity (:name "\\alpha")) italic)
  213. (org-element-map (org-element-property :title headline) '(entity italic)
  214. #'org-element-type))))))
  215. (ert-deftest test-org-element/set-element ()
  216. "Test `org-element-set-element' specifications."
  217. ;; Check if new element is inserted.
  218. (should
  219. (org-test-with-temp-text "* Headline\n*a*"
  220. (let* ((tree (org-element-parse-buffer))
  221. (bold (org-element-map tree 'bold 'identity nil t)))
  222. (org-element-set-element bold '(italic nil "b"))
  223. (org-element-map tree 'italic 'identity))))
  224. ;; Check if old element is removed.
  225. (should-not
  226. (org-test-with-temp-text "* Headline\n*a*"
  227. (let* ((tree (org-element-parse-buffer))
  228. (bold (org-element-map tree 'bold 'identity nil t)))
  229. (org-element-set-element bold '(italic nil "b"))
  230. (org-element-map tree 'bold 'identity))))
  231. ;; Check if :parent property is correctly set.
  232. (should
  233. (eq 'paragraph
  234. (org-test-with-temp-text "* Headline\n*a*"
  235. (let* ((tree (org-element-parse-buffer))
  236. (bold (org-element-map tree 'bold 'identity nil t)))
  237. (org-element-set-element bold '(italic nil "b"))
  238. (org-element-type
  239. (org-element-property
  240. :parent (org-element-map tree 'italic 'identity nil t)))))))
  241. ;; Allow to replace strings with elements.
  242. (should
  243. (equal '("b")
  244. (org-test-with-temp-text "* Headline"
  245. (let* ((tree (org-element-parse-buffer))
  246. (text (org-element-map tree 'plain-text 'identity nil t)))
  247. (org-element-set-element text (list 'bold nil "b"))
  248. (org-element-map tree 'plain-text 'identity)))))
  249. ;; Allow to replace elements with strings.
  250. (should
  251. (equal "a"
  252. (org-test-with-temp-text "* =verbatim="
  253. (let* ((tree (org-element-parse-buffer))
  254. (verb (org-element-map tree 'verbatim 'identity nil t)))
  255. (org-element-set-element verb "a")
  256. (org-element-map tree 'plain-text 'identity nil t)))))
  257. ;; Allow to replace strings with strings.
  258. (should
  259. (equal "b"
  260. (org-test-with-temp-text "a"
  261. (let* ((tree (org-element-parse-buffer))
  262. (text (org-element-map tree 'plain-text 'identity nil t)))
  263. (org-element-set-element text "b")
  264. (org-element-map tree 'plain-text 'identity nil t))))))
  265. ;;; Test Parsers
  266. ;;;; Affiliated Keywords
  267. (ert-deftest test-org-element/affiliated-keywords-parser ()
  268. "Test affiliated keywords parsing."
  269. ;; Read simple keywords.
  270. (should
  271. (equal "para"
  272. (org-element-property
  273. :name
  274. (org-test-with-temp-text "#+NAME: para\nParagraph"
  275. (org-element-at-point)))))
  276. (should
  277. (= 1
  278. (org-element-property
  279. :begin
  280. (org-test-with-temp-text "#+NAME: para\nParagraph"
  281. (org-element-at-point)))))
  282. ;; Parse multiple keywords.
  283. (should
  284. (equal
  285. '("line2" "line1")
  286. (org-element-property
  287. :attr_ascii
  288. (org-test-with-temp-text
  289. "#+ATTR_ASCII: line1\n#+ATTR_ASCII: line2\nParagraph"
  290. (org-element-at-point)))))
  291. ;; Parse "parsed" keywords.
  292. (should
  293. (equal
  294. '(("caption"))
  295. (org-test-with-temp-text "#+CAPTION: caption\nParagraph"
  296. (car (org-element-property :caption (org-element-at-point))))))
  297. ;; Parse dual keywords.
  298. (should
  299. (equal
  300. '((("long") "short"))
  301. (org-test-with-temp-text "#+CAPTION[short]: long\nParagraph"
  302. (org-element-property :caption (org-element-at-point)))))
  303. ;; Allow multiple caption keywords.
  304. (should
  305. (equal
  306. '((("l2") "s2") (("l1") "s1"))
  307. (org-test-with-temp-text "#+CAPTION[s1]: l1\n#+CAPTION[s2]: l2\nParagraph"
  308. (org-element-property :caption (org-element-at-point)))))
  309. (should
  310. (equal
  311. '((("l1")) (nil "s1"))
  312. (org-test-with-temp-text "#+CAPTION[s1]:\n#+CAPTION: l1\nParagraph"
  313. (org-element-property :caption (org-element-at-point)))))
  314. ;; Corner case: orphaned keyword at the end of an element.
  315. (should
  316. (eq 'keyword
  317. (org-test-with-temp-text "- item\n #+name: name\nSome paragraph"
  318. (progn (search-forward "name")
  319. (org-element-type (org-element-at-point))))))
  320. (should-not
  321. (org-test-with-temp-text "- item\n #+name: name\nSome paragraph"
  322. (progn (search-forward "Some")
  323. (org-element-property :name (org-element-at-point))))))
  324. ;;;; Babel Call
  325. (ert-deftest test-org-element/babel-call-parser ()
  326. "Test `babel-call' parsing."
  327. ;; Standard test.
  328. (should
  329. (org-test-with-temp-text "#+CALL: test()"
  330. (org-element-map (org-element-parse-buffer) 'babel-call 'identity)))
  331. ;; Ignore case.
  332. (should
  333. (org-test-with-temp-text "#+call: test()"
  334. (org-element-map (org-element-parse-buffer) 'babel-call 'identity)))
  335. ;; Handle non-empty blank line at the end of buffer.
  336. (should
  337. (org-test-with-temp-text "#+CALL: test()\n "
  338. (= (org-element-property :end (org-element-at-point)) (point-max)))))
  339. ;;;; Bold
  340. (ert-deftest test-org-element/bold-parser ()
  341. "Test `bold' parser."
  342. ;; Standard test.
  343. (should
  344. (let ((org-emph-re "\\([ ('\"{]\\|^\\)\\(\\([+*/_=~]\\)\\([^ \n,\"']\\|[^ \n,\"'].*?\\(?:\n.*?\\)\\{0,1\\}[^ \n,\"']\\)\\3\\)\\([- .,:!?;'\")}\\]\\|$\\)"))
  345. (org-test-with-temp-text "*bold*"
  346. (org-element-map (org-element-parse-buffer) 'bold 'identity nil t))))
  347. ;; Multi-line markup.
  348. (should
  349. (equal
  350. (org-element-contents
  351. (let ((org-emph-re "\\([ ('\"{]\\|^\\)\\(\\([+*/_=~]\\)\\([^ \n,\"']\\|[^ \n,\"'].*?\\(?:\n.*?\\)\\{0,1\\}[^ \n,\"']\\)\\3\\)\\([- .,:!?;'\")}\\]\\|$\\)"))
  352. (org-test-with-temp-text "*first line\nsecond line*"
  353. (org-element-map (org-element-parse-buffer) 'bold 'identity nil t))))
  354. '("first line\nsecond line"))))
  355. ;;;; Center Block
  356. (ert-deftest test-org-element/center-block-parser ()
  357. "Test `center-block' parser."
  358. ;; Standard test.
  359. (should
  360. (org-test-with-temp-text "#+BEGIN_CENTER\nText\n#+END_CENTER"
  361. (org-element-map (org-element-parse-buffer) 'center-block 'identity)))
  362. ;; Ignore case.
  363. (should
  364. (org-test-with-temp-text "#+begin_center\nText\n#+end_center"
  365. (org-element-map (org-element-parse-buffer) 'center-block 'identity)))
  366. ;; Ignore incomplete block.
  367. (should-not
  368. (org-test-with-temp-text "#+BEGIN_CENTER"
  369. (org-element-map (org-element-parse-buffer) 'center-block
  370. 'identity nil t)))
  371. ;; Handle non-empty blank line at the end of buffer.
  372. (should
  373. (org-test-with-temp-text "#+BEGIN_CENTER\nC\n#+END_CENTER\n "
  374. (= (org-element-property :end (org-element-at-point)) (point-max)))))
  375. ;;;; Clock
  376. (ert-deftest test-org-element/clock-parser ()
  377. "Test `clock' parser."
  378. ;; Running clock.
  379. (let ((clock (org-test-with-temp-text "CLOCK: [2012-01-01 sun. 00:01]"
  380. (org-element-at-point))))
  381. (should (eq (org-element-property :status clock) 'running))
  382. (should
  383. (equal (org-element-property :raw-value
  384. (org-element-property :value clock))
  385. "[2012-01-01 sun. 00:01]"))
  386. (should-not (org-element-property :duration clock)))
  387. ;; Closed clock.
  388. (let ((clock
  389. (org-test-with-temp-text
  390. "CLOCK: [2012-01-01 sun. 00:01]--[2012-01-01 sun. 00:02] => 0:01"
  391. (org-element-at-point))))
  392. (should (eq (org-element-property :status clock) 'closed))
  393. (should (equal (org-element-property :raw-value
  394. (org-element-property :value clock))
  395. "[2012-01-01 sun. 00:01]--[2012-01-01 sun. 00:02]"))
  396. (should (equal (org-element-property :duration clock) "0:01"))))
  397. ;;;; Code
  398. (ert-deftest test-org-element/code-parser ()
  399. "Test `code' parser."
  400. ;; Regular test.
  401. (should
  402. (let ((org-emph-re "\\([ ('\"{]\\|^\\)\\(\\([+*/_=~]\\)\\([^ \n,\"']\\|[^ \n,\"'].*?\\(?:\n.*?\\)\\{0,1\\}[^ \n,\"']\\)\\3\\)\\([- .,:!?;'\")}\\]\\|$\\)"))
  403. (org-test-with-temp-text "~code~"
  404. (org-element-map (org-element-parse-buffer) 'code 'identity))))
  405. ;; Multi-line markup.
  406. (should
  407. (equal
  408. (org-element-property
  409. :value
  410. (let ((org-emph-re "\\([ ('\"{]\\|^\\)\\(\\([+*/_=~]\\)\\([^ \n,\"']\\|[^ \n,\"'].*?\\(?:\n.*?\\)\\{0,1\\}[^ \n,\"']\\)\\3\\)\\([- .,:!?;'\")}\\]\\|$\\)"))
  411. (org-test-with-temp-text "~first line\nsecond line~"
  412. (org-element-map
  413. (org-element-parse-buffer) 'code 'identity nil t))))
  414. "first line\nsecond line")))
  415. ;;;; Comment
  416. (ert-deftest test-org-element/comment-parser ()
  417. "Test `comment' parser."
  418. ;; Regular comment.
  419. (should
  420. (org-test-with-temp-text "# Comment"
  421. (org-element-map (org-element-parse-buffer) 'comment 'identity)))
  422. ;; Inline comment.
  423. (should
  424. (org-test-with-temp-text " # Comment"
  425. (org-element-map (org-element-parse-buffer) 'comment 'identity)))
  426. ;; Preserve indentation.
  427. (should
  428. (equal
  429. (org-element-property
  430. :value
  431. (org-test-with-temp-text "# No blank\n# One blank"
  432. (org-element-map (org-element-parse-buffer) 'comment 'identity nil t)))
  433. "No blank\n One blank"))
  434. ;; Comment with blank lines.
  435. (should
  436. (equal
  437. (org-element-property
  438. :value
  439. (org-test-with-temp-text "# First part\n# \n#\n# Second part"
  440. (org-element-map (org-element-parse-buffer) 'comment 'identity nil t)))
  441. "First part\n\n\nSecond part"))
  442. ;; Do not mix comments and keywords.
  443. (should
  444. (eq 1
  445. (org-test-with-temp-text "#+keyword: value\n# comment\n#+keyword: value"
  446. (length (org-element-map (org-element-parse-buffer) 'comment
  447. 'identity)))))
  448. (should
  449. (equal "comment"
  450. (org-test-with-temp-text "#+keyword: value\n# comment\n#+keyword: value"
  451. (org-element-property
  452. :value
  453. (org-element-map (org-element-parse-buffer) 'comment
  454. 'identity nil t)))))
  455. ;; Correctly handle non-empty blank lines at the end of buffer.
  456. (should
  457. (org-test-with-temp-text "# A\n "
  458. (= (org-element-property :end (org-element-at-point)) (point-max)))))
  459. ;;;; Comment Block
  460. (ert-deftest test-org-element/comment-block-parser ()
  461. "Test `comment-block' parser."
  462. ;; Standard test.
  463. (should
  464. (org-test-with-temp-text "#+BEGIN_COMMENT\nText\n#+END_COMMENT"
  465. (org-element-map (org-element-parse-buffer) 'comment-block 'identity)))
  466. ;; Ignore case.
  467. (should
  468. (org-test-with-temp-text "#+begin_comment\nText\n#+end_comment"
  469. (org-element-map (org-element-parse-buffer) 'comment-block 'identity)))
  470. ;; Ignore incomplete block.
  471. (should-not
  472. (org-test-with-temp-text "#+BEGIN_COMMENT"
  473. (org-element-map (org-element-parse-buffer) 'comment-block
  474. 'identity nil t)))
  475. ;; Handle non-empty blank line at the end of buffer.
  476. (should
  477. (org-test-with-temp-text "#+BEGIN_COMMENT\nC\n#+END_COMMENT\n "
  478. (= (org-element-property :end (org-element-at-point)) (point-max)))))
  479. ;;;; Diary Sexp
  480. (ert-deftest test-org-element/diary-sexp-parser ()
  481. "Test `diary-sexp' parser."
  482. ;; Standard test.
  483. (should
  484. (eq 'diary-sexp
  485. (org-test-with-temp-text
  486. "%%(org-anniversary 1956 5 14)(2) Arthur Dent is %d years old"
  487. (org-element-type (org-element-at-point)))))
  488. ;; Diary sexp must live at beginning of line
  489. (should-not
  490. (eq 'diary-sexp
  491. (org-test-with-temp-text " %%(org-bbdb-anniversaries)"
  492. (org-element-type (org-element-at-point)))))
  493. ;; Handle non-empty blank line at the end of buffer.
  494. (should
  495. (org-test-with-temp-text "%%(org-bbdb-anniversaries)\n "
  496. (= (org-element-property :end (org-element-at-point)) (point-max)))))
  497. ;;;; Drawer
  498. (ert-deftest test-org-element/drawer-parser ()
  499. "Test `drawer' parser."
  500. ;; Standard test.
  501. (should
  502. (org-test-with-temp-text ":TEST:\nText\n:END:"
  503. (org-element-map (org-element-parse-buffer) 'drawer 'identity)))
  504. ;; Do not mix regular drawers and property drawers.
  505. (should-not
  506. (org-test-with-temp-text ":PROPERTIES:\n:prop: value\n:END:"
  507. (org-element-map (org-element-parse-buffer) 'drawer 'identity nil t)))
  508. ;; Ignore incomplete drawer.
  509. (should-not
  510. (org-test-with-temp-text ":TEST:"
  511. (org-element-map (org-element-parse-buffer) 'drawer 'identity nil t)))
  512. ;; Handle non-empty blank line at the end of buffer.
  513. (should
  514. (org-test-with-temp-text ":TEST:\nC\n:END:\n "
  515. (= (org-element-property :end (org-element-at-point)) (point-max)))))
  516. ;;;; Dynamic Block
  517. (ert-deftest test-org-element/dynamic-block-parser ()
  518. "Test `dynamic-block' parser."
  519. ;; Standard test.
  520. (should
  521. (org-test-with-temp-text
  522. "#+BEGIN: myblock :param1 val1 :param2 val2\nText\n#+END:"
  523. (org-element-map (org-element-parse-buffer) 'dynamic-block 'identity)))
  524. ;; Ignore case.
  525. (should
  526. (org-test-with-temp-text
  527. "#+begin: myblock :param1 val1 :param2 val2\nText\n#+end:"
  528. (org-element-map (org-element-parse-buffer) 'dynamic-block 'identity)))
  529. ;; Ignore incomplete block.
  530. (should-not
  531. (org-test-with-temp-text "#+BEGIN: myblock :param1 val1 :param2 val2"
  532. (org-element-map (org-element-parse-buffer) 'dynamic-block
  533. 'identity nil t)))
  534. ;; Handle non-empty blank line at the end of buffer.
  535. (should
  536. (org-test-with-temp-text "#+BEGIN: myblock :param val1\nC\n#+END:\n "
  537. (= (org-element-property :end (org-element-at-point)) (point-max)))))
  538. ;;;; Entity
  539. (ert-deftest test-org-element/entity-parser ()
  540. "Test `entity' parser."
  541. ;; Without brackets.
  542. (should
  543. (org-test-with-temp-text "\\sin"
  544. (org-element-map (org-element-parse-buffer) 'entity 'identity)))
  545. ;; With brackets.
  546. (should
  547. (org-element-property
  548. :use-brackets-p
  549. (org-test-with-temp-text "\\alpha{}text"
  550. (org-element-map (org-element-parse-buffer) 'entity 'identity nil t))))
  551. ;; User-defined entity.
  552. (should
  553. (equal
  554. (org-element-property
  555. :name
  556. (let ((org-entities-user
  557. '(("test" "test" nil "test" "test" "test" "test"))))
  558. (org-test-with-temp-text "\\test"
  559. (org-element-map (org-element-parse-buffer) 'entity 'identity nil t))))
  560. "test"))
  561. ;; Special case: entity at the end of a container.
  562. (should
  563. (eq 'entity
  564. (org-test-with-temp-text "*\\alpha \\beta*"
  565. (search-forward "be")
  566. (org-element-type (org-element-context))))))
  567. ;;;; Example Block
  568. (ert-deftest test-org-element/example-block-parser ()
  569. "Test `example-block' parser."
  570. ;; Standard test.
  571. (should
  572. (org-test-with-temp-text "#+BEGIN_EXAMPLE\nText\n#+END_EXAMPLE"
  573. (org-element-map (org-element-parse-buffer) 'example-block 'identity)))
  574. ;; Ignore incomplete block.
  575. (should-not
  576. (eq 'example-block
  577. (org-test-with-temp-text "#+BEGIN_EXAMPLE"
  578. (org-element-type (org-element-at-point)))))
  579. ;; Properly un-escape code.
  580. (should
  581. (equal "* Headline\n #+keyword\nText\n"
  582. (org-test-with-temp-text
  583. "#+BEGIN_EXAMPLE\n,* Headline\n ,#+keyword\nText\n#+END_EXAMPLE"
  584. (org-element-property :value (org-element-at-point)))))
  585. ;; Remove block indentation according to block boundaries, unless
  586. ;; block contents are less indented than block boundaries.
  587. (should
  588. (equal " L1\nL2\n"
  589. (org-test-with-temp-text " #+BEGIN_EXAMPLE\n L1\n L2\n #+END_EXAMPLE"
  590. (let ((org-src-preserve-indentation t))
  591. (org-element-property :value (org-element-at-point))))))
  592. (should
  593. (equal
  594. " L1\n L2\n"
  595. (org-test-with-temp-text " #+BEGIN_EXAMPLE\n L1\n L2\n #+END_EXAMPLE"
  596. (let ((org-src-preserve-indentation t))
  597. (org-element-property :value (org-element-at-point))))))
  598. ;; Handle non-empty blank line at the end of buffer.
  599. (should
  600. (org-test-with-temp-text "#+BEGIN_EXAMPLE\nC\n#+END_EXAMPLE\n "
  601. (= (org-element-property :end (org-element-at-point)) (point-max)))))
  602. (ert-deftest test-org-element/block-switches ()
  603. "Test `example-block' and `src-block' switches parsing."
  604. (let ((org-coderef-label-format "(ref:%s)"))
  605. ;; 1. Test "-i" switch.
  606. (should-not
  607. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  608. (org-element-property :preserve-indent (org-element-at-point))))
  609. (should
  610. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp -i\n(+ 1 1)\n#+END_SRC"
  611. (org-element-property :preserve-indent (org-element-at-point))))
  612. (should-not
  613. (org-test-with-temp-text "#+BEGIN_EXAMPLE\nText.\n#+END_EXAMPLE"
  614. (org-element-property :preserve-indent (org-element-at-point))))
  615. (should
  616. (org-test-with-temp-text "#+BEGIN_EXAMPLE -i\nText.\n#+END_EXAMPLE"
  617. (org-element-property :preserve-indent (org-element-at-point))))
  618. ;; 2. "-n -r -k" combination should number lines, retain labels but
  619. ;; not use them in coderefs.
  620. (should
  621. (org-test-with-temp-text "#+BEGIN_EXAMPLE -n -r -k\nText.\n#+END_EXAMPLE"
  622. (let ((element (org-element-at-point)))
  623. (and (org-element-property :number-lines element)
  624. (org-element-property :retain-labels element)
  625. (not (org-element-property :use-labels element))))))
  626. (should
  627. (org-test-with-temp-text
  628. "#+BEGIN_SRC emacs-lisp -n -r -k\n(+ 1 1)\n#+END_SRC"
  629. (let ((element (org-element-at-point)))
  630. (and (org-element-property :number-lines element)
  631. (org-element-property :retain-labels element)
  632. (not (org-element-property :use-labels element))))))
  633. ;; 3. "-n -r" combination should number-lines remove labels and not
  634. ;; use them in coderefs.
  635. (should
  636. (org-test-with-temp-text "#+BEGIN_EXAMPLE -n -r\nText.\n#+END_EXAMPLE"
  637. (let ((element (org-element-at-point)))
  638. (and (org-element-property :number-lines element)
  639. (not (org-element-property :retain-labels element))
  640. (not (org-element-property :use-labels element))))))
  641. (should
  642. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp -n -r\n(+ 1 1)\n#+END_SRC"
  643. (let ((element (org-element-at-point)))
  644. (and (org-element-property :number-lines element)
  645. (not (org-element-property :retain-labels element))
  646. (not (org-element-property :use-labels element))))))
  647. ;; 4. "-n" or "+n" should number lines, retain labels and use them
  648. ;; in coderefs.
  649. (should
  650. (org-test-with-temp-text "#+BEGIN_EXAMPLE -n\nText.\n#+END_EXAMPLE"
  651. (let ((element (org-element-at-point)))
  652. (and (org-element-property :number-lines element)
  653. (org-element-property :retain-labels element)
  654. (org-element-property :use-labels element)))))
  655. (should
  656. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp -n\n(+ 1 1)\n#+END_SRC"
  657. (let ((element (org-element-at-point)))
  658. (and (org-element-property :number-lines element)
  659. (org-element-property :retain-labels element)
  660. (org-element-property :use-labels element)))))
  661. (should
  662. (org-test-with-temp-text "#+BEGIN_EXAMPLE +n\nText.\n#+END_EXAMPLE"
  663. (let ((element (org-element-at-point)))
  664. (and (org-element-property :number-lines element)
  665. (org-element-property :retain-labels element)
  666. (org-element-property :use-labels element)))))
  667. (should
  668. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp +n\n(+ 1 1)\n#+END_SRC"
  669. (let ((element (org-element-at-point)))
  670. (and (org-element-property :number-lines element)
  671. (org-element-property :retain-labels element)
  672. (org-element-property :use-labels element)))))
  673. ;; 5. No switch should not number lines, but retain labels and use
  674. ;; them in coderefs.
  675. (should
  676. (org-test-with-temp-text "#+BEGIN_EXAMPLE\nText.\n#+END_EXAMPLE"
  677. (let ((element (org-element-at-point)))
  678. (and (not (org-element-property :number-lines element))
  679. (org-element-property :retain-labels element)
  680. (org-element-property :use-labels element)))))
  681. (should
  682. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  683. (let ((element (org-element-at-point)))
  684. (and (not (org-element-property :number-lines element))
  685. (org-element-property :retain-labels element)
  686. (org-element-property :use-labels element)))))
  687. ;; 6. "-r" switch only: do not number lines, remove labels, and
  688. ;; don't use labels in coderefs.
  689. (should
  690. (org-test-with-temp-text "#+BEGIN_EXAMPLE -r\nText.\n#+END_EXAMPLE"
  691. (let ((element (org-element-at-point)))
  692. (and (not (org-element-property :number-lines element))
  693. (not (org-element-property :retain-labels element))
  694. (not (org-element-property :use-labels element))))))
  695. (should
  696. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp -r\n(+ 1 1)\n#+END_SRC"
  697. (let ((element (org-element-at-point)))
  698. (and (not (org-element-property :number-lines element))
  699. (not (org-element-property :retain-labels element))
  700. (not (org-element-property :use-labels element))))))
  701. ;; 7. Recognize coderefs with user-defined syntax.
  702. (should
  703. (equal
  704. "[ref:%s]"
  705. (org-test-with-temp-text
  706. "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\nText [ref:text]\n#+END_EXAMPLE"
  707. (org-element-property :label-fmt (org-element-at-point)))))
  708. (should
  709. (equal
  710. "[ref:%s]"
  711. (org-test-with-temp-text
  712. "#+BEGIN_SRC emacs-lisp -l \"[ref:%s]\"\n(+ 1 1) [ref:text]\n#+END_SRC"
  713. (org-element-property :label-fmt (org-element-at-point)))))))
  714. ;;;; Export Snippet
  715. (ert-deftest test-org-element/export-snippet-parser ()
  716. "Test `export-snippet' parser."
  717. (should
  718. (equal
  719. '("back-end" . "contents")
  720. (org-test-with-temp-text "@@back-end:contents@@"
  721. (org-element-map
  722. (org-element-parse-buffer) 'export-snippet
  723. (lambda (snippet) (cons (org-element-property :back-end snippet)
  724. (org-element-property :value snippet)))
  725. nil t)))))
  726. ;;;; Fixed Width
  727. (ert-deftest test-org-element/fixed-width-parser ()
  728. "Test fixed-width area parsing."
  729. ;; Preserve indentation.
  730. (should
  731. (org-test-with-temp-text ": no blank\n: one blank"
  732. (org-element-map (org-element-parse-buffer) 'fixed-width 'identity)))
  733. ;; Fixed-width with empty lines.
  734. (should
  735. (org-test-with-temp-text ": first part\n:\n: \n: second part"
  736. (org-element-map (org-element-parse-buffer) 'fixed-width 'identity)))
  737. ;; Parse indented fixed-width markers.
  738. (should
  739. (org-test-with-temp-text "Text\n : no blank\n : one blank"
  740. (org-element-map (org-element-parse-buffer) 'fixed-width 'identity)))
  741. ;; Distinguish fixed-width areas within a list and outside of it.
  742. (should
  743. (= 2
  744. (length
  745. (org-test-with-temp-text "
  746. - Item
  747. : fixed-width inside
  748. : fixed-width outside"
  749. (org-element-map (org-element-parse-buffer) 'fixed-width 'identity)))))
  750. ;; Handle non-empty blank line at the end of buffer.
  751. (should
  752. (org-test-with-temp-text ": A\n "
  753. (= (org-element-property :end (org-element-at-point)) (point-max)))))
  754. ;;;; Footnote Definition
  755. (ert-deftest test-org-element/footnote-definition-parser ()
  756. "Test `footnote-definition' parser."
  757. (should
  758. (org-test-with-temp-text "[fn:1] Definition"
  759. (org-element-map (org-element-parse-buffer) 'footnote-definition
  760. 'identity nil t)))
  761. ;; Footnote with more contents
  762. (should
  763. (= 29
  764. (org-element-property
  765. :end
  766. (org-test-with-temp-text "[fn:1] Definition\n\n| a | b |"
  767. (org-element-map (org-element-parse-buffer) 'footnote-definition
  768. 'identity nil t)))))
  769. ;; Footnote starting with special syntax.
  770. (should-not
  771. (org-test-with-temp-text "[fn:1] - no item"
  772. (org-element-map (org-element-parse-buffer) 'item 'identity)))
  773. ;; Correctly handle footnote starting with an empty line.
  774. (should
  775. (= 9
  776. (org-test-with-temp-text "[fn:1]\n\n Body"
  777. (org-element-property :contents-begin (org-element-at-point)))))
  778. ;; Handle non-empty blank line at the end of buffer.
  779. (should
  780. (org-test-with-temp-text "[fn:1] Definition\n "
  781. (= (org-element-property :end (org-element-at-point)) (point-max)))))
  782. ;;;; Footnotes Reference.
  783. (ert-deftest test-org-element/footnote-reference-parser ()
  784. "Test `footnote-reference' parser."
  785. ;; Parse a standard reference.
  786. (should
  787. (org-test-with-temp-text "Text[fn:label]"
  788. (org-element-map
  789. (org-element-parse-buffer) 'footnote-reference 'identity)))
  790. ;; Parse a normalized reference.
  791. (should
  792. (org-test-with-temp-text "Text[1]"
  793. (org-element-map
  794. (org-element-parse-buffer) 'footnote-reference 'identity)))
  795. ;; Parse an inline reference.
  796. (should
  797. (org-test-with-temp-text "Text[fn:test:def]"
  798. (org-element-map
  799. (org-element-parse-buffer) 'footnote-reference 'identity)))
  800. ;; Parse an anonymous reference.
  801. (should
  802. (org-test-with-temp-text "Text[fn::def]"
  803. (org-element-map
  804. (org-element-parse-buffer) 'footnote-reference 'identity)))
  805. ;; Parse nested footnotes.
  806. (should
  807. (= 2
  808. (length
  809. (org-test-with-temp-text "Text[fn::def [fn:label]]"
  810. (org-element-map
  811. (org-element-parse-buffer) 'footnote-reference 'identity)))))
  812. ;; Parse adjacent footnotes.
  813. (should
  814. (org-test-with-temp-text "Text[fn:label1][fn:label2]"
  815. (= 2
  816. (length
  817. (org-element-map
  818. (org-element-parse-buffer) 'footnote-reference 'identity)))))
  819. ;; Only properly closed footnotes are recognized as such.
  820. (should-not
  821. (org-test-with-temp-text "Text[fn:label"
  822. (org-element-map
  823. (org-element-parse-buffer) 'footnote-reference 'identity))))
  824. ;;;; Headline
  825. (ert-deftest test-org-element/headline-comment-keyword ()
  826. "Test COMMENT keyword recognition."
  827. ;; Reference test.
  828. (org-test-with-temp-text "* Headline"
  829. (let ((org-comment-string "COMMENT"))
  830. (should-not (org-element-property :commentedp (org-element-at-point)))))
  831. ;; Standard position.
  832. (org-test-with-temp-text "* COMMENT Headline"
  833. (let ((org-comment-string "COMMENT")
  834. (headline (org-element-at-point)))
  835. (should (org-element-property :commentedp headline))
  836. (should (equal (org-element-property :raw-value headline) "Headline"))))
  837. ;; Case sensitivity.
  838. (org-test-with-temp-text "* COMMENT Headline"
  839. (let* ((org-comment-string "Comment")
  840. (headline (org-element-at-point)))
  841. (should-not (org-element-property :commentedp headline))
  842. (should (equal (org-element-property :raw-value headline)
  843. "COMMENT Headline"))))
  844. ;; With another keyword.
  845. (org-test-with-temp-text "* TODO COMMENT Headline"
  846. (let* ((org-comment-string "COMMENT")
  847. (org-todo-keywords '((sequence "TODO" "DONE")))
  848. (headline (org-element-at-point)))
  849. (should (org-element-property :commentedp headline))
  850. (should (equal (org-element-property :raw-value headline) "Headline"))))
  851. ;; With the keyword only.
  852. (org-test-with-temp-text "* COMMENT"
  853. (let* ((org-comment-string "COMMENT")
  854. (headline (org-element-at-point)))
  855. (should (org-element-property :commentedp headline))
  856. (should (equal (org-element-property :raw-value headline) "")))))
  857. (ert-deftest test-org-element/headline-archive-tag ()
  858. "Test ARCHIVE tag recognition."
  859. ;; Reference test.
  860. (should-not
  861. (org-test-with-temp-text "* Headline"
  862. (let ((org-archive-tag "ARCHIVE"))
  863. (org-element-property :archivedp (org-element-at-point)))))
  864. ;; Single tag.
  865. (org-test-with-temp-text "* Headline :ARCHIVE:"
  866. (let ((org-archive-tag "ARCHIVE"))
  867. (let ((headline (org-element-at-point)))
  868. (should (org-element-property :archivedp headline))
  869. ;; Test tag removal.
  870. (should-not (org-element-property :tags headline)))))
  871. ;; Multiple tags.
  872. (org-test-with-temp-text "* Headline :test:ARCHIVE:"
  873. (let ((org-archive-tag "ARCHIVE"))
  874. (let ((headline (org-element-at-point)))
  875. (should (org-element-property :archivedp headline))
  876. ;; Test tag removal.
  877. (should (equal (org-element-property :tags headline) '("test"))))))
  878. ;; Tag is case-sensitive.
  879. (should-not
  880. (org-test-with-temp-text "* Headline :ARCHIVE:"
  881. (let ((org-archive-tag "Archive"))
  882. (org-element-property :archivedp (org-element-at-point))))))
  883. (ert-deftest test-org-element/headline-properties ()
  884. "Test properties from property drawer."
  885. ;; All properties from property drawer have their symbol upper
  886. ;; cased.
  887. (should
  888. (org-test-with-temp-text "* Headline\n:PROPERTIES:\n:foo: bar\n:END:"
  889. (org-element-property :FOO (org-element-at-point))))
  890. (should-not
  891. (org-test-with-temp-text "* Headline\n:PROPERTIES:\n:foo: bar\n:END:"
  892. (org-element-property :foo (org-element-at-point))))
  893. ;; Also parse properties associated in inlinetasks.
  894. (when (featurep 'org-inlinetask)
  895. (should
  896. (org-test-with-temp-text "*************** Inlinetask
  897. :PROPERTIES:
  898. :foo: bar
  899. :END:
  900. *************** END"
  901. (org-element-property :FOO (org-element-at-point)))))
  902. ;; Do not find property drawer in a verbatim area.
  903. (should-not
  904. (org-test-with-temp-text
  905. "* Headline
  906. #+BEGIN_EXAMPLE
  907. :PROPERTIES:
  908. :foo: bar
  909. :END:
  910. #+END_EXAMPLE"
  911. (org-element-property :FOO (org-element-at-point))))
  912. ;; Do not use properties from a drawer associated to an inlinetask.
  913. (when (featurep 'org-inlinetask)
  914. (should-not
  915. (org-test-with-temp-text
  916. "* Headline
  917. *************** Inlinetask
  918. :PROPERTIES:
  919. :foo: bar
  920. :END:
  921. *************** END"
  922. (org-element-property
  923. :FOO (let ((org-inlinetask-min-level 15)) (org-element-at-point))))))
  924. ;; Do not find incomplete drawers.
  925. (should-not
  926. (org-test-with-temp-text "* Headline\n:PROPERTIES:\n:foo: bar"
  927. (org-element-property :FOO (org-element-at-point)))))
  928. ;;;; Horizontal Rule
  929. (ert-deftest test-org-element/horizontal-rule-parser ()
  930. "Test `horizontal-rule' parser."
  931. ;; Standard.
  932. (should
  933. (org-test-with-temp-text "-----"
  934. (org-element-map (org-element-parse-buffer) 'horizontal-rule 'identity)))
  935. ;; Indented.
  936. (should
  937. (org-test-with-temp-text " -----"
  938. (org-element-map (org-element-parse-buffer) 'horizontal-rule 'identity)))
  939. ;; Hyphen must be alone on the line.
  940. (should-not
  941. (org-test-with-temp-text "-----wrong"
  942. (org-element-map (org-element-parse-buffer) 'horizontal-rule 'identity)))
  943. ;; 4 hyphens is too small.
  944. (should-not
  945. (org-test-with-temp-text "----"
  946. (org-element-map (org-element-parse-buffer) 'horizontal-rule 'identity)))
  947. ;; Handle non-empty blank line at the end of buffer.
  948. (should
  949. (org-test-with-temp-text "-----\n "
  950. (= (org-element-property :end (org-element-at-point)) (point-max)))))
  951. ;;;; Inline Babel Call
  952. (ert-deftest test-org-element/inline-babel-call-parser ()
  953. "Test `inline-babel-call' parser."
  954. (should
  955. (org-test-with-temp-text "call_test()"
  956. (org-element-map
  957. (org-element-parse-buffer) 'inline-babel-call 'identity))))
  958. ;;;; Inline Src Block
  959. (ert-deftest test-org-element/inline-src-block-parser ()
  960. "Test `inline-src-block' parser."
  961. (should
  962. (org-test-with-temp-text "src_emacs-lisp{(+ 1 1)}"
  963. (org-element-map (org-element-parse-buffer) 'inline-src-block 'identity)))
  964. ;; With switches.
  965. (should
  966. (org-test-with-temp-text "src_emacs-lisp[:foo bar]{(+ 1 1)}"
  967. (org-element-map (org-element-parse-buffer) 'inline-src-block 'identity)))
  968. (should
  969. (org-test-with-temp-text "src_emacs-lisp[ :foo bar]{(+ 1 1)}"
  970. (org-element-map (org-element-parse-buffer) 'inline-src-block 'identity)))
  971. ;; Empty switches.
  972. (should
  973. (org-test-with-temp-text "src_emacs-lisp[]{(+ 1 1)}"
  974. (org-element-map (org-element-parse-buffer) 'inline-src-block 'identity)))
  975. ;; Invalid syntax.
  976. (should-not
  977. (org-test-with-temp-text "foosrc_emacs-lisp[]{(+ 1 1)}"
  978. (org-element-map (org-element-parse-buffer) 'inline-src-block 'identity)))
  979. (should-not
  980. (org-test-with-temp-text "src_emacs-lisp[]foo{(+ 1 1)}"
  981. (org-element-map (org-element-parse-buffer) 'inline-src-block 'identity)))
  982. ;; Invalid language name
  983. (should-not
  984. (org-test-with-temp-text "src_emacs-\tlisp{(+ 1 1)}"
  985. (org-element-map (org-element-parse-buffer) 'inline-src-block 'identity)))
  986. ;; Test parsing at the beginning of an item.
  987. (should
  988. (org-test-with-temp-text "- src_emacs-lisp{(+ 1 1)}"
  989. (org-element-map (org-element-parse-buffer) 'inline-src-block 'identity))))
  990. ;;;; Inlinetask
  991. (ert-deftest test-org-element/inlinetask-parser ()
  992. "Test `inlinetask' parser."
  993. (when (featurep 'org-inlinetask)
  994. (let ((org-inlinetask-min-level 15))
  995. ;; Regular inlinetask.
  996. (should
  997. (eq 'inlinetask
  998. (org-test-with-temp-text
  999. "*************** Task\nTest\n*************** END"
  1000. (org-element-type (org-element-at-point)))))
  1001. ;; Degenerate inlinetask.
  1002. (should
  1003. (eq 'inlinetask
  1004. (org-test-with-temp-text "*************** Task"
  1005. (org-element-type (org-element-at-point)))))
  1006. ;; Mixed inlinetasks.
  1007. (should-not
  1008. (org-test-with-temp-text
  1009. "
  1010. *************** Task
  1011. *************** Task2
  1012. Contents
  1013. *************** END"
  1014. (forward-line)
  1015. (goto-char (org-element-property :end (org-element-at-point)))
  1016. (eobp)))
  1017. ;; TODO keyword.
  1018. (should
  1019. (equal
  1020. "TODO"
  1021. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  1022. (org-test-with-temp-text "*************** TODO Task"
  1023. (org-element-property :todo-keyword (org-element-at-point))))))
  1024. ;; Planning info.
  1025. (should
  1026. (org-test-with-temp-text "
  1027. *************** Task
  1028. DEADLINE: <2012-03-29 thu.>"
  1029. (forward-line)
  1030. (org-element-property :deadline (org-element-at-point))))
  1031. ;; Priority.
  1032. (should
  1033. (eq
  1034. ?A
  1035. (org-test-with-temp-text "
  1036. *************** [#A] Task"
  1037. (forward-line)
  1038. (org-element-property :priority (org-element-at-point)))))
  1039. ;; Tags.
  1040. (should
  1041. (equal
  1042. '("test")
  1043. (org-test-with-temp-text "
  1044. *************** Task :test:"
  1045. (forward-line)
  1046. (org-element-property :tags (org-element-at-point)))))
  1047. ;; Regular properties are accessed through upper case keywords.
  1048. (should
  1049. (org-test-with-temp-text "
  1050. *************** Task
  1051. :PROPERTIES:
  1052. :foo: bar
  1053. :END:
  1054. *************** END"
  1055. (forward-line)
  1056. (org-element-property :FOO (org-element-at-point))))
  1057. (should-not
  1058. (org-test-with-temp-text "
  1059. *************** Task
  1060. :PROPERTIES:
  1061. :foo: bar
  1062. :END:
  1063. *************** END"
  1064. (forward-line)
  1065. (org-element-property :foo (org-element-at-point))))
  1066. ;; Handle non-empty blank line at the end of buffer.
  1067. (should
  1068. (org-test-with-temp-text "*************** Task\n*************** END\n "
  1069. (= (org-element-property :end (org-element-at-point)) (point-max)))))))
  1070. ;;;; Italic
  1071. (ert-deftest test-org-element/italic-parser ()
  1072. "Test `italic' parser."
  1073. ;; Regular test.
  1074. (should
  1075. (let ((org-emph-re "\\([ ('\"{]\\|^\\)\\(\\([+*/_=~]\\)\\([^ \n,\"']\\|[^ \n,\"'].*?\\(?:\n.*?\\)\\{0,1\\}[^ \n,\"']\\)\\3\\)\\([- .,:!?;'\")}\\]\\|$\\)"))
  1076. (org-test-with-temp-text "/italic/"
  1077. (org-element-map (org-element-parse-buffer) 'italic 'identity nil t))))
  1078. ;; Multi-line markup.
  1079. (should
  1080. (equal
  1081. (org-element-contents
  1082. (let ((org-emph-re "\\([ ('\"{]\\|^\\)\\(\\([+*/_=~]\\)\\([^ \n,\"']\\|[^ \n,\"'].*?\\(?:\n.*?\\)\\{0,1\\}[^ \n,\"']\\)\\3\\)\\([- .,:!?;'\")}\\]\\|$\\)"))
  1083. (org-test-with-temp-text "/first line\nsecond line/"
  1084. (org-element-map (org-element-parse-buffer) 'italic 'identity nil t))))
  1085. '("first line\nsecond line"))))
  1086. ;;;; Item
  1087. (ert-deftest test-org-element/item-parser ()
  1088. "Test `item' parser."
  1089. ;; Standard test.
  1090. (should
  1091. (org-test-with-temp-text "- item"
  1092. (org-element-map (org-element-parse-buffer) 'item 'identity)))
  1093. ;; Counter.
  1094. (should
  1095. (= 6
  1096. (org-element-property
  1097. :counter
  1098. (org-test-with-temp-text "6. [@6] item"
  1099. (org-element-map (org-element-parse-buffer) 'item 'identity nil t)))))
  1100. ;; Tag
  1101. (should
  1102. (equal
  1103. '("tag")
  1104. (org-element-property
  1105. :tag
  1106. (org-test-with-temp-text "- tag :: description"
  1107. (org-element-map (org-element-parse-buffer) 'item 'identity nil t)))))
  1108. ;; No tags in ordered lists.
  1109. (should-not
  1110. (org-element-property
  1111. :tag
  1112. (org-test-with-temp-text "1. tag :: description"
  1113. (org-element-map (org-element-parse-buffer) 'item 'identity nil t))))
  1114. ;; Check-boxes
  1115. (should
  1116. (equal
  1117. '(trans on off)
  1118. (org-test-with-temp-text "
  1119. - [-] item 1
  1120. - [X] item 1.1
  1121. - [ ] item 1.2"
  1122. (org-element-map
  1123. (org-element-parse-buffer) 'item
  1124. (lambda (item) (org-element-property :checkbox item))))))
  1125. ;; Item starting with special syntax.
  1126. (should
  1127. (equal '(("- item"))
  1128. (org-test-with-temp-text "- - item"
  1129. (org-element-map (org-element-parse-buffer) 'paragraph
  1130. 'org-element-contents))))
  1131. ;; Block in an item: ignore indentation within the block.
  1132. (should
  1133. (org-test-with-temp-text "- item\n #+begin_src emacs-lisp\n(+ 1 1)\n #+end_src"
  1134. (forward-char)
  1135. (= (org-element-property :end (org-element-at-point)) (point-max)))))
  1136. ;;;; Keyword
  1137. (ert-deftest test-org-element/keyword-parser ()
  1138. "Test `keyword' parser."
  1139. ;; Standard test.
  1140. (should
  1141. (org-test-with-temp-text "#+KEYWORD: value"
  1142. (org-element-map (org-element-parse-buffer) 'keyword 'identity)))
  1143. ;; Keywords are case-insensitive.
  1144. (should
  1145. (org-test-with-temp-text "#+keyword: value"
  1146. (org-element-map (org-element-parse-buffer) 'keyword 'identity)))
  1147. ;; Affiliated keywords are not keywords.
  1148. (should-not
  1149. (org-test-with-temp-text "#+NAME: value
  1150. Paragraph"
  1151. (org-element-map (org-element-parse-buffer) 'keyword 'identity)))
  1152. ;; Do not mix keywords with Babel calls and dynamic blocks.
  1153. (should-not
  1154. (org-test-with-temp-text "#+CALL: fun()"
  1155. (org-element-map (org-element-parse-buffer) 'keyword 'identity)))
  1156. (should-not
  1157. (org-test-with-temp-text "#+BEGIN: my-fun\nBody\n#+END:"
  1158. (org-element-map (org-element-parse-buffer) 'keyword 'identity)))
  1159. ;; Handle non-empty blank line at the end of buffer.
  1160. (should
  1161. (org-test-with-temp-text "#+KEYWORD: value\n "
  1162. (= (org-element-property :end (org-element-at-point)) (point-max)))))
  1163. ;;;; Latex Environment
  1164. (ert-deftest test-org-element/latex-environment-parser ()
  1165. "Test `latex-environment' parser."
  1166. (should
  1167. (org-test-with-temp-text "\\begin{equation}\ne^{i\\pi}+1=0\n\\end{equation}"
  1168. (org-element-map (org-element-parse-buffer) 'latex-environment 'identity)))
  1169. ;; Allow nested environments.
  1170. (should
  1171. (equal
  1172. "\\begin{outer}
  1173. \\begin{inner}
  1174. e^{i\\pi}+1=0
  1175. \\end{inner}
  1176. \\end{outer}"
  1177. (org-test-with-temp-text "
  1178. \\begin{outer}
  1179. \\begin{inner}
  1180. e^{i\\pi}+1=0
  1181. \\end{inner}
  1182. \\end{outer}"
  1183. (org-element-property
  1184. :value
  1185. (org-element-map
  1186. (org-element-parse-buffer) 'latex-environment 'identity nil t)))))
  1187. ;; Allow environments with options and arguments.
  1188. (should
  1189. (eq 'latex-environment
  1190. (org-test-with-temp-text
  1191. "\\begin{theorem}[Euler]\ne^{i\\pi}+1=0\n\\end{theorem}"
  1192. (org-element-type (org-element-at-point)))))
  1193. (should
  1194. (eq 'latex-environment
  1195. (org-test-with-temp-text "\\begin{env}{arg}\nvalue\n\\end{env}"
  1196. (org-element-type (org-element-at-point)))))
  1197. ;; Allow environments without newline after \begin{.}.
  1198. (should
  1199. (eq 'latex-environment
  1200. (org-test-with-temp-text "\\begin{env}{arg}something\nvalue\n\\end{env}"
  1201. (org-element-type (org-element-at-point)))))
  1202. ;; Allow one-line environments.
  1203. (should
  1204. (eq 'latex-environment
  1205. (org-test-with-temp-text "\\begin{env}{arg}something\\end{env}"
  1206. (org-element-type (org-element-at-point)))))
  1207. ;; Should not allow different tags.
  1208. (should-not
  1209. (eq 'latex-environment
  1210. (org-test-with-temp-text "\\begin{env*}{arg}something\\end{env}"
  1211. (org-element-type (org-element-at-point)))))
  1212. ;; LaTeX environments must be on separate lines.
  1213. (should-not
  1214. (eq 'latex-environment
  1215. (org-test-with-temp-text "\\begin{env} x \\end{env} y"
  1216. (org-element-type (org-element-at-point)))))
  1217. (should-not
  1218. (eq 'latex-environment
  1219. (org-test-with-temp-text "y \\begin{env} x<point> \\end{env}"
  1220. (org-element-type (org-element-at-point)))))
  1221. ;; Handle non-empty blank line at the end of buffer.
  1222. (should
  1223. (org-test-with-temp-text "\\begin{env}\n\\end{env}\n "
  1224. (= (org-element-property :end (org-element-at-point)) (point-max)))))
  1225. ;;;; Latex Fragment
  1226. (ert-deftest test-org-element/latex-fragment-parser ()
  1227. "Test `latex-fragment' parser."
  1228. (should
  1229. (org-test-with-temp-text "$a$"
  1230. (org-element-map (org-element-parse-buffer) 'latex-fragment 'identity)))
  1231. (should
  1232. (org-test-with-temp-text "$$a$$"
  1233. (org-element-map (org-element-parse-buffer) 'latex-fragment 'identity)))
  1234. (should
  1235. (org-test-with-temp-text "\\(a\\)"
  1236. (org-element-map (org-element-parse-buffer) 'latex-fragment 'identity)))
  1237. (should
  1238. (org-test-with-temp-text "\\[a\\]"
  1239. (org-element-map
  1240. (org-element-parse-buffer) 'latex-fragment 'identity)))
  1241. ;; Test fragment at the beginning of an item.
  1242. (should
  1243. (eq 'latex-fragment
  1244. (org-test-with-temp-text "- $x$"
  1245. (progn (search-forward "$")
  1246. (org-element-type (org-element-context)))))))
  1247. ;;;; Line Break
  1248. (ert-deftest test-org-element/line-break-parser ()
  1249. "Test `line-break' parser."
  1250. ;; Regular test.
  1251. (should
  1252. (org-test-with-temp-text "Text \\\\"
  1253. (org-element-map (org-element-parse-buffer) 'line-break 'identity)))
  1254. ;; Line break with trailing white spaces.
  1255. (should
  1256. (org-test-with-temp-text "Text \\\\ "
  1257. (org-element-map (org-element-parse-buffer) 'line-break 'identity)))
  1258. ;; Three backslashes are too much.
  1259. (should-not
  1260. (org-test-with-temp-text "Text \\\\\\"
  1261. (org-element-map (org-element-parse-buffer) 'line-break 'identity))))
  1262. ;;;; Link
  1263. (ert-deftest test-org-element/link-parser ()
  1264. "Test `link' parser."
  1265. ;; Radio target.
  1266. (should
  1267. (equal
  1268. "radio"
  1269. (org-test-with-temp-text "<<<radio>>>A radio link"
  1270. (org-update-radio-target-regexp)
  1271. (org-element-property
  1272. :type
  1273. (org-element-map (org-element-parse-buffer) 'link #'identity nil t)))))
  1274. ;; Standard link.
  1275. ;;
  1276. ;; ... with description.
  1277. (should
  1278. (equal
  1279. '("Orgmode.org")
  1280. (org-test-with-temp-text "[[http://orgmode.org][Orgmode.org]]"
  1281. (org-element-contents
  1282. (org-element-map (org-element-parse-buffer) 'link 'identity nil t)))))
  1283. ;; ... without description.
  1284. (should
  1285. (equal
  1286. "http"
  1287. (org-test-with-temp-text "[[http://orgmode.org]]"
  1288. (org-element-property
  1289. :type
  1290. (org-element-map (org-element-parse-buffer) 'link 'identity nil t)))))
  1291. ;; ... with expansion.
  1292. (should
  1293. (equal
  1294. "//orgmode.org/worg"
  1295. (org-test-with-temp-text "[[Org:worg]]"
  1296. (let ((org-link-abbrev-alist '(("Org" . "http://orgmode.org/"))))
  1297. (org-element-property
  1298. :path
  1299. (org-element-map (org-element-parse-buffer) 'link 'identity nil t))))))
  1300. ;; ... with translation.
  1301. (should
  1302. (equal
  1303. "127.0.0.1"
  1304. (org-test-with-temp-text "[[http://orgmode.org]]"
  1305. (flet ((link-translate (type path) (cons type "127.0.0.1")))
  1306. (let ((org-link-translation-function 'link-translate))
  1307. (org-element-property
  1308. :path
  1309. (org-element-map (org-element-parse-buffer) 'link
  1310. 'identity nil t)))))))
  1311. ;; ... id link.
  1312. (should
  1313. (equal
  1314. "id"
  1315. (org-test-with-temp-text "[[id:aaaa]]"
  1316. (org-element-property
  1317. :type
  1318. (org-element-map (org-element-parse-buffer) 'link 'identity nil t)))))
  1319. ;; ... custom-id link.
  1320. (should
  1321. (equal
  1322. "custom-id"
  1323. (org-test-with-temp-text "[[#some-id]]"
  1324. (org-element-property
  1325. :type
  1326. (org-element-map (org-element-parse-buffer) 'link 'identity nil t)))))
  1327. ;; ... coderef link.
  1328. (should
  1329. (equal
  1330. "coderef"
  1331. (org-test-with-temp-text "[[(reference)]]"
  1332. (org-element-property
  1333. :type
  1334. (org-element-map (org-element-parse-buffer) 'link 'identity nil t)))))
  1335. ;; ... fuzzy link.
  1336. (should
  1337. (equal
  1338. "fuzzy"
  1339. (org-test-with-temp-text "[[target-or-title]]"
  1340. (org-element-property
  1341. :type
  1342. (org-element-map (org-element-parse-buffer) 'link 'identity nil t)))))
  1343. ;; ... file-type link with search option.
  1344. (should
  1345. (equal
  1346. '(("file" "projects.org" "*task title"))
  1347. (org-test-with-temp-text "[[file:projects.org::*task title]]"
  1348. (org-element-map (org-element-parse-buffer) 'link
  1349. (lambda (l) (list (org-element-property :type l)
  1350. (org-element-property :path l)
  1351. (org-element-property :search-option l)))))))
  1352. ;; ... file-type link with application...
  1353. (should
  1354. (equal
  1355. '("file" "projects.org" "docview")
  1356. (org-test-with-temp-text "[[docview:projects.org]]"
  1357. (let ((l (org-element-context)))
  1358. (list (org-element-property :type l)
  1359. (org-element-property :path l)
  1360. (org-element-property :application l))))))
  1361. ;; ... `:path' in a file-type link must be compatible with "file"
  1362. ;; scheme in URI syntax, even if Org syntax isn't...
  1363. (should
  1364. (org-test-with-temp-text-in-file ""
  1365. (let ((file (expand-file-name (buffer-file-name))))
  1366. (insert (format "[[file://%s]]" file))
  1367. (equal (org-element-property :path (org-element-context))
  1368. (concat "//" file)))))
  1369. (should
  1370. (org-test-with-temp-text-in-file ""
  1371. (let ((file (expand-file-name (buffer-file-name))))
  1372. (insert (format "[[file:%s]]" file))
  1373. (equal (org-element-property :path (org-element-context))
  1374. (concat "//" file)))))
  1375. (should
  1376. (org-test-with-temp-text-in-file ""
  1377. (let ((file (file-relative-name (buffer-file-name))))
  1378. (insert (format "[[file:%s]]" file))
  1379. (list (org-element-property :path (org-element-context)) file))))
  1380. ;; ... multi-line link.
  1381. (should
  1382. (equal "//orgmode.org"
  1383. (org-test-with-temp-text "[[http://orgmode.\norg]]"
  1384. (org-element-property :path (org-element-context)))))
  1385. ;; Plain link.
  1386. (should
  1387. (org-test-with-temp-text "A link: http://orgmode.org"
  1388. (org-element-map (org-element-parse-buffer) 'link 'identity)))
  1389. ;; Angular link.
  1390. (should
  1391. (org-test-with-temp-text "A link: <http://orgmode.org>"
  1392. (org-element-map (org-element-parse-buffer) 'link 'identity nil t)))
  1393. ;; Link abbreviation.
  1394. (should
  1395. (equal "http"
  1396. (org-test-with-temp-text
  1397. "#+LINK: orgmode http://www.orgmode.org/\n[[orgmode:#docs]]"
  1398. (progn (org-mode-restart)
  1399. (goto-char (1- (point-max)))
  1400. (org-element-property :type (org-element-context))))))
  1401. ;; Link abbreviation in a secondary string.
  1402. (should
  1403. (equal "http"
  1404. (org-test-with-temp-text
  1405. "#+LINK: orgmode http://www.orgmode.org/\n* H [[orgmode:#docs]]"
  1406. (progn (org-mode-restart)
  1407. (org-element-map (org-element-parse-buffer) 'link
  1408. (lambda (link) (org-element-property :type link))
  1409. nil t nil t)))))
  1410. ;; Plain links are allowed as description of regular links.
  1411. (should
  1412. (equal "file"
  1413. (org-test-with-temp-text "[[http://orgmode.org][file:unicorn.jpg]]"
  1414. (search-forward "file:")
  1415. (org-element-property :type (org-element-context))))))
  1416. ;;;; Macro
  1417. (ert-deftest test-org-element/macro-parser ()
  1418. "Test `macro' parser."
  1419. ;; Without arguments.
  1420. (should
  1421. (org-test-with-temp-text "{{{macro}}}"
  1422. (org-element-map (org-element-parse-buffer) 'macro 'identity)))
  1423. ;; With arguments.
  1424. (should
  1425. (org-test-with-temp-text "{{{macro(arg1,arg2)}}}"
  1426. (org-element-map (org-element-parse-buffer) 'macro 'identity)))
  1427. ;; Properly handle protected commas in arguments...
  1428. (should
  1429. (= 2
  1430. (length
  1431. (org-test-with-temp-text "{{{macro(arg1\\,arg1,arg2)}}}"
  1432. (org-element-property :args (org-element-context))))))
  1433. ;; ... even when last argument ends with a protected comma.
  1434. (should
  1435. (equal '("C-,")
  1436. (org-test-with-temp-text "{{{macro(C-\\,)}}}"
  1437. (org-element-property :args (org-element-context)))))
  1438. ;; Allow to escape escaping character.
  1439. (should
  1440. (equal '("C-\\" "")
  1441. (org-test-with-temp-text "{{{macro(C-\\\\,)}}}"
  1442. (org-element-property :args (org-element-context)))))
  1443. ;; No need to escape backslashes elsewhere.
  1444. (should
  1445. (equal '("\\")
  1446. (org-test-with-temp-text "{{{macro(\\)}}}"
  1447. (org-element-property :args (org-element-context))))))
  1448. ;;;; Node Property
  1449. (ert-deftest test-org-element/node-property ()
  1450. "Test `node-property' parser."
  1451. ;; Standard test.
  1452. (should
  1453. (equal '("abc" "value")
  1454. (org-test-with-temp-text ":PROPERTIES:\n<point>:abc: value\n:END:"
  1455. (let ((element (org-element-at-point)))
  1456. (list (org-element-property :key element)
  1457. (org-element-property :value element))))))
  1458. ;; Value should be trimmed.
  1459. (should
  1460. (equal "value"
  1461. (org-test-with-temp-text ":PROPERTIES:\n<point>:abc: value \n:END:"
  1462. (org-element-property :value (org-element-at-point)))))
  1463. ;; A node property requires to be wrapped within a property drawer.
  1464. (should-not
  1465. (eq 'node-property
  1466. (org-test-with-temp-text ":abc: value"
  1467. (org-element-type (org-element-at-point)))))
  1468. ;; Accept empty properties.
  1469. (should
  1470. (equal '(("foo" "value") ("bar" nil))
  1471. (org-test-with-temp-text ":PROPERTIES:\n:foo: value\n:bar:\n:END:"
  1472. (org-element-map (org-element-parse-buffer) 'node-property
  1473. (lambda (p)
  1474. (list (org-element-property :key p)
  1475. (org-element-property :value p)))))))
  1476. ;; Ignore all non-property lines in property drawers.
  1477. (should
  1478. (equal
  1479. '(("foo" "value"))
  1480. (org-test-with-temp-text ":PROPERTIES:\nWrong1\n:foo: value\nWrong2\n:END:"
  1481. (org-element-map (org-element-parse-buffer) 'node-property
  1482. (lambda (p)
  1483. (list (org-element-property :key p)
  1484. (org-element-property :value p))))))))
  1485. ;;;; Paragraph
  1486. (ert-deftest test-org-element/paragraph-parser ()
  1487. "Test `paragraph' parser."
  1488. ;; Standard test.
  1489. (should
  1490. (org-test-with-temp-text "Paragraph"
  1491. (org-element-map (org-element-parse-buffer) 'paragraph 'identity nil t)))
  1492. ;; Property find end of a paragraph stuck to another element.
  1493. (should
  1494. (eq ?#
  1495. (org-test-with-temp-text "Paragraph\n# Comment"
  1496. (org-element-map (org-element-parse-buffer) 'paragraph
  1497. (lambda (p) (char-after (org-element-property :end p)))
  1498. nil t))))
  1499. ;; Include ill-formed Keywords.
  1500. (should
  1501. (org-test-with-temp-text "#+wrong_keyword something"
  1502. (org-element-map (org-element-parse-buffer) 'paragraph 'identity)))
  1503. ;; Include incomplete-drawers.
  1504. (should
  1505. (org-test-with-temp-text ":TEST:\nParagraph"
  1506. (let ((elem (org-element-at-point)))
  1507. (and (eq (org-element-type elem) 'paragraph)
  1508. (= (point-max) (org-element-property :end elem))))))
  1509. ;; Include incomplete blocks.
  1510. (should
  1511. (org-test-with-temp-text "#+BEGIN_CENTER\nParagraph"
  1512. (let ((elem (org-element-at-point)))
  1513. (and (eq (org-element-type elem) 'paragraph)
  1514. (= (point-max) (org-element-property :end elem))))))
  1515. ;; Include incomplete dynamic blocks.
  1516. (should
  1517. (org-test-with-temp-text "#+BEGIN: \nParagraph"
  1518. (let ((elem (org-element-at-point)))
  1519. (and (eq (org-element-type elem) 'paragraph)
  1520. (= (point-max) (org-element-property :end elem))))))
  1521. ;; Include incomplete latex environments.
  1522. (should
  1523. (org-test-with-temp-text "\begin{equation}\nParagraph"
  1524. (let ((elem (org-element-at-point)))
  1525. (and (eq (org-element-type elem) 'paragraph)
  1526. (= (point-max) (org-element-property :end elem))))))
  1527. ;; Do not steal affiliated keywords from container.
  1528. (should
  1529. (org-test-with-temp-text "#+ATTR_LATEX: test\n- item 1"
  1530. (let ((elem (progn (search-forward "item") (org-element-at-point))))
  1531. (and (eq (org-element-type elem) 'paragraph)
  1532. (not (org-element-property :attr_latex elem))
  1533. (/= (org-element-property :begin elem) 1)))))
  1534. ;; Handle non-empty blank line at the end of buffer.
  1535. (should
  1536. (org-test-with-temp-text "#+BEGIN_CENTER\nC\n#+END_CENTER\n "
  1537. (= (org-element-property :end (org-element-at-point)) (point-max)))))
  1538. ;;;; Plain List
  1539. (ert-deftest test-org-element/plain-list-parser ()
  1540. "Test `plain-list' parser."
  1541. (org-test-with-temp-text "- item"
  1542. (should (org-element-map (org-element-parse-buffer) 'plain-list 'identity)))
  1543. ;; Blank lines after the list only belong to outer plain list.
  1544. (should
  1545. (equal
  1546. '(t t)
  1547. (org-test-with-temp-text "
  1548. - outer
  1549. - inner
  1550. Outside list"
  1551. (let ((endings (org-element-map (org-element-parse-buffer) 'plain-list
  1552. (lambda (pl) (org-element-property :end pl)))))
  1553. (list
  1554. ;; Move to ending of outer list.
  1555. (progn (goto-char (car endings)) (looking-at "Outside list"))
  1556. ;; Move to ending of inner list.
  1557. (progn (goto-char (nth 1 endings)) (looking-at "^$")))))))
  1558. ;; Correctly compute end of list if it doesn't end at a line
  1559. ;; beginning.
  1560. (should
  1561. (org-test-with-temp-text "- list\n \n "
  1562. (= (org-element-property :end (org-element-at-point)) (point-max)))))
  1563. ;;;; Planning
  1564. (ert-deftest test-org-element/planning-parser ()
  1565. "Test `planning' parser."
  1566. (should
  1567. (equal "[2012-03-29 thu.]"
  1568. (org-element-property
  1569. :raw-value
  1570. (org-element-property
  1571. :closed
  1572. (org-test-with-temp-text "CLOSED: [2012-03-29 thu.]"
  1573. (org-element-at-point))))))
  1574. (should
  1575. (equal "<2012-03-29 thu.>"
  1576. (org-element-property
  1577. :raw-value
  1578. (org-element-property
  1579. :deadline
  1580. (org-test-with-temp-text "DEADLINE: <2012-03-29 thu.>"
  1581. (org-element-at-point))))))
  1582. (should
  1583. (equal "<2012-03-29 thu.>"
  1584. (org-element-property
  1585. :raw-value
  1586. (org-element-property
  1587. :scheduled
  1588. (org-test-with-temp-text "SCHEDULED: <2012-03-29 thu.>"
  1589. (org-element-at-point)))))))
  1590. ;;;; Property Drawer
  1591. (ert-deftest test-org-element/property-drawer-parser ()
  1592. "Test `property-drawer' parser."
  1593. ;; Standard test.
  1594. (should
  1595. (org-test-with-temp-text ":PROPERTIES:\n:prop: value\n:END:"
  1596. (org-element-map
  1597. (org-element-parse-buffer) 'property-drawer 'identity nil t)))
  1598. ;; Do not mix property drawers and regular drawers.
  1599. (should-not
  1600. (org-test-with-temp-text ":TEST:\n:prop: value\n:END:"
  1601. (org-element-map
  1602. (org-element-parse-buffer) 'property-drawer 'identity nil t)))
  1603. ;; Ignore incomplete drawer.
  1604. (should-not
  1605. (org-test-with-temp-text ":PROPERTIES:\n:prop: value"
  1606. (org-element-map
  1607. (org-element-parse-buffer) 'property-drawer 'identity nil t)))
  1608. ;; Handle non-empty blank line at the end of buffer.
  1609. (should
  1610. (org-test-with-temp-text ":PROPERTIES:\n:END:\n "
  1611. (= (org-element-property :end (org-element-at-point)) (point-max)))))
  1612. ;;;; Quote Block
  1613. (ert-deftest test-org-element/quote-block-parser ()
  1614. "Test `quote-block' parser."
  1615. ;; Regular test.
  1616. (should
  1617. (org-test-with-temp-text "#+BEGIN_QUOTE\nText\n#+END_QUOTE"
  1618. (org-element-map (org-element-parse-buffer) 'quote-block 'identity)))
  1619. ;; Ignore incomplete block.
  1620. (should-not
  1621. (org-test-with-temp-text "#+BEGIN_QUOTE"
  1622. (org-element-map (org-element-parse-buffer) 'quote-block 'identity nil t)))
  1623. ;; Handle non-empty blank line at the end of buffer.
  1624. (should
  1625. (org-test-with-temp-text "#+BEGIN_QUOTE\nC\n#+END_QUOTE\n "
  1626. (= (org-element-property :end (org-element-at-point)) (point-max)))))
  1627. ;;;; Radio Target
  1628. (ert-deftest test-org-element/radio-target-parser ()
  1629. "Test `radio-target' parser."
  1630. ;; Standard test.
  1631. (should
  1632. (eq 'radio-target
  1633. (org-test-with-temp-text "<<<radio>>>"
  1634. (org-element-type (org-element-context)))))
  1635. ;; Radio targets with objects.
  1636. (should
  1637. (eq 'radio-target
  1638. (org-test-with-temp-text "<<<radio \\alpha>>>"
  1639. (org-element-type (org-element-context)))))
  1640. ;; Radio targets starting with an object.
  1641. (should
  1642. (eq 'radio-target
  1643. (org-test-with-temp-text "<<<\\alpha radio>>>"
  1644. (org-element-type (org-element-context)))))
  1645. ;; Radio targets cannot begin or end with white space.
  1646. (should-not
  1647. (eq 'radio-target
  1648. (org-test-with-temp-text "<<< radio>>>"
  1649. (org-element-type (org-element-context)))))
  1650. (should-not
  1651. (eq 'radio-target
  1652. (org-test-with-temp-text "<<<radio >>>"
  1653. (org-element-type (org-element-context))))))
  1654. ;;;; Section
  1655. (ert-deftest test-org-element/section-parser ()
  1656. "Test `section' parser."
  1657. ;; Standard test.
  1658. (should
  1659. (org-test-with-temp-text "* Headline\nText"
  1660. (org-element-map (org-element-parse-buffer) 'section 'identity)))
  1661. ;; There's a section before the first headline.
  1662. (should
  1663. (org-test-with-temp-text "Text"
  1664. (org-element-map (org-element-parse-buffer) 'section 'identity)))
  1665. ;; A section cannot be empty.
  1666. (should-not
  1667. (org-test-with-temp-text "* Headline 1\n* Headline 2"
  1668. (org-element-map (org-element-parse-buffer) 'section 'identity)))
  1669. ;; A section doesn't contain sub-trees.
  1670. (should-not
  1671. (org-test-with-temp-text "* Head\nText\n** Sub-Head"
  1672. (org-element-map
  1673. (org-element-map (org-element-parse-buffer) 'section 'identity nil t)
  1674. 'headline 'identity))))
  1675. ;;;; Special Block
  1676. (ert-deftest test-org-element/special-block-parser ()
  1677. "Test `special-block' parser."
  1678. ;; Standard test.
  1679. (should
  1680. (equal "SPECIAL"
  1681. (org-test-with-temp-text "#+BEGIN_SPECIAL\nText\n#+END_SPECIAL"
  1682. (org-element-property :type (org-element-at-point)))))
  1683. ;; Special blocks can contain paragraphs.
  1684. (should
  1685. (eq 'paragraph
  1686. (org-test-with-temp-text "#+BEGIN_SPECIAL\nText\n#+END_SPECIAL"
  1687. (forward-line)
  1688. (org-element-type (org-element-at-point)))))
  1689. ;; Ignore incomplete block.
  1690. (should-not
  1691. (eq 'special-block
  1692. (org-test-with-temp-text "#+BEGIN_SPECIAL"
  1693. (org-element-type (org-element-at-point)))))
  1694. ;; Allow special characters in type.
  1695. (should
  1696. (equal '(special-block "SPECIAL*")
  1697. (org-test-with-temp-text "#+BEGIN_SPECIAL*\nContents\n#+END_SPECIAL*"
  1698. (let ((element (org-element-at-point)))
  1699. (list (org-element-type element)
  1700. (org-element-property :type element))))))
  1701. ;; Handle non-empty blank line at the end of buffer.
  1702. (should
  1703. (org-test-with-temp-text "#+BEGIN_SPECIAL\nC\n#+END_SPECIAL\n "
  1704. (= (org-element-property :end (org-element-at-point)) (point-max)))))
  1705. ;;;; Src Block
  1706. (ert-deftest test-org-element/src-block-parser ()
  1707. "Test `src-block' parser."
  1708. ;; Regular tests.
  1709. (should
  1710. (org-test-with-temp-text "#+BEGIN_SRC org\nText\n#+END_SRC"
  1711. (org-element-map (org-element-parse-buffer) 'src-block 'identity)))
  1712. ;; Ignore incomplete block.
  1713. (should-not
  1714. (org-test-with-temp-text "#+BEGIN_SRC"
  1715. (org-element-map (org-element-parse-buffer) 'src-block 'identity)))
  1716. ;; Properly un-escape code.
  1717. (should
  1718. (equal "* Headline\n #+keyword\nText\n"
  1719. (org-test-with-temp-text
  1720. "#+BEGIN_SRC org\n,* Headline\n ,#+keyword\nText\n#+END_SRC"
  1721. (org-element-property :value (org-element-at-point)))))
  1722. ;; Remove block indentation according to block boundaries, unless
  1723. ;; block contents are less indented than block boundaries.
  1724. (should
  1725. (equal " L1\nL2\n"
  1726. (org-test-with-temp-text " #+BEGIN_SRC org\n L1\n L2\n #+END_SRC"
  1727. (let ((org-src-preserve-indentation t))
  1728. (org-element-property :value (org-element-at-point))))))
  1729. (should
  1730. (equal
  1731. " L1\n L2\n"
  1732. (org-test-with-temp-text " #+BEGIN_SRC org\n L1\n L2\n #+END_SRC"
  1733. (let ((org-src-preserve-indentation t))
  1734. (org-element-property :value (org-element-at-point))))))
  1735. ;; Handle non-empty blank line at the end of buffer.
  1736. (should
  1737. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\nC\n#+END_SRC\n "
  1738. (= (org-element-property :end (org-element-at-point)) (point-max)))))
  1739. ;;;; Statistics Cookie
  1740. (ert-deftest test-org-element/statistics-cookie ()
  1741. "Test `statistics-cookie' parser."
  1742. ;; With numbers.
  1743. (should
  1744. (org-test-with-temp-text "[1/2]"
  1745. (org-element-map (org-element-parse-buffer) 'statistics-cookie 'identity)))
  1746. ;; With percents.
  1747. (should
  1748. (org-test-with-temp-text "[33%]"
  1749. (org-element-map
  1750. (org-element-parse-buffer) 'statistics-cookie 'identity))))
  1751. ;;;; Strike Through
  1752. (ert-deftest test-org-element/strike-through-parser ()
  1753. "Test `strike-through' parser."
  1754. ;; Regular test.
  1755. (should
  1756. (let ((org-emph-re "\\([ ('\"{]\\|^\\)\\(\\([+*/_=~]\\)\\([^ \n,\"']\\|[^ \n,\"'].*?\\(?:\n.*?\\)\\{0,1\\}[^ \n,\"']\\)\\3\\)\\([- .,:!?;'\")}\\]\\|$\\)"))
  1757. (org-test-with-temp-text "+strike-through+"
  1758. (org-element-map (org-element-parse-buffer) 'strike-through 'identity))))
  1759. ;; Multi-line markup.
  1760. (should
  1761. (equal
  1762. (org-element-contents
  1763. (let ((org-emph-re "\\([ ('\"{]\\|^\\)\\(\\([+*/_=~]\\)\\([^ \n,\"']\\|[^ \n,\"'].*?\\(?:\n.*?\\)\\{0,1\\}[^ \n,\"']\\)\\3\\)\\([- .,:!?;'\")}\\]\\|$\\)"))
  1764. (org-test-with-temp-text "+first line\nsecond line+"
  1765. (org-element-map
  1766. (org-element-parse-buffer) 'strike-through 'identity nil t))))
  1767. '("first line\nsecond line"))))
  1768. ;;;; Subscript
  1769. (ert-deftest test-org-element/subscript-parser ()
  1770. "Test `subscript' parser."
  1771. ;; Without braces.
  1772. (should
  1773. (org-test-with-temp-text "a_b"
  1774. (org-element-map (org-element-parse-buffer) 'subscript 'identity)))
  1775. ;; With braces.
  1776. (should
  1777. (org-test-with-temp-text "a_{b}"
  1778. (org-element-map (org-element-parse-buffer) 'subscript 'identity)))
  1779. ;; Multiple subscripts in a paragraph.
  1780. (should
  1781. (= 2
  1782. (org-test-with-temp-text "a_b and c_d"
  1783. (length
  1784. (org-element-map (org-element-parse-buffer) 'subscript 'identity))))))
  1785. ;;;; Superscript
  1786. (ert-deftest test-org-element/superscript-parser ()
  1787. "Test `superscript' parser."
  1788. ;; Without braces.
  1789. (should
  1790. (org-test-with-temp-text "a^b"
  1791. (org-element-map (org-element-parse-buffer) 'superscript 'identity)))
  1792. ;; With braces.
  1793. (should
  1794. (org-test-with-temp-text "a^{b}"
  1795. (org-element-map (org-element-parse-buffer) 'superscript 'identity)))
  1796. ;; Multiple superscript in a paragraph.
  1797. (should
  1798. (= 2
  1799. (org-test-with-temp-text "a^b and c^d"
  1800. (length
  1801. (org-element-map
  1802. (org-element-parse-buffer) 'superscript 'identity))))))
  1803. ;;;; Table
  1804. (ert-deftest test-org-element/table-parser ()
  1805. "Test `table' parser."
  1806. (should
  1807. (org-test-with-temp-text "| a |"
  1808. (org-element-map (org-element-parse-buffer) 'table 'identity)))
  1809. ;; TBLFM keyword is case insensitive.
  1810. (should
  1811. (org-test-with-temp-text "| a |\n#+tblfm: test"
  1812. (org-element-property
  1813. :tblfm
  1814. (org-element-map (org-element-parse-buffer) 'table 'identity nil t))))
  1815. ;; Handle multiple TBLFM lines.
  1816. (should
  1817. (= 2
  1818. (org-test-with-temp-text "| a |\n#+TBLFM: test1\n#+TBLFM: test2"
  1819. (length (org-element-property
  1820. :tblfm
  1821. (org-element-map
  1822. (org-element-parse-buffer) 'table 'identity nil t))))))
  1823. ;; Handle non-empty blank line at the end of buffer.
  1824. (should
  1825. (org-test-with-temp-text "| a |\n "
  1826. (= (org-element-property :end (org-element-at-point)) (point-max)))))
  1827. ;;;; Table Cell
  1828. (ert-deftest test-org-element/table-cell-parser ()
  1829. "Test `table-cell' parser."
  1830. ;; Regular table cell.
  1831. (should
  1832. (org-test-with-temp-text "| a |"
  1833. (org-element-map (org-element-parse-buffer) 'table-cell 'identity)))
  1834. ;; Last vertical bar may be omitted.
  1835. (should
  1836. (org-test-with-temp-text "| a "
  1837. (org-element-map (org-element-parse-buffer) 'table-cell 'identity))))
  1838. ;;;; Table Row
  1839. (ert-deftest test-org-element/table-row-parser ()
  1840. "Test `table-row' parser."
  1841. (should
  1842. (equal '(standard rule)
  1843. (org-test-with-temp-text "| a |\n|---|"
  1844. (org-element-map
  1845. (org-element-parse-buffer) 'table-row
  1846. (lambda (row) (org-element-property :type row)))))))
  1847. ;;;; Target
  1848. (ert-deftest test-org-element/target-parser ()
  1849. "Test `target' parser."
  1850. (should
  1851. (org-test-with-temp-text "<<target>>"
  1852. (org-element-map (org-element-parse-buffer) 'target 'identity))))
  1853. ;;;; Timestamp
  1854. (ert-deftest test-org-element/timestamp ()
  1855. "Test `timestamp' parser."
  1856. ;; Active timestamp.
  1857. (should
  1858. (org-test-with-temp-text "<2012-03-29 16:40>"
  1859. (eq (org-element-property :type (org-element-context)) 'active)))
  1860. (should-not
  1861. (org-test-with-temp-text "<2012-03-29 Thu>"
  1862. (let ((timestamp (org-element-context)))
  1863. (or (org-element-property :hour-start timestamp)
  1864. (org-element-property :minute-start timestamp)))))
  1865. (should
  1866. (equal '(2012 3 29 16 40)
  1867. (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
  1868. (let ((object (org-element-context)))
  1869. (list (org-element-property :year-start object)
  1870. (org-element-property :month-start object)
  1871. (org-element-property :day-start object)
  1872. (org-element-property :hour-start object)
  1873. (org-element-property :minute-start object))))))
  1874. ;; Inactive timestamp.
  1875. (should
  1876. (org-test-with-temp-text "[2012-03-29 Thu 16:40]"
  1877. (eq (org-element-property :type (org-element-context)) 'inactive)))
  1878. ;; Time range.
  1879. (should
  1880. (equal '(2012 3 29 16 40 7 30)
  1881. (org-test-with-temp-text "<2012-03-29 Thu 7:30-16:40>"
  1882. (let ((object (org-element-context)))
  1883. (list (org-element-property :year-end object)
  1884. (org-element-property :month-end object)
  1885. (org-element-property :day-end object)
  1886. (org-element-property :hour-end object)
  1887. (org-element-property :minute-end object)
  1888. (org-element-property :hour-start object)
  1889. (org-element-property :minute-start object))))))
  1890. (should
  1891. (eq 'active-range
  1892. (org-test-with-temp-text "<2012-03-29 Thu 7:30-16:40>"
  1893. (org-element-property :type (org-element-context)))))
  1894. ;; Date range.
  1895. (should
  1896. (org-test-with-temp-text "[2012-03-29 Thu 16:40]--[2012-03-29 Thu 16:41]"
  1897. (eq (org-element-property :type (org-element-context)) 'inactive-range)))
  1898. (should-not
  1899. (org-test-with-temp-text "[2011-07-14 Thu]--[2012-03-29 Thu]"
  1900. (let ((timestamp (org-element-context)))
  1901. (or (org-element-property :hour-end timestamp)
  1902. (org-element-property :minute-end timestamp)))))
  1903. ;; With repeater, warning delay and both.
  1904. (should
  1905. (eq 'catch-up
  1906. (org-test-with-temp-text "<2012-03-29 Thu ++1y>"
  1907. (org-element-property :repeater-type (org-element-context)))))
  1908. (should
  1909. (eq 'first
  1910. (org-test-with-temp-text "<2012-03-29 Thu --1y>"
  1911. (org-element-property :warning-type (org-element-context)))))
  1912. (should
  1913. (equal '(cumulate all)
  1914. (org-test-with-temp-text "<2012-03-29 Thu +1y -1y>"
  1915. (let ((ts (org-element-context)))
  1916. (list (org-element-property :repeater-type ts)
  1917. (org-element-property :warning-type ts))))))
  1918. ;; Timestamps are not planning elements.
  1919. (should-not
  1920. (org-test-with-temp-text "SCHEDULED: <2012-03-29 Thu 16:40>"
  1921. (org-element-map (org-element-parse-buffer) 'timestamp 'identity))))
  1922. ;;;; Underline
  1923. (ert-deftest test-org-element/underline-parser ()
  1924. "Test `underline' parser."
  1925. ;; Regular test.
  1926. (should
  1927. (let ((org-emph-re "\\([ ('\"{]\\|^\\)\\(\\([+*/_=~]\\)\\([^ \n,\"']\\|[^ \n,\"'].*?\\(?:\n.*?\\)\\{0,1\\}[^ \n,\"']\\)\\3\\)\\([- .,:!?;'\")}\\]\\|$\\)"))
  1928. (org-test-with-temp-text "_underline_"
  1929. (org-element-map (org-element-parse-buffer) 'underline 'identity))))
  1930. ;; Multi-line markup.
  1931. (should
  1932. (equal
  1933. (org-element-contents
  1934. (let ((org-emph-re "\\([ ('\"{]\\|^\\)\\(\\([+*/_=~]\\)\\([^ \n,\"']\\|[^ \n,\"'].*?\\(?:\n.*?\\)\\{0,1\\}[^ \n,\"']\\)\\3\\)\\([- .,:!?;'\")}\\]\\|$\\)"))
  1935. (org-test-with-temp-text "_first line\nsecond line_"
  1936. (org-element-map
  1937. (org-element-parse-buffer) 'underline 'identity nil t))))
  1938. '("first line\nsecond line"))))
  1939. ;;;; Verbatim
  1940. (ert-deftest test-org-element/verbatim-parser ()
  1941. "Test `verbatim' parser."
  1942. ;; Regular test.
  1943. (should
  1944. (let ((org-emph-re "\\([ ('\"{]\\|^\\)\\(\\([+*/_=~]\\)\\([^ \n,\"']\\|[^ \n,\"'].*?\\(?:\n.*?\\)\\{0,1\\}[^ \n,\"']\\)\\3\\)\\([- .,:!?;'\")}\\]\\|$\\)"))
  1945. (org-test-with-temp-text "=verbatim="
  1946. (org-element-map (org-element-parse-buffer) 'verbatim 'identity))))
  1947. ;; Multi-line markup.
  1948. (should
  1949. (equal
  1950. (org-element-property
  1951. :value
  1952. (let ((org-emph-re "\\([ ('\"{]\\|^\\)\\(\\([+*/_=~]\\)\\([^ \n,\"']\\|[^ \n,\"'].*?\\(?:\n.*?\\)\\{0,1\\}[^ \n,\"']\\)\\3\\)\\([- .,:!?;'\")}\\]\\|$\\)"))
  1953. (org-test-with-temp-text "=first line\nsecond line="
  1954. (org-element-map
  1955. (org-element-parse-buffer) 'verbatim 'identity nil t))))
  1956. "first line\nsecond line")))
  1957. ;;;; Verse Block
  1958. (ert-deftest test-org-element/verse-block-parser ()
  1959. "Test `verse-block' parser."
  1960. ;; Standard test.
  1961. (should
  1962. (org-test-with-temp-text "#+BEGIN_VERSE\nVerse block\n#+END_VERSE"
  1963. (org-element-map (org-element-parse-buffer) 'verse-block 'identity)))
  1964. ;; Ignore case.
  1965. (should
  1966. (org-test-with-temp-text "#+begin_verse\nVerse block\n#+end_verse"
  1967. (org-element-map (org-element-parse-buffer) 'verse-block 'identity)))
  1968. ;; Parse objects in verse blocks.
  1969. (should
  1970. (org-test-with-temp-text "#+BEGIN_VERSE\nVerse \\alpha\n#+END_VERSE"
  1971. (org-element-map (org-element-parse-buffer) 'entity 'identity)))
  1972. ;; Ignore incomplete verse block.
  1973. (should-not
  1974. (org-test-with-temp-text "#+BEGIN_VERSE"
  1975. (org-element-map (org-element-parse-buffer) 'verse-block 'identity nil t)))
  1976. ;; Handle non-empty blank line at the end of buffer.
  1977. (should
  1978. (org-test-with-temp-text "#+BEGIN_VERSE\nC\n#+END_VERSE\n "
  1979. (= (org-element-property :end (org-element-at-point)) (point-max)))))
  1980. ;;; Test Interpreters.
  1981. (ert-deftest test-org-element/interpret-data ()
  1982. "Test `org-element-interpret-data' specifications."
  1983. ;; Interpret simple affiliated keywords.
  1984. (should
  1985. (equal
  1986. (org-element-interpret-data
  1987. '(org-data nil (paragraph (:name "para") "Paragraph")))
  1988. "#+NAME: para\nParagraph\n"))
  1989. ;; Interpret multiple affiliated keywords.
  1990. (should
  1991. (equal
  1992. (org-element-interpret-data
  1993. '(org-data nil (paragraph (:attr_ascii ("line2" "line1")) "Paragraph")))
  1994. "#+ATTR_ASCII: line1\n#+ATTR_ASCII: line2\nParagraph\n"))
  1995. ;; Interpret parsed affiliated keywords.
  1996. (should
  1997. (equal
  1998. (org-element-interpret-data
  1999. '(org-data nil (paragraph (:caption (("caption"))) "Paragraph")))
  2000. "#+CAPTION: caption\nParagraph\n"))
  2001. ;; Interpret dual affiliated keywords.
  2002. (should
  2003. (equal
  2004. (org-element-interpret-data
  2005. '(org-data nil (paragraph (:caption ((("long") "short"))) "Paragraph")))
  2006. "#+CAPTION[short]: long\nParagraph\n"))
  2007. ;; Interpret multiple parsed dual keywords.
  2008. (should
  2009. (equal
  2010. (org-element-interpret-data
  2011. '(org-data nil (paragraph
  2012. (:caption ((("l2") "s2") (("l1") "s1"))) "Paragraph")))
  2013. "#+CAPTION[s1]: l1\n#+CAPTION[s2]: l2\nParagraph\n"))
  2014. ;; Pseudo objects and elements are transparent.
  2015. (should
  2016. (equal "A B\n"
  2017. (org-element-interpret-data
  2018. '(paragraph nil (pseudo-object (:post-blank 1) "A") "B")
  2019. '(pseudo-object))))
  2020. (should
  2021. (equal "A\n\nB\n"
  2022. (org-element-interpret-data
  2023. '(center nil
  2024. (pseudo-element (:post-blank 1) (paragraph nil "A"))
  2025. (paragraph nil "B"))))))
  2026. (ert-deftest test-org-element/center-block-interpreter ()
  2027. "Test center block interpreter."
  2028. (should
  2029. (equal (org-test-parse-and-interpret "#+BEGIN_CENTER\nTest\n#+END_CENTER")
  2030. "#+BEGIN_CENTER\nTest\n#+END_CENTER\n")))
  2031. (ert-deftest test-org-element/drawer-interpreter ()
  2032. "Test drawer interpreter."
  2033. (should
  2034. (equal (org-test-parse-and-interpret ":TEST:\nTest\n:END:")
  2035. ":TEST:\nTest\n:END:\n")))
  2036. (ert-deftest test-org-element/dynamic-block-interpreter ()
  2037. "Test dynamic block interpreter."
  2038. (should
  2039. (equal (org-test-parse-and-interpret
  2040. "#+BEGIN: myblock :parameter value1\nTest\n#+END:")
  2041. "#+BEGIN: myblock :parameter value1\nTest\n#+END:\n")))
  2042. (ert-deftest test-org-element/footnote-definition-interpreter ()
  2043. "Test footnote definition interpreter."
  2044. (should (equal (org-test-parse-and-interpret "[fn:1] Test") "[fn:1] Test\n")))
  2045. (ert-deftest test-org-element/headline-interpreter ()
  2046. "Test headline and section interpreters."
  2047. ;; 1. Standard test.
  2048. (should (equal (org-test-parse-and-interpret "* Headline") "* Headline\n"))
  2049. ;; 2. With TODO keywords.
  2050. (should
  2051. (equal (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  2052. (org-test-parse-and-interpret "* TODO Headline"))
  2053. "* TODO Headline\n"))
  2054. ;; 3. With tags...
  2055. ;;
  2056. ;; 3.1. ... and a positive `org-tags-column' value.
  2057. (should
  2058. (equal (let ((org-tags-column 20))
  2059. (org-test-parse-and-interpret "* Headline :tag:"))
  2060. "* Headline :tag:\n"))
  2061. ;; 3.2. ... and a negative `org-tags-column' value.
  2062. (should
  2063. (equal (let ((org-tags-column -20))
  2064. (org-test-parse-and-interpret "* Headline :tag:"))
  2065. "* Headline :tag:\n"))
  2066. ;; 3.3. ... and a null `org-tags-column' value.
  2067. (should
  2068. (equal (let ((org-tags-column 0))
  2069. (org-test-parse-and-interpret "* Headline :tag:"))
  2070. "* Headline :tag:\n"))
  2071. ;; 4. With priority cookie.
  2072. (should
  2073. (equal (org-test-parse-and-interpret "* [#B] Headline")
  2074. "* [#B] Headline\n"))
  2075. ;; 5. With comment keyword.
  2076. (should
  2077. (equal (let ((org-comment-string "COMMENT"))
  2078. (org-test-parse-and-interpret "* COMMENT Headline"))
  2079. "* COMMENT Headline\n"))
  2080. ;; 6. Keep same number of blank lines before body.
  2081. (should
  2082. (equal (org-test-parse-and-interpret
  2083. "* Headline\n\n\nText after two blank lines.")
  2084. "* Headline\n\n\nText after two blank lines.\n"))
  2085. ;; 8. Preserve `org-odd-levels-only' state.
  2086. (should
  2087. (equal "* H\n*** H2\n"
  2088. (let ((org-odd-levels-only t))
  2089. (org-test-parse-and-interpret "* H\n*** H2")))))
  2090. (ert-deftest test-org-element/inlinetask-interpreter ()
  2091. "Test inlinetask interpretation."
  2092. (when (featurep 'org-inlinetask)
  2093. (let ((org-inlinetask-min-level 15))
  2094. ;; 1. Regular inlinetask.
  2095. (should (equal (org-test-parse-and-interpret
  2096. "*************** Task\nTest\n*************** END")
  2097. "*************** Task\nTest\n*************** END\n"))
  2098. ;; 2. Degenerate inlinetask.
  2099. (should (equal (org-test-parse-and-interpret "*************** Task")
  2100. "*************** Task\n"))
  2101. ;; 3. Prefer degenerate form when there are no contents.
  2102. (should (equal (org-test-parse-and-interpret
  2103. "*************** Task\n*************** END")
  2104. "*************** Task\n"))
  2105. ;; 4. With TODO keywords.
  2106. (should
  2107. (equal (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  2108. (org-test-parse-and-interpret "*************** TODO Task"))
  2109. "*************** TODO Task\n"))
  2110. ;; 5. With tags...
  2111. ;;
  2112. ;; 5.1. ... and a positive `org-tags-column' value.
  2113. (should
  2114. (equal (let ((org-tags-column 30))
  2115. (org-test-parse-and-interpret "*************** Task :tag:"))
  2116. "*************** Task :tag:\n"))
  2117. ;; 5.2. ... and a negative `org-tags-column' value.
  2118. (should
  2119. (equal (let ((org-tags-column -30))
  2120. (org-test-parse-and-interpret "*************** Task :tag:"))
  2121. "*************** Task :tag:\n"))
  2122. ;; 5.3. ... and a null `org-tags-column' value.
  2123. (should
  2124. (equal (let ((org-tags-column 0))
  2125. (org-test-parse-and-interpret "*************** Task :tag:"))
  2126. "*************** Task :tag:\n"))
  2127. ;; 6. With priority cookie.
  2128. (should
  2129. (equal (org-test-parse-and-interpret "*************** [#B] Task")
  2130. "*************** [#B] Task\n")))))
  2131. (ert-deftest test-org-element/plain-list-interpreter ()
  2132. "Test plain-list and item interpreters."
  2133. (let ((org-list-two-spaces-after-bullet-regexp nil))
  2134. ;; Unordered list.
  2135. (should (equal (org-test-parse-and-interpret "- item 1") "- item 1\n"))
  2136. ;; Description list.
  2137. (should
  2138. (equal (org-test-parse-and-interpret "- tag :: desc") "- tag :: desc\n"))
  2139. ;; Ordered list.
  2140. (should
  2141. (equal (let ((org-plain-list-ordered-item-terminator t))
  2142. (org-test-parse-and-interpret "1. Item"))
  2143. "1. Item\n"))
  2144. (should
  2145. (equal (let ((org-plain-list-ordered-item-terminator ?\)))
  2146. (org-test-parse-and-interpret "1) Item"))
  2147. "1) Item\n"))
  2148. ;; Ordered list with counter.
  2149. (should
  2150. (equal (let ((org-plain-list-ordered-item-terminator t))
  2151. (org-test-parse-and-interpret "1. [@5] Item"))
  2152. "5. [@5] Item\n"))
  2153. ;; List with check-boxes.
  2154. (should
  2155. (equal (org-test-parse-and-interpret
  2156. "- [-] Item 1\n - [X] Item 2\n - [ ] Item 3")
  2157. "- [-] Item 1\n - [X] Item 2\n - [ ] Item 3\n"))
  2158. ;; Item not starting with a paragraph.
  2159. (should
  2160. (equal (org-test-parse-and-interpret "-\n | a | b |")
  2161. "- \n | a | b |\n"))
  2162. ;; Special case: correctly handle "*" bullets.
  2163. (should (org-test-parse-and-interpret " * item"))
  2164. ;; Special case: correctly handle empty items.
  2165. (should (org-test-parse-and-interpret "-"))))
  2166. (ert-deftest test-org-element/quote-block-interpreter ()
  2167. "Test quote block interpreter."
  2168. (should (equal (org-test-parse-and-interpret
  2169. "#+BEGIN_QUOTE\nTest\n#+END_QUOTE")
  2170. "#+BEGIN_QUOTE\nTest\n#+END_QUOTE\n")))
  2171. (ert-deftest test-org-element/special-block-interpreter ()
  2172. "Test special block interpreter."
  2173. (should (equal (org-test-parse-and-interpret
  2174. "#+BEGIN_SPECIAL\nTest\n#+END_SPECIAL")
  2175. "#+BEGIN_SPECIAL\nTest\n#+END_SPECIAL\n")))
  2176. (ert-deftest test-org-element/babel-call-interpreter ()
  2177. "Test babel call interpreter."
  2178. ;; 1. Without argument.
  2179. (should (equal (org-test-parse-and-interpret "#+CALL: test()")
  2180. "#+CALL: test()\n"))
  2181. ;; 2. With argument.
  2182. (should (equal (org-test-parse-and-interpret "#+CALL: test(x=2)")
  2183. "#+CALL: test(x=2)\n"))
  2184. ;; 3. With header arguments.
  2185. (should (equal (org-test-parse-and-interpret
  2186. "#+CALL: test[:results output]()[:results html]")
  2187. "#+CALL: test[:results output]()[:results html]\n")))
  2188. (ert-deftest test-org-element/clock-interpreter ()
  2189. "Test clock interpreter."
  2190. ;; Running clock.
  2191. (should
  2192. (string-match
  2193. "CLOCK: \\[2012-01-01 .* 00:01\\]"
  2194. (org-test-parse-and-interpret "CLOCK: [2012-01-01 sun. 00:01]")))
  2195. ;; Closed clock.
  2196. (should
  2197. (string-match
  2198. "CLOCK: \\[2012-01-01 .* 00:01\\]--\\[2012-01-01 .* 00:02\\] => 0:01"
  2199. (org-test-parse-and-interpret "
  2200. CLOCK: [2012-01-01 sun. 00:01]--[2012-01-01 sun. 00:02] => 0:01"))))
  2201. (ert-deftest test-org-element/comment-interpreter ()
  2202. "Test comment interpreter."
  2203. ;; Regular comment.
  2204. (should (equal (org-test-parse-and-interpret "# Comment") "# Comment\n"))
  2205. ;; Inline comment.
  2206. (should (equal (org-test-parse-and-interpret " # Comment")
  2207. "# Comment\n"))
  2208. ;; Preserve indentation.
  2209. (should (equal (org-test-parse-and-interpret " # No blank\n# One blank")
  2210. "# No blank\n# One blank\n")))
  2211. (ert-deftest test-org-element/comment-block-interpreter ()
  2212. "Test comment block interpreter."
  2213. (should (equal (org-test-parse-and-interpret
  2214. "#+BEGIN_COMMENT\nTest\n#+END_COMMENT")
  2215. "#+BEGIN_COMMENT\nTest\n#+END_COMMENT\n"))
  2216. ;; Accept missing final newline in value.
  2217. (should
  2218. (equal
  2219. "#+BEGIN_COMMENT\nTest\n#+END_COMMENT\n"
  2220. (org-element-interpret-data '(comment-block (:value "Test"))))))
  2221. (ert-deftest test-org-element/diary-sexp ()
  2222. "Test diary-sexp interpreter."
  2223. (should
  2224. (equal
  2225. (org-test-parse-and-interpret
  2226. "%%(org-anniversary 1956 5 14)(2) Arthur Dent is %d years old")
  2227. "%%(org-anniversary 1956 5 14)(2) Arthur Dent is %d years old\n")))
  2228. (ert-deftest test-org-element/example-block-interpreter ()
  2229. "Test example block interpreter."
  2230. ;; Without switches.
  2231. (should (equal (org-test-parse-and-interpret
  2232. "#+BEGIN_EXAMPLE\nTest\n#+END_EXAMPLE")
  2233. "#+BEGIN_EXAMPLE\nTest\n#+END_EXAMPLE\n"))
  2234. ;; With switches.
  2235. (should
  2236. (equal (org-test-parse-and-interpret
  2237. "#+BEGIN_EXAMPLE -n -k\n(+ 1 1)\n#+END_EXAMPLE")
  2238. "#+BEGIN_EXAMPLE -n -k\n(+ 1 1)\n#+END_EXAMPLE\n"))
  2239. ;; Preserve code escaping.
  2240. (should
  2241. (equal (org-test-parse-and-interpret
  2242. "#+BEGIN_EXAMPLE\n,* Headline\n ,#+keyword\nText #+END_EXAMPLE")
  2243. "#+BEGIN_EXAMPLE\n,* Headline\n ,#+keyword\nText #+END_EXAMPLE\n"))
  2244. ;; Accept missing final newline in value.
  2245. (should
  2246. (equal
  2247. "#+BEGIN_EXAMPLE\nTest\n#+END_EXAMPLE\n"
  2248. (org-element-interpret-data '(example-block (:value "Test"))))))
  2249. (ert-deftest test-org-element/fixed-width-interpreter ()
  2250. "Test fixed width interpreter."
  2251. ;; Standard test.
  2252. (should (equal (org-test-parse-and-interpret ": Test") ": Test\n"))
  2253. ;; Preserve indentation.
  2254. (should (equal (org-test-parse-and-interpret ": 2 blanks\n: 1 blank")
  2255. ": 2 blanks\n: 1 blank\n"))
  2256. ;; Remove last newline character
  2257. (should
  2258. (equal (org-element-fixed-width-interpreter
  2259. '(fixed-width (:value "Test\n")) nil)
  2260. ": Test"))
  2261. (should
  2262. (equal (org-element-fixed-width-interpreter
  2263. '(fixed-width (:value "Test")) nil)
  2264. ": Test"))
  2265. ;; Handle empty string.
  2266. (should
  2267. (equal (org-element-fixed-width-interpreter
  2268. '(fixed-width (:value "")) nil)
  2269. ""))
  2270. ;; Handle nil value.
  2271. (should-not
  2272. (org-element-fixed-width-interpreter
  2273. '(fixed-width (:value nil)) nil)))
  2274. (ert-deftest test-org-element/horizontal-rule-interpreter ()
  2275. "Test horizontal rule interpreter."
  2276. (should (equal (org-test-parse-and-interpret "-------") "-----\n")))
  2277. (ert-deftest test-org-element/keyword-interpreter ()
  2278. "Test keyword interpreter."
  2279. (should (equal (org-test-parse-and-interpret "#+KEYWORD: value")
  2280. "#+KEYWORD: value\n")))
  2281. (ert-deftest test-org-element/latex-environment-interpreter ()
  2282. "Test latex environment interpreter."
  2283. (should (equal (org-test-parse-and-interpret
  2284. "\\begin{equation}\n1+1=2\n\\end{equation}")
  2285. "\\begin{equation}\n1+1=2\n\\end{equation}\n"))
  2286. (should (equal (org-test-parse-and-interpret
  2287. "\\begin{theorem}[me]\n1+1=2\n\\end{theorem}")
  2288. "\\begin{theorem}[me]\n1+1=2\n\\end{theorem}\n")))
  2289. (ert-deftest test-org-element/planning-interpreter ()
  2290. "Test planning interpreter."
  2291. (should
  2292. (string-match
  2293. "\\* Headline
  2294. DEADLINE: <2012-03-29 .*?> SCHEDULED: <2012-03-29 .*?> CLOSED: \\[2012-03-29 .*?\\]"
  2295. (org-test-parse-and-interpret
  2296. "* Headline
  2297. DEADLINE: <2012-03-29 thu.> SCHEDULED: <2012-03-29 thu.> CLOSED: [2012-03-29 thu.]"))))
  2298. (ert-deftest test-org-element/property-drawer-interpreter ()
  2299. "Test property drawer interpreter."
  2300. (should (equal (let ((org-property-format "%-10s %s"))
  2301. (org-test-parse-and-interpret
  2302. ":PROPERTIES:\n:prop: value\n:END:"))
  2303. ":PROPERTIES:\n:prop: value\n:END:\n")))
  2304. (ert-deftest test-org-element/src-block-interpreter ()
  2305. "Test src block interpreter."
  2306. ;; With arguments.
  2307. (should
  2308. (equal (let ((org-edit-src-content-indentation 2))
  2309. (org-test-parse-and-interpret
  2310. "#+BEGIN_SRC emacs-lisp :results silent\n(+ 1 1)\n#+END_SRC"))
  2311. "#+BEGIN_SRC emacs-lisp :results silent\n (+ 1 1)\n#+END_SRC\n"))
  2312. ;; With switches.
  2313. (should
  2314. (equal (let ((org-edit-src-content-indentation 2))
  2315. (org-test-parse-and-interpret
  2316. "#+BEGIN_SRC emacs-lisp -n -k\n(+ 1 1)\n#+END_SRC"))
  2317. "#+BEGIN_SRC emacs-lisp -n -k\n (+ 1 1)\n#+END_SRC\n"))
  2318. ;; Preserve code escaping.
  2319. (should
  2320. (equal (let ((org-edit-src-content-indentation 2))
  2321. (org-test-parse-and-interpret
  2322. "#+BEGIN_SRC org\n,* Headline\n ,#+keyword\nText #+END_SRC"))
  2323. "#+BEGIN_SRC org\n,* Headline\n ,#+keyword\nText #+END_SRC\n"))
  2324. ;; Do not apply `org-edit-src-content-indentation' when preserving
  2325. ;; indentation.
  2326. (should
  2327. (equal (let ((org-edit-src-content-indentation 2)
  2328. (org-src-preserve-indentation t))
  2329. (org-test-parse-and-interpret
  2330. "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"))
  2331. "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC\n"))
  2332. (should
  2333. (equal (let ((org-edit-src-content-indentation 2)
  2334. (org-src-preserve-indentation nil))
  2335. (org-test-parse-and-interpret
  2336. "#+BEGIN_SRC emacs-lisp -i\n(+ 1 1)\n#+END_SRC"))
  2337. "#+BEGIN_SRC emacs-lisp -i\n(+ 1 1)\n#+END_SRC\n"))
  2338. ;; Accept missing final newline in value.
  2339. (should
  2340. (equal
  2341. "#+BEGIN_SRC emacs-lisp\n Test\n#+END_SRC\n"
  2342. (let ((org-edit-src-content-indentation 2))
  2343. (org-element-interpret-data
  2344. '(src-block (:language "emacs-lisp" :value "Test")))))))
  2345. (ert-deftest test-org-element/table-interpreter ()
  2346. "Test table, table-row and table-cell interpreters."
  2347. ;; 1. Simple table.
  2348. (should (equal (org-test-parse-and-interpret "| a | b |\n| c | d |")
  2349. "| a | b |\n| c | d |\n"))
  2350. ;; 2. With horizontal rules.
  2351. (should (equal (org-test-parse-and-interpret
  2352. "| a | b |\n|---+---|\n| c | d |")
  2353. "| a | b |\n|---+---|\n| c | d |\n"))
  2354. ;; 3. With meta-data.
  2355. (should (equal (org-test-parse-and-interpret "| / | < | > |\n| * | 1 | 2 |")
  2356. "| / | < | > |\n| * | 1 | 2 |\n"))
  2357. ;; 4. With a formula.
  2358. (should
  2359. (equal (org-test-parse-and-interpret
  2360. "| 2 |\n| 4 |\n| 3 |\n#+TBLFM: @3=vmean(@1..@2)")
  2361. "| 2 |\n| 4 |\n| 3 |\n#+TBLFM: @3=vmean(@1..@2)\n"))
  2362. ;; 5. With multiple formulas.
  2363. (should
  2364. (equal (org-test-parse-and-interpret
  2365. "| 2 |\n| 4 |\n| 3 |\n#+TBLFM: test1\n#+TBLFM: test2")
  2366. "| 2 |\n| 4 |\n| 3 |\n#+TBLFM: test1\n#+TBLFM: test2\n")))
  2367. (ert-deftest test-org-element/timestamp-interpreter ()
  2368. "Test timestamp interpreter."
  2369. ;; Active.
  2370. (should
  2371. (string-match "<2012-03-29 .* 16:40>"
  2372. (org-test-parse-and-interpret "<2012-03-29 thu. 16:40>")))
  2373. (should
  2374. (string-match "<2012-03-29 .* 16:40>"
  2375. (org-element-timestamp-interpreter
  2376. '(timestamp
  2377. (:type active :year-start 2012 :month-start 3 :day-start 29
  2378. :hour-start 16 :minute-start 40)) nil)))
  2379. ;; Inactive.
  2380. (should
  2381. (string-match "\\[2012-03-29 .* 16:40\\]"
  2382. (org-test-parse-and-interpret "[2012-03-29 thu. 16:40]")))
  2383. (should
  2384. (string-match
  2385. "\\[2012-03-29 .* 16:40\\]"
  2386. (org-element-timestamp-interpreter
  2387. '(timestamp
  2388. (:type inactive :year-start 2012 :month-start 3 :day-start 29
  2389. :hour-start 16 :minute-start 40)) nil)))
  2390. ;; Active range.
  2391. (should
  2392. (string-match "<2012-03-29 .* 16:40>--<2012-03-29 .* 16:41>"
  2393. (org-test-parse-and-interpret
  2394. "<2012-03-29 thu. 16:40>--<2012-03-29 thu. 16:41>")))
  2395. (should
  2396. (string-match
  2397. "<2012-03-29 .* 16:40>--<2012-03-29 .* 16:41>"
  2398. (org-element-timestamp-interpreter
  2399. '(timestamp
  2400. (:type active-range :year-start 2012 :month-start 3 :day-start 29
  2401. :hour-start 16 :minute-start 40 :year-end 2012 :month-end 3
  2402. :day-end 29 :hour-end 16 :minute-end 41)) nil)))
  2403. ;; Inactive range.
  2404. (should
  2405. (string-match "\\[2012-03-29 .* 16:40\\]--\\[2012-03-29 .* 16:41\\]"
  2406. (org-test-parse-and-interpret
  2407. "[2012-03-29 thu. 16:40]--[2012-03-29 thu. 16:41]")))
  2408. (should
  2409. (string-match
  2410. "\\[2012-03-29 .* 16:40\\]--\\[2012-03-29 .* 16:41\\]"
  2411. (org-element-timestamp-interpreter
  2412. '(timestamp
  2413. (:type inactive-range :year-start 2012 :month-start 3 :day-start 29
  2414. :hour-start 16 :minute-start 40 :year-end 2012 :month-end 3
  2415. :day-end 29 :hour-end 16 :minute-end 41)) nil)))
  2416. ;; Diary.
  2417. (should (equal (org-test-parse-and-interpret "<%%diary-float t 4 2>")
  2418. "<%%diary-float t 4 2>\n"))
  2419. ;; Timestamp with repeater interval, with delay, with both.
  2420. (should
  2421. (string-match "<2012-03-29 .* \\+1y>"
  2422. (org-test-parse-and-interpret "<2012-03-29 thu. +1y>")))
  2423. (should
  2424. (string-match
  2425. "<2012-03-29 .* \\+1y>"
  2426. (org-element-timestamp-interpreter
  2427. '(timestamp
  2428. (:type active :year-start 2012 :month-start 3 :day-start 29
  2429. :repeater-type cumulate :repeater-value 1 :repeater-unit year))
  2430. nil)))
  2431. (should
  2432. (string-match
  2433. "<2012-03-29 .* -1y>"
  2434. (org-element-timestamp-interpreter
  2435. '(timestamp
  2436. (:type active :year-start 2012 :month-start 3 :day-start 29
  2437. :warning-type all :warning-value 1 :warning-unit year))
  2438. nil)))
  2439. (should
  2440. (string-match
  2441. "<2012-03-29 .* \\+1y -1y>"
  2442. (org-element-timestamp-interpreter
  2443. '(timestamp
  2444. (:type active :year-start 2012 :month-start 3 :day-start 29
  2445. :warning-type all :warning-value 1 :warning-unit year
  2446. :repeater-type cumulate :repeater-value 1 :repeater-unit year))
  2447. nil)))
  2448. ;; Timestamp range with repeater interval
  2449. (should
  2450. (string-match "<2012-03-29 .* \\+1y>--<2012-03-30 .* \\+1y>"
  2451. (org-test-parse-and-interpret
  2452. "<2012-03-29 Thu +1y>--<2012-03-30 Thu +1y>")))
  2453. (should
  2454. (string-match
  2455. "<2012-03-29 .* \\+1y>--<2012-03-30 .* \\+1y>"
  2456. (org-element-timestamp-interpreter
  2457. '(timestamp
  2458. (:type active-range :year-start 2012 :month-start 3 :day-start 29
  2459. :year-end 2012 :month-end 3 :day-end 30 :repeater-type cumulate
  2460. :repeater-value 1 :repeater-unit year))
  2461. nil))))
  2462. (ert-deftest test-org-element/verse-block-interpreter ()
  2463. "Test verse block interpretation."
  2464. (should
  2465. (equal (org-test-parse-and-interpret "#+BEGIN_VERSE\nTest\n#+END_VERSE")
  2466. "#+BEGIN_VERSE\nTest\n#+END_VERSE\n")))
  2467. (ert-deftest test-org-element/bold-interpreter ()
  2468. "Test bold interpreter."
  2469. (should (equal (org-test-parse-and-interpret "*text*") "*text*\n")))
  2470. (ert-deftest test-org-element/code-interpreter ()
  2471. "Test code interpreter."
  2472. (should (equal (org-test-parse-and-interpret "~text~") "~text~\n")))
  2473. (ert-deftest test-org-element/entity-interpreter ()
  2474. "Test entity interpreter."
  2475. ;; 1. Without brackets.
  2476. (should
  2477. (equal (org-test-parse-and-interpret "\\alpha text") "\\alpha text\n"))
  2478. ;; 2. With brackets.
  2479. (should
  2480. (equal (org-test-parse-and-interpret "\\alpha{}text") "\\alpha{}text\n")))
  2481. (ert-deftest test-org-element/export-snippet-interpreter ()
  2482. "Test export snippet interpreter."
  2483. (should (equal (org-test-parse-and-interpret "@@back-end:contents@@")
  2484. "@@back-end:contents@@\n")))
  2485. (ert-deftest test-org-element/footnote-reference-interpreter ()
  2486. "Test footnote reference interpreter."
  2487. ;; 1. Regular reference.
  2488. (should (equal (org-test-parse-and-interpret "Text[fn:1]") "Text[fn:1]\n"))
  2489. ;; 2. Normalized reference.
  2490. (should (equal (org-test-parse-and-interpret "Text[1]") "Text[1]\n"))
  2491. ;; 3. Named reference.
  2492. (should (equal (org-test-parse-and-interpret "Text[fn:label]")
  2493. "Text[fn:label]\n"))
  2494. ;; 4. Inline reference.
  2495. (should (equal (org-test-parse-and-interpret "Text[fn:label:def]")
  2496. "Text[fn:label:def]\n"))
  2497. ;; 5. Anonymous reference.
  2498. (should (equal (org-test-parse-and-interpret "Text[fn::def]")
  2499. "Text[fn::def]\n")))
  2500. (ert-deftest test-org-element/inline-babel-call-interpreter ()
  2501. "Test inline babel call interpreter."
  2502. ;; 1. Without arguments.
  2503. (should (equal (org-test-parse-and-interpret "call_test()") "call_test()\n"))
  2504. ;; 2. With arguments.
  2505. (should (equal (org-test-parse-and-interpret "call_test(x=2)")
  2506. "call_test(x=2)\n"))
  2507. ;; 3. With header arguments.
  2508. (should (equal (org-test-parse-and-interpret
  2509. "call_test[:results output]()[:results html]")
  2510. "call_test[:results output]()[:results html]\n")))
  2511. (ert-deftest test-org-element/inline-src-block-interpreter ()
  2512. "Test inline src block interpreter."
  2513. ;; 1. Without header argument.
  2514. (should (equal (org-test-parse-and-interpret "src_emacs-lisp{(+ 1 1)}")
  2515. "src_emacs-lisp{(+ 1 1)}\n"))
  2516. ;; 2. With header arguments.
  2517. (should (equal (org-test-parse-and-interpret
  2518. "src_emacs-lisp[:results silent]{(+ 1 1)}")
  2519. "src_emacs-lisp[:results silent]{(+ 1 1)}\n")))
  2520. (ert-deftest test-org-element/italic-interpreter ()
  2521. "Test italic interpreter."
  2522. (should (equal (org-test-parse-and-interpret "/text/") "/text/\n")))
  2523. (ert-deftest test-org-element/latex-fragment-interpreter ()
  2524. "Test latex fragment interpreter."
  2525. (should (equal (org-test-parse-and-interpret "\\command{}") "\\command{}\n"))
  2526. (should (equal (org-test-parse-and-interpret "$x$") "$x$\n"))
  2527. (should (equal (org-test-parse-and-interpret "$x+y$") "$x+y$\n"))
  2528. (should (equal (org-test-parse-and-interpret "$$x+y$$") "$$x+y$$\n"))
  2529. (should (equal (org-test-parse-and-interpret "\\(x+y\\)") "\\(x+y\\)\n"))
  2530. (should (equal (org-test-parse-and-interpret "\\[x+y\\]") "\\[x+y\\]\n")))
  2531. (ert-deftest test-org-element/line-break-interpreter ()
  2532. "Test line break interpreter."
  2533. (should (equal (org-test-parse-and-interpret "First line \\\\ \nSecond line")
  2534. "First line \\\\\nSecond line\n")))
  2535. (ert-deftest test-org-element/link-interpreter ()
  2536. "Test link interpreter."
  2537. ;; 1. Links targeted from a radio target.
  2538. (should (equal (let ((org-target-link-regexp "radio-target"))
  2539. (org-test-parse-and-interpret "a radio-target"))
  2540. "a radio-target\n"))
  2541. ;; 2. Regular links.
  2542. ;;
  2543. ;; 2.1. Without description.
  2544. (should (equal (org-test-parse-and-interpret "[[http://orgmode.org]]")
  2545. "[[http://orgmode.org]]\n"))
  2546. ;; 2.2. With a description.
  2547. (should (equal (org-test-parse-and-interpret
  2548. "[[http://orgmode.org][Org mode]]")
  2549. "[[http://orgmode.org][Org mode]]\n"))
  2550. ;; 2.3. Id links.
  2551. (should (equal (org-test-parse-and-interpret "[[id:aaaa]]") "[[id:aaaa]]\n"))
  2552. ;; 2.4. Custom-id links.
  2553. (should (equal (org-test-parse-and-interpret "[[#id]]") "[[#id]]\n"))
  2554. ;; 2.5 Code-ref links.
  2555. (should (equal (org-test-parse-and-interpret "[[(ref)]]") "[[(ref)]]\n"))
  2556. ;; 3. Normalize plain links.
  2557. (should (equal (org-test-parse-and-interpret "http://orgmode.org")
  2558. "[[http://orgmode.org]]\n"))
  2559. ;; 4. Normalize angular links.
  2560. (should (equal (org-test-parse-and-interpret "<http://orgmode.org>")
  2561. "[[http://orgmode.org]]\n")))
  2562. (ert-deftest test-org-element/macro-interpreter ()
  2563. "Test macro interpreter."
  2564. ;; 1. Without argument.
  2565. (should (equal (org-test-parse-and-interpret "{{{test}}}") "{{{test}}}\n"))
  2566. ;; 2. With arguments.
  2567. (should (equal (org-test-parse-and-interpret "{{{test(arg1,arg2)}}}")
  2568. "{{{test(arg1,arg2)}}}\n")))
  2569. (ert-deftest test-org-element/radio-target-interpreter ()
  2570. "Test radio target interpreter."
  2571. (should (equal (org-test-parse-and-interpret "<<<some text>>>")
  2572. "<<<some text>>>\n")))
  2573. (ert-deftest test-org-element/statistics-cookie-interpreter ()
  2574. "Test statistics cookie interpreter."
  2575. ;; 1. Without percent
  2576. (should (equal (org-test-parse-and-interpret "[0/1]") "[0/1]\n"))
  2577. ;; 2. With percent.
  2578. (should (equal (org-test-parse-and-interpret "[66%]") "[66%]\n")))
  2579. (ert-deftest test-org-element/strike-through-interpreter ()
  2580. "Test strike through interpreter."
  2581. (should (equal (org-test-parse-and-interpret "+target+") "+target+\n")))
  2582. (ert-deftest test-org-element/subscript-interpreter ()
  2583. "Test subscript interpreter."
  2584. ;; 1. Without brackets.
  2585. (should (equal (org-test-parse-and-interpret "a_b") "a_b\n"))
  2586. ;; 2. With brackets.
  2587. (should (equal (org-test-parse-and-interpret "a_{b}") "a_{b}\n")))
  2588. (ert-deftest test-org-element/superscript-interpreter ()
  2589. "Test superscript interpreter."
  2590. ;; 1. Without brackets.
  2591. (should (equal (org-test-parse-and-interpret "a^b") "a^b\n"))
  2592. ;; 2. With brackets.
  2593. (should (equal (org-test-parse-and-interpret "a^{b}") "a^{b}\n")))
  2594. (ert-deftest test-org-element/target-interpreter ()
  2595. "Test target interpreter."
  2596. (should (equal (org-test-parse-and-interpret "<<target>>") "<<target>>\n")))
  2597. (ert-deftest test-org-element/underline-interpreter ()
  2598. "Test underline interpreter."
  2599. (should (equal (org-test-parse-and-interpret "_text_") "_text_\n")))
  2600. (ert-deftest test-org-element/verbatim-interpreter ()
  2601. "Test verbatim interpreter."
  2602. (should (equal (org-test-parse-and-interpret "=text=") "=text=\n")))
  2603. ;;; Test Granularity
  2604. (ert-deftest test-org-element/granularity ()
  2605. "Test granularity impact on buffer parsing."
  2606. (org-test-with-temp-text "
  2607. * Head 1
  2608. ** Head 2
  2609. #+BEGIN_CENTER
  2610. Centered paragraph.
  2611. #+END_CENTER
  2612. Paragraph \\alpha."
  2613. ;; 1.1. Granularity set to `headline' should parse every headline
  2614. ;; in buffer, and only them.
  2615. (let ((tree (org-element-parse-buffer 'headline)))
  2616. (should (= 2 (length (org-element-map tree 'headline 'identity))))
  2617. (should-not (org-element-map tree 'paragraph 'identity)))
  2618. ;; 1.2. Granularity set to `greater-element' should not enter
  2619. ;; greater elements excepted headlines and sections.
  2620. (let ((tree (org-element-parse-buffer 'greater-element)))
  2621. (should (= 1 (length (org-element-map tree 'center-block 'identity))))
  2622. (should (= 1 (length (org-element-map tree 'paragraph 'identity))))
  2623. (should-not (org-element-map tree 'entity 'identity)))
  2624. ;; 1.3. Granularity set to `element' should enter every
  2625. ;; greater-element.
  2626. (let ((tree (org-element-parse-buffer 'element)))
  2627. (should (= 2 (length (org-element-map tree 'paragraph 'identity))))
  2628. (should-not (org-element-map tree 'entity 'identity)))
  2629. ;; 1.4. Granularity set to `object' can see everything.
  2630. (let ((tree (org-element-parse-buffer 'object)))
  2631. (should (= 1 (length (org-element-map tree 'entity 'identity)))))))
  2632. (ert-deftest test-org-element/secondary-string-parsing ()
  2633. "Test if granularity correctly toggles secondary strings parsing."
  2634. ;; With a granularity bigger than `object', no secondary string
  2635. ;; should be parsed.
  2636. (should
  2637. (stringp
  2638. (org-test-with-temp-text "* Headline"
  2639. (let ((headline
  2640. (org-element-map (org-element-parse-buffer 'headline) 'headline
  2641. #'identity nil 'first-match)))
  2642. (org-element-property :title headline)))))
  2643. (should
  2644. (stringp
  2645. (org-test-with-temp-text "* Headline\n- tag :: item"
  2646. (let ((item (org-element-map (org-element-parse-buffer 'element) 'item
  2647. #'identity nil 'first-match)))
  2648. (org-element-property :tag item)))))
  2649. (when (featurep 'org-inlinetask)
  2650. (should
  2651. (stringp
  2652. (let ((org-inlinetask-min-level 15))
  2653. (org-test-with-temp-text "*************** Inlinetask"
  2654. (let ((inlinetask (org-element-map (org-element-parse-buffer 'element)
  2655. 'inlinetask
  2656. #'identity nil 'first-match)))
  2657. (org-element-property :title inlinetask)))))))
  2658. ;; With a default granularity, secondary strings should be parsed.
  2659. (should
  2660. (listp
  2661. (org-test-with-temp-text "* Headline"
  2662. (let ((headline
  2663. (org-element-map (org-element-parse-buffer) 'headline
  2664. #'identity nil 'first-match)))
  2665. (org-element-property :title headline)))))
  2666. ;; `org-element-at-point' should never parse a secondary string.
  2667. (should-not
  2668. (listp
  2669. (org-test-with-temp-text "* Headline"
  2670. (org-element-property :title (org-element-at-point)))))
  2671. ;; Preserve current local variables when parsing a secondary string.
  2672. (should
  2673. (let ((org-entities nil)
  2674. (org-entities-user nil))
  2675. (org-test-with-temp-text "
  2676. #+CAPTION: \\foo
  2677. Text
  2678. # Local Variables:
  2679. # org-entities-user: ((\"foo\"))
  2680. # End:"
  2681. (let ((safe-local-variable-values '((org-entities-user . (("foo"))))))
  2682. (hack-local-variables))
  2683. (org-element-map (org-element-parse-buffer) 'entity
  2684. #'identity nil nil nil t)))))
  2685. ;;; Test Visible Only Parsing
  2686. (ert-deftest test-org-element/parse-buffer-visible ()
  2687. "Test `org-element-parse-buffer' with visible only argument."
  2688. (should
  2689. (equal '("H1" "H3" "H5")
  2690. (org-test-with-temp-text
  2691. "* H1\n** H2\n** H3 :visible:\n** H4\n** H5 :visible:"
  2692. (org-occur ":visible:")
  2693. (org-element-map (org-element-parse-buffer nil t) 'headline
  2694. (lambda (hl) (org-element-property :raw-value hl)))))))
  2695. ;;; Test `:parent' Property
  2696. (ert-deftest test-org-element/parent-property ()
  2697. "Test `:parent' property."
  2698. ;; Elements.
  2699. (org-test-with-temp-text "#+BEGIN_CENTER\nText\n#+END_CENTER"
  2700. (let* ((tree (org-element-parse-buffer))
  2701. (parent (org-element-property
  2702. :parent
  2703. (org-element-map tree 'paragraph 'identity nil t))))
  2704. (should parent)
  2705. (should (eq (org-element-map tree 'center-block 'identity nil t)
  2706. parent))))
  2707. ;; Objects.
  2708. (org-test-with-temp-text "a_{/b/}"
  2709. (let* ((tree (org-element-parse-buffer))
  2710. (parent (org-element-property
  2711. :parent
  2712. (org-element-map tree 'italic 'identity nil t))))
  2713. (should parent)
  2714. (should (eq parent
  2715. (org-element-map tree 'subscript 'identity nil t)))))
  2716. ;; Secondary strings
  2717. (org-test-with-temp-text "* /italic/"
  2718. (let* ((tree (org-element-parse-buffer))
  2719. (parent (org-element-property
  2720. :parent (org-element-map tree 'italic 'identity nil t))))
  2721. (should parent)
  2722. (should (eq parent
  2723. (org-element-map tree 'headline 'identity nil t))))))
  2724. ;;; Test Normalize Contents
  2725. (ert-deftest test-org-element/normalize-contents ()
  2726. "Test `org-element-normalize-contents' specifications."
  2727. ;; Remove maximum common indentation from element's contents.
  2728. (should
  2729. (equal
  2730. (org-element-normalize-contents
  2731. '(paragraph nil " Two spaces\n Three spaces"))
  2732. '(paragraph nil "Two spaces\n Three spaces")))
  2733. ;; Ignore objects within contents when computing maximum common
  2734. ;; indentation.
  2735. (should
  2736. (equal
  2737. (org-element-normalize-contents
  2738. '(paragraph nil " One " (emphasis nil "space") "\n Two spaces"))
  2739. '(paragraph nil "One " (emphasis nil "space") "\n Two spaces")))
  2740. ;; Ignore blank lines.
  2741. (should
  2742. (equal
  2743. (org-element-normalize-contents
  2744. '(paragraph nil " Two spaces\n\n \n Two spaces"))
  2745. '(paragraph nil "Two spaces\n\n \nTwo spaces")))
  2746. (should
  2747. (equal
  2748. '(paragraph nil " Two spaces\n" (verbatim nil "V") "\n Two spaces")
  2749. (org-element-normalize-contents
  2750. '(paragraph nil " Two spaces\n " (verbatim nil "V") "\n Two spaces"))))
  2751. (should
  2752. (equal
  2753. '(verse-block nil "line 1\n\nline 2")
  2754. (org-element-normalize-contents
  2755. '(verse-block nil " line 1\n\n line 2"))))
  2756. ;; Recursively enter objects in order to compute common indentation.
  2757. (should
  2758. (equal
  2759. (org-element-normalize-contents
  2760. '(paragraph nil " Two spaces " (bold nil " and\n One space")))
  2761. '(paragraph nil " Two spaces " (bold nil " and\nOne space"))))
  2762. ;; When optional argument is provided, ignore first line
  2763. ;; indentation.
  2764. (should
  2765. (equal
  2766. (org-element-normalize-contents
  2767. '(paragraph nil "No space\n Two spaces\n Three spaces") t)
  2768. '(paragraph nil "No space\nTwo spaces\n Three spaces"))))
  2769. ;;; Test Navigation Tools.
  2770. (ert-deftest test-org-element/at-point ()
  2771. "Test `org-element-at-point' specifications."
  2772. ;; Return closest element containing point.
  2773. (should
  2774. (eq 'paragraph
  2775. (org-test-with-temp-text "#+BEGIN_CENTER\nA\n#+END_CENTER"
  2776. (progn (search-forward "A")
  2777. (org-element-type (org-element-at-point))))))
  2778. ;; Correctly set `:parent' property.
  2779. (should
  2780. (eq 'center-block
  2781. (org-test-with-temp-text "#+BEGIN_CENTER\nA\n#+END_CENTER"
  2782. (progn (search-forward "A")
  2783. (org-element-type
  2784. (org-element-property :parent (org-element-at-point)))))))
  2785. ;; Special case: at a blank line just below a headline, return that
  2786. ;; headline.
  2787. (should
  2788. (equal "H1" (org-test-with-temp-text "* H1\n \n* H2\n"
  2789. (forward-line)
  2790. (org-element-property :title (org-element-at-point)))))
  2791. ;; Special case: at the very beginning of a table, return `table'
  2792. ;; object instead of `table-row'.
  2793. (should
  2794. (eq 'table
  2795. (org-test-with-temp-text "| a | b |"
  2796. (org-element-type (org-element-at-point)))))
  2797. ;; Special case: at the very beginning of a list or sub-list, return
  2798. ;; `plain-list' object instead of `item'.
  2799. (should
  2800. (eq 'plain-list
  2801. (org-test-with-temp-text "- item"
  2802. (org-element-type (org-element-at-point)))))
  2803. ;; Special case: at the closing line of a greater element, be sure
  2804. ;; to return it instead of the last element in its contents.
  2805. (should
  2806. (eq 'center-block
  2807. (org-test-with-temp-text "#+BEGIN_CENTER\nParagraph\n#+END_CENTER"
  2808. (progn (forward-line 2)
  2809. (org-element-type (org-element-at-point))))))
  2810. ;; Special case: at a blank line between two items, be sure to
  2811. ;; return item above instead of the last element of its contents.
  2812. (should
  2813. (eq 'item
  2814. (org-test-with-temp-text "- Para1\n\n- Para2"
  2815. (progn (forward-line)
  2816. (org-element-type
  2817. (let ((org-list-empty-line-terminates-plain-lists nil))
  2818. (org-element-at-point)))))))
  2819. ;; Special case: at the last blank line in a plain list, return it
  2820. ;; instead of the last item.
  2821. (should
  2822. (eq 'plain-list
  2823. (org-test-with-temp-text "- Para1\n- Para2\n\nPara3"
  2824. (progn (forward-line 2)
  2825. (org-element-type (org-element-at-point))))))
  2826. ;; Special case: when a list ends at the end of buffer and there's
  2827. ;; no final newline, return last element in last item.
  2828. (should
  2829. (eq 'paragraph
  2830. (org-test-with-temp-text "- a"
  2831. (end-of-line)
  2832. (org-element-type (org-element-at-point)))))
  2833. ;; Parse a list within a block itself contained in a list.
  2834. (should
  2835. (eq 'plain-list
  2836. (org-test-with-temp-text
  2837. "- outer\n #+begin_center\n - inner\n #+end_center"
  2838. (search-forward "inner")
  2839. (beginning-of-line)
  2840. (org-element-type (org-element-at-point)))))
  2841. ;; Do not error at eob on an empty line.
  2842. (should
  2843. (org-test-with-temp-text "* H\n"
  2844. (forward-line)
  2845. (or (org-element-at-point) t))))
  2846. (ert-deftest test-org-element/context ()
  2847. "Test `org-element-context' specifications."
  2848. ;; Return closest object containing point.
  2849. (should
  2850. (eq 'underline
  2851. (org-test-with-temp-text "Some *text with _underline_ text*"
  2852. (progn (search-forward "under")
  2853. (org-element-type (org-element-context))))))
  2854. ;; Find objects in secondary strings.
  2855. (should
  2856. (eq 'underline
  2857. (org-test-with-temp-text "* Headline _with_ underlining"
  2858. (progn (search-forward "w")
  2859. (org-element-type (org-element-context))))))
  2860. ;; Find objects in objects.
  2861. (should
  2862. (eq 'macro
  2863. (org-test-with-temp-text "| a | {{{macro}}} |"
  2864. (progn (search-forward "{")
  2865. (org-element-type (org-element-context))))))
  2866. (should
  2867. (eq 'table-cell
  2868. (org-test-with-temp-text "| a | b {{{macro}}} |"
  2869. (progn (search-forward "b")
  2870. (org-element-type (org-element-context))))))
  2871. ;; Find objects in planning lines.
  2872. (should
  2873. (eq 'timestamp
  2874. (org-test-with-temp-text "* H\n SCHEDULED: <2012-03-29 thu.>"
  2875. (search-forward "2012")
  2876. (org-element-type (org-element-context)))))
  2877. (should-not
  2878. (eq 'timestamp
  2879. (org-test-with-temp-text "* H\n SCHEDULED: <2012-03-29 thu.>"
  2880. (search-forward "SCHEDULED")
  2881. (org-element-type (org-element-context)))))
  2882. ;; Find objects in document keywords.
  2883. (should
  2884. (eq 'macro
  2885. (org-test-with-temp-text "#+DATE: {{{macro}}}"
  2886. (progn (search-forward "{")
  2887. (org-element-type (org-element-context))))))
  2888. ;; Do not find objects in table rules.
  2889. (should
  2890. (eq 'table-row
  2891. (org-test-with-temp-text "| a | b |\n+---+---+\n| c | d |"
  2892. (forward-line)
  2893. (org-element-type (org-element-context)))))
  2894. ;; Find objects in parsed affiliated keywords.
  2895. (should
  2896. (eq 'macro
  2897. (org-test-with-temp-text "#+CAPTION: {{{macro}}}\n| a | b |."
  2898. (progn (search-forward "{")
  2899. (org-element-type (org-element-context))))))
  2900. (should
  2901. (eq 'bold
  2902. (org-test-with-temp-text "#+caption: *bold*\nParagraph"
  2903. (progn (search-forward "*")
  2904. (org-element-type (org-element-context))))))
  2905. ;; Find objects at the end of buffer.
  2906. (should
  2907. (eq 'bold
  2908. (org-test-with-temp-text "*bold*"
  2909. (goto-char (point-max))
  2910. (org-element-type (org-element-context)))))
  2911. ;; Correctly set `:parent' property.
  2912. (should
  2913. (eq 'paragraph
  2914. (org-test-with-temp-text "Some *bold* text"
  2915. (progn (search-forward "bold")
  2916. (org-element-type
  2917. (org-element-property :parent (org-element-context)))))))
  2918. ;; Between two objects, return the second one.
  2919. (should
  2920. (eq 'macro
  2921. (org-test-with-temp-text "<<target>>{{{test}}}"
  2922. (progn (search-forward "{")
  2923. (backward-char)
  2924. (org-element-type (org-element-context))))))
  2925. ;; Test optional argument.
  2926. (should
  2927. (eq 'underline
  2928. (org-test-with-temp-text "Some *text with _underline_ text*"
  2929. (progn
  2930. (search-forward "under")
  2931. (org-element-type (org-element-context (org-element-at-point)))))))
  2932. ;; Special case: bold object at the beginning of a headline.
  2933. (should
  2934. (eq 'bold
  2935. (org-test-with-temp-text "* *bold*"
  2936. (search-forward "bo")
  2937. (org-element-type (org-element-context)))))
  2938. ;; Special case: incomplete cell at the end of a table row.
  2939. (should
  2940. (eq 'table-cell
  2941. (org-test-with-temp-text "|a|b|c"
  2942. (goto-char (point-max))
  2943. (org-element-type (org-element-context)))))
  2944. ;; Special case: objects in inline footnotes.
  2945. (should
  2946. (eq 'link
  2947. (org-test-with-temp-text "[fn::[[<point>http://orgmode.org]]]"
  2948. (org-element-type (org-element-context))))))
  2949. ;;; Test Cache.
  2950. (ert-deftest test-org-element/cache ()
  2951. "Test basic expectations and common pitfalls for cache."
  2952. ;; Shift positions.
  2953. (should
  2954. (equal '(18 . 23)
  2955. (org-test-with-temp-text "para1\n\npara2\n\npara3"
  2956. (let ((org-element-use-cache t))
  2957. (save-excursion (goto-char (point-max)) (org-element-at-point))
  2958. (insert "add")
  2959. (forward-line 4)
  2960. (let ((element (org-element-at-point)))
  2961. (cons (org-element-property :begin element)
  2962. (org-element-property :end element)))))))
  2963. ;; Partial shifting: when the contents of a greater element are
  2964. ;; modified, only shift ending positions.
  2965. (should
  2966. (org-test-with-temp-text
  2967. "#+BEGIN_CENTER\nPara1\n\nPara2\n\nPara3\n#+END_CENTER"
  2968. (let ((org-element-use-cache t))
  2969. (save-excursion (search-forward "3") (org-element-at-point))
  2970. (search-forward "Para2")
  2971. (insert " ")
  2972. (let ((element (org-element-property :parent (org-element-at-point))))
  2973. (equal (cons (org-element-property :begin element)
  2974. (org-element-property :end element))
  2975. (cons (point-min) (point-max)))))))
  2976. ;; Re-parent shifted elements.
  2977. (should
  2978. (eq 'item
  2979. (org-test-with-temp-text "- item\n\n\n para1\n para2"
  2980. (let ((org-element-use-cache t))
  2981. (end-of-line)
  2982. (org-element-at-point)
  2983. (save-excursion (goto-char (point-max)) (org-element-at-point))
  2984. (forward-line)
  2985. (delete-char 1)
  2986. (goto-char (point-max))
  2987. (org-element-type
  2988. (org-element-property :parent (org-element-at-point)))))))
  2989. ;; Preserve local structures when re-parenting.
  2990. (should
  2991. (eq 'table
  2992. (org-test-with-temp-text
  2993. "#+begin_center\nP0\n\n<point>\n\n P1\n | a | b |\n| c | d |\n#+end_center"
  2994. (let ((org-element-use-cache t))
  2995. (save-excursion (search-forward "| c |") (org-element-at-point))
  2996. (insert "- item")
  2997. (search-forward "| c |")
  2998. (beginning-of-line)
  2999. (org-element-type
  3000. (org-element-property :parent (org-element-at-point)))))))
  3001. (should-not
  3002. (eq 'center-block
  3003. (org-test-with-temp-text
  3004. "#+begin_center\nP0\n\n<point>\n\n P1\n | a | b |\n#+end_center"
  3005. (let ((org-element-use-cache t))
  3006. (save-excursion (search-forward "| a |") (org-element-at-point))
  3007. (insert "- item")
  3008. (search-forward "| a |")
  3009. (beginning-of-line)
  3010. (org-element-type
  3011. (org-element-property :parent (org-element-at-point)))))))
  3012. ;; When re-parenting, also propagate changes to list structures.
  3013. (should
  3014. (= 2
  3015. (org-test-with-temp-text "\n Para\n - item<point>"
  3016. (let ((org-element-use-cache t))
  3017. (org-element-at-point)
  3018. (goto-char (point-min))
  3019. (insert "- Top\n")
  3020. (search-forward "- item")
  3021. (beginning-of-line)
  3022. (length (org-element-property :structure (org-element-at-point)))))))
  3023. ;; Modifying the last line of an element alters the element below.
  3024. (should
  3025. (org-test-with-temp-text "para1\n\npara2"
  3026. (let ((org-element-use-cache t))
  3027. (goto-char (point-max))
  3028. (org-element-at-point)
  3029. (forward-line -1)
  3030. (insert "merge")
  3031. (let ((element (org-element-at-point)))
  3032. (equal (cons (org-element-property :begin element)
  3033. (org-element-property :end element))
  3034. (cons (point-min) (point-max)))))))
  3035. ;; Modifying the first line of an element alters the element above.
  3036. (should
  3037. (org-test-with-temp-text ": fixed-width\n:not-fixed-width"
  3038. (let ((org-element-use-cache t))
  3039. (goto-char (point-max))
  3040. (org-element-at-point)
  3041. (search-backward ":")
  3042. (forward-char)
  3043. (insert " ")
  3044. (let ((element (org-element-at-point)))
  3045. (equal (cons (org-element-property :begin element)
  3046. (org-element-property :end element))
  3047. (cons (point-min) (point-max)))))))
  3048. ;; Sensitive change: adding a line alters document structure both
  3049. ;; above and below.
  3050. (should
  3051. (eq 'example-block
  3052. (org-test-with-temp-text "#+BEGIN_EXAMPLE\nPara1\n\nPara2\n"
  3053. (let ((org-element-use-cache t))
  3054. (goto-char (point-max))
  3055. (org-element-at-point)
  3056. (insert "#+END_EXAMPLE")
  3057. (search-backward "Para1")
  3058. (org-element-type (org-element-at-point))))))
  3059. (should
  3060. (eq 'example-block
  3061. (org-test-with-temp-text "Para1\n\nPara2\n#+END_EXAMPLE"
  3062. (let ((org-element-use-cache t))
  3063. (save-excursion (goto-char (point-max)) (org-element-at-point))
  3064. (insert "#+BEGIN_EXAMPLE\n")
  3065. (search-forward "Para2")
  3066. (org-element-type (org-element-at-point))))))
  3067. ;; Sensitive change: removing a line alters document structure both
  3068. ;; above and below.
  3069. (should
  3070. (eq 'example-block
  3071. (org-test-with-temp-text
  3072. "# +BEGIN_EXAMPLE\nPara1\n\nPara2\n#+END_EXAMPLE"
  3073. (let ((org-element-use-cache t))
  3074. (save-excursion (goto-char (point-max)) (org-element-at-point))
  3075. (forward-char)
  3076. (delete-char 1)
  3077. (search-forward "Para2")
  3078. (org-element-type (org-element-at-point))))))
  3079. (should
  3080. (eq 'example-block
  3081. (org-test-with-temp-text
  3082. "#+BEGIN_EXAMPLE\nPara1\n\nPara2\n# +END_EXAMPLE"
  3083. (let ((org-element-use-cache t))
  3084. (save-excursion (goto-char (point-max)) (org-element-at-point))
  3085. (search-forward "# ")
  3086. (delete-char -1)
  3087. (search-backward "Para1")
  3088. (org-element-type (org-element-at-point)))))))
  3089. (provide 'test-org-element)
  3090. ;;; test-org-element.el ends here