test-org-element.el 151 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336
  1. ;;; test-org-element.el --- Tests for org-element.el -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2012-2015, 2019 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 <https://www.gnu.org/licenses/>.
  14. ;;; Code:
  15. (eval-and-compile (require 'cl-lib))
  16. (require 'org-element)
  17. (require 'org)
  18. (require 'org-inlinetask)
  19. (defun org-test-parse-and-interpret (text)
  20. "Parse TEXT as Org syntax and interpret it.
  21. Return interpreted string."
  22. (with-temp-buffer
  23. (org-mode)
  24. (insert text)
  25. (org-element-interpret-data (org-element-parse-buffer))))
  26. ;;; Test `org-element-map'
  27. (ert-deftest test-org-element/map ()
  28. "Test `org-element-map'."
  29. ;; Can map to `plain-text' objects.
  30. (should
  31. (= 2
  32. (org-test-with-temp-text "Some text \alpha
  33. #+BEGIN_CENTER
  34. Some other text
  35. #+END_CENTER"
  36. (let ((count 0))
  37. (org-element-map
  38. (org-element-parse-buffer) 'plain-text
  39. (lambda (s) (when (string-match "text" s) (cl-incf count))))
  40. count))))
  41. ;; Applies to secondary strings
  42. (should
  43. (org-element-map '("some " (bold nil "bold") "text") 'bold 'identity))
  44. ;; Enter secondary strings before entering contents.
  45. (should
  46. (equal
  47. "alpha"
  48. (org-element-property
  49. :name
  50. (org-test-with-temp-text "* Some \\alpha headline\n\\beta entity."
  51. (org-element-map (org-element-parse-buffer) 'entity 'identity nil t)))))
  52. ;; Apply NO-RECURSION argument.
  53. (should-not
  54. (org-test-with-temp-text "#+BEGIN_CENTER\n\\alpha\n#+END_CENTER"
  55. (org-element-map
  56. (org-element-parse-buffer) 'entity 'identity nil nil 'center-block)))
  57. ;; Use WITH-AFFILIATED argument.
  58. (should
  59. (equal
  60. '("a" "1" "b" "2")
  61. (org-test-with-temp-text "#+CAPTION[a]: 1\n#+CAPTION[b]: 2\nParagraph"
  62. (org-element-map
  63. (org-element-at-point) 'plain-text 'identity nil nil nil t)))))
  64. ;;; Test Setters
  65. (ert-deftest test-org-element/put-property ()
  66. "Test `org-element-put-property' specifications."
  67. ;; Standard test.
  68. (org-test-with-temp-text "* Headline\n *a*"
  69. (let ((tree (org-element-parse-buffer)))
  70. (org-element-put-property
  71. (org-element-map tree 'bold 'identity nil t) :test 1)
  72. (should (org-element-property
  73. :test (org-element-map tree 'bold 'identity nil t)))))
  74. ;; Put property on a string.
  75. (should
  76. (org-element-property :test (org-element-put-property "Paragraph" :test t))))
  77. (ert-deftest test-org-element/set-contents ()
  78. "Test `org-element-set-contents' specifications."
  79. ;; Accept multiple entries.
  80. (should
  81. (equal '("b" (italic nil "a"))
  82. (org-test-with-temp-text "* Headline\n *a*"
  83. (let ((tree (org-element-parse-buffer)))
  84. (org-element-set-contents
  85. (org-element-map tree 'bold 'identity nil t) "b" '(italic nil "a"))
  86. (org-element-contents
  87. (org-element-map tree 'bold 'identity nil t))))))
  88. ;; Accept atoms and elements.
  89. (should
  90. (equal '("b")
  91. (org-test-with-temp-text "* Headline\n *a*"
  92. (let ((tree (org-element-parse-buffer)))
  93. (org-element-set-contents
  94. (org-element-map tree 'bold 'identity nil t) "b")
  95. (org-element-contents
  96. (org-element-map tree 'bold 'identity nil t))))))
  97. (should
  98. (equal '((italic nil "b"))
  99. (org-test-with-temp-text "* Headline\n *a*"
  100. (let ((tree (org-element-parse-buffer)))
  101. (org-element-set-contents
  102. (org-element-map tree 'bold 'identity nil t) '(italic nil "b"))
  103. (org-element-contents
  104. (org-element-map tree 'bold 'identity nil t))))))
  105. ;; Allow nil contents.
  106. (should-not
  107. (org-test-with-temp-text "* Headline\n *a*"
  108. (let ((tree (org-element-parse-buffer)))
  109. (org-element-set-contents (org-element-map tree 'bold 'identity nil t))
  110. (org-element-contents (org-element-map tree 'bold 'identity nil t))))))
  111. (ert-deftest test-org-element/secondary-p ()
  112. "Test `org-element-secondary-p' specifications."
  113. ;; In a secondary string, return property name.
  114. (should
  115. (eq :title
  116. (org-test-with-temp-text "* Headline *object*"
  117. (org-element-map (org-element-parse-buffer) 'bold
  118. (lambda (object) (org-element-secondary-p object))
  119. nil t))))
  120. ;; Outside a secondary string, return nil.
  121. (should-not
  122. (org-test-with-temp-text "Paragraph *object*"
  123. (org-element-map (org-element-parse-buffer) 'bold
  124. (lambda (object) (org-element-type (org-element-secondary-p object)))
  125. nil t))))
  126. ;; FIXME: `org-element-class' is a defsubst and cannot be tested
  127. ;; properly (i.e., "make test" fails).
  128. ;;
  129. ;; (ert-deftest test-org-element/class ()
  130. ;; "Test `org-element-class' specifications."
  131. ;; ;; Regular tests.
  132. ;; (should (eq 'element (org-element-class '(paragraph nil) nil)))
  133. ;; (should (eq 'object (org-element-class '(target nil) nil)))
  134. ;; ;; Special types.
  135. ;; (should (eq 'element (org-element-class '(org-data nil) nil)))
  136. ;; (should (eq 'object (org-element-class "text" nil)))
  137. ;; (should (eq 'object (org-element-class '("secondary " "string") nil)))
  138. ;; ;; Pseudo elements.
  139. ;; (should (eq 'element (org-element-class '(foo nil) nil)))
  140. ;; (should (eq 'element (org-element-class '(foo nil) '(center-block nil))))
  141. ;; (should (eq 'element (org-element-class '(foo nil) '(org-data nil))))
  142. ;; ;; Pseudo objects.
  143. ;; (should (eq 'object (org-element-class '(foo nil) '(bold nil))))
  144. ;; (should (eq 'object (org-element-class '(foo nil) '(paragraph nil))))
  145. ;; (should (eq 'object (org-element-class '(foo nil) '("secondary"))))
  146. ;; (should
  147. ;; (eq 'object
  148. ;; (let* ((datum '(foo nil))
  149. ;; (headline `(headline (:title (,datum)))))
  150. ;; (org-element-put-property datum :parent headline)
  151. ;; (org-element-class datum)))))
  152. (ert-deftest test-org-element/adopt-elements ()
  153. "Test `org-element-adopt-elements' specifications."
  154. ;; Adopt an element.
  155. (should
  156. (equal '(plain-text italic)
  157. (org-test-with-temp-text "* Headline\n *a*"
  158. (let ((tree (org-element-parse-buffer)))
  159. (org-element-adopt-elements
  160. (org-element-map tree 'bold 'identity nil t) '(italic nil "a"))
  161. (mapcar (lambda (blob) (org-element-type blob))
  162. (org-element-contents
  163. (org-element-map tree 'bold 'identity nil t)))))))
  164. ;; Adopt a string.
  165. (should
  166. (equal '("a" "b")
  167. (org-test-with-temp-text "* Headline\n *a*"
  168. (let ((tree (org-element-parse-buffer)))
  169. (org-element-adopt-elements
  170. (org-element-map tree 'bold 'identity nil t) "b")
  171. (org-element-contents
  172. (org-element-map tree 'bold 'identity nil t)))))))
  173. (ert-deftest test-org-element/extract-element ()
  174. "Test `org-element-extract-element' specifications."
  175. ;; Extract a greater element.
  176. (should
  177. (eq 'org-data
  178. (org-test-with-temp-text "* Headline"
  179. (let* ((tree (org-element-parse-buffer))
  180. (element (org-element-map tree 'headline 'identity nil t)))
  181. (org-element-extract-element element)
  182. (org-element-type tree)))))
  183. ;; Extract an element.
  184. (should-not
  185. (org-element-map
  186. (org-test-with-temp-text "Paragraph"
  187. (let* ((tree (org-element-parse-buffer))
  188. (element (org-element-map tree 'paragraph 'identity nil t)))
  189. (org-element-extract-element element)
  190. tree))
  191. 'paragraph
  192. 'identity))
  193. ;; Extract an object, even in a secondary string.
  194. (should-not
  195. (org-element-map
  196. (org-test-with-temp-text "*bold*"
  197. (let* ((tree (org-element-parse-buffer))
  198. (element (org-element-map tree 'bold 'identity nil t)))
  199. (org-element-extract-element element)
  200. tree))
  201. 'bold
  202. 'identity))
  203. (should-not
  204. (org-element-map
  205. (org-test-with-temp-text "* Headline *bold*"
  206. (let* ((tree (org-element-parse-buffer))
  207. (element (org-element-map tree 'bold 'identity nil t)))
  208. (org-element-extract-element element)
  209. tree))
  210. 'bold
  211. 'identity))
  212. ;; Return value doesn't have any :parent set.
  213. (should-not
  214. (org-element-property
  215. :parent
  216. (org-test-with-temp-text "* Headline\n Paragraph with *bold* text."
  217. (let* ((tree (org-element-parse-buffer))
  218. (element (org-element-map tree 'bold 'identity nil t)))
  219. (org-element-extract-element element))))))
  220. (ert-deftest test-org-element/insert-before ()
  221. "Test `org-element-insert-before' specifications."
  222. ;; Standard test.
  223. (should
  224. (equal
  225. '(italic entity bold)
  226. (org-test-with-temp-text "/some/ *paragraph*"
  227. (let* ((tree (org-element-parse-buffer))
  228. (_paragraph (org-element-map tree 'paragraph #'identity nil t))
  229. (bold (org-element-map tree 'bold 'identity nil t)))
  230. (org-element-insert-before '(entity (:name "\\alpha")) bold)
  231. (org-element-map tree '(bold entity italic) #'org-element-type nil)))))
  232. ;; Insert an object in a secondary string.
  233. (should
  234. (equal
  235. '(entity italic)
  236. (org-test-with-temp-text "* /A/\n Paragraph."
  237. (let* ((tree (org-element-parse-buffer))
  238. (headline (org-element-map tree 'headline 'identity nil t))
  239. (italic (org-element-map tree 'italic 'identity nil t)))
  240. (org-element-insert-before '(entity (:name "\\alpha")) italic)
  241. (org-element-map (org-element-property :title headline) '(entity italic)
  242. #'org-element-type))))))
  243. (ert-deftest test-org-element/set-element ()
  244. "Test `org-element-set-element' specifications."
  245. ;; Check if new element is inserted.
  246. (should
  247. (org-test-with-temp-text "* Headline\n*a*"
  248. (let* ((tree (org-element-parse-buffer))
  249. (bold (org-element-map tree 'bold 'identity nil t)))
  250. (org-element-set-element bold '(italic nil "b"))
  251. (org-element-map tree 'italic 'identity))))
  252. ;; Check if old element is removed.
  253. (should-not
  254. (org-test-with-temp-text "* Headline\n*a*"
  255. (let* ((tree (org-element-parse-buffer))
  256. (bold (org-element-map tree 'bold 'identity nil t)))
  257. (org-element-set-element bold '(italic nil "b"))
  258. (org-element-map tree 'bold 'identity))))
  259. ;; Check if :parent property is correctly set.
  260. (should
  261. (eq 'paragraph
  262. (org-test-with-temp-text "* Headline\n*a*"
  263. (let* ((tree (org-element-parse-buffer))
  264. (bold (org-element-map tree 'bold 'identity nil t)))
  265. (org-element-set-element bold '(italic nil "b"))
  266. (org-element-type
  267. (org-element-property
  268. :parent (org-element-map tree 'italic 'identity nil t)))))))
  269. ;; Allow to replace strings with elements.
  270. (should
  271. (equal '("b")
  272. (org-test-with-temp-text "* Headline"
  273. (let* ((tree (org-element-parse-buffer))
  274. (text (org-element-map tree 'plain-text 'identity nil t)))
  275. (org-element-set-element text (list 'bold nil "b"))
  276. (org-element-map tree 'plain-text 'identity)))))
  277. ;; Allow to replace elements with strings.
  278. (should
  279. (equal "a"
  280. (org-test-with-temp-text "* =verbatim="
  281. (let* ((tree (org-element-parse-buffer))
  282. (verb (org-element-map tree 'verbatim 'identity nil t)))
  283. (org-element-set-element verb "a")
  284. (org-element-map tree 'plain-text 'identity nil t)))))
  285. ;; Allow to replace strings with strings.
  286. (should
  287. (equal "b"
  288. (org-test-with-temp-text "a"
  289. (let* ((tree (org-element-parse-buffer))
  290. (text (org-element-map tree 'plain-text 'identity nil t)))
  291. (org-element-set-element text "b")
  292. (org-element-map tree 'plain-text 'identity nil t))))))
  293. (ert-deftest test-org-element/copy ()
  294. "Test `org-element-copy' specifications."
  295. ;; Preserve type.
  296. (should (eq 'bold
  297. (org-test-with-temp-text "*bold*"
  298. (org-element-type (org-element-copy (org-element-context))))))
  299. (should (eq 'plain-text
  300. (org-test-with-temp-text "*bold*"
  301. (org-element-type
  302. (org-element-map (org-element-parse-buffer) 'plain-text
  303. #'org-element-copy nil t)))))
  304. ;; Preserve properties except `:parent'.
  305. (should (= 7
  306. (org-test-with-temp-text "*bold*"
  307. (org-element-property
  308. :end (org-element-copy (org-element-context))))))
  309. (should-not
  310. (org-test-with-temp-text "*bold*"
  311. (org-element-property
  312. :parent (org-element-copy (org-element-context)))))
  313. (should-not
  314. (org-test-with-temp-text "*bold*"
  315. (org-element-property
  316. :parent
  317. (org-element-map (org-element-parse-buffer) 'plain-text
  318. #'org-element-copy nil t))))
  319. ;; Copying nil returns nil.
  320. (should-not (org-element-copy nil))
  321. ;; Return a copy secondary strings.
  322. (should (equal '("text") (org-element-copy '("text"))))
  323. (should-not (eq '("text") (org-element-copy '("text")))))
  324. ;;; Test Parsers
  325. ;;;; Affiliated Keywords
  326. (ert-deftest test-org-element/affiliated-keywords-parser ()
  327. "Test affiliated keywords parsing."
  328. ;; Read simple keywords.
  329. (should
  330. (equal "para"
  331. (org-element-property
  332. :name
  333. (org-test-with-temp-text "#+NAME: para\nParagraph"
  334. (org-element-at-point)))))
  335. (should
  336. (= 1
  337. (org-element-property
  338. :begin
  339. (org-test-with-temp-text "#+NAME: para\nParagraph"
  340. (org-element-at-point)))))
  341. ;; Parse multiple keywords.
  342. (should
  343. (equal
  344. '("line2" "line1")
  345. (org-element-property
  346. :attr_ascii
  347. (org-test-with-temp-text
  348. "#+ATTR_ASCII: line1\n#+ATTR_ASCII: line2\nParagraph"
  349. (org-element-at-point)))))
  350. ;; Parse "parsed" keywords, unless granularity prevents it.
  351. (should
  352. (equal
  353. '(("caption"))
  354. (org-test-with-temp-text "#+CAPTION: caption\nParagraph"
  355. (car (org-element-property :caption (org-element-at-point))))))
  356. (should
  357. (org-test-with-temp-text "#+CAPTION: *caption*\nParagraph"
  358. (org-element-map (org-element-map (org-element-parse-buffer)
  359. 'paragraph
  360. (lambda (e) (org-element-property :caption e)) nil t)
  361. 'bold
  362. #'org-element-type nil t)))
  363. (should-not
  364. (org-test-with-temp-text "#+CAPTION: *caption*\nParagraph"
  365. (org-element-map (org-element-map (org-element-parse-buffer 'element)
  366. 'paragraph
  367. (lambda (e) (org-element-property :caption e)) nil t)
  368. 'bold
  369. #'org-element-type nil t)))
  370. ;; Parse dual keywords.
  371. (should
  372. (equal
  373. '((("long") "short"))
  374. (org-test-with-temp-text "#+CAPTION[short]: long\nParagraph"
  375. (org-element-property :caption (org-element-at-point)))))
  376. ;; Allow multiple caption keywords.
  377. (should
  378. (equal
  379. '((("l2") "s2") (("l1") "s1"))
  380. (org-test-with-temp-text "#+CAPTION[s1]: l1\n#+CAPTION[s2]: l2\nParagraph"
  381. (org-element-property :caption (org-element-at-point)))))
  382. (should
  383. (equal
  384. '((("l1")) (nil "s1"))
  385. (org-test-with-temp-text "#+CAPTION[s1]:\n#+CAPTION: l1\nParagraph"
  386. (org-element-property :caption (org-element-at-point)))))
  387. ;; Corner case: orphaned keyword at the end of an element.
  388. (should
  389. (eq 'keyword
  390. (org-test-with-temp-text "- item\n #+name: name\nSome paragraph"
  391. (progn (search-forward "name")
  392. (org-element-type (org-element-at-point))))))
  393. (should-not
  394. (org-test-with-temp-text "- item\n #+name: name\nSome paragraph"
  395. (progn (search-forward "Some")
  396. (org-element-property :name (org-element-at-point))))))
  397. ;;;; Babel Call
  398. (ert-deftest test-org-element/babel-call-parser ()
  399. "Test `babel-call' parsing."
  400. ;; Standard test.
  401. (should
  402. (eq 'babel-call
  403. (org-test-with-temp-text "#+CALL: test()"
  404. (org-element-type (org-element-at-point)))))
  405. ;; Ignore case.
  406. (should
  407. (eq 'babel-call
  408. (org-test-with-temp-text "#+call: test()"
  409. (org-element-type (org-element-at-point)))))
  410. ;; Handle non-empty blank line at the end of buffer.
  411. (should
  412. (org-test-with-temp-text "#+CALL: test()\n "
  413. (= (org-element-property :end (org-element-at-point)) (point-max))))
  414. ;; Parse call name.
  415. (should
  416. (equal "test"
  417. (org-test-with-temp-text "#+CALL: test()"
  418. (org-element-property :call (org-element-at-point)))))
  419. ;; Parse inside header. It may contain paired square brackets.
  420. (should
  421. (equal ":results output"
  422. (org-test-with-temp-text "#+CALL: test[:results output]()"
  423. (org-element-property :inside-header (org-element-at-point)))))
  424. (should
  425. (equal ":results output, a=table[1:2], b=2"
  426. (org-test-with-temp-text
  427. "#+CALL: test[:results output, a=table[1:2], b=2]()"
  428. (org-element-property :inside-header (org-element-at-point)))))
  429. ;; Parse arguments, which can be nested. However, stop at paired
  430. ;; parenthesis, even when, e.g.,end header contains some.
  431. (should
  432. (equal "n=4"
  433. (org-test-with-temp-text "#+CALL: test(n=4)"
  434. (org-element-property :arguments (org-element-at-point)))))
  435. (should
  436. (equal "test()"
  437. (org-test-with-temp-text "#+CALL: test(test())"
  438. (org-element-property :arguments (org-element-at-point)))))
  439. (should
  440. (equal "a=1"
  441. (org-test-with-temp-text "#+CALL: test(a=1) :post another-call()"
  442. (org-element-property :arguments (org-element-at-point)))))
  443. ;; Parse end header.
  444. (should
  445. (equal ":results html"
  446. (org-test-with-temp-text "#+CALL: test() :results html"
  447. (org-element-property :end-header (org-element-at-point))))))
  448. ;;;; Bold
  449. (ert-deftest test-org-element/bold-parser ()
  450. "Test `bold' parser."
  451. ;; Standard test.
  452. (should
  453. (org-test-with-temp-text "*bold*"
  454. (org-element-map (org-element-parse-buffer) 'bold #'identity nil t)))
  455. ;; Multi-line markup.
  456. (should
  457. (equal
  458. (org-element-contents
  459. (org-test-with-temp-text "*first line\nsecond line*"
  460. (org-element-map (org-element-parse-buffer) 'bold #'identity nil t)))
  461. '("first line\nsecond line"))))
  462. ;;;; Center Block
  463. (ert-deftest test-org-element/center-block-parser ()
  464. "Test `center-block' parser."
  465. ;; Standard test.
  466. (should
  467. (org-test-with-temp-text "#+BEGIN_CENTER\nText\n#+END_CENTER"
  468. (org-element-map (org-element-parse-buffer) 'center-block 'identity)))
  469. ;; Ignore case.
  470. (should
  471. (org-test-with-temp-text "#+begin_center\nText\n#+end_center"
  472. (org-element-map (org-element-parse-buffer) 'center-block 'identity)))
  473. ;; Ignore incomplete block.
  474. (should-not
  475. (org-test-with-temp-text "#+BEGIN_CENTER"
  476. (org-element-map (org-element-parse-buffer) 'center-block
  477. 'identity nil t)))
  478. ;; Handle non-empty blank line at the end of buffer.
  479. (should
  480. (org-test-with-temp-text "#+BEGIN_CENTER\nC\n#+END_CENTER\n "
  481. (= (org-element-property :end (org-element-at-point)) (point-max)))))
  482. ;;;; Citation
  483. (ert-deftest test-org-element/citation-parser ()
  484. "Test `citation' parser"
  485. ;; Parse citations. They must contain at least a bare key.
  486. (should
  487. (eq 'citation
  488. (org-test-with-temp-text "[cite:@key]"
  489. (org-element-type (org-element-context)))))
  490. (should-not
  491. (eq 'citation
  492. (org-test-with-temp-text "[cite:text]"
  493. (org-element-type (org-element-context)))))
  494. ;; Citation may contain a style.
  495. (should
  496. (eq 'citation
  497. (org-test-with-temp-text "[cite/style:@key]"
  498. (org-element-type (org-element-context)))))
  499. (should
  500. (equal "style"
  501. (org-test-with-temp-text "[cite/style:@key]"
  502. (org-element-property :style (org-element-context)))))
  503. ;; Handle multi citations separated with semi-columns.
  504. (should
  505. (eq 'citation
  506. (org-test-with-temp-text "[cite:@a;@b;@c]"
  507. (org-element-type (org-element-context)))))
  508. (should
  509. (equal '("a" "b" "c")
  510. (org-test-with-temp-text "[cite:@a;@b;@c]"
  511. (org-element-map (org-element-parse-buffer) 'citation-reference
  512. (lambda (r) (org-element-property :key r))))))
  513. (should
  514. (eq 'citation
  515. (org-test-with-temp-text "[cite:@a;-@b]"
  516. (org-element-type (org-element-context)))))
  517. (should
  518. (equal '("a" "b")
  519. (org-test-with-temp-text "[cite:@a;-@b]"
  520. (org-element-map (org-element-parse-buffer) 'citation-reference
  521. (lambda (r) (org-element-property :key r))))))
  522. ;; Multi citations accept `:prefix' and `:suffix' properties.
  523. (should
  524. (equal '("common-prefix")
  525. (org-test-with-temp-text "[cite:common-prefix;@a]"
  526. (org-element-property :prefix (org-element-context)))))
  527. (should
  528. (equal '("common-suffix")
  529. (org-test-with-temp-text "[cite:@a;common-suffix]"
  530. (org-element-property :suffix (org-element-context)))))
  531. ;; White spaces right after "cite" tags are ignored. So are white
  532. ;; spaces at the end of the citation.
  533. (should
  534. (equal '("common-prefix ")
  535. (org-test-with-temp-text "[cite: common-prefix ;@a]"
  536. (org-element-property :prefix (org-element-context)))))
  537. (should
  538. (equal '(" common-suffix")
  539. (org-test-with-temp-text "[cite: @a; common-suffix ]"
  540. (org-element-property :suffix (org-element-context)))))
  541. ;; Allow citations in a table cell.
  542. (should
  543. (eq 'citation
  544. (org-test-with-temp-text "| <point>[cite:@key] |"
  545. (org-element-type (org-element-context))))))
  546. ;;;; Citation Reference
  547. (ert-deftest test-org-element/citation-reference-parser ()
  548. "Test `citation' reference parser."
  549. ;; Parse bare keys.
  550. (should
  551. (eq 'citation-reference
  552. (org-test-with-temp-text "[cite:<point>@key]"
  553. (org-element-type (org-element-context)))))
  554. ;; Bare keys can contain any word character, and some punctuation,
  555. ;; but not semicolon, square brackets, and space.
  556. (should
  557. (equal "_key"
  558. (org-test-with-temp-text "[cite:@_k<point>ey]"
  559. (org-element-property :key (org-element-context)))))
  560. (should
  561. (eq 'citation-reference
  562. (org-test-with-temp-text "[cite:<point>@a]"
  563. (org-element-type (org-element-context)))))
  564. (should
  565. (eq 'citation-reference
  566. (org-test-with-temp-text "[cite:<point>@ö]"
  567. (org-element-type (org-element-context)))))
  568. (should
  569. (eq 'citation-reference
  570. (org-test-with-temp-text "[cite:<point>@_]"
  571. (org-element-type (org-element-context)))))
  572. (should
  573. (equal "a:.#$%&-+?<>~/1"
  574. (org-test-with-temp-text "[cite:<point>@a:.#$%&-+?<>~/1]"
  575. (org-element-property :key (org-element-context)))))
  576. (should-not
  577. (eq 'citation-reference
  578. (org-test-with-temp-text "[cite:<point>@;]"
  579. (org-element-type (org-element-context)))))
  580. (should-not
  581. (equal "key"
  582. (org-test-with-temp-text "[cite:<point>@[]]"
  583. (org-element-property :key (org-element-context)))))
  584. ;; References in citations accept optional `:prefix' and `:suffix'
  585. ;; properties.
  586. (should
  587. (equal '("pre ")
  588. (org-test-with-temp-text "[cite:pre <point>@key]"
  589. (org-element-property :prefix (org-element-context)))))
  590. (should
  591. (equal '(" post")
  592. (org-test-with-temp-text "[cite:<point>@key post]"
  593. (org-element-property :suffix (org-element-context)))))
  594. ;; White spaces between "cite" tag and prefix are ignored.
  595. (should
  596. (equal '("pre ")
  597. (org-test-with-temp-text "[cite: pre <point>@key]"
  598. (org-element-property :prefix (org-element-context)))))
  599. ;; Semicolons do not belong to prefix or suffix.
  600. (should
  601. (equal '("pre ")
  602. (org-test-with-temp-text "[cite:@key1;pre <point>@key2]"
  603. (org-element-property :prefix (org-element-context)))))
  604. (should
  605. (equal '(" post")
  606. (org-test-with-temp-text "[cite:@key1 <point>post;@key2]"
  607. (org-element-property :suffix (org-element-context)))))
  608. (should
  609. (equal '("pre ")
  610. (org-test-with-temp-text "[cite:global prefix;pre<point> @key1]"
  611. (org-element-property :prefix (org-element-context)))))
  612. (should
  613. (equal '(" post")
  614. (org-test-with-temp-text "[cite:@key1 <point>post; global suffix]"
  615. (org-element-property :suffix (org-element-context))))))
  616. ;;;; Clock
  617. (ert-deftest test-org-element/clock-parser ()
  618. "Test `clock' parser."
  619. ;; Running clock.
  620. (let ((clock (org-test-with-temp-text "CLOCK: [2012-01-01 sun. 00:01]"
  621. (org-element-at-point))))
  622. (should (eq (org-element-property :status clock) 'running))
  623. (should
  624. (equal (org-element-property :raw-value
  625. (org-element-property :value clock))
  626. "[2012-01-01 sun. 00:01]"))
  627. (should-not (org-element-property :duration clock)))
  628. ;; Closed clock.
  629. (let ((clock
  630. (org-test-with-temp-text
  631. "CLOCK: [2012-01-01 sun. 00:01]--[2012-01-01 sun. 00:02] => 0:01"
  632. (org-element-at-point))))
  633. (should (eq (org-element-property :status clock) 'closed))
  634. (should (equal (org-element-property :raw-value
  635. (org-element-property :value clock))
  636. "[2012-01-01 sun. 00:01]--[2012-01-01 sun. 00:02]"))
  637. (should (equal (org-element-property :duration clock) "0:01"))))
  638. ;;;; Code
  639. (ert-deftest test-org-element/code-parser ()
  640. "Test `code' parser."
  641. ;; Regular test.
  642. (should
  643. (org-test-with-temp-text "~code~"
  644. (org-element-map (org-element-parse-buffer) 'code #'identity)))
  645. ;; Multi-line markup.
  646. (should
  647. (equal
  648. (org-element-property
  649. :value
  650. (org-test-with-temp-text "~first line\nsecond line~"
  651. (org-element-map
  652. (org-element-parse-buffer) 'code #'identity nil t)))
  653. "first line\nsecond line")))
  654. ;;;; Comment
  655. (ert-deftest test-org-element/comment-parser ()
  656. "Test `comment' parser."
  657. ;; Regular comment.
  658. (should (eq 'comment
  659. (org-test-with-temp-text "# Comment"
  660. (org-element-type (org-element-at-point)))))
  661. ;; Inline comment.
  662. (should (eq 'comment
  663. (org-test-with-temp-text " # Comment"
  664. (org-element-type (org-element-at-point)))))
  665. ;; Preserve indentation.
  666. (should
  667. (equal "No blank\n One blank"
  668. (org-element-property
  669. :value
  670. (org-test-with-temp-text "# No blank\n# One blank"
  671. (org-element-at-point)))))
  672. ;; Comment with blank lines.
  673. (should
  674. (equal "First part\n\n\nSecond part"
  675. (org-element-property
  676. :value
  677. (org-test-with-temp-text "# First part\n# \n#\n# Second part"
  678. (org-element-at-point)))))
  679. ;; Do not mix comments and keywords.
  680. (should
  681. (eq 1
  682. (org-test-with-temp-text "#+keyword: value\n# comment\n#+keyword: value"
  683. (length (org-element-map (org-element-parse-buffer) 'comment
  684. #'identity)))))
  685. (should
  686. (equal "comment"
  687. (org-test-with-temp-text
  688. "#+key: value\n<point># comment\n#+key: value"
  689. (org-element-property :value (org-element-at-point)))))
  690. ;; Correctly handle non-empty blank lines at the end of buffer.
  691. (should
  692. (org-test-with-temp-text "# A\n "
  693. (= (org-element-property :end (org-element-at-point)) (point-max)))))
  694. ;;;; Comment Block
  695. (ert-deftest test-org-element/comment-block-parser ()
  696. "Test `comment-block' parser."
  697. ;; Standard test.
  698. (should
  699. (org-test-with-temp-text "#+BEGIN_COMMENT\nText\n#+END_COMMENT"
  700. (org-element-map (org-element-parse-buffer) 'comment-block 'identity)))
  701. ;; Ignore case.
  702. (should
  703. (org-test-with-temp-text "#+begin_comment\nText\n#+end_comment"
  704. (org-element-map (org-element-parse-buffer) 'comment-block 'identity)))
  705. ;; Ignore incomplete block.
  706. (should-not
  707. (org-test-with-temp-text "#+BEGIN_COMMENT"
  708. (org-element-map (org-element-parse-buffer) 'comment-block
  709. 'identity nil t)))
  710. ;; Handle non-empty blank line at the end of buffer.
  711. (should
  712. (org-test-with-temp-text "#+BEGIN_COMMENT\nC\n#+END_COMMENT\n "
  713. (= (org-element-property :end (org-element-at-point)) (point-max)))))
  714. ;;;; Diary Sexp
  715. (ert-deftest test-org-element/diary-sexp-parser ()
  716. "Test `diary-sexp' parser."
  717. ;; Standard test.
  718. (should
  719. (eq 'diary-sexp
  720. (org-test-with-temp-text
  721. "%%(org-anniversary 1956 5 14)(2) Arthur Dent is %d years old"
  722. (org-element-type (org-element-at-point)))))
  723. ;; Diary sexp must live at beginning of line
  724. (should-not
  725. (eq 'diary-sexp
  726. (org-test-with-temp-text " %%(org-bbdb-anniversaries)"
  727. (org-element-type (org-element-at-point)))))
  728. ;; Handle non-empty blank line at the end of buffer.
  729. (should
  730. (org-test-with-temp-text "%%(org-bbdb-anniversaries)\n "
  731. (= (org-element-property :end (org-element-at-point)) (point-max)))))
  732. ;;;; Drawer
  733. (ert-deftest test-org-element/drawer-parser ()
  734. "Test `drawer' parser."
  735. ;; Standard test.
  736. (should
  737. (org-test-with-temp-text ":TEST:\nText\n:END:"
  738. (org-element-map (org-element-parse-buffer) 'drawer 'identity)))
  739. ;; Ignore incomplete drawer.
  740. (should-not
  741. (org-test-with-temp-text ":TEST:"
  742. (org-element-map (org-element-parse-buffer) 'drawer 'identity nil t)))
  743. (should-not
  744. (org-test-with-temp-text ":END:"
  745. (org-element-map (org-element-parse-buffer) 'drawer 'identity nil t)))
  746. ;; Handle non-empty blank line at the end of buffer.
  747. (should
  748. (org-test-with-temp-text ":TEST:\nC\n:END:\n "
  749. (= (org-element-property :end (org-element-at-point)) (point-max)))))
  750. ;;;; Dynamic Block
  751. (ert-deftest test-org-element/dynamic-block-parser ()
  752. "Test `dynamic-block' parser."
  753. ;; Standard test.
  754. (should
  755. (org-test-with-temp-text
  756. "#+BEGIN: myblock :param1 val1 :param2 val2\nText\n#+END:"
  757. (org-element-map (org-element-parse-buffer) 'dynamic-block 'identity)))
  758. ;; Ignore case.
  759. (should
  760. (org-test-with-temp-text
  761. "#+begin: myblock :param1 val1 :param2 val2\nText\n#+end:"
  762. (org-element-map (org-element-parse-buffer) 'dynamic-block 'identity)))
  763. ;; Ignore incomplete block.
  764. (should-not
  765. (org-test-with-temp-text "#+BEGIN: myblock :param1 val1 :param2 val2"
  766. (org-element-map (org-element-parse-buffer) 'dynamic-block
  767. 'identity nil t)))
  768. ;; Handle non-empty blank line at the end of buffer.
  769. (should
  770. (org-test-with-temp-text "#+BEGIN: myblock :param val1\nC\n#+END:\n "
  771. (= (org-element-property :end (org-element-at-point)) (point-max)))))
  772. ;;;; Entity
  773. (ert-deftest test-org-element/entity-parser ()
  774. "Test `entity' parser."
  775. ;; Without brackets.
  776. (should
  777. (org-test-with-temp-text "\\sin"
  778. (org-element-map (org-element-parse-buffer) 'entity 'identity)))
  779. ;; With brackets.
  780. (should
  781. (org-element-property
  782. :use-brackets-p
  783. (org-test-with-temp-text "\\alpha{}text"
  784. (org-element-map (org-element-parse-buffer) 'entity 'identity nil t))))
  785. ;; User-defined entity.
  786. (should
  787. (equal
  788. (org-element-property
  789. :name
  790. (let ((org-entities-user
  791. '(("test" "test" nil "test" "test" "test" "test"))))
  792. (org-test-with-temp-text "\\test"
  793. (org-element-map (org-element-parse-buffer) 'entity 'identity nil t))))
  794. "test"))
  795. ;; Special case: entity at the end of a container.
  796. (should
  797. (eq 'entity
  798. (org-test-with-temp-text "*\\alpha \\beta*"
  799. (search-forward "be")
  800. (org-element-type (org-element-context))))))
  801. ;;;; Example Block
  802. (ert-deftest test-org-element/example-block-parser ()
  803. "Test `example-block' parser."
  804. ;; Standard test.
  805. (should
  806. (org-test-with-temp-text "#+BEGIN_EXAMPLE\nText\n#+END_EXAMPLE"
  807. (org-element-map (org-element-parse-buffer) 'example-block 'identity)))
  808. ;; Ignore incomplete block.
  809. (should-not
  810. (eq 'example-block
  811. (org-test-with-temp-text "#+BEGIN_EXAMPLE"
  812. (org-element-type (org-element-at-point)))))
  813. ;; Properly un-escape code.
  814. (should
  815. (equal "* Headline\n #+keyword:\nText\n"
  816. (org-test-with-temp-text
  817. "#+BEGIN_EXAMPLE\n,* Headline\n ,#+keyword:\nText\n#+END_EXAMPLE"
  818. (org-element-property :value (org-element-at-point)))))
  819. ;; Handle non-empty blank line at the end of buffer.
  820. (should
  821. (org-test-with-temp-text "#+BEGIN_EXAMPLE\nC\n#+END_EXAMPLE\n "
  822. (= (org-element-property :end (org-element-at-point)) (point-max)))))
  823. (ert-deftest test-org-element/block-switches ()
  824. "Test `example-block' and `src-block' switches parsing."
  825. (let ((org-coderef-label-format "(ref:%s)"))
  826. ;; 1. Test "-i" switch.
  827. (should-not
  828. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  829. (org-element-property :preserve-indent (org-element-at-point))))
  830. (should
  831. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp -i\n(+ 1 1)\n#+END_SRC"
  832. (org-element-property :preserve-indent (org-element-at-point))))
  833. (should-not
  834. (org-test-with-temp-text "#+BEGIN_EXAMPLE\nText.\n#+END_EXAMPLE"
  835. (org-element-property :preserve-indent (org-element-at-point))))
  836. (should
  837. (org-test-with-temp-text "#+BEGIN_EXAMPLE -i\nText.\n#+END_EXAMPLE"
  838. (org-element-property :preserve-indent (org-element-at-point))))
  839. ;; 2. "-n -r -k" combination should number lines, retain labels but
  840. ;; not use them in coderefs.
  841. (org-test-with-temp-text "#+BEGIN_EXAMPLE -n -r -k\nText.\n#+END_EXAMPLE"
  842. (let ((element (org-element-at-point)))
  843. (should (org-element-property :number-lines element))
  844. (should (org-element-property :retain-labels element))
  845. (should-not (org-element-property :use-labels element))))
  846. (org-test-with-temp-text
  847. "#+BEGIN_SRC emacs-lisp -n -r -k\n(+ 1 1)\n#+END_SRC"
  848. (let ((element (org-element-at-point)))
  849. (should (org-element-property :number-lines element))
  850. (should (org-element-property :retain-labels element))
  851. (should-not (org-element-property :use-labels element))))
  852. ;; 3. "-n -r" combination should number-lines remove labels and not
  853. ;; use them in coderefs.
  854. (org-test-with-temp-text "#+BEGIN_EXAMPLE -n -r\nText.\n#+END_EXAMPLE"
  855. (let ((element (org-element-at-point)))
  856. (should (org-element-property :number-lines element))
  857. (should-not (org-element-property :retain-labels element))
  858. (should-not (org-element-property :use-labels element))))
  859. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp -n -r\n(+ 1 1)\n#+END_SRC"
  860. (let ((element (org-element-at-point)))
  861. (should (org-element-property :number-lines element))
  862. (should-not (org-element-property :retain-labels element))
  863. (should-not (org-element-property :use-labels element))))
  864. ;; 4. "-n" or "+n" should number lines, retain labels and use them
  865. ;; in coderefs.
  866. (should
  867. (org-test-with-temp-text "#+BEGIN_EXAMPLE -n\nText.\n#+END_EXAMPLE"
  868. (let ((element (org-element-at-point)))
  869. (and (org-element-property :number-lines element)
  870. (org-element-property :retain-labels element)
  871. (org-element-property :use-labels element)))))
  872. (should
  873. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp -n\n(+ 1 1)\n#+END_SRC"
  874. (let ((element (org-element-at-point)))
  875. (and (org-element-property :number-lines element)
  876. (org-element-property :retain-labels element)
  877. (org-element-property :use-labels element)))))
  878. (should
  879. (org-test-with-temp-text "#+BEGIN_EXAMPLE +n\nText.\n#+END_EXAMPLE"
  880. (let ((element (org-element-at-point)))
  881. (and (org-element-property :number-lines element)
  882. (org-element-property :retain-labels element)
  883. (org-element-property :use-labels element)))))
  884. (should
  885. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp +n\n(+ 1 1)\n#+END_SRC"
  886. (let ((element (org-element-at-point)))
  887. (and (org-element-property :number-lines element)
  888. (org-element-property :retain-labels element)
  889. (org-element-property :use-labels element)))))
  890. ;; 5. No switch should not number lines, but retain labels and use
  891. ;; them in coderefs.
  892. (org-test-with-temp-text "#+BEGIN_EXAMPLE\nText.\n#+END_EXAMPLE"
  893. (let ((element (org-element-at-point)))
  894. (should (not (org-element-property :number-lines element)))
  895. (should (org-element-property :retain-labels element))
  896. (should (org-element-property :use-labels element))))
  897. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  898. (let ((element (org-element-at-point)))
  899. (should (not (org-element-property :number-lines element)))
  900. (should (org-element-property :retain-labels element))
  901. (should (org-element-property :use-labels element))))
  902. ;; 6. "-r" switch only: do not number lines, remove labels, and
  903. ;; don't use labels in coderefs.
  904. (org-test-with-temp-text "#+BEGIN_EXAMPLE -r\nText.\n#+END_EXAMPLE"
  905. (let ((element (org-element-at-point)))
  906. (should (not (org-element-property :number-lines element)))
  907. (should (not (org-element-property :retain-labels element)))
  908. (should (not (org-element-property :use-labels element)))))
  909. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp -r\n(+ 1 1)\n#+END_SRC"
  910. (let ((element (org-element-at-point)))
  911. (should (not (org-element-property :number-lines element)))
  912. (should (not (org-element-property :retain-labels element)))
  913. (should (not (org-element-property :use-labels element)))))
  914. ;; 7. Recognize coderefs with user-defined syntax.
  915. (should
  916. (equal
  917. "[ref:%s]"
  918. (org-test-with-temp-text
  919. "#+BEGIN_EXAMPLE -l \"[ref:%s]\"\nText [ref:text]\n#+END_EXAMPLE"
  920. (org-element-property :label-fmt (org-element-at-point)))))
  921. (should
  922. (equal
  923. "[ref:%s]"
  924. (org-test-with-temp-text
  925. "#+BEGIN_SRC emacs-lisp -l \"[ref:%s]\"\n(+ 1 1) [ref:text]\n#+END_SRC"
  926. (org-element-property :label-fmt (org-element-at-point)))))))
  927. ;;;; Export Block
  928. (ert-deftest test-org-element/export-block-parser ()
  929. "Test `export-block' parser."
  930. ;; Standard test.
  931. (should
  932. (eq 'export-block
  933. (org-test-with-temp-text "#+BEGIN_EXPORT LATEX\nText\n#+END_EXPORT"
  934. (org-element-type (org-element-at-point)))))
  935. (should
  936. (equal "LATEX"
  937. (org-test-with-temp-text "#+BEGIN_EXPORT LATEX\nText\n#+END_EXPORT"
  938. (org-element-property :type (org-element-at-point)))))
  939. ;; Ignore case.
  940. (should
  941. (eq 'export-block
  942. (org-test-with-temp-text "#+begin_export latex\nText\n#+end_export"
  943. (org-element-type (org-element-at-point)))))
  944. ;; Ignore incomplete block.
  945. (should-not
  946. (eq 'export-block
  947. (org-test-with-temp-text "#+BEGIN_EXPORT"
  948. (org-element-type (org-element-at-point)))))
  949. ;; Handle non-empty blank line at the end of buffer.
  950. (should
  951. (org-test-with-temp-text "#+BEGIN_EXPORT latex\nC\n#+END_EXPORT\n "
  952. (= (org-element-property :end (org-element-at-point)) (point-max))))
  953. ;; Un-escape commas in `:value'.
  954. (should
  955. (equal "* H\n"
  956. (org-test-with-temp-text "#+BEGIN_EXPORT org\n,* H\n#+END_EXPORT\n "
  957. (org-element-property :value (org-element-at-point))))))
  958. ;;;; Export Snippet
  959. (ert-deftest test-org-element/export-snippet-parser ()
  960. "Test `export-snippet' parser."
  961. (should
  962. (equal
  963. '("back-end" . "contents")
  964. (org-test-with-temp-text "@@back-end:contents@@"
  965. (org-element-map
  966. (org-element-parse-buffer) 'export-snippet
  967. (lambda (snippet) (cons (org-element-property :back-end snippet)
  968. (org-element-property :value snippet)))
  969. nil t)))))
  970. ;;;; Fixed Width
  971. (ert-deftest test-org-element/fixed-width-parser ()
  972. "Test fixed-width area parsing."
  973. ;; Preserve indentation.
  974. (should
  975. (equal "no blank\n one blank"
  976. (org-test-with-temp-text ": no blank\n: one blank"
  977. (org-element-property :value (org-element-at-point)))))
  978. ;; Fixed-width with empty lines.
  979. (should
  980. (equal "first part\n\n\nsecond part"
  981. (org-test-with-temp-text ": first part\n:\n: \n: second part"
  982. (org-element-property :value (org-element-at-point)))))
  983. ;; Parse indented fixed-width markers.
  984. (should
  985. (eq 'fixed-width
  986. (org-test-with-temp-text "Text\n<point> : no blank\n : one blank"
  987. (org-element-type (org-element-at-point)))))
  988. ;; Distinguish fixed-width areas within a list and outside of it.
  989. (should
  990. (org-test-with-temp-text "
  991. - Item
  992. : fixed-width inside<point>
  993. : fixed-width outside"
  994. (= (org-element-property :end (org-element-at-point))
  995. (line-beginning-position 2))))
  996. ;; Handle non-empty blank line at the end of buffer.
  997. (should
  998. (org-test-with-temp-text ": A\n "
  999. (= (org-element-property :end (org-element-at-point))
  1000. (point-max)))))
  1001. ;;;; Footnote Definition
  1002. (ert-deftest test-org-element/footnote-definition-parser ()
  1003. "Test `footnote-definition' parser."
  1004. (should
  1005. (org-test-with-temp-text "[fn:1] Definition"
  1006. (eq (org-element-type (org-element-at-point)) 'footnote-definition)))
  1007. ;; Footnote with more contents.
  1008. (should
  1009. (= 29 (org-test-with-temp-text "[fn:1] Definition\n\n| a | b |"
  1010. (org-element-property :end (org-element-at-point)))))
  1011. ;; Test difference between :contents-end and :end property
  1012. (should
  1013. (< (org-test-with-temp-text "[fn:1] Definition\n\n\n"
  1014. (org-element-property :contents-end (org-element-at-point)))
  1015. (org-test-with-temp-text "[fn:1] Definition\n\n\n"
  1016. (org-element-property :end (org-element-at-point)))))
  1017. ;; Footnote starting with special syntax.
  1018. (should-not
  1019. (org-test-with-temp-text "[fn:1] <point>- no item"
  1020. (eq (org-element-type (org-element-at-point)) 'item)))
  1021. ;; Correctly handle footnote starting with an empty line.
  1022. (should
  1023. (= 9
  1024. (org-test-with-temp-text "[fn:1]\n\n Body"
  1025. (org-element-property :contents-begin (org-element-at-point)))))
  1026. ;; Handle non-empty blank line at the end of buffer.
  1027. (should
  1028. (org-test-with-temp-text "[fn:1] Definition\n "
  1029. (= (org-element-property :end (org-element-at-point)) (point-max))))
  1030. ;; Footnote with attributes.
  1031. (should
  1032. (= 1
  1033. (org-test-with-temp-text "#+attr_latex: :offset 0in\n[fn:1] A footnote."
  1034. (length
  1035. (org-element-map (org-element-parse-buffer) 'footnote-definition
  1036. #'identity)))))
  1037. (should
  1038. (org-test-with-temp-text "[fn:1] 1\n\n#+attr_latex: :offset 0in\n[fn:2] 2"
  1039. (goto-char (org-element-property :end (org-element-at-point)))
  1040. (looking-at "#")))
  1041. ;; An empty footnote has no contents.
  1042. (should-not
  1043. (org-test-with-temp-text "[fn:1]\n\n"
  1044. (let ((footnote (org-element-at-point)))
  1045. (or (org-element-property :contents-begin footnote)
  1046. (org-element-property :contents-end footnote)))))
  1047. ;; Parse `:pre-blank'.
  1048. (should
  1049. (= 0
  1050. (org-test-with-temp-text "[fn:1] A"
  1051. (org-element-property :pre-blank (org-element-at-point)))))
  1052. (should
  1053. (= 1
  1054. (org-test-with-temp-text "[fn:1]\nA"
  1055. (org-element-property :pre-blank (org-element-at-point)))))
  1056. (should
  1057. (= 2
  1058. (org-test-with-temp-text "[fn:1]\n\nA"
  1059. (org-element-property :pre-blank (org-element-at-point))))))
  1060. ;;;; Footnotes Reference.
  1061. (ert-deftest test-org-element/footnote-reference-parser ()
  1062. "Test `footnote-reference' parser."
  1063. ;; Parse a standard reference.
  1064. (should
  1065. (org-test-with-temp-text "Text[fn:label]"
  1066. (org-element-map
  1067. (org-element-parse-buffer) 'footnote-reference 'identity)))
  1068. ;; Parse an inline reference.
  1069. (should
  1070. (org-test-with-temp-text "Text[fn:test:def]"
  1071. (org-element-map
  1072. (org-element-parse-buffer) 'footnote-reference 'identity)))
  1073. ;; Parse an anonymous reference.
  1074. (should
  1075. (org-test-with-temp-text "Text[fn::def]"
  1076. (org-element-map
  1077. (org-element-parse-buffer) 'footnote-reference 'identity)))
  1078. ;; Parse inline references with syntax loaded characters.
  1079. (should
  1080. (eq 'footnote-reference
  1081. (org-test-with-temp-text "Text[fn<point>::(def]"
  1082. (org-element-type (org-element-context)))))
  1083. (should
  1084. (eq 'footnote-reference
  1085. (org-test-with-temp-text "Text[fn<point>::\"def]"
  1086. (org-element-type (org-element-context)))))
  1087. ;; Parse nested footnotes.
  1088. (should
  1089. (= 2
  1090. (length
  1091. (org-test-with-temp-text "Text[fn::def [fn:label]]"
  1092. (org-element-map
  1093. (org-element-parse-buffer) 'footnote-reference 'identity)))))
  1094. ;; Parse adjacent footnotes.
  1095. (should
  1096. (org-test-with-temp-text "Text[fn:label1][fn:label2]"
  1097. (= 2
  1098. (length
  1099. (org-element-map
  1100. (org-element-parse-buffer) 'footnote-reference 'identity)))))
  1101. ;; Only properly closed footnotes are recognized as such.
  1102. (should-not
  1103. (org-test-with-temp-text "Text[fn:label"
  1104. (org-element-map
  1105. (org-element-parse-buffer) 'footnote-reference 'identity))))
  1106. ;;;; Headline
  1107. (ert-deftest test-org-element/headline-todo-keyword ()
  1108. "Test todo keyword recognition."
  1109. ;; Reference test.
  1110. (org-test-with-temp-text "* TODO Headline"
  1111. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  1112. (should (org-element-property :todo-keyword (org-element-at-point)))))
  1113. ;; Todo keyword is prefix of headlines first word.
  1114. (org-test-with-temp-text "* TODOHeadline"
  1115. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  1116. (should-not (org-element-property :todo-keyword (org-element-at-point))))))
  1117. (ert-deftest test-org-element/headline-comment-keyword ()
  1118. "Test COMMENT keyword recognition."
  1119. ;; Reference test.
  1120. (org-test-with-temp-text "* Headline"
  1121. (should-not (org-element-property :commentedp (org-element-at-point))))
  1122. ;; Standard position.
  1123. (org-test-with-temp-text "* COMMENT Headline"
  1124. (let ((headline (org-element-at-point)))
  1125. (should (org-element-property :commentedp headline))
  1126. (should (equal (org-element-property :raw-value headline) "Headline"))))
  1127. ;; Case sensitivity.
  1128. (org-test-with-temp-text "* Comment Headline"
  1129. (let ((headline (org-element-at-point)))
  1130. (should-not (org-element-property :commentedp headline))
  1131. (should (equal (org-element-property :raw-value headline)
  1132. "Comment Headline"))))
  1133. ;; With another keyword.
  1134. (org-test-with-temp-text "* TODO COMMENT Headline"
  1135. (let* ((org-todo-keywords '((sequence "TODO" "DONE")))
  1136. (headline (org-element-at-point)))
  1137. (should (org-element-property :commentedp headline))
  1138. (should (equal (org-element-property :raw-value headline) "Headline"))))
  1139. ;; With the keyword only.
  1140. (org-test-with-temp-text "* COMMENT"
  1141. (let* ((headline (org-element-at-point)))
  1142. (should (org-element-property :commentedp headline))
  1143. (should (equal (org-element-property :raw-value headline) "")))))
  1144. (ert-deftest test-org-element/headline-archive-tag ()
  1145. "Test ARCHIVE tag recognition."
  1146. ;; Reference test.
  1147. (should-not
  1148. (org-test-with-temp-text "* Headline"
  1149. (org-element-property :archivedp (org-element-at-point))))
  1150. ;; Single tag.
  1151. (org-test-with-temp-text "* Headline :ARCHIVE:"
  1152. (let ((headline (org-element-at-point)))
  1153. (should (org-element-property :archivedp headline))))
  1154. ;; Multiple tags.
  1155. (org-test-with-temp-text "* Headline :test:ARCHIVE:"
  1156. (let ((headline (org-element-at-point)))
  1157. (should (org-element-property :archivedp headline))))
  1158. ;; Tag is case-sensitive.
  1159. (should-not
  1160. (org-test-with-temp-text "* Headline :Archive:"
  1161. (org-element-property :archivedp (org-element-at-point)))))
  1162. (ert-deftest test-org-element/headline-properties ()
  1163. "Test properties from property drawer."
  1164. ;; All properties from property drawer have their symbol upper
  1165. ;; cased.
  1166. (should
  1167. (org-test-with-temp-text "* Headline\n:PROPERTIES:\n:foo: bar\n:END:"
  1168. (org-element-property :FOO (org-element-at-point))))
  1169. (should-not
  1170. (org-test-with-temp-text "* Headline\n:PROPERTIES:\n:foo: bar\n:END:"
  1171. (org-element-property :foo (org-element-at-point))))
  1172. ;; Also parse properties associated in inlinetasks.
  1173. (when (featurep 'org-inlinetask)
  1174. (should
  1175. (org-test-with-temp-text "*************** Inlinetask
  1176. :PROPERTIES:
  1177. :foo: bar
  1178. :END:
  1179. *************** END"
  1180. (org-element-property :FOO (org-element-at-point)))))
  1181. ;; Do not find property drawer in a verbatim area.
  1182. (should-not
  1183. (org-test-with-temp-text
  1184. "* Headline
  1185. #+BEGIN_EXAMPLE
  1186. :PROPERTIES:
  1187. :foo: bar
  1188. :END:
  1189. #+END_EXAMPLE"
  1190. (org-element-property :FOO (org-element-at-point))))
  1191. ;; Do not use properties from a drawer associated to an inlinetask.
  1192. (when (featurep 'org-inlinetask)
  1193. (should-not
  1194. (org-test-with-temp-text
  1195. "* Headline
  1196. *************** Inlinetask
  1197. :PROPERTIES:
  1198. :foo: bar
  1199. :END:
  1200. *************** END"
  1201. (org-element-property
  1202. :FOO (let ((org-inlinetask-min-level 15)) (org-element-at-point))))))
  1203. ;; Do not find incomplete drawers.
  1204. (should-not
  1205. (org-test-with-temp-text "* Headline\n:PROPERTIES:\n:foo: bar"
  1206. (org-element-property :FOO (org-element-at-point)))))
  1207. ;;;; Horizontal Rule
  1208. (ert-deftest test-org-element/horizontal-rule-parser ()
  1209. "Test `horizontal-rule' parser."
  1210. ;; Standard.
  1211. (should
  1212. (org-test-with-temp-text "-----"
  1213. (org-element-map (org-element-parse-buffer) 'horizontal-rule 'identity)))
  1214. ;; Indented.
  1215. (should
  1216. (org-test-with-temp-text " -----"
  1217. (org-element-map (org-element-parse-buffer) 'horizontal-rule 'identity)))
  1218. ;; Hyphen must be alone on the line.
  1219. (should-not
  1220. (org-test-with-temp-text "-----wrong"
  1221. (org-element-map (org-element-parse-buffer) 'horizontal-rule 'identity)))
  1222. ;; 4 hyphens is too small.
  1223. (should-not
  1224. (org-test-with-temp-text "----"
  1225. (org-element-map (org-element-parse-buffer) 'horizontal-rule 'identity)))
  1226. ;; Handle non-empty blank line at the end of buffer.
  1227. (should
  1228. (org-test-with-temp-text "-----\n "
  1229. (= (org-element-property :end (org-element-at-point)) (point-max)))))
  1230. ;;;; Inline Babel Call
  1231. (ert-deftest test-org-element/inline-babel-call-parser ()
  1232. "Test `inline-babel-call' parser."
  1233. ;; Standard test.
  1234. (should
  1235. (eq 'inline-babel-call
  1236. (org-test-with-temp-text "call_test()"
  1237. (org-element-type (org-element-context)))))
  1238. (should
  1239. (eq 'inline-babel-call
  1240. (org-test-with-temp-text "call_test[:results output](x=2)[:results html]"
  1241. (org-element-type (org-element-context)))))
  1242. ;; Parse call name.
  1243. (should
  1244. (equal
  1245. "test"
  1246. (org-test-with-temp-text "call_test[:results output](x=2)[:results html]"
  1247. (org-element-property :call (org-element-context)))))
  1248. ;; Parse inside header.
  1249. (should
  1250. (equal
  1251. ":results output"
  1252. (org-test-with-temp-text "call_test[:results output](x=2)[:results html]"
  1253. (org-element-property :inside-header (org-element-context)))))
  1254. ;; Parse arguments.
  1255. (should
  1256. (equal
  1257. "x=2"
  1258. (org-test-with-temp-text "call_test[:results output](x=2)[:results html]"
  1259. (org-element-property :arguments (org-element-context)))))
  1260. ;; Parse end header.
  1261. (should
  1262. (equal
  1263. ":results html"
  1264. (org-test-with-temp-text "call_test[:results output](x=2)[:results html]"
  1265. (org-element-property :end-header (org-element-context)))))
  1266. ;; Handle multi-line babel calls.
  1267. (should
  1268. (eq 'inline-babel-call
  1269. (org-test-with-temp-text
  1270. "call_test[:results\noutput](x=2)[:results html]"
  1271. (org-element-type (org-element-context)))))
  1272. (should
  1273. (eq 'inline-babel-call
  1274. (org-test-with-temp-text
  1275. "call_test[:results output](x=2\ny=3)[:results html]"
  1276. (org-element-type (org-element-context)))))
  1277. (should
  1278. (eq 'inline-babel-call
  1279. (org-test-with-temp-text
  1280. "call_test[:results output](x=2)[:results\nhtml]"
  1281. (org-element-type (org-element-context)))))
  1282. ;; Parse parameters containing round brackets.
  1283. (should
  1284. (eq 'inline-babel-call
  1285. (org-test-with-temp-text "call_test[:var x='(1)](x=2)"
  1286. (org-element-type (org-element-context))))))
  1287. ;;;; Inline Src Block
  1288. (ert-deftest test-org-element/inline-src-block-parser ()
  1289. "Test `inline-src-block' parser."
  1290. (should
  1291. (org-test-with-temp-text "src_emacs-lisp{(+ 1 1)}"
  1292. (org-element-map (org-element-parse-buffer) 'inline-src-block 'identity)))
  1293. ;; With switches.
  1294. (should
  1295. (org-test-with-temp-text "src_emacs-lisp[:foo bar]{(+ 1 1)}"
  1296. (org-element-map (org-element-parse-buffer) 'inline-src-block 'identity)))
  1297. (should
  1298. (org-test-with-temp-text "src_emacs-lisp[ :foo bar]{(+ 1 1)}"
  1299. (org-element-map (org-element-parse-buffer) 'inline-src-block 'identity)))
  1300. ;; Empty switches.
  1301. (should
  1302. (org-test-with-temp-text "src_emacs-lisp[]{(+ 1 1)}"
  1303. (org-element-map (org-element-parse-buffer) 'inline-src-block 'identity)))
  1304. ;; Invalid syntax.
  1305. (should-not
  1306. (org-test-with-temp-text "src_emacs-lisp[]foo{(+ 1 1)}"
  1307. (org-element-map (org-element-parse-buffer) 'inline-src-block 'identity)))
  1308. (should-not
  1309. (org-test-with-temp-text "foosrc_emacs-lisp[]{(+ 1 1)}"
  1310. (org-element-map (org-element-parse-buffer) 'inline-src-block 'identity)))
  1311. ;; Invalid language name
  1312. (should-not
  1313. (org-test-with-temp-text "src_emacs-\tlisp{(+ 1 1)}"
  1314. (org-element-map (org-element-parse-buffer) 'inline-src-block 'identity)))
  1315. ;; Test parsing at the beginning of an item.
  1316. (should
  1317. (org-test-with-temp-text "- src_emacs-lisp{(+ 1 1)}"
  1318. (org-element-map (org-element-parse-buffer) 'inline-src-block 'identity)))
  1319. ;; Test parsing multi-line source blocks.
  1320. (should
  1321. (eq 'inline-src-block
  1322. (org-test-with-temp-text "src_emacs-lisp{(+ 1\n 1)}"
  1323. (org-element-type (org-element-context)))))
  1324. (should
  1325. (eq 'inline-src-block
  1326. (org-test-with-temp-text "src_emacs-lisp[\n:foo bar]{(+ 1 1)}"
  1327. (org-element-type (org-element-context)))))
  1328. (should
  1329. (eq 'inline-src-block
  1330. (org-test-with-temp-text "src_emacs-lisp[:foo\nbar]{(+ 1 1)}"
  1331. (org-element-type (org-element-context)))))
  1332. ;; Besides curly brackets, ignore any other bracket type.
  1333. (should
  1334. (equal "[foo"
  1335. (org-test-with-temp-text "src_emacs-lisp{[foo}"
  1336. (org-element-property :value (org-element-context)))))
  1337. (should
  1338. (equal "foo]"
  1339. (org-test-with-temp-text "src_emacs-lisp{foo]}"
  1340. (org-element-property :value (org-element-context)))))
  1341. (should
  1342. (equal "(foo"
  1343. (org-test-with-temp-text "src_emacs-lisp{(foo}"
  1344. (org-element-property :value (org-element-context)))))
  1345. (should
  1346. (equal "foo)"
  1347. (org-test-with-temp-text "src_emacs-lisp{foo)}"
  1348. (org-element-property :value (org-element-context)))))
  1349. ;; Parse parameters containing square brackets.
  1350. (should
  1351. (eq 'inline-src-block
  1352. (org-test-with-temp-text "src_emacs-lisp[:var table=t[1,1]]{(+ 1 1)}"
  1353. (org-element-type (org-element-context))))))
  1354. ;;;; Inlinetask
  1355. (ert-deftest test-org-element/inlinetask-parser ()
  1356. "Test `inlinetask' parser."
  1357. (when (featurep 'org-inlinetask)
  1358. (let ((org-inlinetask-min-level 15))
  1359. ;; Regular inlinetask.
  1360. (should
  1361. (eq 'inlinetask
  1362. (org-test-with-temp-text
  1363. "*************** Task\nTest\n*************** END"
  1364. (org-element-type (org-element-at-point)))))
  1365. (should
  1366. (eq 'inlinetask
  1367. (org-test-with-temp-text
  1368. "*************** Task\nTest\n*************** END"
  1369. (org-element-type (org-element-at-point)))))
  1370. ;; Degenerate inlinetask.
  1371. (should
  1372. (eq 'inlinetask
  1373. (org-test-with-temp-text "*************** Task"
  1374. (org-element-type (org-element-at-point)))))
  1375. ;; Mixed inlinetasks.
  1376. (should-not
  1377. (org-test-with-temp-text
  1378. "
  1379. *************** Task
  1380. *************** Task2
  1381. Contents
  1382. *************** END"
  1383. (forward-line)
  1384. (goto-char (org-element-property :end (org-element-at-point)))
  1385. (eobp)))
  1386. ;; TODO keyword.
  1387. (should
  1388. (equal
  1389. "TODO"
  1390. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  1391. (org-test-with-temp-text "*************** TODO Task"
  1392. (org-element-property :todo-keyword (org-element-at-point))))))
  1393. ;; Planning info.
  1394. (should
  1395. (org-test-with-temp-text "
  1396. *************** Task<point>
  1397. DEADLINE: <2012-03-29 thu.>
  1398. *************** END"
  1399. (org-element-property :deadline (org-element-at-point))))
  1400. (should
  1401. (eq 'planning
  1402. (org-test-with-temp-text "
  1403. *************** Task
  1404. <point>DEADLINE: <2012-03-29 thu.>
  1405. *************** END"
  1406. (org-element-type (org-element-at-point)))))
  1407. (should-not
  1408. (org-test-with-temp-text "
  1409. *************** Task<point>
  1410. DEADLINE: <2012-03-29 thu.>"
  1411. (org-element-property :deadline (org-element-at-point))))
  1412. (should-not
  1413. (eq 'planning
  1414. (org-test-with-temp-text "
  1415. *************** Task
  1416. <point>DEADLINE: <2012-03-29 thu.>"
  1417. (org-element-type (org-element-at-point)))))
  1418. ;; Priority.
  1419. (should
  1420. (eq
  1421. ?A
  1422. (org-test-with-temp-text "
  1423. *************** [#A] Task"
  1424. (forward-line)
  1425. (org-element-property :priority (org-element-at-point)))))
  1426. ;; Tags.
  1427. (should
  1428. (equal
  1429. '("test")
  1430. (org-test-with-temp-text "
  1431. *************** Task :test:"
  1432. (forward-line)
  1433. (org-element-property :tags (org-element-at-point)))))
  1434. ;; Regular properties are accessed through upper case keywords.
  1435. (should
  1436. (org-test-with-temp-text "
  1437. *************** Task
  1438. :PROPERTIES:
  1439. :foo: bar
  1440. :END:
  1441. *************** END"
  1442. (forward-line)
  1443. (org-element-property :FOO (org-element-at-point))))
  1444. (should-not
  1445. (org-test-with-temp-text "
  1446. *************** Task
  1447. :PROPERTIES:
  1448. :foo: bar
  1449. :END:
  1450. *************** END"
  1451. (forward-line)
  1452. (org-element-property :foo (org-element-at-point))))
  1453. ;; Handle non-empty blank line at the end of buffer.
  1454. (should
  1455. (org-test-with-temp-text "*************** Task\n*************** END\n "
  1456. (= (org-element-property :end (org-element-at-point)) (point-max)))))))
  1457. ;;;; Italic
  1458. (ert-deftest test-org-element/italic-parser ()
  1459. "Test `italic' parser."
  1460. ;; Regular test.
  1461. (should
  1462. (org-test-with-temp-text "/italic/"
  1463. (org-element-map (org-element-parse-buffer) 'italic #'identity nil t)))
  1464. ;; Multi-line markup.
  1465. (should
  1466. (equal
  1467. (org-element-contents
  1468. (org-test-with-temp-text "/first line\nsecond line/"
  1469. (org-element-map (org-element-parse-buffer) 'italic #'identity nil t)))
  1470. '("first line\nsecond line"))))
  1471. ;;;; Item
  1472. (ert-deftest test-org-element/item-parser ()
  1473. "Test `item' parser."
  1474. ;; Standard test.
  1475. (should
  1476. (org-test-with-temp-text "- item"
  1477. (org-element-map (org-element-parse-buffer) 'item 'identity)))
  1478. ;; Counter.
  1479. (should
  1480. (= 6
  1481. (org-element-property
  1482. :counter
  1483. (org-test-with-temp-text "6. [@6] item"
  1484. (org-element-map (org-element-parse-buffer) 'item 'identity nil t)))))
  1485. ;; Tag
  1486. (should
  1487. (equal
  1488. '("tag")
  1489. (org-element-property
  1490. :tag
  1491. (org-test-with-temp-text "- tag :: description"
  1492. (org-element-map (org-element-parse-buffer) 'item 'identity nil t)))))
  1493. ;; No tags in ordered lists.
  1494. (should-not
  1495. (org-element-property
  1496. :tag
  1497. (org-test-with-temp-text "1. tag :: description"
  1498. (org-element-map (org-element-parse-buffer) 'item 'identity nil t))))
  1499. ;; Check-boxes
  1500. (should
  1501. (equal
  1502. '(trans on off)
  1503. (org-test-with-temp-text "
  1504. - [-] item 1
  1505. - [X] item 1.1
  1506. - [ ] item 1.2"
  1507. (org-element-map (org-element-parse-buffer) 'item
  1508. (lambda (item) (org-element-property :checkbox item))))))
  1509. ;; Item starting with special syntax.
  1510. (should
  1511. (equal '(("- item"))
  1512. (org-test-with-temp-text "- - item"
  1513. (org-element-map (org-element-parse-buffer) 'paragraph
  1514. 'org-element-contents))))
  1515. ;; Block in an item: ignore indentation within the block.
  1516. (should
  1517. (org-test-with-temp-text
  1518. "-<point> item\n #+begin_src emacs-lisp\n(+ 1 1)\n #+end_src"
  1519. (= (org-element-property :end (org-element-at-point)) (point-max))))
  1520. ;; Parse `:pre-blank'.
  1521. (should
  1522. (= 0
  1523. (org-test-with-temp-text "-<point> A"
  1524. (org-element-property :pre-blank (org-element-at-point)))))
  1525. (should
  1526. (= 1
  1527. (org-test-with-temp-text "-<point>\n A"
  1528. (org-element-property :pre-blank (org-element-at-point)))))
  1529. (should
  1530. (= 2
  1531. (org-test-with-temp-text "-<point>\n\n A"
  1532. (org-element-property :pre-blank (org-element-at-point)))))
  1533. ;; Last item in a list or sub-list has no `:post-blank' lines, since
  1534. ;; those belong to the plain-list.
  1535. (should
  1536. (= 0
  1537. (org-test-with-temp-text "- A\n\n- <point>B\n\nEnd list"
  1538. (org-element-property :post-blank (org-element-at-point)))))
  1539. (should
  1540. (= 0
  1541. (org-test-with-temp-text "- A\n\n - B\n\n<point> - C\n\n End sub-list"
  1542. (org-element-property :post-blank (org-element-at-point))))))
  1543. ;;;; Keyword
  1544. (ert-deftest test-org-element/keyword-parser ()
  1545. "Test `keyword' parser."
  1546. ;; Standard test.
  1547. (should
  1548. (org-test-with-temp-text "#+KEYWORD: value"
  1549. (org-element-map (org-element-parse-buffer) 'keyword 'identity)))
  1550. ;; Keywords are case-insensitive.
  1551. (should
  1552. (org-test-with-temp-text "#+keyword: value"
  1553. (org-element-map (org-element-parse-buffer) 'keyword 'identity)))
  1554. ;; Affiliated keywords are not keywords.
  1555. (should-not
  1556. (org-test-with-temp-text "#+NAME: value
  1557. Paragraph"
  1558. (org-element-map (org-element-parse-buffer) 'keyword 'identity)))
  1559. ;; Do not mix keywords with Babel calls and dynamic blocks.
  1560. (should-not
  1561. (org-test-with-temp-text "#+CALL: fun()"
  1562. (org-element-map (org-element-parse-buffer) 'keyword 'identity)))
  1563. (should-not
  1564. (org-test-with-temp-text "#+BEGIN: my-fun\nBody\n#+END:"
  1565. (org-element-map (org-element-parse-buffer) 'keyword 'identity)))
  1566. ;; Handle non-empty blank line at the end of buffer.
  1567. (should
  1568. (org-test-with-temp-text "#+KEYWORD: value\n "
  1569. (= (org-element-property :end (org-element-at-point)) (point-max)))))
  1570. ;;;; Latex Environment
  1571. (ert-deftest test-org-element/latex-environment-parser ()
  1572. "Test `latex-environment' parser."
  1573. (should
  1574. (org-test-with-temp-text "\\begin{equation}\ne^{i\\pi}+1=0\n\\end{equation}"
  1575. (org-element-map (org-element-parse-buffer) 'latex-environment 'identity)))
  1576. ;; Allow nested environments.
  1577. (should
  1578. (equal
  1579. "\\begin{outer}
  1580. \\begin{inner}
  1581. e^{i\\pi}+1=0
  1582. \\end{inner}
  1583. \\end{outer}"
  1584. (org-test-with-temp-text "
  1585. \\begin{outer}
  1586. \\begin{inner}
  1587. e^{i\\pi}+1=0
  1588. \\end{inner}
  1589. \\end{outer}"
  1590. (org-element-property
  1591. :value
  1592. (org-element-map
  1593. (org-element-parse-buffer) 'latex-environment 'identity nil t)))))
  1594. ;; Allow environments with options and arguments.
  1595. (should
  1596. (eq 'latex-environment
  1597. (org-test-with-temp-text
  1598. "\\begin{theorem}[Euler]\ne^{i\\pi}+1=0\n\\end{theorem}"
  1599. (org-element-type (org-element-at-point)))))
  1600. (should
  1601. (eq 'latex-environment
  1602. (org-test-with-temp-text "\\begin{env}{arg}\nvalue\n\\end{env}"
  1603. (org-element-type (org-element-at-point)))))
  1604. ;; Allow environments without newline after \begin{.}.
  1605. (should
  1606. (eq 'latex-environment
  1607. (org-test-with-temp-text "\\begin{env}{arg}something\nvalue\n\\end{env}"
  1608. (org-element-type (org-element-at-point)))))
  1609. ;; Allow one-line environments.
  1610. (should
  1611. (eq 'latex-environment
  1612. (org-test-with-temp-text "\\begin{env}{arg}something\\end{env}"
  1613. (org-element-type (org-element-at-point)))))
  1614. ;; Should not allow different tags.
  1615. (should-not
  1616. (eq 'latex-environment
  1617. (org-test-with-temp-text "\\begin{env*}{arg}something\\end{env}"
  1618. (org-element-type (org-element-at-point)))))
  1619. ;; LaTeX environments must be on separate lines.
  1620. (should-not
  1621. (eq 'latex-environment
  1622. (org-test-with-temp-text "\\begin{env} x \\end{env} y"
  1623. (org-element-type (org-element-at-point)))))
  1624. (should-not
  1625. (eq 'latex-environment
  1626. (org-test-with-temp-text "y \\begin{env} x<point> \\end{env}"
  1627. (org-element-type (org-element-at-point)))))
  1628. ;; Handle non-empty blank line at the end of buffer.
  1629. (should
  1630. (org-test-with-temp-text "\\begin{env}\n\\end{env}\n "
  1631. (= (org-element-property :end (org-element-at-point)) (point-max)))))
  1632. ;;;; Latex Fragment
  1633. (ert-deftest test-org-element/latex-fragment-parser ()
  1634. "Test `latex-fragment' parser."
  1635. ;; Basic $...$ test.
  1636. (should
  1637. (eq 'latex-fragment
  1638. (org-test-with-temp-text "$a$"
  1639. (org-element-type (org-element-context)))))
  1640. ;; Test valid characters after $...$ construct.
  1641. (should-not
  1642. (eq 'latex-fragment
  1643. (org-test-with-temp-text "$a$a"
  1644. (org-element-type (org-element-context)))))
  1645. (should
  1646. (eq 'latex-fragment
  1647. (org-test-with-temp-text "$a$!"
  1648. (org-element-type (org-element-context)))))
  1649. (should
  1650. (eq 'latex-fragment
  1651. (org-test-with-temp-text "$a$,"
  1652. (org-element-type (org-element-context)))))
  1653. (should
  1654. (eq 'latex-fragment
  1655. (org-test-with-temp-text "$a$\""
  1656. (org-element-type (org-element-context)))))
  1657. (should
  1658. (eq 'latex-fragment
  1659. (org-test-with-temp-text "$a$)"
  1660. (org-element-type (org-element-context)))))
  1661. (should
  1662. (eq 'latex-fragment
  1663. (org-test-with-temp-text "$a$ "
  1664. (org-element-type (org-element-context)))))
  1665. (should
  1666. (eq 'latex-fragment
  1667. (org-test-with-temp-text "$a$'"
  1668. (org-element-type (org-element-context)))))
  1669. ;; Test forbidden characters inside $...$.
  1670. (should-not
  1671. (eq 'latex-fragment
  1672. (org-test-with-temp-text "$.a$"
  1673. (org-element-type (org-element-context)))))
  1674. (should-not
  1675. (eq 'latex-fragment
  1676. (org-test-with-temp-text "$,a$"
  1677. (org-element-type (org-element-context)))))
  1678. (should-not
  1679. (eq 'latex-fragment
  1680. (org-test-with-temp-text "$;a$"
  1681. (org-element-type (org-element-context)))))
  1682. (should-not
  1683. (eq 'latex-fragment
  1684. (org-test-with-temp-text "$ a$"
  1685. (org-element-type (org-element-context)))))
  1686. (should-not
  1687. (eq 'latex-fragment
  1688. (org-test-with-temp-text "$a.$"
  1689. (org-element-type (org-element-context)))))
  1690. (should-not
  1691. (eq 'latex-fragment
  1692. (org-test-with-temp-text "$a,$"
  1693. (org-element-type (org-element-context)))))
  1694. (should-not
  1695. (eq 'latex-fragment
  1696. (org-test-with-temp-text "$a $"
  1697. (org-element-type (org-element-context)))))
  1698. ;; Test $$...$$.
  1699. (should
  1700. (eq 'latex-fragment
  1701. (org-test-with-temp-text "$$a$$"
  1702. (org-element-type (org-element-context)))))
  1703. ;; Test \(...\).
  1704. (should
  1705. (eq 'latex-fragment
  1706. (org-test-with-temp-text "\\(a\\)"
  1707. (org-element-type (org-element-context)))))
  1708. ;; Test \[...\].
  1709. (should
  1710. (eq 'latex-fragment
  1711. (org-test-with-temp-text "\\[a\\]"
  1712. (org-element-type (org-element-context)))))
  1713. ;; Test fragment at the beginning of an item.
  1714. (should
  1715. (eq 'latex-fragment
  1716. (org-test-with-temp-text "- $<point>x$"
  1717. (org-element-type (org-element-context))))))
  1718. ;;;; Line Break
  1719. (ert-deftest test-org-element/line-break-parser ()
  1720. "Test `line-break' parser."
  1721. ;; Regular test.
  1722. (should
  1723. (org-test-with-temp-text "Text \\\\"
  1724. (org-element-map (org-element-parse-buffer) 'line-break 'identity)))
  1725. ;; Line break with trailing white spaces.
  1726. (should
  1727. (org-test-with-temp-text "Text \\\\ "
  1728. (org-element-map (org-element-parse-buffer) 'line-break 'identity)))
  1729. ;; Three backslashes are too much.
  1730. (should-not
  1731. (org-test-with-temp-text "Text \\\\\\"
  1732. (org-element-map (org-element-parse-buffer) 'line-break 'identity))))
  1733. ;;;; Link
  1734. (ert-deftest test-org-element/link-parser ()
  1735. "Test `link' parser."
  1736. ;; Radio target.
  1737. (should
  1738. (equal
  1739. "radio"
  1740. (org-test-with-temp-text "<<<radio>>>A radio link"
  1741. (org-update-radio-target-regexp)
  1742. (org-element-property
  1743. :type
  1744. (org-element-map (org-element-parse-buffer) 'link #'identity nil t)))))
  1745. ;; Pathological case: radio target of length 1 at beginning of line
  1746. ;; not followed by spaces.
  1747. (should
  1748. (org-test-with-temp-text "* <<<a>>>\n<point>a-bug"
  1749. (org-update-radio-target-regexp)
  1750. (org-element-parse-buffer)))
  1751. ;; Pathological case: radio target in an emphasis environment.
  1752. (should
  1753. (eq 'bold
  1754. (org-test-with-temp-text "* <<<radio>>>\n<point>*radio*"
  1755. (org-update-radio-target-regexp)
  1756. (org-element-type (org-element-context)))))
  1757. (should
  1758. (eq 'link
  1759. (org-test-with-temp-text "* <<<radio>>>\n*<point>radio*"
  1760. (org-update-radio-target-regexp)
  1761. (org-element-type (org-element-context)))))
  1762. ;; Standard link.
  1763. ;;
  1764. ;; ... with description.
  1765. (should
  1766. (equal
  1767. '("Orgmode.org")
  1768. (org-test-with-temp-text "[[https://orgmode.org][Orgmode.org]]"
  1769. (org-element-contents
  1770. (org-element-map (org-element-parse-buffer) 'link 'identity nil t)))))
  1771. ;; ... without description.
  1772. (should
  1773. (equal
  1774. "https"
  1775. (org-test-with-temp-text "[[https://orgmode.org]]"
  1776. (org-element-property
  1777. :type
  1778. (org-element-map (org-element-parse-buffer) 'link 'identity nil t)))))
  1779. ;; ... with expansion.
  1780. (should
  1781. (equal
  1782. "//orgmode.org/worg"
  1783. (org-test-with-temp-text "[[Org:worg]]"
  1784. (let ((org-link-abbrev-alist '(("Org" . "https://orgmode.org/"))))
  1785. (org-element-property
  1786. :path
  1787. (org-element-map (org-element-parse-buffer) 'link 'identity nil t))))))
  1788. ;; ... with translation.
  1789. (should
  1790. (equal
  1791. "127.0.0.1"
  1792. (org-test-with-temp-text "[[https://orgmode.org]]"
  1793. (let ((org-link-translation-function
  1794. (lambda (type _) (cons type "127.0.0.1"))))
  1795. (org-element-property
  1796. :path
  1797. (org-element-map (org-element-parse-buffer) 'link
  1798. #'identity nil t))))))
  1799. ;; ... custom-id link.
  1800. (should
  1801. (equal
  1802. "custom-id"
  1803. (org-test-with-temp-text "[[#some-id]]"
  1804. (org-element-property
  1805. :type
  1806. (org-element-map (org-element-parse-buffer) 'link 'identity nil t)))))
  1807. ;; ... coderef link.
  1808. (should
  1809. (equal
  1810. "coderef"
  1811. (org-test-with-temp-text "[[(reference)]]"
  1812. (org-element-property
  1813. :type
  1814. (org-element-map (org-element-parse-buffer) 'link 'identity nil t)))))
  1815. ;; ... fuzzy link.
  1816. (should
  1817. (equal
  1818. "fuzzy"
  1819. (org-test-with-temp-text "[[target-or-title]]"
  1820. (org-element-property
  1821. :type
  1822. (org-element-map (org-element-parse-buffer) 'link 'identity nil t)))))
  1823. ;; ... file-type link with search option.
  1824. (should
  1825. (equal
  1826. '(("file" "projects.org" "*task title"))
  1827. (org-test-with-temp-text "[[file:projects.org::*task title]]"
  1828. (org-element-map (org-element-parse-buffer) 'link
  1829. (lambda (l) (list (org-element-property :type l)
  1830. (org-element-property :path l)
  1831. (org-element-property :search-option l)))))))
  1832. ;; ... file-type link with application...
  1833. (should
  1834. (equal
  1835. '("file" "projects.org" "emacs")
  1836. (org-test-with-temp-text "[[file+emacs:projects.org]]"
  1837. (let ((l (org-element-context)))
  1838. (list (org-element-property :type l)
  1839. (org-element-property :path l)
  1840. (org-element-property :application l))))))
  1841. ;; ... `:path' in a file-type link must be compatible with "file"
  1842. ;; scheme in URI syntax, even if Org syntax isn't...
  1843. (should
  1844. (org-test-with-temp-text-in-file ""
  1845. (let ((file (expand-file-name (buffer-file-name))))
  1846. (insert (format "[[file://%s]]" file))
  1847. (equal (org-element-property :path (org-element-context)) file))))
  1848. (should
  1849. (org-test-with-temp-text-in-file ""
  1850. (let ((file (expand-file-name (buffer-file-name))))
  1851. (insert (format "[[file:%s]]" file))
  1852. (equal (org-element-property :path (org-element-context)) file))))
  1853. (should
  1854. (org-test-with-temp-text-in-file ""
  1855. (let ((file (expand-file-name (buffer-file-name))))
  1856. (insert (format "[[file:%s]]" file))
  1857. (equal (org-element-property :path (org-element-context)) file))))
  1858. ;; ... multi-line link.
  1859. (should
  1860. (equal "ls *.org"
  1861. (org-test-with-temp-text "[[shell:ls\n*.org]]"
  1862. (org-element-property :path (org-element-context)))))
  1863. ;; Plain link.
  1864. (should
  1865. (org-test-with-temp-text "A link: https://orgmode.org"
  1866. (org-element-map (org-element-parse-buffer) 'link 'identity)))
  1867. ;; Angular link. Follow RFC 3986.
  1868. (should
  1869. (eq 'link
  1870. (org-test-with-temp-text "A link: <point><https://orgmode.org>"
  1871. (org-element-type (org-element-context)))))
  1872. (should
  1873. (equal "//orgmode.org"
  1874. (org-test-with-temp-text "A link: <point><https://orgmode\n.org>"
  1875. (org-element-property :path (org-element-context)))))
  1876. ;; Link abbreviation.
  1877. (should
  1878. (equal "https"
  1879. (org-test-with-temp-text
  1880. "#+LINK: orgmode https://www.orgmode.org/\n[[orgmode:#docs]]"
  1881. (progn (org-mode-restart)
  1882. (goto-char (1- (point-max)))
  1883. (org-element-property :type (org-element-context))))))
  1884. ;; Link abbreviation in a secondary string.
  1885. (should
  1886. (equal "https"
  1887. (org-test-with-temp-text
  1888. "#+LINK: orgmode https://www.orgmode.org/\n* H [[orgmode:#docs]]"
  1889. (progn (org-mode-restart)
  1890. (org-element-map (org-element-parse-buffer) 'link
  1891. (lambda (link) (org-element-property :type link))
  1892. nil t nil t))))))
  1893. ;;;; Macro
  1894. (ert-deftest test-org-element/macro-parser ()
  1895. "Test `macro' parser."
  1896. ;; Without arguments.
  1897. (should
  1898. (org-test-with-temp-text "{{{macro}}}"
  1899. (org-element-map (org-element-parse-buffer) 'macro 'identity)))
  1900. ;; With arguments.
  1901. (should
  1902. (org-test-with-temp-text "{{{macro(arg1,arg2)}}}"
  1903. (org-element-map (org-element-parse-buffer) 'macro 'identity)))
  1904. ;; Properly handle protected commas in arguments...
  1905. (should
  1906. (= 2
  1907. (length
  1908. (org-test-with-temp-text "{{{macro(arg1\\,arg1,arg2)}}}"
  1909. (org-element-property :args (org-element-context))))))
  1910. ;; ... even when last argument ends with a protected comma.
  1911. (should
  1912. (equal '("C-,")
  1913. (org-test-with-temp-text "{{{macro(C-\\,)}}}"
  1914. (org-element-property :args (org-element-context)))))
  1915. ;; Allow to escape escaping character.
  1916. (should
  1917. (equal '("C-\\" "")
  1918. (org-test-with-temp-text "{{{macro(C-\\\\,)}}}"
  1919. (org-element-property :args (org-element-context)))))
  1920. ;; No need to escape backslashes elsewhere.
  1921. (should
  1922. (equal '("\\")
  1923. (org-test-with-temp-text "{{{macro(\\)}}}"
  1924. (org-element-property :args (org-element-context))))))
  1925. ;;;; Node Property
  1926. (ert-deftest test-org-element/node-property ()
  1927. "Test `node-property' parser."
  1928. ;; Standard test.
  1929. (should
  1930. (equal '("abc" "value")
  1931. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:abc: value\n:END:"
  1932. (let ((element (org-element-at-point)))
  1933. (list (org-element-property :key element)
  1934. (org-element-property :value element))))))
  1935. ;; The insides of property blocks on document level are parsed the
  1936. ;; same way as headline property blocks. I.e. the concept of
  1937. ;; `node-property' apply also for properties in those blocks.
  1938. (should
  1939. (equal '("abc" "value")
  1940. (org-test-with-temp-text ":PROPERTIES:\n<point>:abc: value\n:END:"
  1941. (let ((element (org-element-at-point)))
  1942. (list (org-element-property :key element)
  1943. (org-element-property :value element))))))
  1944. ;; Value should be trimmed.
  1945. (should
  1946. (equal "value"
  1947. (org-test-with-temp-text
  1948. "* H\n:PROPERTIES:\n<point>:abc: value \n:END:"
  1949. (org-element-property :value (org-element-at-point)))))
  1950. ;; A node property requires to be wrapped within a property drawer.
  1951. (should-not
  1952. (eq 'node-property
  1953. (org-test-with-temp-text ":abc: value"
  1954. (org-element-type (org-element-at-point)))))
  1955. ;; Accept empty properties.
  1956. (should
  1957. (equal '(("foo" "value") ("bar" ""))
  1958. (org-test-with-temp-text "* H\n:PROPERTIES:\n:foo: value\n:bar:\n:END:"
  1959. (org-element-map (org-element-parse-buffer) 'node-property
  1960. (lambda (p)
  1961. (list (org-element-property :key p)
  1962. (org-element-property :value p))))))))
  1963. ;;;; Paragraph
  1964. (ert-deftest test-org-element/paragraph-parser ()
  1965. "Test `paragraph' parser."
  1966. ;; Standard test.
  1967. (should
  1968. (org-test-with-temp-text "Paragraph"
  1969. (org-element-map (org-element-parse-buffer) 'paragraph 'identity nil t)))
  1970. ;; Property find end of a paragraph stuck to another element.
  1971. (should
  1972. (eq ?#
  1973. (org-test-with-temp-text "Paragraph\n# Comment"
  1974. (org-element-map (org-element-parse-buffer) 'paragraph
  1975. (lambda (p) (char-after (org-element-property :end p)))
  1976. nil t))))
  1977. ;; Include ill-formed Keywords.
  1978. (should
  1979. (org-test-with-temp-text "#+wrong_keyword something"
  1980. (org-element-map (org-element-parse-buffer) 'paragraph 'identity)))
  1981. ;; Include incomplete-drawers.
  1982. (should
  1983. (org-test-with-temp-text ":TEST:\nParagraph"
  1984. (let ((elem (org-element-at-point)))
  1985. (and (eq (org-element-type elem) 'paragraph)
  1986. (= (point-max) (org-element-property :end elem))))))
  1987. ;; Include incomplete blocks.
  1988. (should
  1989. (org-test-with-temp-text "#+BEGIN_CENTER\nParagraph"
  1990. (let ((elem (org-element-at-point)))
  1991. (and (eq (org-element-type elem) 'paragraph)
  1992. (= (point-max) (org-element-property :end elem))))))
  1993. ;; Include incomplete dynamic blocks.
  1994. (should
  1995. (org-test-with-temp-text "#+BEGIN: \n<point>Paragraph"
  1996. (let ((elem (org-element-at-point)))
  1997. (and (eq (org-element-type elem) 'paragraph)
  1998. (= (point-max) (org-element-property :end elem))))))
  1999. ;; Include incomplete latex environments.
  2000. (should
  2001. (org-test-with-temp-text "\begin{equation}\nParagraph"
  2002. (let ((elem (org-element-at-point)))
  2003. (and (eq (org-element-type elem) 'paragraph)
  2004. (= (point-max) (org-element-property :end elem))))))
  2005. (should
  2006. (org-test-with-temp-text "Paragraph\n\begin{equation}"
  2007. (let ((elem (org-element-at-point)))
  2008. (and (eq (org-element-type elem) 'paragraph)
  2009. (= (point-max) (org-element-property :end elem))))))
  2010. ;; Stop at affiliated keywords.
  2011. (should
  2012. (org-test-with-temp-text "Paragraph\n#+NAME: test\n| table |"
  2013. (let ((elem (org-element-at-point)))
  2014. (and (eq (org-element-type elem) 'paragraph)
  2015. (not (org-element-property :name elem))
  2016. (= (org-element-property :end elem) (line-beginning-position 2))))))
  2017. (should
  2018. (org-test-with-temp-text
  2019. "Paragraph\n#+CAPTION[with short caption]: test\n| table |"
  2020. (let ((elem (org-element-at-point)))
  2021. (and (eq (org-element-type elem) 'paragraph)
  2022. (not (org-element-property :name elem))
  2023. (= (org-element-property :end elem) (line-beginning-position 2))))))
  2024. ;; Do not steal affiliated keywords from container.
  2025. (should
  2026. (org-test-with-temp-text "#+ATTR_LATEX: test\n- item<point> 1"
  2027. (let ((elem (org-element-at-point)))
  2028. (and (eq (org-element-type elem) 'paragraph)
  2029. (not (org-element-property :attr_latex elem))
  2030. (/= (org-element-property :begin elem) 1)))))
  2031. ;; Handle non-empty blank line at the end of buffer.
  2032. (should
  2033. (org-test-with-temp-text "#+BEGIN_CENTER\nC\n#+END_CENTER\n "
  2034. (= (org-element-property :end (org-element-at-point)) (point-max))))
  2035. (should
  2036. (org-test-with-temp-text "#+BEGIN_CENTER\n<point>C\n#+END_CENTER\n "
  2037. (= (org-element-property :end (org-element-at-point))
  2038. (save-excursion
  2039. (search-forward "END")
  2040. (line-beginning-position))))))
  2041. ;;;; Plain List
  2042. (ert-deftest test-org-element/plain-list-parser ()
  2043. "Test `plain-list' parser."
  2044. (org-test-with-temp-text "- item"
  2045. (should (org-element-map (org-element-parse-buffer) 'plain-list 'identity)))
  2046. ;; Blank lines after a list or sub-list belongs to that list.
  2047. (should
  2048. (= 1
  2049. (org-test-with-temp-text "- A\n\n- B\n\nEnd list"
  2050. (org-element-property :post-blank (org-element-at-point)))))
  2051. (should
  2052. (= 1
  2053. (org-test-with-temp-text "- A\n\n<point> - B\n\n - C\n\n End sub-list"
  2054. (org-element-property :post-blank (org-element-at-point)))))
  2055. ;; Blank lines after the list only belong to outer plain list,
  2056. ;; however.
  2057. (should
  2058. (equal
  2059. '(t t)
  2060. (org-test-with-temp-text "
  2061. - outer
  2062. - inner
  2063. Outside list"
  2064. (let ((endings (org-element-map (org-element-parse-buffer) 'plain-list
  2065. (lambda (pl) (org-element-property :end pl)))))
  2066. (list
  2067. ;; Move to ending of outer list.
  2068. (progn (goto-char (car endings)) (looking-at "Outside list"))
  2069. ;; Move to ending of inner list.
  2070. (progn (goto-char (nth 1 endings)) (looking-at "^$")))))))
  2071. ;; Correctly compute end of list if it doesn't end at a line
  2072. ;; beginning.
  2073. (should
  2074. (org-test-with-temp-text "- list\n \n "
  2075. (= (org-element-property :end (org-element-at-point)) (point-max))))
  2076. ;; Correctly compute list ending when list is before first headline.
  2077. (dolist (org-element-use-cache '(t nil))
  2078. (org-test-with-temp-text "- list\n* Headline\n"
  2079. (should (= (org-element-property :end (org-element-at-point)) 8)))))
  2080. ;;;; Planning
  2081. (ert-deftest test-org-element/planning-parser ()
  2082. "Test `planning' parser."
  2083. ;; Test various keywords.
  2084. (should
  2085. (org-element-property
  2086. :closed
  2087. (org-test-with-temp-text "* H\n<point>CLOSED: [2012-03-29 thu.]"
  2088. (org-element-at-point))))
  2089. (should
  2090. (org-element-property
  2091. :deadline
  2092. (org-test-with-temp-text "* H\n<point>DEADLINE: <2012-03-29 thu.>"
  2093. (org-element-at-point))))
  2094. (should
  2095. (org-element-property
  2096. :scheduled
  2097. (org-test-with-temp-text "* H\n<point>SCHEDULED: <2012-03-29 thu.>"
  2098. (org-element-at-point))))
  2099. ;; Planning line only exists right after a headline.
  2100. (should-not
  2101. (eq 'planning
  2102. (org-test-with-temp-text "DEADLINE: <2012-03-29 thu.>"
  2103. (org-element-type (org-element-at-point)))))
  2104. (should-not
  2105. (eq 'planning
  2106. (org-test-with-temp-text
  2107. "* H\n# Comment\n<point>DEADLINE: <2012-03-29 thu.>"
  2108. (org-element-type (org-element-at-point)))))
  2109. (should-not
  2110. (eq 'planning
  2111. (org-test-with-temp-text
  2112. "* H\n\n<point>DEADLINE: <2012-03-29 thu.>"
  2113. (org-element-type (org-element-at-point))))))
  2114. ;;;; Property Drawer
  2115. (ert-deftest test-org-element/property-drawer-parser ()
  2116. "Test `property-drawer' parser."
  2117. ;; Standard test.
  2118. (should
  2119. (eq 'property-drawer
  2120. (org-test-with-temp-text "* H\n<point>:PROPERTIES:\n:prop: value\n:END:"
  2121. (org-element-type (org-element-at-point)))))
  2122. (should
  2123. (eq 'property-drawer
  2124. (org-test-with-temp-text
  2125. "* H\nDEADLINE: <2014-03-04 tue.>\n<point>:PROPERTIES:\n:prop: value\n:END:"
  2126. (org-element-type (org-element-at-point)))))
  2127. ;; Parse property drawer at the beginning of the document, possibly
  2128. ;; after some initial comments.
  2129. (should
  2130. (eq 'property-drawer
  2131. (org-test-with-temp-text "<point>:PROPERTIES:\n:prop: value\n:END:"
  2132. (org-element-type (org-element-at-point)))))
  2133. (should
  2134. (eq 'property-drawer
  2135. (org-test-with-temp-text "# C\n# C\n<point>:PROPERTIES:\n:prop: value\n:END:"
  2136. (org-element-type (org-element-at-point)))))
  2137. (should-not
  2138. (eq 'property-drawer
  2139. (org-test-with-temp-text "\n<point>:PROPERTIES:\n:prop: value\n:END:"
  2140. (org-element-type (org-element-at-point)))))
  2141. ;; Allow properties without value and no property at all.
  2142. (should
  2143. (eq 'property-drawer
  2144. (org-test-with-temp-text "* H\n<point>:PROPERTIES:\n:prop:\n:END:"
  2145. (org-element-type (org-element-at-point)))))
  2146. (should
  2147. (eq 'property-drawer
  2148. (org-test-with-temp-text "* H\n<point>:PROPERTIES:\n:END:"
  2149. (org-element-type (org-element-at-point)))))
  2150. ;; Ignore incomplete drawer, drawer at a wrong location or with
  2151. ;; wrong contents.
  2152. (should-not
  2153. (eq 'property-drawer
  2154. (org-test-with-temp-text "* H\n<point>:PROPERTIES:\n:prop: value"
  2155. (org-element-type (org-element-at-point)))))
  2156. (should-not
  2157. (eq 'property-drawer
  2158. (org-test-with-temp-text
  2159. "* H\nParagraph\n<point>:PROPERTIES:\n:prop: value\n:END:"
  2160. (org-element-type (org-element-at-point)))))
  2161. (should-not
  2162. (eq 'property-drawer
  2163. (org-test-with-temp-text
  2164. "* H\nParagraph\n<point>:PROPERTIES:\nparagraph\n:END:"
  2165. (org-element-type (org-element-at-point)))))
  2166. (should-not
  2167. (eq 'property-drawer
  2168. (org-test-with-temp-text
  2169. "* H\n\n<point>:PROPERTIES:\n:prop: value\n:END:"
  2170. (org-element-type (org-element-at-point)))))
  2171. ;; Handle non-empty blank line at the end of buffer.
  2172. (should
  2173. (org-test-with-temp-text "* H\n<point>:PROPERTIES:\n:END:\n "
  2174. (= (org-element-property :end (org-element-at-point)) (point-max)))))
  2175. ;;;; Quote Block
  2176. (ert-deftest test-org-element/quote-block-parser ()
  2177. "Test `quote-block' parser."
  2178. ;; Regular test.
  2179. (should
  2180. (org-test-with-temp-text "#+BEGIN_QUOTE\nText\n#+END_QUOTE"
  2181. (org-element-map (org-element-parse-buffer) 'quote-block 'identity)))
  2182. ;; Ignore incomplete block.
  2183. (should-not
  2184. (org-test-with-temp-text "#+BEGIN_QUOTE"
  2185. (org-element-map (org-element-parse-buffer) 'quote-block 'identity nil t)))
  2186. ;; Handle non-empty blank line at the end of buffer.
  2187. (should
  2188. (org-test-with-temp-text "#+BEGIN_QUOTE\nC\n#+END_QUOTE\n "
  2189. (= (org-element-property :end (org-element-at-point)) (point-max)))))
  2190. ;;;; Radio Target
  2191. (ert-deftest test-org-element/radio-target-parser ()
  2192. "Test `radio-target' parser."
  2193. ;; Standard test.
  2194. (should
  2195. (eq 'radio-target
  2196. (org-test-with-temp-text "<<<radio>>>"
  2197. (org-element-type (org-element-context)))))
  2198. ;; Radio targets with objects.
  2199. (should
  2200. (eq 'radio-target
  2201. (org-test-with-temp-text "<<<radio \\alpha>>>"
  2202. (org-element-type (org-element-context)))))
  2203. ;; Radio targets starting with an object.
  2204. (should
  2205. (eq 'radio-target
  2206. (org-test-with-temp-text "<<<\\alpha radio>>>"
  2207. (org-element-type (org-element-context)))))
  2208. ;; Radio targets cannot begin or end with white space.
  2209. (should-not
  2210. (eq 'radio-target
  2211. (org-test-with-temp-text "<<< radio>>>"
  2212. (org-element-type (org-element-context)))))
  2213. (should-not
  2214. (eq 'radio-target
  2215. (org-test-with-temp-text "<<<radio >>>"
  2216. (org-element-type (org-element-context))))))
  2217. ;;;; Section
  2218. (ert-deftest test-org-element/section-parser ()
  2219. "Test `section' parser."
  2220. ;; Standard test.
  2221. (should
  2222. (org-test-with-temp-text "* Headline\nText"
  2223. (org-element-map (org-element-parse-buffer) 'section 'identity)))
  2224. ;; There's a section before the first headline.
  2225. (should
  2226. (org-test-with-temp-text "Text"
  2227. (org-element-map (org-element-parse-buffer) 'section 'identity)))
  2228. ;; A section cannot be empty.
  2229. (should-not
  2230. (org-test-with-temp-text "* Headline 1\n* Headline 2"
  2231. (org-element-map (org-element-parse-buffer) 'section 'identity)))
  2232. ;; A section doesn't contain sub-trees.
  2233. (should-not
  2234. (org-test-with-temp-text "* Head\nText\n** Sub-Head"
  2235. (org-element-map
  2236. (org-element-map (org-element-parse-buffer) 'section 'identity nil t)
  2237. 'headline 'identity))))
  2238. ;;;; Special Block
  2239. (ert-deftest test-org-element/special-block-parser ()
  2240. "Test `special-block' parser."
  2241. ;; Standard test.
  2242. (should
  2243. (equal "SPECIAL"
  2244. (org-test-with-temp-text "#+BEGIN_SPECIAL\nText\n#+END_SPECIAL"
  2245. (org-element-property :type (org-element-at-point)))))
  2246. ;; Special blocks are case sensitive.
  2247. (should
  2248. (equal "CaSe"
  2249. (org-test-with-temp-text "#+BEGIN_CaSe\nText\n#+END_CaSe"
  2250. (org-element-property :type (org-element-at-point)))))
  2251. ;; Special blocks can contain paragraphs.
  2252. (should
  2253. (eq 'paragraph
  2254. (org-test-with-temp-text "#+BEGIN_SPECIAL\nText\n#+END_SPECIAL"
  2255. (forward-line)
  2256. (org-element-type (org-element-at-point)))))
  2257. ;; Ignore incomplete block.
  2258. (should-not
  2259. (eq 'special-block
  2260. (org-test-with-temp-text "#+BEGIN_SPECIAL"
  2261. (org-element-type (org-element-at-point)))))
  2262. ;; Allow special characters in type.
  2263. (should
  2264. (equal '(special-block "SPECIAL*")
  2265. (org-test-with-temp-text "#+BEGIN_SPECIAL*\nContents\n#+END_SPECIAL*"
  2266. (let ((element (org-element-at-point)))
  2267. (list (org-element-type element)
  2268. (org-element-property :type element))))))
  2269. ;; Handle non-empty blank line at the end of buffer.
  2270. (should
  2271. (org-test-with-temp-text "#+BEGIN_SPECIAL\nC\n#+END_SPECIAL\n "
  2272. (= (org-element-property :end (org-element-at-point)) (point-max)))))
  2273. ;;;; Src Block
  2274. (ert-deftest test-org-element/src-block-parser ()
  2275. "Test `src-block' parser."
  2276. ;; Regular tests.
  2277. (should
  2278. (org-test-with-temp-text "#+BEGIN_SRC org\nText\n#+END_SRC"
  2279. (org-element-map (org-element-parse-buffer) 'src-block 'identity)))
  2280. ;; Ignore incomplete block.
  2281. (should-not
  2282. (org-test-with-temp-text "#+BEGIN_SRC"
  2283. (org-element-map (org-element-parse-buffer) 'src-block 'identity)))
  2284. ;; Properly un-escape code.
  2285. (should
  2286. (equal "* Headline\n #+keyword\nText\n"
  2287. (org-test-with-temp-text
  2288. "#+BEGIN_SRC org\n,* Headline\n ,#+keyword\nText\n#+END_SRC"
  2289. (org-element-property :value (org-element-at-point)))))
  2290. ;; Handle non-empty blank line at the end of buffer.
  2291. (should
  2292. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\nC\n#+END_SRC\n "
  2293. (= (org-element-property :end (org-element-at-point)) (point-max)))))
  2294. ;;;; Statistics Cookie
  2295. (ert-deftest test-org-element/statistics-cookie ()
  2296. "Test `statistics-cookie' parser."
  2297. ;; With numbers.
  2298. (should
  2299. (org-test-with-temp-text "[1/2]"
  2300. (org-element-map (org-element-parse-buffer) 'statistics-cookie 'identity)))
  2301. ;; With percents.
  2302. (should
  2303. (org-test-with-temp-text "[33%]"
  2304. (org-element-map
  2305. (org-element-parse-buffer) 'statistics-cookie 'identity))))
  2306. ;;;; Strike Through
  2307. (ert-deftest test-org-element/strike-through-parser ()
  2308. "Test `strike-through' parser."
  2309. ;; Regular test.
  2310. (should
  2311. (org-test-with-temp-text "+strike-through+"
  2312. (org-element-map (org-element-parse-buffer) 'strike-through #'identity)))
  2313. ;; Multi-line markup.
  2314. (should
  2315. (equal
  2316. (org-element-contents
  2317. (org-test-with-temp-text "+first line\nsecond line+"
  2318. (org-element-map
  2319. (org-element-parse-buffer) 'strike-through #'identity nil t)))
  2320. '("first line\nsecond line"))))
  2321. ;;;; Subscript
  2322. (ert-deftest test-org-element/subscript-parser ()
  2323. "Test `subscript' parser."
  2324. ;; Without braces.
  2325. (should
  2326. (org-test-with-temp-text "a_b"
  2327. (org-element-map (org-element-parse-buffer) 'subscript 'identity)))
  2328. ;; With braces.
  2329. (should
  2330. (org-test-with-temp-text "a_{b}"
  2331. (org-element-map (org-element-parse-buffer) 'subscript 'identity)))
  2332. ;; Multiple subscripts in a paragraph.
  2333. (should
  2334. (= 2
  2335. (org-test-with-temp-text "a_b and c_d"
  2336. (length
  2337. (org-element-map (org-element-parse-buffer) 'subscript 'identity))))))
  2338. ;;;; Superscript
  2339. (ert-deftest test-org-element/superscript-parser ()
  2340. "Test `superscript' parser."
  2341. ;; Without braces.
  2342. (should
  2343. (org-test-with-temp-text "a^b"
  2344. (org-element-map (org-element-parse-buffer) 'superscript 'identity)))
  2345. ;; With braces.
  2346. (should
  2347. (org-test-with-temp-text "a^{b}"
  2348. (org-element-map (org-element-parse-buffer) 'superscript 'identity)))
  2349. ;; Multiple superscript in a paragraph.
  2350. (should
  2351. (= 2
  2352. (org-test-with-temp-text "a^b and c^d"
  2353. (length
  2354. (org-element-map
  2355. (org-element-parse-buffer) 'superscript 'identity))))))
  2356. ;;;; Table
  2357. (ert-deftest test-org-element/table-parser ()
  2358. "Test `table' parser."
  2359. (should
  2360. (org-test-with-temp-text "| a |"
  2361. (org-element-map (org-element-parse-buffer) 'table 'identity)))
  2362. ;; TBLFM keyword is case insensitive.
  2363. (should
  2364. (org-test-with-temp-text "| a |\n#+tblfm: test"
  2365. (org-element-property
  2366. :tblfm
  2367. (org-element-map (org-element-parse-buffer) 'table 'identity nil t))))
  2368. ;; Handle multiple TBLFM lines.
  2369. (should
  2370. (= 2
  2371. (org-test-with-temp-text "| a |\n#+TBLFM: test1\n#+TBLFM: test2"
  2372. (length (org-element-property
  2373. :tblfm
  2374. (org-element-map
  2375. (org-element-parse-buffer) 'table 'identity nil t))))))
  2376. ;; Handle non-empty blank line at the end of buffer.
  2377. (should
  2378. (org-test-with-temp-text "| a |\n "
  2379. (= (org-element-property :end (org-element-at-point)) (point-max)))))
  2380. ;;;; Table Cell
  2381. (ert-deftest test-org-element/table-cell-parser ()
  2382. "Test `table-cell' parser."
  2383. ;; Regular table cell.
  2384. (should
  2385. (org-test-with-temp-text "| a |"
  2386. (org-element-map (org-element-parse-buffer) 'table-cell 'identity)))
  2387. ;; Last vertical bar may be omitted.
  2388. (should
  2389. (org-test-with-temp-text "| a "
  2390. (org-element-map (org-element-parse-buffer) 'table-cell 'identity))))
  2391. ;;;; Table Row
  2392. (ert-deftest test-org-element/table-row-parser ()
  2393. "Test `table-row' parser."
  2394. (should
  2395. (equal '(standard rule)
  2396. (org-test-with-temp-text "| a |\n|---|"
  2397. (org-element-map
  2398. (org-element-parse-buffer) 'table-row
  2399. (lambda (row) (org-element-property :type row)))))))
  2400. ;;;; Target
  2401. (ert-deftest test-org-element/target-parser ()
  2402. "Test `target' parser."
  2403. (should
  2404. (org-test-with-temp-text "<<target>>"
  2405. (org-element-map (org-element-parse-buffer) 'target 'identity))))
  2406. ;;;; Timestamp
  2407. (ert-deftest test-org-element/timestamp-parser ()
  2408. "Test `timestamp' parser."
  2409. ;; Active timestamp.
  2410. (should
  2411. (org-test-with-temp-text "<2012-03-29 16:40>"
  2412. (eq (org-element-property :type (org-element-context)) 'active)))
  2413. (should-not
  2414. (org-test-with-temp-text "<2012-03-29 Thu>"
  2415. (let ((timestamp (org-element-context)))
  2416. (or (org-element-property :hour-start timestamp)
  2417. (org-element-property :minute-start timestamp)))))
  2418. (should
  2419. (equal '(2012 3 29 16 40)
  2420. (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
  2421. (let ((object (org-element-context)))
  2422. (list (org-element-property :year-start object)
  2423. (org-element-property :month-start object)
  2424. (org-element-property :day-start object)
  2425. (org-element-property :hour-start object)
  2426. (org-element-property :minute-start object))))))
  2427. ;; Inactive timestamp.
  2428. (should
  2429. (org-test-with-temp-text "[2012-03-29 Thu 16:40]"
  2430. (eq (org-element-property :type (org-element-context)) 'inactive)))
  2431. ;; Time range.
  2432. (should
  2433. (equal '(2012 3 29 16 40 7 30)
  2434. (org-test-with-temp-text "<2012-03-29 Thu 7:30-16:40>"
  2435. (let ((object (org-element-context)))
  2436. (list (org-element-property :year-end object)
  2437. (org-element-property :month-end object)
  2438. (org-element-property :day-end object)
  2439. (org-element-property :hour-end object)
  2440. (org-element-property :minute-end object)
  2441. (org-element-property :hour-start object)
  2442. (org-element-property :minute-start object))))))
  2443. (should
  2444. (eq 'active-range
  2445. (org-test-with-temp-text "<2012-03-29 Thu 7:30-16:40>"
  2446. (org-element-property :type (org-element-context)))))
  2447. ;; Date range.
  2448. (should
  2449. (org-test-with-temp-text "[2012-03-29 Thu 16:40]--[2012-03-29 Thu 16:41]"
  2450. (eq (org-element-property :type (org-element-context)) 'inactive-range)))
  2451. (should-not
  2452. (org-test-with-temp-text "[2011-07-14 Thu]--[2012-03-29 Thu]"
  2453. (let ((timestamp (org-element-context)))
  2454. (or (org-element-property :hour-end timestamp)
  2455. (org-element-property :minute-end timestamp)))))
  2456. ;; With repeater, warning delay and both.
  2457. (should
  2458. (eq 'catch-up
  2459. (org-test-with-temp-text "<2012-03-29 Thu ++1y>"
  2460. (org-element-property :repeater-type (org-element-context)))))
  2461. (should
  2462. (eq 'first
  2463. (org-test-with-temp-text "<2012-03-29 Thu --1y>"
  2464. (org-element-property :warning-type (org-element-context)))))
  2465. (should
  2466. (equal '(cumulate all)
  2467. (org-test-with-temp-text "<2012-03-29 Thu +1y -1y>"
  2468. (let ((ts (org-element-context)))
  2469. (list (org-element-property :repeater-type ts)
  2470. (org-element-property :warning-type ts)))))))
  2471. ;;;; Underline
  2472. (ert-deftest test-org-element/underline-parser ()
  2473. "Test `underline' parser."
  2474. ;; Regular test.
  2475. (should
  2476. (org-test-with-temp-text "_underline_"
  2477. (org-element-map (org-element-parse-buffer) 'underline #'identity)))
  2478. ;; Multi-line markup.
  2479. (should
  2480. (equal
  2481. (org-element-contents
  2482. (org-test-with-temp-text "_first line\nsecond line_"
  2483. (org-element-map
  2484. (org-element-parse-buffer) 'underline #'identity nil t)))
  2485. '("first line\nsecond line"))))
  2486. ;;;; Verbatim
  2487. (ert-deftest test-org-element/verbatim-parser ()
  2488. "Test `verbatim' parser."
  2489. ;; Regular test.
  2490. (should
  2491. (org-test-with-temp-text "=verbatim="
  2492. (org-element-map (org-element-parse-buffer) 'verbatim #'identity)))
  2493. ;; Multi-line markup.
  2494. (should
  2495. (equal
  2496. (org-element-property
  2497. :value
  2498. (org-test-with-temp-text "=first line\nsecond line="
  2499. (org-element-map (org-element-parse-buffer) 'verbatim #'identity nil t)))
  2500. "first line\nsecond line")))
  2501. ;;;; Verse Block
  2502. (ert-deftest test-org-element/verse-block-parser ()
  2503. "Test `verse-block' parser."
  2504. ;; Standard test.
  2505. (should
  2506. (org-test-with-temp-text "#+BEGIN_VERSE\nVerse block\n#+END_VERSE"
  2507. (org-element-map (org-element-parse-buffer) 'verse-block 'identity)))
  2508. ;; Ignore case.
  2509. (should
  2510. (org-test-with-temp-text "#+begin_verse\nVerse block\n#+end_verse"
  2511. (org-element-map (org-element-parse-buffer) 'verse-block 'identity)))
  2512. ;; Parse objects in verse blocks.
  2513. (should
  2514. (org-test-with-temp-text "#+BEGIN_VERSE\nVerse \\alpha\n#+END_VERSE"
  2515. (org-element-map (org-element-parse-buffer) 'entity 'identity)))
  2516. ;; Ignore incomplete verse block.
  2517. (should-not
  2518. (org-test-with-temp-text "#+BEGIN_VERSE"
  2519. (org-element-map (org-element-parse-buffer) 'verse-block 'identity nil t)))
  2520. ;; Handle non-empty blank line at the end of buffer.
  2521. (should
  2522. (org-test-with-temp-text "#+BEGIN_VERSE\nC\n#+END_VERSE\n "
  2523. (= (org-element-property :end (org-element-at-point)) (point-max)))))
  2524. ;;; Test Interpreters.
  2525. (ert-deftest test-org-element/interpret-data ()
  2526. "Test `org-element-interpret-data' specifications."
  2527. ;; Interpret simple affiliated keywords.
  2528. (should
  2529. (equal
  2530. (org-element-interpret-data
  2531. '(org-data nil (paragraph (:name "para") "Paragraph")))
  2532. "#+name: para\nParagraph\n"))
  2533. ;; Interpret multiple affiliated keywords.
  2534. (should
  2535. (equal
  2536. (org-element-interpret-data
  2537. '(org-data nil (paragraph (:attr_ascii ("line2" "line1")) "Paragraph")))
  2538. "#+attr_ascii: line1\n#+attr_ascii: line2\nParagraph\n"))
  2539. ;; Interpret parsed affiliated keywords.
  2540. (should
  2541. (equal
  2542. (org-element-interpret-data
  2543. '(org-data nil (paragraph (:caption (("caption"))) "Paragraph")))
  2544. "#+caption: caption\nParagraph\n"))
  2545. ;; Interpret dual affiliated keywords.
  2546. (should
  2547. (equal
  2548. (org-element-interpret-data
  2549. '(org-data nil (paragraph (:caption ((("long") "short"))) "Paragraph")))
  2550. "#+caption[short]: long\nParagraph\n"))
  2551. ;; Interpret multiple parsed dual keywords.
  2552. (should
  2553. (equal
  2554. (org-element-interpret-data
  2555. '(org-data nil (paragraph
  2556. (:caption ((("l2") "s2") (("l1") "s1"))) "Paragraph")))
  2557. "#+caption[s1]: l1\n#+caption[s2]: l2\nParagraph\n"))
  2558. ;; Pseudo objects and elements are transparent.
  2559. (should
  2560. (equal "A B"
  2561. (org-trim
  2562. (org-element-interpret-data
  2563. '(paragraph nil (pseudo-object (:post-blank 1) "A") "B")))))
  2564. (should
  2565. (equal "A\n\nB\n"
  2566. (org-element-interpret-data
  2567. '(center nil
  2568. (pseudo-element (:post-blank 1) (paragraph nil "A"))
  2569. (paragraph nil "B")))))
  2570. ;; Obey post-blank property in strings.
  2571. (should
  2572. (equal "A "
  2573. (org-element-interpret-data
  2574. (org-element-put-property "A" :post-blank 1)))))
  2575. (ert-deftest test-org-element/center-block-interpreter ()
  2576. "Test center block interpreter."
  2577. (should
  2578. (equal (org-test-parse-and-interpret "#+BEGIN_CENTER\nTest\n#+END_CENTER")
  2579. "#+begin_center\nTest\n#+end_center\n")))
  2580. (ert-deftest test-org-element/drawer-interpreter ()
  2581. "Test drawer interpreter."
  2582. (should
  2583. (equal (org-test-parse-and-interpret ":TEST:\nTest\n:END:")
  2584. ":TEST:\nTest\n:END:\n")))
  2585. (ert-deftest test-org-element/dynamic-block-interpreter ()
  2586. "Test dynamic block interpreter."
  2587. (should
  2588. (equal (org-test-parse-and-interpret
  2589. "#+BEGIN: myblock :parameter value1\nTest\n#+END:")
  2590. "#+begin: myblock :parameter value1\nTest\n#+end:\n")))
  2591. (ert-deftest test-org-element/footnote-definition-interpreter ()
  2592. "Test footnote definition interpreter."
  2593. (should (equal (org-test-parse-and-interpret "[fn:1] Test") "[fn:1] Test\n"))
  2594. ;; Handle `:pre-blank' in definitions.
  2595. (should
  2596. (equal (org-test-parse-and-interpret "[fn:1]\nparagraph")
  2597. "[fn:1]\nparagraph\n"))
  2598. (should
  2599. (equal (org-test-parse-and-interpret "[fn:1]\n\nparagraph")
  2600. "[fn:1]\n\nparagraph\n")))
  2601. (ert-deftest test-org-element/headline-interpreter ()
  2602. "Test headline and section interpreters."
  2603. ;; 1. Standard test.
  2604. (should (equal (org-test-parse-and-interpret "* Headline") "* Headline\n"))
  2605. ;; 2. With TODO keywords.
  2606. (should
  2607. (equal (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  2608. (org-test-parse-and-interpret "* TODO Headline"))
  2609. "* TODO Headline\n"))
  2610. ;; 3. With tags...
  2611. ;;
  2612. ;; 3.1. ... and a positive `org-tags-column' value.
  2613. (should
  2614. (equal (let ((org-tags-column 20))
  2615. (org-test-parse-and-interpret "* Headline :tag:"))
  2616. "* Headline :tag:\n"))
  2617. ;; 3.2. ... and a negative `org-tags-column' value.
  2618. (should
  2619. (equal (let ((org-tags-column -20))
  2620. (org-test-parse-and-interpret "* Headline :tag:"))
  2621. "* Headline :tag:\n"))
  2622. ;; 3.3. ... and a null `org-tags-column' value.
  2623. (should
  2624. (equal (let ((org-tags-column 0))
  2625. (org-test-parse-and-interpret "* Headline :tag:"))
  2626. "* Headline :tag:\n"))
  2627. ;; 4. With priority cookie.
  2628. (should
  2629. (equal (org-test-parse-and-interpret "* [#B] Headline")
  2630. "* [#B] Headline\n"))
  2631. ;; 5. With comment keyword.
  2632. (should
  2633. (equal (let ((org-comment-string "COMMENT"))
  2634. (org-test-parse-and-interpret "* COMMENT Headline"))
  2635. "* COMMENT Headline\n"))
  2636. ;; 6. Keep same number of blank lines before body.
  2637. (should
  2638. (equal (org-test-parse-and-interpret
  2639. "* Headline\n\n\nText after two blank lines.")
  2640. "* Headline\n\n\nText after two blank lines.\n"))
  2641. ;; 8. Preserve `org-odd-levels-only' state.
  2642. (should
  2643. (equal "* H\n*** H2\n"
  2644. (let ((org-odd-levels-only t))
  2645. (org-test-parse-and-interpret "* H\n*** H2")))))
  2646. (ert-deftest test-org-element/inlinetask-interpreter ()
  2647. "Test inlinetask interpretation."
  2648. (when (featurep 'org-inlinetask)
  2649. (let ((org-inlinetask-min-level 15))
  2650. ;; 1. Regular inlinetask.
  2651. (should (equal (org-test-parse-and-interpret
  2652. "*************** Task\nTest\n*************** END")
  2653. "*************** Task\nTest\n*************** end\n"))
  2654. ;; 2. Degenerate inlinetask.
  2655. (should (equal (org-test-parse-and-interpret "*************** Task")
  2656. "*************** Task\n"))
  2657. ;; 3. Prefer degenerate form when there are no contents.
  2658. (should (equal (org-test-parse-and-interpret
  2659. "*************** Task\n*************** end")
  2660. "*************** Task\n"))
  2661. ;; 4. With TODO keywords.
  2662. (should
  2663. (equal (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  2664. (org-test-parse-and-interpret "*************** TODO Task"))
  2665. "*************** TODO Task\n"))
  2666. ;; 5. With tags...
  2667. ;;
  2668. ;; 5.1. ... and a positive `org-tags-column' value.
  2669. (should
  2670. (equal (let ((org-tags-column 30))
  2671. (org-test-parse-and-interpret "*************** Task :tag:"))
  2672. "*************** Task :tag:\n"))
  2673. ;; 5.2. ... and a negative `org-tags-column' value.
  2674. (should
  2675. (equal (let ((org-tags-column -30))
  2676. (org-test-parse-and-interpret "*************** Task :tag:"))
  2677. "*************** Task :tag:\n"))
  2678. ;; 5.3. ... and a null `org-tags-column' value.
  2679. (should
  2680. (equal (let ((org-tags-column 0))
  2681. (org-test-parse-and-interpret "*************** Task :tag:"))
  2682. "*************** Task :tag:\n"))
  2683. ;; 6. With priority cookie.
  2684. (should
  2685. (equal (org-test-parse-and-interpret "*************** [#B] Task")
  2686. "*************** [#B] Task\n")))))
  2687. (ert-deftest test-org-element/plain-list-interpreter ()
  2688. "Test plain-list and item interpreters."
  2689. (let ((org-list-two-spaces-after-bullet-regexp nil))
  2690. ;; Unordered list.
  2691. (should (equal (org-test-parse-and-interpret "- item 1") "- item 1\n"))
  2692. ;; Description list.
  2693. (should
  2694. (equal (org-test-parse-and-interpret "- tag :: desc") "- tag :: desc\n"))
  2695. ;; Ordered list.
  2696. (should
  2697. (equal (let ((org-plain-list-ordered-item-terminator t))
  2698. (org-test-parse-and-interpret "1. Item"))
  2699. "1. Item\n"))
  2700. (should
  2701. (equal (let ((org-plain-list-ordered-item-terminator ?\)))
  2702. (org-test-parse-and-interpret "1) Item"))
  2703. "1) Item\n"))
  2704. ;; Ordered list with counter.
  2705. (should
  2706. (equal (let ((org-plain-list-ordered-item-terminator t))
  2707. (org-test-parse-and-interpret "1. [@5] Item"))
  2708. "5. [@5] Item\n"))
  2709. ;; List with check-boxes.
  2710. (should
  2711. (equal (org-test-parse-and-interpret
  2712. "- [-] Item 1\n - [X] Item 2\n - [ ] Item 3")
  2713. "- [-] Item 1\n - [X] Item 2\n - [ ] Item 3\n"))
  2714. ;; Item not starting with a paragraph.
  2715. (should
  2716. (equal (org-test-parse-and-interpret "-\n | a | b |")
  2717. "- \n | a | b |\n"))
  2718. ;; Handle `:pre-blank' in items.
  2719. (should
  2720. (equal (org-test-parse-and-interpret "-\n paragraph")
  2721. "- \n paragraph\n"))
  2722. (should
  2723. (equal (org-test-parse-and-interpret "-\n\n paragraph")
  2724. "- \n\n paragraph\n"))
  2725. ;; Special case: correctly handle "*" bullets.
  2726. (should (org-test-parse-and-interpret " * item"))
  2727. ;; Special case: correctly handle empty items.
  2728. (should (org-test-parse-and-interpret "-"))))
  2729. (ert-deftest test-org-element/quote-block-interpreter ()
  2730. "Test quote block interpreter."
  2731. (should (equal (org-test-parse-and-interpret
  2732. "#+BEGIN_QUOTE\nTest\n#+END_QUOTE")
  2733. "#+begin_quote\nTest\n#+end_quote\n")))
  2734. (ert-deftest test-org-element/special-block-interpreter ()
  2735. "Test special block interpreter."
  2736. (should (equal (org-test-parse-and-interpret
  2737. "#+BEGIN_SPECIAL\nTest\n#+END_SPECIAL")
  2738. "#+begin_SPECIAL\nTest\n#+end_SPECIAL\n")))
  2739. (ert-deftest test-org-element/babel-call-interpreter ()
  2740. "Test Babel call interpreter."
  2741. ;; Without argument.
  2742. (should (equal (org-test-parse-and-interpret "#+CALL: test()")
  2743. "#+call: test()\n"))
  2744. ;; With argument.
  2745. (should (equal (org-test-parse-and-interpret "#+CALL: test(x=2)")
  2746. "#+call: test(x=2)\n"))
  2747. ;; With header arguments.
  2748. (should (equal (org-test-parse-and-interpret
  2749. "#+CALL: test[:results output]() :results html")
  2750. "#+call: test[:results output]() :results html\n")))
  2751. (ert-deftest test-org-element/clock-interpreter ()
  2752. "Test clock interpreter."
  2753. ;; Running clock.
  2754. (should
  2755. (string-match
  2756. "CLOCK: \\[2012-01-01 .* 00:01\\]"
  2757. (org-test-parse-and-interpret "CLOCK: [2012-01-01 sun. 00:01]")))
  2758. ;; Closed clock.
  2759. (should
  2760. (string-match
  2761. "CLOCK: \\[2012-01-01 .* 00:01\\]--\\[2012-01-01 .* 00:02\\] => 0:01"
  2762. (org-test-parse-and-interpret "
  2763. CLOCK: [2012-01-01 sun. 00:01]--[2012-01-01 sun. 00:02] => 0:01"))))
  2764. (ert-deftest test-org-element/comment-interpreter ()
  2765. "Test comment interpreter."
  2766. ;; Regular comment.
  2767. (should (equal (org-test-parse-and-interpret "# Comment") "# Comment\n"))
  2768. ;; Inline comment.
  2769. (should (equal (org-test-parse-and-interpret " # Comment")
  2770. "# Comment\n"))
  2771. ;; Preserve indentation.
  2772. (should (equal (org-test-parse-and-interpret " # No blank\n# One blank")
  2773. "# No blank\n# One blank\n")))
  2774. (ert-deftest test-org-element/comment-block-interpreter ()
  2775. "Test comment block interpreter."
  2776. (should (equal (org-test-parse-and-interpret
  2777. "#+BEGIN_COMMENT\nTest\n#+END_COMMENT")
  2778. "#+begin_comment\nTest\n#+end_comment\n"))
  2779. ;; Accept missing final newline in value.
  2780. (should
  2781. (equal "#+begin_comment\nTest\n#+end_comment\n"
  2782. (org-element-interpret-data '(comment-block (:value "Test"))))))
  2783. (ert-deftest test-org-element/diary-sexp ()
  2784. "Test diary-sexp interpreter."
  2785. (should
  2786. (equal
  2787. (org-test-parse-and-interpret
  2788. "%%(org-anniversary 1956 5 14)(2) Arthur Dent is %d years old")
  2789. "%%(org-anniversary 1956 5 14)(2) Arthur Dent is %d years old\n")))
  2790. (ert-deftest test-org-element/example-block-interpreter ()
  2791. "Test example block interpreter."
  2792. ;; Without switches.
  2793. (should (equal "#+begin_example\nTest\n#+end_example\n"
  2794. (let ((org-src-preserve-indentation t))
  2795. (org-test-parse-and-interpret
  2796. "#+BEGIN_EXAMPLE\nTest\n#+END_EXAMPLE"))))
  2797. ;; With switches.
  2798. (should
  2799. (equal "#+begin_example -n -k\n(+ 1 1)\n#+end_example\n"
  2800. (let ((org-src-preserve-indentation t))
  2801. (org-test-parse-and-interpret
  2802. "#+BEGIN_EXAMPLE -n -k\n(+ 1 1)\n#+END_EXAMPLE"))))
  2803. ;; Preserve code escaping.
  2804. (should
  2805. (equal
  2806. (let ((org-src-preserve-indentation t))
  2807. (org-test-parse-and-interpret
  2808. "#+BEGIN_EXAMPLE\n,* Headline\n,#+KEYWORD: value\nText\n#+END_EXAMPLE"))
  2809. "#+begin_example\n,* Headline\n,#+KEYWORD: value\nText\n#+end_example\n"))
  2810. ;; Accept missing final newline in value.
  2811. (should
  2812. (equal
  2813. "#+begin_example\nTest\n#+end_example\n"
  2814. (let ((org-src-preserve-indentation t))
  2815. (org-element-interpret-data '(example-block (:value "Test"))))))
  2816. ;; Handle indentation.
  2817. (should (equal "#+begin_example\n Test\n#+end_example\n"
  2818. (let ((org-src-preserve-indentation nil)
  2819. (org-edit-src-content-indentation 2))
  2820. (org-test-parse-and-interpret
  2821. "#+BEGIN_EXAMPLE\nTest\n#+END_EXAMPLE"))))
  2822. (should (equal "#+begin_example\n Test\n#+end_example\n"
  2823. (let ((org-src-preserve-indentation t)
  2824. (org-edit-src-content-indentation 2))
  2825. (org-test-parse-and-interpret
  2826. "#+BEGIN_EXAMPLE\n Test\n#+END_EXAMPLE")))))
  2827. (ert-deftest test-org-element/export-block-interpreter ()
  2828. "Test export block interpreter."
  2829. (should (equal (org-test-parse-and-interpret
  2830. "#+begin_export HTML\nTest\n#+end_export")
  2831. "#+begin_export HTML\nTest\n#+end_export\n")))
  2832. (ert-deftest test-org-element/fixed-width-interpreter ()
  2833. "Test fixed width interpreter."
  2834. ;; Standard test.
  2835. (should (equal (org-test-parse-and-interpret ": Test") ": Test\n"))
  2836. ;; Preserve indentation.
  2837. (should (equal (org-test-parse-and-interpret ": 2 blanks\n: 1 blank")
  2838. ": 2 blanks\n: 1 blank\n"))
  2839. ;; Handle empty string.
  2840. (should
  2841. (equal (org-element-fixed-width-interpreter
  2842. '(fixed-width (:value "")) nil)
  2843. ""))
  2844. ;; Handle nil value.
  2845. (should-not
  2846. (org-element-fixed-width-interpreter
  2847. '(fixed-width (:value nil)) nil)))
  2848. (ert-deftest test-org-element/horizontal-rule-interpreter ()
  2849. "Test horizontal rule interpreter."
  2850. (should (equal (org-test-parse-and-interpret "-------") "-----\n")))
  2851. (ert-deftest test-org-element/keyword-interpreter ()
  2852. "Test keyword interpreter."
  2853. (should (equal (org-test-parse-and-interpret "#+KEYWORD: value")
  2854. "#+keyword: value\n")))
  2855. (ert-deftest test-org-element/latex-environment-interpreter ()
  2856. "Test latex environment interpreter."
  2857. (should (equal (org-test-parse-and-interpret
  2858. "\\begin{equation}\n1+1=2\n\\end{equation}")
  2859. "\\begin{equation}\n1+1=2\n\\end{equation}\n"))
  2860. (should (equal (org-test-parse-and-interpret
  2861. "\\begin{theorem}[me]\n1+1=2\n\\end{theorem}")
  2862. "\\begin{theorem}[me]\n1+1=2\n\\end{theorem}\n")))
  2863. (ert-deftest test-org-element/planning-interpreter ()
  2864. "Test planning interpreter."
  2865. (should
  2866. (string-match
  2867. "\\* Headline
  2868. DEADLINE: <2012-03-29 .*?> SCHEDULED: <2012-03-29 .*?> CLOSED: \\[2012-03-29 .*?\\]"
  2869. (org-test-parse-and-interpret
  2870. "* Headline
  2871. DEADLINE: <2012-03-29 thu.> SCHEDULED: <2012-03-29 thu.> CLOSED: [2012-03-29 thu.]"))))
  2872. (ert-deftest test-org-element/property-drawer-interpreter ()
  2873. "Test property drawer interpreter."
  2874. (should (equal (let ((org-property-format "%-10s %s"))
  2875. (org-test-parse-and-interpret
  2876. "* H\n:PROPERTIES:\n:prop: value\n:END:"))
  2877. "* H\n:PROPERTIES:\n:prop: value\n:END:\n")))
  2878. (ert-deftest test-org-element/src-block-interpreter ()
  2879. "Test src block interpreter."
  2880. ;; With arguments.
  2881. (should
  2882. (equal (let ((org-edit-src-content-indentation 2)
  2883. (org-src-preserve-indentation nil))
  2884. (org-test-parse-and-interpret
  2885. "#+BEGIN_SRC emacs-lisp :results silent\n(+ 1 1)\n#+END_SRC"))
  2886. "#+begin_src emacs-lisp :results silent\n (+ 1 1)\n#+end_src\n"))
  2887. ;; With switches.
  2888. (should
  2889. (equal (let ((org-edit-src-content-indentation 2)
  2890. (org-src-preserve-indentation nil))
  2891. (org-test-parse-and-interpret
  2892. "#+BEGIN_SRC emacs-lisp -n -k\n(+ 1 1)\n#+END_SRC"))
  2893. "#+begin_src emacs-lisp -n -k\n (+ 1 1)\n#+end_src\n"))
  2894. ;; Preserve code escaping.
  2895. (should
  2896. (equal
  2897. (let ((org-edit-src-content-indentation 2)
  2898. (org-src-preserve-indentation nil))
  2899. (org-test-parse-and-interpret
  2900. "#+BEGIN_SRC org\n,* Headline\n,#+KEYWORD: value\nText\n#+END_SRC"))
  2901. "#+begin_src org\n ,* Headline\n ,#+KEYWORD: value\n Text\n#+end_src\n"))
  2902. ;; Do not apply `org-edit-src-content-indentation' when preserving
  2903. ;; indentation.
  2904. (should
  2905. (equal (let ((org-edit-src-content-indentation 2)
  2906. (org-src-preserve-indentation t))
  2907. (org-test-parse-and-interpret
  2908. "#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"))
  2909. "#+begin_src emacs-lisp\n(+ 1 1)\n#+end_src\n"))
  2910. (should
  2911. (equal (let ((org-edit-src-content-indentation 2)
  2912. (org-src-preserve-indentation nil))
  2913. (org-test-parse-and-interpret
  2914. "#+BEGIN_SRC emacs-lisp -i\n(+ 1 1)\n#+END_SRC"))
  2915. "#+begin_src emacs-lisp -i\n(+ 1 1)\n#+end_src\n"))
  2916. ;; Accept missing final newline in value.
  2917. (should
  2918. (equal
  2919. "#+begin_src emacs-lisp\n Test\n#+end_src\n"
  2920. (let ((org-edit-src-content-indentation 2)
  2921. (org-src-preserve-indentation nil))
  2922. (org-element-interpret-data
  2923. '(src-block (:language "emacs-lisp" :value "Test")))))))
  2924. (ert-deftest test-org-element/table-interpreter ()
  2925. "Test table, table-row and table-cell interpreters."
  2926. ;; 1. Simple table.
  2927. (should (equal (org-test-parse-and-interpret "| a | b |\n| c | d |")
  2928. "| a | b |\n| c | d |\n"))
  2929. ;; 2. With horizontal rules.
  2930. (should (equal (org-test-parse-and-interpret
  2931. "| a | b |\n|---+---|\n| c | d |")
  2932. "| a | b |\n|---+---|\n| c | d |\n"))
  2933. ;; 3. With meta-data.
  2934. (should (equal (org-test-parse-and-interpret "| / | < | > |\n| * | 1 | 2 |")
  2935. "| / | < | > |\n| * | 1 | 2 |\n"))
  2936. ;; 4. With a formula.
  2937. (should
  2938. (equal (org-test-parse-and-interpret
  2939. "| 2 |\n| 4 |\n| 3 |\n#+TBLFM: @3=vmean(@1..@2)")
  2940. "| 2 |\n| 4 |\n| 3 |\n#+TBLFM: @3=vmean(@1..@2)\n"))
  2941. ;; 5. With multiple formulas.
  2942. (should
  2943. (equal (org-test-parse-and-interpret
  2944. "| 2 |\n| 4 |\n| 3 |\n#+TBLFM: test1\n#+TBLFM: test2")
  2945. "| 2 |\n| 4 |\n| 3 |\n#+TBLFM: test1\n#+TBLFM: test2\n")))
  2946. (ert-deftest test-org-element/timestamp-interpreter ()
  2947. "Test timestamp interpreter."
  2948. ;; Active.
  2949. (should
  2950. (string-match "<2012-03-29 .* 16:40>"
  2951. (org-test-parse-and-interpret "<2012-03-29 thu. 16:40>")))
  2952. (should
  2953. (string-match "<2012-03-29 .* 16:40>"
  2954. (org-element-timestamp-interpreter
  2955. '(timestamp
  2956. (:type active :year-start 2012 :month-start 3 :day-start 29
  2957. :hour-start 16 :minute-start 40)) nil)))
  2958. ;; Inactive.
  2959. (should
  2960. (string-match "\\[2012-03-29 .* 16:40\\]"
  2961. (org-test-parse-and-interpret "[2012-03-29 thu. 16:40]")))
  2962. (should
  2963. (string-match
  2964. "\\[2012-03-29 .* 16:40\\]"
  2965. (org-element-timestamp-interpreter
  2966. '(timestamp
  2967. (:type inactive :year-start 2012 :month-start 3 :day-start 29
  2968. :hour-start 16 :minute-start 40)) nil)))
  2969. ;; Active range.
  2970. (should
  2971. (string-match "<2012-03-29 .* 16:40>--<2012-03-29 .* 16:41>"
  2972. (org-test-parse-and-interpret
  2973. "<2012-03-29 thu. 16:40>--<2012-03-29 thu. 16:41>")))
  2974. (should
  2975. (string-match
  2976. "<2012-03-29 .* 16:40>--<2012-03-29 .* 16:41>"
  2977. (org-element-timestamp-interpreter
  2978. '(timestamp
  2979. (:type active-range :year-start 2012 :month-start 3 :day-start 29
  2980. :hour-start 16 :minute-start 40 :year-end 2012 :month-end 3
  2981. :day-end 29 :hour-end 16 :minute-end 41)) nil)))
  2982. ;; Inactive range.
  2983. (should
  2984. (string-match "\\[2012-03-29 .* 16:40\\]--\\[2012-03-29 .* 16:41\\]"
  2985. (org-test-parse-and-interpret
  2986. "[2012-03-29 thu. 16:40]--[2012-03-29 thu. 16:41]")))
  2987. (should
  2988. (string-match
  2989. "\\[2012-03-29 .* 16:40\\]--\\[2012-03-29 .* 16:41\\]"
  2990. (org-element-timestamp-interpreter
  2991. '(timestamp
  2992. (:type inactive-range :year-start 2012 :month-start 3 :day-start 29
  2993. :hour-start 16 :minute-start 40 :year-end 2012 :month-end 3
  2994. :day-end 29 :hour-end 16 :minute-end 41)) nil)))
  2995. ;; Diary.
  2996. (should (equal (org-test-parse-and-interpret "<%%diary-float t 4 2>")
  2997. "<%%diary-float t 4 2>\n"))
  2998. ;; Timestamp with repeater interval, with delay, with both.
  2999. (should
  3000. (string-match "<2012-03-29 .* \\+1y>"
  3001. (org-test-parse-and-interpret "<2012-03-29 thu. +1y>")))
  3002. (should
  3003. (string-match
  3004. "<2012-03-29 .* \\+1y>"
  3005. (org-element-timestamp-interpreter
  3006. '(timestamp
  3007. (:type active :year-start 2012 :month-start 3 :day-start 29
  3008. :repeater-type cumulate :repeater-value 1 :repeater-unit year))
  3009. nil)))
  3010. (should
  3011. (string-match
  3012. "<2012-03-29 .* -1y>"
  3013. (org-element-timestamp-interpreter
  3014. '(timestamp
  3015. (:type active :year-start 2012 :month-start 3 :day-start 29
  3016. :warning-type all :warning-value 1 :warning-unit year))
  3017. nil)))
  3018. (should
  3019. (string-match
  3020. "<2012-03-29 .* \\+1y -1y>"
  3021. (org-element-timestamp-interpreter
  3022. '(timestamp
  3023. (:type active :year-start 2012 :month-start 3 :day-start 29
  3024. :warning-type all :warning-value 1 :warning-unit year
  3025. :repeater-type cumulate :repeater-value 1 :repeater-unit year))
  3026. nil)))
  3027. ;; Timestamp range with repeater interval
  3028. (should
  3029. (string-match "<2012-03-29 .* \\+1y>--<2012-03-30 .* \\+1y>"
  3030. (org-test-parse-and-interpret
  3031. "<2012-03-29 Thu +1y>--<2012-03-30 Thu +1y>")))
  3032. (should
  3033. (string-match
  3034. "<2012-03-29 .* \\+1y>--<2012-03-30 .* \\+1y>"
  3035. (org-element-timestamp-interpreter
  3036. '(timestamp
  3037. (:type active-range :year-start 2012 :month-start 3 :day-start 29
  3038. :year-end 2012 :month-end 3 :day-end 30 :repeater-type cumulate
  3039. :repeater-value 1 :repeater-unit year))
  3040. nil))))
  3041. (ert-deftest test-org-element/verse-block-interpreter ()
  3042. "Test verse block interpretation."
  3043. (should
  3044. (equal (org-test-parse-and-interpret "#+BEGIN_VERSE\nTest\n#+END_VERSE")
  3045. "#+begin_verse\nTest\n#+end_verse\n")))
  3046. (ert-deftest test-org-element/bold-interpreter ()
  3047. "Test bold interpreter."
  3048. (should (equal (org-test-parse-and-interpret "*text*") "*text*\n")))
  3049. (ert-deftest test-org-element/citation-interpreter ()
  3050. "Test citation interpreter."
  3051. (should
  3052. (equal "[cite:@key]\n"
  3053. (org-test-parse-and-interpret "[cite:@key]")))
  3054. (should
  3055. (equal "[cite:-@key]\n"
  3056. (org-test-parse-and-interpret "[cite:-@key]")))
  3057. (should
  3058. (equal "[cite/style:@key]\n"
  3059. (org-test-parse-and-interpret "[cite/style:@key]")))
  3060. (should
  3061. (equal "[cite:pre @key]\n"
  3062. (org-test-parse-and-interpret "[cite:pre @key]")))
  3063. (should
  3064. (equal "[cite:@key post]\n"
  3065. (org-test-parse-and-interpret "[cite:@key post]")))
  3066. (should
  3067. (equal "[cite:@a ;b]\n"
  3068. (org-test-parse-and-interpret "[cite: @a ;b]")))
  3069. (should
  3070. (equal "[cite:@a;@b;@c]\n"
  3071. (org-test-parse-and-interpret "[cite:@a;@b;@c]")))
  3072. (should
  3073. (equal "[cite:common-pre ; @a]\n"
  3074. (org-test-parse-and-interpret "[cite:common-pre ; @a]")))
  3075. (should
  3076. (equal "[cite:@a ; common-post]\n"
  3077. (org-test-parse-and-interpret "[cite:@a ; common-post]"))))
  3078. (ert-deftest test-org-element/code-interpreter ()
  3079. "Test code interpreter."
  3080. (should (equal (org-test-parse-and-interpret "~text~") "~text~\n")))
  3081. (ert-deftest test-org-element/entity-interpreter ()
  3082. "Test entity interpreter."
  3083. ;; 1. Without brackets.
  3084. (should
  3085. (equal (org-test-parse-and-interpret "\\alpha text") "\\alpha text\n"))
  3086. ;; 2. With brackets.
  3087. (should
  3088. (equal (org-test-parse-and-interpret "\\alpha{}text") "\\alpha{}text\n")))
  3089. (ert-deftest test-org-element/export-snippet-interpreter ()
  3090. "Test export snippet interpreter."
  3091. (should (equal (org-test-parse-and-interpret "@@back-end:contents@@")
  3092. "@@back-end:contents@@\n")))
  3093. (ert-deftest test-org-element/footnote-reference-interpreter ()
  3094. "Test footnote reference interpreter."
  3095. ;; Regular reference.
  3096. (should (equal (org-test-parse-and-interpret "Text[fn:1]") "Text[fn:1]\n"))
  3097. ;; Named reference.
  3098. (should (equal (org-test-parse-and-interpret "Text[fn:label]")
  3099. "Text[fn:label]\n"))
  3100. ;; Inline reference.
  3101. (should (equal (org-test-parse-and-interpret "Text[fn:label:def]")
  3102. "Text[fn:label:def]\n"))
  3103. ;; Anonymous reference.
  3104. (should (equal (org-test-parse-and-interpret "Text[fn::def]")
  3105. "Text[fn::def]\n")))
  3106. (ert-deftest test-org-element/inline-babel-call-interpreter ()
  3107. "Test inline babel call interpreter."
  3108. ;; Without arguments.
  3109. (should (equal (org-test-parse-and-interpret "call_test()") "call_test()\n"))
  3110. ;; With arguments.
  3111. (should (equal (org-test-parse-and-interpret "call_test(x=2)")
  3112. "call_test(x=2)\n"))
  3113. ;; With header arguments.
  3114. (should (equal (org-test-parse-and-interpret
  3115. "call_test[:results output]()[:results html]")
  3116. "call_test[:results output]()[:results html]\n")))
  3117. (ert-deftest test-org-element/inline-src-block-interpreter ()
  3118. "Test inline src block interpreter."
  3119. ;; 1. Without header argument.
  3120. (should (equal (org-test-parse-and-interpret "src_emacs-lisp{(+ 1 1)}")
  3121. "src_emacs-lisp{(+ 1 1)}\n"))
  3122. ;; 2. With header arguments.
  3123. (should (equal (org-test-parse-and-interpret
  3124. "src_emacs-lisp[:results silent]{(+ 1 1)}")
  3125. "src_emacs-lisp[:results silent]{(+ 1 1)}\n")))
  3126. (ert-deftest test-org-element/italic-interpreter ()
  3127. "Test italic interpreter."
  3128. (should (equal (org-test-parse-and-interpret "/text/") "/text/\n")))
  3129. (ert-deftest test-org-element/latex-fragment-interpreter ()
  3130. "Test latex fragment interpreter."
  3131. (should (equal (org-test-parse-and-interpret "\\command{}") "\\command{}\n"))
  3132. (should (equal (org-test-parse-and-interpret "$x$") "$x$\n"))
  3133. (should (equal (org-test-parse-and-interpret "$x+y$") "$x+y$\n"))
  3134. (should (equal (org-test-parse-and-interpret "$$x+y$$") "$$x+y$$\n"))
  3135. (should (equal (org-test-parse-and-interpret "\\(x+y\\)") "\\(x+y\\)\n"))
  3136. (should (equal (org-test-parse-and-interpret "\\[x+y\\]") "\\[x+y\\]\n")))
  3137. (ert-deftest test-org-element/line-break-interpreter ()
  3138. "Test line break interpreter."
  3139. (should (equal (org-test-parse-and-interpret "First line \\\\ \nSecond line")
  3140. "First line \\\\\nSecond line\n")))
  3141. (ert-deftest test-org-element/link-interpreter ()
  3142. "Test link interpreter."
  3143. ;; Links targeted from a radio target.
  3144. (should (equal (let ((org-target-link-regexp "radio-target"))
  3145. (org-test-parse-and-interpret "a radio-target"))
  3146. "a radio-target\n"))
  3147. ;; Links without description.
  3148. (should (equal (org-test-parse-and-interpret "[[https://orgmode.org]]")
  3149. "[[https://orgmode.org]]\n"))
  3150. ;; Links with a description, even one containing a link.
  3151. (should (equal (org-test-parse-and-interpret
  3152. "[[https://orgmode.org][Org mode]]")
  3153. "[[https://orgmode.org][Org mode]]\n"))
  3154. (should (equal (org-test-parse-and-interpret
  3155. "[[https://orgmode.org][https://orgmode.org]]")
  3156. "[[https://orgmode.org][https://orgmode.org]]\n"))
  3157. ;; File links.
  3158. (should
  3159. (equal (org-test-parse-and-interpret "[[file+emacs:todo.org]]")
  3160. "[[file+emacs:todo.org]]\n"))
  3161. (should
  3162. (equal (org-test-parse-and-interpret "[[file:todo.org::*task]]")
  3163. "[[file:todo.org::*task]]\n"))
  3164. ;; Id links.
  3165. (should (equal (org-test-parse-and-interpret "[[id:aaaa]]") "[[id:aaaa]]\n"))
  3166. ;; Custom-id links.
  3167. (should (equal (org-test-parse-and-interpret "[[#id]]") "[[#id]]\n"))
  3168. ;; Code-ref links.
  3169. (should (equal (org-test-parse-and-interpret "[[(ref)]]") "[[(ref)]]\n"))
  3170. ;; Plain links.
  3171. (should (equal (org-test-parse-and-interpret "https://orgmode.org")
  3172. "https://orgmode.org\n"))
  3173. ;; Angular links.
  3174. (should (equal (org-test-parse-and-interpret "<https://orgmode.org>")
  3175. "<https://orgmode.org>\n"))
  3176. ;; Pathological case: link with a %-sign in description.
  3177. (should (equal (org-test-parse-and-interpret "[[file://path][%s]]")
  3178. "[[file://path][%s]]\n")))
  3179. (ert-deftest test-org-element/macro-interpreter ()
  3180. "Test macro interpreter."
  3181. ;; 1. Without argument.
  3182. (should (equal (org-test-parse-and-interpret "{{{test}}}") "{{{test}}}\n"))
  3183. ;; 2. With arguments.
  3184. (should (equal (org-test-parse-and-interpret "{{{test(arg1,arg2)}}}")
  3185. "{{{test(arg1,arg2)}}}\n")))
  3186. (ert-deftest test-org-element/radio-target-interpreter ()
  3187. "Test radio target interpreter."
  3188. (should (equal (org-test-parse-and-interpret "<<<some text>>>")
  3189. "<<<some text>>>\n")))
  3190. (ert-deftest test-org-element/statistics-cookie-interpreter ()
  3191. "Test statistics cookie interpreter."
  3192. ;; 1. Without percent
  3193. (should (equal (org-test-parse-and-interpret "[0/1]") "[0/1]\n"))
  3194. ;; 2. With percent.
  3195. (should (equal (org-test-parse-and-interpret "[66%]") "[66%]\n")))
  3196. (ert-deftest test-org-element/strike-through-interpreter ()
  3197. "Test strike through interpreter."
  3198. (should (equal (org-test-parse-and-interpret "+target+") "+target+\n")))
  3199. (ert-deftest test-org-element/subscript-interpreter ()
  3200. "Test subscript interpreter."
  3201. ;; 1. Without brackets.
  3202. (should (equal (org-test-parse-and-interpret "a_b") "a_b\n"))
  3203. ;; 2. With brackets.
  3204. (should (equal (org-test-parse-and-interpret "a_{b}") "a_{b}\n")))
  3205. (ert-deftest test-org-element/superscript-interpreter ()
  3206. "Test superscript interpreter."
  3207. ;; 1. Without brackets.
  3208. (should (equal (org-test-parse-and-interpret "a^b") "a^b\n"))
  3209. ;; 2. With brackets.
  3210. (should (equal (org-test-parse-and-interpret "a^{b}") "a^{b}\n")))
  3211. (ert-deftest test-org-element/target-interpreter ()
  3212. "Test target interpreter."
  3213. (should (equal (org-test-parse-and-interpret "<<target>>") "<<target>>\n")))
  3214. (ert-deftest test-org-element/underline-interpreter ()
  3215. "Test underline interpreter."
  3216. (should (equal (org-test-parse-and-interpret "_text_") "_text_\n")))
  3217. (ert-deftest test-org-element/verbatim-interpreter ()
  3218. "Test verbatim interpreter."
  3219. (should (equal (org-test-parse-and-interpret "=text=") "=text=\n")))
  3220. ;;; Test Granularity
  3221. (ert-deftest test-org-element/granularity ()
  3222. "Test granularity impact on buffer parsing."
  3223. (org-test-with-temp-text
  3224. "* Head 1
  3225. ** Head 2
  3226. #+BEGIN_CENTER
  3227. Centered paragraph.
  3228. #+END_CENTER
  3229. Paragraph \\alpha."
  3230. ;; 1.1. Granularity set to `headline' should parse every headline
  3231. ;; in buffer, and only them.
  3232. (let ((tree (org-element-parse-buffer 'headline)))
  3233. (should (= 2 (length (org-element-map tree 'headline 'identity))))
  3234. (should-not (org-element-map tree 'paragraph 'identity)))
  3235. ;; 1.2. Granularity set to `greater-element' should not enter
  3236. ;; greater elements excepted headlines and sections.
  3237. (let ((tree (org-element-parse-buffer 'greater-element)))
  3238. (should (= 1 (length (org-element-map tree 'center-block 'identity))))
  3239. (should (= 1 (length (org-element-map tree 'paragraph 'identity))))
  3240. (should-not (org-element-map tree 'entity 'identity)))
  3241. ;; 1.3. Granularity set to `element' should enter every
  3242. ;; greater-element.
  3243. (let ((tree (org-element-parse-buffer 'element)))
  3244. (should (= 2 (length (org-element-map tree 'paragraph 'identity))))
  3245. (should-not (org-element-map tree 'entity 'identity)))
  3246. ;; 1.4. Granularity set to `object' can see everything.
  3247. (let ((tree (org-element-parse-buffer 'object)))
  3248. (should (= 1 (length (org-element-map tree 'entity 'identity)))))))
  3249. (ert-deftest test-org-element/secondary-string-parsing ()
  3250. "Test if granularity correctly toggles secondary strings parsing."
  3251. ;; With a granularity bigger than `object', no secondary string
  3252. ;; should be parsed.
  3253. (should
  3254. (stringp
  3255. (org-test-with-temp-text "* Headline"
  3256. (let ((headline
  3257. (org-element-map (org-element-parse-buffer 'headline) 'headline
  3258. #'identity nil 'first-match)))
  3259. (org-element-property :title headline)))))
  3260. (should
  3261. (stringp
  3262. (org-test-with-temp-text "* Headline\n- tag :: item"
  3263. (let ((item (org-element-map (org-element-parse-buffer 'element) 'item
  3264. #'identity nil 'first-match)))
  3265. (org-element-property :tag item)))))
  3266. (when (featurep 'org-inlinetask)
  3267. (should
  3268. (stringp
  3269. (let ((org-inlinetask-min-level 15))
  3270. (org-test-with-temp-text "*************** Inlinetask"
  3271. (let ((inlinetask (org-element-map (org-element-parse-buffer 'element)
  3272. 'inlinetask
  3273. #'identity nil 'first-match)))
  3274. (org-element-property :title inlinetask)))))))
  3275. ;; With a default granularity, secondary strings should be parsed.
  3276. (should
  3277. (listp
  3278. (org-test-with-temp-text "* Headline"
  3279. (let ((headline
  3280. (org-element-map (org-element-parse-buffer) 'headline
  3281. #'identity nil 'first-match)))
  3282. (org-element-property :title headline)))))
  3283. ;; `org-element-at-point' should never parse a secondary string.
  3284. (should-not
  3285. (listp
  3286. (org-test-with-temp-text "* Headline"
  3287. (org-element-property :title (org-element-at-point)))))
  3288. ;; Preserve current local variables when parsing a secondary string.
  3289. (should
  3290. (let ((org-entities nil)
  3291. (org-entities-user nil))
  3292. (org-test-with-temp-text "
  3293. #+CAPTION: \\foo
  3294. Text
  3295. # Local Variables:
  3296. # org-entities-user: ((\"foo\"))
  3297. # End:"
  3298. (let ((safe-local-variable-values '((org-entities-user . (("foo"))))))
  3299. (hack-local-variables))
  3300. (org-element-map (org-element-parse-buffer) 'entity
  3301. #'identity nil nil nil t)))))
  3302. ;;; Test Visible Only Parsing
  3303. (ert-deftest test-org-element/parse-buffer-visible ()
  3304. "Test `org-element-parse-buffer' with visible only argument."
  3305. (should
  3306. (equal '("H1" "H3" "H5")
  3307. (org-test-with-temp-text
  3308. "* H1\n** H2\n** H3 :visible:\n** H4\n** H5 :visible:"
  3309. (org-occur ":visible:")
  3310. (org-element-map (org-element-parse-buffer nil t) 'headline
  3311. (lambda (hl) (org-element-property :raw-value hl))))))
  3312. (should
  3313. (equal "Test"
  3314. (let ((contents "Test"))
  3315. (org-test-with-temp-text contents
  3316. (add-text-properties 0 1 '(invisible t) contents)
  3317. (org-element-map (org-element-parse-buffer nil t) 'plain-text
  3318. #'org-no-properties nil t)))))
  3319. (should
  3320. (equal "Test"
  3321. (let ((contents "Test"))
  3322. (org-test-with-temp-text (concat "- " contents)
  3323. (add-text-properties 0 1 '(invisible t) contents)
  3324. (org-element-map (org-element-parse-buffer nil t) 'plain-text
  3325. #'org-no-properties nil t))))))
  3326. ;;; Test `:parent' Property
  3327. (ert-deftest test-org-element/parent-property ()
  3328. "Test `:parent' property."
  3329. ;; Elements.
  3330. (org-test-with-temp-text "#+BEGIN_CENTER\nText\n#+END_CENTER"
  3331. (let* ((tree (org-element-parse-buffer))
  3332. (parent (org-element-property
  3333. :parent
  3334. (org-element-map tree 'paragraph 'identity nil t))))
  3335. (should parent)
  3336. (should (eq (org-element-map tree 'center-block 'identity nil t)
  3337. parent))))
  3338. ;; Objects.
  3339. (org-test-with-temp-text "a_{/b/}"
  3340. (let* ((tree (org-element-parse-buffer))
  3341. (parent (org-element-property
  3342. :parent
  3343. (org-element-map tree 'italic 'identity nil t))))
  3344. (should parent)
  3345. (should (eq parent
  3346. (org-element-map tree 'subscript 'identity nil t)))))
  3347. ;; Secondary strings
  3348. (org-test-with-temp-text "* /italic/"
  3349. (let* ((tree (org-element-parse-buffer))
  3350. (parent (org-element-property
  3351. :parent (org-element-map tree 'italic 'identity nil t))))
  3352. (should parent)
  3353. (should (equal parent
  3354. (org-element-map tree 'headline 'identity nil t))))))
  3355. ;;; Test Normalize Contents
  3356. (ert-deftest test-org-element/normalize-contents ()
  3357. "Test `org-element-normalize-contents' specifications."
  3358. ;; Remove maximum common indentation from element's contents.
  3359. (should
  3360. (equal
  3361. (org-element-normalize-contents
  3362. '(paragraph nil " Two spaces\n Three spaces"))
  3363. '(paragraph nil "Two spaces\n Three spaces")))
  3364. (should
  3365. (equal
  3366. (org-element-normalize-contents
  3367. '(paragraph nil " Two spaces\nNo space"))
  3368. '(paragraph nil " Two spaces\nNo space")))
  3369. ;; Ignore objects within contents when computing maximum common
  3370. ;; indentation. However, if contents start with an object, common
  3371. ;; indentation is 0.
  3372. (should
  3373. (equal
  3374. (org-element-normalize-contents
  3375. '(paragraph nil " One " (emphasis nil "space") "\n Two spaces"))
  3376. '(paragraph nil "One " (emphasis nil "space") "\n Two spaces")))
  3377. (should
  3378. (equal
  3379. (org-element-normalize-contents
  3380. '(paragraph nil (verbatim nil "V") "No space\n Two\n Three"))
  3381. '(paragraph nil (verbatim nil "V") "No space\n Two\n Three")))
  3382. ;; Ignore blank lines.
  3383. (should
  3384. (equal
  3385. (org-element-normalize-contents
  3386. '(paragraph nil " Two spaces\n\n \n Two spaces"))
  3387. '(paragraph nil "Two spaces\n\n\nTwo spaces")))
  3388. (should
  3389. (equal
  3390. '(paragraph nil " Two spaces\n" (verbatim nil "V") "\n Two spaces")
  3391. (org-element-normalize-contents
  3392. '(paragraph nil " Two spaces\n " (verbatim nil "V") "\n Two spaces"))))
  3393. (should
  3394. (equal
  3395. '(verse-block nil "line 1\n\nline 2")
  3396. (org-element-normalize-contents
  3397. '(verse-block nil " line 1\n\n line 2"))))
  3398. ;; Recursively enter objects in order to compute common indentation.
  3399. (should
  3400. (equal
  3401. (org-element-normalize-contents
  3402. '(paragraph nil " Two spaces " (bold nil " and\n One space")))
  3403. '(paragraph nil " Two spaces " (bold nil " and\nOne space"))))
  3404. ;; When optional argument is provided, ignore first line
  3405. ;; indentation.
  3406. (should
  3407. (equal
  3408. (org-element-normalize-contents
  3409. '(paragraph nil "No space\n Two spaces\n Three spaces") t)
  3410. '(paragraph nil "No space\nTwo spaces\n Three spaces")))
  3411. (should
  3412. (equal
  3413. (org-element-normalize-contents
  3414. '(paragraph nil (verbatim nil "V") "No space\n Two\n Three") t)
  3415. '(paragraph nil (verbatim nil "V") "No space\nTwo\n Three")))
  3416. ;; Corner case: do not ignore indentation of string right after
  3417. ;; a line break.
  3418. (should
  3419. (equal
  3420. (org-element-normalize-contents
  3421. '(paragraph nil " 1 space" (line-break) " 2 spaces"))
  3422. '(paragraph nil "1 space" (line-break) " 2 spaces"))))
  3423. ;;; Test Navigation Tools.
  3424. (ert-deftest test-org-element/at-point ()
  3425. "Test `org-element-at-point' specifications."
  3426. ;; Return closest element containing point.
  3427. (should
  3428. (eq 'paragraph
  3429. (org-test-with-temp-text "#+BEGIN_CENTER\nA\n#+END_CENTER"
  3430. (progn (search-forward "A")
  3431. (org-element-type (org-element-at-point))))))
  3432. ;; Correctly set `:parent' property.
  3433. (should
  3434. (eq 'center-block
  3435. (org-test-with-temp-text "#+BEGIN_CENTER\nA\n#+END_CENTER"
  3436. (progn (search-forward "A")
  3437. (org-element-type
  3438. (org-element-property :parent (org-element-at-point)))))))
  3439. ;; Special case: at a blank line just below a headline, return that
  3440. ;; headline.
  3441. (should
  3442. (equal "H1" (org-test-with-temp-text "* H1\n \n* H2\n"
  3443. (forward-line)
  3444. (org-element-property :title (org-element-at-point)))))
  3445. ;; Special case: at the very beginning of a table, return `table'
  3446. ;; object instead of `table-row'.
  3447. (should
  3448. (eq 'table
  3449. (org-test-with-temp-text "| a | b |"
  3450. (org-element-type (org-element-at-point)))))
  3451. ;; Special case: at the very beginning of a list or sub-list, return
  3452. ;; `plain-list' object instead of `item'.
  3453. (should
  3454. (eq 'plain-list
  3455. (org-test-with-temp-text "- item"
  3456. (org-element-type (org-element-at-point)))))
  3457. ;; Special case: at the closing line of a greater element, be sure
  3458. ;; to return it instead of the last element in its contents.
  3459. (should
  3460. (eq 'center-block
  3461. (org-test-with-temp-text "#+BEGIN_CENTER\nParagraph\n#+END_CENTER"
  3462. (progn (forward-line 2)
  3463. (org-element-type (org-element-at-point))))))
  3464. ;; Special case: at a blank line between two items, be sure to
  3465. ;; return item above instead of the last element of its contents.
  3466. (should
  3467. (eq 'item
  3468. (org-test-with-temp-text "- Para1\n\n- Para2"
  3469. (forward-line)
  3470. (org-element-type (org-element-at-point)))))
  3471. ;; Special case: at the last blank line in a plain list, return it
  3472. ;; instead of the last item.
  3473. (should
  3474. (eq 'plain-list
  3475. (org-test-with-temp-text "- Para1\n- Para2\n\nPara3"
  3476. (progn (forward-line 2)
  3477. (org-element-type (org-element-at-point))))))
  3478. ;; Special case: at the last blank line in a plain list at the end of
  3479. ;; a headline, return the plain list, not the last item, and not the
  3480. ;; headline.
  3481. (should
  3482. (eq 'plain-list
  3483. (org-test-with-temp-text "* Headline\n- Para1\n- Para2\n\nPara3\n* Another headline"
  3484. (progn (forward-line 3)
  3485. (org-element-type (org-element-at-point))))))
  3486. ;; Special case: when a list ends at the end of buffer and there's
  3487. ;; no final newline, return last element in last item.
  3488. (should
  3489. (eq 'paragraph
  3490. (org-test-with-temp-text "- a"
  3491. (end-of-line)
  3492. (org-element-type (org-element-at-point)))))
  3493. ;; Parse a list within a block itself contained in a list.
  3494. (should
  3495. (eq 'plain-list
  3496. (org-test-with-temp-text
  3497. "- outer\n #+begin_center\n - inner\n #+end_center"
  3498. (search-forward "inner")
  3499. (beginning-of-line)
  3500. (org-element-type (org-element-at-point)))))
  3501. ;; Do not error at eob on an empty line.
  3502. (should
  3503. (org-test-with-temp-text "* H\n"
  3504. (forward-line)
  3505. (or (org-element-at-point) t)))
  3506. ;; Return greater element when outside contents.
  3507. (should
  3508. (eq 'drawer
  3509. (org-test-with-temp-text
  3510. ":DRAWER:\ntest\n:EN<point>D:\n"
  3511. (org-element-type (org-element-at-point)))))
  3512. (should
  3513. (eq 'drawer
  3514. (org-test-with-temp-text
  3515. ":DRA<point>WER:\ntest\n:END:\n"
  3516. (org-element-type (org-element-at-point)))))
  3517. ;; Return greater element when at :contents-end.
  3518. (should
  3519. (eq 'drawer
  3520. (org-test-with-temp-text
  3521. ":DRAWER:\ntest\n<point>:END:\n"
  3522. (org-element-type (org-element-at-point))))))
  3523. (ert-deftest test-org-element/context ()
  3524. "Test `org-element-context' specifications."
  3525. ;; Return closest object containing point.
  3526. (should
  3527. (eq 'underline
  3528. (org-test-with-temp-text "Some *text with _under<point>line_ text*"
  3529. (org-element-type (org-element-context)))))
  3530. ;; Find objects in secondary strings.
  3531. (should
  3532. (eq 'underline
  3533. (org-test-with-temp-text "* Headline _<point>with_ underlining"
  3534. (org-element-type (org-element-context)))))
  3535. ;; Find objects in objects.
  3536. (should
  3537. (eq 'macro
  3538. (org-test-with-temp-text "| a | {<point>{{macro}}} |"
  3539. (org-element-type (org-element-context)))))
  3540. (should
  3541. (eq 'table-cell
  3542. (org-test-with-temp-text "| a | b<point> {{{macro}}} |"
  3543. (org-element-type (org-element-context)))))
  3544. ;; Find objects in item tags.
  3545. (should
  3546. (eq 'bold
  3547. (org-test-with-temp-text "- *bo<point>ld* ::"
  3548. (org-element-type (org-element-context)))))
  3549. (should-not
  3550. (eq 'bold
  3551. (org-test-with-temp-text "- *bold* ::<point>"
  3552. (org-element-type (org-element-context)))))
  3553. (should-not
  3554. (eq 'bold
  3555. (org-test-with-temp-text "- *bold* ::\n<point>"
  3556. (org-element-type (org-element-context)))))
  3557. ;; Do not find objects in table rules.
  3558. (should
  3559. (eq 'table-row
  3560. (org-test-with-temp-text "| a | b |\n|-<point>--|---|\n| c | d |"
  3561. (org-element-type (org-element-context)))))
  3562. ;; Find objects in parsed affiliated keywords.
  3563. (should
  3564. (eq 'macro
  3565. (org-test-with-temp-text "#+CAPTION: {<point>{{macro}}}\n| a | b |"
  3566. (org-element-type (org-element-context)))))
  3567. (should
  3568. (eq 'bold
  3569. (org-test-with-temp-text "#+caption: *<point>bold*\nParagraph"
  3570. (org-element-type (org-element-context)))))
  3571. ;; Find objects at the end of buffer.
  3572. (should
  3573. (eq 'bold
  3574. (org-test-with-temp-text "*bold*"
  3575. (goto-char (point-max))
  3576. (org-element-type (org-element-context)))))
  3577. ;; Correctly set `:parent' property.
  3578. (should
  3579. (eq 'paragraph
  3580. (org-test-with-temp-text "Some *bold<point>* text"
  3581. (org-element-type
  3582. (org-element-property :parent (org-element-context))))))
  3583. ;; Between two objects, return the second one.
  3584. (should
  3585. (eq 'macro
  3586. (org-test-with-temp-text "<<target>><point>{{{test}}}"
  3587. (org-element-type (org-element-context)))))
  3588. ;; Test optional argument.
  3589. (should
  3590. (eq 'underline
  3591. (org-test-with-temp-text "Some *text with _under<point>line_ text*"
  3592. (org-element-type (org-element-context (org-element-at-point))))))
  3593. ;; Special case: bold object at the beginning of a headline.
  3594. (should
  3595. (eq 'bold
  3596. (org-test-with-temp-text "* *bo<point>ld*"
  3597. (org-element-type (org-element-context)))))
  3598. ;; Special case: incomplete cell at the end of a table row.
  3599. (should
  3600. (eq 'table-cell
  3601. (org-test-with-temp-text "|a|b|c<point>"
  3602. (org-element-type (org-element-context)))))
  3603. ;; Special case: objects in inline footnotes.
  3604. (should
  3605. (eq 'link
  3606. (org-test-with-temp-text "[fn::[[<point>https://orgmode.org]]]"
  3607. (org-element-type (org-element-context)))))
  3608. ;; Special case: tags looking like a link.
  3609. (should-not
  3610. (eq 'link
  3611. (org-test-with-temp-text "* Headline :file<point>:tags:"
  3612. (org-element-type (org-element-context)))))
  3613. (should
  3614. (eq 'link
  3615. (org-test-with-temp-text "* Headline :file<point>:tags: :real:tag:"
  3616. (org-element-type (org-element-context))))))
  3617. ;;; Test Tools
  3618. (ert-deftest test-org-element/lineage ()
  3619. "Test `org-element-lineage' specifications."
  3620. ;; Regular tests. When applied to an element or object returned by
  3621. ;; `org-element-at-point' or `org-element-context', the list is
  3622. ;; limited to the current section.
  3623. (should
  3624. (equal '(paragraph center-block section headline headline org-data)
  3625. (org-test-with-temp-text
  3626. "* H1\n** H2\n#+BEGIN_CENTER\n*bold<point>*\n#+END_CENTER"
  3627. (mapcar #'car (org-element-lineage (org-element-context))))))
  3628. (should
  3629. (equal '(paragraph center-block section headline headline org-data)
  3630. (org-test-with-temp-text
  3631. "* H1\n** H2\n#+BEGIN_CENTER\n*bold<point>*\n#+END_CENTER"
  3632. (mapcar #'car
  3633. (org-element-lineage
  3634. (org-element-map (org-element-parse-buffer) 'bold
  3635. #'identity nil t))))))
  3636. ;; Test TYPES optional argument.
  3637. (should
  3638. (eq 'center-block
  3639. (org-test-with-temp-text
  3640. "* H1\n** H2\n#+BEGIN_CENTER\n*bold<point>*\n#+END_CENTER"
  3641. (org-element-type
  3642. (org-element-lineage (org-element-context) '(center-block))))))
  3643. (should-not
  3644. (org-test-with-temp-text
  3645. "* H1\n** H2\n#+BEGIN_CENTER\n*bold<point>*\n#+END_CENTER"
  3646. (org-element-lineage (org-element-context) '(example-block))))
  3647. ;; Test WITH-SELF optional argument.
  3648. (should
  3649. (equal '(bold paragraph center-block section headline headline org-data)
  3650. (org-test-with-temp-text
  3651. "* H1\n** H2\n#+BEGIN_CENTER\n*bold<point>*\n#+END_CENTER"
  3652. (mapcar #'car (org-element-lineage (org-element-context) nil t)))))
  3653. ;; When TYPES and WITH-SELF are provided, the latter is also checked
  3654. ;; against the former.
  3655. (should
  3656. (org-test-with-temp-text
  3657. "* H1\n** H2\n#+BEGIN_CENTER\n*bold<point>*\n#+END_CENTER"
  3658. (org-element-lineage (org-element-context) '(bold) t))))
  3659. ;;; Test Cache.
  3660. (ert-deftest test-org-element/cache ()
  3661. "Test basic expectations and common pitfalls for cache."
  3662. ;; Shift positions.
  3663. (should
  3664. (equal '(18 . 23)
  3665. (org-test-with-temp-text "para1\n\npara2\n\npara3"
  3666. (let ((org-element-use-cache t))
  3667. (save-excursion (goto-char (point-max)) (org-element-at-point))
  3668. (insert "add")
  3669. (forward-line 4)
  3670. (let ((element (org-element-at-point)))
  3671. (cons (org-element-property :begin element)
  3672. (org-element-property :end element)))))))
  3673. ;; Partial shifting: when the contents of a greater element are
  3674. ;; modified, only shift ending positions.
  3675. (should
  3676. (org-test-with-temp-text
  3677. "#+BEGIN_CENTER\nPara1\n\nPara2\n\nPara3\n#+END_CENTER"
  3678. (let ((org-element-use-cache t))
  3679. (save-excursion (search-forward "3") (org-element-at-point))
  3680. (search-forward "Para2")
  3681. (insert " ")
  3682. (let ((element (org-element-property :parent (org-element-at-point))))
  3683. (equal (cons (org-element-property :begin element)
  3684. (org-element-property :end element))
  3685. (cons (point-min) (point-max)))))))
  3686. ;; Re-parent shifted elements.
  3687. (should
  3688. (eq 'item
  3689. (org-test-with-temp-text "- item\n\n\n para1\n para2"
  3690. (let ((org-element-use-cache t))
  3691. (end-of-line)
  3692. (org-element-at-point)
  3693. (save-excursion (goto-char (point-max)) (org-element-at-point))
  3694. (forward-line)
  3695. (delete-char 1)
  3696. (goto-char (point-max))
  3697. (org-element-type
  3698. (org-element-property :parent (org-element-at-point)))))))
  3699. ;; Preserve local structures when re-parenting.
  3700. (should
  3701. (eq 'table
  3702. (let ((org-element-use-cache t))
  3703. (org-test-with-temp-text
  3704. "#+begin_center\nP0\n\n<point>\n\n P1\n | a | b |\n | c | d |\n#+end_center"
  3705. (save-excursion (search-forward "| c |") (org-element-at-point))
  3706. (insert "- item")
  3707. (search-forward "| c |")
  3708. (beginning-of-line)
  3709. (org-element-type
  3710. (org-element-property :parent (org-element-at-point)))))))
  3711. (should-not
  3712. (eq 'center-block
  3713. (org-test-with-temp-text
  3714. "#+begin_center\nP0\n\n<point>\n\n P1\n | a | b |\n#+end_center"
  3715. (let ((org-element-use-cache t))
  3716. (save-excursion (search-forward "| a |") (org-element-at-point))
  3717. (insert "- item")
  3718. (search-forward "| a |")
  3719. (beginning-of-line)
  3720. (org-element-type
  3721. (org-element-property :parent (org-element-at-point)))))))
  3722. ;; When re-parenting, also propagate changes to list structures.
  3723. (should
  3724. (= 2
  3725. (org-test-with-temp-text "\n Para\n - item<point>"
  3726. (let ((org-element-use-cache t))
  3727. (org-element-at-point)
  3728. (goto-char (point-min))
  3729. (insert "- Top\n")
  3730. (search-forward "- item")
  3731. (beginning-of-line)
  3732. (length (org-element-property :structure (org-element-at-point)))))))
  3733. ;; Modifying the last line of an element alters the element below.
  3734. (should
  3735. (org-test-with-temp-text "para1\n\npara2"
  3736. (let ((org-element-use-cache t))
  3737. (goto-char (point-max))
  3738. (org-element-at-point)
  3739. (forward-line -1)
  3740. (insert "merge")
  3741. (let ((element (org-element-at-point)))
  3742. (equal (cons (org-element-property :begin element)
  3743. (org-element-property :end element))
  3744. (cons (point-min) (point-max)))))))
  3745. ;; Modifying the first line of an element alters the element above.
  3746. (should
  3747. (org-test-with-temp-text ": fixed-width\n:not-fixed-width"
  3748. (let ((org-element-use-cache t))
  3749. (goto-char (point-max))
  3750. (org-element-at-point)
  3751. (search-backward ":")
  3752. (forward-char)
  3753. (insert " ")
  3754. (let ((element (org-element-at-point)))
  3755. (equal (cons (org-element-property :begin element)
  3756. (org-element-property :end element))
  3757. (cons (point-min) (point-max)))))))
  3758. (org-test-with-temp-text ":DRAWER:\ntest\n:END:\n <point>#\nParagraph"
  3759. (let ((org-element-use-cache t))
  3760. (org-element-cache-map #'ignore :granularity 'element)
  3761. (should (eq 'comment (org-element-type (org-element-at-point))))
  3762. (should (eq 0 (org-element-property :post-blank (org-element-at-point (point-min)))))
  3763. (insert " ") (delete-char -1)
  3764. (org-element-cache-map #'ignore :granularity 'element)
  3765. (delete-char 1)
  3766. (should (eq 1 (org-element-property :post-blank (org-element-at-point (point-min)))))))
  3767. ;; Sensitive change: adding a line alters document structure both
  3768. ;; above and below.
  3769. (should
  3770. (eq 'example-block
  3771. (org-test-with-temp-text "#+BEGIN_EXAMPLE\nPara1\n\nPara2\n"
  3772. (let ((org-element-use-cache t))
  3773. (goto-char (point-max))
  3774. (org-element-at-point)
  3775. (insert "#+END_EXAMPLE")
  3776. (search-backward "Para1")
  3777. (org-element-type (org-element-at-point))))))
  3778. (should
  3779. (eq 'example-block
  3780. (org-test-with-temp-text "Para1\n\nPara2\n#+END_EXAMPLE"
  3781. (let ((org-element-use-cache t))
  3782. (save-excursion (goto-char (point-max)) (org-element-at-point))
  3783. (insert "#+BEGIN_EXAMPLE\n")
  3784. (search-forward "Para2")
  3785. (org-element-type (org-element-at-point))))))
  3786. ;; Sensitive change: removing a line alters document structure both
  3787. ;; above and below.
  3788. (should
  3789. (eq 'example-block
  3790. (org-test-with-temp-text
  3791. "# +BEGIN_EXAMPLE\nPara1\n\nPara2\n#+END_EXAMPLE"
  3792. (let ((org-element-use-cache t))
  3793. (save-excursion (goto-char (point-max)) (org-element-at-point))
  3794. (forward-char)
  3795. (delete-char 1)
  3796. (search-forward "Para2")
  3797. (org-element-type (org-element-at-point))))))
  3798. (should
  3799. (eq 'example-block
  3800. (org-test-with-temp-text
  3801. "#+BEGIN_EXAMPLE\nPara1\n\nPara2\n# +END_EXAMPLE"
  3802. (let ((org-element-use-cache t))
  3803. (save-excursion (goto-char (point-max)) (org-element-at-point))
  3804. (search-forward "# ")
  3805. (delete-char -1)
  3806. (search-backward "Para1")
  3807. (org-element-type (org-element-at-point))))))
  3808. ;; Make sure that we do not generate intersecting elements.
  3809. (should (eq 'paragraph
  3810. (org-test-with-temp-text ":DRAWER:\nP1\n<point>\n:END:\n#+END_EXAMPLE"
  3811. (let ((org-element-use-cache t))
  3812. (org-element-at-point (point-max))
  3813. (org-element-at-point)
  3814. (insert "#+BEGIN_EXAMPLE")
  3815. (org-element-type (org-element-at-point))))))
  3816. ;; But yet correctly slurp obsolete elements inside a new element.
  3817. (should (eq 'example-block
  3818. (org-test-with-temp-text ":DRAWER:\nP1\n<point>\nP2\n#+END_EXAMPLE\n:END:"
  3819. (let ((org-element-use-cache t))
  3820. (org-element-at-point (point-max))
  3821. (save-excursion
  3822. (re-search-forward "P2")
  3823. (should (eq 'paragraph (org-element-type (org-element-at-point))))
  3824. (re-search-forward "END_")
  3825. (should (eq 'paragraph (org-element-type (org-element-at-point)))))
  3826. (insert "#+BEGIN_EXAMPLE")
  3827. (re-search-forward "P2")
  3828. (should (eq 'example-block (org-element-type (org-element-at-point))))
  3829. (re-search-forward "END_")
  3830. (org-element-type (org-element-at-point))))))
  3831. ;; Test edits near :end of element
  3832. (should-not (eq 'headline
  3833. (org-test-with-temp-text "* H1\nP1\n<point>*H2\n"
  3834. (let ((org-element-use-cache t))
  3835. (org-element-cache-map #'ignore :granularity 'element)
  3836. (insert "Blah")
  3837. (org-element-type (org-element-at-point))))))
  3838. (should-not (eq 'headline
  3839. (org-test-with-temp-text "* H1\nP1\n<point>*H2\n"
  3840. (let ((org-element-use-cache t))
  3841. (org-element-cache-map #'ignore :granularity 'element)
  3842. (backward-delete-char 1)
  3843. (org-element-type (org-element-at-point))))))
  3844. (org-test-with-temp-text "Paragraph.\n #<point> comment"
  3845. (let ((org-element-use-cache t))
  3846. (org-element-cache-map #'ignore :granularity 'element)
  3847. (should (eq 'comment (org-element-type (org-element-at-point))))
  3848. (insert "not comment anymore")
  3849. (org-element-cache-map #'ignore :granularity 'element)
  3850. (should-not (eq 'comment (org-element-type (org-element-at-point))))
  3851. (should (eq (org-element-at-point) (org-element-at-point 1)))))
  3852. (should (eq 'headline
  3853. (org-test-with-temp-text "* H1\nP1\n<point>* H2\n"
  3854. (let ((org-element-use-cache t))
  3855. (org-element-cache-map #'ignore :granularity 'element)
  3856. (insert "Blah\n")
  3857. (org-element-type (org-element-at-point))))))
  3858. ;; Corner case: watch out drawers named "PROPERTIES" as they are
  3859. ;; fragile, unlike to other drawers.
  3860. (should
  3861. (eq 'node-property
  3862. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A<point>\n:END:"
  3863. (let ((org-element-use-cache t))
  3864. (org-element-at-point)
  3865. (insert "+:")
  3866. (org-element-type (org-element-at-point))))))
  3867. ;; Properly handle elements not altered by modifications but whose
  3868. ;; parents were removed from cache.
  3869. (should
  3870. (org-test-with-temp-text
  3871. "Paragraph\n\n\n\n#+begin_center\n<point>contents\n#+end_center"
  3872. (let ((org-element-use-cache t)
  3873. (parent-end (point-max)))
  3874. (org-element-at-point)
  3875. (save-excursion (search-backward "Paragraph")
  3876. (forward-line 2)
  3877. (insert "\n "))
  3878. (eq (org-element-property
  3879. :end (org-element-property :parent (org-element-at-point)))
  3880. (+ parent-end 3))))))
  3881. (ert-deftest test-org-element/cache-affiliated ()
  3882. "Test updating affiliated keywords."
  3883. ;; Inserting a line right after other keywords.
  3884. (let ((org-element-use-cache t))
  3885. (org-test-with-temp-text "
  3886. #+caption: test
  3887. #+name: test
  3888. <point>
  3889. line"
  3890. (org-element-cache-map #'ignore :granularity 'element)
  3891. (should (eq 'keyword (org-element-type (org-element-at-point))))
  3892. (insert "#")
  3893. (should (eq 2 (org-element-property :begin (org-element-at-point)))))))
  3894. (ert-deftest test-org-element/cache-table ()
  3895. "Test handling edits in tables."
  3896. ;; Unindented second row of the table should not be re-parented by
  3897. ;; inserted item.
  3898. (should
  3899. (eq 'table
  3900. (let ((org-element-use-cache t))
  3901. (org-test-with-temp-text
  3902. "#+begin_center
  3903. P0
  3904. <point>
  3905. P1
  3906. | a | b |
  3907. | c | d |
  3908. #+end_center"
  3909. (save-excursion (search-forward "| c |") (org-element-at-point))
  3910. (insert "- item")
  3911. (search-forward "| c |")
  3912. (beginning-of-line)
  3913. (org-element-type (org-element-at-point))))))
  3914. (should
  3915. (eq 'table
  3916. (let ((org-element-use-cache t))
  3917. (org-test-with-temp-text
  3918. "
  3919. - item 1
  3920. <point>
  3921. | a | b |
  3922. | c | d |
  3923. #+end_center"
  3924. (save-excursion (search-forward "| c |") (org-element-at-point))
  3925. (delete-char 1)
  3926. (search-forward "| c |")
  3927. (beginning-of-line)
  3928. (org-element-type (org-element-at-point))))))
  3929. (should
  3930. (eq 'table-row
  3931. (let ((org-element-use-cache t))
  3932. (org-test-with-temp-text
  3933. "
  3934. - item 1
  3935. <point>
  3936. | a | b |
  3937. | c | d |
  3938. #+end_center"
  3939. (save-excursion (search-forward "| c |") (org-element-at-point))
  3940. (insert "\n")
  3941. (search-forward "| c |")
  3942. (beginning-of-line)
  3943. (org-element-type (org-element-at-point)))))))
  3944. (ert-deftest test-org-element/cache-headline ()
  3945. "Test basic expectations and common pitfalls for cached headings."
  3946. ;; Appending to final headline in a subtree.
  3947. (org-test-with-temp-text
  3948. "
  3949. * Heading
  3950. Aliquam erat volutpat.
  3951. *** Subheading
  3952. ** Another
  3953. ** Final
  3954. :PROPERTIES:
  3955. :ID: some
  3956. <point>:END:
  3957. * Heading 2
  3958. ** End
  3959. "
  3960. (let ((org-element-use-cache t))
  3961. (org-element-at-point)
  3962. (save-excursion
  3963. (goto-char (point-max))
  3964. (org-element-at-point))
  3965. (insert ":CATEOGORY: cat\n")
  3966. (search-backward "* Heading")
  3967. (should
  3968. (eq (org-element-property :end (org-element-at-point))
  3969. (save-excursion
  3970. (search-forward "* Heading 2")
  3971. (line-beginning-position))))
  3972. (search-forward "* Heading 2")
  3973. (beginning-of-line)
  3974. (insert "\n\n")
  3975. (search-backward "* Heading")
  3976. (should
  3977. (eq (org-element-property :end (org-element-at-point))
  3978. (save-excursion
  3979. (search-forward "* Heading 2")
  3980. (line-beginning-position))))))
  3981. ;; Appending at eob.
  3982. (org-test-with-temp-text
  3983. "
  3984. * Heading
  3985. *** Sub-heading
  3986. ** Another
  3987. *** 1
  3988. ***** 2
  3989. ** 3
  3990. Aenean in sem ac leo mollis blandit.
  3991. <point>"
  3992. (let ((org-element-use-cache t))
  3993. (org-element-at-point (point-max))
  3994. (insert "\n\nTest\n")
  3995. (search-backward "* Heading")
  3996. (should
  3997. (eq (point-max)
  3998. (org-element-property :end (org-element-at-point))))))
  3999. ;; Breaking headline at eob.
  4000. (org-test-with-temp-text
  4001. "
  4002. * Heading
  4003. *** Sub-heading
  4004. <point>"
  4005. (let ((org-element-use-cache t))
  4006. (org-element-at-point (point-max))
  4007. (insert "* heading 2")
  4008. (beginning-of-line)
  4009. (should
  4010. (eq (point-max)
  4011. (org-element-property :end (org-element-at-point))))
  4012. (delete-char 1)
  4013. (search-backward "* Heading")
  4014. (should
  4015. (eq (point-max)
  4016. (org-element-property :end (org-element-at-point))))))
  4017. ;; Inserting low-level headline in-between.
  4018. (org-test-with-temp-text
  4019. "
  4020. * Heading
  4021. *** Sub-heading
  4022. <point>
  4023. *** Sub-heading 2
  4024. *** Sub-heading 3
  4025. "
  4026. (let ((org-element-use-cache t))
  4027. (org-element-at-point (point-max))
  4028. (insert "** heading 2")
  4029. (search-forward "*** Sub-heading 2")
  4030. (should
  4031. (equal (org-element-property :parent (org-element-at-point))
  4032. (progn
  4033. (search-backward "** heading 2")
  4034. (org-element-at-point))))))
  4035. ;; Test when `org-element--cache-for-removal' modifies common parent
  4036. ;; (`org-data' in this case) around changed region.
  4037. (org-test-with-temp-text
  4038. "blah
  4039. :DRAWER:
  4040. <point>test
  4041. :END:
  4042. paragraph
  4043. * headline"
  4044. (let ((org-element-use-cache t))
  4045. (org-element-at-point (point-max))
  4046. (delete-region (point) (point-max))
  4047. (should (eq 'paragraph (org-element-type (org-element-at-point)))))))
  4048. (provide 'test-org-element)
  4049. ;;; test-org-element.el ends here