test-org-element.el 146 KB

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