test-org.el 160 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958
  1. ;;; test-org.el --- tests for org.el
  2. ;; Copyright (c) David Maus
  3. ;; Authors: David Maus
  4. ;; This file is not part of GNU Emacs.
  5. ;; This program is free software; you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; This program is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Comments:
  16. ;; Template test file for Org tests
  17. ;;; Code:
  18. ;;; Comments
  19. (ert-deftest test-org/toggle-comment ()
  20. "Test `org-toggle-comment' specifications."
  21. ;; Simple headline.
  22. (should
  23. (equal "* Test"
  24. (org-test-with-temp-text "* COMMENT Test"
  25. (org-toggle-comment)
  26. (buffer-string))))
  27. (should
  28. (equal "* COMMENT Test"
  29. (org-test-with-temp-text "* Test"
  30. (org-toggle-comment)
  31. (buffer-string))))
  32. ;; Headline with a regular keyword.
  33. (should
  34. (equal "* TODO Test"
  35. (org-test-with-temp-text "* TODO COMMENT Test"
  36. (org-toggle-comment)
  37. (buffer-string))))
  38. (should
  39. (equal "* TODO COMMENT Test"
  40. (org-test-with-temp-text "* TODO Test"
  41. (org-toggle-comment)
  42. (buffer-string))))
  43. ;; Empty headline.
  44. (should
  45. (equal "* "
  46. (org-test-with-temp-text "* COMMENT"
  47. (org-toggle-comment)
  48. (buffer-string))))
  49. (should
  50. (equal "* COMMENT"
  51. (org-test-with-temp-text "* "
  52. (org-toggle-comment)
  53. (buffer-string))))
  54. ;; Headline with a single keyword.
  55. (should
  56. (equal "* TODO "
  57. (org-test-with-temp-text "* TODO COMMENT"
  58. (org-toggle-comment)
  59. (buffer-string))))
  60. (should
  61. (equal "* TODO COMMENT"
  62. (org-test-with-temp-text "* TODO"
  63. (org-toggle-comment)
  64. (buffer-string))))
  65. ;; Headline with a keyword, a priority cookie and contents.
  66. (should
  67. (equal "* TODO [#A] Headline"
  68. (org-test-with-temp-text "* TODO [#A] COMMENT Headline"
  69. (org-toggle-comment)
  70. (buffer-string))))
  71. (should
  72. (equal "* TODO [#A] COMMENT Headline"
  73. (org-test-with-temp-text "* TODO [#A] Headline"
  74. (org-toggle-comment)
  75. (buffer-string)))))
  76. (ert-deftest test-org/comment-dwim ()
  77. "Test `comment-dwim' behaviour in an Org buffer."
  78. ;; No region selected, no comment on current line and line not
  79. ;; empty: insert comment on line above.
  80. (should
  81. (equal "# \nComment"
  82. (org-test-with-temp-text "Comment"
  83. (progn (call-interactively 'comment-dwim)
  84. (buffer-string)))))
  85. ;; No region selected, no comment on current line and line empty:
  86. ;; insert comment on this line.
  87. (should
  88. (equal "# \nParagraph"
  89. (org-test-with-temp-text "\nParagraph"
  90. (progn (call-interactively 'comment-dwim)
  91. (buffer-string)))))
  92. ;; No region selected, and a comment on this line: indent it.
  93. (should
  94. (equal "* Headline\n # Comment"
  95. (org-test-with-temp-text "* Headline\n# Comment"
  96. (progn (forward-line)
  97. (let ((org-adapt-indentation t))
  98. (call-interactively 'comment-dwim))
  99. (buffer-string)))))
  100. ;; Also recognize single # at column 0 as comments.
  101. (should
  102. (equal "# Comment"
  103. (org-test-with-temp-text "# Comment"
  104. (progn (forward-line)
  105. (call-interactively 'comment-dwim)
  106. (buffer-string)))))
  107. ;; Region selected and only comments and blank lines within it:
  108. ;; un-comment all commented lines.
  109. (should
  110. (equal "Comment 1\n\nComment 2"
  111. (org-test-with-temp-text "# Comment 1\n\n# Comment 2"
  112. (progn
  113. (transient-mark-mode 1)
  114. (push-mark (point) t t)
  115. (goto-char (point-max))
  116. (call-interactively 'comment-dwim)
  117. (buffer-string)))))
  118. ;; Region selected without comments: comment all lines if
  119. ;; `comment-empty-lines' is non-nil, only non-blank lines otherwise.
  120. (should
  121. (equal "# Comment 1\n\n# Comment 2"
  122. (org-test-with-temp-text "Comment 1\n\nComment 2"
  123. (progn
  124. (transient-mark-mode 1)
  125. (push-mark (point) t t)
  126. (goto-char (point-max))
  127. (let ((comment-empty-lines nil))
  128. (call-interactively 'comment-dwim))
  129. (buffer-string)))))
  130. (should
  131. (equal "# Comment 1\n# \n# Comment 2"
  132. (org-test-with-temp-text "Comment 1\n\nComment 2"
  133. (progn
  134. (transient-mark-mode 1)
  135. (push-mark (point) t t)
  136. (goto-char (point-max))
  137. (let ((comment-empty-lines t))
  138. (call-interactively 'comment-dwim))
  139. (buffer-string)))))
  140. ;; In front of a keyword without region, insert a new comment.
  141. (should
  142. (equal "# \n#+KEYWORD: value"
  143. (org-test-with-temp-text "#+KEYWORD: value"
  144. (progn (call-interactively 'comment-dwim)
  145. (buffer-string)))))
  146. ;; In a source block, use appropriate syntax.
  147. (should
  148. (equal " ;; "
  149. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n\n#+END_SRC"
  150. (forward-line)
  151. (let ((org-edit-src-content-indentation 2))
  152. (call-interactively 'comment-dwim))
  153. (buffer-substring-no-properties (line-beginning-position) (point)))))
  154. (should
  155. (equal "#+BEGIN_SRC emacs-lisp\n ;; a\n ;; b\n#+END_SRC"
  156. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\na\nb\n#+END_SRC"
  157. (forward-line)
  158. (transient-mark-mode 1)
  159. (push-mark (point) t t)
  160. (forward-line 2)
  161. (let ((org-edit-src-content-indentation 2))
  162. (call-interactively 'comment-dwim))
  163. (buffer-string)))))
  164. ;;; Date and time analysis
  165. (ert-deftest test-org/org-read-date ()
  166. "Test `org-read-date' specifications."
  167. ;; Parse ISO date with abbreviated year and month.
  168. (should (equal "2012-03-29 16:40"
  169. (let ((org-time-was-given t))
  170. (org-read-date t nil "12-3-29 16:40"))))
  171. ;; Parse Europeans dates.
  172. (should (equal "2012-03-29 16:40"
  173. (let ((org-time-was-given t))
  174. (org-read-date t nil "29.03.2012 16:40"))))
  175. ;; Parse Europeans dates without year.
  176. (should (string-match "2[0-9]\\{3\\}-03-29 16:40"
  177. (let ((org-time-was-given t))
  178. (org-read-date t nil "29.03. 16:40"))))
  179. ;; Relative date applied to current time if there is single
  180. ;; plus/minus, or to default date when there are two of them.
  181. (should
  182. (equal
  183. "2015-03-04"
  184. (cl-letf (((symbol-function 'current-time)
  185. (lambda ()
  186. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  187. (org-read-date
  188. t nil "+1y" nil
  189. (apply #'encode-time (org-parse-time-string "2012-03-29"))))))
  190. (should
  191. (equal
  192. "2013-03-29"
  193. (cl-letf (((symbol-function 'current-time)
  194. (lambda ()
  195. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  196. (org-read-date
  197. t nil "++1y" nil
  198. (apply #'encode-time (org-parse-time-string "2012-03-29"))))))
  199. ;; When `org-read-date-prefer-future' is non-nil, prefer future
  200. ;; dates (relatively to now) when incomplete. Otherwise, use
  201. ;; default date.
  202. (should
  203. (equal
  204. "2014-04-01"
  205. (cl-letf (((symbol-function 'current-time)
  206. (lambda ()
  207. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  208. (let ((org-read-date-prefer-future t))
  209. (org-read-date t nil "1")))))
  210. (should
  211. (equal
  212. "2013-03-04"
  213. (cl-letf (((symbol-function 'current-time)
  214. (lambda ()
  215. (apply #'encode-time (org-parse-time-string "2012-03-29")))))
  216. (let ((org-read-date-prefer-future t))
  217. (org-read-date t nil "3-4")))))
  218. (should
  219. (equal
  220. "2012-03-04"
  221. (cl-letf (((symbol-function 'current-time)
  222. (lambda ()
  223. (apply #'encode-time (org-parse-time-string "2012-03-29")))))
  224. (let ((org-read-date-prefer-future nil))
  225. (org-read-date t nil "3-4")))))
  226. ;; When set to `org-read-date-prefer-future' is set to `time', read
  227. ;; day is moved to tomorrow if specified hour is before current
  228. ;; time. However, it only happens in no other part of the date is
  229. ;; specified.
  230. (should
  231. (equal
  232. "2012-03-30"
  233. (cl-letf (((symbol-function 'current-time)
  234. (lambda ()
  235. (apply #'encode-time (org-parse-time-string "2012-03-29 16:40")))))
  236. (let ((org-read-date-prefer-future 'time))
  237. (org-read-date t nil "00:40" nil)))))
  238. (should-not
  239. (equal
  240. "2012-03-30"
  241. (cl-letf (((symbol-function 'current-time)
  242. (lambda ()
  243. (apply #'encode-time (org-parse-time-string "2012-03-29 16:40")))))
  244. (let ((org-read-date-prefer-future 'time))
  245. (org-read-date t nil "29 00:40" nil)))))
  246. ;; Caveat: `org-read-date-prefer-future' always refers to current
  247. ;; time, not default time, when they differ.
  248. (should
  249. (equal
  250. "2014-04-01"
  251. (cl-letf (((symbol-function 'current-time)
  252. (lambda ()
  253. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  254. (let ((org-read-date-prefer-future t))
  255. (org-read-date
  256. t nil "1" nil
  257. (apply #'encode-time (org-parse-time-string "2012-03-29")))))))
  258. (should
  259. (equal
  260. "2014-03-25"
  261. (cl-letf (((symbol-function 'current-time)
  262. (lambda ()
  263. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  264. (let ((org-read-date-prefer-future t))
  265. (org-read-date
  266. t nil "25" nil
  267. (apply #'encode-time (org-parse-time-string "2012-03-29"))))))))
  268. (ert-deftest test-org/org-parse-time-string ()
  269. "Test `org-parse-time-string'."
  270. (should (equal (org-parse-time-string "2012-03-29 16:40")
  271. '(0 40 16 29 3 2012 nil nil nil)))
  272. (should (equal (org-parse-time-string "[2012-03-29 16:40]")
  273. '(0 40 16 29 3 2012 nil nil nil)))
  274. (should (equal (org-parse-time-string "<2012-03-29 16:40>")
  275. '(0 40 16 29 3 2012 nil nil nil)))
  276. (should (equal (org-parse-time-string "<2012-03-29>")
  277. '(0 0 0 29 3 2012 nil nil nil)))
  278. (should (equal (org-parse-time-string "<2012-03-29>" t)
  279. '(0 nil nil 29 3 2012 nil nil nil))))
  280. (ert-deftest test-org/closest-date ()
  281. "Test `org-closest-date' specifications."
  282. (require 'calendar)
  283. ;; Time stamps without a repeater are returned unchanged.
  284. (should
  285. (equal
  286. '(3 29 2012)
  287. (calendar-gregorian-from-absolute
  288. (org-closest-date "<2012-03-29>" "<2014-03-04>" nil))))
  289. ;; Time stamps with a null repeater are returned unchanged.
  290. (should
  291. (equal
  292. '(3 29 2012)
  293. (calendar-gregorian-from-absolute
  294. (org-closest-date "<2012-03-29 +0d>" "<2014-03-04>" nil))))
  295. ;; if PREFER is set to `past' always return a date before, or equal
  296. ;; to CURRENT.
  297. (should
  298. (equal
  299. '(3 1 2014)
  300. (calendar-gregorian-from-absolute
  301. (org-closest-date "<2012-03-29 +1m>" "<2014-03-04>" 'past))))
  302. (should
  303. (equal
  304. '(3 4 2014)
  305. (calendar-gregorian-from-absolute
  306. (org-closest-date "<2012-03-04 +1m>" "<2014-03-04>" 'past))))
  307. ;; if PREFER is set to `future' always return a date before, or equal
  308. ;; to CURRENT.
  309. (should
  310. (equal
  311. '(3 29 2014)
  312. (calendar-gregorian-from-absolute
  313. (org-closest-date "<2012-03-29 +1m>" "<2014-03-04>" 'future))))
  314. (should
  315. (equal
  316. '(3 4 2014)
  317. (calendar-gregorian-from-absolute
  318. (org-closest-date "<2012-03-04 +1m>" "<2014-03-04>" 'future))))
  319. ;; If PREFER is neither `past' nor `future', select closest date.
  320. (should
  321. (equal
  322. '(3 1 2014)
  323. (calendar-gregorian-from-absolute
  324. (org-closest-date "<2012-03-29 +1m>" "<2014-03-04>" nil))))
  325. (should
  326. (equal
  327. '(5 4 2014)
  328. (calendar-gregorian-from-absolute
  329. (org-closest-date "<2012-03-04 +1m>" "<2014-04-28>" nil))))
  330. ;; Test "day" repeater.
  331. (should
  332. (equal '(3 8 2014)
  333. (calendar-gregorian-from-absolute
  334. (org-closest-date "<2014-03-04 +2d>" "<2014-03-09>" 'past))))
  335. (should
  336. (equal '(3 10 2014)
  337. (calendar-gregorian-from-absolute
  338. (org-closest-date "<2014-03-04 +2d>" "<2014-03-09>" 'future))))
  339. ;; Test "month" repeater.
  340. (should
  341. (equal '(1 5 2015)
  342. (calendar-gregorian-from-absolute
  343. (org-closest-date "<2014-03-05 +2m>" "<2015-02-04>" 'past))))
  344. (should
  345. (equal '(3 29 2014)
  346. (calendar-gregorian-from-absolute
  347. (org-closest-date "<2012-03-29 +2m>" "<2014-03-04>" 'future))))
  348. ;; Test "year" repeater.
  349. (should
  350. (equal '(3 5 2014)
  351. (calendar-gregorian-from-absolute
  352. (org-closest-date "<2014-03-05 +2y>" "<2015-02-04>" 'past))))
  353. (should
  354. (equal '(3 29 2014)
  355. (calendar-gregorian-from-absolute
  356. (org-closest-date "<2012-03-29 +2y>" "<2014-03-04>" 'future)))))
  357. (ert-deftest test-org/deadline-close-p ()
  358. "Test `org-deadline-close-p' specifications."
  359. ;; Pretend that the current time is 2016-06-03 Fri 01:43
  360. (cl-letf (((symbol-function 'current-time)
  361. (lambda () '(22353 6425 905205 644000))))
  362. ;; Timestamps are close if they are within `ndays' of lead time.
  363. (org-test-with-temp-text "* Heading"
  364. (should (org-deadline-close-p "2016-06-03 Fri" 0))
  365. (should (org-deadline-close-p "2016-06-02 Thu" 0))
  366. (should-not (org-deadline-close-p "2016-06-04 Sat" 0))
  367. (should (org-deadline-close-p "2016-06-04 Sat" 1))
  368. (should (org-deadline-close-p "2016-06-03 Fri 12:00" 0)))
  369. ;; Read `ndays' from timestamp if argument not given.
  370. (org-test-with-temp-text "* H"
  371. (should (org-deadline-close-p "2016-06-04 Sat -1d"))
  372. (should-not (org-deadline-close-p "2016-06-04 Sat -0d"))
  373. (should (org-deadline-close-p "2016-06-10 Fri -1w"))
  374. (should-not (org-deadline-close-p "2016-06-11 Sat -1w")))
  375. ;; Prefer `ndays' argument over lead time in timestamp.
  376. (org-test-with-temp-text "* H"
  377. (should (org-deadline-close-p "2016-06-04 Sat -0d" 1))
  378. (should-not (org-deadline-close-p "2016-06-04 Sat -0d" 0)))
  379. ;; Completed tasks are never close.
  380. (let ((org-todo-keywords '(("TODO" "|" "DONE"))))
  381. (org-test-with-temp-text "* TODO Heading"
  382. (should (org-deadline-close-p "2016-06-03")))
  383. (org-test-with-temp-text "* DONE Heading"
  384. (should-not (org-deadline-close-p "2016-06-03"))))))
  385. ;;; Drawers
  386. (ert-deftest test-org/insert-property-drawer ()
  387. "Test `org-insert-property-drawer' specifications."
  388. ;; Error before first headline.
  389. (should-error (org-test-with-temp-text "" (org-insert-property-drawer)))
  390. ;; Insert drawer right after headline if there is no planning line,
  391. ;; or after it otherwise.
  392. (should
  393. (equal "* H\n:PROPERTIES:\n:END:\nParagraph"
  394. (org-test-with-temp-text "* H\nParagraph<point>"
  395. (let ((org-adapt-indentation nil)) (org-insert-property-drawer))
  396. (buffer-string))))
  397. (should
  398. (equal "* H\nDEADLINE: <2014-03-04 tue.>\n:PROPERTIES:\n:END:\nParagraph"
  399. (org-test-with-temp-text
  400. "* H\nDEADLINE: <2014-03-04 tue.>\nParagraph<point>"
  401. (let ((org-adapt-indentation nil)) (org-insert-property-drawer))
  402. (buffer-string))))
  403. ;; Indent inserted drawer.
  404. (should
  405. (equal "* H\n :PROPERTIES:\n :END:\nParagraph"
  406. (org-test-with-temp-text "* H\nParagraph<point>"
  407. (let ((org-adapt-indentation t)) (org-insert-property-drawer))
  408. (buffer-string))))
  409. ;; Handle insertion at eob.
  410. (should
  411. (equal "* H\n:PROPERTIES:\n:END:\n"
  412. (org-test-with-temp-text "* H"
  413. (let ((org-adapt-indentation nil)) (org-insert-property-drawer))
  414. (buffer-string))))
  415. ;; Skip inlinetasks before point.
  416. (when (featurep 'org-inlinetask)
  417. (should
  418. (equal "* H\n:PROPERTIES:\n:END:\n*************** I\n*************** END\nP"
  419. (org-test-with-temp-text
  420. "* H\n*************** I\n*************** END\nP<point>"
  421. (let ((org-adapt-indentation nil)
  422. (org-inlinetask-min-level 15))
  423. (org-insert-property-drawer))
  424. (buffer-string)))))
  425. ;; Correctly set drawer in an inlinetask.
  426. (when (featurep 'org-inlinetask)
  427. (should
  428. (equal "* H\n*************** I\n:PROPERTIES:\n:END:\nP\n*************** END"
  429. (org-test-with-temp-text
  430. "* H\n*************** I\nP<point>\n*************** END"
  431. (let ((org-adapt-indentation nil)
  432. (org-inlinetask-min-level 15))
  433. (org-insert-property-drawer))
  434. (buffer-string))))))
  435. ;;; Filling
  436. (ert-deftest test-org/fill-paragraph ()
  437. "Test `org-fill-paragraph' specifications."
  438. ;; At an Org table, align it.
  439. (should
  440. (equal "| a |\n"
  441. (org-test-with-temp-text "|a|"
  442. (org-fill-paragraph)
  443. (buffer-string))))
  444. (should
  445. (equal "#+name: table\n| a |\n"
  446. (org-test-with-temp-text "#+name: table\n| a |\n"
  447. (org-fill-paragraph)
  448. (buffer-string))))
  449. ;; At a paragraph, preserve line breaks.
  450. (org-test-with-temp-text "some \\\\\nlong\ntext"
  451. (let ((fill-column 20))
  452. (org-fill-paragraph)
  453. (should (equal (buffer-string) "some \\\\\nlong text"))))
  454. ;; Correctly fill a paragraph when point is at its very end.
  455. (should
  456. (equal "A B"
  457. (org-test-with-temp-text "A\nB"
  458. (let ((fill-column 20))
  459. (goto-char (point-max))
  460. (org-fill-paragraph)
  461. (buffer-string)))))
  462. ;; Correctly fill the last paragraph of a greater element.
  463. (should
  464. (equal "#+BEGIN_CENTER\n- 012345\n 789\n#+END_CENTER"
  465. (org-test-with-temp-text "#+BEGIN_CENTER\n- 012345 789\n#+END_CENTER"
  466. (let ((fill-column 8))
  467. (forward-line)
  468. (end-of-line)
  469. (org-fill-paragraph)
  470. (buffer-string)))))
  471. ;; Correctly fill an element in a narrowed buffer.
  472. (should
  473. (equal "01234\n6"
  474. (org-test-with-temp-text "01234 6789"
  475. (let ((fill-column 5))
  476. (narrow-to-region 1 8)
  477. (org-fill-paragraph)
  478. (buffer-string)))))
  479. ;; Handle `adaptive-fill-regexp' in paragraphs.
  480. (should
  481. (equal "> a b"
  482. (org-test-with-temp-text "> a\n> b"
  483. (let ((fill-column 5)
  484. (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
  485. (org-fill-paragraph)
  486. (buffer-string)))))
  487. ;; Special case: Fill first paragraph when point is at an item or
  488. ;; a plain-list or a footnote reference.
  489. (should
  490. (equal "- A B"
  491. (org-test-with-temp-text "- A\n B"
  492. (let ((fill-column 20))
  493. (org-fill-paragraph)
  494. (buffer-string)))))
  495. (should
  496. (equal "[fn:1] A B"
  497. (org-test-with-temp-text "[fn:1] A\nB"
  498. (let ((fill-column 20))
  499. (org-fill-paragraph)
  500. (buffer-string)))))
  501. (org-test-with-temp-text "#+BEGIN_VERSE\nSome \\\\\nlong\ntext\n#+END_VERSE"
  502. (let ((fill-column 20))
  503. (org-fill-paragraph)
  504. (should (equal (buffer-string)
  505. "#+BEGIN_VERSE\nSome \\\\\nlong\ntext\n#+END_VERSE"))))
  506. ;; Fill contents of `comment-block' elements.
  507. (should
  508. (equal
  509. (org-test-with-temp-text "#+BEGIN_COMMENT\nSome\ntext\n#+END_COMMENT"
  510. (let ((fill-column 20))
  511. (forward-line)
  512. (org-fill-paragraph)
  513. (buffer-string)))
  514. "#+BEGIN_COMMENT\nSome text\n#+END_COMMENT"))
  515. ;; Fill `comment' elements.
  516. (should
  517. (equal " # A B"
  518. (org-test-with-temp-text " # A\n # B"
  519. (let ((fill-column 20))
  520. (org-fill-paragraph)
  521. (buffer-string)))))
  522. ;; Do not mix consecutive comments when filling one of them.
  523. (should
  524. (equal "# A B\n\n# C"
  525. (org-test-with-temp-text "# A\n# B\n\n# C"
  526. (let ((fill-column 20))
  527. (org-fill-paragraph)
  528. (buffer-string)))))
  529. ;; Use commented empty lines as separators when filling comments.
  530. (should
  531. (equal "# A B\n#\n# C"
  532. (org-test-with-temp-text "# A\n# B\n#\n# C"
  533. (let ((fill-column 20))
  534. (org-fill-paragraph)
  535. (buffer-string)))))
  536. ;; Handle `adaptive-fill-regexp' in comments.
  537. (should
  538. (equal "# > a b"
  539. (org-test-with-temp-text "# > a\n# > b"
  540. (let ((fill-column 20)
  541. (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
  542. (org-fill-paragraph)
  543. (buffer-string)))))
  544. ;; Do nothing at affiliated keywords.
  545. (org-test-with-temp-text "#+NAME: para\nSome\ntext."
  546. (let ((fill-column 20))
  547. (org-fill-paragraph)
  548. (should (equal (buffer-string) "#+NAME: para\nSome\ntext."))))
  549. ;; Do not move point after table when filling a table.
  550. (should-not
  551. (org-test-with-temp-text "| a | b |\n| c | d |\n"
  552. (forward-char)
  553. (org-fill-paragraph)
  554. (eobp))))
  555. (ert-deftest test-org/auto-fill-function ()
  556. "Test auto-filling features."
  557. ;; Auto fill paragraph.
  558. (should
  559. (equal "12345\n7890"
  560. (org-test-with-temp-text "12345 7890"
  561. (let ((fill-column 5))
  562. (end-of-line)
  563. (org-auto-fill-function)
  564. (buffer-string)))))
  565. ;; Auto fill first paragraph in an item.
  566. (should
  567. (equal "- 12345\n 7890"
  568. (org-test-with-temp-text "- 12345 7890"
  569. (let ((fill-column 7))
  570. (end-of-line)
  571. (org-auto-fill-function)
  572. (buffer-string)))))
  573. ;; Auto fill paragraph when `adaptive-fill-regexp' matches.
  574. (should
  575. (equal "> 12345\n 7890"
  576. (org-test-with-temp-text "> 12345 7890"
  577. (let ((fill-column 10)
  578. (adaptive-fill-regexp "[ \t]*>+[ \t]*")
  579. (adaptive-fill-first-line-regexp "\\`[ ]*\\'"))
  580. (end-of-line)
  581. (org-auto-fill-function)
  582. (buffer-string)))))
  583. (should
  584. (equal "> 12345\n> 12345\n> 7890"
  585. (org-test-with-temp-text "> 12345\n> 12345 7890"
  586. (let ((fill-column 10)
  587. (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
  588. (goto-char (point-max))
  589. (org-auto-fill-function)
  590. (buffer-string)))))
  591. (should-not
  592. (equal " 12345\n *12345\n *12345"
  593. (org-test-with-temp-text " 12345\n *12345 12345"
  594. (let ((fill-column 10)
  595. (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
  596. (goto-char (point-max))
  597. (org-auto-fill-function)
  598. (buffer-string)))))
  599. ;; Auto fill comments.
  600. (should
  601. (equal " # 12345\n # 7890"
  602. (org-test-with-temp-text " # 12345 7890"
  603. (let ((fill-column 10))
  604. (end-of-line)
  605. (org-auto-fill-function)
  606. (buffer-string)))))
  607. ;; A hash within a line isn't a comment.
  608. (should-not
  609. (equal "12345 # 7890\n# 1"
  610. (org-test-with-temp-text "12345 # 7890 1"
  611. (let ((fill-column 12))
  612. (end-of-line)
  613. (org-auto-fill-function)
  614. (buffer-string)))))
  615. ;; Correctly interpret empty prefix.
  616. (should-not
  617. (equal "# a\n# b\nRegular\n# paragraph"
  618. (org-test-with-temp-text "# a\n# b\nRegular paragraph"
  619. (let ((fill-column 12))
  620. (end-of-line 3)
  621. (org-auto-fill-function)
  622. (buffer-string)))))
  623. ;; Comment block: auto fill contents.
  624. (should
  625. (equal "#+BEGIN_COMMENT\n12345\n7890\n#+END_COMMENT"
  626. (org-test-with-temp-text "#+BEGIN_COMMENT\n12345 7890\n#+END_COMMENT"
  627. (let ((fill-column 5))
  628. (forward-line)
  629. (end-of-line)
  630. (org-auto-fill-function)
  631. (buffer-string)))))
  632. (should
  633. (equal "#+BEGIN_COMMENT\n12345\n7890\n#+END_COMMENT"
  634. (org-test-with-temp-text "#+BEGIN_COMMENT\n12345 7890\n#+END_COMMENT"
  635. (let ((fill-column 5))
  636. (forward-line)
  637. (end-of-line)
  638. (org-auto-fill-function)
  639. (buffer-string)))))
  640. ;; Do not fill if a new item could be created.
  641. (should-not
  642. (equal "12345\n- 90"
  643. (org-test-with-temp-text "12345 - 90"
  644. (let ((fill-column 5))
  645. (end-of-line)
  646. (org-auto-fill-function)
  647. (buffer-string)))))
  648. ;; Do not fill if a line break could be introduced.
  649. (should-not
  650. (equal "123\\\\\n7890"
  651. (org-test-with-temp-text "123\\\\ 7890"
  652. (let ((fill-column 6))
  653. (end-of-line)
  654. (org-auto-fill-function)
  655. (buffer-string)))))
  656. ;; Do not fill affiliated keywords.
  657. (should-not
  658. (equal "#+ATTR_LATEX: ABC\nDEFGHIJKL"
  659. (org-test-with-temp-text "#+ATTR_LATEX: ABC DEFGHIJKL"
  660. (let ((fill-column 20))
  661. (end-of-line)
  662. (org-auto-fill-function)
  663. (buffer-string))))))
  664. ;;; Indentation
  665. (ert-deftest test-org/indent-line ()
  666. "Test `org-indent-line' specifications."
  667. ;; Do not indent diary sexps, footnote definitions or headlines.
  668. (should
  669. (zerop
  670. (org-test-with-temp-text "%%(org-calendar-holiday)"
  671. (org-indent-line)
  672. (org-get-indentation))))
  673. (should
  674. (zerop
  675. (org-test-with-temp-text "[fn:1] fn"
  676. (let ((org-adapt-indentation t)) (org-indent-line))
  677. (org-get-indentation))))
  678. (should
  679. (zerop
  680. (org-test-with-temp-text "* H"
  681. (org-indent-line)
  682. (org-get-indentation))))
  683. ;; Do not indent before first headline.
  684. (should
  685. (zerop
  686. (org-test-with-temp-text ""
  687. (org-indent-line)
  688. (org-get-indentation))))
  689. ;; Indent according to headline level otherwise, unless
  690. ;; `org-adapt-indentation' is nil.
  691. (should
  692. (= 2
  693. (org-test-with-temp-text "* H\nA"
  694. (forward-line)
  695. (let ((org-adapt-indentation t)) (org-indent-line))
  696. (org-get-indentation))))
  697. (should
  698. (= 2
  699. (org-test-with-temp-text "* H\n\nA"
  700. (forward-line)
  701. (let ((org-adapt-indentation t)) (org-indent-line))
  702. (org-get-indentation))))
  703. (should
  704. (zerop
  705. (org-test-with-temp-text "* H\nA"
  706. (forward-line)
  707. (let ((org-adapt-indentation nil)) (org-indent-line))
  708. (org-get-indentation))))
  709. ;; Indenting preserves point position.
  710. (should
  711. (org-test-with-temp-text "* H\nAB"
  712. (forward-line)
  713. (forward-char)
  714. (let ((org-adapt-indentation t)) (org-indent-line))
  715. (looking-at "B")))
  716. ;; Do not change indentation at an item.
  717. (should
  718. (= 1
  719. (org-test-with-temp-text "* H\n - A"
  720. (forward-line)
  721. (let ((org-adapt-indentation t)) (org-indent-line))
  722. (org-get-indentation))))
  723. ;; On blank lines at the end of a list, indent like last element
  724. ;; within it if the line is still in the list. If the last element
  725. ;; is an item, indent like its contents. Otherwise, indent like the
  726. ;; whole list.
  727. (should
  728. (= 4
  729. (org-test-with-temp-text "* H\n- A\n - AA\n<point>"
  730. (let ((org-adapt-indentation t)) (org-indent-line))
  731. (org-get-indentation))))
  732. (should
  733. (= 4
  734. (org-test-with-temp-text "* H\n- A\n -\n\n<point>"
  735. (let ((org-adapt-indentation t)) (org-indent-line))
  736. (org-get-indentation))))
  737. (should
  738. (zerop
  739. (org-test-with-temp-text "* H\n- A\n - AA\n\n\n\n<point>"
  740. (let ((org-adapt-indentation t)) (org-indent-line))
  741. (org-get-indentation))))
  742. (should
  743. (= 4
  744. (org-test-with-temp-text "* H\n- A\n - \n<point>"
  745. (let ((org-adapt-indentation t)) (org-indent-line))
  746. (org-get-indentation))))
  747. (should
  748. (= 4
  749. (org-test-with-temp-text
  750. "* H\n - \n #+BEGIN_SRC emacs-lisp\n t\n #+END_SRC\n<point>"
  751. (let ((org-adapt-indentation t)) (org-indent-line))
  752. (org-get-indentation))))
  753. (should
  754. (= 2
  755. (org-test-with-temp-text "- A\n B\n\n<point>"
  756. (let ((org-adapt-indentation nil)) (org-indent-line))
  757. (org-get-indentation))))
  758. ;; Likewise, on a blank line at the end of a footnote definition,
  759. ;; indent at column 0 if line belongs to the definition. Otherwise,
  760. ;; indent like the definition itself.
  761. (should
  762. (zerop
  763. (org-test-with-temp-text "* H\n[fn:1] Definition\n"
  764. (goto-char (point-max))
  765. (let ((org-adapt-indentation t)) (org-indent-line))
  766. (org-get-indentation))))
  767. (should
  768. (zerop
  769. (org-test-with-temp-text "* H\n[fn:1] Definition\n\n\n\n"
  770. (goto-char (point-max))
  771. (let ((org-adapt-indentation t)) (org-indent-line))
  772. (org-get-indentation))))
  773. ;; After the end of the contents of a greater element, indent like
  774. ;; the beginning of the element.
  775. (should
  776. (= 1
  777. (org-test-with-temp-text " #+BEGIN_CENTER\n Contents\n#+END_CENTER"
  778. (forward-line 2)
  779. (org-indent-line)
  780. (org-get-indentation))))
  781. ;; On blank lines after a paragraph, indent like its last non-empty
  782. ;; line.
  783. (should
  784. (= 1
  785. (org-test-with-temp-text " Paragraph\n\n<point>"
  786. (org-indent-line)
  787. (org-get-indentation))))
  788. ;; At the first line of an element, indent like previous element's
  789. ;; first line, ignoring footnotes definitions and inline tasks, or
  790. ;; according to parent.
  791. (should
  792. (= 2
  793. (org-test-with-temp-text "A\n\n B\n\nC"
  794. (goto-char (point-max))
  795. (org-indent-line)
  796. (org-get-indentation))))
  797. (should
  798. (= 1
  799. (org-test-with-temp-text " A\n\n[fn:1] B\n\n\nC"
  800. (goto-char (point-max))
  801. (org-indent-line)
  802. (org-get-indentation))))
  803. (should
  804. (= 1
  805. (org-test-with-temp-text " #+BEGIN_CENTER\n Contents\n#+END_CENTER"
  806. (forward-line 1)
  807. (org-indent-line)
  808. (org-get-indentation))))
  809. ;; Within code part of a source block, use language major mode if
  810. ;; `org-src-tab-acts-natively' is non-nil. Otherwise, indent
  811. ;; according to line above.
  812. (should
  813. (= 6
  814. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
  815. (forward-line 2)
  816. (let ((org-src-tab-acts-natively t)
  817. (org-edit-src-content-indentation 0))
  818. (org-indent-line))
  819. (org-get-indentation))))
  820. (should
  821. (= 1
  822. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
  823. (forward-line 2)
  824. (let ((org-src-tab-acts-natively nil)
  825. (org-edit-src-content-indentation 0))
  826. (org-indent-line))
  827. (org-get-indentation))))
  828. ;; Otherwise, indent like the first non-blank line above.
  829. (should
  830. (zerop
  831. (org-test-with-temp-text "#+BEGIN_CENTER\nline1\n\n line2\n#+END_CENTER"
  832. (forward-line 3)
  833. (org-indent-line)
  834. (org-get-indentation))))
  835. ;; Align node properties according to `org-property-format'. Handle
  836. ;; nicely empty values.
  837. (should
  838. (equal "* H\n:PROPERTIES:\n:key: value\n:END:"
  839. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:key: value\n:END:"
  840. (let ((org-property-format "%-10s %s")) (org-indent-line))
  841. (buffer-string))))
  842. (should
  843. (equal "* H\n:PROPERTIES:\n:key:\n:END:"
  844. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:key:\n:END:"
  845. (let ((org-property-format "%-10s %s")) (org-indent-line))
  846. (buffer-string)))))
  847. (ert-deftest test-org/indent-region ()
  848. "Test `org-indent-region' specifications."
  849. ;; Indent paragraph.
  850. (should
  851. (equal "A\nB\nC"
  852. (org-test-with-temp-text " A\nB\n C"
  853. (org-indent-region (point-min) (point-max))
  854. (buffer-string))))
  855. ;; Indent greater elements along with their contents.
  856. (should
  857. (equal "#+BEGIN_CENTER\nA\nB\n#+END_CENTER"
  858. (org-test-with-temp-text "#+BEGIN_CENTER\n A\n B\n#+END_CENTER"
  859. (org-indent-region (point-min) (point-max))
  860. (buffer-string))))
  861. ;; Ignore contents of verse blocks and example blocks.
  862. (should
  863. (equal "#+BEGIN_VERSE\n A\n B\n#+END_VERSE"
  864. (org-test-with-temp-text "#+BEGIN_VERSE\n A\n B\n#+END_VERSE"
  865. (org-indent-region (point-min) (point-max))
  866. (buffer-string))))
  867. (should
  868. (equal "#+BEGIN_EXAMPLE\n A\n B\n#+END_EXAMPLE"
  869. (org-test-with-temp-text "#+BEGIN_EXAMPLE\n A\n B\n#+END_EXAMPLE"
  870. (org-indent-region (point-min) (point-max))
  871. (buffer-string))))
  872. ;; Indent according to mode if `org-src-tab-acts-natively' is
  873. ;; non-nil. Otherwise, do not indent code at all.
  874. (should
  875. (equal "#+BEGIN_SRC emacs-lisp\n(and A\n B)\n#+END_SRC"
  876. (org-test-with-temp-text
  877. "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
  878. (let ((org-src-tab-acts-natively t)
  879. (org-edit-src-content-indentation 0))
  880. (org-indent-region (point-min) (point-max)))
  881. (buffer-string))))
  882. (should
  883. (equal "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
  884. (org-test-with-temp-text
  885. "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
  886. (let ((org-src-tab-acts-natively nil)
  887. (org-edit-src-content-indentation 0))
  888. (org-indent-region (point-min) (point-max)))
  889. (buffer-string))))
  890. ;; Align node properties according to `org-property-format'. Handle
  891. ;; nicely empty values.
  892. (should
  893. (equal "* H\n:PROPERTIES:\n:key: value\n:END:"
  894. (org-test-with-temp-text "* H\n<point>:PROPERTIES:\n:key: value\n:END:"
  895. (let ((org-property-format "%-10s %s")
  896. (org-adapt-indentation nil))
  897. (org-indent-region (point) (point-max)))
  898. (buffer-string))))
  899. (should
  900. (equal "* H\n:PROPERTIES:\n:key:\n:END:"
  901. (org-test-with-temp-text "* H\n<point>:PROPERTIES:\n:key:\n:END:"
  902. (let ((org-property-format "%-10s %s")
  903. (org-adapt-indentation nil))
  904. (org-indent-region (point) (point-max)))
  905. (buffer-string))))
  906. ;; Indent plain lists.
  907. (should
  908. (equal "- A\n B\n - C\n\n D"
  909. (org-test-with-temp-text "- A\n B\n - C\n\n D"
  910. (org-indent-region (point-min) (point-max))
  911. (buffer-string))))
  912. (should
  913. (equal "- A\n\n- B"
  914. (org-test-with-temp-text " - A\n\n - B"
  915. (org-indent-region (point-min) (point-max))
  916. (buffer-string))))
  917. ;; Indent footnote definitions.
  918. (should
  919. (equal "[fn:1] Definition\n\nDefinition"
  920. (org-test-with-temp-text "[fn:1] Definition\n\n Definition"
  921. (org-indent-region (point-min) (point-max))
  922. (buffer-string))))
  923. ;; Special case: Start indenting on a blank line.
  924. (should
  925. (equal "\nParagraph"
  926. (org-test-with-temp-text "\n Paragraph"
  927. (org-indent-region (point-min) (point-max))
  928. (buffer-string)))))
  929. ;;; Editing
  930. (ert-deftest test-org/delete-indentation ()
  931. "Test `org-delete-indentation' specifications."
  932. ;; Regular test.
  933. (should (equal "foo bar"
  934. (org-test-with-temp-text
  935. "foo \n bar<point>"
  936. (org-delete-indentation)
  937. (buffer-string))))
  938. ;; With optional argument.
  939. (should (equal "foo bar"
  940. (org-test-with-temp-text
  941. "foo<point> \n bar"
  942. (org-delete-indentation t)
  943. (buffer-string))))
  944. ;; At headline text should be appended to the headline text.
  945. (should
  946. (equal"* foo bar :tag:"
  947. (let (org-auto-align-tags)
  948. (org-test-with-temp-text
  949. "* foo :tag:\n bar<point>"
  950. (org-delete-indentation)
  951. (buffer-string)))))
  952. (should
  953. (equal "* foo bar :tag:"
  954. (let (org-auto-align-tags)
  955. (org-test-with-temp-text
  956. "* foo <point>:tag:\n bar"
  957. (org-delete-indentation t)
  958. (buffer-string))))))
  959. (ert-deftest test-org/return ()
  960. "Test `org-return' specifications."
  961. ;; Regular test.
  962. (should
  963. (equal "Para\ngraph"
  964. (org-test-with-temp-text "Para<point>graph"
  965. (org-return)
  966. (buffer-string))))
  967. ;; With optional argument, indent line.
  968. (should
  969. (equal " Para\n graph"
  970. (org-test-with-temp-text " Para<point>graph"
  971. (org-return t)
  972. (buffer-string))))
  973. ;; On a table, call `org-table-next-row'.
  974. (should
  975. (org-test-with-temp-text "| <point>a |\n| b |"
  976. (org-return)
  977. (looking-at-p "b")))
  978. ;; Open link or timestamp under point when `org-return-follows-link'
  979. ;; is non-nil.
  980. (should
  981. (org-test-with-temp-text "Link [[target<point>]] <<target>>"
  982. (let ((org-return-follows-link t)
  983. (org-link-search-must-match-exact-headline nil))
  984. (org-return))
  985. (looking-at-p "<<target>>")))
  986. (should-not
  987. (org-test-with-temp-text "Link [[target<point>]] <<target>>"
  988. (let ((org-return-follows-link nil)) (org-return))
  989. (looking-at-p "<<target>>")))
  990. (should
  991. (org-test-with-temp-text "* [[b][a<point>]]\n* b"
  992. (let ((org-return-follows-link t)) (org-return))
  993. (looking-at-p "* b")))
  994. (should
  995. (org-test-with-temp-text "Link [[target][/descipt<point>ion/]] <<target>>"
  996. (let ((org-return-follows-link t)
  997. (org-link-search-must-match-exact-headline nil))
  998. (org-return))
  999. (looking-at-p "<<target>>")))
  1000. (should-not
  1001. (org-test-with-temp-text "Link [[target]]<point> <<target>>"
  1002. (let ((org-return-follows-link t)
  1003. (org-link-search-must-match-exact-headline nil))
  1004. (org-return))
  1005. (looking-at-p "<<target>>")))
  1006. ;; When `org-return-follows-link' is non-nil, tolerate links and
  1007. ;; timestamps in comments, node properties, etc.
  1008. (should
  1009. (org-test-with-temp-text "# Comment [[target<point>]]\n <<target>>"
  1010. (let ((org-return-follows-link t)
  1011. (org-link-search-must-match-exact-headline nil))
  1012. (org-return))
  1013. (looking-at-p "<<target>>")))
  1014. (should-not
  1015. (org-test-with-temp-text "# Comment [[target<point>]]\n <<target>>"
  1016. (let ((org-return-follows-link nil)) (org-return))
  1017. (looking-at-p "<<target>>")))
  1018. (should-not
  1019. (org-test-with-temp-text "# Comment [[target]]<point>\n <<target>>"
  1020. (let ((org-return-follows-link t)
  1021. (org-link-search-must-match-exact-headline nil))
  1022. (org-return))
  1023. (looking-at-p "<<target>>")))
  1024. ;; However, do not open link when point is in a table.
  1025. (should
  1026. (org-test-with-temp-text "| [[target<point>]] |\n| between |\n| <<target>> |"
  1027. (let ((org-return-follows-link t)) (org-return))
  1028. (looking-at-p "between")))
  1029. ;; Special case: in a list, when indenting, do not break structure.
  1030. (should
  1031. (equal "- A\n B"
  1032. (org-test-with-temp-text "- A <point>B"
  1033. (org-return t)
  1034. (buffer-string))))
  1035. (should
  1036. (equal "- A\n\n- B"
  1037. (org-test-with-temp-text "- A\n<point>- B"
  1038. (org-return t)
  1039. (buffer-string))))
  1040. ;; On tags part of a headline, add a newline below it instead of
  1041. ;; breaking it.
  1042. (should
  1043. (equal "* H :tag:\n"
  1044. (org-test-with-temp-text "* H :<point>tag:"
  1045. (org-return)
  1046. (buffer-string))))
  1047. ;; Before headline text, add a newline below it instead of breaking
  1048. ;; it.
  1049. (should
  1050. (equal "* TODO H :tag:\n"
  1051. (org-test-with-temp-text "* <point>TODO H :tag:"
  1052. (org-return)
  1053. (buffer-string))))
  1054. (should
  1055. (equal "* TODO [#B] H :tag:\n"
  1056. (org-test-with-temp-text "* TODO<point> [#B] H :tag:"
  1057. (org-return)
  1058. (buffer-string))))
  1059. ;; At headline text, break headline text but preserve tags.
  1060. (should
  1061. (equal "* TODO [#B] foo :tag:\nbar"
  1062. (let (org-auto-align-tags)
  1063. (org-test-with-temp-text "* TODO [#B] foo<point>bar :tag:"
  1064. (org-return)
  1065. (buffer-string)))))
  1066. ;; At bol of headline insert newline.
  1067. (should
  1068. (equal "\n* h"
  1069. (org-test-with-temp-text "<point>* h"
  1070. (org-return)
  1071. (buffer-string))))
  1072. ;; Refuse to leave invalid headline in buffer.
  1073. (should
  1074. (equal "* h\n"
  1075. (org-test-with-temp-text "*<point> h"
  1076. (org-return)
  1077. (buffer-string)))))
  1078. (ert-deftest test-org/meta-return ()
  1079. "Test M-RET (`org-meta-return') specifications."
  1080. ;; In a table field insert a row above.
  1081. (should
  1082. (org-test-with-temp-text "| a |"
  1083. (forward-char)
  1084. (org-meta-return)
  1085. (forward-line -1)
  1086. (looking-at "| |$")))
  1087. ;; In a paragraph change current line into a header.
  1088. (should
  1089. (org-test-with-temp-text "a"
  1090. (org-meta-return)
  1091. (beginning-of-line)
  1092. (looking-at "\* a$")))
  1093. ;; In an item insert an item, in this case above.
  1094. (should
  1095. (org-test-with-temp-text "- a"
  1096. (org-meta-return)
  1097. (beginning-of-line)
  1098. (looking-at "- $")))
  1099. ;; In a drawer and item insert an item, in this case above.
  1100. (should
  1101. (org-test-with-temp-text ":MYDRAWER:\n- a\n:END:"
  1102. (forward-line)
  1103. (org-meta-return)
  1104. (beginning-of-line)
  1105. (looking-at "- $"))))
  1106. (ert-deftest test-org/insert-heading ()
  1107. "Test `org-insert-heading' specifications."
  1108. ;; In an empty buffer, insert a new headline.
  1109. (should
  1110. (equal "* "
  1111. (org-test-with-temp-text ""
  1112. (org-insert-heading)
  1113. (buffer-string))))
  1114. ;; At the beginning of a line, turn it into a headline
  1115. (should
  1116. (equal "* P"
  1117. (org-test-with-temp-text "<point>P"
  1118. (org-insert-heading)
  1119. (buffer-string))))
  1120. ;; In the middle of a line, split the line if allowed, otherwise,
  1121. ;; insert the headline at its end.
  1122. (should
  1123. (equal "Para\n* graph"
  1124. (org-test-with-temp-text "Para<point>graph"
  1125. (let ((org-M-RET-may-split-line '((default . t))))
  1126. (org-insert-heading))
  1127. (buffer-string))))
  1128. (should
  1129. (equal "Paragraph\n* "
  1130. (org-test-with-temp-text "Para<point>graph"
  1131. (let ((org-M-RET-may-split-line '((default . nil))))
  1132. (org-insert-heading))
  1133. (buffer-string))))
  1134. ;; When on a list, insert an item instead, unless called with an
  1135. ;; universal argument or if list is invisible. In this case, create
  1136. ;; a new headline after contents.
  1137. (should
  1138. (equal "* H\n- item\n- "
  1139. (org-test-with-temp-text "* H\n- item<point>"
  1140. (let ((org-insert-heading-respect-content nil))
  1141. (org-insert-heading))
  1142. (buffer-string))))
  1143. (should
  1144. (equal "* H\n- item\n- item 2\n* "
  1145. (org-test-with-temp-text "* H\n- item<point>\n- item 2"
  1146. (let ((org-insert-heading-respect-content nil))
  1147. (org-insert-heading '(4)))
  1148. (buffer-string))))
  1149. (should
  1150. (equal "* H\n- item\n* "
  1151. (org-test-with-temp-text "* H\n- item"
  1152. (org-cycle)
  1153. (goto-char (point-max))
  1154. (let ((org-insert-heading-respect-content nil)) (org-insert-heading))
  1155. (buffer-string))))
  1156. ;; Preserve list visibility when inserting an item.
  1157. (should
  1158. (equal
  1159. '(outline outline)
  1160. (org-test-with-temp-text "- A\n - B\n- C\n - D"
  1161. (let ((org-cycle-include-plain-lists t))
  1162. (org-cycle)
  1163. (forward-line 2)
  1164. (org-cycle)
  1165. (let ((org-insert-heading-respect-content nil)) (org-insert-heading))
  1166. (list (get-char-property (line-beginning-position 0) 'invisible)
  1167. (get-char-property (line-end-position 2) 'invisible))))))
  1168. ;; When called with two universal arguments, insert a new headline
  1169. ;; at the end of the grandparent subtree.
  1170. (should
  1171. (equal "* H1\n** H3\n- item\n** H2\n** "
  1172. (org-test-with-temp-text "* H1\n** H3\n- item<point>\n** H2"
  1173. (let ((org-insert-heading-respect-content nil))
  1174. (org-insert-heading '(16)))
  1175. (buffer-string))))
  1176. ;; When optional TOP-LEVEL argument is non-nil, always insert
  1177. ;; a level 1 heading.
  1178. (should
  1179. (equal "* H1\n** H2\n* "
  1180. (org-test-with-temp-text "* H1\n** H2<point>"
  1181. (org-insert-heading nil nil t)
  1182. (buffer-string))))
  1183. (should
  1184. (equal "* H1\n- item\n* "
  1185. (org-test-with-temp-text "* H1\n- item<point>"
  1186. (org-insert-heading nil nil t)
  1187. (buffer-string))))
  1188. ;; Corner case: correctly insert a headline after an empty one.
  1189. (should
  1190. (equal "* \n* "
  1191. (org-test-with-temp-text "* <point>"
  1192. (org-insert-heading)
  1193. (buffer-string)))))
  1194. (ert-deftest test-org/insert-todo-heading-respect-content ()
  1195. "Test `org-insert-todo-heading-respect-content' specifications."
  1196. ;; Create a TODO heading.
  1197. (should
  1198. (org-test-with-temp-text "* H1\n Body"
  1199. (org-insert-todo-heading-respect-content)
  1200. (nth 2 (org-heading-components))))
  1201. ;; Add headline at the end of the first subtree
  1202. (should
  1203. (org-test-with-temp-text "* H1\nH1Body\n** H2\nH2Body"
  1204. (search-forward "H1Body")
  1205. (org-insert-todo-heading-respect-content)
  1206. (and (eobp) (org-at-heading-p))))
  1207. ;; In a list, do not create a new item.
  1208. (should
  1209. (org-test-with-temp-text "* H\n- an item\n- another one"
  1210. (search-forward "an ")
  1211. (org-insert-todo-heading-respect-content)
  1212. (and (eobp) (org-at-heading-p)))))
  1213. (ert-deftest test-org/clone-with-time-shift ()
  1214. "Test `org-clone-subtree-with-time-shift'."
  1215. ;; Clone non-repeating once.
  1216. (should
  1217. (equal "\
  1218. * H1\n<2015-06-21>
  1219. * H1\n<2015-06-23>
  1220. "
  1221. (org-test-with-temp-text "* H1\n<2015-06-21 Sun>"
  1222. (org-clone-subtree-with-time-shift 1 "+2d")
  1223. (replace-regexp-in-string
  1224. "\\( [.A-Za-z]+\\)\\( \\+[0-9][hdmwy]\\)?>" "" (buffer-string)
  1225. nil nil 1))))
  1226. ;; Clone repeating once.
  1227. (should
  1228. (equal "\
  1229. * H1\n<2015-06-21>
  1230. * H1\n<2015-06-23>
  1231. * H1\n<2015-06-25 +1w>
  1232. "
  1233. (org-test-with-temp-text "* H1\n<2015-06-21 Sun +1w>"
  1234. (org-clone-subtree-with-time-shift 1 "+2d")
  1235. (replace-regexp-in-string
  1236. "\\( [.A-Za-z]+\\)\\( \\+[0-9][hdmwy]\\)?>" "" (buffer-string)
  1237. nil nil 1))))
  1238. ;; Clone non-repeating zero times.
  1239. (should
  1240. (equal "\
  1241. * H1\n<2015-06-21>
  1242. "
  1243. (org-test-with-temp-text "* H1\n<2015-06-21 Sun>"
  1244. (org-clone-subtree-with-time-shift 0 "+2d")
  1245. (replace-regexp-in-string
  1246. "\\( [.A-Za-z]+\\)\\( \\+[0-9][hdmwy]\\)?>" "" (buffer-string)
  1247. nil nil 1))))
  1248. ;; Clone repeating "zero" times.
  1249. (should
  1250. (equal "\
  1251. * H1\n<2015-06-21>
  1252. * H1\n<2015-06-23 +1w>
  1253. "
  1254. (org-test-with-temp-text "* H1\n<2015-06-21 Sun +1w>"
  1255. (org-clone-subtree-with-time-shift 0 "+2d")
  1256. (replace-regexp-in-string
  1257. "\\( [.A-Za-z]+\\)\\( \\+[0-9][hdmwy]\\)?>" "" (buffer-string)
  1258. nil nil 1)))))
  1259. ;;; Fixed-Width Areas
  1260. (ert-deftest test-org/toggle-fixed-width ()
  1261. "Test `org-toggle-fixed-width' specifications."
  1262. ;; No region: Toggle on fixed-width marker in paragraphs.
  1263. (should
  1264. (equal ": A"
  1265. (org-test-with-temp-text "A"
  1266. (org-toggle-fixed-width)
  1267. (buffer-string))))
  1268. ;; No region: Toggle off fixed-width markers in fixed-width areas.
  1269. (should
  1270. (equal "A"
  1271. (org-test-with-temp-text ": A"
  1272. (org-toggle-fixed-width)
  1273. (buffer-string))))
  1274. ;; No region: Toggle on marker in blank lines after elements or just
  1275. ;; after a headline.
  1276. (should
  1277. (equal "* H\n: "
  1278. (org-test-with-temp-text "* H\n"
  1279. (forward-line)
  1280. (org-toggle-fixed-width)
  1281. (buffer-string))))
  1282. (should
  1283. (equal "#+BEGIN_EXAMPLE\nContents\n#+END_EXAMPLE\n: "
  1284. (org-test-with-temp-text "#+BEGIN_EXAMPLE\nContents\n#+END_EXAMPLE\n"
  1285. (goto-char (point-max))
  1286. (org-toggle-fixed-width)
  1287. (buffer-string))))
  1288. ;; No region: Toggle on marker in front of one line elements (e.g.,
  1289. ;; headlines, clocks)
  1290. (should
  1291. (equal ": * Headline"
  1292. (org-test-with-temp-text "* Headline"
  1293. (org-toggle-fixed-width)
  1294. (buffer-string))))
  1295. (should
  1296. (equal ": #+KEYWORD: value"
  1297. (org-test-with-temp-text "#+KEYWORD: value"
  1298. (org-toggle-fixed-width)
  1299. (buffer-string))))
  1300. ;; No region: error in other situations.
  1301. (should-error
  1302. (org-test-with-temp-text "#+BEGIN_EXAMPLE\n: A\n#+END_EXAMPLE"
  1303. (forward-line)
  1304. (org-toggle-fixed-width)
  1305. (buffer-string)))
  1306. ;; No region: Indentation is preserved.
  1307. (should
  1308. (equal "- A\n : B"
  1309. (org-test-with-temp-text "- A\n B"
  1310. (forward-line)
  1311. (org-toggle-fixed-width)
  1312. (buffer-string))))
  1313. ;; Region: If it contains only fixed-width elements and blank lines,
  1314. ;; toggle off fixed-width markup.
  1315. (should
  1316. (equal "A\n\nB"
  1317. (org-test-with-temp-text ": A\n\n: B"
  1318. (transient-mark-mode 1)
  1319. (push-mark (point) t t)
  1320. (goto-char (point-max))
  1321. (org-toggle-fixed-width)
  1322. (buffer-string))))
  1323. ;; Region: If it contains anything else, toggle on fixed-width but
  1324. ;; not on fixed-width areas.
  1325. (should
  1326. (equal ": A\n: \n: B\n: \n: C"
  1327. (org-test-with-temp-text "A\n\n: B\n\nC"
  1328. (transient-mark-mode 1)
  1329. (push-mark (point) t t)
  1330. (goto-char (point-max))
  1331. (org-toggle-fixed-width)
  1332. (buffer-string))))
  1333. ;; Region: Ignore blank lines at its end, unless it contains only
  1334. ;; such lines.
  1335. (should
  1336. (equal ": A\n\n"
  1337. (org-test-with-temp-text "A\n\n"
  1338. (transient-mark-mode 1)
  1339. (push-mark (point) t t)
  1340. (goto-char (point-max))
  1341. (org-toggle-fixed-width)
  1342. (buffer-string))))
  1343. (should
  1344. (equal ": \n: \n"
  1345. (org-test-with-temp-text "\n\n"
  1346. (transient-mark-mode 1)
  1347. (push-mark (point) t t)
  1348. (goto-char (point-max))
  1349. (org-toggle-fixed-width)
  1350. (buffer-string)))))
  1351. ;;; Headline
  1352. (ert-deftest test-org/get-heading ()
  1353. "Test `org-get-heading' specifications."
  1354. ;; Return current heading, even if point is not on it.
  1355. (should
  1356. (equal "H"
  1357. (org-test-with-temp-text "* H"
  1358. (org-get-heading))))
  1359. (should
  1360. (equal "H"
  1361. (org-test-with-temp-text "* H\nText<point>"
  1362. (org-get-heading))))
  1363. ;; Without any optional argument, return TODO keywords and tags.
  1364. (should
  1365. (equal "TODO H"
  1366. (org-test-with-temp-text "#+TODO: TODO | DONE\n* TODO H<point>"
  1367. (org-get-heading))))
  1368. (should
  1369. (equal "H :tag:"
  1370. (org-test-with-temp-text "* H :tag:"
  1371. (org-get-heading))))
  1372. ;; With NO-TAGS argument, ignore tags.
  1373. (should
  1374. (equal "TODO H"
  1375. (org-test-with-temp-text "#+TODO: TODO | DONE\n* TODO H<point>"
  1376. (org-get-heading t))))
  1377. (should
  1378. (equal "H"
  1379. (org-test-with-temp-text "* H :tag:"
  1380. (org-get-heading t))))
  1381. ;; With NO-TODO, ignore TODO keyword.
  1382. (should
  1383. (equal "H"
  1384. (org-test-with-temp-text "#+TODO: TODO | DONE\n* TODO H<point>"
  1385. (org-get-heading nil t))))
  1386. (should
  1387. (equal "H :tag:"
  1388. (org-test-with-temp-text "* H :tag:"
  1389. (org-get-heading nil t))))
  1390. ;; TODO keywords are case-sensitive.
  1391. (should
  1392. (equal "Todo H"
  1393. (org-test-with-temp-text "#+TODO: TODO | DONE\n* Todo H<point>"
  1394. (org-get-heading nil t))))
  1395. ;; On an empty headline, return value is consistent.
  1396. (should (equal "" (org-test-with-temp-text "* " (org-get-heading))))
  1397. (should (equal "" (org-test-with-temp-text "* " (org-get-heading t))))
  1398. (should (equal "" (org-test-with-temp-text "* " (org-get-heading nil t))))
  1399. (should (equal "" (org-test-with-temp-text "* " (org-get-heading t t)))))
  1400. (ert-deftest test-org/in-commented-heading-p ()
  1401. "Test `org-in-commented-heading-p' specifications."
  1402. ;; Commented headline.
  1403. (should
  1404. (org-test-with-temp-text "* COMMENT Headline\nBody"
  1405. (goto-char (point-max))
  1406. (org-in-commented-heading-p)))
  1407. ;; Commented ancestor.
  1408. (should
  1409. (org-test-with-temp-text "* COMMENT Headline\n** Level 2\nBody"
  1410. (goto-char (point-max))
  1411. (org-in-commented-heading-p)))
  1412. ;; Comment keyword is case-sensitive.
  1413. (should-not
  1414. (org-test-with-temp-text "* Comment Headline\nBody"
  1415. (goto-char (point-max))
  1416. (org-in-commented-heading-p)))
  1417. ;; Keyword is standalone.
  1418. (should-not
  1419. (org-test-with-temp-text "* COMMENTHeadline\nBody"
  1420. (goto-char (point-max))
  1421. (org-in-commented-heading-p)))
  1422. ;; Optional argument.
  1423. (should-not
  1424. (org-test-with-temp-text "* COMMENT Headline\n** Level 2\nBody"
  1425. (goto-char (point-max))
  1426. (org-in-commented-heading-p t))))
  1427. (ert-deftest test-org/entry-blocked-p ()
  1428. ;; Check other dependencies.
  1429. (should
  1430. (org-test-with-temp-text "* TODO Blocked\n** DONE one\n** TODO two"
  1431. (let ((org-enforce-todo-dependencies t)
  1432. (org-blocker-hook
  1433. '(org-block-todo-from-children-or-siblings-or-parent)))
  1434. (org-entry-blocked-p))))
  1435. (should-not
  1436. (org-test-with-temp-text "* TODO Blocked\n** DONE one\n** DONE two"
  1437. (let ((org-enforce-todo-dependencies t)
  1438. (org-blocker-hook
  1439. '(org-block-todo-from-children-or-siblings-or-parent)))
  1440. (org-entry-blocked-p))))
  1441. ;; Entry without a TODO keyword or with a DONE keyword cannot be
  1442. ;; blocked.
  1443. (should-not
  1444. (org-test-with-temp-text "* Blocked\n** TODO one"
  1445. (let ((org-enforce-todo-dependencies t)
  1446. (org-blocker-hook
  1447. '(org-block-todo-from-children-or-siblings-or-parent)))
  1448. (org-entry-blocked-p))))
  1449. (should-not
  1450. (org-test-with-temp-text "* DONE Blocked\n** TODO one"
  1451. (let ((org-enforce-todo-dependencies t)
  1452. (org-blocker-hook
  1453. '(org-block-todo-from-children-or-siblings-or-parent)))
  1454. (org-entry-blocked-p))))
  1455. ;; Follow :ORDERED: specifications.
  1456. (should
  1457. (org-test-with-temp-text
  1458. "* H\n:PROPERTIES:\n:ORDERED: t\n:END:\n** TODO one\n** <point>TODO two"
  1459. (let ((org-enforce-todo-dependencies t)
  1460. (org-blocker-hook
  1461. '(org-block-todo-from-children-or-siblings-or-parent)))
  1462. (org-entry-blocked-p))))
  1463. (should-not
  1464. (org-test-with-temp-text
  1465. "* H\n:PROPERTIES:\n:ORDERED: t\n:END:\n** <point>TODO one\n** DONE two"
  1466. (let ((org-enforce-todo-dependencies t)
  1467. (org-blocker-hook
  1468. '(org-block-todo-from-children-or-siblings-or-parent)))
  1469. (org-entry-blocked-p)))))
  1470. (ert-deftest test-org/get-outline-path ()
  1471. "Test `org-get-outline-path' specifications."
  1472. ;; Top-level headlines have no outline path.
  1473. (should-not
  1474. (org-test-with-temp-text "* H"
  1475. (org-get-outline-path)))
  1476. ;; Otherwise, outline path is the path leading to the headline.
  1477. (should
  1478. (equal '("H")
  1479. (org-test-with-temp-text "* H\n** S<point>"
  1480. (org-get-outline-path))))
  1481. ;; Find path even when point is not on a headline.
  1482. (should
  1483. (equal '("H")
  1484. (org-test-with-temp-text "* H\n** S\nText<point>"
  1485. (org-get-outline-path))))
  1486. ;; TODO keywords, tags and statistics cookies are ignored.
  1487. (should
  1488. (equal '("H")
  1489. (org-test-with-temp-text "* TODO H [0/1] :tag:\n** S<point>"
  1490. (org-get-outline-path))))
  1491. ;; Links are replaced with their description or their path.
  1492. (should
  1493. (equal '("Org")
  1494. (org-test-with-temp-text
  1495. "* [[http://orgmode.org][Org]]\n** S<point>"
  1496. (org-get-outline-path))))
  1497. (should
  1498. (equal '("http://orgmode.org")
  1499. (org-test-with-temp-text
  1500. "* [[http://orgmode.org]]\n** S<point>"
  1501. (org-get-outline-path))))
  1502. ;; When WITH-SELF is non-nil, include current heading.
  1503. (should
  1504. (equal '("H")
  1505. (org-test-with-temp-text "* H"
  1506. (org-get-outline-path t))))
  1507. (should
  1508. (equal '("H" "S")
  1509. (org-test-with-temp-text "* H\n** S\nText<point>"
  1510. (org-get-outline-path t))))
  1511. ;; Using cache is transparent to the user.
  1512. (should
  1513. (equal '("H")
  1514. (org-test-with-temp-text "* H\n** S<point>"
  1515. (setq org-outline-path-cache nil)
  1516. (org-get-outline-path nil t))))
  1517. ;; Do not corrupt cache when finding outline path in distant part of
  1518. ;; the buffer.
  1519. (should
  1520. (equal '("H2")
  1521. (org-test-with-temp-text "* H\n** S<point>\n* H2\n** S2"
  1522. (setq org-outline-path-cache nil)
  1523. (org-get-outline-path nil t)
  1524. (search-forward "S2")
  1525. (org-get-outline-path nil t))))
  1526. ;; Do not choke on empty headlines.
  1527. (should
  1528. (org-test-with-temp-text "* H\n** <point>"
  1529. (org-get-outline-path)))
  1530. (should
  1531. (org-test-with-temp-text "* \n** H<point>"
  1532. (org-get-outline-path))))
  1533. (ert-deftest test-org/format-outline-path ()
  1534. "Test `org-format-outline-path' specifications."
  1535. (should
  1536. (string= (org-format-outline-path (list "one" "two" "three"))
  1537. "one/two/three"))
  1538. ;; Empty path.
  1539. (should
  1540. (string= (org-format-outline-path '())
  1541. ""))
  1542. (should
  1543. (string= (org-format-outline-path '(nil))
  1544. ""))
  1545. ;; Empty path and prefix.
  1546. (should
  1547. (string= (org-format-outline-path '() nil ">>")
  1548. ">>"))
  1549. ;; Trailing whitespace in headings.
  1550. (should
  1551. (string= (org-format-outline-path (list "one\t" "tw o " "three "))
  1552. "one/tw o/three"))
  1553. ;; Non-default prefix and separators.
  1554. (should
  1555. (string= (org-format-outline-path (list "one" "two" "three") nil ">>" "|")
  1556. ">>|one|two|three"))
  1557. ;; Truncate.
  1558. (should
  1559. (string= (org-format-outline-path (list "one" "two" "three" "four") 10)
  1560. "one/two/.."))
  1561. ;; Give a very narrow width.
  1562. (should
  1563. (string= (org-format-outline-path (list "one" "two" "three" "four") 2)
  1564. "on"))
  1565. ;; Give a prefix that extends beyond the width.
  1566. (should
  1567. (string= (org-format-outline-path (list "one" "two" "three" "four") 10
  1568. ">>>>>>>>>>")
  1569. ">>>>>>>>..")))
  1570. (ert-deftest test-org/map-entries ()
  1571. "Test `org-map-entries' specifications."
  1572. ;; Full match.
  1573. (should
  1574. (equal '(1 11)
  1575. (org-test-with-temp-text "* Level 1\n** Level 2"
  1576. (org-map-entries #'point))))
  1577. ;; Level match.
  1578. (should
  1579. (equal '(1)
  1580. (org-test-with-temp-text "* Level 1\n** Level 2"
  1581. (let (org-odd-levels-only) (org-map-entries #'point "LEVEL=1")))))
  1582. (should
  1583. (equal '(11)
  1584. (org-test-with-temp-text "* Level 1\n** Level 2"
  1585. (let (org-odd-levels-only) (org-map-entries #'point "LEVEL>1")))))
  1586. ;; Tag match.
  1587. (should
  1588. (equal '(11)
  1589. (org-test-with-temp-text "* H1 :no:\n* H2 :yes:"
  1590. (org-map-entries #'point "yes"))))
  1591. (should
  1592. (equal '(14)
  1593. (org-test-with-temp-text "* H1 :yes:a:\n* H2 :yes:b:"
  1594. (org-map-entries #'point "+yes-a"))))
  1595. (should
  1596. (equal '(11 23)
  1597. (org-test-with-temp-text "* H1 :no:\n* H2 :yes1:\n* H3 :yes2:"
  1598. (org-map-entries #'point "{yes?}"))))
  1599. ;; Priority match.
  1600. (should
  1601. (equal '(1)
  1602. (org-test-with-temp-text "* [#A] H1\n* [#B] H2"
  1603. (org-map-entries #'point "PRIORITY=\"A\""))))
  1604. ;; Date match.
  1605. (should
  1606. (equal '(36)
  1607. (org-test-with-temp-text "
  1608. * H1
  1609. SCHEDULED: <2012-03-29 thu.>
  1610. * H2
  1611. SCHEDULED: <2014-03-04 tue.>"
  1612. (org-map-entries #'point "SCHEDULED=\"<2014-03-04 tue.>\""))))
  1613. (should
  1614. (equal '(2)
  1615. (org-test-with-temp-text "
  1616. * H1
  1617. SCHEDULED: <2012-03-29 thu.>
  1618. * H2
  1619. SCHEDULED: <2014-03-04 tue.>"
  1620. (org-map-entries #'point "SCHEDULED<\"<2013-01-01>\""))))
  1621. ;; Regular property match.
  1622. (should
  1623. (equal '(2)
  1624. (org-test-with-temp-text "
  1625. * H1
  1626. :PROPERTIES:
  1627. :TEST: 1
  1628. :END:
  1629. * H2
  1630. :PROPERTIES:
  1631. :TEST: 2
  1632. :END:"
  1633. (org-map-entries #'point "TEST=1"))))
  1634. ;; Multiple criteria.
  1635. (should
  1636. (equal '(23)
  1637. (org-test-with-temp-text "* H1 :no:\n** H2 :yes:\n* H3 :yes:"
  1638. (let (org-odd-levels-only
  1639. (org-use-tag-inheritance nil))
  1640. (org-map-entries #'point "yes+LEVEL=1")))))
  1641. ;; "or" criteria.
  1642. (should
  1643. (equal '(12 24)
  1644. (org-test-with-temp-text "* H1 :yes:\n** H2 :yes:\n** H3 :no:"
  1645. (let (org-odd-levels-only)
  1646. (org-map-entries #'point "LEVEL=2|no")))))
  1647. (should
  1648. (equal '(1 12)
  1649. (org-test-with-temp-text "* H1 :yes:\n* H2 :no:\n* H3 :maybe:"
  1650. (let (org-odd-levels-only)
  1651. (org-map-entries #'point "yes|no")))))
  1652. ;; "and" criteria.
  1653. (should
  1654. (equal '(22)
  1655. (org-test-with-temp-text "* H1 :yes:\n* H2 :no:\n* H3 :yes:no:"
  1656. (let (org-odd-levels-only)
  1657. (org-map-entries #'point "yes&no"))))))
  1658. (ert-deftest test-org/edit-headline ()
  1659. "Test `org-edit-headline' specifications."
  1660. (should
  1661. (equal "* B"
  1662. (org-test-with-temp-text "* A"
  1663. (org-edit-headline "B")
  1664. (buffer-string))))
  1665. ;; Handle empty headings.
  1666. (should
  1667. (equal "* "
  1668. (org-test-with-temp-text "* A"
  1669. (org-edit-headline "")
  1670. (buffer-string))))
  1671. (should
  1672. (equal "* A"
  1673. (org-test-with-temp-text "* "
  1674. (org-edit-headline "A")
  1675. (buffer-string))))
  1676. ;; Handle TODO keywords and priority cookies.
  1677. (should
  1678. (equal "* TODO B"
  1679. (org-test-with-temp-text "* TODO A"
  1680. (org-edit-headline "B")
  1681. (buffer-string))))
  1682. (should
  1683. (equal "* [#A] B"
  1684. (org-test-with-temp-text "* [#A] A"
  1685. (org-edit-headline "B")
  1686. (buffer-string))))
  1687. (should
  1688. (equal "* TODO [#A] B"
  1689. (org-test-with-temp-text "* TODO [#A] A"
  1690. (org-edit-headline "B")
  1691. (buffer-string))))
  1692. ;; Handle tags.
  1693. (equal "* B :tag:"
  1694. (org-test-with-temp-text "* A :tag:"
  1695. (let ((org-tags-column 4)) (org-edit-headline "B"))
  1696. (buffer-string))))
  1697. ;;; Keywords
  1698. (ert-deftest test-org/set-regexps-and-options ()
  1699. "Test `org-set-regexps-and-options' specifications."
  1700. ;; TAGS keyword.
  1701. (should
  1702. (equal '(("A"))
  1703. (let ((org-tag-alist '(("A")))
  1704. (org-tag-persistent-alist nil))
  1705. (org-test-with-temp-text ""
  1706. (org-mode-restart)
  1707. org-current-tag-alist))))
  1708. (should
  1709. (equal '(("B"))
  1710. (let ((org-tag-alist '(("A")))
  1711. (org-tag-persistent-alist nil))
  1712. (org-test-with-temp-text "#+TAGS: B"
  1713. (org-mode-restart)
  1714. org-current-tag-alist))))
  1715. (should
  1716. (equal '(("C") ("B"))
  1717. (let ((org-tag-alist '(("A")))
  1718. (org-tag-persistent-alist '(("C"))))
  1719. (org-test-with-temp-text "#+TAGS: B"
  1720. (org-mode-restart)
  1721. org-current-tag-alist))))
  1722. (should
  1723. (equal '(("B"))
  1724. (let ((org-tag-alist '(("A")))
  1725. (org-tag-persistent-alist '(("C"))))
  1726. (org-test-with-temp-text "#+STARTUP: noptag\n#+TAGS: B"
  1727. (org-mode-restart)
  1728. org-current-tag-alist))))
  1729. (should
  1730. (equal '(("A" . ?a) ("B") ("C"))
  1731. (let ((org-tag-persistant-alist nil))
  1732. (org-test-with-temp-text "#+TAGS: A(a) B C"
  1733. (org-mode-restart)
  1734. org-current-tag-alist))))
  1735. (should
  1736. (equal '(("A") (:newline) ("B"))
  1737. (let ((org-tag-persistent-alist nil))
  1738. (org-test-with-temp-text "#+TAGS: A\n#+TAGS: B"
  1739. (org-mode-restart)
  1740. org-current-tag-alist))))
  1741. (should
  1742. (equal '((:startgroup) ("A") ("B") (:endgroup) ("C"))
  1743. (let ((org-tag-persistent-alist nil))
  1744. (org-test-with-temp-text "#+TAGS: { A B } C"
  1745. (org-mode-restart)
  1746. org-current-tag-alist))))
  1747. (should
  1748. (equal '((:startgroup) ("A") (:grouptags) ("B") ("C") (:endgroup))
  1749. (let ((org-tag-persistent-alist nil))
  1750. (org-test-with-temp-text "#+TAGS: { A : B C }"
  1751. (org-mode-restart)
  1752. org-current-tag-alist))))
  1753. (should
  1754. (equal '(("A" "B" "C"))
  1755. (let ((org-tag-persistent-alist nil))
  1756. (org-test-with-temp-text "#+TAGS: { A : B C }"
  1757. (org-mode-restart)
  1758. org-tag-groups-alist))))
  1759. (should
  1760. (equal '((:startgrouptag) ("A") (:grouptags) ("B") ("C") (:endgrouptag))
  1761. (let ((org-tag-persistent-alist nil))
  1762. (org-test-with-temp-text "#+TAGS: [ A : B C ]"
  1763. (org-mode-restart)
  1764. org-current-tag-alist))))
  1765. (should
  1766. (equal '(("A" "B" "C"))
  1767. (let ((org-tag-persistent-alist nil))
  1768. (org-test-with-temp-text "#+TAGS: [ A : B C ]"
  1769. (org-mode-restart)
  1770. org-tag-groups-alist))))
  1771. ;; FILETAGS keyword.
  1772. (should
  1773. (equal '("A" "B" "C")
  1774. (org-test-with-temp-text "#+FILETAGS: :A:B:C:"
  1775. (org-mode-restart)
  1776. org-file-tags)))
  1777. ;; PROPERTY keyword. Property names are case-insensitive.
  1778. (should
  1779. (equal "foo=1"
  1780. (org-test-with-temp-text "#+PROPERTY: var foo=1"
  1781. (org-mode-restart)
  1782. (cdr (assoc "var" org-file-properties)))))
  1783. (should
  1784. (equal
  1785. "foo=1 bar=2"
  1786. (org-test-with-temp-text "#+PROPERTY: var foo=1\n#+PROPERTY: var+ bar=2"
  1787. (org-mode-restart)
  1788. (cdr (assoc "var" org-file-properties)))))
  1789. (should
  1790. (equal
  1791. "foo=1 bar=2"
  1792. (org-test-with-temp-text "#+PROPERTY: var foo=1\n#+PROPERTY: VAR+ bar=2"
  1793. (org-mode-restart)
  1794. (cdr (assoc "var" org-file-properties)))))
  1795. ;; ARCHIVE keyword.
  1796. (should
  1797. (equal "%s_done::"
  1798. (org-test-with-temp-text "#+ARCHIVE: %s_done::"
  1799. (org-mode-restart)
  1800. org-archive-location)))
  1801. ;; CATEGORY keyword.
  1802. (should
  1803. (eq 'test
  1804. (org-test-with-temp-text "#+CATEGORY: test"
  1805. (org-mode-restart)
  1806. org-category)))
  1807. (should
  1808. (equal "test"
  1809. (org-test-with-temp-text "#+CATEGORY: test"
  1810. (org-mode-restart)
  1811. (cdr (assoc "CATEGORY" org-file-properties)))))
  1812. ;; COLUMNS keyword.
  1813. (should
  1814. (equal "%25ITEM %TAGS %PRIORITY %TODO"
  1815. (org-test-with-temp-text "#+COLUMNS: %25ITEM %TAGS %PRIORITY %TODO"
  1816. (org-mode-restart)
  1817. org-columns-default-format)))
  1818. ;; CONSTANTS keyword. Constants names are case sensitive.
  1819. (should
  1820. (equal '("299792458." "3.14")
  1821. (org-test-with-temp-text "#+CONSTANTS: c=299792458. pi=3.14"
  1822. (org-mode-restart)
  1823. (mapcar
  1824. (lambda (n) (cdr (assoc n org-table-formula-constants-local)))
  1825. '("c" "pi")))))
  1826. (should
  1827. (equal "3.14"
  1828. (org-test-with-temp-text "#+CONSTANTS: pi=22/7 pi=3.14"
  1829. (org-mode-restart)
  1830. (cdr (assoc "pi" org-table-formula-constants-local)))))
  1831. (should
  1832. (equal "22/7"
  1833. (org-test-with-temp-text "#+CONSTANTS: PI=22/7 pi=3.14"
  1834. (org-mode-restart)
  1835. (cdr (assoc "PI" org-table-formula-constants-local)))))
  1836. ;; LINK keyword.
  1837. (should
  1838. (equal
  1839. '("url1" "url2")
  1840. (org-test-with-temp-text "#+LINK: a url1\n#+LINK: b url2"
  1841. (org-mode-restart)
  1842. (mapcar (lambda (abbrev) (cdr (assoc abbrev org-link-abbrev-alist-local)))
  1843. '("a" "b")))))
  1844. ;; PRIORITIES keyword. Incomplete priorities sets are ignored.
  1845. (should
  1846. (equal
  1847. '(?X ?Z ?Y)
  1848. (org-test-with-temp-text "#+PRIORITIES: X Z Y"
  1849. (org-mode-restart)
  1850. (list org-highest-priority org-lowest-priority org-default-priority))))
  1851. (should
  1852. (equal
  1853. '(?A ?C ?B)
  1854. (org-test-with-temp-text "#+PRIORITIES: X Z"
  1855. (org-mode-restart)
  1856. (list org-highest-priority org-lowest-priority org-default-priority))))
  1857. ;; STARTUP keyword.
  1858. (should
  1859. (equal '(t t)
  1860. (org-test-with-temp-text "#+STARTUP: fold odd"
  1861. (org-mode-restart)
  1862. (list org-startup-folded org-odd-levels-only))))
  1863. ;; TODO keywords.
  1864. (should
  1865. (equal '(("A" "B") ("C"))
  1866. (org-test-with-temp-text "#+TODO: A B | C"
  1867. (org-mode-restart)
  1868. (list org-not-done-keywords org-done-keywords))))
  1869. (should
  1870. (equal '(("A" "C") ("B" "D"))
  1871. (org-test-with-temp-text "#+TODO: A | B\n#+TODO: C | D"
  1872. (org-mode-restart)
  1873. (list org-not-done-keywords org-done-keywords))))
  1874. (should
  1875. (equal '(("A" "B") ("C"))
  1876. (org-test-with-temp-text "#+TYP_TODO: A B | C"
  1877. (org-mode-restart)
  1878. (list org-not-done-keywords org-done-keywords))))
  1879. (should
  1880. (equal '((:startgroup) ("A" . ?a) (:endgroup))
  1881. (org-test-with-temp-text "#+TODO: A(a)"
  1882. (org-mode-restart)
  1883. org-todo-key-alist)))
  1884. (should
  1885. (equal '(("D" note nil) ("C" time nil) ("B" note time))
  1886. (org-test-with-temp-text "#+TODO: A(a) B(b@/!) | C(c!) D(d@)"
  1887. (org-mode-restart)
  1888. org-todo-log-states)))
  1889. ;; Enter SETUPFILE keyword.
  1890. (should
  1891. (equal "1"
  1892. (org-test-with-temp-text
  1893. (format "#+SETUPFILE: \"%s/examples/setupfile.org\"" org-test-dir)
  1894. (org-mode-restart)
  1895. (cdr (assoc "a" org-file-properties))))))
  1896. ;;; Links
  1897. ;;;; Coderefs
  1898. (ert-deftest test-org/coderef ()
  1899. "Test coderef links specifications."
  1900. (should
  1901. (org-test-with-temp-text "
  1902. #+BEGIN_SRC emacs-lisp
  1903. \(+ 1 1) (ref:sc)
  1904. #+END_SRC
  1905. \[[(sc)<point>]]"
  1906. (org-open-at-point)
  1907. (looking-at "(ref:sc)")))
  1908. ;; Find coderef even with alternate label format.
  1909. (should
  1910. (org-test-with-temp-text "
  1911. #+BEGIN_SRC emacs-lisp -l \"{ref:%s}\"
  1912. \(+ 1 1) {ref:sc}
  1913. #+END_SRC
  1914. \[[(sc)<point>]]"
  1915. (org-open-at-point)
  1916. (looking-at "{ref:sc}"))))
  1917. ;;;; Custom ID
  1918. (ert-deftest test-org/custom-id ()
  1919. "Test custom ID links specifications."
  1920. (should
  1921. (org-test-with-temp-text
  1922. "* H1\n:PROPERTIES:\n:CUSTOM_ID: custom\n:END:\n* H2\n[[#custom<point>]]"
  1923. (org-open-at-point)
  1924. (looking-at-p "\\* H1")))
  1925. ;; Throw an error on false positives.
  1926. (should-error
  1927. (org-test-with-temp-text
  1928. "* H1\n:DRAWER:\n:CUSTOM_ID: custom\n:END:\n* H2\n[[#custom<point>]]"
  1929. (org-open-at-point)
  1930. (looking-at-p "\\* H1"))))
  1931. ;;;; Fuzzy Links
  1932. ;; Fuzzy links [[text]] encompass links to a target (<<text>>), to
  1933. ;; a named element (#+name: text) and to headlines (* Text).
  1934. (ert-deftest test-org/fuzzy-links ()
  1935. "Test fuzzy links specifications."
  1936. ;; Fuzzy link goes in priority to a matching target.
  1937. (should
  1938. (org-test-with-temp-text
  1939. "#+NAME: Test\n|a|b|\n<<Test>>\n* Test\n<point>[[Test]]"
  1940. (let ((org-link-search-must-match-exact-headline nil)) (org-open-at-point))
  1941. (looking-at "<<Test>>")))
  1942. ;; Then fuzzy link points to an element with a given name.
  1943. (should
  1944. (org-test-with-temp-text "Test\n#+NAME: Test\n|a|b|\n* Test\n<point>[[Test]]"
  1945. (let ((org-link-search-must-match-exact-headline nil)) (org-open-at-point))
  1946. (looking-at "#\\+NAME: Test")))
  1947. ;; A target still lead to a matching headline otherwise.
  1948. (should
  1949. (org-test-with-temp-text "* Head1\n* Head2\n*Head3\n<point>[[Head2]]"
  1950. (let ((org-link-search-must-match-exact-headline nil)) (org-open-at-point))
  1951. (looking-at "\\* Head2")))
  1952. ;; With a leading star in link, enforce heading match.
  1953. (should
  1954. (org-test-with-temp-text "* Test\n<<Test>>\n<point>[[*Test]]"
  1955. (let ((org-link-search-must-match-exact-headline nil)) (org-open-at-point))
  1956. (looking-at "\\* Test")))
  1957. ;; With a leading star in link, enforce exact heading match, even
  1958. ;; with `org-link-search-must-match-exact-headline' set to nil.
  1959. (should-error
  1960. (org-test-with-temp-text "* Test 1\nFoo Bar\n<point>[[*Test]]"
  1961. (let ((org-link-search-must-match-exact-headline nil))
  1962. (org-open-at-point))))
  1963. ;; Handle non-nil `org-link-search-must-match-exact-headline'.
  1964. (should
  1965. (org-test-with-temp-text "* Test\nFoo Bar\n<point>[[Test]]"
  1966. (let ((org-link-search-must-match-exact-headline t)) (org-open-at-point))
  1967. (looking-at "\\* Test")))
  1968. (should
  1969. (org-test-with-temp-text "* Test\nFoo Bar\n<point>[[*Test]]"
  1970. (let ((org-link-search-must-match-exact-headline t)) (org-open-at-point))
  1971. (looking-at "\\* Test")))
  1972. ;; Heading match should not care about spaces, cookies, TODO
  1973. ;; keywords, priorities, and tags.
  1974. (should
  1975. (let ((first-line
  1976. "** TODO [#A] [/] Test [1/2] [33%] 1 \t 2 [%] :work:urgent: "))
  1977. (org-test-with-temp-text
  1978. (concat first-line "\nFoo Bar\n<point>[[*Test 1 2]]")
  1979. (let ((org-link-search-must-match-exact-headline nil)
  1980. (org-todo-regexp "TODO"))
  1981. (org-open-at-point))
  1982. (looking-at (regexp-quote first-line)))))
  1983. ;; Heading match should still be exact.
  1984. (should-error
  1985. (let ((first-line
  1986. "** TODO [#A] [/] Test [1/2] [33%] 1 \t 2 [%] :work:urgent: "))
  1987. (org-test-with-temp-text
  1988. (concat first-line "\nFoo Bar\n<point>[[*Test 1]]")
  1989. (let ((org-link-search-must-match-exact-headline nil)
  1990. (org-todo-regexp "TODO"))
  1991. (org-open-at-point)))))
  1992. ;; Heading match ignores COMMENT keyword.
  1993. (should
  1994. (org-test-with-temp-text "[[*Test]]\n* COMMENT Test"
  1995. (org-open-at-point)
  1996. (looking-at "\\* COMMENT Test")))
  1997. ;; Correctly un-hexify fuzzy links.
  1998. (should
  1999. (org-test-with-temp-text "* With space\n[[*With%20space][With space<point>]]"
  2000. (org-open-at-point)
  2001. (bobp))))
  2002. ;;;; Link Escaping
  2003. (ert-deftest test-org/org-link-escape-ascii-character ()
  2004. "Escape an ascii character."
  2005. (should
  2006. (string=
  2007. "%5B"
  2008. (org-link-escape "["))))
  2009. (ert-deftest test-org/org-link-escape-ascii-ctrl-character ()
  2010. "Escape an ascii control character."
  2011. (should
  2012. (string=
  2013. "%09"
  2014. (org-link-escape "\t"))))
  2015. (ert-deftest test-org/org-link-escape-multibyte-character ()
  2016. "Escape an unicode multibyte character."
  2017. (should
  2018. (string=
  2019. "%E2%82%AC"
  2020. (org-link-escape "€"))))
  2021. (ert-deftest test-org/org-link-escape-custom-table ()
  2022. "Escape string with custom character table."
  2023. (should
  2024. (string=
  2025. "Foo%3A%42ar%0A"
  2026. (org-link-escape "Foo:Bar\n" '(?\: ?\B)))))
  2027. (ert-deftest test-org/org-link-escape-custom-table-merge ()
  2028. "Escape string with custom table merged with default table."
  2029. (should
  2030. (string=
  2031. "%5BF%6F%6F%3A%42ar%0A%5D"
  2032. (org-link-escape "[Foo:Bar\n]" '(?\: ?\B ?\o) t))))
  2033. (ert-deftest test-org/org-link-unescape-ascii-character ()
  2034. "Unescape an ascii character."
  2035. (should
  2036. (string=
  2037. "["
  2038. (org-link-unescape "%5B"))))
  2039. (ert-deftest test-org/org-link-unescape-ascii-ctrl-character ()
  2040. "Unescpae an ascii control character."
  2041. (should
  2042. (string=
  2043. "\n"
  2044. (org-link-unescape "%0A"))))
  2045. (ert-deftest test-org/org-link-unescape-multibyte-character ()
  2046. "Unescape unicode multibyte character."
  2047. (should
  2048. (string=
  2049. "€"
  2050. (org-link-unescape "%E2%82%AC"))))
  2051. (ert-deftest test-org/org-link-unescape-ascii-extended-char ()
  2052. "Unescape old style percent escaped character."
  2053. (should
  2054. (string=
  2055. "àâçèéêîôùû"
  2056. (decode-coding-string
  2057. (org-link-unescape "%E0%E2%E7%E8%E9%EA%EE%F4%F9%FB") 'latin-1))))
  2058. (ert-deftest test-org/org-link-escape-url-with-escaped-char ()
  2059. "Escape and unescape a URL that includes an escaped char.
  2060. http://article.gmane.org/gmane.emacs.orgmode/21459/"
  2061. (should
  2062. (string=
  2063. "http://some.host.com/form?&id=blah%2Bblah25"
  2064. (org-link-unescape
  2065. (org-link-escape "http://some.host.com/form?&id=blah%2Bblah25")))))
  2066. ;;;; Open at point
  2067. (ert-deftest test-org/open-at-point-in-keyword ()
  2068. "Does `org-open-at-point' open link in a keyword line?"
  2069. (should
  2070. (org-test-with-temp-text
  2071. "<<top>>\n#+KEYWORD: <point>[[top]]"
  2072. (org-open-at-point) t)))
  2073. (ert-deftest test-org/open-at-point-in-property ()
  2074. "Does `org-open-at-point' open link in property drawer?"
  2075. (should
  2076. (org-test-with-temp-text
  2077. "* Headline
  2078. :PROPERTIES:
  2079. :URL: <point>[[*Headline]]
  2080. :END:"
  2081. (org-open-at-point) t)))
  2082. (ert-deftest test-org/open-at-point-in-comment ()
  2083. "Does `org-open-at-point' open link in a commented line?"
  2084. (should
  2085. (org-test-with-temp-text
  2086. "<<top>>\n# <point>[[top]]"
  2087. (org-open-at-point) t)))
  2088. (ert-deftest test-org/open-at-point/inline-image ()
  2089. "Test `org-open-at-point' on nested links."
  2090. (should
  2091. (org-test-with-temp-text "<<top>>\n[[top][file:<point>unicorn.jpg]]"
  2092. (org-open-at-point)
  2093. (bobp))))
  2094. (ert-deftest test-org/open-at-point/radio-target ()
  2095. "Test `org-open-at-point' on radio targets."
  2096. (should
  2097. (org-test-with-temp-text "<<<target>>> <point>target"
  2098. (org-update-radio-target-regexp)
  2099. (org-open-at-point)
  2100. (eq (org-element-type (org-element-context)) 'radio-target))))
  2101. ;;;; Stored links
  2102. (ert-deftest test-org/store-link ()
  2103. "Test `org-store-link' specifications."
  2104. ;; On a headline, link to that headline. Use heading as the
  2105. ;; description of the link.
  2106. (should
  2107. (let (org-store-link-props org-stored-links)
  2108. (org-test-with-temp-text-in-file "* H1"
  2109. (let ((file (buffer-file-name)))
  2110. (equal (format "[[file:%s::*H1][H1]]" file)
  2111. (org-store-link nil))))))
  2112. ;; On a headline, remove any link from description.
  2113. (should
  2114. (let (org-store-link-props org-stored-links)
  2115. (org-test-with-temp-text-in-file "* [[#l][d]]"
  2116. (let ((file (buffer-file-name)))
  2117. (equal (format "[[file:%s::*%s][d]]"
  2118. file
  2119. (org-link-escape "[[#l][d]]"))
  2120. (org-store-link nil))))))
  2121. (should
  2122. (let (org-store-link-props org-stored-links)
  2123. (org-test-with-temp-text-in-file "* [[l]]"
  2124. (let ((file (buffer-file-name)))
  2125. (equal (format "[[file:%s::*%s][l]]" file (org-link-escape "[[l]]"))
  2126. (org-store-link nil))))))
  2127. (should
  2128. (let (org-store-link-props org-stored-links)
  2129. (org-test-with-temp-text-in-file "* [[l1][d1]] [[l2][d2]]"
  2130. (let ((file (buffer-file-name)))
  2131. (equal (format "[[file:%s::*%s][d1 d2]]"
  2132. file
  2133. (org-link-escape "[[l1][d1]] [[l2][d2]]"))
  2134. (org-store-link nil))))))
  2135. ;; On a named element, link to that element.
  2136. (should
  2137. (let (org-store-link-props org-stored-links)
  2138. (org-test-with-temp-text-in-file "#+NAME: foo\nParagraph"
  2139. (let ((file (buffer-file-name)))
  2140. (equal (format "[[file:%s::foo][foo]]" file)
  2141. (org-store-link nil)))))))
  2142. ;;; Node Properties
  2143. (ert-deftest test-org/accumulated-properties-in-drawers ()
  2144. "Ensure properties accumulate in subtree drawers."
  2145. (org-test-at-id "75282ba2-f77a-4309-a970-e87c149fe125"
  2146. (org-babel-next-src-block)
  2147. (should (equal '(2 1) (org-babel-execute-src-block)))))
  2148. (ert-deftest test-org/custom-properties ()
  2149. "Test custom properties specifications."
  2150. ;; Standard test.
  2151. (should
  2152. (let ((org-custom-properties '("FOO")))
  2153. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:FOO: val\n:END:\n"
  2154. (org-toggle-custom-properties-visibility)
  2155. (org-invisible-p2))))
  2156. ;; Properties are case-insensitive.
  2157. (should
  2158. (let ((org-custom-properties '("FOO")))
  2159. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:foo: val\n:END:\n"
  2160. (org-toggle-custom-properties-visibility)
  2161. (org-invisible-p2))))
  2162. (should
  2163. (let ((org-custom-properties '("foo")))
  2164. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:FOO: val\n:END:\n"
  2165. (org-toggle-custom-properties-visibility)
  2166. (org-invisible-p2))))
  2167. ;; Multiple custom properties in the same drawer.
  2168. (should
  2169. (let ((org-custom-properties '("FOO" "BAR")))
  2170. (org-test-with-temp-text
  2171. "* H\n:PROPERTIES:\n<point>:FOO: val\n:P: 1\n:BAR: baz\n:END:\n"
  2172. (org-toggle-custom-properties-visibility)
  2173. (and (org-invisible-p2)
  2174. (not (progn (forward-line) (org-invisible-p2)))
  2175. (progn (forward-line) (org-invisible-p2))))))
  2176. ;; Hide custom properties with an empty value.
  2177. (should
  2178. (let ((org-custom-properties '("FOO")))
  2179. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:FOO:\n:END:\n"
  2180. (org-toggle-custom-properties-visibility)
  2181. (org-invisible-p2))))
  2182. ;; Do not hide fake properties.
  2183. (should-not
  2184. (let ((org-custom-properties '("FOO")))
  2185. (org-test-with-temp-text ":FOO: val\n"
  2186. (org-toggle-custom-properties-visibility)
  2187. (org-invisible-p2))))
  2188. (should-not
  2189. (let ((org-custom-properties '("A")))
  2190. (org-test-with-temp-text
  2191. "* H\n:PROPERTIES:\n:A: 1\n:END:\n\n:PROPERTIES:\n<point>:A: 2\n:END:"
  2192. (org-toggle-custom-properties-visibility)
  2193. (org-invisible-p2)))))
  2194. ;;; Mark Region
  2195. (ert-deftest test-org/mark-subtree ()
  2196. "Test `org-mark-subtree' specifications."
  2197. ;; Error when point is before first headline.
  2198. (should-error
  2199. (org-test-with-temp-text "Paragraph\n* Headline\nBody"
  2200. (progn (transient-mark-mode 1)
  2201. (org-mark-subtree))))
  2202. ;; Without argument, mark current subtree.
  2203. (should
  2204. (equal
  2205. '(12 32)
  2206. (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
  2207. (progn (transient-mark-mode 1)
  2208. (forward-line 2)
  2209. (org-mark-subtree)
  2210. (list (region-beginning) (region-end))))))
  2211. ;; With an argument, move ARG up.
  2212. (should
  2213. (equal
  2214. '(1 32)
  2215. (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
  2216. (progn (transient-mark-mode 1)
  2217. (forward-line 2)
  2218. (org-mark-subtree 1)
  2219. (list (region-beginning) (region-end))))))
  2220. ;; Do not get fooled by inlinetasks.
  2221. (when (featurep 'org-inlinetask)
  2222. (should
  2223. (= 1
  2224. (org-test-with-temp-text "* Headline\n*************** Task\nContents"
  2225. (progn (transient-mark-mode 1)
  2226. (forward-line 1)
  2227. (let ((org-inlinetask-min-level 15)) (org-mark-subtree))
  2228. (region-beginning)))))))
  2229. ;;; Miscellaneous
  2230. (ert-deftest test-org/in-regexp ()
  2231. "Test `org-in-regexp' specifications."
  2232. ;; Standard tests.
  2233. (should
  2234. (org-test-with-temp-text "xx ab<point>c xx"
  2235. (org-in-regexp "abc")))
  2236. (should-not
  2237. (org-test-with-temp-text "xx abc <point>xx"
  2238. (org-in-regexp "abc")))
  2239. ;; Return non-nil even with multiple matching regexps in the same
  2240. ;; line.
  2241. (should
  2242. (org-test-with-temp-text "abc xx ab<point>c xx"
  2243. (org-in-regexp "abc")))
  2244. ;; With optional argument NLINES, check extra lines around point.
  2245. (should-not
  2246. (org-test-with-temp-text "A\nB<point>\nC"
  2247. (org-in-regexp "A\nB\nC")))
  2248. (should
  2249. (org-test-with-temp-text "A\nB<point>\nC"
  2250. (org-in-regexp "A\nB\nC" 1)))
  2251. (should-not
  2252. (org-test-with-temp-text "A\nB\nC<point>"
  2253. (org-in-regexp "A\nB\nC" 1)))
  2254. ;; When optional argument VISUALLY is non-nil, return nil if at
  2255. ;; regexp boundaries.
  2256. (should
  2257. (org-test-with-temp-text "xx abc<point> xx"
  2258. (org-in-regexp "abc")))
  2259. (should-not
  2260. (org-test-with-temp-text "xx abc<point> xx"
  2261. (org-in-regexp "abc" nil t))))
  2262. ;;; Navigation
  2263. (ert-deftest test-org/end-of-meta-data ()
  2264. "Test `org-end-of-meta-data' specifications."
  2265. ;; Skip planning line.
  2266. (should
  2267. (org-test-with-temp-text "* Headline\nSCHEDULED: <2014-03-04 tue.>"
  2268. (org-end-of-meta-data)
  2269. (eobp)))
  2270. ;; Skip properties drawer.
  2271. (should
  2272. (org-test-with-temp-text
  2273. "* Headline\nSCHEDULED: <2014-03-04 tue.>\n:PROPERTIES:\n:A: 1\n:END:"
  2274. (org-end-of-meta-data)
  2275. (eobp)))
  2276. ;; Skip both.
  2277. (should
  2278. (org-test-with-temp-text "* Headline\n:PROPERTIES:\n:A: 1\n:END:"
  2279. (org-end-of-meta-data)
  2280. (eobp)))
  2281. ;; Nothing to skip, go to first line.
  2282. (should
  2283. (org-test-with-temp-text "* Headline\nContents"
  2284. (org-end-of-meta-data)
  2285. (looking-at "Contents")))
  2286. ;; With option argument, skip empty lines, regular drawers and
  2287. ;; clocking lines.
  2288. (should
  2289. (org-test-with-temp-text "* Headline\n\nContents"
  2290. (org-end-of-meta-data t)
  2291. (looking-at "Contents")))
  2292. (should
  2293. (org-test-with-temp-text "* Headline\nCLOCK:\nContents"
  2294. (org-end-of-meta-data t)
  2295. (looking-at "Contents")))
  2296. (should
  2297. (org-test-with-temp-text "* Headline\n:LOGBOOK:\nlogging\n:END:\nContents"
  2298. (org-end-of-meta-data t)
  2299. (looking-at "Contents")))
  2300. ;; Special case: do not skip incomplete drawers.
  2301. (should
  2302. (org-test-with-temp-text "* Headline\n:LOGBOOK:\nlogging\nContents"
  2303. (org-end-of-meta-data t)
  2304. (looking-at ":LOGBOOK:")))
  2305. ;; Special case: Be careful about consecutive headlines.
  2306. (should-not
  2307. (org-test-with-temp-text "* H1\n*H2\nContents"
  2308. (org-end-of-meta-data t)
  2309. (looking-at "Contents"))))
  2310. (ert-deftest test-org/beginning-of-line ()
  2311. "Test `org-beginning-of-line' specifications."
  2312. ;; Standard test.
  2313. (should
  2314. (org-test-with-temp-text "Some text\nSome other text"
  2315. (progn (org-beginning-of-line) (bolp))))
  2316. ;; Standard test with `visual-line-mode'.
  2317. (should-not
  2318. (org-test-with-temp-text "A long line of text\nSome other text"
  2319. (progn (visual-line-mode)
  2320. (forward-char 2)
  2321. (dotimes (i 1000) (insert "very "))
  2322. (org-beginning-of-line)
  2323. (bolp))))
  2324. ;; At an headline with special movement.
  2325. (should
  2326. (org-test-with-temp-text "* TODO Headline"
  2327. (let ((org-special-ctrl-a/e t))
  2328. (org-end-of-line)
  2329. (and (progn (org-beginning-of-line) (looking-at "Headline"))
  2330. (progn (org-beginning-of-line) (bolp))
  2331. (progn (org-beginning-of-line) (looking-at "Headline"))))))
  2332. ;; Special case: Do not error when the buffer contains only a single
  2333. ;; asterisk.
  2334. (should
  2335. (org-test-with-temp-text "*<point>"
  2336. (let ((org-special-ctrl-a/e t)) (org-beginning-of-line))))
  2337. (should
  2338. (org-test-with-temp-text "*<point>"
  2339. (let ((org-special-ctrl-a/e nil)) (org-beginning-of-line)))))
  2340. (ert-deftest test-org/end-of-line ()
  2341. "Test `org-end-of-line' specifications."
  2342. ;; Standard test.
  2343. (should
  2344. (org-test-with-temp-text "Some text\nSome other text"
  2345. (progn (org-end-of-line) (eolp))))
  2346. ;; Standard test with `visual-line-mode'.
  2347. (should-not
  2348. (org-test-with-temp-text "A long line of text\nSome other text"
  2349. (progn (visual-line-mode)
  2350. (forward-char 2)
  2351. (dotimes (i 1000) (insert "very "))
  2352. (goto-char (point-min))
  2353. (org-end-of-line)
  2354. (eolp))))
  2355. ;; At an headline with special movement.
  2356. (should
  2357. (org-test-with-temp-text "* Headline1 :tag:\n"
  2358. (let ((org-special-ctrl-a/e t))
  2359. (and (progn (org-end-of-line) (looking-at " :tag:"))
  2360. (progn (org-end-of-line) (eolp))
  2361. (progn (org-end-of-line) (looking-at " :tag:"))))))
  2362. ;; At an headline without special movement.
  2363. (should
  2364. (org-test-with-temp-text "* Headline2 :tag:\n"
  2365. (let ((org-special-ctrl-a/e nil))
  2366. (and (progn (org-end-of-line) (eolp))
  2367. (progn (org-end-of-line) (eolp))))))
  2368. ;; At an headline, with reversed movement.
  2369. (should
  2370. (org-test-with-temp-text "* Headline3 :tag:\n"
  2371. (let ((org-special-ctrl-a/e 'reversed)
  2372. (this-command last-command))
  2373. (and (progn (org-end-of-line) (eolp))
  2374. (progn (org-end-of-line) (looking-at " :tag:"))))))
  2375. ;; At a block without hidden contents.
  2376. (should
  2377. (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
  2378. (progn (org-end-of-line) (eolp))))
  2379. ;; At a block with hidden contents.
  2380. (should-not
  2381. (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
  2382. (let ((org-special-ctrl-a/e t))
  2383. (org-hide-block-toggle)
  2384. (org-end-of-line)
  2385. (eobp)))))
  2386. (ert-deftest test-org/open-line ()
  2387. "Test `org-open-line' specifications."
  2388. ;; Call `open-line' outside of tables.
  2389. (should
  2390. (equal "\nText"
  2391. (org-test-with-temp-text "Text"
  2392. (org-open-line 1)
  2393. (buffer-string))))
  2394. ;; At a table, create a row above.
  2395. (should
  2396. (equal "\n| |\n| a |"
  2397. (org-test-with-temp-text "\n<point>| a |"
  2398. (org-open-line 1)
  2399. (buffer-string))))
  2400. ;; At the very first character of the buffer, also call `open-line'.
  2401. (should
  2402. (equal "\n| a |"
  2403. (org-test-with-temp-text "| a |"
  2404. (org-open-line 1)
  2405. (buffer-string))))
  2406. ;; Narrowing does not count.
  2407. (should
  2408. (equal "Text\n| |\n| a |"
  2409. (org-test-with-temp-text "Text\n<point>| a |"
  2410. (narrow-to-region (point) (point-max))
  2411. (org-open-line 1)
  2412. (widen)
  2413. (buffer-string)))))
  2414. (ert-deftest test-org/forward-sentence ()
  2415. "Test `org-forward-sentence' specifications."
  2416. ;; At the end of a table cell, move to the end of the next one.
  2417. (should
  2418. (org-test-with-temp-text "| a<point> | b |"
  2419. (org-forward-sentence)
  2420. (looking-at " |$")))
  2421. ;; Elsewhere in a cell, move to its end.
  2422. (should
  2423. (org-test-with-temp-text "| a<point>c | b |"
  2424. (org-forward-sentence)
  2425. (looking-at " | b |$")))
  2426. ;; Otherwise, simply call `forward-sentence'.
  2427. (should
  2428. (org-test-with-temp-text "Sentence<point> 1. Sentence 2."
  2429. (org-forward-sentence)
  2430. (looking-at " Sentence 2.")))
  2431. (should
  2432. (org-test-with-temp-text "Sentence<point> 1. Sentence 2."
  2433. (org-forward-sentence)
  2434. (org-forward-sentence)
  2435. (eobp)))
  2436. ;; At the end of an element, jump to the next one, without stopping
  2437. ;; on blank lines in-between.
  2438. (should
  2439. (org-test-with-temp-text "Paragraph 1.<point>\n\nParagraph 2."
  2440. (org-forward-sentence)
  2441. (eobp))))
  2442. (ert-deftest test-org/backward-sentence ()
  2443. "Test `org-backward-sentence' specifications."
  2444. ;; At the beginning of a table cell, move to the beginning of the
  2445. ;; previous one.
  2446. (should
  2447. (org-test-with-temp-text "| a | <point>b |"
  2448. (org-backward-sentence)
  2449. (looking-at "a | b |$")))
  2450. ;; Elsewhere in a cell, move to its beginning.
  2451. (should
  2452. (org-test-with-temp-text "| a | b<point>c |"
  2453. (org-backward-sentence)
  2454. (looking-at "bc |$")))
  2455. ;; Otherwise, simply call `backward-sentence'.
  2456. (should
  2457. (org-test-with-temp-text "Sentence 1. Sentence<point> 2."
  2458. (org-backward-sentence)
  2459. (looking-at "Sentence 2.")))
  2460. (should
  2461. (org-test-with-temp-text "Sentence 1. Sentence<point> 2."
  2462. (org-backward-sentence)
  2463. (org-backward-sentence)
  2464. (bobp)))
  2465. ;; Make sure to hit the beginning of a sentence on the same line as
  2466. ;; an item.
  2467. (should
  2468. (org-test-with-temp-text "- Line 1\n line <point>2."
  2469. (org-backward-sentence)
  2470. (looking-at "Line 1"))))
  2471. (ert-deftest test-org/forward-paragraph ()
  2472. "Test `org-forward-paragraph' specifications."
  2473. ;; At end of buffer, return an error.
  2474. (should-error
  2475. (org-test-with-temp-text "Paragraph"
  2476. (goto-char (point-max))
  2477. (org-forward-paragraph)))
  2478. ;; Standard test.
  2479. (should
  2480. (org-test-with-temp-text "P1\n\nP2\n\nP3"
  2481. (org-forward-paragraph)
  2482. (looking-at "P2")))
  2483. ;; Ignore depth.
  2484. (should
  2485. (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n#+END_CENTER\nP2"
  2486. (org-forward-paragraph)
  2487. (looking-at "P1")))
  2488. ;; Do not enter elements with invisible contents.
  2489. (should
  2490. (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n\nP2\n#+END_CENTER\nP3"
  2491. (org-hide-block-toggle)
  2492. (org-forward-paragraph)
  2493. (looking-at "P3")))
  2494. ;; On an affiliated keyword, jump to the beginning of the element.
  2495. (should
  2496. (org-test-with-temp-text "#+name: para\n#+caption: caption\nPara"
  2497. (org-forward-paragraph)
  2498. (looking-at "Para")))
  2499. ;; On an item or a footnote definition, move to the second element
  2500. ;; inside, if any.
  2501. (should
  2502. (org-test-with-temp-text "- Item1\n\n Paragraph\n- Item2"
  2503. (org-forward-paragraph)
  2504. (looking-at " Paragraph")))
  2505. (should
  2506. (org-test-with-temp-text "[fn:1] Def1\n\nParagraph\n\n[fn:2] Def2"
  2507. (org-forward-paragraph)
  2508. (looking-at "Paragraph")))
  2509. ;; On an item, or a footnote definition, when the first line is
  2510. ;; empty, move to the first item.
  2511. (should
  2512. (org-test-with-temp-text "- \n\n Paragraph\n- Item2"
  2513. (org-forward-paragraph)
  2514. (looking-at " Paragraph")))
  2515. (should
  2516. (org-test-with-temp-text "[fn:1]\n\nParagraph\n\n[fn:2] Def2"
  2517. (org-forward-paragraph)
  2518. (looking-at "Paragraph")))
  2519. ;; On a table (resp. a property drawer) do not move through table
  2520. ;; rows (resp. node properties).
  2521. (should
  2522. (org-test-with-temp-text "| a | b |\n| c | d |\nParagraph"
  2523. (org-forward-paragraph)
  2524. (looking-at "Paragraph")))
  2525. (should
  2526. (org-test-with-temp-text
  2527. "* H\n<point>:PROPERTIES:\n:prop: value\n:END:\nParagraph"
  2528. (org-forward-paragraph)
  2529. (looking-at "Paragraph")))
  2530. ;; On a verse or source block, stop after blank lines.
  2531. (should
  2532. (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n#+END_VERSE"
  2533. (org-forward-paragraph)
  2534. (looking-at "L2")))
  2535. (should
  2536. (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n#+END_SRC"
  2537. (org-forward-paragraph)
  2538. (looking-at "L2"))))
  2539. (ert-deftest test-org/backward-paragraph ()
  2540. "Test `org-backward-paragraph' specifications."
  2541. ;; Error at beginning of buffer.
  2542. (should-error
  2543. (org-test-with-temp-text "Paragraph"
  2544. (org-backward-paragraph)))
  2545. ;; Regular test.
  2546. (should
  2547. (org-test-with-temp-text "P1\n\nP2\n\nP3"
  2548. (goto-char (point-max))
  2549. (org-backward-paragraph)
  2550. (looking-at "P3")))
  2551. (should
  2552. (org-test-with-temp-text "P1\n\nP2\n\nP3"
  2553. (goto-char (point-max))
  2554. (beginning-of-line)
  2555. (org-backward-paragraph)
  2556. (looking-at "P2")))
  2557. ;; Ignore depth.
  2558. (should
  2559. (org-test-with-temp-text "P1\n\n#+BEGIN_CENTER\nP2\n#+END_CENTER\nP3"
  2560. (goto-char (point-max))
  2561. (beginning-of-line)
  2562. (org-backward-paragraph)
  2563. (looking-at "P2")))
  2564. ;; Ignore invisible elements.
  2565. (should
  2566. (org-test-with-temp-text "* H1\n P1\n* H2"
  2567. (org-cycle)
  2568. (goto-char (point-max))
  2569. (beginning-of-line)
  2570. (org-backward-paragraph)
  2571. (bobp)))
  2572. ;; On an affiliated keyword, jump to the first one.
  2573. (should
  2574. (org-test-with-temp-text "P1\n#+name: n\n#+caption: c1\n#+caption: c2\nP2"
  2575. (search-forward "c2")
  2576. (org-backward-paragraph)
  2577. (looking-at "#\\+name")))
  2578. ;; On the second element in an item or a footnote definition, jump
  2579. ;; to item or the definition.
  2580. (should
  2581. (org-test-with-temp-text "- line1\n\n line2"
  2582. (goto-char (point-max))
  2583. (beginning-of-line)
  2584. (org-backward-paragraph)
  2585. (looking-at "- line1")))
  2586. (should
  2587. (org-test-with-temp-text "[fn:1] line1\n\n line2"
  2588. (goto-char (point-max))
  2589. (beginning-of-line)
  2590. (org-backward-paragraph)
  2591. (looking-at "\\[fn:1\\] line1")))
  2592. ;; On a table (resp. a property drawer), ignore table rows
  2593. ;; (resp. node properties).
  2594. (should
  2595. (org-test-with-temp-text "| a | b |\n| c | d |\nP1"
  2596. (goto-char (point-max))
  2597. (beginning-of-line)
  2598. (org-backward-paragraph)
  2599. (bobp)))
  2600. (should
  2601. (org-test-with-temp-text "* H\n:PROPERTIES:\n:prop: value\n:END:\n<point>P1"
  2602. (org-backward-paragraph)
  2603. (looking-at ":PROPERTIES:")))
  2604. ;; On a source or verse block, stop before blank lines.
  2605. (should
  2606. (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n\nL3\n#+END_VERSE"
  2607. (search-forward "L3")
  2608. (beginning-of-line)
  2609. (org-backward-paragraph)
  2610. (looking-at "L2")))
  2611. (should
  2612. (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n\nL3#+END_SRC"
  2613. (search-forward "L3")
  2614. (beginning-of-line)
  2615. (org-backward-paragraph)
  2616. (looking-at "L2"))))
  2617. (ert-deftest test-org/forward-element ()
  2618. "Test `org-forward-element' specifications."
  2619. ;; 1. At EOB: should error.
  2620. (org-test-with-temp-text "Some text\n"
  2621. (goto-char (point-max))
  2622. (should-error (org-forward-element)))
  2623. ;; 2. Standard move: expected to ignore blank lines.
  2624. (org-test-with-temp-text "First paragraph.\n\n\nSecond paragraph."
  2625. (org-forward-element)
  2626. (should (looking-at (regexp-quote "Second paragraph."))))
  2627. ;; 3. Headline tests.
  2628. (org-test-with-temp-text "
  2629. * Head 1
  2630. ** Head 1.1
  2631. *** Head 1.1.1
  2632. ** Head 1.2"
  2633. ;; 3.1. At an headline beginning: move to next headline at the
  2634. ;; same level.
  2635. (goto-line 3)
  2636. (org-forward-element)
  2637. (should (looking-at (regexp-quote "** Head 1.2")))
  2638. ;; 3.2. At an headline beginning: move to parent headline if no
  2639. ;; headline at the same level.
  2640. (goto-line 3)
  2641. (org-forward-element)
  2642. (should (looking-at (regexp-quote "** Head 1.2"))))
  2643. ;; 4. Greater element tests.
  2644. (org-test-with-temp-text
  2645. "#+BEGIN_CENTER\nInside.\n#+END_CENTER\n\nOutside."
  2646. ;; 4.1. At a greater element: expected to skip contents.
  2647. (org-forward-element)
  2648. (should (looking-at (regexp-quote "Outside.")))
  2649. ;; 4.2. At the end of greater element contents: expected to skip
  2650. ;; to the end of the greater element.
  2651. (goto-line 2)
  2652. (org-forward-element)
  2653. (should (looking-at (regexp-quote "Outside."))))
  2654. ;; 5. List tests.
  2655. (org-test-with-temp-text "
  2656. - item1
  2657. - sub1
  2658. - sub2
  2659. - sub3
  2660. Inner paragraph.
  2661. - item2
  2662. Outside."
  2663. ;; 5.1. At list top point: expected to move to the element after
  2664. ;; the list.
  2665. (goto-line 2)
  2666. (org-forward-element)
  2667. (should (looking-at (regexp-quote "Outside.")))
  2668. ;; 5.2. Special case: at the first line of a sub-list, but not at
  2669. ;; beginning of line, move to next item.
  2670. (goto-line 2)
  2671. (forward-char)
  2672. (org-forward-element)
  2673. (should (looking-at "- item2"))
  2674. (goto-line 4)
  2675. (forward-char)
  2676. (org-forward-element)
  2677. (should (looking-at " - sub2"))
  2678. ;; 5.3 At sub-list beginning: expected to move after the sub-list.
  2679. (goto-line 4)
  2680. (org-forward-element)
  2681. (should (looking-at (regexp-quote " Inner paragraph.")))
  2682. ;; 5.4. At sub-list end: expected to move outside the sub-list.
  2683. (goto-line 8)
  2684. (org-forward-element)
  2685. (should (looking-at (regexp-quote " Inner paragraph.")))
  2686. ;; 5.5. At an item: expected to move to next item, if any.
  2687. (goto-line 6)
  2688. (org-forward-element)
  2689. (should (looking-at " - sub3"))))
  2690. (ert-deftest test-org/backward-element ()
  2691. "Test `org-backward-element' specifications."
  2692. ;; 1. Should error at BOB.
  2693. (org-test-with-temp-text " \nParagraph."
  2694. (should-error (org-backward-element)))
  2695. ;; 2. Should move at BOB when called on the first element in buffer.
  2696. (should
  2697. (org-test-with-temp-text "\n#+TITLE: test"
  2698. (progn (forward-line)
  2699. (org-backward-element)
  2700. (bobp))))
  2701. ;; 3. Not at the beginning of an element: move at its beginning.
  2702. (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
  2703. (goto-line 3)
  2704. (end-of-line)
  2705. (org-backward-element)
  2706. (should (looking-at (regexp-quote "Paragraph2."))))
  2707. ;; 4. Headline tests.
  2708. (org-test-with-temp-text "
  2709. * Head 1
  2710. ** Head 1.1
  2711. *** Head 1.1.1
  2712. ** Head 1.2"
  2713. ;; 4.1. At an headline beginning: move to previous headline at the
  2714. ;; same level.
  2715. (goto-line 5)
  2716. (org-backward-element)
  2717. (should (looking-at (regexp-quote "** Head 1.1")))
  2718. ;; 4.2. At an headline beginning: move to parent headline if no
  2719. ;; headline at the same level.
  2720. (goto-line 3)
  2721. (org-backward-element)
  2722. (should (looking-at (regexp-quote "* Head 1")))
  2723. ;; 4.3. At the first top-level headline: should error.
  2724. (goto-line 2)
  2725. (should-error (org-backward-element)))
  2726. ;; 5. At beginning of first element inside a greater element:
  2727. ;; expected to move to greater element's beginning.
  2728. (org-test-with-temp-text "Before.\n#+BEGIN_CENTER\nInside.\n#+END_CENTER"
  2729. (goto-line 3)
  2730. (org-backward-element)
  2731. (should (looking-at "#\\+BEGIN_CENTER")))
  2732. ;; 6. At the beginning of the first element in a section: should
  2733. ;; move back to headline, if any.
  2734. (should
  2735. (org-test-with-temp-text "#+TITLE: test\n* Headline\n\nParagraph"
  2736. (progn (goto-char (point-max))
  2737. (beginning-of-line)
  2738. (org-backward-element)
  2739. (org-at-heading-p))))
  2740. ;; 7. List tests.
  2741. (org-test-with-temp-text "
  2742. - item1
  2743. - sub1
  2744. - sub2
  2745. - sub3
  2746. Inner paragraph.
  2747. - item2
  2748. Outside."
  2749. ;; 7.1. At beginning of sub-list: expected to move to the
  2750. ;; paragraph before it.
  2751. (goto-line 4)
  2752. (org-backward-element)
  2753. (should (looking-at "item1"))
  2754. ;; 7.2. At an item in a list: expected to move at previous item.
  2755. (goto-line 8)
  2756. (org-backward-element)
  2757. (should (looking-at " - sub2"))
  2758. (goto-line 12)
  2759. (org-backward-element)
  2760. (should (looking-at "- item1"))
  2761. ;; 7.3. At end of list/sub-list: expected to move to list/sub-list
  2762. ;; beginning.
  2763. (goto-line 10)
  2764. (org-backward-element)
  2765. (should (looking-at " - sub1"))
  2766. (goto-line 15)
  2767. (org-backward-element)
  2768. (should (looking-at "- item1"))
  2769. ;; 7.4. At blank-lines before list end: expected to move to top
  2770. ;; item.
  2771. (goto-line 14)
  2772. (org-backward-element)
  2773. (should (looking-at "- item1"))))
  2774. (ert-deftest test-org/up-element ()
  2775. "Test `org-up-element' specifications."
  2776. ;; 1. At BOB or with no surrounding element: should error.
  2777. (org-test-with-temp-text "Paragraph."
  2778. (should-error (org-up-element)))
  2779. (org-test-with-temp-text "* Head1\n* Head2"
  2780. (goto-line 2)
  2781. (should-error (org-up-element)))
  2782. (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
  2783. (goto-line 3)
  2784. (should-error (org-up-element)))
  2785. ;; 2. At an headline: move to parent headline.
  2786. (org-test-with-temp-text "* Head1\n** Sub-Head1\n** Sub-Head2"
  2787. (goto-line 3)
  2788. (org-up-element)
  2789. (should (looking-at "\\* Head1")))
  2790. ;; 3. Inside a greater element: move to greater element beginning.
  2791. (org-test-with-temp-text
  2792. "Before.\n#+BEGIN_CENTER\nParagraph1\nParagraph2\n#+END_CENTER\n"
  2793. (goto-line 3)
  2794. (org-up-element)
  2795. (should (looking-at "#\\+BEGIN_CENTER")))
  2796. ;; 4. List tests.
  2797. (org-test-with-temp-text "* Top
  2798. - item1
  2799. - sub1
  2800. - sub2
  2801. Paragraph within sub2.
  2802. - item2"
  2803. ;; 4.1. Within an item: move to the item beginning.
  2804. (goto-line 8)
  2805. (org-up-element)
  2806. (should (looking-at " - sub2"))
  2807. ;; 4.2. At an item in a sub-list: move to parent item.
  2808. (goto-line 4)
  2809. (org-up-element)
  2810. (should (looking-at "- item1"))
  2811. ;; 4.3. At an item in top list: move to beginning of whole list.
  2812. (goto-line 10)
  2813. (org-up-element)
  2814. (should (looking-at "- item1"))
  2815. ;; 4.4. Special case. At very top point: should move to parent of
  2816. ;; list.
  2817. (goto-line 2)
  2818. (org-up-element)
  2819. (should (looking-at "\\* Top"))))
  2820. (ert-deftest test-org/down-element ()
  2821. "Test `org-down-element' specifications."
  2822. ;; Error when the element hasn't got a recursive type.
  2823. (org-test-with-temp-text "Paragraph."
  2824. (should-error (org-down-element)))
  2825. ;; Error when the element has no contents
  2826. (org-test-with-temp-text "* Headline"
  2827. (should-error (org-down-element)))
  2828. ;; When at a plain-list, move to first item.
  2829. (org-test-with-temp-text "- Item 1\n - Item 1.1\n - Item 2.2"
  2830. (goto-line 2)
  2831. (org-down-element)
  2832. (should (looking-at " - Item 1.1")))
  2833. (org-test-with-temp-text "#+NAME: list\n- Item 1"
  2834. (org-down-element)
  2835. (should (looking-at " Item 1")))
  2836. ;; When at a table, move to first row
  2837. (org-test-with-temp-text "#+NAME: table\n| a | b |"
  2838. (org-down-element)
  2839. (should (looking-at " a | b |")))
  2840. ;; Otherwise, move inside the greater element.
  2841. (org-test-with-temp-text "#+BEGIN_CENTER\nParagraph.\n#+END_CENTER"
  2842. (org-down-element)
  2843. (should (looking-at "Paragraph"))))
  2844. (ert-deftest test-org/drag-element-backward ()
  2845. "Test `org-drag-element-backward' specifications."
  2846. ;; Standard test.
  2847. (should
  2848. (equal
  2849. "#+key2: val2\n#+key1: val1\n#+key3: val3"
  2850. (org-test-with-temp-text "#+key1: val1\n<point>#+key2: val2\n#+key3: val3"
  2851. (org-drag-element-backward)
  2852. (buffer-string))))
  2853. (should
  2854. (equal
  2855. "#+BEGIN_CENTER\n#+B: 2\n#+A: 1\n#+END_CENTER"
  2856. (org-test-with-temp-text
  2857. "#+BEGIN_CENTER\n#+A: 1\n<point>#+B: 2\n#+END_CENTER"
  2858. (org-drag-element-backward)
  2859. (buffer-string))))
  2860. ;; Preserve blank lines.
  2861. (should
  2862. (equal "Paragraph 2\n\n\nPara1\n\nPara3"
  2863. (org-test-with-temp-text "Para1\n\n\n<point>Paragraph 2\n\nPara3"
  2864. (org-drag-element-backward)
  2865. (buffer-string))))
  2866. ;; Preserve column.
  2867. (should
  2868. (org-test-with-temp-text "#+key1: v\n#+key<point>2: v\n#+key3: v"
  2869. (org-drag-element-backward)
  2870. (looking-at-p "2")))
  2871. ;; Error when trying to move first element of buffer.
  2872. (should-error
  2873. (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
  2874. (org-drag-element-backward)))
  2875. ;; Error when trying to swap nested elements.
  2876. (should-error
  2877. (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
  2878. (forward-line)
  2879. (org-drag-element-backward)))
  2880. ;; Error when trying to swap an headline element and a non-headline
  2881. ;; element.
  2882. (should-error
  2883. (org-test-with-temp-text "Test.\n* Head 1"
  2884. (forward-line)
  2885. (org-drag-element-backward)))
  2886. ;; Preserve visibility of elements and their contents.
  2887. (should
  2888. (equal '((63 . 82) (26 . 48))
  2889. (org-test-with-temp-text "
  2890. #+BEGIN_CENTER
  2891. Text.
  2892. #+END_CENTER
  2893. - item 1
  2894. #+BEGIN_QUOTE
  2895. Text.
  2896. #+END_QUOTE"
  2897. (while (search-forward "BEGIN_" nil t) (org-cycle))
  2898. (search-backward "- item 1")
  2899. (org-drag-element-backward)
  2900. (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
  2901. (overlays-in (point-min) (point-max)))))))
  2902. (ert-deftest test-org/drag-element-forward ()
  2903. "Test `org-drag-element-forward' specifications."
  2904. ;; 1. Error when trying to move first element of buffer.
  2905. (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
  2906. (goto-line 3)
  2907. (should-error (org-drag-element-forward)))
  2908. ;; 2. Error when trying to swap nested elements.
  2909. (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
  2910. (forward-line)
  2911. (should-error (org-drag-element-forward)))
  2912. ;; 3. Error when trying to swap a non-headline element and an
  2913. ;; headline.
  2914. (org-test-with-temp-text "Test.\n* Head 1"
  2915. (should-error (org-drag-element-forward)))
  2916. ;; 4. Otherwise, swap elements, preserving column and blank lines
  2917. ;; between elements.
  2918. (org-test-with-temp-text "Paragraph 1\n\n\nPara2\n\nPara3"
  2919. (search-forward "graph")
  2920. (org-drag-element-forward)
  2921. (should (equal (buffer-string) "Para2\n\n\nParagraph 1\n\nPara3"))
  2922. (should (looking-at " 1")))
  2923. ;; 5. Preserve visibility of elements and their contents.
  2924. (org-test-with-temp-text "
  2925. #+BEGIN_CENTER
  2926. Text.
  2927. #+END_CENTER
  2928. - item 1
  2929. #+BEGIN_QUOTE
  2930. Text.
  2931. #+END_QUOTE"
  2932. (while (search-forward "BEGIN_" nil t) (org-cycle))
  2933. (search-backward "#+BEGIN_CENTER")
  2934. (org-drag-element-forward)
  2935. (should
  2936. (equal
  2937. '((63 . 82) (26 . 48))
  2938. (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
  2939. (overlays-in (point-min) (point-max)))))))
  2940. (ert-deftest test-org/next-block ()
  2941. "Test `org-next-block' specifications."
  2942. ;; Regular test.
  2943. (should
  2944. (org-test-with-temp-text "Paragraph\n#+BEGIN_CENTER\ncontents\n#+END_CENTER"
  2945. (org-next-block 1)
  2946. (looking-at "#\\+BEGIN_CENTER")))
  2947. ;; Ignore case.
  2948. (should
  2949. (org-test-with-temp-text "Paragraph\n#+begin_center\ncontents\n#+end_center"
  2950. (let ((case-fold-search nil))
  2951. (org-next-block 1)
  2952. (looking-at "#\\+begin_center"))))
  2953. ;; Ignore current line.
  2954. (should
  2955. (org-test-with-temp-text
  2956. "#+BEGIN_QUOTE\n#+END_QUOTE\n#+BEGIN_CENTER\n#+END_CENTER"
  2957. (org-next-block 1)
  2958. (looking-at "#\\+BEGIN_CENTER")))
  2959. ;; Throw an error when no block is found.
  2960. (should-error
  2961. (org-test-with-temp-text "Paragraph"
  2962. (org-next-block 1)))
  2963. ;; With an argument, skip many blocks at once.
  2964. (should
  2965. (org-test-with-temp-text
  2966. "Start\n#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE"
  2967. (org-next-block 2)
  2968. (looking-at "#\\+BEGIN_QUOTE")))
  2969. ;; With optional argument BLOCK-REGEXP, filter matched blocks.
  2970. (should
  2971. (org-test-with-temp-text
  2972. "Start\n#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE"
  2973. (org-next-block 1 nil "^[ \t]*#\\+BEGIN_QUOTE")
  2974. (looking-at "#\\+BEGIN_QUOTE")))
  2975. ;; Optional argument is also case-insensitive.
  2976. (should
  2977. (org-test-with-temp-text
  2978. "Start\n#+BEGIN_CENTER\nA\n#+END_CENTER\n#+begin_quote\nB\n#+end_quote"
  2979. (let ((case-fold-search nil))
  2980. (org-next-block 1 nil "^[ \t]*#\\+BEGIN_QUOTE")
  2981. (looking-at "#\\+begin_quote")))))
  2982. (ert-deftest test-org/previous-block ()
  2983. "Test `org-previous-block' specifications."
  2984. ;; Regular test.
  2985. (should
  2986. (org-test-with-temp-text "#+BEGIN_CENTER\ncontents\n#+END_CENTER\n<point>"
  2987. (org-previous-block 1)
  2988. (looking-at "#\\+BEGIN_CENTER")))
  2989. ;; Ignore case.
  2990. (should
  2991. (org-test-with-temp-text "#+begin_center\ncontents\n#+end_center\n<point>"
  2992. (let ((case-fold-search nil))
  2993. (org-previous-block 1)
  2994. (looking-at "#\\+begin_center"))))
  2995. ;; Ignore current line.
  2996. (should
  2997. (org-test-with-temp-text
  2998. "#+BEGIN_QUOTE\n#+END_QUOTE\n#+BEGIN_CENTER<point>\n#+END_CENTER"
  2999. (org-previous-block 1)
  3000. (looking-at "#\\+BEGIN_QUOTE")))
  3001. ;; Throw an error when no block is found.
  3002. (should-error
  3003. (org-test-with-temp-text "Paragraph<point>"
  3004. (org-previous-block 1)))
  3005. ;; With an argument, skip many blocks at once.
  3006. (should
  3007. (org-test-with-temp-text
  3008. "#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE\n<point>"
  3009. (org-previous-block 2)
  3010. (looking-at "#\\+BEGIN_CENTER")))
  3011. ;; With optional argument BLOCK-REGEXP, filter matched blocks.
  3012. (should
  3013. (org-test-with-temp-text
  3014. "#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE\n<point>"
  3015. (org-previous-block 1 "^[ \t]*#\\+BEGIN_QUOTE")
  3016. (looking-at "#\\+BEGIN_QUOTE")))
  3017. ;; Optional argument is also case-insensitive.
  3018. (should
  3019. (org-test-with-temp-text
  3020. "#+BEGIN_CENTER\nA\n#+END_CENTER\n#+begin_quote\nB\n#+end_quote\n<point>"
  3021. (let ((case-fold-search nil))
  3022. (org-next-block 1 "^[ \t]*#\\+BEGIN_QUOTE")
  3023. (looking-at "#\\+begin_quote")))))
  3024. ;;; Outline structure
  3025. (ert-deftest test-org/demote ()
  3026. "Test `org-demote' specifications."
  3027. ;; Add correct number of stars according to `org-odd-levels-only'.
  3028. (should
  3029. (= 2
  3030. (org-test-with-temp-text "* H"
  3031. (let ((org-odd-levels-only nil)) (org-demote))
  3032. (org-current-level))))
  3033. (should
  3034. (= 3
  3035. (org-test-with-temp-text "* H"
  3036. (let ((org-odd-levels-only t)) (org-demote))
  3037. (org-current-level))))
  3038. ;; When `org-auto-align-tags' is non-nil, move tags accordingly.
  3039. (should
  3040. (org-test-with-temp-text "* H :tag:"
  3041. (let ((org-tags-column 10)
  3042. (org-auto-align-tags t)
  3043. (org-odd-levels-only nil))
  3044. (org-demote))
  3045. (org-move-to-column 10)
  3046. (looking-at-p ":tag:$")))
  3047. (should-not
  3048. (org-test-with-temp-text "* H :tag:"
  3049. (let ((org-tags-column 10)
  3050. (org-auto-align-tags nil)
  3051. (org-odd-levels-only nil))
  3052. (org-demote))
  3053. (org-move-to-column 10)
  3054. (looking-at-p ":tag:$")))
  3055. ;; When `org-adapt-indentation' is non-nil, always indent planning
  3056. ;; info and property drawers accordingly.
  3057. (should
  3058. (= 3
  3059. (org-test-with-temp-text "* H\n SCHEDULED: <2014-03-04 tue.>"
  3060. (let ((org-odd-levels-only nil)
  3061. (org-adapt-indentation t))
  3062. (org-demote))
  3063. (forward-line)
  3064. (org-get-indentation))))
  3065. (should
  3066. (= 3
  3067. (org-test-with-temp-text "* H\n :PROPERTIES:\n :FOO: Bar\n :END:"
  3068. (let ((org-odd-levels-only nil)
  3069. (org-adapt-indentation t))
  3070. (org-demote))
  3071. (forward-line)
  3072. (org-get-indentation))))
  3073. (should-not
  3074. (= 3
  3075. (org-test-with-temp-text "* H\n SCHEDULED: <2014-03-04 tue.>"
  3076. (let ((org-odd-levels-only nil)
  3077. (org-adapt-indentation nil))
  3078. (org-demote))
  3079. (forward-line)
  3080. (org-get-indentation))))
  3081. ;; When `org-adapt-indentation' is non-nil, shift all lines in
  3082. ;; section accordingly. Ignore, however, footnote definitions and
  3083. ;; inlinetasks boundaries.
  3084. (should
  3085. (= 3
  3086. (org-test-with-temp-text "* H\n Paragraph"
  3087. (let ((org-odd-levels-only nil)
  3088. (org-adapt-indentation t))
  3089. (org-demote))
  3090. (forward-line)
  3091. (org-get-indentation))))
  3092. (should
  3093. (= 2
  3094. (org-test-with-temp-text "* H\n Paragraph"
  3095. (let ((org-odd-levels-only nil)
  3096. (org-adapt-indentation nil))
  3097. (org-demote))
  3098. (forward-line)
  3099. (org-get-indentation))))
  3100. (should
  3101. (zerop
  3102. (org-test-with-temp-text "* H\n[fn:1] def line 1\ndef line 2"
  3103. (let ((org-odd-levels-only nil)
  3104. (org-adapt-indentation t))
  3105. (org-demote))
  3106. (goto-char (point-max))
  3107. (org-get-indentation))))
  3108. (should
  3109. (= 3
  3110. (org-test-with-temp-text "* H\n[fn:1] Def.\n\n\n After def."
  3111. (let ((org-odd-levels-only nil)
  3112. (org-adapt-indentation t))
  3113. (org-demote))
  3114. (goto-char (point-max))
  3115. (org-get-indentation))))
  3116. (when (featurep 'org-inlinetask)
  3117. (should
  3118. (zerop
  3119. (let ((org-inlinetask-min-level 5)
  3120. (org-adapt-indentation t))
  3121. (org-test-with-temp-text "* H\n***** I\n***** END"
  3122. (org-demote)
  3123. (forward-line)
  3124. (org-get-indentation))))))
  3125. (when (featurep 'org-inlinetask)
  3126. (should
  3127. (= 3
  3128. (let ((org-inlinetask-min-level 5)
  3129. (org-adapt-indentation t))
  3130. (org-test-with-temp-text "* H\n***** I\n Contents\n***** END"
  3131. (org-demote)
  3132. (forward-line 2)
  3133. (org-get-indentation))))))
  3134. ;; Ignore contents of source blocks or example blocks when
  3135. ;; indentation should be preserved (through
  3136. ;; `org-src-preserve-indentation' or "-i" flag).
  3137. (should-not
  3138. (zerop
  3139. (org-test-with-temp-text "* H\n#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  3140. (let ((org-adapt-indentation t)
  3141. (org-src-preserve-indentation nil))
  3142. (org-demote))
  3143. (forward-line 2)
  3144. (org-get-indentation))))
  3145. (should
  3146. (zerop
  3147. (org-test-with-temp-text "* H\n#+BEGIN_EXAMPLE\n(+ 1 1)\n#+END_EXAMPLE"
  3148. (let ((org-adapt-indentation t)
  3149. (org-src-preserve-indentation t))
  3150. (org-demote))
  3151. (forward-line 2)
  3152. (org-get-indentation))))
  3153. (should
  3154. (zerop
  3155. (org-test-with-temp-text "* H\n#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  3156. (let ((org-adapt-indentation t)
  3157. (org-src-preserve-indentation t))
  3158. (org-demote))
  3159. (forward-line 2)
  3160. (org-get-indentation))))
  3161. (should
  3162. (zerop
  3163. (org-test-with-temp-text
  3164. "* H\n#+BEGIN_SRC emacs-lisp -i\n(+ 1 1)\n#+END_SRC"
  3165. (let ((org-adapt-indentation t)
  3166. (org-src-preserve-indentation nil))
  3167. (org-demote))
  3168. (forward-line 2)
  3169. (org-get-indentation)))))
  3170. (ert-deftest test-org/promote ()
  3171. "Test `org-promote' specifications."
  3172. ;; Return an error if headline is to be promoted to level 0, unless
  3173. ;; `org-allow-promoting-top-level-subtree' is non-nil, in which case
  3174. ;; headline becomes a comment.
  3175. (should-error
  3176. (org-test-with-temp-text "* H"
  3177. (let ((org-allow-promoting-top-level-subtree nil)) (org-promote))))
  3178. (should
  3179. (equal "# H"
  3180. (org-test-with-temp-text "* H"
  3181. (let ((org-allow-promoting-top-level-subtree t)) (org-promote))
  3182. (buffer-string))))
  3183. ;; Remove correct number of stars according to
  3184. ;; `org-odd-levels-only'.
  3185. (should
  3186. (= 2
  3187. (org-test-with-temp-text "*** H"
  3188. (let ((org-odd-levels-only nil)) (org-promote))
  3189. (org-current-level))))
  3190. (should
  3191. (= 1
  3192. (org-test-with-temp-text "*** H"
  3193. (let ((org-odd-levels-only t)) (org-promote))
  3194. (org-current-level))))
  3195. ;; When `org-auto-align-tags' is non-nil, move tags accordingly.
  3196. (should
  3197. (org-test-with-temp-text "** H :tag:"
  3198. (let ((org-tags-column 10)
  3199. (org-auto-align-tags t)
  3200. (org-odd-levels-only nil))
  3201. (org-promote))
  3202. (org-move-to-column 10)
  3203. (looking-at-p ":tag:$")))
  3204. (should-not
  3205. (org-test-with-temp-text "** H :tag:"
  3206. (let ((org-tags-column 10)
  3207. (org-auto-align-tags nil)
  3208. (org-odd-levels-only nil))
  3209. (org-promote))
  3210. (org-move-to-column 10)
  3211. (looking-at-p ":tag:$")))
  3212. ;; When `org-adapt-indentation' is non-nil, always indent planning
  3213. ;; info and property drawers.
  3214. (should
  3215. (= 2
  3216. (org-test-with-temp-text "** H\n SCHEDULED: <2014-03-04 tue.>"
  3217. (let ((org-odd-levels-only nil)
  3218. (org-adapt-indentation t))
  3219. (org-promote))
  3220. (forward-line)
  3221. (org-get-indentation))))
  3222. (should
  3223. (= 2
  3224. (org-test-with-temp-text "** H\n :PROPERTIES:\n :FOO: Bar\n :END:"
  3225. (let ((org-odd-levels-only nil)
  3226. (org-adapt-indentation t))
  3227. (org-promote))
  3228. (forward-line)
  3229. (org-get-indentation))))
  3230. (should-not
  3231. (= 2
  3232. (org-test-with-temp-text "** H\n SCHEDULED: <2014-03-04 tue.>"
  3233. (let ((org-odd-levels-only nil)
  3234. (org-adapt-indentation nil))
  3235. (org-promote))
  3236. (forward-line)
  3237. (org-get-indentation))))
  3238. ;; When `org-adapt-indentation' is non-nil, shift all lines in
  3239. ;; section accordingly. Ignore, however, footnote definitions and
  3240. ;; inlinetasks boundaries.
  3241. (should
  3242. (= 2
  3243. (org-test-with-temp-text "** H\n Paragraph"
  3244. (let ((org-odd-levels-only nil)
  3245. (org-adapt-indentation t))
  3246. (org-promote))
  3247. (forward-line)
  3248. (org-get-indentation))))
  3249. (should-not
  3250. (= 2
  3251. (org-test-with-temp-text "** H\n Paragraph"
  3252. (let ((org-odd-levels-only nil)
  3253. (org-adapt-indentation nil))
  3254. (org-promote))
  3255. (forward-line)
  3256. (org-get-indentation))))
  3257. (should
  3258. (= 2
  3259. (org-test-with-temp-text "** H\n Paragraph\n[fn:1] line1\nline2"
  3260. (let ((org-odd-levels-only nil)
  3261. (org-adapt-indentation t))
  3262. (org-promote))
  3263. (forward-line)
  3264. (org-get-indentation))))
  3265. (when (featurep 'org-inlinetask)
  3266. (should
  3267. (zerop
  3268. (let ((org-inlinetask-min-level 5)
  3269. (org-adapt-indentation t))
  3270. (org-test-with-temp-text "** H\n***** I\n***** END"
  3271. (org-promote)
  3272. (forward-line)
  3273. (org-get-indentation))))))
  3274. (when (featurep 'org-inlinetask)
  3275. (should
  3276. (= 2
  3277. (let ((org-inlinetask-min-level 5)
  3278. (org-adapt-indentation t))
  3279. (org-test-with-temp-text "** H\n***** I\n Contents\n***** END"
  3280. (org-promote)
  3281. (forward-line 2)
  3282. (org-get-indentation))))))
  3283. ;; Give up shifting if it would break document's structure
  3284. ;; otherwise.
  3285. (should
  3286. (= 3
  3287. (org-test-with-temp-text "** H\n Paragraph\n [fn:1] Def."
  3288. (let ((org-odd-levels-only nil)
  3289. (org-adapt-indentation t))
  3290. (org-promote))
  3291. (forward-line)
  3292. (org-get-indentation))))
  3293. (should
  3294. (= 3
  3295. (org-test-with-temp-text "** H\n Paragraph\n * list."
  3296. (let ((org-odd-levels-only nil)
  3297. (org-adapt-indentation t))
  3298. (org-promote))
  3299. (forward-line)
  3300. (org-get-indentation))))
  3301. ;; Ignore contents of source blocks or example blocks when
  3302. ;; indentation should be preserved (through
  3303. ;; `org-src-preserve-indentation' or "-i" flag).
  3304. (should-not
  3305. (zerop
  3306. (org-test-with-temp-text
  3307. "** H\n #+BEGIN_SRC emacs-lisp\n(+ 1 1)\n #+END_SRC"
  3308. (let ((org-adapt-indentation t)
  3309. (org-src-preserve-indentation nil)
  3310. (org-odd-levels-only nil))
  3311. (org-promote))
  3312. (forward-line)
  3313. (org-get-indentation))))
  3314. (should
  3315. (zerop
  3316. (org-test-with-temp-text
  3317. "** H\n #+BEGIN_EXAMPLE\nContents\n #+END_EXAMPLE"
  3318. (let ((org-adapt-indentation t)
  3319. (org-src-preserve-indentation t)
  3320. (org-odd-levels-only nil))
  3321. (org-promote))
  3322. (forward-line)
  3323. (org-get-indentation))))
  3324. (should
  3325. (zerop
  3326. (org-test-with-temp-text
  3327. "** H\n #+BEGIN_SRC emacs-lisp\n(+ 1 1)\n #+END_SRC"
  3328. (let ((org-adapt-indentation t)
  3329. (org-src-preserve-indentation t)
  3330. (org-odd-levels-only nil))
  3331. (org-promote))
  3332. (forward-line)
  3333. (org-get-indentation))))
  3334. (should
  3335. (zerop
  3336. (org-test-with-temp-text
  3337. "** H\n #+BEGIN_SRC emacs-lisp -i\n(+ 1 1)\n #+END_SRC"
  3338. (let ((org-adapt-indentation t)
  3339. (org-src-preserve-indentation nil)
  3340. (org-odd-levels-only nil))
  3341. (org-promote))
  3342. (forward-line)
  3343. (org-get-indentation)))))
  3344. ;;; Planning
  3345. (ert-deftest test-org/at-planning-p ()
  3346. "Test `org-at-planning-p' specifications."
  3347. ;; Regular test.
  3348. (should
  3349. (org-test-with-temp-text "* Headline\n<point>DEADLINE: <2014-03-04 tue.>"
  3350. (org-at-planning-p)))
  3351. (should-not
  3352. (org-test-with-temp-text "DEADLINE: <2014-03-04 tue.>"
  3353. (org-at-planning-p)))
  3354. ;; Correctly find planning attached to inlinetasks.
  3355. (when (featurep 'org-inlinetask)
  3356. (should
  3357. (org-test-with-temp-text
  3358. "*** Inlinetask\n<point>DEADLINE: <2014-03-04 tue.>\n*** END"
  3359. (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))
  3360. (should-not
  3361. (org-test-with-temp-text
  3362. "*** Inlinetask\n<point>DEADLINE: <2014-03-04 tue.>"
  3363. (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))
  3364. (should-not
  3365. (org-test-with-temp-text
  3366. "* Headline\n*** Inlinetask\n<point>DEADLINE: <2014-03-04 tue.>"
  3367. (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))
  3368. (should-not
  3369. (org-test-with-temp-text
  3370. "* Headline\n*** Inlinetask\n*** END\n<point>DEADLINE: <2014-03-04 tue.>"
  3371. (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))))
  3372. (ert-deftest test-org/add-planning-info ()
  3373. "Test `org-add-planning-info'."
  3374. ;; Create deadline when `org-adapt-indentation' is non-nil.
  3375. (should
  3376. (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
  3377. (org-test-with-temp-text "* H\nParagraph<point>"
  3378. (let ((org-adapt-indentation t))
  3379. (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
  3380. (replace-regexp-in-string
  3381. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  3382. nil nil 1))))
  3383. ;; Create deadline when `org-adapt-indentation' is nil.
  3384. (should
  3385. (equal "* H\nDEADLINE: <2015-06-25>\nParagraph"
  3386. (org-test-with-temp-text "* H\nParagraph<point>"
  3387. (let ((org-adapt-indentation nil))
  3388. (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
  3389. (replace-regexp-in-string
  3390. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  3391. nil nil 1))))
  3392. ;; Update deadline when `org-adapt-indentation' is non-nil.
  3393. (should
  3394. (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
  3395. (org-test-with-temp-text "\
  3396. * H
  3397. DEADLINE: <2015-06-24 Wed>
  3398. Paragraph<point>"
  3399. (let ((org-adapt-indentation t))
  3400. (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
  3401. (replace-regexp-in-string
  3402. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  3403. nil nil 1))))
  3404. ;; Update deadline when `org-adapt-indentation' is nil.
  3405. (should
  3406. (equal "* H\nDEADLINE: <2015-06-25>\nParagraph"
  3407. (org-test-with-temp-text "\
  3408. * H
  3409. DEADLINE: <2015-06-24 Wed>
  3410. Paragraph<point>"
  3411. (let ((org-adapt-indentation nil))
  3412. (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
  3413. (replace-regexp-in-string
  3414. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  3415. nil nil 1))))
  3416. ;; Schedule when `org-adapt-indentation' is non-nil.
  3417. (should
  3418. (equal "* H\n SCHEDULED: <2015-06-25>\nParagraph"
  3419. (org-test-with-temp-text "* H\nParagraph<point>"
  3420. (let ((org-adapt-indentation t))
  3421. (org-add-planning-info 'scheduled "<2015-06-25 Thu>"))
  3422. (replace-regexp-in-string
  3423. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  3424. nil nil 1))))
  3425. ;; Schedule when `org-adapt-indentation' is nil.
  3426. (should
  3427. (equal "* H\nSCHEDULED: <2015-06-25>\nParagraph"
  3428. (org-test-with-temp-text "* H\nParagraph<point>"
  3429. (let ((org-adapt-indentation nil))
  3430. (org-add-planning-info 'scheduled "<2015-06-25 Thu>"))
  3431. (replace-regexp-in-string
  3432. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  3433. nil nil 1))))
  3434. ;; Add deadline when scheduled.
  3435. (should
  3436. (equal "\
  3437. * H
  3438. DEADLINE: <2015-06-25> SCHEDULED: <2015-06-24>
  3439. Paragraph"
  3440. (org-test-with-temp-text "\
  3441. * H
  3442. SCHEDULED: <2015-06-24 Wed>
  3443. Paragraph<point>"
  3444. (let ((org-adapt-indentation t))
  3445. (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
  3446. (replace-regexp-in-string
  3447. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  3448. nil nil 1))))
  3449. ;; Remove middle entry.
  3450. (should
  3451. (equal "\
  3452. * H
  3453. CLOSED: [2015-06-24] SCHEDULED: <2015-06-24>
  3454. Paragraph"
  3455. (org-test-with-temp-text "\
  3456. * H
  3457. CLOSED: [2015-06-24 Wed] DEADLINE: <2015-06-25 Thu> SCHEDULED: <2015-06-24 Wed>
  3458. Paragraph<point>"
  3459. (let ((org-adapt-indentation t))
  3460. (org-add-planning-info nil nil 'deadline))
  3461. (replace-regexp-in-string
  3462. "\\( [.A-Za-z]+\\)[]>]" "" (buffer-string)
  3463. nil nil 1))))
  3464. ;; Remove last entry and then middle entry (order should not
  3465. ;; matter).
  3466. (should
  3467. (equal "\
  3468. * H
  3469. CLOSED: [2015-06-24]
  3470. Paragraph"
  3471. (org-test-with-temp-text "\
  3472. * H
  3473. CLOSED: [2015-06-24 Wed] DEADLINE: <2015-06-25 Thu> SCHEDULED: <2015-06-24 Wed>
  3474. Paragraph<point>"
  3475. (let ((org-adapt-indentation t))
  3476. (org-add-planning-info nil nil 'scheduled 'deadline))
  3477. (replace-regexp-in-string
  3478. "\\( [.A-Za-z]+\\)[]>]" "" (buffer-string)
  3479. nil nil 1))))
  3480. ;; Remove closed when `org-adapt-indentation' is non-nil.
  3481. (should
  3482. (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
  3483. (org-test-with-temp-text "\
  3484. * H
  3485. CLOSED: [2015-06-25 Thu] DEADLINE: <2015-06-25 Thu>
  3486. Paragraph<point>"
  3487. (let ((org-adapt-indentation t))
  3488. (org-add-planning-info nil nil 'closed))
  3489. (replace-regexp-in-string
  3490. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  3491. nil nil 1))))
  3492. (should
  3493. (equal "* H\n Paragraph"
  3494. (org-test-with-temp-text "\
  3495. * H
  3496. CLOSED: [2015-06-25 Thu]
  3497. Paragraph<point>"
  3498. (let ((org-adapt-indentation t))
  3499. (org-add-planning-info nil nil 'closed))
  3500. (replace-regexp-in-string
  3501. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  3502. nil nil 1))))
  3503. ;; Remove closed when `org-adapt-indentation' is nil.
  3504. (should
  3505. (equal "* H\nDEADLINE: <2015-06-25>\nParagraph"
  3506. (org-test-with-temp-text "\
  3507. * H
  3508. CLOSED: [2015-06-25 Thu] DEADLINE: <2015-06-25 Thu>
  3509. Paragraph<point>"
  3510. (let ((org-adapt-indentation nil))
  3511. (org-add-planning-info nil nil 'closed))
  3512. (replace-regexp-in-string
  3513. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  3514. nil nil 1))))
  3515. (should
  3516. (equal "* H\nParagraph"
  3517. (org-test-with-temp-text "\
  3518. * H
  3519. CLOSED: [2015-06-25 Thu]
  3520. Paragraph<point>"
  3521. (let ((org-adapt-indentation nil))
  3522. (org-add-planning-info nil nil 'closed))
  3523. (replace-regexp-in-string
  3524. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  3525. nil nil 1))))
  3526. ;; Remove closed entry and delete empty line.
  3527. (should
  3528. (equal "\
  3529. * H
  3530. Paragraph"
  3531. (org-test-with-temp-text "\
  3532. * H
  3533. CLOSED: [2015-06-24 Wed]
  3534. Paragraph<point>"
  3535. (let ((org-adapt-indentation t))
  3536. (org-add-planning-info nil nil 'closed))
  3537. (replace-regexp-in-string
  3538. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  3539. nil nil 1))))
  3540. ;; Remove one entry and update another.
  3541. (should
  3542. (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
  3543. (org-test-with-temp-text "\
  3544. * H
  3545. SCHEDULED: <2015-06-23 Tue> DEADLINE: <2015-06-24 Wed>
  3546. Paragraph<point>"
  3547. (let ((org-adapt-indentation t))
  3548. (org-add-planning-info 'deadline "<2015-06-25 Thu>" 'scheduled))
  3549. (replace-regexp-in-string
  3550. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  3551. nil nil 1)))))
  3552. ;;; Property API
  3553. (ert-deftest test-org/buffer-property-keys ()
  3554. "Test `org-buffer-property-keys' specifications."
  3555. ;; Retrieve properties accross siblings.
  3556. (should
  3557. (equal '("A" "B")
  3558. (org-test-with-temp-text "
  3559. * H1
  3560. :PROPERTIES:
  3561. :A: 1
  3562. :END:
  3563. * H2
  3564. :PROPERTIES:
  3565. :B: 1
  3566. :END:"
  3567. (org-buffer-property-keys))))
  3568. ;; Retrieve properties accross children.
  3569. (should
  3570. (equal '("A" "B")
  3571. (org-test-with-temp-text "
  3572. * H1
  3573. :PROPERTIES:
  3574. :A: 1
  3575. :END:
  3576. ** H2
  3577. :PROPERTIES:
  3578. :B: 1
  3579. :END:"
  3580. (org-buffer-property-keys))))
  3581. ;; Retrieve muliple properties in the same drawer.
  3582. (should
  3583. (equal '("A" "B")
  3584. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
  3585. (org-buffer-property-keys))))
  3586. ;; Ignore extension symbol in property name.
  3587. (should
  3588. (equal '("A")
  3589. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:END:"
  3590. (org-buffer-property-keys))))
  3591. ;; With non-nil COLUMNS, extract property names from columns.
  3592. (should
  3593. (equal '("A" "B")
  3594. (org-test-with-temp-text "#+COLUMNS: %25ITEM %A %20B"
  3595. (org-buffer-property-keys nil nil t))))
  3596. (should
  3597. (equal '("A" "B" "COLUMNS")
  3598. (org-test-with-temp-text
  3599. "* H\n:PROPERTIES:\n:COLUMNS: %25ITEM %A %20B\n:END:"
  3600. (org-buffer-property-keys nil nil t))))
  3601. ;; With non-nil IGNORE-MALFORMED malformed property drawers are silently ignored.
  3602. (should
  3603. (equal '("A")
  3604. (org-test-with-temp-text
  3605. "* a\n:PROPERTIES:\n:A: 1\n:END:\n* b\n:PROPERTIES:\nsome junk here\n:END:\n"
  3606. (org-buffer-property-keys nil nil nil t)))))
  3607. (ert-deftest test-org/property-values ()
  3608. "Test `org-property-values' specifications."
  3609. ;; Regular test.
  3610. (should
  3611. (equal '("2" "1")
  3612. (org-test-with-temp-text
  3613. "* H\n:PROPERTIES:\n:A: 1\n:END:\n* H\n:PROPERTIES:\n:A: 2\n:END:"
  3614. (org-property-values "A"))))
  3615. ;; Ignore empty values.
  3616. (should-not
  3617. (org-test-with-temp-text
  3618. "* H1\n:PROPERTIES:\n:A:\n:END:\n* H2\n:PROPERTIES:\n:A: \n:END:"
  3619. (org-property-values "A")))
  3620. ;; Take into consideration extended values.
  3621. (should
  3622. (equal '("1 2")
  3623. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:END:"
  3624. (org-property-values "A")))))
  3625. (ert-deftest test-org/find-property ()
  3626. "Test `org-find-property' specifications."
  3627. ;; Regular test.
  3628. (should
  3629. (= 1
  3630. (org-test-with-temp-text "* H\n:PROPERTIES:\n:PROP: value\n:END:"
  3631. (org-find-property "prop"))))
  3632. ;; Ignore false positives.
  3633. (should
  3634. (= 27
  3635. (org-test-with-temp-text
  3636. "* H1\n:DRAWER:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:A: 1\n:END:"
  3637. (org-find-property "A"))))
  3638. ;; Return first entry found in buffer.
  3639. (should
  3640. (= 1
  3641. (org-test-with-temp-text
  3642. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:<point>A: 1\n:END:"
  3643. (org-find-property "A"))))
  3644. ;; Only search visible part of the buffer.
  3645. (should
  3646. (= 31
  3647. (org-test-with-temp-text
  3648. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:<point>A: 1\n:END:"
  3649. (org-narrow-to-subtree)
  3650. (org-find-property "A"))))
  3651. ;; With optional argument, only find entries with a specific value.
  3652. (should-not
  3653. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  3654. (org-find-property "A" "2")))
  3655. (should
  3656. (= 31
  3657. (org-test-with-temp-text
  3658. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:A: 2\n:END:"
  3659. (org-find-property "A" "2"))))
  3660. ;; Use "nil" for explicit nil values.
  3661. (should
  3662. (= 31
  3663. (org-test-with-temp-text
  3664. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:A: nil\n:END:"
  3665. (org-find-property "A" "nil")))))
  3666. (ert-deftest test-org/entry-delete ()
  3667. "Test `org-entry-delete' specifications."
  3668. ;; Regular test.
  3669. (should
  3670. (string-match
  3671. " *:PROPERTIES:\n *:B: +2\n *:END:"
  3672. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
  3673. (org-entry-delete (point) "A")
  3674. (buffer-string))))
  3675. ;; Also remove accumulated properties.
  3676. (should-not
  3677. (string-match
  3678. ":A"
  3679. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:B: 3\n:END:"
  3680. (org-entry-delete (point) "A")
  3681. (buffer-string))))
  3682. ;; When last property is removed, remove the property drawer.
  3683. (should-not
  3684. (string-match
  3685. ":PROPERTIES:"
  3686. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\nParagraph"
  3687. (org-entry-delete (point) "A")
  3688. (buffer-string))))
  3689. ;; Return a non-nil value when some property was removed.
  3690. (should
  3691. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
  3692. (org-entry-delete (point) "A")))
  3693. (should-not
  3694. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
  3695. (org-entry-delete (point) "C"))))
  3696. (ert-deftest test-org/entry-get ()
  3697. "Test `org-entry-get' specifications."
  3698. ;; Regular test.
  3699. (should
  3700. (equal "1"
  3701. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  3702. (org-entry-get (point) "A"))))
  3703. ;; Ignore case.
  3704. (should
  3705. (equal "1"
  3706. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  3707. (org-entry-get (point) "a"))))
  3708. ;; Handle extended values, both before and after base value.
  3709. (should
  3710. (equal "1 2 3"
  3711. (org-test-with-temp-text
  3712. "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:A+: 3\n:END:"
  3713. (org-entry-get (point) "A"))))
  3714. ;; Empty values are returned as the empty string.
  3715. (should
  3716. (equal ""
  3717. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A:\n:END:"
  3718. (org-entry-get (point) "A"))))
  3719. ;; Special nil value. If LITERAL-NIL is non-nil, return "nil",
  3720. ;; otherwise, return nil.
  3721. (should-not
  3722. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: nil\n:END:"
  3723. (org-entry-get (point) "A")))
  3724. (should
  3725. (equal "nil"
  3726. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: nil\n:END:"
  3727. (org-entry-get (point) "A" nil t))))
  3728. ;; Return nil when no property is found, independently on the
  3729. ;; LITERAL-NIL argument.
  3730. (should-not
  3731. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  3732. (org-entry-get (point) "B")))
  3733. (should-not
  3734. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  3735. (org-entry-get (point) "B" nil t)))
  3736. ;; Handle inheritance, when allowed. Include extended values and
  3737. ;; possibly global values.
  3738. (should
  3739. (equal
  3740. "1"
  3741. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
  3742. (org-entry-get (point) "A" t))))
  3743. (should
  3744. (equal
  3745. "1"
  3746. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
  3747. (let ((org-use-property-inheritance t))
  3748. (org-entry-get (point) "A" 'selective)))))
  3749. (should-not
  3750. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
  3751. (let ((org-use-property-inheritance nil))
  3752. (org-entry-get (point) "A" 'selective))))
  3753. (should
  3754. (equal
  3755. "1 2"
  3756. (org-test-with-temp-text
  3757. "* H\n:PROPERTIES:\n:A: 1\n:END:\n** H2\n:PROPERTIES:\n:A+: 2\n:END:"
  3758. (org-entry-get (point-max) "A" t))))
  3759. (should
  3760. (equal "1"
  3761. (org-test-with-temp-text
  3762. "#+PROPERTY: A 0\n* H\n:PROPERTIES:\n:A: 1\n:END:"
  3763. (org-mode-restart)
  3764. (org-entry-get (point-max) "A" t))))
  3765. (should
  3766. (equal "0 1"
  3767. (org-test-with-temp-text
  3768. "#+PROPERTY: A 0\n* H\n:PROPERTIES:\n:A+: 1\n:END:"
  3769. (org-mode-restart)
  3770. (org-entry-get (point-max) "A" t)))))
  3771. (ert-deftest test-org/entry-properties ()
  3772. "Test `org-entry-properties' specifications."
  3773. ;; Get "ITEM" property.
  3774. (should
  3775. (equal "H"
  3776. (org-test-with-temp-text "* TODO H"
  3777. (cdr (assoc "ITEM" (org-entry-properties nil "ITEM"))))))
  3778. (should
  3779. (equal "H"
  3780. (org-test-with-temp-text "* TODO H"
  3781. (cdr (assoc "ITEM" (org-entry-properties))))))
  3782. ;; Get "TODO" property. TODO keywords are case sensitive.
  3783. (should
  3784. (equal "TODO"
  3785. (org-test-with-temp-text "* TODO H"
  3786. (cdr (assoc "TODO" (org-entry-properties nil "TODO"))))))
  3787. (should
  3788. (equal "TODO"
  3789. (org-test-with-temp-text "* TODO H"
  3790. (cdr (assoc "TODO" (org-entry-properties))))))
  3791. (should-not
  3792. (org-test-with-temp-text "* H"
  3793. (assoc "TODO" (org-entry-properties nil "TODO"))))
  3794. (should-not
  3795. (org-test-with-temp-text "* todo H"
  3796. (assoc "TODO" (org-entry-properties nil "TODO"))))
  3797. ;; Get "PRIORITY" property.
  3798. (should
  3799. (equal "A"
  3800. (org-test-with-temp-text "* [#A] H"
  3801. (cdr (assoc "PRIORITY" (org-entry-properties nil "PRIORITY"))))))
  3802. (should
  3803. (equal "A"
  3804. (org-test-with-temp-text "* [#A] H"
  3805. (cdr (assoc "PRIORITY" (org-entry-properties))))))
  3806. (should
  3807. (equal (char-to-string org-default-priority)
  3808. (org-test-with-temp-text "* H"
  3809. (cdr (assoc "PRIORITY" (org-entry-properties nil "PRIORITY"))))))
  3810. ;; Get "FILE" property.
  3811. (should
  3812. (org-test-with-temp-text-in-file "* H\nParagraph"
  3813. (file-equal-p (cdr (assoc "FILE" (org-entry-properties nil "FILE")))
  3814. (buffer-file-name))))
  3815. (should
  3816. (org-test-with-temp-text-in-file "* H\nParagraph"
  3817. (file-equal-p (cdr (assoc "FILE" (org-entry-properties)))
  3818. (buffer-file-name))))
  3819. (should-not
  3820. (org-test-with-temp-text "* H\nParagraph"
  3821. (cdr (assoc "FILE" (org-entry-properties nil "FILE")))))
  3822. ;; Get "TAGS" property.
  3823. (should
  3824. (equal ":tag1:tag2:"
  3825. (org-test-with-temp-text "* H :tag1:tag2:"
  3826. (cdr (assoc "TAGS" (org-entry-properties nil "TAGS"))))))
  3827. (should
  3828. (equal ":tag1:tag2:"
  3829. (org-test-with-temp-text "* H :tag1:tag2:"
  3830. (cdr (assoc "TAGS" (org-entry-properties))))))
  3831. (should-not
  3832. (org-test-with-temp-text "* H"
  3833. (cdr (assoc "TAGS" (org-entry-properties nil "TAGS")))))
  3834. ;; Get "ALLTAGS" property.
  3835. (should
  3836. (equal ":tag1:tag2:"
  3837. (org-test-with-temp-text "* H :tag1:\n<point>** H2 :tag2:"
  3838. (cdr (assoc "ALLTAGS" (org-entry-properties nil "ALLTAGS"))))))
  3839. (should
  3840. (equal ":tag1:tag2:"
  3841. (org-test-with-temp-text "* H :tag1:\n<point>** H2 :tag2:"
  3842. (cdr (assoc "ALLTAGS" (org-entry-properties))))))
  3843. (should-not
  3844. (org-test-with-temp-text "* H"
  3845. (cdr (assoc "ALLTAGS" (org-entry-properties nil "ALLTAGS")))))
  3846. ;; Get "BLOCKED" property.
  3847. (should
  3848. (equal "t"
  3849. (org-test-with-temp-text "* TODO Blocked\n** DONE one\n** TODO two"
  3850. (let ((org-enforce-todo-dependencies t)
  3851. (org-blocker-hook
  3852. '(org-block-todo-from-children-or-siblings-or-parent)))
  3853. (cdr (assoc "BLOCKED" (org-entry-properties nil "BLOCKED")))))))
  3854. (should
  3855. (equal ""
  3856. (org-test-with-temp-text "* TODO Blocked\n** DONE one\n** DONE two"
  3857. (let ((org-enforce-todo-dependencies t)
  3858. (org-blocker-hook
  3859. '(org-block-todo-from-children-or-siblings-or-parent)))
  3860. (cdr (assoc "BLOCKED" (org-entry-properties nil "BLOCKED")))))))
  3861. ;; Get "CLOSED", "DEADLINE" and "SCHEDULED" properties.
  3862. (should
  3863. (equal
  3864. "[2012-03-29 thu.]"
  3865. (org-test-with-temp-text "* H\nCLOSED: [2012-03-29 thu.]"
  3866. (cdr (assoc "CLOSED" (org-entry-properties nil "CLOSED"))))))
  3867. (should
  3868. (equal
  3869. "[2012-03-29 thu.]"
  3870. (org-test-with-temp-text "* H\nCLOSED: [2012-03-29 thu.]"
  3871. (cdr (assoc "CLOSED" (org-entry-properties))))))
  3872. (should-not
  3873. (org-test-with-temp-text "* H"
  3874. (cdr (assoc "CLOSED" (org-entry-properties nil "CLOSED")))))
  3875. (should
  3876. (equal
  3877. "<2014-03-04 tue.>"
  3878. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  3879. (cdr (assoc "DEADLINE" (org-entry-properties nil "DEADLINE"))))))
  3880. (should
  3881. (equal
  3882. "<2014-03-04 tue.>"
  3883. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  3884. (cdr (assoc "DEADLINE" (org-entry-properties))))))
  3885. (should-not
  3886. (org-test-with-temp-text "* H"
  3887. (cdr (assoc "DEADLINE" (org-entry-properties nil "DEADLINE")))))
  3888. (should
  3889. (equal
  3890. "<2014-03-04 tue.>"
  3891. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  3892. (cdr (assoc "SCHEDULED" (org-entry-properties nil "SCHEDULED"))))))
  3893. (should
  3894. (equal
  3895. "<2014-03-04 tue.>"
  3896. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  3897. (cdr (assoc "SCHEDULED" (org-entry-properties))))))
  3898. (should-not
  3899. (org-test-with-temp-text "* H"
  3900. (cdr (assoc "SCHEDULED" (org-entry-properties nil "SCHEDULED")))))
  3901. ;; Get "CATEGORY"
  3902. (should
  3903. (equal "cat"
  3904. (org-test-with-temp-text "#+CATEGORY: cat\n<point>* H"
  3905. (cdr (assoc "CATEGORY" (org-entry-properties))))))
  3906. (should
  3907. (equal "cat"
  3908. (org-test-with-temp-text "#+CATEGORY: cat\n<point>* H"
  3909. (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
  3910. (should
  3911. (equal "cat"
  3912. (org-test-with-temp-text "* H\n:PROPERTIES:\n:CATEGORY: cat\n:END:"
  3913. (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
  3914. (should
  3915. (equal "cat2"
  3916. (org-test-with-temp-text
  3917. (concat "* H\n:PROPERTIES:\n:CATEGORY: cat1\n:END:"
  3918. "\n"
  3919. "** H2\n:PROPERTIES:\n:CATEGORY: cat2\n:END:<point>")
  3920. (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
  3921. ;; Get "TIMESTAMP" and "TIMESTAMP_IA" properties.
  3922. (should
  3923. (equal "<2012-03-29 thu.>"
  3924. (org-test-with-temp-text "* Entry\n<2012-03-29 thu.>"
  3925. (cdr (assoc "TIMESTAMP" (org-entry-properties))))))
  3926. (should
  3927. (equal "[2012-03-29 thu.]"
  3928. (org-test-with-temp-text "* Entry\n[2012-03-29 thu.]"
  3929. (cdr (assoc "TIMESTAMP_IA" (org-entry-properties))))))
  3930. (should
  3931. (equal "<2012-03-29 thu.>"
  3932. (org-test-with-temp-text "* Entry\n[2014-03-04 tue.]<2012-03-29 thu.>"
  3933. (cdr (assoc "TIMESTAMP" (org-entry-properties nil "TIMESTAMP"))))))
  3934. (should
  3935. (equal "[2014-03-04 tue.]"
  3936. (org-test-with-temp-text "* Entry\n<2012-03-29 thu.>[2014-03-04 tue.]"
  3937. (cdr (assoc "TIMESTAMP_IA" (org-entry-properties nil "TIMESTAMP_IA"))))))
  3938. ;; Get standard properties.
  3939. (should
  3940. (equal "1"
  3941. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  3942. (cdr (assoc "A" (org-entry-properties nil 'standard))))))
  3943. ;; Handle extended properties.
  3944. (should
  3945. (equal "1 2 3"
  3946. (org-test-with-temp-text
  3947. "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:A+: 3\n:END:"
  3948. (cdr (assoc "A" (org-entry-properties nil 'standard))))))
  3949. (should
  3950. (equal "1 2 3"
  3951. (org-test-with-temp-text
  3952. "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:a+: 3\n:END:"
  3953. (cdr (assoc "A" (org-entry-properties nil 'standard))))))
  3954. ;; Ignore forbidden (special) properties.
  3955. (should-not
  3956. (org-test-with-temp-text "* H\n:PROPERTIES:\n:TODO: foo\n:END:"
  3957. (cdr (assoc "TODO" (org-entry-properties nil 'standard))))))
  3958. (ert-deftest test-org/entry-put ()
  3959. "Test `org-entry-put' specifications."
  3960. ;; Error when not a string or nil.
  3961. (should-error
  3962. (org-test-with-temp-text "* H\n:PROPERTIES:\n:test: 1\n:END:"
  3963. (org-entry-put 1 "test" 2)))
  3964. ;; Error when property name is invalid.
  3965. (should-error
  3966. (org-test-with-temp-text "* H\n:PROPERTIES:\n:test: 1\n:END:"
  3967. (org-entry-put 1 "no space" "value")))
  3968. (should-error
  3969. (org-test-with-temp-text "* H\n:PROPERTIES:\n:test: 1\n:END:"
  3970. (org-entry-put 1 "" "value")))
  3971. ;; Set "TODO" property.
  3972. (should
  3973. (string-match (regexp-quote " TODO H")
  3974. (org-test-with-temp-text "#+TODO: TODO | DONE\n<point>* H"
  3975. (org-entry-put (point) "TODO" "TODO")
  3976. (buffer-string))))
  3977. (should
  3978. (string-match (regexp-quote "* H")
  3979. (org-test-with-temp-text "#+TODO: TODO | DONE\n<point>* H"
  3980. (org-entry-put (point) "TODO" nil)
  3981. (buffer-string))))
  3982. ;; Set "PRIORITY" property.
  3983. (should
  3984. (equal "* [#A] H"
  3985. (org-test-with-temp-text "* [#B] H"
  3986. (org-entry-put (point) "PRIORITY" "A")
  3987. (buffer-string))))
  3988. (should
  3989. (equal "* H"
  3990. (org-test-with-temp-text "* [#B] H"
  3991. (org-entry-put (point) "PRIORITY" nil)
  3992. (buffer-string))))
  3993. ;; Set "SCHEDULED" property.
  3994. (should
  3995. (string-match "* H\n *SCHEDULED: <2014-03-04 .*?>"
  3996. (org-test-with-temp-text "* H"
  3997. (org-entry-put (point) "SCHEDULED" "2014-03-04")
  3998. (buffer-string))))
  3999. (should
  4000. (string= "* H\n"
  4001. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  4002. (org-entry-put (point) "SCHEDULED" nil)
  4003. (buffer-string))))
  4004. (should
  4005. (string-match "* H\n *SCHEDULED: <2014-03-03 .*?>"
  4006. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  4007. (org-entry-put (point) "SCHEDULED" "earlier")
  4008. (buffer-string))))
  4009. (should
  4010. (string-match "^ *SCHEDULED: <2014-03-05 .*?>"
  4011. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  4012. (org-entry-put (point) "SCHEDULED" "later")
  4013. (buffer-string))))
  4014. ;; Set "DEADLINE" property.
  4015. (should
  4016. (string-match "^ *DEADLINE: <2014-03-04 .*?>"
  4017. (org-test-with-temp-text "* H"
  4018. (org-entry-put (point) "DEADLINE" "2014-03-04")
  4019. (buffer-string))))
  4020. (should
  4021. (string= "* H\n"
  4022. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  4023. (org-entry-put (point) "DEADLINE" nil)
  4024. (buffer-string))))
  4025. (should
  4026. (string-match "^ *DEADLINE: <2014-03-03 .*?>"
  4027. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  4028. (org-entry-put (point) "DEADLINE" "earlier")
  4029. (buffer-string))))
  4030. (should
  4031. (string-match "^ *DEADLINE: <2014-03-05 .*?>"
  4032. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  4033. (org-entry-put (point) "DEADLINE" "later")
  4034. (buffer-string))))
  4035. ;; Set "CATEGORY" property
  4036. (should
  4037. (string-match "^ *:CATEGORY: cat"
  4038. (org-test-with-temp-text "* H"
  4039. (org-entry-put (point) "CATEGORY" "cat")
  4040. (buffer-string))))
  4041. ;; Regular properties, with or without pre-existing drawer.
  4042. (should
  4043. (string-match "^ *:A: +2$"
  4044. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  4045. (org-entry-put (point) "A" "2")
  4046. (buffer-string))))
  4047. (should
  4048. (string-match "^ *:A: +1$"
  4049. (org-test-with-temp-text "* H"
  4050. (org-entry-put (point) "A" "1")
  4051. (buffer-string))))
  4052. ;; Special case: two consecutive headlines.
  4053. (should
  4054. (string-match "\\* A\n *:PROPERTIES:"
  4055. (org-test-with-temp-text "* A\n** B"
  4056. (org-entry-put (point) "A" "1")
  4057. (buffer-string)))))
  4058. ;;; Radio Targets
  4059. (ert-deftest test-org/update-radio-target-regexp ()
  4060. "Test `org-update-radio-target-regexp' specifications."
  4061. ;; Properly update cache with no previous radio target regexp.
  4062. (should
  4063. (eq 'link
  4064. (org-test-with-temp-text "radio\n\nParagraph\n\nradio"
  4065. (save-excursion (goto-char (point-max)) (org-element-context))
  4066. (insert "<<<")
  4067. (search-forward "o")
  4068. (insert ">>>")
  4069. (org-update-radio-target-regexp)
  4070. (goto-char (point-max))
  4071. (org-element-type (org-element-context)))))
  4072. ;; Properly update cache with previous radio target regexp.
  4073. (should
  4074. (eq 'link
  4075. (org-test-with-temp-text "radio\n\nParagraph\n\nradio"
  4076. (save-excursion (goto-char (point-max)) (org-element-context))
  4077. (insert "<<<")
  4078. (search-forward "o")
  4079. (insert ">>>")
  4080. (org-update-radio-target-regexp)
  4081. (search-backward "r")
  4082. (delete-char 5)
  4083. (insert "new")
  4084. (org-update-radio-target-regexp)
  4085. (goto-char (point-max))
  4086. (delete-region (line-beginning-position) (point))
  4087. (insert "new")
  4088. (org-element-type (org-element-context))))))
  4089. ;;; Sparse trees
  4090. (ert-deftest test-org/match-sparse-tree ()
  4091. "Test `org-match-sparse-tree' specifications."
  4092. ;; Match tags.
  4093. (should-not
  4094. (org-test-with-temp-text "* H\n** H1 :tag:"
  4095. (org-match-sparse-tree nil "tag")
  4096. (search-forward "H1")
  4097. (org-invisible-p2)))
  4098. (should
  4099. (org-test-with-temp-text "* H\n** H1 :tag:\n** H2 :tag2:"
  4100. (org-match-sparse-tree nil "tag")
  4101. (search-forward "H2")
  4102. (org-invisible-p2)))
  4103. ;; "-" operator for tags.
  4104. (should-not
  4105. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  4106. (org-match-sparse-tree nil "tag1-tag2")
  4107. (search-forward "H1")
  4108. (org-invisible-p2)))
  4109. (should
  4110. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  4111. (org-match-sparse-tree nil "tag1-tag2")
  4112. (search-forward "H2")
  4113. (org-invisible-p2)))
  4114. ;; "&" operator for tags.
  4115. (should
  4116. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  4117. (org-match-sparse-tree nil "tag1&tag2")
  4118. (search-forward "H1")
  4119. (org-invisible-p2)))
  4120. (should-not
  4121. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  4122. (org-match-sparse-tree nil "tag1&tag2")
  4123. (search-forward "H2")
  4124. (org-invisible-p2)))
  4125. ;; "|" operator for tags.
  4126. (should-not
  4127. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  4128. (org-match-sparse-tree nil "tag1|tag2")
  4129. (search-forward "H1")
  4130. (org-invisible-p2)))
  4131. (should-not
  4132. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  4133. (org-match-sparse-tree nil "tag1|tag2")
  4134. (search-forward "H2")
  4135. (org-invisible-p2)))
  4136. ;; Regexp match on tags.
  4137. (should-not
  4138. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :foo:"
  4139. (org-match-sparse-tree nil "{^tag.*}")
  4140. (search-forward "H1")
  4141. (org-invisible-p2)))
  4142. (should
  4143. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :foo:"
  4144. (org-match-sparse-tree nil "{^tag.*}")
  4145. (search-forward "H2")
  4146. (org-invisible-p2)))
  4147. ;; Match group tags.
  4148. (should-not
  4149. (org-test-with-temp-text
  4150. "#+TAGS: { work : lab }\n* H\n** H1 :work:\n** H2 :lab:"
  4151. (org-match-sparse-tree nil "work")
  4152. (search-forward "H1")
  4153. (org-invisible-p2)))
  4154. (should-not
  4155. (org-test-with-temp-text
  4156. "#+TAGS: { work : lab }\n* H\n** H1 :work:\n** H2 :lab:"
  4157. (org-match-sparse-tree nil "work")
  4158. (search-forward "H2")
  4159. (org-invisible-p2)))
  4160. ;; Match group tags with hard brackets.
  4161. (should-not
  4162. (org-test-with-temp-text
  4163. "#+TAGS: [ work : lab ]\n* H\n** H1 :work:\n** H2 :lab:"
  4164. (org-match-sparse-tree nil "work")
  4165. (search-forward "H1")
  4166. (org-invisible-p2)))
  4167. (should-not
  4168. (org-test-with-temp-text
  4169. "#+TAGS: [ work : lab ]\n* H\n** H1 :work:\n** H2 :lab:"
  4170. (org-match-sparse-tree nil "work")
  4171. (search-forward "H2")
  4172. (org-invisible-p2)))
  4173. ;; Match tags in hierarchies
  4174. (should-not
  4175. (org-test-with-temp-text
  4176. "#+TAGS: [ Lev_1 : Lev_2 ]\n
  4177. #+TAGS: [ Lev_2 : Lev_3 ]\n
  4178. #+TAGS: { Lev_3 : Lev_4 }\n
  4179. * H\n** H1 :Lev_1:\n** H2 :Lev_2:\n** H3 :Lev_3:\n** H4 :Lev_4:"
  4180. (org-match-sparse-tree nil "Lev_1")
  4181. (search-forward "H4")
  4182. (org-invisible-p2)))
  4183. ;; Match regular expressions in tags
  4184. (should-not
  4185. (org-test-with-temp-text
  4186. "#+TAGS: [ Lev : {Lev_[0-9]} ]\n* H\n** H1 :Lev_1:"
  4187. (org-match-sparse-tree nil "Lev")
  4188. (search-forward "H1")
  4189. (org-invisible-p2)))
  4190. (should
  4191. (org-test-with-temp-text
  4192. "#+TAGS: [ Lev : {Lev_[0-9]} ]\n* H\n** H1 :Lev_n:"
  4193. (org-match-sparse-tree nil "Lev")
  4194. (search-forward "H1")
  4195. (org-invisible-p2)))
  4196. ;; Match properties.
  4197. (should
  4198. (org-test-with-temp-text
  4199. "* H\n** H1\n:PROPERTIES:\n:A: 1\n:END:\n** H2\n:PROPERTIES:\n:A: 2\n:END:"
  4200. (org-match-sparse-tree nil "A=\"1\"")
  4201. (search-forward "H2")
  4202. (org-invisible-p2)))
  4203. (should-not
  4204. (org-test-with-temp-text "* H1\n** H2\n:PROPERTIES:\n:A: 1\n:END:"
  4205. (org-match-sparse-tree nil "A=\"1\"")
  4206. (search-forward "H2")
  4207. (org-invisible-p2)))
  4208. ;; Case is not significant when matching properties.
  4209. (should-not
  4210. (org-test-with-temp-text "* H1\n** H2\n:PROPERTIES:\n:A: 1\n:END:"
  4211. (org-match-sparse-tree nil "a=\"1\"")
  4212. (search-forward "H2")
  4213. (org-invisible-p2)))
  4214. (should-not
  4215. (org-test-with-temp-text "* H1\n** H2\n:PROPERTIES:\n:a: 1\n:END:"
  4216. (org-match-sparse-tree nil "A=\"1\"")
  4217. (search-forward "H2")
  4218. (org-invisible-p2)))
  4219. ;; Match special LEVEL property.
  4220. (should-not
  4221. (org-test-with-temp-text "* H\n** H1\n*** H2"
  4222. (let ((org-odd-levels-only nil)) (org-match-sparse-tree nil "LEVEL=2"))
  4223. (search-forward "H1")
  4224. (org-invisible-p2)))
  4225. (should
  4226. (org-test-with-temp-text "* H\n** H1\n*** H2"
  4227. (let ((org-odd-levels-only nil)) (org-match-sparse-tree nil "LEVEL=2"))
  4228. (search-forward "H2")
  4229. (org-invisible-p2)))
  4230. ;; Comparison operators when matching properties.
  4231. (should
  4232. (org-test-with-temp-text
  4233. "* H\n** H1\nSCHEDULED: <2014-03-04 tue.>\n** H2\nSCHEDULED: <2012-03-29 thu.>"
  4234. (org-match-sparse-tree nil "SCHEDULED<=\"<2013-01-01>\"")
  4235. (search-forward "H1")
  4236. (org-invisible-p2)))
  4237. (should-not
  4238. (org-test-with-temp-text
  4239. "* H\n** H1\nSCHEDULED: <2014-03-04 tue.>\n** H2\nSCHEDULED: <2012-03-29 thu.>"
  4240. (org-match-sparse-tree nil "SCHEDULED<=\"<2013-01-01>\"")
  4241. (search-forward "H2")
  4242. (org-invisible-p2)))
  4243. ;; Regexp match on properties values.
  4244. (should-not
  4245. (org-test-with-temp-text
  4246. "* H\n** H1\n:PROPERTIES:\n:A: foo\n:END:\n** H2\n:PROPERTIES:\n:A: bar\n:END:"
  4247. (org-match-sparse-tree nil "A={f.*}")
  4248. (search-forward "H1")
  4249. (org-invisible-p2)))
  4250. (should
  4251. (org-test-with-temp-text
  4252. "* H\n** H1\n:PROPERTIES:\n:A: foo\n:END:\n** H2\n:PROPERTIES:\n:A: bar\n:END:"
  4253. (org-match-sparse-tree nil "A={f.*}")
  4254. (search-forward "H2")
  4255. (org-invisible-p2)))
  4256. ;; With an optional argument, limit match to TODO entries.
  4257. (should-not
  4258. (org-test-with-temp-text "* H\n** TODO H1 :tag:\n** H2 :tag:"
  4259. (org-match-sparse-tree t "tag")
  4260. (search-forward "H1")
  4261. (org-invisible-p2)))
  4262. (should
  4263. (org-test-with-temp-text "* H\n** TODO H1 :tag:\n** H2 :tag:"
  4264. (org-match-sparse-tree t "tag")
  4265. (search-forward "H2")
  4266. (org-invisible-p2))))
  4267. (ert-deftest test-org/occur ()
  4268. "Test `org-occur' specifications."
  4269. ;; Count number of matches.
  4270. (should
  4271. (= 1
  4272. (org-test-with-temp-text "* H\nA\n* H2"
  4273. (org-occur "A"))))
  4274. (should
  4275. (= 2
  4276. (org-test-with-temp-text "* H\nA\n* H2\nA"
  4277. (org-occur "A"))))
  4278. ;; Test CALLBACK optional argument.
  4279. (should
  4280. (= 0
  4281. (org-test-with-temp-text "* H\nA\n* H2"
  4282. (org-occur "A" nil (lambda () (equal (org-get-heading) "H2"))))))
  4283. (should
  4284. (= 1
  4285. (org-test-with-temp-text "* H\nA\n* H2\nA"
  4286. (org-occur "A" nil (lambda () (equal (org-get-heading) "H2"))))))
  4287. ;; Case-fold searches according to `org-occur-case-fold-search'.
  4288. (should
  4289. (= 2
  4290. (org-test-with-temp-text "Aa"
  4291. (let ((org-occur-case-fold-search t)) (org-occur "A")))))
  4292. (should
  4293. (= 2
  4294. (org-test-with-temp-text "Aa"
  4295. (let ((org-occur-case-fold-search t)) (org-occur "a")))))
  4296. (should
  4297. (= 1
  4298. (org-test-with-temp-text "Aa"
  4299. (let ((org-occur-case-fold-search nil)) (org-occur "A")))))
  4300. (should
  4301. (= 1
  4302. (org-test-with-temp-text "Aa"
  4303. (let ((org-occur-case-fold-search nil)) (org-occur "a")))))
  4304. (should
  4305. (= 1
  4306. (org-test-with-temp-text "Aa"
  4307. (let ((org-occur-case-fold-search 'smart)) (org-occur "A")))))
  4308. (should
  4309. (= 2
  4310. (org-test-with-temp-text "Aa"
  4311. (let ((org-occur-case-fold-search 'smart)) (org-occur "a"))))))
  4312. ;;; Tags
  4313. (ert-deftest test-org/tag-string-to-alist ()
  4314. "Test `org-tag-string-to-alist' specifications."
  4315. ;; Tag without selection key.
  4316. (should (equal (org-tag-string-to-alist "tag1") '(("tag1"))))
  4317. ;; Tag with selection key.
  4318. (should (equal (org-tag-string-to-alist "tag1(t)") '(("tag1" . ?t))))
  4319. ;; Tag group.
  4320. (should
  4321. (equal
  4322. (org-tag-string-to-alist "[ group : t1 t2 ]")
  4323. '((:startgrouptag) ("group") (:grouptags) ("t1") ("t2") (:endgrouptag))))
  4324. ;; Mutually exclusive tags.
  4325. (should (equal (org-tag-string-to-alist "{ tag1 tag2 }")
  4326. '((:startgroup) ("tag1") ("tag2") (:endgroup))))
  4327. (should
  4328. (equal
  4329. (org-tag-string-to-alist "{ group : tag1 tag2 }")
  4330. '((:startgroup) ("group") (:grouptags) ("tag1") ("tag2") (:endgroup)))))
  4331. (ert-deftest test-org/tag-alist-to-string ()
  4332. "Test `org-tag-alist-to-string' specifications."
  4333. (should (equal (org-tag-alist-to-string '(("tag1"))) "tag1"))
  4334. (should (equal (org-tag-alist-to-string '(("tag1" . ?t))) "tag1(t)"))
  4335. (should
  4336. (equal
  4337. (org-tag-alist-to-string
  4338. '((:startgrouptag) ("group") (:grouptags) ("t1") ("t2") (:endgrouptag)))
  4339. "[ group : t1 t2 ]"))
  4340. (should
  4341. (equal (org-tag-alist-to-string
  4342. '((:startgroup) ("tag1") ("tag2") (:endgroup)))
  4343. "{ tag1 tag2 }"))
  4344. (should
  4345. (equal
  4346. (org-tag-alist-to-string
  4347. '((:startgroup) ("group") (:grouptags) ("tag1") ("tag2") (:endgroup)))
  4348. "{ group : tag1 tag2 }")))
  4349. (ert-deftest test-org/tag-alist-to-groups ()
  4350. "Test `org-tag-alist-to-groups' specifications."
  4351. (should
  4352. (equal (org-tag-alist-to-groups
  4353. '((:startgroup) ("group") (:grouptags) ("t1") ("t2") (:endgroup)))
  4354. '(("group" "t1" "t2"))))
  4355. (should
  4356. (equal
  4357. (org-tag-alist-to-groups
  4358. '((:startgrouptag) ("group") (:grouptags) ("t1") ("t2") (:endgrouptag)))
  4359. '(("group" "t1" "t2"))))
  4360. (should-not
  4361. (org-tag-alist-to-groups
  4362. '((:startgroup) ("group") ("t1") ("t2") (:endgroup)))))
  4363. (ert-deftest test-org/tag-align ()
  4364. "Test `org-align-tags-here' specifications"
  4365. ;; Test aligning tags with different display width.
  4366. (should
  4367. ;; 12345678901234567890
  4368. (equal "* Test :abc:"
  4369. (org-test-with-temp-text "* Test :abc:"
  4370. (let ((org-tags-column -20)
  4371. (indent-tabs-mode nil))
  4372. (org-fix-tags-on-the-fly))
  4373. (buffer-string))))
  4374. (should
  4375. ;; 12345678901234567890
  4376. (equal "* Test :日本語:"
  4377. (org-test-with-temp-text "* Test :日本語:"
  4378. (let ((org-tags-column -20)
  4379. (indent-tabs-mode nil))
  4380. (org-fix-tags-on-the-fly))
  4381. (buffer-string))))
  4382. ;; Make sure aligning tags do not skip invisible text.
  4383. (should
  4384. (equal "* [[linkx]] :tag:"
  4385. (org-test-with-temp-text "* [[link<point>]] :tag:"
  4386. (let ((org-tags-column 0))
  4387. (org-fix-tags-on-the-fly)
  4388. (insert "x")
  4389. (buffer-string))))))
  4390. (ert-deftest test-org/tags-at ()
  4391. (should
  4392. (equal '("foo" "bar")
  4393. (org-test-with-temp-text
  4394. "* T<point>est :foo:bar:"
  4395. (org-get-tags-at)))))
  4396. ;;; Timestamps API
  4397. (ert-deftest test-org/time-stamp ()
  4398. "Test `org-time-stamp' specifications."
  4399. ;; Insert chosen time stamp at point.
  4400. (should
  4401. (string-match
  4402. "Te<2014-03-04 .*?>xt"
  4403. (org-test-with-temp-text "Te<point>xt"
  4404. (cl-letf (((symbol-function 'org-read-date)
  4405. (lambda (&rest args)
  4406. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  4407. (org-time-stamp nil)
  4408. (buffer-string)))))
  4409. ;; With a prefix argument, also insert time.
  4410. (should
  4411. (string-match
  4412. "Te<2014-03-04 .*? 00:41>xt"
  4413. (org-test-with-temp-text "Te<point>xt"
  4414. (cl-letf (((symbol-function 'org-read-date)
  4415. (lambda (&rest args)
  4416. (apply #'encode-time
  4417. (org-parse-time-string "2014-03-04 00:41")))))
  4418. (org-time-stamp '(4))
  4419. (buffer-string)))))
  4420. ;; With two universal prefix arguments, insert an active timestamp
  4421. ;; with the current time without prompting the user.
  4422. (should
  4423. (string-match
  4424. "Te<2014-03-04 .*? 00:41>xt"
  4425. (org-test-with-temp-text "Te<point>xt"
  4426. (cl-letf (((symbol-function 'current-time)
  4427. (lambda ()
  4428. (apply #'encode-time
  4429. (org-parse-time-string "2014-03-04 00:41")))))
  4430. (org-time-stamp '(16))
  4431. (buffer-string)))))
  4432. ;; When optional argument is non-nil, insert an inactive timestamp.
  4433. (should
  4434. (string-match
  4435. "Te\\[2014-03-04 .*?\\]xt"
  4436. (org-test-with-temp-text "Te<point>xt"
  4437. (cl-letf (((symbol-function 'org-read-date)
  4438. (lambda (&rest args)
  4439. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  4440. (org-time-stamp nil t)
  4441. (buffer-string)))))
  4442. ;; When called from a timestamp, replace existing one.
  4443. (should
  4444. (string-match
  4445. "<2014-03-04 .*?>"
  4446. (org-test-with-temp-text "<2012-03-29<point> thu.>"
  4447. (cl-letf (((symbol-function 'org-read-date)
  4448. (lambda (&rest args)
  4449. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  4450. (org-time-stamp nil)
  4451. (buffer-string)))))
  4452. (should
  4453. (string-match
  4454. "<2014-03-04 .*?>--<2014-03-04 .*?>"
  4455. (org-test-with-temp-text "<2012-03-29<point> thu.>--<2014-03-04 tue.>"
  4456. (cl-letf (((symbol-function 'org-read-date)
  4457. (lambda (&rest args)
  4458. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  4459. (org-time-stamp nil)
  4460. (buffer-string)))))
  4461. ;; When replacing a timestamp, preserve repeater, if any.
  4462. (should
  4463. (string-match
  4464. "<2014-03-04 .*? \\+2y>"
  4465. (org-test-with-temp-text "<2012-03-29<point> thu. +2y>"
  4466. (cl-letf (((symbol-function 'org-read-date)
  4467. (lambda (&rest args)
  4468. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  4469. (org-time-stamp nil)
  4470. (buffer-string)))))
  4471. ;; When called twice in a raw, build a date range.
  4472. (should
  4473. (string-match
  4474. "<2012-03-29 .*?>--<2014-03-04 .*?>"
  4475. (org-test-with-temp-text "<2012-03-29 thu.><point>"
  4476. (cl-letf (((symbol-function 'org-read-date)
  4477. (lambda (&rest args)
  4478. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  4479. (let ((last-command 'org-time-stamp)
  4480. (this-command 'org-time-stamp))
  4481. (org-time-stamp nil))
  4482. (buffer-string))))))
  4483. (ert-deftest test-org/timestamp-has-time-p ()
  4484. "Test `org-timestamp-has-time-p' specifications."
  4485. ;; With time.
  4486. (should
  4487. (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
  4488. (org-timestamp-has-time-p (org-element-context))))
  4489. ;; Without time.
  4490. (should-not
  4491. (org-test-with-temp-text "<2012-03-29 Thu>"
  4492. (org-timestamp-has-time-p (org-element-context)))))
  4493. (ert-deftest test-org/timestamp-format ()
  4494. "Test `org-timestamp-format' specifications."
  4495. ;; Regular test.
  4496. (should
  4497. (equal
  4498. "2012-03-29 16:40"
  4499. (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
  4500. (org-timestamp-format (org-element-context) "%Y-%m-%d %R"))))
  4501. ;; Range end.
  4502. (should
  4503. (equal
  4504. "2012-03-29"
  4505. (org-test-with-temp-text "[2011-07-14 Thu]--[2012-03-29 Thu]"
  4506. (org-timestamp-format (org-element-context) "%Y-%m-%d" t)))))
  4507. (ert-deftest test-org/timestamp-split-range ()
  4508. "Test `org-timestamp-split-range' specifications."
  4509. ;; Extract range start (active).
  4510. (should
  4511. (equal '(2012 3 29)
  4512. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  4513. (let ((ts (org-timestamp-split-range (org-element-context))))
  4514. (mapcar (lambda (p) (org-element-property p ts))
  4515. '(:year-end :month-end :day-end))))))
  4516. ;; Extract range start (inactive)
  4517. (should
  4518. (equal '(2012 3 29)
  4519. (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
  4520. (let ((ts (org-timestamp-split-range (org-element-context))))
  4521. (mapcar (lambda (p) (org-element-property p ts))
  4522. '(:year-end :month-end :day-end))))))
  4523. ;; Extract range end (active).
  4524. (should
  4525. (equal '(2012 3 30)
  4526. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  4527. (let ((ts (org-timestamp-split-range
  4528. (org-element-context) t)))
  4529. (mapcar (lambda (p) (org-element-property p ts))
  4530. '(:year-end :month-end :day-end))))))
  4531. ;; Extract range end (inactive)
  4532. (should
  4533. (equal '(2012 3 30)
  4534. (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
  4535. (let ((ts (org-timestamp-split-range
  4536. (org-element-context) t)))
  4537. (mapcar (lambda (p) (org-element-property p ts))
  4538. '(:year-end :month-end :day-end))))))
  4539. ;; Return the timestamp if not a range.
  4540. (should
  4541. (org-test-with-temp-text "[2012-03-29 Thu]"
  4542. (let* ((ts-orig (org-element-context))
  4543. (ts-copy (org-timestamp-split-range ts-orig)))
  4544. (eq ts-orig ts-copy))))
  4545. (should
  4546. (org-test-with-temp-text "<%%(org-float t 4 2)>"
  4547. (let* ((ts-orig (org-element-context))
  4548. (ts-copy (org-timestamp-split-range ts-orig)))
  4549. (eq ts-orig ts-copy)))))
  4550. (ert-deftest test-org/timestamp-translate ()
  4551. "Test `org-timestamp-translate' specifications."
  4552. ;; Translate whole date range.
  4553. (should
  4554. (equal "<29>--<30>"
  4555. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  4556. (let ((org-display-custom-times t)
  4557. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  4558. (org-timestamp-translate (org-element-context))))))
  4559. ;; Translate date range start.
  4560. (should
  4561. (equal "<29>"
  4562. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  4563. (let ((org-display-custom-times t)
  4564. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  4565. (org-timestamp-translate (org-element-context) 'start)))))
  4566. ;; Translate date range end.
  4567. (should
  4568. (equal "<30>"
  4569. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  4570. (let ((org-display-custom-times t)
  4571. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  4572. (org-timestamp-translate (org-element-context) 'end)))))
  4573. ;; Translate time range.
  4574. (should
  4575. (equal "<08>--<16>"
  4576. (org-test-with-temp-text "<2012-03-29 Thu 8:30-16:40>"
  4577. (let ((org-display-custom-times t)
  4578. (org-time-stamp-custom-formats '("<%d>" . "<%H>")))
  4579. (org-timestamp-translate (org-element-context))))))
  4580. ;; Translate non-range timestamp.
  4581. (should
  4582. (equal "<29>"
  4583. (org-test-with-temp-text "<2012-03-29 Thu>"
  4584. (let ((org-display-custom-times t)
  4585. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  4586. (org-timestamp-translate (org-element-context))))))
  4587. ;; Do not change `diary' timestamps.
  4588. (should
  4589. (equal "<%%(org-float t 4 2)>"
  4590. (org-test-with-temp-text "<%%(org-float t 4 2)>"
  4591. (let ((org-display-custom-times t)
  4592. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  4593. (org-timestamp-translate (org-element-context)))))))
  4594. ;;; Visibility
  4595. (ert-deftest test-org/flag-drawer ()
  4596. "Test `org-flag-drawer' specifications."
  4597. ;; Hide drawer.
  4598. (should
  4599. (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
  4600. (org-flag-drawer t)
  4601. (get-char-property (line-end-position) 'invisible)))
  4602. ;; Show drawer.
  4603. (should-not
  4604. (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
  4605. (org-flag-drawer t)
  4606. (org-flag-drawer nil)
  4607. (get-char-property (line-end-position) 'invisible)))
  4608. ;; Test optional argument.
  4609. (should
  4610. (org-test-with-temp-text "Text\n:D1:\nc1\n:END:\n\n:D2:\nc2\n:END:"
  4611. (let ((drawer (save-excursion (search-forward ":D2")
  4612. (org-element-at-point))))
  4613. (org-flag-drawer t drawer)
  4614. (get-char-property (progn (search-forward ":D2") (line-end-position))
  4615. 'invisible))))
  4616. (should-not
  4617. (org-test-with-temp-text ":D1:\nc1\n:END:\n\n:D2:\nc2\n:END:"
  4618. (let ((drawer (save-excursion (search-forward ":D2")
  4619. (org-element-at-point))))
  4620. (org-flag-drawer t drawer)
  4621. (get-char-property (line-end-position) 'invisible))))
  4622. ;; Do not hide fake drawers.
  4623. (should-not
  4624. (org-test-with-temp-text "#+begin_example\n:D:\nc\n:END:\n#+end_example"
  4625. (forward-line 1)
  4626. (org-flag-drawer t)
  4627. (get-char-property (line-end-position) 'invisible)))
  4628. ;; Do not hide incomplete drawers.
  4629. (should-not
  4630. (org-test-with-temp-text ":D:\nparagraph"
  4631. (forward-line 1)
  4632. (org-flag-drawer t)
  4633. (get-char-property (line-end-position) 'invisible)))
  4634. ;; Do not hide drawers when called from final blank lines.
  4635. (should-not
  4636. (org-test-with-temp-text ":DRAWER:\nA\n:END:\n\n"
  4637. (goto-char (point-max))
  4638. (org-flag-drawer t)
  4639. (goto-char (point-min))
  4640. (get-char-property (line-end-position) 'invisible)))
  4641. ;; Don't leave point in an invisible part of the buffer when hiding
  4642. ;; a drawer away.
  4643. (should-not
  4644. (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
  4645. (goto-char (point-max))
  4646. (org-flag-drawer t)
  4647. (get-char-property (point) 'invisible))))
  4648. (ert-deftest test-org/hide-block-toggle ()
  4649. "Test `org-hide-block-toggle' specifications."
  4650. ;; Error when not at a block.
  4651. (should-error
  4652. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents"
  4653. (org-hide-block-toggle 'off)
  4654. (get-char-property (line-end-position) 'invisible)))
  4655. ;; Hide block.
  4656. (should
  4657. (org-test-with-temp-text "#+BEGIN_CENTER\ncontents\n#+END_CENTER"
  4658. (org-hide-block-toggle)
  4659. (get-char-property (line-end-position) 'invisible)))
  4660. (should
  4661. (org-test-with-temp-text "#+BEGIN_EXAMPLE\ncontents\n#+END_EXAMPLE"
  4662. (org-hide-block-toggle)
  4663. (get-char-property (line-end-position) 'invisible)))
  4664. ;; Show block unconditionally when optional argument is `off'.
  4665. (should-not
  4666. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  4667. (org-hide-block-toggle)
  4668. (org-hide-block-toggle 'off)
  4669. (get-char-property (line-end-position) 'invisible)))
  4670. (should-not
  4671. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  4672. (org-hide-block-toggle 'off)
  4673. (get-char-property (line-end-position) 'invisible)))
  4674. ;; Hide block unconditionally when optional argument is non-nil.
  4675. (should
  4676. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  4677. (org-hide-block-toggle t)
  4678. (get-char-property (line-end-position) 'invisible)))
  4679. (should
  4680. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  4681. (org-hide-block-toggle)
  4682. (org-hide-block-toggle t)
  4683. (get-char-property (line-end-position) 'invisible)))
  4684. ;; Do not hide block when called from final blank lines.
  4685. (should-not
  4686. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE\n\n<point>"
  4687. (org-hide-block-toggle)
  4688. (goto-char (point-min))
  4689. (get-char-property (line-end-position) 'invisible)))
  4690. ;; Don't leave point in an invisible part of the buffer when hiding
  4691. ;; a block away.
  4692. (should-not
  4693. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n<point>#+END_QUOTE"
  4694. (org-hide-block-toggle)
  4695. (get-char-property (point) 'invisible))))
  4696. (ert-deftest test-org/hide-block-toggle-maybe ()
  4697. "Test `org-hide-block-toggle-maybe' specifications."
  4698. (should
  4699. (org-test-with-temp-text "#+BEGIN: dynamic\nContents\n#+END:"
  4700. (org-hide-block-toggle-maybe)))
  4701. (should-not
  4702. (org-test-with-temp-text "Paragraph" (org-hide-block-toggle-maybe))))
  4703. (ert-deftest test-org/show-set-visibility ()
  4704. "Test `org-show-set-visibility' specifications."
  4705. ;; Do not throw an error before first heading.
  4706. (should
  4707. (org-test-with-temp-text "Preamble\n* Headline"
  4708. (org-show-set-visibility 'tree)
  4709. t))
  4710. ;; Test all visibility spans, both on headline and in entry.
  4711. (let ((list-visible-lines
  4712. (lambda (state headerp)
  4713. (org-test-with-temp-text "* Grandmother (0)
  4714. ** Uncle (1)
  4715. *** Heir (2)
  4716. ** Father (3)
  4717. Ancestor text (4)
  4718. *** Sister (5)
  4719. Sibling text (6)
  4720. *** Self (7)
  4721. Match (8)
  4722. **** First born (9)
  4723. Child text (10)
  4724. **** The other child (11)
  4725. *** Brother (12)
  4726. ** Aunt (13)
  4727. "
  4728. (org-cycle t)
  4729. (search-forward (if headerp "Self" "Match"))
  4730. (org-show-set-visibility state)
  4731. (goto-char (point-min))
  4732. (let (result (line 0))
  4733. (while (not (eobp))
  4734. (unless (org-invisible-p2) (push line result))
  4735. (incf line)
  4736. (forward-line))
  4737. (nreverse result))))))
  4738. (should (equal '(0 7) (funcall list-visible-lines 'minimal t)))
  4739. (should (equal '(0 7 8) (funcall list-visible-lines 'minimal nil)))
  4740. (should (equal '(0 7 8 9) (funcall list-visible-lines 'local t)))
  4741. (should (equal '(0 7 8 9) (funcall list-visible-lines 'local nil)))
  4742. (should (equal '(0 3 7) (funcall list-visible-lines 'ancestors t)))
  4743. (should (equal '(0 3 7 8) (funcall list-visible-lines 'ancestors nil)))
  4744. (should (equal '(0 3 5 7 12) (funcall list-visible-lines 'lineage t)))
  4745. (should (equal '(0 3 5 7 8 9 12) (funcall list-visible-lines 'lineage nil)))
  4746. (should (equal '(0 1 3 5 7 12 13) (funcall list-visible-lines 'tree t)))
  4747. (should (equal '(0 1 3 5 7 8 9 11 12 13)
  4748. (funcall list-visible-lines 'tree nil)))
  4749. (should (equal '(0 1 3 4 5 7 12 13)
  4750. (funcall list-visible-lines 'canonical t)))
  4751. (should (equal '(0 1 3 4 5 7 8 9 11 12 13)
  4752. (funcall list-visible-lines 'canonical nil))))
  4753. ;; When point is hidden in a drawer or a block, make sure to make it
  4754. ;; visible.
  4755. (should-not
  4756. (org-test-with-temp-text "#+BEGIN_QUOTE\nText\n#+END_QUOTE"
  4757. (org-hide-block-toggle)
  4758. (search-forward "Text")
  4759. (org-show-set-visibility 'minimal)
  4760. (org-invisible-p2)))
  4761. (should-not
  4762. (org-test-with-temp-text ":DRAWER:\nText\n:END:"
  4763. (org-flag-drawer t)
  4764. (search-forward "Text")
  4765. (org-show-set-visibility 'minimal)
  4766. (org-invisible-p2)))
  4767. (should-not
  4768. (org-test-with-temp-text
  4769. "#+BEGIN_QUOTE\n<point>:DRAWER:\nText\n:END:\n#+END_QUOTE"
  4770. (org-flag-drawer t)
  4771. (forward-line -1)
  4772. (org-hide-block-toggle)
  4773. (search-forward "Text")
  4774. (org-show-set-visibility 'minimal)
  4775. (org-invisible-p2))))
  4776. (provide 'test-org)
  4777. ;;; test-org.el ends here