test-org-element.el 150 KB

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