test-org-element.el 135 KB

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