test-org-element.el 134 KB

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