test-org-element.el 151 KB

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