test-org-element.el 133 KB

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