test-org-element.el 151 KB

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