test-org-element.el 134 KB

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