test-org-element.el 148 KB

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