test-org-element.el 127 KB

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