test-org-element.el 152 KB

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