test-org.el 173 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312
  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. (should ;TODO are case-sensitive
  1060. (equal "* \nTodo"
  1061. (org-test-with-temp-text "* <point>Todo"
  1062. (org-return)
  1063. (buffer-string))))
  1064. ;; At headline text, break headline text but preserve tags.
  1065. (should
  1066. (equal "* TODO [#B] foo :tag:\nbar"
  1067. (let (org-auto-align-tags)
  1068. (org-test-with-temp-text "* TODO [#B] foo<point>bar :tag:"
  1069. (org-return)
  1070. (buffer-string)))))
  1071. ;; At bol of headline insert newline.
  1072. (should
  1073. (equal "\n* h"
  1074. (org-test-with-temp-text "<point>* h"
  1075. (org-return)
  1076. (buffer-string))))
  1077. ;; Refuse to leave invalid headline in buffer.
  1078. (should
  1079. (equal "* h\n"
  1080. (org-test-with-temp-text "*<point> h"
  1081. (org-return)
  1082. (buffer-string)))))
  1083. (ert-deftest test-org/meta-return ()
  1084. "Test M-RET (`org-meta-return') specifications."
  1085. ;; In a table field insert a row above.
  1086. (should
  1087. (org-test-with-temp-text "| a |"
  1088. (forward-char)
  1089. (org-meta-return)
  1090. (forward-line -1)
  1091. (looking-at "| |$")))
  1092. ;; In a paragraph change current line into a header.
  1093. (should
  1094. (org-test-with-temp-text "a"
  1095. (org-meta-return)
  1096. (beginning-of-line)
  1097. (looking-at "\* a$")))
  1098. ;; In an item insert an item, in this case above.
  1099. (should
  1100. (org-test-with-temp-text "- a"
  1101. (org-meta-return)
  1102. (beginning-of-line)
  1103. (looking-at "- $")))
  1104. ;; In a drawer and item insert an item, in this case above.
  1105. (should
  1106. (org-test-with-temp-text ":MYDRAWER:\n- a\n:END:"
  1107. (forward-line)
  1108. (org-meta-return)
  1109. (beginning-of-line)
  1110. (looking-at "- $"))))
  1111. (ert-deftest test-org/insert-heading ()
  1112. "Test `org-insert-heading' specifications."
  1113. ;; In an empty buffer, insert a new headline.
  1114. (should
  1115. (equal "* "
  1116. (org-test-with-temp-text ""
  1117. (org-insert-heading)
  1118. (buffer-string))))
  1119. ;; At the beginning of a line, turn it into a headline.
  1120. (should
  1121. (equal "* P"
  1122. (org-test-with-temp-text "<point>P"
  1123. (org-insert-heading)
  1124. (buffer-string))))
  1125. ;; In the middle of a line, split the line if allowed, otherwise,
  1126. ;; insert the headline at its end.
  1127. (should
  1128. (equal "Para\n* graph"
  1129. (org-test-with-temp-text "Para<point>graph"
  1130. (let ((org-M-RET-may-split-line '((default . t))))
  1131. (org-insert-heading))
  1132. (buffer-string))))
  1133. (should
  1134. (equal "Paragraph\n* "
  1135. (org-test-with-temp-text "Para<point>graph"
  1136. (let ((org-M-RET-may-split-line '((default . nil))))
  1137. (org-insert-heading))
  1138. (buffer-string))))
  1139. ;; At the beginning of a headline, create one above.
  1140. (should
  1141. (equal "* \n* H"
  1142. (org-test-with-temp-text "* H"
  1143. (org-insert-heading)
  1144. (buffer-string))))
  1145. ;; In the middle of a headline, split it if allowed.
  1146. (should
  1147. (equal "* H\n* 1\n"
  1148. (org-test-with-temp-text "* H<point>1"
  1149. (let ((org-M-RET-may-split-line '((headline . t))))
  1150. (org-insert-heading))
  1151. (buffer-string))))
  1152. (should
  1153. (equal "* H1\n* \n"
  1154. (org-test-with-temp-text "* H<point>1"
  1155. (let ((org-M-RET-may-split-line '((headline . nil))))
  1156. (org-insert-heading))
  1157. (buffer-string))))
  1158. ;; However, splitting cannot happen on TODO keywords, priorities or
  1159. ;; tags.
  1160. (should
  1161. (equal "* TODO H1\n* \n"
  1162. (org-test-with-temp-text "* TO<point>DO H1"
  1163. (let ((org-M-RET-may-split-line '((headline . t))))
  1164. (org-insert-heading))
  1165. (buffer-string))))
  1166. (should
  1167. (equal "* [#A] H1\n* \n"
  1168. (org-test-with-temp-text "* [#<point>A] H1"
  1169. (let ((org-M-RET-may-split-line '((headline . t))))
  1170. (org-insert-heading))
  1171. (buffer-string))))
  1172. (should
  1173. (equal "* H1 :tag:\n* \n"
  1174. (org-test-with-temp-text "* H1 :ta<point>g:"
  1175. (let ((org-M-RET-may-split-line '((headline . t))))
  1176. (org-insert-heading))
  1177. (buffer-string))))
  1178. ;; New headline level depends on the level of the headline above.
  1179. (should
  1180. (equal "** H\n** P"
  1181. (org-test-with-temp-text "** H\n<point>P"
  1182. (org-insert-heading)
  1183. (buffer-string))))
  1184. (should
  1185. (equal "** H\nPara\n** graph"
  1186. (org-test-with-temp-text "** H\nPara<point>graph"
  1187. (let ((org-M-RET-may-split-line '((default . t))))
  1188. (org-insert-heading))
  1189. (buffer-string))))
  1190. (should
  1191. (equal "** \n** H"
  1192. (org-test-with-temp-text "** H"
  1193. (org-insert-heading)
  1194. (buffer-string))))
  1195. ;; When called with one universal argument, insert a new headline at
  1196. ;; the end of the current subtree, independently on the position of
  1197. ;; point.
  1198. (should
  1199. (equal
  1200. "* H1\n** H2\n* \n"
  1201. (org-test-with-temp-text "* H1\n** H2"
  1202. (let ((org-insert-heading-respect-content nil))
  1203. (org-insert-heading '(4)))
  1204. (buffer-string))))
  1205. (should
  1206. (equal
  1207. "* H1\n** H2\n* \n"
  1208. (org-test-with-temp-text "* H<point>1\n** H2"
  1209. (let ((org-insert-heading-respect-content nil))
  1210. (org-insert-heading '(4)))
  1211. (buffer-string))))
  1212. ;; When called with two universal arguments, insert a new headline
  1213. ;; at the end of the grandparent subtree.
  1214. (should
  1215. (equal "* H1\n** H3\n- item\n** H2\n** \n"
  1216. (org-test-with-temp-text "* H1\n** H3\n- item<point>\n** H2"
  1217. (let ((org-insert-heading-respect-content nil))
  1218. (org-insert-heading '(16)))
  1219. (buffer-string))))
  1220. ;; When optional TOP-LEVEL argument is non-nil, always insert
  1221. ;; a level 1 heading.
  1222. (should
  1223. (equal "* H1\n** H2\n* \n"
  1224. (org-test-with-temp-text "* H1\n** H2<point>"
  1225. (org-insert-heading nil nil t)
  1226. (buffer-string))))
  1227. (should
  1228. (equal "* H1\n- item\n* "
  1229. (org-test-with-temp-text "* H1\n- item<point>"
  1230. (org-insert-heading nil nil t)
  1231. (buffer-string))))
  1232. ;; Obey `org-blank-before-new-entry'.
  1233. (should
  1234. (equal "* H1\n\n* \n"
  1235. (org-test-with-temp-text "* H1<point>"
  1236. (let ((org-blank-before-new-entry '((heading . t))))
  1237. (org-insert-heading))
  1238. (buffer-string))))
  1239. (should
  1240. (equal "* H1\n* \n"
  1241. (org-test-with-temp-text "* H1<point>"
  1242. (let ((org-blank-before-new-entry '((heading . nil))))
  1243. (org-insert-heading))
  1244. (buffer-string))))
  1245. (should
  1246. (equal "* H1\n* H2\n* \n"
  1247. (org-test-with-temp-text "* H1\n* H2<point>"
  1248. (let ((org-blank-before-new-entry '((heading . auto))))
  1249. (org-insert-heading))
  1250. (buffer-string))))
  1251. (should
  1252. (equal "* H1\n\n* H2\n\n* \n"
  1253. (org-test-with-temp-text "* H1\n\n* H2<point>"
  1254. (let ((org-blank-before-new-entry '((heading . auto))))
  1255. (org-insert-heading))
  1256. (buffer-string))))
  1257. ;; Corner case: correctly insert a headline after an empty one.
  1258. (should
  1259. (equal "* \n* \n"
  1260. (org-test-with-temp-text "* <point>"
  1261. (org-insert-heading)
  1262. (buffer-string)))))
  1263. (ert-deftest test-org/insert-todo-heading-respect-content ()
  1264. "Test `org-insert-todo-heading-respect-content' specifications."
  1265. ;; Create a TODO heading.
  1266. (should
  1267. (org-test-with-temp-text "* H1\n Body"
  1268. (org-insert-todo-heading-respect-content)
  1269. (nth 2 (org-heading-components))))
  1270. ;; Add headline at the end of the first subtree
  1271. (should
  1272. (equal
  1273. "* TODO \n"
  1274. (org-test-with-temp-text "* H1\nH1Body<point>\n** H2\nH2Body"
  1275. (org-insert-todo-heading-respect-content)
  1276. (buffer-substring-no-properties (line-beginning-position) (point-max)))))
  1277. ;; In a list, do not create a new item.
  1278. (should
  1279. (equal
  1280. "* TODO \n"
  1281. (org-test-with-temp-text "* H\n- an item\n- another one"
  1282. (search-forward "an ")
  1283. (org-insert-todo-heading-respect-content)
  1284. (buffer-substring-no-properties (line-beginning-position) (point-max))))))
  1285. (ert-deftest test-org/clone-with-time-shift ()
  1286. "Test `org-clone-subtree-with-time-shift'."
  1287. ;; Raise an error before first heading.
  1288. (should-error
  1289. (org-test-with-temp-text ""
  1290. (org-clone-subtree-with-time-shift 1)))
  1291. ;; Raise an error on invalid number of clones.
  1292. (should-error
  1293. (org-test-with-temp-text "* Clone me"
  1294. (org-clone-subtree-with-time-shift -1)))
  1295. ;; Clone non-repeating once.
  1296. (should
  1297. (equal "\
  1298. * H1\n<2015-06-21>
  1299. * H1\n<2015-06-23>
  1300. "
  1301. (org-test-with-temp-text "* H1\n<2015-06-21 Sun>"
  1302. (org-clone-subtree-with-time-shift 1 "+2d")
  1303. (replace-regexp-in-string
  1304. "\\( [.A-Za-z]+\\)\\( \\+[0-9][hdmwy]\\)?>" "" (buffer-string)
  1305. nil nil 1))))
  1306. ;; Clone repeating once.
  1307. (should
  1308. (equal "\
  1309. * H1\n<2015-06-21>
  1310. * H1\n<2015-06-23>
  1311. * H1\n<2015-06-25 +1w>
  1312. "
  1313. (org-test-with-temp-text "* H1\n<2015-06-21 Sun +1w>"
  1314. (org-clone-subtree-with-time-shift 1 "+2d")
  1315. (replace-regexp-in-string
  1316. "\\( [.A-Za-z]+\\)\\( \\+[0-9][hdmwy]\\)?>" "" (buffer-string)
  1317. nil nil 1))))
  1318. ;; Clone non-repeating zero times.
  1319. (should
  1320. (equal "\
  1321. * H1\n<2015-06-21>
  1322. "
  1323. (org-test-with-temp-text "* H1\n<2015-06-21 Sun>"
  1324. (org-clone-subtree-with-time-shift 0 "+2d")
  1325. (replace-regexp-in-string
  1326. "\\( [.A-Za-z]+\\)\\( \\+[0-9][hdmwy]\\)?>" "" (buffer-string)
  1327. nil nil 1))))
  1328. ;; Clone repeating "zero" times.
  1329. (should
  1330. (equal "\
  1331. * H1\n<2015-06-21>
  1332. * H1\n<2015-06-23 +1w>
  1333. "
  1334. (org-test-with-temp-text "* H1\n<2015-06-21 Sun +1w>"
  1335. (org-clone-subtree-with-time-shift 0 "+2d")
  1336. (replace-regexp-in-string
  1337. "\\( [.A-Za-z]+\\)\\( \\+[0-9][hdmwy]\\)?>" "" (buffer-string)
  1338. nil nil 1))))
  1339. ;; Find time stamps before point. If SHIFT is not specified, ask
  1340. ;; for a time shift.
  1341. (should
  1342. (string-prefix-p
  1343. "* H <2012-03-30"
  1344. (org-test-with-temp-text "* H <2012-03-29 Thu><point>"
  1345. (org-clone-subtree-with-time-shift 1 "+1d")
  1346. (buffer-substring-no-properties (line-beginning-position 2)
  1347. (line-end-position 2)))))
  1348. (should
  1349. (string-prefix-p
  1350. "* H <2014-03-05"
  1351. (org-test-with-temp-text "* H <2014-03-04 Tue><point>"
  1352. (cl-letf (((symbol-function 'read-from-minibuffer)
  1353. (lambda (&rest args) "+1d")))
  1354. (org-clone-subtree-with-time-shift 1 "+1d"))
  1355. (buffer-substring-no-properties (line-beginning-position 2)
  1356. (line-end-position 2))))))
  1357. ;;; Fixed-Width Areas
  1358. (ert-deftest test-org/toggle-fixed-width ()
  1359. "Test `org-toggle-fixed-width' specifications."
  1360. ;; No region: Toggle on fixed-width marker in paragraphs.
  1361. (should
  1362. (equal ": A"
  1363. (org-test-with-temp-text "A"
  1364. (org-toggle-fixed-width)
  1365. (buffer-string))))
  1366. ;; No region: Toggle off fixed-width markers in fixed-width areas.
  1367. (should
  1368. (equal "A"
  1369. (org-test-with-temp-text ": A"
  1370. (org-toggle-fixed-width)
  1371. (buffer-string))))
  1372. ;; No region: Toggle on marker in blank lines after elements or just
  1373. ;; after a headline.
  1374. (should
  1375. (equal "* H\n: "
  1376. (org-test-with-temp-text "* H\n"
  1377. (forward-line)
  1378. (org-toggle-fixed-width)
  1379. (buffer-string))))
  1380. (should
  1381. (equal "#+BEGIN_EXAMPLE\nContents\n#+END_EXAMPLE\n: "
  1382. (org-test-with-temp-text "#+BEGIN_EXAMPLE\nContents\n#+END_EXAMPLE\n"
  1383. (goto-char (point-max))
  1384. (org-toggle-fixed-width)
  1385. (buffer-string))))
  1386. ;; No region: Toggle on marker in front of one line elements (e.g.,
  1387. ;; headlines, clocks)
  1388. (should
  1389. (equal ": * Headline"
  1390. (org-test-with-temp-text "* Headline"
  1391. (org-toggle-fixed-width)
  1392. (buffer-string))))
  1393. (should
  1394. (equal ": #+KEYWORD: value"
  1395. (org-test-with-temp-text "#+KEYWORD: value"
  1396. (org-toggle-fixed-width)
  1397. (buffer-string))))
  1398. ;; No region: error in other situations.
  1399. (should-error
  1400. (org-test-with-temp-text "#+BEGIN_EXAMPLE\n: A\n#+END_EXAMPLE"
  1401. (forward-line)
  1402. (org-toggle-fixed-width)
  1403. (buffer-string)))
  1404. ;; No region: Indentation is preserved.
  1405. (should
  1406. (equal "- A\n : B"
  1407. (org-test-with-temp-text "- A\n B"
  1408. (forward-line)
  1409. (org-toggle-fixed-width)
  1410. (buffer-string))))
  1411. ;; Region: If it contains only fixed-width elements and blank lines,
  1412. ;; toggle off fixed-width markup.
  1413. (should
  1414. (equal "A\n\nB"
  1415. (org-test-with-temp-text ": A\n\n: B"
  1416. (transient-mark-mode 1)
  1417. (push-mark (point) t t)
  1418. (goto-char (point-max))
  1419. (org-toggle-fixed-width)
  1420. (buffer-string))))
  1421. ;; Region: If it contains anything else, toggle on fixed-width but
  1422. ;; not on fixed-width areas.
  1423. (should
  1424. (equal ": A\n: \n: B\n: \n: C"
  1425. (org-test-with-temp-text "A\n\n: B\n\nC"
  1426. (transient-mark-mode 1)
  1427. (push-mark (point) t t)
  1428. (goto-char (point-max))
  1429. (org-toggle-fixed-width)
  1430. (buffer-string))))
  1431. ;; Region: Ignore blank lines at its end, unless it contains only
  1432. ;; such lines.
  1433. (should
  1434. (equal ": A\n\n"
  1435. (org-test-with-temp-text "A\n\n"
  1436. (transient-mark-mode 1)
  1437. (push-mark (point) t t)
  1438. (goto-char (point-max))
  1439. (org-toggle-fixed-width)
  1440. (buffer-string))))
  1441. (should
  1442. (equal ": \n: \n"
  1443. (org-test-with-temp-text "\n\n"
  1444. (transient-mark-mode 1)
  1445. (push-mark (point) t t)
  1446. (goto-char (point-max))
  1447. (org-toggle-fixed-width)
  1448. (buffer-string)))))
  1449. ;;; Headline
  1450. (ert-deftest test-org/get-heading ()
  1451. "Test `org-get-heading' specifications."
  1452. ;; Return current heading, even if point is not on it.
  1453. (should
  1454. (equal "H"
  1455. (org-test-with-temp-text "* H"
  1456. (org-get-heading))))
  1457. (should
  1458. (equal "H"
  1459. (org-test-with-temp-text "* H\nText<point>"
  1460. (org-get-heading))))
  1461. ;; Without any optional argument, return TODO keywords and tags.
  1462. (should
  1463. (equal "TODO H"
  1464. (org-test-with-temp-text "#+TODO: TODO | DONE\n* TODO H<point>"
  1465. (org-get-heading))))
  1466. (should
  1467. (equal "H :tag:"
  1468. (org-test-with-temp-text "* H :tag:"
  1469. (org-get-heading))))
  1470. ;; With NO-TAGS argument, ignore tags.
  1471. (should
  1472. (equal "TODO H"
  1473. (org-test-with-temp-text "#+TODO: TODO | DONE\n* TODO H<point>"
  1474. (org-get-heading t))))
  1475. (should
  1476. (equal "H"
  1477. (org-test-with-temp-text "* H :tag:"
  1478. (org-get-heading t))))
  1479. ;; With NO-TODO, ignore TODO keyword.
  1480. (should
  1481. (equal "H"
  1482. (org-test-with-temp-text "#+TODO: TODO | DONE\n* TODO H<point>"
  1483. (org-get-heading nil t))))
  1484. (should
  1485. (equal "H :tag:"
  1486. (org-test-with-temp-text "* H :tag:"
  1487. (org-get-heading nil t))))
  1488. ;; TODO keywords are case-sensitive.
  1489. (should
  1490. (equal "Todo H"
  1491. (org-test-with-temp-text "#+TODO: TODO | DONE\n* Todo H<point>"
  1492. (org-get-heading nil t))))
  1493. ;; On an empty headline, return value is consistent.
  1494. (should (equal "" (org-test-with-temp-text "* " (org-get-heading))))
  1495. (should (equal "" (org-test-with-temp-text "* " (org-get-heading t))))
  1496. (should (equal "" (org-test-with-temp-text "* " (org-get-heading nil t))))
  1497. (should (equal "" (org-test-with-temp-text "* " (org-get-heading t t)))))
  1498. (ert-deftest test-org/in-commented-heading-p ()
  1499. "Test `org-in-commented-heading-p' specifications."
  1500. ;; Commented headline.
  1501. (should
  1502. (org-test-with-temp-text "* COMMENT Headline\nBody"
  1503. (goto-char (point-max))
  1504. (org-in-commented-heading-p)))
  1505. ;; Commented ancestor.
  1506. (should
  1507. (org-test-with-temp-text "* COMMENT Headline\n** Level 2\nBody"
  1508. (goto-char (point-max))
  1509. (org-in-commented-heading-p)))
  1510. ;; Comment keyword is case-sensitive.
  1511. (should-not
  1512. (org-test-with-temp-text "* Comment Headline\nBody"
  1513. (goto-char (point-max))
  1514. (org-in-commented-heading-p)))
  1515. ;; Keyword is standalone.
  1516. (should-not
  1517. (org-test-with-temp-text "* COMMENTHeadline\nBody"
  1518. (goto-char (point-max))
  1519. (org-in-commented-heading-p)))
  1520. ;; Optional argument.
  1521. (should-not
  1522. (org-test-with-temp-text "* COMMENT Headline\n** Level 2\nBody"
  1523. (goto-char (point-max))
  1524. (org-in-commented-heading-p t))))
  1525. (ert-deftest test-org/entry-blocked-p ()
  1526. ;; Check other dependencies.
  1527. (should
  1528. (org-test-with-temp-text "* TODO Blocked\n** DONE one\n** TODO two"
  1529. (let ((org-enforce-todo-dependencies t)
  1530. (org-blocker-hook
  1531. '(org-block-todo-from-children-or-siblings-or-parent)))
  1532. (org-entry-blocked-p))))
  1533. (should-not
  1534. (org-test-with-temp-text "* TODO Blocked\n** DONE one\n** DONE two"
  1535. (let ((org-enforce-todo-dependencies t)
  1536. (org-blocker-hook
  1537. '(org-block-todo-from-children-or-siblings-or-parent)))
  1538. (org-entry-blocked-p))))
  1539. ;; Entry without a TODO keyword or with a DONE keyword cannot be
  1540. ;; blocked.
  1541. (should-not
  1542. (org-test-with-temp-text "* Blocked\n** TODO one"
  1543. (let ((org-enforce-todo-dependencies t)
  1544. (org-blocker-hook
  1545. '(org-block-todo-from-children-or-siblings-or-parent)))
  1546. (org-entry-blocked-p))))
  1547. (should-not
  1548. (org-test-with-temp-text "* DONE Blocked\n** TODO one"
  1549. (let ((org-enforce-todo-dependencies t)
  1550. (org-blocker-hook
  1551. '(org-block-todo-from-children-or-siblings-or-parent)))
  1552. (org-entry-blocked-p))))
  1553. ;; Follow :ORDERED: specifications.
  1554. (should
  1555. (org-test-with-temp-text
  1556. "* H\n:PROPERTIES:\n:ORDERED: t\n:END:\n** TODO one\n** <point>TODO two"
  1557. (let ((org-enforce-todo-dependencies t)
  1558. (org-blocker-hook
  1559. '(org-block-todo-from-children-or-siblings-or-parent)))
  1560. (org-entry-blocked-p))))
  1561. (should-not
  1562. (org-test-with-temp-text
  1563. "* H\n:PROPERTIES:\n:ORDERED: t\n:END:\n** <point>TODO one\n** DONE two"
  1564. (let ((org-enforce-todo-dependencies t)
  1565. (org-blocker-hook
  1566. '(org-block-todo-from-children-or-siblings-or-parent)))
  1567. (org-entry-blocked-p)))))
  1568. (ert-deftest test-org/get-outline-path ()
  1569. "Test `org-get-outline-path' specifications."
  1570. ;; Top-level headlines have no outline path.
  1571. (should-not
  1572. (org-test-with-temp-text "* H"
  1573. (org-get-outline-path)))
  1574. ;; Otherwise, outline path is the path leading to the headline.
  1575. (should
  1576. (equal '("H")
  1577. (org-test-with-temp-text "* H\n** S<point>"
  1578. (org-get-outline-path))))
  1579. ;; Find path even when point is not on a headline.
  1580. (should
  1581. (equal '("H")
  1582. (org-test-with-temp-text "* H\n** S\nText<point>"
  1583. (org-get-outline-path))))
  1584. ;; TODO keywords, tags and statistics cookies are ignored.
  1585. (should
  1586. (equal '("H")
  1587. (org-test-with-temp-text "* TODO H [0/1] :tag:\n** S<point>"
  1588. (org-get-outline-path))))
  1589. ;; Links are replaced with their description or their path.
  1590. (should
  1591. (equal '("Org")
  1592. (org-test-with-temp-text
  1593. "* [[http://orgmode.org][Org]]\n** S<point>"
  1594. (org-get-outline-path))))
  1595. (should
  1596. (equal '("http://orgmode.org")
  1597. (org-test-with-temp-text
  1598. "* [[http://orgmode.org]]\n** S<point>"
  1599. (org-get-outline-path))))
  1600. ;; When WITH-SELF is non-nil, include current heading.
  1601. (should
  1602. (equal '("H")
  1603. (org-test-with-temp-text "* H"
  1604. (org-get-outline-path t))))
  1605. (should
  1606. (equal '("H" "S")
  1607. (org-test-with-temp-text "* H\n** S\nText<point>"
  1608. (org-get-outline-path t))))
  1609. ;; Using cache is transparent to the user.
  1610. (should
  1611. (equal '("H")
  1612. (org-test-with-temp-text "* H\n** S<point>"
  1613. (setq org-outline-path-cache nil)
  1614. (org-get-outline-path nil t))))
  1615. ;; Do not corrupt cache when finding outline path in distant part of
  1616. ;; the buffer.
  1617. (should
  1618. (equal '("H2")
  1619. (org-test-with-temp-text "* H\n** S<point>\n* H2\n** S2"
  1620. (setq org-outline-path-cache nil)
  1621. (org-get-outline-path nil t)
  1622. (search-forward "S2")
  1623. (org-get-outline-path nil t))))
  1624. ;; Do not choke on empty headlines.
  1625. (should
  1626. (org-test-with-temp-text "* H\n** <point>"
  1627. (org-get-outline-path)))
  1628. (should
  1629. (org-test-with-temp-text "* \n** H<point>"
  1630. (org-get-outline-path))))
  1631. (ert-deftest test-org/format-outline-path ()
  1632. "Test `org-format-outline-path' specifications."
  1633. (should
  1634. (string= (org-format-outline-path (list "one" "two" "three"))
  1635. "one/two/three"))
  1636. ;; Empty path.
  1637. (should
  1638. (string= (org-format-outline-path '())
  1639. ""))
  1640. (should
  1641. (string= (org-format-outline-path '(nil))
  1642. ""))
  1643. ;; Empty path and prefix.
  1644. (should
  1645. (string= (org-format-outline-path '() nil ">>")
  1646. ">>"))
  1647. ;; Trailing whitespace in headings.
  1648. (should
  1649. (string= (org-format-outline-path (list "one\t" "tw o " "three "))
  1650. "one/tw o/three"))
  1651. ;; Non-default prefix and separators.
  1652. (should
  1653. (string= (org-format-outline-path (list "one" "two" "three") nil ">>" "|")
  1654. ">>|one|two|three"))
  1655. ;; Truncate.
  1656. (should
  1657. (string= (org-format-outline-path (list "one" "two" "three" "four") 10)
  1658. "one/two/.."))
  1659. ;; Give a very narrow width.
  1660. (should
  1661. (string= (org-format-outline-path (list "one" "two" "three" "four") 2)
  1662. "on"))
  1663. ;; Give a prefix that extends beyond the width.
  1664. (should
  1665. (string= (org-format-outline-path (list "one" "two" "three" "four") 10
  1666. ">>>>>>>>>>")
  1667. ">>>>>>>>..")))
  1668. (ert-deftest test-org/map-entries ()
  1669. "Test `org-map-entries' specifications."
  1670. ;; Full match.
  1671. (should
  1672. (equal '(1 11)
  1673. (org-test-with-temp-text "* Level 1\n** Level 2"
  1674. (org-map-entries #'point))))
  1675. ;; Level match.
  1676. (should
  1677. (equal '(1)
  1678. (org-test-with-temp-text "* Level 1\n** Level 2"
  1679. (let (org-odd-levels-only) (org-map-entries #'point "LEVEL=1")))))
  1680. (should
  1681. (equal '(11)
  1682. (org-test-with-temp-text "* Level 1\n** Level 2"
  1683. (let (org-odd-levels-only) (org-map-entries #'point "LEVEL>1")))))
  1684. ;; Tag match.
  1685. (should
  1686. (equal '(11)
  1687. (org-test-with-temp-text "* H1 :no:\n* H2 :yes:"
  1688. (org-map-entries #'point "yes"))))
  1689. (should
  1690. (equal '(14)
  1691. (org-test-with-temp-text "* H1 :yes:a:\n* H2 :yes:b:"
  1692. (org-map-entries #'point "+yes-a"))))
  1693. (should
  1694. (equal '(11 23)
  1695. (org-test-with-temp-text "* H1 :no:\n* H2 :yes1:\n* H3 :yes2:"
  1696. (org-map-entries #'point "{yes?}"))))
  1697. ;; Priority match.
  1698. (should
  1699. (equal '(1)
  1700. (org-test-with-temp-text "* [#A] H1\n* [#B] H2"
  1701. (org-map-entries #'point "PRIORITY=\"A\""))))
  1702. ;; Date match.
  1703. (should
  1704. (equal '(36)
  1705. (org-test-with-temp-text "
  1706. * H1
  1707. SCHEDULED: <2012-03-29 thu.>
  1708. * H2
  1709. SCHEDULED: <2014-03-04 tue.>"
  1710. (org-map-entries #'point "SCHEDULED=\"<2014-03-04 tue.>\""))))
  1711. (should
  1712. (equal '(2)
  1713. (org-test-with-temp-text "
  1714. * H1
  1715. SCHEDULED: <2012-03-29 thu.>
  1716. * H2
  1717. SCHEDULED: <2014-03-04 tue.>"
  1718. (org-map-entries #'point "SCHEDULED<\"<2013-01-01>\""))))
  1719. ;; Regular property match.
  1720. (should
  1721. (equal '(2)
  1722. (org-test-with-temp-text "
  1723. * H1
  1724. :PROPERTIES:
  1725. :TEST: 1
  1726. :END:
  1727. * H2
  1728. :PROPERTIES:
  1729. :TEST: 2
  1730. :END:"
  1731. (org-map-entries #'point "TEST=1"))))
  1732. ;; Multiple criteria.
  1733. (should
  1734. (equal '(23)
  1735. (org-test-with-temp-text "* H1 :no:\n** H2 :yes:\n* H3 :yes:"
  1736. (let (org-odd-levels-only
  1737. (org-use-tag-inheritance nil))
  1738. (org-map-entries #'point "yes+LEVEL=1")))))
  1739. ;; "or" criteria.
  1740. (should
  1741. (equal '(12 24)
  1742. (org-test-with-temp-text "* H1 :yes:\n** H2 :yes:\n** H3 :no:"
  1743. (let (org-odd-levels-only)
  1744. (org-map-entries #'point "LEVEL=2|no")))))
  1745. (should
  1746. (equal '(1 12)
  1747. (org-test-with-temp-text "* H1 :yes:\n* H2 :no:\n* H3 :maybe:"
  1748. (let (org-odd-levels-only)
  1749. (org-map-entries #'point "yes|no")))))
  1750. ;; "and" criteria.
  1751. (should
  1752. (equal '(22)
  1753. (org-test-with-temp-text "* H1 :yes:\n* H2 :no:\n* H3 :yes:no:"
  1754. (let (org-odd-levels-only)
  1755. (org-map-entries #'point "yes&no"))))))
  1756. (ert-deftest test-org/edit-headline ()
  1757. "Test `org-edit-headline' specifications."
  1758. (should
  1759. (equal "* B"
  1760. (org-test-with-temp-text "* A"
  1761. (org-edit-headline "B")
  1762. (buffer-string))))
  1763. ;; Handle empty headings.
  1764. (should
  1765. (equal "* "
  1766. (org-test-with-temp-text "* A"
  1767. (org-edit-headline "")
  1768. (buffer-string))))
  1769. (should
  1770. (equal "* A"
  1771. (org-test-with-temp-text "* "
  1772. (org-edit-headline "A")
  1773. (buffer-string))))
  1774. ;; Handle TODO keywords and priority cookies.
  1775. (should
  1776. (equal "* TODO B"
  1777. (org-test-with-temp-text "* TODO A"
  1778. (org-edit-headline "B")
  1779. (buffer-string))))
  1780. (should
  1781. (equal "* [#A] B"
  1782. (org-test-with-temp-text "* [#A] A"
  1783. (org-edit-headline "B")
  1784. (buffer-string))))
  1785. (should
  1786. (equal "* TODO [#A] B"
  1787. (org-test-with-temp-text "* TODO [#A] A"
  1788. (org-edit-headline "B")
  1789. (buffer-string))))
  1790. ;; Handle tags.
  1791. (equal "* B :tag:"
  1792. (org-test-with-temp-text "* A :tag:"
  1793. (let ((org-tags-column 4)) (org-edit-headline "B"))
  1794. (buffer-string))))
  1795. ;;; Keywords
  1796. (ert-deftest test-org/set-regexps-and-options ()
  1797. "Test `org-set-regexps-and-options' specifications."
  1798. ;; TAGS keyword.
  1799. (should
  1800. (equal '(("A"))
  1801. (let ((org-tag-alist '(("A")))
  1802. (org-tag-persistent-alist nil))
  1803. (org-test-with-temp-text ""
  1804. (org-mode-restart)
  1805. org-current-tag-alist))))
  1806. (should
  1807. (equal '(("B"))
  1808. (let ((org-tag-alist '(("A")))
  1809. (org-tag-persistent-alist nil))
  1810. (org-test-with-temp-text "#+TAGS: B"
  1811. (org-mode-restart)
  1812. org-current-tag-alist))))
  1813. (should
  1814. (equal '(("C") ("B"))
  1815. (let ((org-tag-alist '(("A")))
  1816. (org-tag-persistent-alist '(("C"))))
  1817. (org-test-with-temp-text "#+TAGS: B"
  1818. (org-mode-restart)
  1819. org-current-tag-alist))))
  1820. (should
  1821. (equal '(("B"))
  1822. (let ((org-tag-alist '(("A")))
  1823. (org-tag-persistent-alist '(("C"))))
  1824. (org-test-with-temp-text "#+STARTUP: noptag\n#+TAGS: B"
  1825. (org-mode-restart)
  1826. org-current-tag-alist))))
  1827. (should
  1828. (equal '(("A" . ?a) ("B") ("C"))
  1829. (let ((org-tag-persistant-alist nil))
  1830. (org-test-with-temp-text "#+TAGS: A(a) B C"
  1831. (org-mode-restart)
  1832. org-current-tag-alist))))
  1833. (should
  1834. (equal '(("A") (:newline) ("B"))
  1835. (let ((org-tag-persistent-alist nil))
  1836. (org-test-with-temp-text "#+TAGS: A\n#+TAGS: B"
  1837. (org-mode-restart)
  1838. org-current-tag-alist))))
  1839. (should
  1840. (equal '((:startgroup) ("A") ("B") (:endgroup) ("C"))
  1841. (let ((org-tag-persistent-alist nil))
  1842. (org-test-with-temp-text "#+TAGS: { A B } C"
  1843. (org-mode-restart)
  1844. org-current-tag-alist))))
  1845. (should
  1846. (equal '((:startgroup) ("A") (:grouptags) ("B") ("C") (:endgroup))
  1847. (let ((org-tag-persistent-alist nil))
  1848. (org-test-with-temp-text "#+TAGS: { A : B C }"
  1849. (org-mode-restart)
  1850. org-current-tag-alist))))
  1851. (should
  1852. (equal '(("A" "B" "C"))
  1853. (let ((org-tag-persistent-alist nil))
  1854. (org-test-with-temp-text "#+TAGS: { A : B C }"
  1855. (org-mode-restart)
  1856. org-tag-groups-alist))))
  1857. (should
  1858. (equal '((:startgrouptag) ("A") (:grouptags) ("B") ("C") (:endgrouptag))
  1859. (let ((org-tag-persistent-alist nil))
  1860. (org-test-with-temp-text "#+TAGS: [ A : B C ]"
  1861. (org-mode-restart)
  1862. org-current-tag-alist))))
  1863. (should
  1864. (equal '(("A" "B" "C"))
  1865. (let ((org-tag-persistent-alist nil))
  1866. (org-test-with-temp-text "#+TAGS: [ A : B C ]"
  1867. (org-mode-restart)
  1868. org-tag-groups-alist))))
  1869. ;; FILETAGS keyword.
  1870. (should
  1871. (equal '("A" "B" "C")
  1872. (org-test-with-temp-text "#+FILETAGS: :A:B:C:"
  1873. (org-mode-restart)
  1874. org-file-tags)))
  1875. ;; PROPERTY keyword. Property names are case-insensitive.
  1876. (should
  1877. (equal "foo=1"
  1878. (org-test-with-temp-text "#+PROPERTY: var foo=1"
  1879. (org-mode-restart)
  1880. (cdr (assoc "var" org-file-properties)))))
  1881. (should
  1882. (equal
  1883. "foo=1 bar=2"
  1884. (org-test-with-temp-text "#+PROPERTY: var foo=1\n#+PROPERTY: var+ bar=2"
  1885. (org-mode-restart)
  1886. (cdr (assoc "var" org-file-properties)))))
  1887. (should
  1888. (equal
  1889. "foo=1 bar=2"
  1890. (org-test-with-temp-text "#+PROPERTY: var foo=1\n#+PROPERTY: VAR+ bar=2"
  1891. (org-mode-restart)
  1892. (cdr (assoc "var" org-file-properties)))))
  1893. ;; ARCHIVE keyword.
  1894. (should
  1895. (equal "%s_done::"
  1896. (org-test-with-temp-text "#+ARCHIVE: %s_done::"
  1897. (org-mode-restart)
  1898. org-archive-location)))
  1899. ;; CATEGORY keyword.
  1900. (should
  1901. (eq 'test
  1902. (org-test-with-temp-text "#+CATEGORY: test"
  1903. (org-mode-restart)
  1904. org-category)))
  1905. (should
  1906. (equal "test"
  1907. (org-test-with-temp-text "#+CATEGORY: test"
  1908. (org-mode-restart)
  1909. (cdr (assoc "CATEGORY" org-file-properties)))))
  1910. ;; COLUMNS keyword.
  1911. (should
  1912. (equal "%25ITEM %TAGS %PRIORITY %TODO"
  1913. (org-test-with-temp-text "#+COLUMNS: %25ITEM %TAGS %PRIORITY %TODO"
  1914. (org-mode-restart)
  1915. org-columns-default-format)))
  1916. ;; CONSTANTS keyword. Constants names are case sensitive.
  1917. (should
  1918. (equal '("299792458." "3.14")
  1919. (org-test-with-temp-text "#+CONSTANTS: c=299792458. pi=3.14"
  1920. (org-mode-restart)
  1921. (mapcar
  1922. (lambda (n) (cdr (assoc n org-table-formula-constants-local)))
  1923. '("c" "pi")))))
  1924. (should
  1925. (equal "3.14"
  1926. (org-test-with-temp-text "#+CONSTANTS: pi=22/7 pi=3.14"
  1927. (org-mode-restart)
  1928. (cdr (assoc "pi" org-table-formula-constants-local)))))
  1929. (should
  1930. (equal "22/7"
  1931. (org-test-with-temp-text "#+CONSTANTS: PI=22/7 pi=3.14"
  1932. (org-mode-restart)
  1933. (cdr (assoc "PI" org-table-formula-constants-local)))))
  1934. ;; LINK keyword.
  1935. (should
  1936. (equal
  1937. '("url1" "url2")
  1938. (org-test-with-temp-text "#+LINK: a url1\n#+LINK: b url2"
  1939. (org-mode-restart)
  1940. (mapcar (lambda (abbrev) (cdr (assoc abbrev org-link-abbrev-alist-local)))
  1941. '("a" "b")))))
  1942. ;; PRIORITIES keyword. Incomplete priorities sets are ignored.
  1943. (should
  1944. (equal
  1945. '(?X ?Z ?Y)
  1946. (org-test-with-temp-text "#+PRIORITIES: X Z Y"
  1947. (org-mode-restart)
  1948. (list org-highest-priority org-lowest-priority org-default-priority))))
  1949. (should
  1950. (equal
  1951. '(?A ?C ?B)
  1952. (org-test-with-temp-text "#+PRIORITIES: X Z"
  1953. (org-mode-restart)
  1954. (list org-highest-priority org-lowest-priority org-default-priority))))
  1955. ;; STARTUP keyword.
  1956. (should
  1957. (equal '(t t)
  1958. (org-test-with-temp-text "#+STARTUP: fold odd"
  1959. (org-mode-restart)
  1960. (list org-startup-folded org-odd-levels-only))))
  1961. ;; TODO keywords.
  1962. (should
  1963. (equal '(("A" "B") ("C"))
  1964. (org-test-with-temp-text "#+TODO: A B | C"
  1965. (org-mode-restart)
  1966. (list org-not-done-keywords org-done-keywords))))
  1967. (should
  1968. (equal '(("A" "C") ("B" "D"))
  1969. (org-test-with-temp-text "#+TODO: A | B\n#+TODO: C | D"
  1970. (org-mode-restart)
  1971. (list org-not-done-keywords org-done-keywords))))
  1972. (should
  1973. (equal '(("A" "B") ("C"))
  1974. (org-test-with-temp-text "#+TYP_TODO: A B | C"
  1975. (org-mode-restart)
  1976. (list org-not-done-keywords org-done-keywords))))
  1977. (should
  1978. (equal '((:startgroup) ("A" . ?a) (:endgroup))
  1979. (org-test-with-temp-text "#+TODO: A(a)"
  1980. (org-mode-restart)
  1981. org-todo-key-alist)))
  1982. (should
  1983. (equal '(("D" note nil) ("C" time nil) ("B" note time))
  1984. (org-test-with-temp-text "#+TODO: A(a) B(b@/!) | C(c!) D(d@)"
  1985. (org-mode-restart)
  1986. org-todo-log-states)))
  1987. ;; Enter SETUPFILE keyword.
  1988. (should
  1989. (equal "1"
  1990. (org-test-with-temp-text
  1991. (format "#+SETUPFILE: \"%s/examples/setupfile.org\"" org-test-dir)
  1992. (org-mode-restart)
  1993. (cdr (assoc "a" org-file-properties))))))
  1994. ;;; Links
  1995. ;;;; Coderefs
  1996. (ert-deftest test-org/coderef ()
  1997. "Test coderef links specifications."
  1998. (should
  1999. (org-test-with-temp-text "
  2000. #+BEGIN_SRC emacs-lisp
  2001. \(+ 1 1) (ref:sc)
  2002. #+END_SRC
  2003. \[[(sc)<point>]]"
  2004. (org-open-at-point)
  2005. (looking-at "(ref:sc)")))
  2006. ;; Find coderef even with alternate label format.
  2007. (should
  2008. (org-test-with-temp-text "
  2009. #+BEGIN_SRC emacs-lisp -l \"{ref:%s}\"
  2010. \(+ 1 1) {ref:sc}
  2011. #+END_SRC
  2012. \[[(sc)<point>]]"
  2013. (org-open-at-point)
  2014. (looking-at "{ref:sc}"))))
  2015. ;;;; Custom ID
  2016. (ert-deftest test-org/custom-id ()
  2017. "Test custom ID links specifications."
  2018. (should
  2019. (org-test-with-temp-text
  2020. "* H1\n:PROPERTIES:\n:CUSTOM_ID: custom\n:END:\n* H2\n[[#custom<point>]]"
  2021. (org-open-at-point)
  2022. (looking-at-p "\\* H1")))
  2023. ;; Throw an error on false positives.
  2024. (should-error
  2025. (org-test-with-temp-text
  2026. "* H1\n:DRAWER:\n:CUSTOM_ID: custom\n:END:\n* H2\n[[#custom<point>]]"
  2027. (org-open-at-point)
  2028. (looking-at-p "\\* H1"))))
  2029. ;;;; Fuzzy Links
  2030. ;; Fuzzy links [[text]] encompass links to a target (<<text>>), to
  2031. ;; a named element (#+name: text) and to headlines (* Text).
  2032. (ert-deftest test-org/fuzzy-links ()
  2033. "Test fuzzy links specifications."
  2034. ;; Fuzzy link goes in priority to a matching target.
  2035. (should
  2036. (org-test-with-temp-text
  2037. "#+NAME: Test\n|a|b|\n<<Test>>\n* Test\n<point>[[Test]]"
  2038. (let ((org-link-search-must-match-exact-headline nil)) (org-open-at-point))
  2039. (looking-at "<<Test>>")))
  2040. ;; Then fuzzy link points to an element with a given name.
  2041. (should
  2042. (org-test-with-temp-text "Test\n#+NAME: Test\n|a|b|\n* Test\n<point>[[Test]]"
  2043. (let ((org-link-search-must-match-exact-headline nil)) (org-open-at-point))
  2044. (looking-at "#\\+NAME: Test")))
  2045. ;; A target still lead to a matching headline otherwise.
  2046. (should
  2047. (org-test-with-temp-text "* Head1\n* Head2\n*Head3\n<point>[[Head2]]"
  2048. (let ((org-link-search-must-match-exact-headline nil)) (org-open-at-point))
  2049. (looking-at "\\* Head2")))
  2050. ;; With a leading star in link, enforce heading match.
  2051. (should
  2052. (org-test-with-temp-text "* Test\n<<Test>>\n<point>[[*Test]]"
  2053. (let ((org-link-search-must-match-exact-headline nil)) (org-open-at-point))
  2054. (looking-at "\\* Test")))
  2055. ;; With a leading star in link, enforce exact heading match, even
  2056. ;; with `org-link-search-must-match-exact-headline' set to nil.
  2057. (should-error
  2058. (org-test-with-temp-text "* Test 1\nFoo Bar\n<point>[[*Test]]"
  2059. (let ((org-link-search-must-match-exact-headline nil))
  2060. (org-open-at-point))))
  2061. ;; Handle non-nil `org-link-search-must-match-exact-headline'.
  2062. (should
  2063. (org-test-with-temp-text "* Test\nFoo Bar\n<point>[[Test]]"
  2064. (let ((org-link-search-must-match-exact-headline t)) (org-open-at-point))
  2065. (looking-at "\\* Test")))
  2066. (should
  2067. (org-test-with-temp-text "* Test\nFoo Bar\n<point>[[*Test]]"
  2068. (let ((org-link-search-must-match-exact-headline t)) (org-open-at-point))
  2069. (looking-at "\\* Test")))
  2070. ;; Heading match should not care about spaces, cookies, TODO
  2071. ;; keywords, priorities, and tags. However, TODO keywords are
  2072. ;; case-sensitive.
  2073. (should
  2074. (let ((first-line
  2075. "** TODO [#A] [/] Test [1/2] [33%] 1 \t 2 [%] :work:urgent: "))
  2076. (org-test-with-temp-text
  2077. (concat first-line "\nFoo Bar\n<point>[[*Test 1 2]]")
  2078. (let ((org-link-search-must-match-exact-headline nil)
  2079. (org-todo-regexp "TODO"))
  2080. (org-open-at-point))
  2081. (looking-at (regexp-quote first-line)))))
  2082. (should-error
  2083. (org-test-with-temp-text "** todo Test 1 2\nFoo Bar\n<point>[[*Test 1 2]]"
  2084. (let ((org-link-search-must-match-exact-headline nil)
  2085. (org-todo-regexp "TODO"))
  2086. (org-open-at-point))))
  2087. ;; Heading match should still be exact.
  2088. (should-error
  2089. (org-test-with-temp-text "
  2090. ** TODO [#A] [/] Test [1/2] [33%] 1 \t 2 [%] :work:urgent:
  2091. Foo Bar
  2092. <point>[[*Test 1]]"
  2093. (let ((org-link-search-must-match-exact-headline nil)
  2094. (org-todo-regexp "TODO"))
  2095. (org-open-at-point))))
  2096. (should
  2097. (org-test-with-temp-text "* Test 1 2 3\n** Test 1 2\n<point>[[*Test 1 2]]"
  2098. (let ((org-link-search-must-match-exact-headline nil)
  2099. (org-todo-regexp "TODO"))
  2100. (org-open-at-point))
  2101. (looking-at-p (regexp-quote "** Test 1 2"))))
  2102. ;; Heading match ignores COMMENT keyword.
  2103. (should
  2104. (org-test-with-temp-text "[[*Test]]\n* COMMENT Test"
  2105. (org-open-at-point)
  2106. (looking-at "\\* COMMENT Test")))
  2107. (should
  2108. (org-test-with-temp-text "[[*Test]]\n* TODO COMMENT Test"
  2109. (org-open-at-point)
  2110. (looking-at "\\* TODO COMMENT Test")))
  2111. ;; Correctly un-hexify fuzzy links.
  2112. (should
  2113. (org-test-with-temp-text "* With space\n[[*With%20space][With space<point>]]"
  2114. (org-open-at-point)
  2115. (bobp)))
  2116. (should
  2117. (org-test-with-temp-text "* [1]\n[[*%5B1%5D<point>]]"
  2118. (org-open-at-point)
  2119. (bobp))))
  2120. ;;;; Link Escaping
  2121. (ert-deftest test-org/org-link-escape-ascii-character ()
  2122. "Escape an ascii character."
  2123. (should
  2124. (string=
  2125. "%5B"
  2126. (org-link-escape "["))))
  2127. (ert-deftest test-org/org-link-escape-ascii-ctrl-character ()
  2128. "Escape an ascii control character."
  2129. (should
  2130. (string=
  2131. "%09"
  2132. (org-link-escape "\t"))))
  2133. (ert-deftest test-org/org-link-escape-multibyte-character ()
  2134. "Escape an unicode multibyte character."
  2135. (should
  2136. (string=
  2137. "%E2%82%AC"
  2138. (org-link-escape "€"))))
  2139. (ert-deftest test-org/org-link-escape-custom-table ()
  2140. "Escape string with custom character table."
  2141. (should
  2142. (string=
  2143. "Foo%3A%42ar%0A"
  2144. (org-link-escape "Foo:Bar\n" '(?\: ?\B)))))
  2145. (ert-deftest test-org/org-link-escape-custom-table-merge ()
  2146. "Escape string with custom table merged with default table."
  2147. (should
  2148. (string=
  2149. "%5BF%6F%6F%3A%42ar%0A%5D"
  2150. (org-link-escape "[Foo:Bar\n]" '(?\: ?\B ?\o) t))))
  2151. (ert-deftest test-org/org-link-unescape-ascii-character ()
  2152. "Unescape an ascii character."
  2153. (should
  2154. (string=
  2155. "["
  2156. (org-link-unescape "%5B"))))
  2157. (ert-deftest test-org/org-link-unescape-ascii-ctrl-character ()
  2158. "Unescpae an ascii control character."
  2159. (should
  2160. (string=
  2161. "\n"
  2162. (org-link-unescape "%0A"))))
  2163. (ert-deftest test-org/org-link-unescape-multibyte-character ()
  2164. "Unescape unicode multibyte character."
  2165. (should
  2166. (string=
  2167. "€"
  2168. (org-link-unescape "%E2%82%AC"))))
  2169. (ert-deftest test-org/org-link-unescape-ascii-extended-char ()
  2170. "Unescape old style percent escaped character."
  2171. (should
  2172. (string=
  2173. "àâçèéêîôùû"
  2174. (decode-coding-string
  2175. (org-link-unescape "%E0%E2%E7%E8%E9%EA%EE%F4%F9%FB") 'latin-1))))
  2176. (ert-deftest test-org/org-link-escape-url-with-escaped-char ()
  2177. "Escape and unescape a URL that includes an escaped char.
  2178. http://article.gmane.org/gmane.emacs.orgmode/21459/"
  2179. (should
  2180. (string=
  2181. "http://some.host.com/form?&id=blah%2Bblah25"
  2182. (org-link-unescape
  2183. (org-link-escape "http://some.host.com/form?&id=blah%2Bblah25")))))
  2184. ;;;; Open at point
  2185. (ert-deftest test-org/open-at-point-in-keyword ()
  2186. "Does `org-open-at-point' open link in a keyword line?"
  2187. (should
  2188. (org-test-with-temp-text
  2189. "<<top>>\n#+KEYWORD: <point>[[top]]"
  2190. (org-open-at-point) t)))
  2191. (ert-deftest test-org/open-at-point-in-property ()
  2192. "Does `org-open-at-point' open link in property drawer?"
  2193. (should
  2194. (org-test-with-temp-text
  2195. "* Headline
  2196. :PROPERTIES:
  2197. :URL: <point>[[*Headline]]
  2198. :END:"
  2199. (org-open-at-point) t)))
  2200. (ert-deftest test-org/open-at-point-in-comment ()
  2201. "Does `org-open-at-point' open link in a commented line?"
  2202. (should
  2203. (org-test-with-temp-text
  2204. "<<top>>\n# <point>[[top]]"
  2205. (org-open-at-point) t)))
  2206. (ert-deftest test-org/open-at-point/inline-image ()
  2207. "Test `org-open-at-point' on nested links."
  2208. (should
  2209. (org-test-with-temp-text "<<top>>\n[[top][file:<point>unicorn.jpg]]"
  2210. (org-open-at-point)
  2211. (bobp))))
  2212. (ert-deftest test-org/open-at-point/radio-target ()
  2213. "Test `org-open-at-point' on radio targets."
  2214. (should
  2215. (org-test-with-temp-text "<<<target>>> <point>target"
  2216. (org-update-radio-target-regexp)
  2217. (org-open-at-point)
  2218. (eq (org-element-type (org-element-context)) 'radio-target))))
  2219. ;;;; Stored links
  2220. (ert-deftest test-org/store-link ()
  2221. "Test `org-store-link' specifications."
  2222. ;; On a headline, link to that headline. Use heading as the
  2223. ;; description of the link.
  2224. (should
  2225. (let (org-store-link-props org-stored-links)
  2226. (org-test-with-temp-text-in-file "* H1"
  2227. (let ((file (buffer-file-name)))
  2228. (equal (format "[[file:%s::*H1][H1]]" file)
  2229. (org-store-link nil))))))
  2230. ;; On a headline, remove any link from description.
  2231. (should
  2232. (let (org-store-link-props org-stored-links)
  2233. (org-test-with-temp-text-in-file "* [[#l][d]]"
  2234. (let ((file (buffer-file-name)))
  2235. (equal (format "[[file:%s::*%s][d]]"
  2236. file
  2237. (org-link-escape "[[#l][d]]"))
  2238. (org-store-link nil))))))
  2239. (should
  2240. (let (org-store-link-props org-stored-links)
  2241. (org-test-with-temp-text-in-file "* [[l]]"
  2242. (let ((file (buffer-file-name)))
  2243. (equal (format "[[file:%s::*%s][l]]" file (org-link-escape "[[l]]"))
  2244. (org-store-link nil))))))
  2245. (should
  2246. (let (org-store-link-props org-stored-links)
  2247. (org-test-with-temp-text-in-file "* [[l1][d1]] [[l2][d2]]"
  2248. (let ((file (buffer-file-name)))
  2249. (equal (format "[[file:%s::*%s][d1 d2]]"
  2250. file
  2251. (org-link-escape "[[l1][d1]] [[l2][d2]]"))
  2252. (org-store-link nil))))))
  2253. ;; On a named element, link to that element.
  2254. (should
  2255. (let (org-store-link-props org-stored-links)
  2256. (org-test-with-temp-text-in-file "#+NAME: foo\nParagraph"
  2257. (let ((file (buffer-file-name)))
  2258. (equal (format "[[file:%s::foo][foo]]" file)
  2259. (org-store-link nil)))))))
  2260. ;;; Node Properties
  2261. (ert-deftest test-org/accumulated-properties-in-drawers ()
  2262. "Ensure properties accumulate in subtree drawers."
  2263. (org-test-at-id "75282ba2-f77a-4309-a970-e87c149fe125"
  2264. (org-babel-next-src-block)
  2265. (should (equal '(2 1) (org-babel-execute-src-block)))))
  2266. (ert-deftest test-org/custom-properties ()
  2267. "Test custom properties specifications."
  2268. ;; Standard test.
  2269. (should
  2270. (let ((org-custom-properties '("FOO")))
  2271. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:FOO: val\n:END:\n"
  2272. (org-toggle-custom-properties-visibility)
  2273. (org-invisible-p2))))
  2274. ;; Properties are case-insensitive.
  2275. (should
  2276. (let ((org-custom-properties '("FOO")))
  2277. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:foo: val\n:END:\n"
  2278. (org-toggle-custom-properties-visibility)
  2279. (org-invisible-p2))))
  2280. (should
  2281. (let ((org-custom-properties '("foo")))
  2282. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:FOO: val\n:END:\n"
  2283. (org-toggle-custom-properties-visibility)
  2284. (org-invisible-p2))))
  2285. ;; Multiple custom properties in the same drawer.
  2286. (should
  2287. (let ((org-custom-properties '("FOO" "BAR")))
  2288. (org-test-with-temp-text
  2289. "* H\n:PROPERTIES:\n<point>:FOO: val\n:P: 1\n:BAR: baz\n:END:\n"
  2290. (org-toggle-custom-properties-visibility)
  2291. (and (org-invisible-p2)
  2292. (not (progn (forward-line) (org-invisible-p2)))
  2293. (progn (forward-line) (org-invisible-p2))))))
  2294. ;; Hide custom properties with an empty value.
  2295. (should
  2296. (let ((org-custom-properties '("FOO")))
  2297. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:FOO:\n:END:\n"
  2298. (org-toggle-custom-properties-visibility)
  2299. (org-invisible-p2))))
  2300. ;; Do not hide fake properties.
  2301. (should-not
  2302. (let ((org-custom-properties '("FOO")))
  2303. (org-test-with-temp-text ":FOO: val\n"
  2304. (org-toggle-custom-properties-visibility)
  2305. (org-invisible-p2))))
  2306. (should-not
  2307. (let ((org-custom-properties '("A")))
  2308. (org-test-with-temp-text
  2309. "* H\n:PROPERTIES:\n:A: 1\n:END:\n\n:PROPERTIES:\n<point>:A: 2\n:END:"
  2310. (org-toggle-custom-properties-visibility)
  2311. (org-invisible-p2)))))
  2312. ;;; Mark Region
  2313. (ert-deftest test-org/mark-subtree ()
  2314. "Test `org-mark-subtree' specifications."
  2315. ;; Error when point is before first headline.
  2316. (should-error
  2317. (org-test-with-temp-text "Paragraph\n* Headline\nBody"
  2318. (progn (transient-mark-mode 1)
  2319. (org-mark-subtree))))
  2320. ;; Without argument, mark current subtree.
  2321. (should
  2322. (equal
  2323. '(12 32)
  2324. (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
  2325. (progn (transient-mark-mode 1)
  2326. (forward-line 2)
  2327. (org-mark-subtree)
  2328. (list (region-beginning) (region-end))))))
  2329. ;; With an argument, move ARG up.
  2330. (should
  2331. (equal
  2332. '(1 32)
  2333. (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
  2334. (progn (transient-mark-mode 1)
  2335. (forward-line 2)
  2336. (org-mark-subtree 1)
  2337. (list (region-beginning) (region-end))))))
  2338. ;; Do not get fooled by inlinetasks.
  2339. (when (featurep 'org-inlinetask)
  2340. (should
  2341. (= 1
  2342. (org-test-with-temp-text "* Headline\n*************** Task\nContents"
  2343. (progn (transient-mark-mode 1)
  2344. (forward-line 1)
  2345. (let ((org-inlinetask-min-level 15)) (org-mark-subtree))
  2346. (region-beginning)))))))
  2347. ;;; Miscellaneous
  2348. (ert-deftest test-org/in-regexp ()
  2349. "Test `org-in-regexp' specifications."
  2350. ;; Standard tests.
  2351. (should
  2352. (org-test-with-temp-text "xx ab<point>c xx"
  2353. (org-in-regexp "abc")))
  2354. (should-not
  2355. (org-test-with-temp-text "xx abc <point>xx"
  2356. (org-in-regexp "abc")))
  2357. ;; Return non-nil even with multiple matching regexps in the same
  2358. ;; line.
  2359. (should
  2360. (org-test-with-temp-text "abc xx ab<point>c xx"
  2361. (org-in-regexp "abc")))
  2362. ;; With optional argument NLINES, check extra lines around point.
  2363. (should-not
  2364. (org-test-with-temp-text "A\nB<point>\nC"
  2365. (org-in-regexp "A\nB\nC")))
  2366. (should
  2367. (org-test-with-temp-text "A\nB<point>\nC"
  2368. (org-in-regexp "A\nB\nC" 1)))
  2369. (should-not
  2370. (org-test-with-temp-text "A\nB\nC<point>"
  2371. (org-in-regexp "A\nB\nC" 1)))
  2372. ;; When optional argument VISUALLY is non-nil, return nil if at
  2373. ;; regexp boundaries.
  2374. (should
  2375. (org-test-with-temp-text "xx abc<point> xx"
  2376. (org-in-regexp "abc")))
  2377. (should-not
  2378. (org-test-with-temp-text "xx abc<point> xx"
  2379. (org-in-regexp "abc" nil t))))
  2380. ;;; Navigation
  2381. (ert-deftest test-org/end-of-meta-data ()
  2382. "Test `org-end-of-meta-data' specifications."
  2383. ;; Skip planning line.
  2384. (should
  2385. (org-test-with-temp-text "* Headline\nSCHEDULED: <2014-03-04 tue.>"
  2386. (org-end-of-meta-data)
  2387. (eobp)))
  2388. ;; Skip properties drawer.
  2389. (should
  2390. (org-test-with-temp-text
  2391. "* Headline\nSCHEDULED: <2014-03-04 tue.>\n:PROPERTIES:\n:A: 1\n:END:"
  2392. (org-end-of-meta-data)
  2393. (eobp)))
  2394. ;; Skip both.
  2395. (should
  2396. (org-test-with-temp-text "* Headline\n:PROPERTIES:\n:A: 1\n:END:"
  2397. (org-end-of-meta-data)
  2398. (eobp)))
  2399. ;; Nothing to skip, go to first line.
  2400. (should
  2401. (org-test-with-temp-text "* Headline\nContents"
  2402. (org-end-of-meta-data)
  2403. (looking-at "Contents")))
  2404. ;; With option argument, skip empty lines, regular drawers and
  2405. ;; clocking lines.
  2406. (should
  2407. (org-test-with-temp-text "* Headline\n\nContents"
  2408. (org-end-of-meta-data t)
  2409. (looking-at "Contents")))
  2410. (should
  2411. (org-test-with-temp-text "* Headline\nCLOCK:\nContents"
  2412. (org-end-of-meta-data t)
  2413. (looking-at "Contents")))
  2414. (should
  2415. (org-test-with-temp-text "* Headline\n:LOGBOOK:\nlogging\n:END:\nContents"
  2416. (org-end-of-meta-data t)
  2417. (looking-at "Contents")))
  2418. ;; Special case: do not skip incomplete drawers.
  2419. (should
  2420. (org-test-with-temp-text "* Headline\n:LOGBOOK:\nlogging\nContents"
  2421. (org-end-of-meta-data t)
  2422. (looking-at ":LOGBOOK:")))
  2423. ;; Special case: Be careful about consecutive headlines.
  2424. (should-not
  2425. (org-test-with-temp-text "* H1\n*H2\nContents"
  2426. (org-end-of-meta-data t)
  2427. (looking-at "Contents"))))
  2428. (ert-deftest test-org/beginning-of-line ()
  2429. "Test `org-beginning-of-line' specifications."
  2430. ;; Move to beginning of line. If current line in invisible, move to
  2431. ;; beginning of visible line instead.
  2432. (should
  2433. (org-test-with-temp-text "Some text\nSome other text<point>"
  2434. (org-beginning-of-line)
  2435. (bolp)))
  2436. (should
  2437. (org-test-with-temp-text "* H1\n** H2<point>"
  2438. (org-overview)
  2439. (org-beginning-of-line)
  2440. (= (line-beginning-position) 1)))
  2441. ;; With `visual-line-mode' active, move to beginning of visual line.
  2442. (should-not
  2443. (org-test-with-temp-text "A <point>long line of text\nSome other text"
  2444. (visual-line-mode)
  2445. (dotimes (i 1000) (insert "very "))
  2446. (org-beginning-of-line)
  2447. (bolp)))
  2448. ;; In a wide headline, with `visual-line-mode', prefer going to the
  2449. ;; beginning of a visual line than to the logical beginning of line,
  2450. ;; even if special movement is active.
  2451. (should-not
  2452. (org-test-with-temp-text "* A <point>long headline"
  2453. (visual-line-mode)
  2454. (dotimes (i 1000) (insert "very "))
  2455. (goto-char (point-max))
  2456. (org-beginning-of-line)
  2457. (bobp)))
  2458. (should-not
  2459. (org-test-with-temp-text "* A <point>long headline"
  2460. (visual-line-mode)
  2461. (dotimes (i 1000) (insert "very "))
  2462. (goto-char (point-max))
  2463. (let ((org-special-ctrl-a/e t)) (org-beginning-of-line))
  2464. (bobp)))
  2465. ;; At an headline with special movement, first move at beginning of
  2466. ;; title, then at the beginning of line, rinse, repeat.
  2467. (should
  2468. (org-test-with-temp-text "* TODO Headline<point>"
  2469. (let ((org-special-ctrl-a/e t))
  2470. (and (progn (org-beginning-of-line) (looking-at-p "Headline"))
  2471. (progn (org-beginning-of-line) (bolp))
  2472. (progn (org-beginning-of-line) (looking-at-p "Headline"))))))
  2473. (should
  2474. (org-test-with-temp-text "* TODO [#A] Headline<point>"
  2475. (let ((org-special-ctrl-a/e t))
  2476. (org-beginning-of-line)
  2477. (looking-at "Headline"))))
  2478. (should
  2479. (org-test-with-temp-text "* TODO [#A] Headline<point>"
  2480. (let ((org-special-ctrl-a/e '(t . nil)))
  2481. (org-beginning-of-line)
  2482. (looking-at "Headline"))))
  2483. (should-not
  2484. (org-test-with-temp-text "* TODO [#A] Headline<point>"
  2485. (let ((org-special-ctrl-a/e '(nil . nil)))
  2486. (org-beginning-of-line)
  2487. (looking-at "Headline"))))
  2488. ;; At an headline with reversed movement, first move to beginning of
  2489. ;; line, then to the beginning of title.
  2490. (should
  2491. (org-test-with-temp-text "* TODO Headline<point>"
  2492. (let ((org-special-ctrl-a/e 'reversed)
  2493. (this-command last-command))
  2494. (and (progn (org-beginning-of-line) (bolp))
  2495. (progn (org-beginning-of-line) (looking-at-p "Headline"))))))
  2496. (should
  2497. (org-test-with-temp-text "* TODO Headline<point>"
  2498. (let ((org-special-ctrl-a/e '(reversed . nil))
  2499. (this-command last-command))
  2500. (and (progn (org-beginning-of-line) (bolp))
  2501. (progn (org-beginning-of-line) (looking-at-p "Headline"))))))
  2502. (should-not
  2503. (org-test-with-temp-text "* TODO Headline<point>"
  2504. (let ((org-special-ctrl-a/e '(t . nil))
  2505. (this-command last-command))
  2506. (and (progn (org-beginning-of-line) (bolp))
  2507. (progn (org-beginning-of-line) (looking-at-p "Headline"))))))
  2508. ;; At an item with special movement, first move after to beginning
  2509. ;; of title, then to the beginning of line, rinse, repeat.
  2510. (should
  2511. (org-test-with-temp-text "- [ ] Item<point>"
  2512. (let ((org-special-ctrl-a/e t))
  2513. (and (progn (org-beginning-of-line) (looking-at-p "Item"))
  2514. (progn (org-beginning-of-line) (bolp))
  2515. (progn (org-beginning-of-line) (looking-at-p "Item"))))))
  2516. ;; At an item with reversed movement, first move to beginning of
  2517. ;; line, then to the beginning of title.
  2518. (should
  2519. (org-test-with-temp-text "- [X] Item<point>"
  2520. (let ((org-special-ctrl-a/e 'reversed)
  2521. (this-command last-command))
  2522. (and (progn (org-beginning-of-line) (bolp))
  2523. (progn (org-beginning-of-line) (looking-at-p "Item"))))))
  2524. ;; Leave point before invisible characters at column 0.
  2525. (should
  2526. (org-test-with-temp-text "[[http://orgmode.org]]<point>"
  2527. (let ((org-special-ctrl-a/e nil))
  2528. (org-beginning-of-line)
  2529. (bolp))))
  2530. (should
  2531. (org-test-with-temp-text "[[http://orgmode.org]]<point>"
  2532. (let ((org-special-ctrl-a/e t))
  2533. (org-beginning-of-line)
  2534. (bolp))))
  2535. (should
  2536. (org-test-with-temp-text "[[http<point>://orgmode.org]]"
  2537. (visual-line-mode)
  2538. (org-beginning-of-line)
  2539. (bolp)))
  2540. ;; Special case: Do not error when the buffer contains only a single
  2541. ;; asterisk.
  2542. (should
  2543. (org-test-with-temp-text "*<point>"
  2544. (let ((org-special-ctrl-a/e t)) (org-beginning-of-line) t)))
  2545. (should
  2546. (org-test-with-temp-text "*<point>"
  2547. (let ((org-special-ctrl-a/e nil)) (org-beginning-of-line) t))))
  2548. (ert-deftest test-org/end-of-line ()
  2549. "Test `org-end-of-line' specifications."
  2550. ;; Standard test.
  2551. (should
  2552. (org-test-with-temp-text "Some text\nSome other text"
  2553. (org-end-of-line)
  2554. (eolp)))
  2555. ;; With `visual-line-mode' active, move to end of visible line.
  2556. ;; However, never go past ellipsis.
  2557. (should-not
  2558. (org-test-with-temp-text "A <point>long line of text\nSome other text"
  2559. (visual-line-mode)
  2560. (dotimes (i 1000) (insert "very "))
  2561. (goto-char (point-min))
  2562. (org-end-of-line)
  2563. (eolp)))
  2564. (should-not
  2565. (org-test-with-temp-text "* A short headline\nSome contents"
  2566. (visual-line-mode)
  2567. (org-overview)
  2568. (org-end-of-line)
  2569. (eobp)))
  2570. ;; In a wide headline, with `visual-line-mode', prefer going to end
  2571. ;; of visible line if tags, or end of line, are farther.
  2572. (should-not
  2573. (org-test-with-temp-text "* A <point>long headline"
  2574. (visual-line-mode)
  2575. (dotimes (i 1000) (insert "very "))
  2576. (goto-char (point-min))
  2577. (org-end-of-line)
  2578. (eolp)))
  2579. (should-not
  2580. (org-test-with-temp-text "* A <point>long headline :tag:"
  2581. (visual-line-mode)
  2582. (dotimes (i 1000) (insert "very "))
  2583. (goto-char (point-min))
  2584. (org-end-of-line)
  2585. (eolp)))
  2586. ;; At an headline without special movement, go to end of line.
  2587. ;; However, never go past ellipsis.
  2588. (should
  2589. (org-test-with-temp-text "* Headline2b :tag:\n"
  2590. (let ((org-special-ctrl-a/e nil))
  2591. (and (progn (org-end-of-line) (eolp))
  2592. (progn (org-end-of-line) (eolp))))))
  2593. (should
  2594. (org-test-with-temp-text "* Headline2b :tag:\n"
  2595. (let ((org-special-ctrl-a/e '(t . nil)))
  2596. (and (progn (org-end-of-line) (eolp))
  2597. (progn (org-end-of-line) (eolp))))))
  2598. (should
  2599. (org-test-with-temp-text "* Headline2a :tag:\n** Sub"
  2600. (org-overview)
  2601. (let ((org-special-ctrl-a/e nil))
  2602. (org-end-of-line)
  2603. (= 1 (line-beginning-position)))))
  2604. ;; At an headline with special movement, first move before tags,
  2605. ;; then at the end of line, rinse, repeat. However, never go past
  2606. ;; ellipsis.
  2607. (should
  2608. (org-test-with-temp-text "* Headline1 :tag:\n"
  2609. (let ((org-special-ctrl-a/e t))
  2610. (and (progn (org-end-of-line) (looking-at-p " :tag:"))
  2611. (progn (org-end-of-line) (eolp))
  2612. (progn (org-end-of-line) (looking-at-p " :tag:"))))))
  2613. (should
  2614. (org-test-with-temp-text "* Headline1 :tag:\n"
  2615. (let ((org-special-ctrl-a/e '(nil . t)))
  2616. (and (progn (org-end-of-line) (looking-at-p " :tag:"))
  2617. (progn (org-end-of-line) (eolp))
  2618. (progn (org-end-of-line) (looking-at-p " :tag:"))))))
  2619. (should-not
  2620. (org-test-with-temp-text "* Headline1 :tag:\n"
  2621. (let ((org-special-ctrl-a/e '(nil . nil)))
  2622. (and (progn (org-end-of-line) (looking-at-p " :tag:"))
  2623. (progn (org-end-of-line) (eolp))
  2624. (progn (org-end-of-line) (looking-at-p " :tag:"))))))
  2625. (should
  2626. (org-test-with-temp-text "* Headline2a :tag:\n** Sub"
  2627. (org-overview)
  2628. (let ((org-special-ctrl-a/e t))
  2629. (org-end-of-line)
  2630. (org-end-of-line)
  2631. (= 1 (line-beginning-position)))))
  2632. ;; At an headline, with reversed movement, first go to end of line,
  2633. ;; then before tags. However, never go past ellipsis.
  2634. (should
  2635. (org-test-with-temp-text "* Headline3 :tag:\n"
  2636. (let ((org-special-ctrl-a/e 'reversed)
  2637. (this-command last-command))
  2638. (and (progn (org-end-of-line) (eolp))
  2639. (progn (org-end-of-line) (looking-at-p " :tag:"))))))
  2640. (should
  2641. (org-test-with-temp-text "* Headline3 :tag:\n"
  2642. (let ((org-special-ctrl-a/e '(nil . reversed))
  2643. (this-command last-command))
  2644. (and (progn (org-end-of-line) (eolp))
  2645. (progn (org-end-of-line) (looking-at-p " :tag:"))))))
  2646. (should-not
  2647. (org-test-with-temp-text "* Headline3 :tag:\n"
  2648. (let ((org-special-ctrl-a/e '(nil . t))
  2649. (this-command last-command))
  2650. (and (progn (org-end-of-line) (eolp))
  2651. (progn (org-end-of-line) (looking-at-p " :tag:"))))))
  2652. (should
  2653. (org-test-with-temp-text "* Headline2a :tag:\n** Sub"
  2654. (org-overview)
  2655. (let ((org-special-ctrl-a/e 'reversed))
  2656. (org-end-of-line)
  2657. (= 1 (line-beginning-position)))))
  2658. ;; At a block without hidden contents.
  2659. (should
  2660. (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
  2661. (progn (org-end-of-line) (eolp))))
  2662. ;; At a block with hidden contents.
  2663. (should-not
  2664. (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
  2665. (let ((org-special-ctrl-a/e t))
  2666. (org-hide-block-toggle)
  2667. (org-end-of-line)
  2668. (eobp))))
  2669. ;; Get past invisible characters at the end of line.
  2670. (should
  2671. (org-test-with-temp-text "[[http://orgmode.org]]"
  2672. (org-end-of-line)
  2673. (eolp))))
  2674. (ert-deftest test-org/open-line ()
  2675. "Test `org-open-line' specifications."
  2676. ;; Call `open-line' outside of tables.
  2677. (should
  2678. (equal "\nText"
  2679. (org-test-with-temp-text "Text"
  2680. (org-open-line 1)
  2681. (buffer-string))))
  2682. ;; At a table, create a row above.
  2683. (should
  2684. (equal "\n| |\n| a |"
  2685. (org-test-with-temp-text "\n<point>| a |"
  2686. (org-open-line 1)
  2687. (buffer-string))))
  2688. ;; At the very first character of the buffer, also call `open-line'.
  2689. (should
  2690. (equal "\n| a |"
  2691. (org-test-with-temp-text "| a |"
  2692. (org-open-line 1)
  2693. (buffer-string))))
  2694. ;; Narrowing does not count.
  2695. (should
  2696. (equal "Text\n| |\n| a |"
  2697. (org-test-with-temp-text "Text\n<point>| a |"
  2698. (narrow-to-region (point) (point-max))
  2699. (org-open-line 1)
  2700. (widen)
  2701. (buffer-string)))))
  2702. (ert-deftest test-org/forward-sentence ()
  2703. "Test `org-forward-sentence' specifications."
  2704. ;; At the end of a table cell, move to the end of the next one.
  2705. (should
  2706. (org-test-with-temp-text "| a<point> | b |"
  2707. (org-forward-sentence)
  2708. (looking-at " |$")))
  2709. ;; Elsewhere in a cell, move to its end.
  2710. (should
  2711. (org-test-with-temp-text "| a<point>c | b |"
  2712. (org-forward-sentence)
  2713. (looking-at " | b |$")))
  2714. ;; Otherwise, simply call `forward-sentence'.
  2715. (should
  2716. (org-test-with-temp-text "Sentence<point> 1. Sentence 2."
  2717. (org-forward-sentence)
  2718. (looking-at " Sentence 2.")))
  2719. (should
  2720. (org-test-with-temp-text "Sentence<point> 1. Sentence 2."
  2721. (org-forward-sentence)
  2722. (org-forward-sentence)
  2723. (eobp)))
  2724. ;; At the end of an element, jump to the next one, without stopping
  2725. ;; on blank lines in-between.
  2726. (should
  2727. (org-test-with-temp-text "Paragraph 1.<point>\n\nParagraph 2."
  2728. (org-forward-sentence)
  2729. (eobp))))
  2730. (ert-deftest test-org/backward-sentence ()
  2731. "Test `org-backward-sentence' specifications."
  2732. ;; At the beginning of a table cell, move to the beginning of the
  2733. ;; previous one.
  2734. (should
  2735. (org-test-with-temp-text "| a | <point>b |"
  2736. (org-backward-sentence)
  2737. (looking-at "a | b |$")))
  2738. ;; Elsewhere in a cell, move to its beginning.
  2739. (should
  2740. (org-test-with-temp-text "| a | b<point>c |"
  2741. (org-backward-sentence)
  2742. (looking-at "bc |$")))
  2743. ;; Otherwise, simply call `backward-sentence'.
  2744. (should
  2745. (org-test-with-temp-text "Sentence 1. Sentence<point> 2."
  2746. (org-backward-sentence)
  2747. (looking-at "Sentence 2.")))
  2748. (should
  2749. (org-test-with-temp-text "Sentence 1. Sentence<point> 2."
  2750. (org-backward-sentence)
  2751. (org-backward-sentence)
  2752. (bobp)))
  2753. ;; Make sure to hit the beginning of a sentence on the same line as
  2754. ;; an item.
  2755. (should
  2756. (org-test-with-temp-text "- Line 1\n line <point>2."
  2757. (org-backward-sentence)
  2758. (looking-at "Line 1"))))
  2759. (ert-deftest test-org/forward-paragraph ()
  2760. "Test `org-forward-paragraph' specifications."
  2761. ;; At end of buffer, return an error.
  2762. (should-error
  2763. (org-test-with-temp-text "Paragraph"
  2764. (goto-char (point-max))
  2765. (org-forward-paragraph)))
  2766. ;; Standard test.
  2767. (should
  2768. (org-test-with-temp-text "P1\n\nP2\n\nP3"
  2769. (org-forward-paragraph)
  2770. (looking-at "P2")))
  2771. ;; Ignore depth.
  2772. (should
  2773. (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n#+END_CENTER\nP2"
  2774. (org-forward-paragraph)
  2775. (looking-at "P1")))
  2776. ;; Do not enter elements with invisible contents.
  2777. (should
  2778. (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n\nP2\n#+END_CENTER\nP3"
  2779. (org-hide-block-toggle)
  2780. (org-forward-paragraph)
  2781. (looking-at "P3")))
  2782. ;; On an affiliated keyword, jump to the beginning of the element.
  2783. (should
  2784. (org-test-with-temp-text "#+name: para\n#+caption: caption\nPara"
  2785. (org-forward-paragraph)
  2786. (looking-at "Para")))
  2787. ;; On an item or a footnote definition, move to the second element
  2788. ;; inside, if any.
  2789. (should
  2790. (org-test-with-temp-text "- Item1\n\n Paragraph\n- Item2"
  2791. (org-forward-paragraph)
  2792. (looking-at " Paragraph")))
  2793. (should
  2794. (org-test-with-temp-text "[fn:1] Def1\n\nParagraph\n\n[fn:2] Def2"
  2795. (org-forward-paragraph)
  2796. (looking-at "Paragraph")))
  2797. ;; On an item, or a footnote definition, when the first line is
  2798. ;; empty, move to the first item.
  2799. (should
  2800. (org-test-with-temp-text "- \n\n Paragraph\n- Item2"
  2801. (org-forward-paragraph)
  2802. (looking-at " Paragraph")))
  2803. (should
  2804. (org-test-with-temp-text "[fn:1]\n\nParagraph\n\n[fn:2] Def2"
  2805. (org-forward-paragraph)
  2806. (looking-at "Paragraph")))
  2807. ;; On a table (resp. a property drawer) do not move through table
  2808. ;; rows (resp. node properties).
  2809. (should
  2810. (org-test-with-temp-text "| a | b |\n| c | d |\nParagraph"
  2811. (org-forward-paragraph)
  2812. (looking-at "Paragraph")))
  2813. (should
  2814. (org-test-with-temp-text
  2815. "* H\n<point>:PROPERTIES:\n:prop: value\n:END:\nParagraph"
  2816. (org-forward-paragraph)
  2817. (looking-at "Paragraph")))
  2818. ;; On a verse or source block, stop after blank lines.
  2819. (should
  2820. (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n#+END_VERSE"
  2821. (org-forward-paragraph)
  2822. (looking-at "L2")))
  2823. (should
  2824. (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n#+END_SRC"
  2825. (org-forward-paragraph)
  2826. (looking-at "L2"))))
  2827. (ert-deftest test-org/backward-paragraph ()
  2828. "Test `org-backward-paragraph' specifications."
  2829. ;; Error at beginning of buffer.
  2830. (should-error
  2831. (org-test-with-temp-text "Paragraph"
  2832. (org-backward-paragraph)))
  2833. ;; Regular test.
  2834. (should
  2835. (org-test-with-temp-text "P1\n\nP2\n\nP3"
  2836. (goto-char (point-max))
  2837. (org-backward-paragraph)
  2838. (looking-at "P3")))
  2839. (should
  2840. (org-test-with-temp-text "P1\n\nP2\n\nP3"
  2841. (goto-char (point-max))
  2842. (beginning-of-line)
  2843. (org-backward-paragraph)
  2844. (looking-at "P2")))
  2845. ;; Ignore depth.
  2846. (should
  2847. (org-test-with-temp-text "P1\n\n#+BEGIN_CENTER\nP2\n#+END_CENTER\nP3"
  2848. (goto-char (point-max))
  2849. (beginning-of-line)
  2850. (org-backward-paragraph)
  2851. (looking-at "P2")))
  2852. ;; Ignore invisible elements.
  2853. (should
  2854. (org-test-with-temp-text "* H1\n P1\n* H2"
  2855. (org-cycle)
  2856. (goto-char (point-max))
  2857. (beginning-of-line)
  2858. (org-backward-paragraph)
  2859. (bobp)))
  2860. ;; On an affiliated keyword, jump to the first one.
  2861. (should
  2862. (org-test-with-temp-text "P1\n#+name: n\n#+caption: c1\n#+caption: c2\nP2"
  2863. (search-forward "c2")
  2864. (org-backward-paragraph)
  2865. (looking-at "#\\+name")))
  2866. ;; On the second element in an item or a footnote definition, jump
  2867. ;; to item or the definition.
  2868. (should
  2869. (org-test-with-temp-text "- line1\n\n line2"
  2870. (goto-char (point-max))
  2871. (beginning-of-line)
  2872. (org-backward-paragraph)
  2873. (looking-at "- line1")))
  2874. (should
  2875. (org-test-with-temp-text "[fn:1] line1\n\n line2"
  2876. (goto-char (point-max))
  2877. (beginning-of-line)
  2878. (org-backward-paragraph)
  2879. (looking-at "\\[fn:1\\] line1")))
  2880. ;; On a table (resp. a property drawer), ignore table rows
  2881. ;; (resp. node properties).
  2882. (should
  2883. (org-test-with-temp-text "| a | b |\n| c | d |\nP1"
  2884. (goto-char (point-max))
  2885. (beginning-of-line)
  2886. (org-backward-paragraph)
  2887. (bobp)))
  2888. (should
  2889. (org-test-with-temp-text "* H\n:PROPERTIES:\n:prop: value\n:END:\n<point>P1"
  2890. (org-backward-paragraph)
  2891. (looking-at ":PROPERTIES:")))
  2892. ;; On a source or verse block, stop before blank lines.
  2893. (should
  2894. (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n\nL3\n#+END_VERSE"
  2895. (search-forward "L3")
  2896. (beginning-of-line)
  2897. (org-backward-paragraph)
  2898. (looking-at "L2")))
  2899. (should
  2900. (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n\nL3#+END_SRC"
  2901. (search-forward "L3")
  2902. (beginning-of-line)
  2903. (org-backward-paragraph)
  2904. (looking-at "L2"))))
  2905. (ert-deftest test-org/forward-element ()
  2906. "Test `org-forward-element' specifications."
  2907. ;; 1. At EOB: should error.
  2908. (org-test-with-temp-text "Some text\n"
  2909. (goto-char (point-max))
  2910. (should-error (org-forward-element)))
  2911. ;; 2. Standard move: expected to ignore blank lines.
  2912. (org-test-with-temp-text "First paragraph.\n\n\nSecond paragraph."
  2913. (org-forward-element)
  2914. (should (looking-at (regexp-quote "Second paragraph."))))
  2915. ;; 3. Headline tests.
  2916. (org-test-with-temp-text "
  2917. * Head 1
  2918. ** Head 1.1
  2919. *** Head 1.1.1
  2920. ** Head 1.2"
  2921. ;; 3.1. At an headline beginning: move to next headline at the
  2922. ;; same level.
  2923. (goto-line 3)
  2924. (org-forward-element)
  2925. (should (looking-at (regexp-quote "** Head 1.2")))
  2926. ;; 3.2. At an headline beginning: move to parent headline if no
  2927. ;; headline at the same level.
  2928. (goto-line 3)
  2929. (org-forward-element)
  2930. (should (looking-at (regexp-quote "** Head 1.2"))))
  2931. ;; 4. Greater element tests.
  2932. (org-test-with-temp-text
  2933. "#+BEGIN_CENTER\nInside.\n#+END_CENTER\n\nOutside."
  2934. ;; 4.1. At a greater element: expected to skip contents.
  2935. (org-forward-element)
  2936. (should (looking-at (regexp-quote "Outside.")))
  2937. ;; 4.2. At the end of greater element contents: expected to skip
  2938. ;; to the end of the greater element.
  2939. (goto-line 2)
  2940. (org-forward-element)
  2941. (should (looking-at (regexp-quote "Outside."))))
  2942. ;; 5. List tests.
  2943. (org-test-with-temp-text "
  2944. - item1
  2945. - sub1
  2946. - sub2
  2947. - sub3
  2948. Inner paragraph.
  2949. - item2
  2950. Outside."
  2951. ;; 5.1. At list top point: expected to move to the element after
  2952. ;; the list.
  2953. (goto-line 2)
  2954. (org-forward-element)
  2955. (should (looking-at (regexp-quote "Outside.")))
  2956. ;; 5.2. Special case: at the first line of a sub-list, but not at
  2957. ;; beginning of line, move to next item.
  2958. (goto-line 2)
  2959. (forward-char)
  2960. (org-forward-element)
  2961. (should (looking-at "- item2"))
  2962. (goto-line 4)
  2963. (forward-char)
  2964. (org-forward-element)
  2965. (should (looking-at " - sub2"))
  2966. ;; 5.3 At sub-list beginning: expected to move after the sub-list.
  2967. (goto-line 4)
  2968. (org-forward-element)
  2969. (should (looking-at (regexp-quote " Inner paragraph.")))
  2970. ;; 5.4. At sub-list end: expected to move outside the sub-list.
  2971. (goto-line 8)
  2972. (org-forward-element)
  2973. (should (looking-at (regexp-quote " Inner paragraph.")))
  2974. ;; 5.5. At an item: expected to move to next item, if any.
  2975. (goto-line 6)
  2976. (org-forward-element)
  2977. (should (looking-at " - sub3"))))
  2978. (ert-deftest test-org/backward-element ()
  2979. "Test `org-backward-element' specifications."
  2980. ;; 1. Should error at BOB.
  2981. (org-test-with-temp-text " \nParagraph."
  2982. (should-error (org-backward-element)))
  2983. ;; 2. Should move at BOB when called on the first element in buffer.
  2984. (should
  2985. (org-test-with-temp-text "\n#+TITLE: test"
  2986. (progn (forward-line)
  2987. (org-backward-element)
  2988. (bobp))))
  2989. ;; 3. Not at the beginning of an element: move at its beginning.
  2990. (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
  2991. (goto-line 3)
  2992. (end-of-line)
  2993. (org-backward-element)
  2994. (should (looking-at (regexp-quote "Paragraph2."))))
  2995. ;; 4. Headline tests.
  2996. (org-test-with-temp-text "
  2997. * Head 1
  2998. ** Head 1.1
  2999. *** Head 1.1.1
  3000. ** Head 1.2"
  3001. ;; 4.1. At an headline beginning: move to previous headline at the
  3002. ;; same level.
  3003. (goto-line 5)
  3004. (org-backward-element)
  3005. (should (looking-at (regexp-quote "** Head 1.1")))
  3006. ;; 4.2. At an headline beginning: move to parent headline if no
  3007. ;; headline at the same level.
  3008. (goto-line 3)
  3009. (org-backward-element)
  3010. (should (looking-at (regexp-quote "* Head 1")))
  3011. ;; 4.3. At the first top-level headline: should error.
  3012. (goto-line 2)
  3013. (should-error (org-backward-element)))
  3014. ;; 5. At beginning of first element inside a greater element:
  3015. ;; expected to move to greater element's beginning.
  3016. (org-test-with-temp-text "Before.\n#+BEGIN_CENTER\nInside.\n#+END_CENTER"
  3017. (goto-line 3)
  3018. (org-backward-element)
  3019. (should (looking-at "#\\+BEGIN_CENTER")))
  3020. ;; 6. At the beginning of the first element in a section: should
  3021. ;; move back to headline, if any.
  3022. (should
  3023. (org-test-with-temp-text "#+TITLE: test\n* Headline\n\nParagraph"
  3024. (progn (goto-char (point-max))
  3025. (beginning-of-line)
  3026. (org-backward-element)
  3027. (org-at-heading-p))))
  3028. ;; 7. List tests.
  3029. (org-test-with-temp-text "
  3030. - item1
  3031. - sub1
  3032. - sub2
  3033. - sub3
  3034. Inner paragraph.
  3035. - item2
  3036. Outside."
  3037. ;; 7.1. At beginning of sub-list: expected to move to the
  3038. ;; paragraph before it.
  3039. (goto-line 4)
  3040. (org-backward-element)
  3041. (should (looking-at "item1"))
  3042. ;; 7.2. At an item in a list: expected to move at previous item.
  3043. (goto-line 8)
  3044. (org-backward-element)
  3045. (should (looking-at " - sub2"))
  3046. (goto-line 12)
  3047. (org-backward-element)
  3048. (should (looking-at "- item1"))
  3049. ;; 7.3. At end of list/sub-list: expected to move to list/sub-list
  3050. ;; beginning.
  3051. (goto-line 10)
  3052. (org-backward-element)
  3053. (should (looking-at " - sub1"))
  3054. (goto-line 15)
  3055. (org-backward-element)
  3056. (should (looking-at "- item1"))
  3057. ;; 7.4. At blank-lines before list end: expected to move to top
  3058. ;; item.
  3059. (goto-line 14)
  3060. (org-backward-element)
  3061. (should (looking-at "- item1"))))
  3062. (ert-deftest test-org/up-element ()
  3063. "Test `org-up-element' specifications."
  3064. ;; 1. At BOB or with no surrounding element: should error.
  3065. (org-test-with-temp-text "Paragraph."
  3066. (should-error (org-up-element)))
  3067. (org-test-with-temp-text "* Head1\n* Head2"
  3068. (goto-line 2)
  3069. (should-error (org-up-element)))
  3070. (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
  3071. (goto-line 3)
  3072. (should-error (org-up-element)))
  3073. ;; 2. At an headline: move to parent headline.
  3074. (org-test-with-temp-text "* Head1\n** Sub-Head1\n** Sub-Head2"
  3075. (goto-line 3)
  3076. (org-up-element)
  3077. (should (looking-at "\\* Head1")))
  3078. ;; 3. Inside a greater element: move to greater element beginning.
  3079. (org-test-with-temp-text
  3080. "Before.\n#+BEGIN_CENTER\nParagraph1\nParagraph2\n#+END_CENTER\n"
  3081. (goto-line 3)
  3082. (org-up-element)
  3083. (should (looking-at "#\\+BEGIN_CENTER")))
  3084. ;; 4. List tests.
  3085. (org-test-with-temp-text "* Top
  3086. - item1
  3087. - sub1
  3088. - sub2
  3089. Paragraph within sub2.
  3090. - item2"
  3091. ;; 4.1. Within an item: move to the item beginning.
  3092. (goto-line 8)
  3093. (org-up-element)
  3094. (should (looking-at " - sub2"))
  3095. ;; 4.2. At an item in a sub-list: move to parent item.
  3096. (goto-line 4)
  3097. (org-up-element)
  3098. (should (looking-at "- item1"))
  3099. ;; 4.3. At an item in top list: move to beginning of whole list.
  3100. (goto-line 10)
  3101. (org-up-element)
  3102. (should (looking-at "- item1"))
  3103. ;; 4.4. Special case. At very top point: should move to parent of
  3104. ;; list.
  3105. (goto-line 2)
  3106. (org-up-element)
  3107. (should (looking-at "\\* Top"))))
  3108. (ert-deftest test-org/down-element ()
  3109. "Test `org-down-element' specifications."
  3110. ;; Error when the element hasn't got a recursive type.
  3111. (org-test-with-temp-text "Paragraph."
  3112. (should-error (org-down-element)))
  3113. ;; Error when the element has no contents
  3114. (org-test-with-temp-text "* Headline"
  3115. (should-error (org-down-element)))
  3116. ;; When at a plain-list, move to first item.
  3117. (org-test-with-temp-text "- Item 1\n - Item 1.1\n - Item 2.2"
  3118. (goto-line 2)
  3119. (org-down-element)
  3120. (should (looking-at " - Item 1.1")))
  3121. (org-test-with-temp-text "#+NAME: list\n- Item 1"
  3122. (org-down-element)
  3123. (should (looking-at " Item 1")))
  3124. ;; When at a table, move to first row
  3125. (org-test-with-temp-text "#+NAME: table\n| a | b |"
  3126. (org-down-element)
  3127. (should (looking-at " a | b |")))
  3128. ;; Otherwise, move inside the greater element.
  3129. (org-test-with-temp-text "#+BEGIN_CENTER\nParagraph.\n#+END_CENTER"
  3130. (org-down-element)
  3131. (should (looking-at "Paragraph"))))
  3132. (ert-deftest test-org/drag-element-backward ()
  3133. "Test `org-drag-element-backward' specifications."
  3134. ;; Standard test.
  3135. (should
  3136. (equal
  3137. "#+key2: val2\n#+key1: val1\n#+key3: val3"
  3138. (org-test-with-temp-text "#+key1: val1\n<point>#+key2: val2\n#+key3: val3"
  3139. (org-drag-element-backward)
  3140. (buffer-string))))
  3141. (should
  3142. (equal
  3143. "#+BEGIN_CENTER\n#+B: 2\n#+A: 1\n#+END_CENTER"
  3144. (org-test-with-temp-text
  3145. "#+BEGIN_CENTER\n#+A: 1\n<point>#+B: 2\n#+END_CENTER"
  3146. (org-drag-element-backward)
  3147. (buffer-string))))
  3148. ;; Preserve blank lines.
  3149. (should
  3150. (equal "Paragraph 2\n\n\nPara1\n\nPara3"
  3151. (org-test-with-temp-text "Para1\n\n\n<point>Paragraph 2\n\nPara3"
  3152. (org-drag-element-backward)
  3153. (buffer-string))))
  3154. ;; Preserve column.
  3155. (should
  3156. (org-test-with-temp-text "#+key1: v\n#+key<point>2: v\n#+key3: v"
  3157. (org-drag-element-backward)
  3158. (looking-at-p "2")))
  3159. ;; Error when trying to move first element of buffer.
  3160. (should-error
  3161. (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
  3162. (org-drag-element-backward))
  3163. :type 'user-error)
  3164. ;; Error when trying to swap nested elements.
  3165. (should-error
  3166. (org-test-with-temp-text "#+BEGIN_CENTER\n<point>Test.\n#+END_CENTER"
  3167. (org-drag-element-backward))
  3168. :type 'user-error)
  3169. ;; Error when trying to swap an headline element and a non-headline
  3170. ;; element.
  3171. (should-error
  3172. (org-test-with-temp-text "Test.\n<point>* Head 1"
  3173. (org-drag-element-backward))
  3174. :type 'error)
  3175. ;; Error when called before first element.
  3176. (should-error
  3177. (org-test-with-temp-text "\n<point>"
  3178. (org-drag-element-backward))
  3179. :type 'user-error)
  3180. ;; Preserve visibility of elements and their contents.
  3181. (should
  3182. (equal '((63 . 82) (26 . 48))
  3183. (org-test-with-temp-text "
  3184. #+BEGIN_CENTER
  3185. Text.
  3186. #+END_CENTER
  3187. - item 1
  3188. #+BEGIN_QUOTE
  3189. Text.
  3190. #+END_QUOTE"
  3191. (while (search-forward "BEGIN_" nil t) (org-cycle))
  3192. (search-backward "- item 1")
  3193. (org-drag-element-backward)
  3194. (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
  3195. (overlays-in (point-min) (point-max))))))
  3196. ;; Pathological case: handle call with point in blank lines right
  3197. ;; after a headline.
  3198. (should
  3199. (equal "* H2\n* H1\nText\n\n"
  3200. (org-test-with-temp-text "* H1\nText\n* H2\n\n<point>"
  3201. (org-drag-element-backward)
  3202. (buffer-string)))))
  3203. (ert-deftest test-org/drag-element-forward ()
  3204. "Test `org-drag-element-forward' specifications."
  3205. ;; 1. Error when trying to move first element of buffer.
  3206. (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
  3207. (goto-line 3)
  3208. (should-error (org-drag-element-forward)))
  3209. ;; 2. Error when trying to swap nested elements.
  3210. (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
  3211. (forward-line)
  3212. (should-error (org-drag-element-forward)))
  3213. ;; 3. Error when trying to swap a non-headline element and an
  3214. ;; headline.
  3215. (org-test-with-temp-text "Test.\n* Head 1"
  3216. (should-error (org-drag-element-forward)))
  3217. ;; 4. Error when called before first element.
  3218. (should-error
  3219. (org-test-with-temp-text "\n"
  3220. (forward-line)
  3221. (org-drag-element-backward))
  3222. :type 'user-error)
  3223. ;; 5. Otherwise, swap elements, preserving column and blank lines
  3224. ;; between elements.
  3225. (org-test-with-temp-text "Paragraph 1\n\n\nPara2\n\nPara3"
  3226. (search-forward "graph")
  3227. (org-drag-element-forward)
  3228. (should (equal (buffer-string) "Para2\n\n\nParagraph 1\n\nPara3"))
  3229. (should (looking-at " 1")))
  3230. ;; 5. Preserve visibility of elements and their contents.
  3231. (org-test-with-temp-text "
  3232. #+BEGIN_CENTER
  3233. Text.
  3234. #+END_CENTER
  3235. - item 1
  3236. #+BEGIN_QUOTE
  3237. Text.
  3238. #+END_QUOTE"
  3239. (while (search-forward "BEGIN_" nil t) (org-cycle))
  3240. (search-backward "#+BEGIN_CENTER")
  3241. (org-drag-element-forward)
  3242. (should
  3243. (equal
  3244. '((63 . 82) (26 . 48))
  3245. (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
  3246. (overlays-in (point-min) (point-max)))))))
  3247. (ert-deftest test-org/next-block ()
  3248. "Test `org-next-block' specifications."
  3249. ;; Regular test.
  3250. (should
  3251. (org-test-with-temp-text "Paragraph\n#+BEGIN_CENTER\ncontents\n#+END_CENTER"
  3252. (org-next-block 1)
  3253. (looking-at "#\\+BEGIN_CENTER")))
  3254. ;; Ignore case.
  3255. (should
  3256. (org-test-with-temp-text "Paragraph\n#+begin_center\ncontents\n#+end_center"
  3257. (let ((case-fold-search nil))
  3258. (org-next-block 1)
  3259. (looking-at "#\\+begin_center"))))
  3260. ;; Ignore current line.
  3261. (should
  3262. (org-test-with-temp-text
  3263. "#+BEGIN_QUOTE\n#+END_QUOTE\n#+BEGIN_CENTER\n#+END_CENTER"
  3264. (org-next-block 1)
  3265. (looking-at "#\\+BEGIN_CENTER")))
  3266. ;; Throw an error when no block is found.
  3267. (should-error
  3268. (org-test-with-temp-text "Paragraph"
  3269. (org-next-block 1)))
  3270. ;; With an argument, skip many blocks at once.
  3271. (should
  3272. (org-test-with-temp-text
  3273. "Start\n#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE"
  3274. (org-next-block 2)
  3275. (looking-at "#\\+BEGIN_QUOTE")))
  3276. ;; With optional argument BLOCK-REGEXP, filter matched blocks.
  3277. (should
  3278. (org-test-with-temp-text
  3279. "Start\n#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE"
  3280. (org-next-block 1 nil "^[ \t]*#\\+BEGIN_QUOTE")
  3281. (looking-at "#\\+BEGIN_QUOTE")))
  3282. ;; Optional argument is also case-insensitive.
  3283. (should
  3284. (org-test-with-temp-text
  3285. "Start\n#+BEGIN_CENTER\nA\n#+END_CENTER\n#+begin_quote\nB\n#+end_quote"
  3286. (let ((case-fold-search nil))
  3287. (org-next-block 1 nil "^[ \t]*#\\+BEGIN_QUOTE")
  3288. (looking-at "#\\+begin_quote")))))
  3289. (ert-deftest test-org/previous-block ()
  3290. "Test `org-previous-block' specifications."
  3291. ;; Regular test.
  3292. (should
  3293. (org-test-with-temp-text "#+BEGIN_CENTER\ncontents\n#+END_CENTER\n<point>"
  3294. (org-previous-block 1)
  3295. (looking-at "#\\+BEGIN_CENTER")))
  3296. ;; Ignore case.
  3297. (should
  3298. (org-test-with-temp-text "#+begin_center\ncontents\n#+end_center\n<point>"
  3299. (let ((case-fold-search nil))
  3300. (org-previous-block 1)
  3301. (looking-at "#\\+begin_center"))))
  3302. ;; Ignore current line.
  3303. (should
  3304. (org-test-with-temp-text
  3305. "#+BEGIN_QUOTE\n#+END_QUOTE\n#+BEGIN_CENTER<point>\n#+END_CENTER"
  3306. (org-previous-block 1)
  3307. (looking-at "#\\+BEGIN_QUOTE")))
  3308. ;; Throw an error when no block is found.
  3309. (should-error
  3310. (org-test-with-temp-text "Paragraph<point>"
  3311. (org-previous-block 1)))
  3312. ;; With an argument, skip many blocks at once.
  3313. (should
  3314. (org-test-with-temp-text
  3315. "#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE\n<point>"
  3316. (org-previous-block 2)
  3317. (looking-at "#\\+BEGIN_CENTER")))
  3318. ;; With optional argument BLOCK-REGEXP, filter matched blocks.
  3319. (should
  3320. (org-test-with-temp-text
  3321. "#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE\n<point>"
  3322. (org-previous-block 1 "^[ \t]*#\\+BEGIN_QUOTE")
  3323. (looking-at "#\\+BEGIN_QUOTE")))
  3324. ;; Optional argument is also case-insensitive.
  3325. (should
  3326. (org-test-with-temp-text
  3327. "#+BEGIN_CENTER\nA\n#+END_CENTER\n#+begin_quote\nB\n#+end_quote\n<point>"
  3328. (let ((case-fold-search nil))
  3329. (org-next-block 1 "^[ \t]*#\\+BEGIN_QUOTE")
  3330. (looking-at "#\\+begin_quote")))))
  3331. ;;; Outline structure
  3332. (ert-deftest test-org/demote ()
  3333. "Test `org-demote' specifications."
  3334. ;; Add correct number of stars according to `org-odd-levels-only'.
  3335. (should
  3336. (= 2
  3337. (org-test-with-temp-text "* H"
  3338. (let ((org-odd-levels-only nil)) (org-demote))
  3339. (org-current-level))))
  3340. (should
  3341. (= 3
  3342. (org-test-with-temp-text "* H"
  3343. (let ((org-odd-levels-only t)) (org-demote))
  3344. (org-current-level))))
  3345. ;; When `org-auto-align-tags' is non-nil, move tags accordingly.
  3346. (should
  3347. (org-test-with-temp-text "* H :tag:"
  3348. (let ((org-tags-column 10)
  3349. (org-auto-align-tags t)
  3350. (org-odd-levels-only nil))
  3351. (org-demote))
  3352. (org-move-to-column 10)
  3353. (looking-at-p ":tag:$")))
  3354. (should-not
  3355. (org-test-with-temp-text "* H :tag:"
  3356. (let ((org-tags-column 10)
  3357. (org-auto-align-tags nil)
  3358. (org-odd-levels-only nil))
  3359. (org-demote))
  3360. (org-move-to-column 10)
  3361. (looking-at-p ":tag:$")))
  3362. ;; When `org-adapt-indentation' is non-nil, always indent planning
  3363. ;; info and property drawers accordingly.
  3364. (should
  3365. (= 3
  3366. (org-test-with-temp-text "* H\n SCHEDULED: <2014-03-04 tue.>"
  3367. (let ((org-odd-levels-only nil)
  3368. (org-adapt-indentation t))
  3369. (org-demote))
  3370. (forward-line)
  3371. (org-get-indentation))))
  3372. (should
  3373. (= 3
  3374. (org-test-with-temp-text "* H\n :PROPERTIES:\n :FOO: Bar\n :END:"
  3375. (let ((org-odd-levels-only nil)
  3376. (org-adapt-indentation t))
  3377. (org-demote))
  3378. (forward-line)
  3379. (org-get-indentation))))
  3380. (should-not
  3381. (= 3
  3382. (org-test-with-temp-text "* H\n SCHEDULED: <2014-03-04 tue.>"
  3383. (let ((org-odd-levels-only nil)
  3384. (org-adapt-indentation nil))
  3385. (org-demote))
  3386. (forward-line)
  3387. (org-get-indentation))))
  3388. ;; When `org-adapt-indentation' is non-nil, shift all lines in
  3389. ;; section accordingly. Ignore, however, footnote definitions and
  3390. ;; inlinetasks boundaries.
  3391. (should
  3392. (= 3
  3393. (org-test-with-temp-text "* H\n Paragraph"
  3394. (let ((org-odd-levels-only nil)
  3395. (org-adapt-indentation t))
  3396. (org-demote))
  3397. (forward-line)
  3398. (org-get-indentation))))
  3399. (should
  3400. (= 2
  3401. (org-test-with-temp-text "* H\n Paragraph"
  3402. (let ((org-odd-levels-only nil)
  3403. (org-adapt-indentation nil))
  3404. (org-demote))
  3405. (forward-line)
  3406. (org-get-indentation))))
  3407. (should
  3408. (zerop
  3409. (org-test-with-temp-text "* H\n[fn:1] def line 1\ndef line 2"
  3410. (let ((org-odd-levels-only nil)
  3411. (org-adapt-indentation t))
  3412. (org-demote))
  3413. (goto-char (point-max))
  3414. (org-get-indentation))))
  3415. (should
  3416. (= 3
  3417. (org-test-with-temp-text "* H\n[fn:1] Def.\n\n\n After def."
  3418. (let ((org-odd-levels-only nil)
  3419. (org-adapt-indentation t))
  3420. (org-demote))
  3421. (goto-char (point-max))
  3422. (org-get-indentation))))
  3423. (when (featurep 'org-inlinetask)
  3424. (should
  3425. (zerop
  3426. (let ((org-inlinetask-min-level 5)
  3427. (org-adapt-indentation t))
  3428. (org-test-with-temp-text "* H\n***** I\n***** END"
  3429. (org-demote)
  3430. (forward-line)
  3431. (org-get-indentation))))))
  3432. (when (featurep 'org-inlinetask)
  3433. (should
  3434. (= 3
  3435. (let ((org-inlinetask-min-level 5)
  3436. (org-adapt-indentation t))
  3437. (org-test-with-temp-text "* H\n***** I\n Contents\n***** END"
  3438. (org-demote)
  3439. (forward-line 2)
  3440. (org-get-indentation))))))
  3441. ;; Ignore contents of source blocks or example blocks when
  3442. ;; indentation should be preserved (through
  3443. ;; `org-src-preserve-indentation' or "-i" flag).
  3444. (should-not
  3445. (zerop
  3446. (org-test-with-temp-text "* H\n#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  3447. (let ((org-adapt-indentation t)
  3448. (org-src-preserve-indentation nil))
  3449. (org-demote))
  3450. (forward-line 2)
  3451. (org-get-indentation))))
  3452. (should
  3453. (zerop
  3454. (org-test-with-temp-text "* H\n#+BEGIN_EXAMPLE\n(+ 1 1)\n#+END_EXAMPLE"
  3455. (let ((org-adapt-indentation t)
  3456. (org-src-preserve-indentation t))
  3457. (org-demote))
  3458. (forward-line 2)
  3459. (org-get-indentation))))
  3460. (should
  3461. (zerop
  3462. (org-test-with-temp-text "* H\n#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  3463. (let ((org-adapt-indentation t)
  3464. (org-src-preserve-indentation t))
  3465. (org-demote))
  3466. (forward-line 2)
  3467. (org-get-indentation))))
  3468. (should
  3469. (zerop
  3470. (org-test-with-temp-text
  3471. "* H\n#+BEGIN_SRC emacs-lisp -i\n(+ 1 1)\n#+END_SRC"
  3472. (let ((org-adapt-indentation t)
  3473. (org-src-preserve-indentation nil))
  3474. (org-demote))
  3475. (forward-line 2)
  3476. (org-get-indentation)))))
  3477. (ert-deftest test-org/promote ()
  3478. "Test `org-promote' specifications."
  3479. ;; Return an error if headline is to be promoted to level 0, unless
  3480. ;; `org-allow-promoting-top-level-subtree' is non-nil, in which case
  3481. ;; headline becomes a comment.
  3482. (should-error
  3483. (org-test-with-temp-text "* H"
  3484. (let ((org-allow-promoting-top-level-subtree nil)) (org-promote))))
  3485. (should
  3486. (equal "# H"
  3487. (org-test-with-temp-text "* H"
  3488. (let ((org-allow-promoting-top-level-subtree t)) (org-promote))
  3489. (buffer-string))))
  3490. ;; Remove correct number of stars according to
  3491. ;; `org-odd-levels-only'.
  3492. (should
  3493. (= 2
  3494. (org-test-with-temp-text "*** H"
  3495. (let ((org-odd-levels-only nil)) (org-promote))
  3496. (org-current-level))))
  3497. (should
  3498. (= 1
  3499. (org-test-with-temp-text "*** H"
  3500. (let ((org-odd-levels-only t)) (org-promote))
  3501. (org-current-level))))
  3502. ;; When `org-auto-align-tags' is non-nil, move tags accordingly.
  3503. (should
  3504. (org-test-with-temp-text "** H :tag:"
  3505. (let ((org-tags-column 10)
  3506. (org-auto-align-tags t)
  3507. (org-odd-levels-only nil))
  3508. (org-promote))
  3509. (org-move-to-column 10)
  3510. (looking-at-p ":tag:$")))
  3511. (should-not
  3512. (org-test-with-temp-text "** H :tag:"
  3513. (let ((org-tags-column 10)
  3514. (org-auto-align-tags nil)
  3515. (org-odd-levels-only nil))
  3516. (org-promote))
  3517. (org-move-to-column 10)
  3518. (looking-at-p ":tag:$")))
  3519. ;; When `org-adapt-indentation' is non-nil, always indent planning
  3520. ;; info and property drawers.
  3521. (should
  3522. (= 2
  3523. (org-test-with-temp-text "** H\n SCHEDULED: <2014-03-04 tue.>"
  3524. (let ((org-odd-levels-only nil)
  3525. (org-adapt-indentation t))
  3526. (org-promote))
  3527. (forward-line)
  3528. (org-get-indentation))))
  3529. (should
  3530. (= 2
  3531. (org-test-with-temp-text "** H\n :PROPERTIES:\n :FOO: Bar\n :END:"
  3532. (let ((org-odd-levels-only nil)
  3533. (org-adapt-indentation t))
  3534. (org-promote))
  3535. (forward-line)
  3536. (org-get-indentation))))
  3537. (should-not
  3538. (= 2
  3539. (org-test-with-temp-text "** H\n SCHEDULED: <2014-03-04 tue.>"
  3540. (let ((org-odd-levels-only nil)
  3541. (org-adapt-indentation nil))
  3542. (org-promote))
  3543. (forward-line)
  3544. (org-get-indentation))))
  3545. ;; When `org-adapt-indentation' is non-nil, shift all lines in
  3546. ;; section accordingly. Ignore, however, footnote definitions and
  3547. ;; inlinetasks boundaries.
  3548. (should
  3549. (= 2
  3550. (org-test-with-temp-text "** H\n Paragraph"
  3551. (let ((org-odd-levels-only nil)
  3552. (org-adapt-indentation t))
  3553. (org-promote))
  3554. (forward-line)
  3555. (org-get-indentation))))
  3556. (should-not
  3557. (= 2
  3558. (org-test-with-temp-text "** H\n Paragraph"
  3559. (let ((org-odd-levels-only nil)
  3560. (org-adapt-indentation nil))
  3561. (org-promote))
  3562. (forward-line)
  3563. (org-get-indentation))))
  3564. (should
  3565. (= 2
  3566. (org-test-with-temp-text "** H\n Paragraph\n[fn:1] line1\nline2"
  3567. (let ((org-odd-levels-only nil)
  3568. (org-adapt-indentation t))
  3569. (org-promote))
  3570. (forward-line)
  3571. (org-get-indentation))))
  3572. (when (featurep 'org-inlinetask)
  3573. (should
  3574. (zerop
  3575. (let ((org-inlinetask-min-level 5)
  3576. (org-adapt-indentation t))
  3577. (org-test-with-temp-text "** H\n***** I\n***** END"
  3578. (org-promote)
  3579. (forward-line)
  3580. (org-get-indentation))))))
  3581. (when (featurep 'org-inlinetask)
  3582. (should
  3583. (= 2
  3584. (let ((org-inlinetask-min-level 5)
  3585. (org-adapt-indentation t))
  3586. (org-test-with-temp-text "** H\n***** I\n Contents\n***** END"
  3587. (org-promote)
  3588. (forward-line 2)
  3589. (org-get-indentation))))))
  3590. ;; Give up shifting if it would break document's structure
  3591. ;; otherwise.
  3592. (should
  3593. (= 3
  3594. (org-test-with-temp-text "** H\n Paragraph\n [fn:1] Def."
  3595. (let ((org-odd-levels-only nil)
  3596. (org-adapt-indentation t))
  3597. (org-promote))
  3598. (forward-line)
  3599. (org-get-indentation))))
  3600. (should
  3601. (= 3
  3602. (org-test-with-temp-text "** H\n Paragraph\n * list."
  3603. (let ((org-odd-levels-only nil)
  3604. (org-adapt-indentation t))
  3605. (org-promote))
  3606. (forward-line)
  3607. (org-get-indentation))))
  3608. ;; Ignore contents of source blocks or example blocks when
  3609. ;; indentation should be preserved (through
  3610. ;; `org-src-preserve-indentation' or "-i" flag).
  3611. (should-not
  3612. (zerop
  3613. (org-test-with-temp-text
  3614. "** H\n #+BEGIN_SRC emacs-lisp\n(+ 1 1)\n #+END_SRC"
  3615. (let ((org-adapt-indentation t)
  3616. (org-src-preserve-indentation nil)
  3617. (org-odd-levels-only nil))
  3618. (org-promote))
  3619. (forward-line)
  3620. (org-get-indentation))))
  3621. (should
  3622. (zerop
  3623. (org-test-with-temp-text
  3624. "** H\n #+BEGIN_EXAMPLE\nContents\n #+END_EXAMPLE"
  3625. (let ((org-adapt-indentation t)
  3626. (org-src-preserve-indentation t)
  3627. (org-odd-levels-only nil))
  3628. (org-promote))
  3629. (forward-line)
  3630. (org-get-indentation))))
  3631. (should
  3632. (zerop
  3633. (org-test-with-temp-text
  3634. "** H\n #+BEGIN_SRC emacs-lisp\n(+ 1 1)\n #+END_SRC"
  3635. (let ((org-adapt-indentation t)
  3636. (org-src-preserve-indentation t)
  3637. (org-odd-levels-only nil))
  3638. (org-promote))
  3639. (forward-line)
  3640. (org-get-indentation))))
  3641. (should
  3642. (zerop
  3643. (org-test-with-temp-text
  3644. "** H\n #+BEGIN_SRC emacs-lisp -i\n(+ 1 1)\n #+END_SRC"
  3645. (let ((org-adapt-indentation t)
  3646. (org-src-preserve-indentation nil)
  3647. (org-odd-levels-only nil))
  3648. (org-promote))
  3649. (forward-line)
  3650. (org-get-indentation)))))
  3651. (ert-deftest test-org/org-get-valid-level ()
  3652. "Test function `org-get-valid-level' specifications."
  3653. (let ((org-odd-levels-only nil))
  3654. (should (equal 1 (org-get-valid-level 0 0)))
  3655. (should (equal 1 (org-get-valid-level 0 1)))
  3656. (should (equal 2 (org-get-valid-level 0 2)))
  3657. (should (equal 3 (org-get-valid-level 0 3)))
  3658. (should (equal 1 (org-get-valid-level 1 0)))
  3659. (should (equal 2 (org-get-valid-level 1 1)))
  3660. (should (equal 23 (org-get-valid-level 1 22)))
  3661. (should (equal 1 (org-get-valid-level 1 -1)))
  3662. (should (equal 1 (org-get-valid-level 2 -1))))
  3663. (let ((org-odd-levels-only t))
  3664. (should (equal 1 (org-get-valid-level 0 0)))
  3665. (should (equal 1 (org-get-valid-level 0 1)))
  3666. (should (equal 3 (org-get-valid-level 0 2)))
  3667. (should (equal 5 (org-get-valid-level 0 3)))
  3668. (should (equal 1 (org-get-valid-level 1 0)))
  3669. (should (equal 3 (org-get-valid-level 1 1)))
  3670. (should (equal 3 (org-get-valid-level 2 1)))
  3671. (should (equal 5 (org-get-valid-level 3 1)))
  3672. (should (equal 5 (org-get-valid-level 4 1)))
  3673. (should (equal 43 (org-get-valid-level 1 21)))
  3674. (should (equal 1 (org-get-valid-level 1 -1)))
  3675. (should (equal 1 (org-get-valid-level 2 -1)))
  3676. (should (equal 1 (org-get-valid-level 3 -1)))
  3677. (should (equal 3 (org-get-valid-level 4 -1)))
  3678. (should (equal 3 (org-get-valid-level 5 -1)))))
  3679. ;;; Planning
  3680. (ert-deftest test-org/at-planning-p ()
  3681. "Test `org-at-planning-p' specifications."
  3682. ;; Regular test.
  3683. (should
  3684. (org-test-with-temp-text "* Headline\n<point>DEADLINE: <2014-03-04 tue.>"
  3685. (org-at-planning-p)))
  3686. (should-not
  3687. (org-test-with-temp-text "DEADLINE: <2014-03-04 tue.>"
  3688. (org-at-planning-p)))
  3689. ;; Correctly find planning attached to inlinetasks.
  3690. (when (featurep 'org-inlinetask)
  3691. (should
  3692. (org-test-with-temp-text
  3693. "*** Inlinetask\n<point>DEADLINE: <2014-03-04 tue.>\n*** END"
  3694. (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))
  3695. (should-not
  3696. (org-test-with-temp-text
  3697. "*** Inlinetask\n<point>DEADLINE: <2014-03-04 tue.>"
  3698. (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))
  3699. (should-not
  3700. (org-test-with-temp-text
  3701. "* Headline\n*** Inlinetask\n<point>DEADLINE: <2014-03-04 tue.>"
  3702. (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))
  3703. (should-not
  3704. (org-test-with-temp-text
  3705. "* Headline\n*** Inlinetask\n*** END\n<point>DEADLINE: <2014-03-04 tue.>"
  3706. (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))))
  3707. (ert-deftest test-org/add-planning-info ()
  3708. "Test `org-add-planning-info'."
  3709. ;; Create deadline when `org-adapt-indentation' is non-nil.
  3710. (should
  3711. (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
  3712. (org-test-with-temp-text "* H\nParagraph<point>"
  3713. (let ((org-adapt-indentation t))
  3714. (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
  3715. (replace-regexp-in-string
  3716. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  3717. nil nil 1))))
  3718. ;; Create deadline when `org-adapt-indentation' is nil.
  3719. (should
  3720. (equal "* H\nDEADLINE: <2015-06-25>\nParagraph"
  3721. (org-test-with-temp-text "* H\nParagraph<point>"
  3722. (let ((org-adapt-indentation nil))
  3723. (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
  3724. (replace-regexp-in-string
  3725. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  3726. nil nil 1))))
  3727. ;; Update deadline when `org-adapt-indentation' is non-nil.
  3728. (should
  3729. (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
  3730. (org-test-with-temp-text "\
  3731. * H
  3732. DEADLINE: <2015-06-24 Wed>
  3733. Paragraph<point>"
  3734. (let ((org-adapt-indentation t))
  3735. (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
  3736. (replace-regexp-in-string
  3737. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  3738. nil nil 1))))
  3739. ;; Update deadline when `org-adapt-indentation' is nil.
  3740. (should
  3741. (equal "* H\nDEADLINE: <2015-06-25>\nParagraph"
  3742. (org-test-with-temp-text "\
  3743. * H
  3744. DEADLINE: <2015-06-24 Wed>
  3745. Paragraph<point>"
  3746. (let ((org-adapt-indentation nil))
  3747. (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
  3748. (replace-regexp-in-string
  3749. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  3750. nil nil 1))))
  3751. ;; Schedule when `org-adapt-indentation' is non-nil.
  3752. (should
  3753. (equal "* H\n SCHEDULED: <2015-06-25>\nParagraph"
  3754. (org-test-with-temp-text "* H\nParagraph<point>"
  3755. (let ((org-adapt-indentation t))
  3756. (org-add-planning-info 'scheduled "<2015-06-25 Thu>"))
  3757. (replace-regexp-in-string
  3758. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  3759. nil nil 1))))
  3760. ;; Schedule when `org-adapt-indentation' is nil.
  3761. (should
  3762. (equal "* H\nSCHEDULED: <2015-06-25>\nParagraph"
  3763. (org-test-with-temp-text "* H\nParagraph<point>"
  3764. (let ((org-adapt-indentation nil))
  3765. (org-add-planning-info 'scheduled "<2015-06-25 Thu>"))
  3766. (replace-regexp-in-string
  3767. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  3768. nil nil 1))))
  3769. ;; Add deadline when scheduled.
  3770. (should
  3771. (equal "\
  3772. * H
  3773. DEADLINE: <2015-06-25> SCHEDULED: <2015-06-24>
  3774. Paragraph"
  3775. (org-test-with-temp-text "\
  3776. * H
  3777. SCHEDULED: <2015-06-24 Wed>
  3778. Paragraph<point>"
  3779. (let ((org-adapt-indentation t))
  3780. (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
  3781. (replace-regexp-in-string
  3782. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  3783. nil nil 1))))
  3784. ;; Remove middle entry.
  3785. (should
  3786. (equal "\
  3787. * H
  3788. CLOSED: [2015-06-24] SCHEDULED: <2015-06-24>
  3789. Paragraph"
  3790. (org-test-with-temp-text "\
  3791. * H
  3792. CLOSED: [2015-06-24 Wed] DEADLINE: <2015-06-25 Thu> SCHEDULED: <2015-06-24 Wed>
  3793. Paragraph<point>"
  3794. (let ((org-adapt-indentation t))
  3795. (org-add-planning-info nil nil 'deadline))
  3796. (replace-regexp-in-string
  3797. "\\( [.A-Za-z]+\\)[]>]" "" (buffer-string)
  3798. nil nil 1))))
  3799. ;; Remove last entry and then middle entry (order should not
  3800. ;; matter).
  3801. (should
  3802. (equal "\
  3803. * H
  3804. CLOSED: [2015-06-24]
  3805. Paragraph"
  3806. (org-test-with-temp-text "\
  3807. * H
  3808. CLOSED: [2015-06-24 Wed] DEADLINE: <2015-06-25 Thu> SCHEDULED: <2015-06-24 Wed>
  3809. Paragraph<point>"
  3810. (let ((org-adapt-indentation t))
  3811. (org-add-planning-info nil nil 'scheduled 'deadline))
  3812. (replace-regexp-in-string
  3813. "\\( [.A-Za-z]+\\)[]>]" "" (buffer-string)
  3814. nil nil 1))))
  3815. ;; Remove closed when `org-adapt-indentation' is non-nil.
  3816. (should
  3817. (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
  3818. (org-test-with-temp-text "\
  3819. * H
  3820. CLOSED: [2015-06-25 Thu] DEADLINE: <2015-06-25 Thu>
  3821. Paragraph<point>"
  3822. (let ((org-adapt-indentation t))
  3823. (org-add-planning-info nil nil 'closed))
  3824. (replace-regexp-in-string
  3825. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  3826. nil nil 1))))
  3827. (should
  3828. (equal "* H\n Paragraph"
  3829. (org-test-with-temp-text "\
  3830. * H
  3831. CLOSED: [2015-06-25 Thu]
  3832. Paragraph<point>"
  3833. (let ((org-adapt-indentation t))
  3834. (org-add-planning-info nil nil 'closed))
  3835. (replace-regexp-in-string
  3836. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  3837. nil nil 1))))
  3838. ;; Remove closed when `org-adapt-indentation' is nil.
  3839. (should
  3840. (equal "* H\nDEADLINE: <2015-06-25>\nParagraph"
  3841. (org-test-with-temp-text "\
  3842. * H
  3843. CLOSED: [2015-06-25 Thu] DEADLINE: <2015-06-25 Thu>
  3844. Paragraph<point>"
  3845. (let ((org-adapt-indentation nil))
  3846. (org-add-planning-info nil nil 'closed))
  3847. (replace-regexp-in-string
  3848. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  3849. nil nil 1))))
  3850. (should
  3851. (equal "* H\nParagraph"
  3852. (org-test-with-temp-text "\
  3853. * H
  3854. CLOSED: [2015-06-25 Thu]
  3855. Paragraph<point>"
  3856. (let ((org-adapt-indentation nil))
  3857. (org-add-planning-info nil nil 'closed))
  3858. (replace-regexp-in-string
  3859. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  3860. nil nil 1))))
  3861. ;; Remove closed entry and delete empty line.
  3862. (should
  3863. (equal "\
  3864. * H
  3865. Paragraph"
  3866. (org-test-with-temp-text "\
  3867. * H
  3868. CLOSED: [2015-06-24 Wed]
  3869. Paragraph<point>"
  3870. (let ((org-adapt-indentation t))
  3871. (org-add-planning-info nil nil 'closed))
  3872. (replace-regexp-in-string
  3873. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  3874. nil nil 1))))
  3875. ;; Remove one entry and update another.
  3876. (should
  3877. (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
  3878. (org-test-with-temp-text "\
  3879. * H
  3880. SCHEDULED: <2015-06-23 Tue> DEADLINE: <2015-06-24 Wed>
  3881. Paragraph<point>"
  3882. (let ((org-adapt-indentation t))
  3883. (org-add-planning-info 'deadline "<2015-06-25 Thu>" 'scheduled))
  3884. (replace-regexp-in-string
  3885. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  3886. nil nil 1)))))
  3887. ;;; Property API
  3888. (ert-deftest test-org/buffer-property-keys ()
  3889. "Test `org-buffer-property-keys' specifications."
  3890. ;; Retrieve properties accross siblings.
  3891. (should
  3892. (equal '("A" "B")
  3893. (org-test-with-temp-text "
  3894. * H1
  3895. :PROPERTIES:
  3896. :A: 1
  3897. :END:
  3898. * H2
  3899. :PROPERTIES:
  3900. :B: 1
  3901. :END:"
  3902. (org-buffer-property-keys))))
  3903. ;; Retrieve properties accross children.
  3904. (should
  3905. (equal '("A" "B")
  3906. (org-test-with-temp-text "
  3907. * H1
  3908. :PROPERTIES:
  3909. :A: 1
  3910. :END:
  3911. ** H2
  3912. :PROPERTIES:
  3913. :B: 1
  3914. :END:"
  3915. (org-buffer-property-keys))))
  3916. ;; Retrieve muliple properties in the same drawer.
  3917. (should
  3918. (equal '("A" "B")
  3919. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
  3920. (org-buffer-property-keys))))
  3921. ;; Ignore extension symbol in property name.
  3922. (should
  3923. (equal '("A")
  3924. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:END:"
  3925. (org-buffer-property-keys))))
  3926. ;; With non-nil COLUMNS, extract property names from columns.
  3927. (should
  3928. (equal '("A" "B")
  3929. (org-test-with-temp-text "#+COLUMNS: %25ITEM %A %20B"
  3930. (org-buffer-property-keys nil nil t))))
  3931. (should
  3932. (equal '("A" "B" "COLUMNS")
  3933. (org-test-with-temp-text
  3934. "* H\n:PROPERTIES:\n:COLUMNS: %25ITEM %A %20B\n:END:"
  3935. (org-buffer-property-keys nil nil t))))
  3936. ;; With non-nil IGNORE-MALFORMED malformed property drawers are silently ignored.
  3937. (should
  3938. (equal '("A")
  3939. (org-test-with-temp-text
  3940. "* a\n:PROPERTIES:\n:A: 1\n:END:\n* b\n:PROPERTIES:\nsome junk here\n:END:\n"
  3941. (org-buffer-property-keys nil nil nil t)))))
  3942. (ert-deftest test-org/property-values ()
  3943. "Test `org-property-values' specifications."
  3944. ;; Regular test.
  3945. (should
  3946. (equal '("2" "1")
  3947. (org-test-with-temp-text
  3948. "* H\n:PROPERTIES:\n:A: 1\n:END:\n* H\n:PROPERTIES:\n:A: 2\n:END:"
  3949. (org-property-values "A"))))
  3950. ;; Ignore empty values.
  3951. (should-not
  3952. (org-test-with-temp-text
  3953. "* H1\n:PROPERTIES:\n:A:\n:END:\n* H2\n:PROPERTIES:\n:A: \n:END:"
  3954. (org-property-values "A")))
  3955. ;; Take into consideration extended values.
  3956. (should
  3957. (equal '("1 2")
  3958. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:END:"
  3959. (org-property-values "A")))))
  3960. (ert-deftest test-org/find-property ()
  3961. "Test `org-find-property' specifications."
  3962. ;; Regular test.
  3963. (should
  3964. (= 1
  3965. (org-test-with-temp-text "* H\n:PROPERTIES:\n:PROP: value\n:END:"
  3966. (org-find-property "prop"))))
  3967. ;; Ignore false positives.
  3968. (should
  3969. (= 27
  3970. (org-test-with-temp-text
  3971. "* H1\n:DRAWER:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:A: 1\n:END:"
  3972. (org-find-property "A"))))
  3973. ;; Return first entry found in buffer.
  3974. (should
  3975. (= 1
  3976. (org-test-with-temp-text
  3977. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:<point>A: 1\n:END:"
  3978. (org-find-property "A"))))
  3979. ;; Only search visible part of the buffer.
  3980. (should
  3981. (= 31
  3982. (org-test-with-temp-text
  3983. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:<point>A: 1\n:END:"
  3984. (org-narrow-to-subtree)
  3985. (org-find-property "A"))))
  3986. ;; With optional argument, only find entries with a specific value.
  3987. (should-not
  3988. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  3989. (org-find-property "A" "2")))
  3990. (should
  3991. (= 31
  3992. (org-test-with-temp-text
  3993. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:A: 2\n:END:"
  3994. (org-find-property "A" "2"))))
  3995. ;; Use "nil" for explicit nil values.
  3996. (should
  3997. (= 31
  3998. (org-test-with-temp-text
  3999. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:A: nil\n:END:"
  4000. (org-find-property "A" "nil")))))
  4001. (ert-deftest test-org/entry-delete ()
  4002. "Test `org-entry-delete' specifications."
  4003. ;; Regular test.
  4004. (should
  4005. (string-match
  4006. " *:PROPERTIES:\n *:B: +2\n *:END:"
  4007. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
  4008. (org-entry-delete (point) "A")
  4009. (buffer-string))))
  4010. ;; Also remove accumulated properties.
  4011. (should-not
  4012. (string-match
  4013. ":A"
  4014. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:B: 3\n:END:"
  4015. (org-entry-delete (point) "A")
  4016. (buffer-string))))
  4017. ;; When last property is removed, remove the property drawer.
  4018. (should-not
  4019. (string-match
  4020. ":PROPERTIES:"
  4021. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\nParagraph"
  4022. (org-entry-delete (point) "A")
  4023. (buffer-string))))
  4024. ;; Return a non-nil value when some property was removed.
  4025. (should
  4026. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
  4027. (org-entry-delete (point) "A")))
  4028. (should-not
  4029. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
  4030. (org-entry-delete (point) "C"))))
  4031. (ert-deftest test-org/entry-get ()
  4032. "Test `org-entry-get' specifications."
  4033. ;; Regular test.
  4034. (should
  4035. (equal "1"
  4036. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  4037. (org-entry-get (point) "A"))))
  4038. ;; Ignore case.
  4039. (should
  4040. (equal "1"
  4041. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  4042. (org-entry-get (point) "a"))))
  4043. ;; Handle extended values, both before and after base value.
  4044. (should
  4045. (equal "1 2 3"
  4046. (org-test-with-temp-text
  4047. "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:A+: 3\n:END:"
  4048. (org-entry-get (point) "A"))))
  4049. ;; Empty values are returned as the empty string.
  4050. (should
  4051. (equal ""
  4052. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A:\n:END:"
  4053. (org-entry-get (point) "A"))))
  4054. ;; Special nil value. If LITERAL-NIL is non-nil, return "nil",
  4055. ;; otherwise, return nil.
  4056. (should-not
  4057. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: nil\n:END:"
  4058. (org-entry-get (point) "A")))
  4059. (should
  4060. (equal "nil"
  4061. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: nil\n:END:"
  4062. (org-entry-get (point) "A" nil t))))
  4063. ;; Return nil when no property is found, independently on the
  4064. ;; LITERAL-NIL argument.
  4065. (should-not
  4066. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  4067. (org-entry-get (point) "B")))
  4068. (should-not
  4069. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  4070. (org-entry-get (point) "B" nil t)))
  4071. ;; Handle inheritance, when allowed. Include extended values and
  4072. ;; possibly global values.
  4073. (should
  4074. (equal
  4075. "1"
  4076. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
  4077. (org-entry-get (point) "A" t))))
  4078. (should
  4079. (equal
  4080. "1"
  4081. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
  4082. (let ((org-use-property-inheritance t))
  4083. (org-entry-get (point) "A" 'selective)))))
  4084. (should-not
  4085. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
  4086. (let ((org-use-property-inheritance nil))
  4087. (org-entry-get (point) "A" 'selective))))
  4088. (should
  4089. (equal
  4090. "1 2"
  4091. (org-test-with-temp-text
  4092. "* H\n:PROPERTIES:\n:A: 1\n:END:\n** H2\n:PROPERTIES:\n:A+: 2\n:END:"
  4093. (org-entry-get (point-max) "A" t))))
  4094. (should
  4095. (equal "1"
  4096. (org-test-with-temp-text
  4097. "#+PROPERTY: A 0\n* H\n:PROPERTIES:\n:A: 1\n:END:"
  4098. (org-mode-restart)
  4099. (org-entry-get (point-max) "A" t))))
  4100. (should
  4101. (equal "0 1"
  4102. (org-test-with-temp-text
  4103. "#+PROPERTY: A 0\n* H\n:PROPERTIES:\n:A+: 1\n:END:"
  4104. (org-mode-restart)
  4105. (org-entry-get (point-max) "A" t)))))
  4106. (ert-deftest test-org/entry-properties ()
  4107. "Test `org-entry-properties' specifications."
  4108. ;; Get "ITEM" property.
  4109. (should
  4110. (equal "H"
  4111. (org-test-with-temp-text "* TODO H"
  4112. (cdr (assoc "ITEM" (org-entry-properties nil "ITEM"))))))
  4113. (should
  4114. (equal "H"
  4115. (org-test-with-temp-text "* TODO H"
  4116. (cdr (assoc "ITEM" (org-entry-properties))))))
  4117. ;; Get "TODO" property. TODO keywords are case sensitive.
  4118. (should
  4119. (equal "TODO"
  4120. (org-test-with-temp-text "* TODO H"
  4121. (cdr (assoc "TODO" (org-entry-properties nil "TODO"))))))
  4122. (should
  4123. (equal "TODO"
  4124. (org-test-with-temp-text "* TODO H"
  4125. (cdr (assoc "TODO" (org-entry-properties))))))
  4126. (should-not
  4127. (org-test-with-temp-text "* H"
  4128. (assoc "TODO" (org-entry-properties nil "TODO"))))
  4129. (should-not
  4130. (org-test-with-temp-text "* todo H"
  4131. (assoc "TODO" (org-entry-properties nil "TODO"))))
  4132. ;; Get "PRIORITY" property.
  4133. (should
  4134. (equal "A"
  4135. (org-test-with-temp-text "* [#A] H"
  4136. (cdr (assoc "PRIORITY" (org-entry-properties nil "PRIORITY"))))))
  4137. (should
  4138. (equal "A"
  4139. (org-test-with-temp-text "* [#A] H"
  4140. (cdr (assoc "PRIORITY" (org-entry-properties))))))
  4141. (should
  4142. (equal (char-to-string org-default-priority)
  4143. (org-test-with-temp-text "* H"
  4144. (cdr (assoc "PRIORITY" (org-entry-properties nil "PRIORITY"))))))
  4145. ;; Get "FILE" property.
  4146. (should
  4147. (org-test-with-temp-text-in-file "* H\nParagraph"
  4148. (file-equal-p (cdr (assoc "FILE" (org-entry-properties nil "FILE")))
  4149. (buffer-file-name))))
  4150. (should
  4151. (org-test-with-temp-text-in-file "* H\nParagraph"
  4152. (file-equal-p (cdr (assoc "FILE" (org-entry-properties)))
  4153. (buffer-file-name))))
  4154. (should-not
  4155. (org-test-with-temp-text "* H\nParagraph"
  4156. (cdr (assoc "FILE" (org-entry-properties nil "FILE")))))
  4157. ;; Get "TAGS" property.
  4158. (should
  4159. (equal ":tag1:tag2:"
  4160. (org-test-with-temp-text "* H :tag1:tag2:"
  4161. (cdr (assoc "TAGS" (org-entry-properties nil "TAGS"))))))
  4162. (should
  4163. (equal ":tag1:tag2:"
  4164. (org-test-with-temp-text "* H :tag1:tag2:"
  4165. (cdr (assoc "TAGS" (org-entry-properties))))))
  4166. (should-not
  4167. (org-test-with-temp-text "* H"
  4168. (cdr (assoc "TAGS" (org-entry-properties nil "TAGS")))))
  4169. ;; Get "ALLTAGS" property.
  4170. (should
  4171. (equal ":tag1:tag2:"
  4172. (org-test-with-temp-text "* H :tag1:\n<point>** H2 :tag2:"
  4173. (cdr (assoc "ALLTAGS" (org-entry-properties nil "ALLTAGS"))))))
  4174. (should
  4175. (equal ":tag1:tag2:"
  4176. (org-test-with-temp-text "* H :tag1:\n<point>** H2 :tag2:"
  4177. (cdr (assoc "ALLTAGS" (org-entry-properties))))))
  4178. (should-not
  4179. (org-test-with-temp-text "* H"
  4180. (cdr (assoc "ALLTAGS" (org-entry-properties nil "ALLTAGS")))))
  4181. ;; Get "BLOCKED" property.
  4182. (should
  4183. (equal "t"
  4184. (org-test-with-temp-text "* TODO Blocked\n** DONE one\n** TODO two"
  4185. (let ((org-enforce-todo-dependencies t)
  4186. (org-blocker-hook
  4187. '(org-block-todo-from-children-or-siblings-or-parent)))
  4188. (cdr (assoc "BLOCKED" (org-entry-properties nil "BLOCKED")))))))
  4189. (should
  4190. (equal ""
  4191. (org-test-with-temp-text "* TODO Blocked\n** DONE one\n** DONE two"
  4192. (let ((org-enforce-todo-dependencies t)
  4193. (org-blocker-hook
  4194. '(org-block-todo-from-children-or-siblings-or-parent)))
  4195. (cdr (assoc "BLOCKED" (org-entry-properties nil "BLOCKED")))))))
  4196. ;; Get "CLOSED", "DEADLINE" and "SCHEDULED" properties.
  4197. (should
  4198. (equal
  4199. "[2012-03-29 thu.]"
  4200. (org-test-with-temp-text "* H\nCLOSED: [2012-03-29 thu.]"
  4201. (cdr (assoc "CLOSED" (org-entry-properties nil "CLOSED"))))))
  4202. (should
  4203. (equal
  4204. "[2012-03-29 thu.]"
  4205. (org-test-with-temp-text "* H\nCLOSED: [2012-03-29 thu.]"
  4206. (cdr (assoc "CLOSED" (org-entry-properties))))))
  4207. (should-not
  4208. (org-test-with-temp-text "* H"
  4209. (cdr (assoc "CLOSED" (org-entry-properties nil "CLOSED")))))
  4210. (should
  4211. (equal
  4212. "<2014-03-04 tue.>"
  4213. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  4214. (cdr (assoc "DEADLINE" (org-entry-properties nil "DEADLINE"))))))
  4215. (should
  4216. (equal
  4217. "<2014-03-04 tue.>"
  4218. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  4219. (cdr (assoc "DEADLINE" (org-entry-properties))))))
  4220. (should-not
  4221. (org-test-with-temp-text "* H"
  4222. (cdr (assoc "DEADLINE" (org-entry-properties nil "DEADLINE")))))
  4223. (should
  4224. (equal
  4225. "<2014-03-04 tue.>"
  4226. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  4227. (cdr (assoc "SCHEDULED" (org-entry-properties nil "SCHEDULED"))))))
  4228. (should
  4229. (equal
  4230. "<2014-03-04 tue.>"
  4231. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  4232. (cdr (assoc "SCHEDULED" (org-entry-properties))))))
  4233. (should-not
  4234. (org-test-with-temp-text "* H"
  4235. (cdr (assoc "SCHEDULED" (org-entry-properties nil "SCHEDULED")))))
  4236. ;; Get "CATEGORY"
  4237. (should
  4238. (equal "cat"
  4239. (org-test-with-temp-text "#+CATEGORY: cat\n<point>* H"
  4240. (cdr (assoc "CATEGORY" (org-entry-properties))))))
  4241. (should
  4242. (equal "cat"
  4243. (org-test-with-temp-text "#+CATEGORY: cat\n<point>* H"
  4244. (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
  4245. (should
  4246. (equal "cat"
  4247. (org-test-with-temp-text "* H\n:PROPERTIES:\n:CATEGORY: cat\n:END:"
  4248. (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
  4249. (should
  4250. (equal "cat2"
  4251. (org-test-with-temp-text
  4252. (concat "* H\n:PROPERTIES:\n:CATEGORY: cat1\n:END:"
  4253. "\n"
  4254. "** H2\n:PROPERTIES:\n:CATEGORY: cat2\n:END:<point>")
  4255. (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
  4256. ;; Get "TIMESTAMP" and "TIMESTAMP_IA" properties.
  4257. (should
  4258. (equal "<2012-03-29 thu.>"
  4259. (org-test-with-temp-text "* Entry\n<2012-03-29 thu.>"
  4260. (cdr (assoc "TIMESTAMP" (org-entry-properties))))))
  4261. (should
  4262. (equal "[2012-03-29 thu.]"
  4263. (org-test-with-temp-text "* Entry\n[2012-03-29 thu.]"
  4264. (cdr (assoc "TIMESTAMP_IA" (org-entry-properties))))))
  4265. (should
  4266. (equal "<2012-03-29 thu.>"
  4267. (org-test-with-temp-text "* Entry\n[2014-03-04 tue.]<2012-03-29 thu.>"
  4268. (cdr (assoc "TIMESTAMP" (org-entry-properties nil "TIMESTAMP"))))))
  4269. (should
  4270. (equal "[2014-03-04 tue.]"
  4271. (org-test-with-temp-text "* Entry\n<2012-03-29 thu.>[2014-03-04 tue.]"
  4272. (cdr (assoc "TIMESTAMP_IA" (org-entry-properties nil "TIMESTAMP_IA"))))))
  4273. ;; Get standard properties.
  4274. (should
  4275. (equal "1"
  4276. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  4277. (cdr (assoc "A" (org-entry-properties nil 'standard))))))
  4278. ;; Handle extended properties.
  4279. (should
  4280. (equal "1 2 3"
  4281. (org-test-with-temp-text
  4282. "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:A+: 3\n:END:"
  4283. (cdr (assoc "A" (org-entry-properties nil 'standard))))))
  4284. (should
  4285. (equal "1 2 3"
  4286. (org-test-with-temp-text
  4287. "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:a+: 3\n:END:"
  4288. (cdr (assoc "A" (org-entry-properties nil 'standard))))))
  4289. ;; Ignore forbidden (special) properties.
  4290. (should-not
  4291. (org-test-with-temp-text "* H\n:PROPERTIES:\n:TODO: foo\n:END:"
  4292. (cdr (assoc "TODO" (org-entry-properties nil 'standard))))))
  4293. (ert-deftest test-org/entry-put ()
  4294. "Test `org-entry-put' specifications."
  4295. ;; Error when not a string or nil.
  4296. (should-error
  4297. (org-test-with-temp-text "* H\n:PROPERTIES:\n:test: 1\n:END:"
  4298. (org-entry-put 1 "test" 2)))
  4299. ;; Error when property name is invalid.
  4300. (should-error
  4301. (org-test-with-temp-text "* H\n:PROPERTIES:\n:test: 1\n:END:"
  4302. (org-entry-put 1 "no space" "value")))
  4303. (should-error
  4304. (org-test-with-temp-text "* H\n:PROPERTIES:\n:test: 1\n:END:"
  4305. (org-entry-put 1 "" "value")))
  4306. ;; Set "TODO" property.
  4307. (should
  4308. (string-match (regexp-quote " TODO H")
  4309. (org-test-with-temp-text "#+TODO: TODO | DONE\n<point>* H"
  4310. (org-entry-put (point) "TODO" "TODO")
  4311. (buffer-string))))
  4312. (should
  4313. (string-match (regexp-quote "* H")
  4314. (org-test-with-temp-text "#+TODO: TODO | DONE\n<point>* H"
  4315. (org-entry-put (point) "TODO" nil)
  4316. (buffer-string))))
  4317. ;; Set "PRIORITY" property.
  4318. (should
  4319. (equal "* [#A] H"
  4320. (org-test-with-temp-text "* [#B] H"
  4321. (org-entry-put (point) "PRIORITY" "A")
  4322. (buffer-string))))
  4323. (should
  4324. (equal "* H"
  4325. (org-test-with-temp-text "* [#B] H"
  4326. (org-entry-put (point) "PRIORITY" nil)
  4327. (buffer-string))))
  4328. ;; Set "SCHEDULED" property.
  4329. (should
  4330. (string-match "* H\n *SCHEDULED: <2014-03-04 .*?>"
  4331. (org-test-with-temp-text "* H"
  4332. (org-entry-put (point) "SCHEDULED" "2014-03-04")
  4333. (buffer-string))))
  4334. (should
  4335. (string= "* H\n"
  4336. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  4337. (org-entry-put (point) "SCHEDULED" nil)
  4338. (buffer-string))))
  4339. (should
  4340. (string-match "* H\n *SCHEDULED: <2014-03-03 .*?>"
  4341. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  4342. (org-entry-put (point) "SCHEDULED" "earlier")
  4343. (buffer-string))))
  4344. (should
  4345. (string-match "^ *SCHEDULED: <2014-03-05 .*?>"
  4346. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  4347. (org-entry-put (point) "SCHEDULED" "later")
  4348. (buffer-string))))
  4349. ;; Set "DEADLINE" property.
  4350. (should
  4351. (string-match "^ *DEADLINE: <2014-03-04 .*?>"
  4352. (org-test-with-temp-text "* H"
  4353. (org-entry-put (point) "DEADLINE" "2014-03-04")
  4354. (buffer-string))))
  4355. (should
  4356. (string= "* H\n"
  4357. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  4358. (org-entry-put (point) "DEADLINE" nil)
  4359. (buffer-string))))
  4360. (should
  4361. (string-match "^ *DEADLINE: <2014-03-03 .*?>"
  4362. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  4363. (org-entry-put (point) "DEADLINE" "earlier")
  4364. (buffer-string))))
  4365. (should
  4366. (string-match "^ *DEADLINE: <2014-03-05 .*?>"
  4367. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  4368. (org-entry-put (point) "DEADLINE" "later")
  4369. (buffer-string))))
  4370. ;; Set "CATEGORY" property
  4371. (should
  4372. (string-match "^ *:CATEGORY: cat"
  4373. (org-test-with-temp-text "* H"
  4374. (org-entry-put (point) "CATEGORY" "cat")
  4375. (buffer-string))))
  4376. ;; Regular properties, with or without pre-existing drawer.
  4377. (should
  4378. (string-match "^ *:A: +2$"
  4379. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  4380. (org-entry-put (point) "A" "2")
  4381. (buffer-string))))
  4382. (should
  4383. (string-match "^ *:A: +1$"
  4384. (org-test-with-temp-text "* H"
  4385. (org-entry-put (point) "A" "1")
  4386. (buffer-string))))
  4387. ;; Special case: two consecutive headlines.
  4388. (should
  4389. (string-match "\\* A\n *:PROPERTIES:"
  4390. (org-test-with-temp-text "* A\n** B"
  4391. (org-entry-put (point) "A" "1")
  4392. (buffer-string)))))
  4393. ;;; Radio Targets
  4394. (ert-deftest test-org/update-radio-target-regexp ()
  4395. "Test `org-update-radio-target-regexp' specifications."
  4396. ;; Properly update cache with no previous radio target regexp.
  4397. (should
  4398. (eq 'link
  4399. (org-test-with-temp-text "radio\n\nParagraph\n\nradio"
  4400. (save-excursion (goto-char (point-max)) (org-element-context))
  4401. (insert "<<<")
  4402. (search-forward "o")
  4403. (insert ">>>")
  4404. (org-update-radio-target-regexp)
  4405. (goto-char (point-max))
  4406. (org-element-type (org-element-context)))))
  4407. ;; Properly update cache with previous radio target regexp.
  4408. (should
  4409. (eq 'link
  4410. (org-test-with-temp-text "radio\n\nParagraph\n\nradio"
  4411. (save-excursion (goto-char (point-max)) (org-element-context))
  4412. (insert "<<<")
  4413. (search-forward "o")
  4414. (insert ">>>")
  4415. (org-update-radio-target-regexp)
  4416. (search-backward "r")
  4417. (delete-char 5)
  4418. (insert "new")
  4419. (org-update-radio-target-regexp)
  4420. (goto-char (point-max))
  4421. (delete-region (line-beginning-position) (point))
  4422. (insert "new")
  4423. (org-element-type (org-element-context))))))
  4424. ;;; Sparse trees
  4425. (ert-deftest test-org/match-sparse-tree ()
  4426. "Test `org-match-sparse-tree' specifications."
  4427. ;; Match tags.
  4428. (should-not
  4429. (org-test-with-temp-text "* H\n** H1 :tag:"
  4430. (org-match-sparse-tree nil "tag")
  4431. (search-forward "H1")
  4432. (org-invisible-p2)))
  4433. (should
  4434. (org-test-with-temp-text "* H\n** H1 :tag:\n** H2 :tag2:"
  4435. (org-match-sparse-tree nil "tag")
  4436. (search-forward "H2")
  4437. (org-invisible-p2)))
  4438. ;; "-" operator for tags.
  4439. (should-not
  4440. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  4441. (org-match-sparse-tree nil "tag1-tag2")
  4442. (search-forward "H1")
  4443. (org-invisible-p2)))
  4444. (should
  4445. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  4446. (org-match-sparse-tree nil "tag1-tag2")
  4447. (search-forward "H2")
  4448. (org-invisible-p2)))
  4449. ;; "&" operator for tags.
  4450. (should
  4451. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  4452. (org-match-sparse-tree nil "tag1&tag2")
  4453. (search-forward "H1")
  4454. (org-invisible-p2)))
  4455. (should-not
  4456. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  4457. (org-match-sparse-tree nil "tag1&tag2")
  4458. (search-forward "H2")
  4459. (org-invisible-p2)))
  4460. ;; "|" operator for tags.
  4461. (should-not
  4462. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  4463. (org-match-sparse-tree nil "tag1|tag2")
  4464. (search-forward "H1")
  4465. (org-invisible-p2)))
  4466. (should-not
  4467. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  4468. (org-match-sparse-tree nil "tag1|tag2")
  4469. (search-forward "H2")
  4470. (org-invisible-p2)))
  4471. ;; Regexp match on tags.
  4472. (should-not
  4473. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :foo:"
  4474. (org-match-sparse-tree nil "{^tag.*}")
  4475. (search-forward "H1")
  4476. (org-invisible-p2)))
  4477. (should
  4478. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :foo:"
  4479. (org-match-sparse-tree nil "{^tag.*}")
  4480. (search-forward "H2")
  4481. (org-invisible-p2)))
  4482. ;; Match group tags.
  4483. (should-not
  4484. (org-test-with-temp-text
  4485. "#+TAGS: { work : lab }\n* H\n** H1 :work:\n** H2 :lab:"
  4486. (org-match-sparse-tree nil "work")
  4487. (search-forward "H1")
  4488. (org-invisible-p2)))
  4489. (should-not
  4490. (org-test-with-temp-text
  4491. "#+TAGS: { work : lab }\n* H\n** H1 :work:\n** H2 :lab:"
  4492. (org-match-sparse-tree nil "work")
  4493. (search-forward "H2")
  4494. (org-invisible-p2)))
  4495. ;; Match group tags with hard brackets.
  4496. (should-not
  4497. (org-test-with-temp-text
  4498. "#+TAGS: [ work : lab ]\n* H\n** H1 :work:\n** H2 :lab:"
  4499. (org-match-sparse-tree nil "work")
  4500. (search-forward "H1")
  4501. (org-invisible-p2)))
  4502. (should-not
  4503. (org-test-with-temp-text
  4504. "#+TAGS: [ work : lab ]\n* H\n** H1 :work:\n** H2 :lab:"
  4505. (org-match-sparse-tree nil "work")
  4506. (search-forward "H2")
  4507. (org-invisible-p2)))
  4508. ;; Match tags in hierarchies
  4509. (should-not
  4510. (org-test-with-temp-text
  4511. "#+TAGS: [ Lev_1 : Lev_2 ]\n
  4512. #+TAGS: [ Lev_2 : Lev_3 ]\n
  4513. #+TAGS: { Lev_3 : Lev_4 }\n
  4514. * H\n** H1 :Lev_1:\n** H2 :Lev_2:\n** H3 :Lev_3:\n** H4 :Lev_4:"
  4515. (org-match-sparse-tree nil "Lev_1")
  4516. (search-forward "H4")
  4517. (org-invisible-p2)))
  4518. ;; Match regular expressions in tags
  4519. (should-not
  4520. (org-test-with-temp-text
  4521. "#+TAGS: [ Lev : {Lev_[0-9]} ]\n* H\n** H1 :Lev_1:"
  4522. (org-match-sparse-tree nil "Lev")
  4523. (search-forward "H1")
  4524. (org-invisible-p2)))
  4525. (should
  4526. (org-test-with-temp-text
  4527. "#+TAGS: [ Lev : {Lev_[0-9]} ]\n* H\n** H1 :Lev_n:"
  4528. (org-match-sparse-tree nil "Lev")
  4529. (search-forward "H1")
  4530. (org-invisible-p2)))
  4531. ;; Match properties.
  4532. (should
  4533. (org-test-with-temp-text
  4534. "* H\n** H1\n:PROPERTIES:\n:A: 1\n:END:\n** H2\n:PROPERTIES:\n:A: 2\n:END:"
  4535. (org-match-sparse-tree nil "A=\"1\"")
  4536. (search-forward "H2")
  4537. (org-invisible-p2)))
  4538. (should-not
  4539. (org-test-with-temp-text "* H1\n** H2\n:PROPERTIES:\n:A: 1\n:END:"
  4540. (org-match-sparse-tree nil "A=\"1\"")
  4541. (search-forward "H2")
  4542. (org-invisible-p2)))
  4543. ;; Case is not significant when matching properties.
  4544. (should-not
  4545. (org-test-with-temp-text "* H1\n** H2\n:PROPERTIES:\n:A: 1\n:END:"
  4546. (org-match-sparse-tree nil "a=\"1\"")
  4547. (search-forward "H2")
  4548. (org-invisible-p2)))
  4549. (should-not
  4550. (org-test-with-temp-text "* H1\n** H2\n:PROPERTIES:\n:a: 1\n:END:"
  4551. (org-match-sparse-tree nil "A=\"1\"")
  4552. (search-forward "H2")
  4553. (org-invisible-p2)))
  4554. ;; Match special LEVEL property.
  4555. (should-not
  4556. (org-test-with-temp-text "* H\n** H1\n*** H2"
  4557. (let ((org-odd-levels-only nil)) (org-match-sparse-tree nil "LEVEL=2"))
  4558. (search-forward "H1")
  4559. (org-invisible-p2)))
  4560. (should
  4561. (org-test-with-temp-text "* H\n** H1\n*** H2"
  4562. (let ((org-odd-levels-only nil)) (org-match-sparse-tree nil "LEVEL=2"))
  4563. (search-forward "H2")
  4564. (org-invisible-p2)))
  4565. ;; Comparison operators when matching properties.
  4566. (should
  4567. (org-test-with-temp-text
  4568. "* H\n** H1\nSCHEDULED: <2014-03-04 tue.>\n** H2\nSCHEDULED: <2012-03-29 thu.>"
  4569. (org-match-sparse-tree nil "SCHEDULED<=\"<2013-01-01>\"")
  4570. (search-forward "H1")
  4571. (org-invisible-p2)))
  4572. (should-not
  4573. (org-test-with-temp-text
  4574. "* H\n** H1\nSCHEDULED: <2014-03-04 tue.>\n** H2\nSCHEDULED: <2012-03-29 thu.>"
  4575. (org-match-sparse-tree nil "SCHEDULED<=\"<2013-01-01>\"")
  4576. (search-forward "H2")
  4577. (org-invisible-p2)))
  4578. ;; Regexp match on properties values.
  4579. (should-not
  4580. (org-test-with-temp-text
  4581. "* H\n** H1\n:PROPERTIES:\n:A: foo\n:END:\n** H2\n:PROPERTIES:\n:A: bar\n:END:"
  4582. (org-match-sparse-tree nil "A={f.*}")
  4583. (search-forward "H1")
  4584. (org-invisible-p2)))
  4585. (should
  4586. (org-test-with-temp-text
  4587. "* H\n** H1\n:PROPERTIES:\n:A: foo\n:END:\n** H2\n:PROPERTIES:\n:A: bar\n:END:"
  4588. (org-match-sparse-tree nil "A={f.*}")
  4589. (search-forward "H2")
  4590. (org-invisible-p2)))
  4591. ;; With an optional argument, limit match to TODO entries.
  4592. (should-not
  4593. (org-test-with-temp-text "* H\n** TODO H1 :tag:\n** H2 :tag:"
  4594. (org-match-sparse-tree t "tag")
  4595. (search-forward "H1")
  4596. (org-invisible-p2)))
  4597. (should
  4598. (org-test-with-temp-text "* H\n** TODO H1 :tag:\n** H2 :tag:"
  4599. (org-match-sparse-tree t "tag")
  4600. (search-forward "H2")
  4601. (org-invisible-p2))))
  4602. (ert-deftest test-org/occur ()
  4603. "Test `org-occur' specifications."
  4604. ;; Count number of matches.
  4605. (should
  4606. (= 1
  4607. (org-test-with-temp-text "* H\nA\n* H2"
  4608. (org-occur "A"))))
  4609. (should
  4610. (= 2
  4611. (org-test-with-temp-text "* H\nA\n* H2\nA"
  4612. (org-occur "A"))))
  4613. ;; Test CALLBACK optional argument.
  4614. (should
  4615. (= 0
  4616. (org-test-with-temp-text "* H\nA\n* H2"
  4617. (org-occur "A" nil (lambda () (equal (org-get-heading) "H2"))))))
  4618. (should
  4619. (= 1
  4620. (org-test-with-temp-text "* H\nA\n* H2\nA"
  4621. (org-occur "A" nil (lambda () (equal (org-get-heading) "H2"))))))
  4622. ;; Case-fold searches according to `org-occur-case-fold-search'.
  4623. (should
  4624. (= 2
  4625. (org-test-with-temp-text "Aa"
  4626. (let ((org-occur-case-fold-search t)) (org-occur "A")))))
  4627. (should
  4628. (= 2
  4629. (org-test-with-temp-text "Aa"
  4630. (let ((org-occur-case-fold-search t)) (org-occur "a")))))
  4631. (should
  4632. (= 1
  4633. (org-test-with-temp-text "Aa"
  4634. (let ((org-occur-case-fold-search nil)) (org-occur "A")))))
  4635. (should
  4636. (= 1
  4637. (org-test-with-temp-text "Aa"
  4638. (let ((org-occur-case-fold-search nil)) (org-occur "a")))))
  4639. (should
  4640. (= 1
  4641. (org-test-with-temp-text "Aa"
  4642. (let ((org-occur-case-fold-search 'smart)) (org-occur "A")))))
  4643. (should
  4644. (= 2
  4645. (org-test-with-temp-text "Aa"
  4646. (let ((org-occur-case-fold-search 'smart)) (org-occur "a"))))))
  4647. ;;; Tags
  4648. (ert-deftest test-org/tag-string-to-alist ()
  4649. "Test `org-tag-string-to-alist' specifications."
  4650. ;; Tag without selection key.
  4651. (should (equal (org-tag-string-to-alist "tag1") '(("tag1"))))
  4652. ;; Tag with selection key.
  4653. (should (equal (org-tag-string-to-alist "tag1(t)") '(("tag1" . ?t))))
  4654. ;; Tag group.
  4655. (should
  4656. (equal
  4657. (org-tag-string-to-alist "[ group : t1 t2 ]")
  4658. '((:startgrouptag) ("group") (:grouptags) ("t1") ("t2") (:endgrouptag))))
  4659. ;; Mutually exclusive tags.
  4660. (should (equal (org-tag-string-to-alist "{ tag1 tag2 }")
  4661. '((:startgroup) ("tag1") ("tag2") (:endgroup))))
  4662. (should
  4663. (equal
  4664. (org-tag-string-to-alist "{ group : tag1 tag2 }")
  4665. '((:startgroup) ("group") (:grouptags) ("tag1") ("tag2") (:endgroup)))))
  4666. (ert-deftest test-org/tag-alist-to-string ()
  4667. "Test `org-tag-alist-to-string' specifications."
  4668. (should (equal (org-tag-alist-to-string '(("tag1"))) "tag1"))
  4669. (should (equal (org-tag-alist-to-string '(("tag1" . ?t))) "tag1(t)"))
  4670. (should
  4671. (equal
  4672. (org-tag-alist-to-string
  4673. '((:startgrouptag) ("group") (:grouptags) ("t1") ("t2") (:endgrouptag)))
  4674. "[ group : t1 t2 ]"))
  4675. (should
  4676. (equal (org-tag-alist-to-string
  4677. '((:startgroup) ("tag1") ("tag2") (:endgroup)))
  4678. "{ tag1 tag2 }"))
  4679. (should
  4680. (equal
  4681. (org-tag-alist-to-string
  4682. '((:startgroup) ("group") (:grouptags) ("tag1") ("tag2") (:endgroup)))
  4683. "{ group : tag1 tag2 }")))
  4684. (ert-deftest test-org/tag-alist-to-groups ()
  4685. "Test `org-tag-alist-to-groups' specifications."
  4686. (should
  4687. (equal (org-tag-alist-to-groups
  4688. '((:startgroup) ("group") (:grouptags) ("t1") ("t2") (:endgroup)))
  4689. '(("group" "t1" "t2"))))
  4690. (should
  4691. (equal
  4692. (org-tag-alist-to-groups
  4693. '((:startgrouptag) ("group") (:grouptags) ("t1") ("t2") (:endgrouptag)))
  4694. '(("group" "t1" "t2"))))
  4695. (should-not
  4696. (org-tag-alist-to-groups
  4697. '((:startgroup) ("group") ("t1") ("t2") (:endgroup)))))
  4698. (ert-deftest test-org/tag-align ()
  4699. "Test tags alignment."
  4700. ;; Test aligning tags with different display width.
  4701. (should
  4702. ;; 12345678901234567890
  4703. (equal "* Test :abc:"
  4704. (org-test-with-temp-text "* Test :abc:"
  4705. (let ((org-tags-column -20)
  4706. (indent-tabs-mode nil))
  4707. (org-fix-tags-on-the-fly))
  4708. (buffer-string))))
  4709. (should
  4710. ;; 12345678901234567890
  4711. (equal "* Test :日本語:"
  4712. (org-test-with-temp-text "* Test :日本語:"
  4713. (let ((org-tags-column -20)
  4714. (indent-tabs-mode nil))
  4715. (org-fix-tags-on-the-fly))
  4716. (buffer-string))))
  4717. ;; Make sure aligning tags do not skip invisible text.
  4718. (should
  4719. (equal "* [[linkx]] :tag:"
  4720. (org-test-with-temp-text "* [[link<point>]] :tag:"
  4721. (let ((org-tags-column 0))
  4722. (org-fix-tags-on-the-fly)
  4723. (insert "x")
  4724. (buffer-string))))))
  4725. (ert-deftest test-org/tags-at ()
  4726. (should
  4727. (equal '("foo" "bar")
  4728. (org-test-with-temp-text
  4729. "* T<point>est :foo:bar:"
  4730. (org-get-tags-at)))))
  4731. ;;; Timestamps API
  4732. (ert-deftest test-org/time-stamp ()
  4733. "Test `org-time-stamp' specifications."
  4734. ;; Insert chosen time stamp at point.
  4735. (should
  4736. (string-match
  4737. "Te<2014-03-04 .*?>xt"
  4738. (org-test-with-temp-text "Te<point>xt"
  4739. (cl-letf (((symbol-function 'org-read-date)
  4740. (lambda (&rest args)
  4741. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  4742. (org-time-stamp nil)
  4743. (buffer-string)))))
  4744. ;; With a prefix argument, also insert time.
  4745. (should
  4746. (string-match
  4747. "Te<2014-03-04 .*? 00:41>xt"
  4748. (org-test-with-temp-text "Te<point>xt"
  4749. (cl-letf (((symbol-function 'org-read-date)
  4750. (lambda (&rest args)
  4751. (apply #'encode-time
  4752. (org-parse-time-string "2014-03-04 00:41")))))
  4753. (org-time-stamp '(4))
  4754. (buffer-string)))))
  4755. ;; With two universal prefix arguments, insert an active timestamp
  4756. ;; with the current time without prompting the user.
  4757. (should
  4758. (string-match
  4759. "Te<2014-03-04 .*? 00:41>xt"
  4760. (org-test-with-temp-text "Te<point>xt"
  4761. (cl-letf (((symbol-function 'current-time)
  4762. (lambda ()
  4763. (apply #'encode-time
  4764. (org-parse-time-string "2014-03-04 00:41")))))
  4765. (org-time-stamp '(16))
  4766. (buffer-string)))))
  4767. ;; When optional argument is non-nil, insert an inactive timestamp.
  4768. (should
  4769. (string-match
  4770. "Te\\[2014-03-04 .*?\\]xt"
  4771. (org-test-with-temp-text "Te<point>xt"
  4772. (cl-letf (((symbol-function 'org-read-date)
  4773. (lambda (&rest args)
  4774. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  4775. (org-time-stamp nil t)
  4776. (buffer-string)))))
  4777. ;; When called from a timestamp, replace existing one.
  4778. (should
  4779. (string-match
  4780. "<2014-03-04 .*?>"
  4781. (org-test-with-temp-text "<2012-03-29<point> thu.>"
  4782. (cl-letf (((symbol-function 'org-read-date)
  4783. (lambda (&rest args)
  4784. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  4785. (org-time-stamp nil)
  4786. (buffer-string)))))
  4787. (should
  4788. (string-match
  4789. "<2014-03-04 .*?>--<2014-03-04 .*?>"
  4790. (org-test-with-temp-text "<2012-03-29<point> thu.>--<2014-03-04 tue.>"
  4791. (cl-letf (((symbol-function 'org-read-date)
  4792. (lambda (&rest args)
  4793. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  4794. (org-time-stamp nil)
  4795. (buffer-string)))))
  4796. ;; When replacing a timestamp, preserve repeater, if any.
  4797. (should
  4798. (string-match
  4799. "<2014-03-04 .*? \\+2y>"
  4800. (org-test-with-temp-text "<2012-03-29<point> thu. +2y>"
  4801. (cl-letf (((symbol-function 'org-read-date)
  4802. (lambda (&rest args)
  4803. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  4804. (org-time-stamp nil)
  4805. (buffer-string)))))
  4806. ;; When called twice in a raw, build a date range.
  4807. (should
  4808. (string-match
  4809. "<2012-03-29 .*?>--<2014-03-04 .*?>"
  4810. (org-test-with-temp-text "<2012-03-29 thu.><point>"
  4811. (cl-letf (((symbol-function 'org-read-date)
  4812. (lambda (&rest args)
  4813. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  4814. (let ((last-command 'org-time-stamp)
  4815. (this-command 'org-time-stamp))
  4816. (org-time-stamp nil))
  4817. (buffer-string))))))
  4818. (ert-deftest test-org/timestamp-has-time-p ()
  4819. "Test `org-timestamp-has-time-p' specifications."
  4820. ;; With time.
  4821. (should
  4822. (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
  4823. (org-timestamp-has-time-p (org-element-context))))
  4824. ;; Without time.
  4825. (should-not
  4826. (org-test-with-temp-text "<2012-03-29 Thu>"
  4827. (org-timestamp-has-time-p (org-element-context)))))
  4828. (ert-deftest test-org/timestamp-format ()
  4829. "Test `org-timestamp-format' specifications."
  4830. ;; Regular test.
  4831. (should
  4832. (equal
  4833. "2012-03-29 16:40"
  4834. (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
  4835. (org-timestamp-format (org-element-context) "%Y-%m-%d %R"))))
  4836. ;; Range end.
  4837. (should
  4838. (equal
  4839. "2012-03-29"
  4840. (org-test-with-temp-text "[2011-07-14 Thu]--[2012-03-29 Thu]"
  4841. (org-timestamp-format (org-element-context) "%Y-%m-%d" t)))))
  4842. (ert-deftest test-org/timestamp-split-range ()
  4843. "Test `org-timestamp-split-range' specifications."
  4844. ;; Extract range start (active).
  4845. (should
  4846. (equal '(2012 3 29)
  4847. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  4848. (let ((ts (org-timestamp-split-range (org-element-context))))
  4849. (mapcar (lambda (p) (org-element-property p ts))
  4850. '(:year-end :month-end :day-end))))))
  4851. ;; Extract range start (inactive)
  4852. (should
  4853. (equal '(2012 3 29)
  4854. (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
  4855. (let ((ts (org-timestamp-split-range (org-element-context))))
  4856. (mapcar (lambda (p) (org-element-property p ts))
  4857. '(:year-end :month-end :day-end))))))
  4858. ;; Extract range end (active).
  4859. (should
  4860. (equal '(2012 3 30)
  4861. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  4862. (let ((ts (org-timestamp-split-range
  4863. (org-element-context) t)))
  4864. (mapcar (lambda (p) (org-element-property p ts))
  4865. '(:year-end :month-end :day-end))))))
  4866. ;; Extract range end (inactive)
  4867. (should
  4868. (equal '(2012 3 30)
  4869. (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
  4870. (let ((ts (org-timestamp-split-range
  4871. (org-element-context) t)))
  4872. (mapcar (lambda (p) (org-element-property p ts))
  4873. '(:year-end :month-end :day-end))))))
  4874. ;; Return the timestamp if not a range.
  4875. (should
  4876. (org-test-with-temp-text "[2012-03-29 Thu]"
  4877. (let* ((ts-orig (org-element-context))
  4878. (ts-copy (org-timestamp-split-range ts-orig)))
  4879. (eq ts-orig ts-copy))))
  4880. (should
  4881. (org-test-with-temp-text "<%%(org-float t 4 2)>"
  4882. (let* ((ts-orig (org-element-context))
  4883. (ts-copy (org-timestamp-split-range ts-orig)))
  4884. (eq ts-orig ts-copy)))))
  4885. (ert-deftest test-org/timestamp-translate ()
  4886. "Test `org-timestamp-translate' specifications."
  4887. ;; Translate whole date range.
  4888. (should
  4889. (equal "<29>--<30>"
  4890. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  4891. (let ((org-display-custom-times t)
  4892. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  4893. (org-timestamp-translate (org-element-context))))))
  4894. ;; Translate date range start.
  4895. (should
  4896. (equal "<29>"
  4897. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  4898. (let ((org-display-custom-times t)
  4899. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  4900. (org-timestamp-translate (org-element-context) 'start)))))
  4901. ;; Translate date range end.
  4902. (should
  4903. (equal "<30>"
  4904. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  4905. (let ((org-display-custom-times t)
  4906. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  4907. (org-timestamp-translate (org-element-context) 'end)))))
  4908. ;; Translate time range.
  4909. (should
  4910. (equal "<08>--<16>"
  4911. (org-test-with-temp-text "<2012-03-29 Thu 8:30-16:40>"
  4912. (let ((org-display-custom-times t)
  4913. (org-time-stamp-custom-formats '("<%d>" . "<%H>")))
  4914. (org-timestamp-translate (org-element-context))))))
  4915. ;; Translate non-range timestamp.
  4916. (should
  4917. (equal "<29>"
  4918. (org-test-with-temp-text "<2012-03-29 Thu>"
  4919. (let ((org-display-custom-times t)
  4920. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  4921. (org-timestamp-translate (org-element-context))))))
  4922. ;; Do not change `diary' timestamps.
  4923. (should
  4924. (equal "<%%(org-float t 4 2)>"
  4925. (org-test-with-temp-text "<%%(org-float t 4 2)>"
  4926. (let ((org-display-custom-times t)
  4927. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  4928. (org-timestamp-translate (org-element-context)))))))
  4929. ;;; Visibility
  4930. (ert-deftest test-org/flag-drawer ()
  4931. "Test `org-flag-drawer' specifications."
  4932. ;; Hide drawer.
  4933. (should
  4934. (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
  4935. (org-flag-drawer t)
  4936. (get-char-property (line-end-position) 'invisible)))
  4937. ;; Show drawer.
  4938. (should-not
  4939. (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
  4940. (org-flag-drawer t)
  4941. (org-flag-drawer nil)
  4942. (get-char-property (line-end-position) 'invisible)))
  4943. ;; Test optional argument.
  4944. (should
  4945. (org-test-with-temp-text "Text\n:D1:\nc1\n:END:\n\n:D2:\nc2\n:END:"
  4946. (let ((drawer (save-excursion (search-forward ":D2")
  4947. (org-element-at-point))))
  4948. (org-flag-drawer t drawer)
  4949. (get-char-property (progn (search-forward ":D2") (line-end-position))
  4950. 'invisible))))
  4951. (should-not
  4952. (org-test-with-temp-text ":D1:\nc1\n:END:\n\n:D2:\nc2\n:END:"
  4953. (let ((drawer (save-excursion (search-forward ":D2")
  4954. (org-element-at-point))))
  4955. (org-flag-drawer t drawer)
  4956. (get-char-property (line-end-position) 'invisible))))
  4957. ;; Do not hide fake drawers.
  4958. (should-not
  4959. (org-test-with-temp-text "#+begin_example\n:D:\nc\n:END:\n#+end_example"
  4960. (forward-line 1)
  4961. (org-flag-drawer t)
  4962. (get-char-property (line-end-position) 'invisible)))
  4963. ;; Do not hide incomplete drawers.
  4964. (should-not
  4965. (org-test-with-temp-text ":D:\nparagraph"
  4966. (forward-line 1)
  4967. (org-flag-drawer t)
  4968. (get-char-property (line-end-position) 'invisible)))
  4969. ;; Do not hide drawers when called from final blank lines.
  4970. (should-not
  4971. (org-test-with-temp-text ":DRAWER:\nA\n:END:\n\n"
  4972. (goto-char (point-max))
  4973. (org-flag-drawer t)
  4974. (goto-char (point-min))
  4975. (get-char-property (line-end-position) 'invisible)))
  4976. ;; Don't leave point in an invisible part of the buffer when hiding
  4977. ;; a drawer away.
  4978. (should-not
  4979. (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
  4980. (goto-char (point-max))
  4981. (org-flag-drawer t)
  4982. (get-char-property (point) 'invisible))))
  4983. (ert-deftest test-org/hide-block-toggle ()
  4984. "Test `org-hide-block-toggle' specifications."
  4985. ;; Error when not at a block.
  4986. (should-error
  4987. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents"
  4988. (org-hide-block-toggle 'off)
  4989. (get-char-property (line-end-position) 'invisible)))
  4990. ;; Hide block.
  4991. (should
  4992. (org-test-with-temp-text "#+BEGIN_CENTER\ncontents\n#+END_CENTER"
  4993. (org-hide-block-toggle)
  4994. (get-char-property (line-end-position) 'invisible)))
  4995. (should
  4996. (org-test-with-temp-text "#+BEGIN_EXAMPLE\ncontents\n#+END_EXAMPLE"
  4997. (org-hide-block-toggle)
  4998. (get-char-property (line-end-position) 'invisible)))
  4999. ;; Show block unconditionally when optional argument is `off'.
  5000. (should-not
  5001. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  5002. (org-hide-block-toggle)
  5003. (org-hide-block-toggle 'off)
  5004. (get-char-property (line-end-position) 'invisible)))
  5005. (should-not
  5006. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  5007. (org-hide-block-toggle 'off)
  5008. (get-char-property (line-end-position) 'invisible)))
  5009. ;; Hide block unconditionally when optional argument is non-nil.
  5010. (should
  5011. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  5012. (org-hide-block-toggle t)
  5013. (get-char-property (line-end-position) 'invisible)))
  5014. (should
  5015. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  5016. (org-hide-block-toggle)
  5017. (org-hide-block-toggle t)
  5018. (get-char-property (line-end-position) 'invisible)))
  5019. ;; Do not hide block when called from final blank lines.
  5020. (should-not
  5021. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE\n\n<point>"
  5022. (org-hide-block-toggle)
  5023. (goto-char (point-min))
  5024. (get-char-property (line-end-position) 'invisible)))
  5025. ;; Don't leave point in an invisible part of the buffer when hiding
  5026. ;; a block away.
  5027. (should-not
  5028. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n<point>#+END_QUOTE"
  5029. (org-hide-block-toggle)
  5030. (get-char-property (point) 'invisible))))
  5031. (ert-deftest test-org/hide-block-toggle-maybe ()
  5032. "Test `org-hide-block-toggle-maybe' specifications."
  5033. (should
  5034. (org-test-with-temp-text "#+BEGIN: dynamic\nContents\n#+END:"
  5035. (org-hide-block-toggle-maybe)))
  5036. (should-not
  5037. (org-test-with-temp-text "Paragraph" (org-hide-block-toggle-maybe))))
  5038. (ert-deftest test-org/set-tags ()
  5039. "Test `org-set-tags' specifications."
  5040. ;; Tags set via fast-tag-selection should be visible afterwards
  5041. (should
  5042. (let ((org-tag-alist '(("NEXT" . ?n)))
  5043. (org-fast-tag-selection-single-key t))
  5044. (cl-letf (((symbol-function 'read-char-exclusive) (lambda () ?n))
  5045. ((symbol-function 'window-width) (lambda (&rest args) 100)))
  5046. (org-test-with-temp-text "<point>* Headline\nAnd its content\n* And another headline\n\nWith some content"
  5047. ;; Show only headlines
  5048. (org-content)
  5049. ;; Set NEXT tag on current entry
  5050. (org-set-tags nil nil)
  5051. ;; Move point to that NEXT tag
  5052. (search-forward "NEXT") (backward-word)
  5053. ;; And it should be visible (i.e. no overlays)
  5054. (not (overlays-at (point))))))))
  5055. (ert-deftest test-org/show-set-visibility ()
  5056. "Test `org-show-set-visibility' specifications."
  5057. ;; Do not throw an error before first heading.
  5058. (should
  5059. (org-test-with-temp-text "Preamble\n* Headline"
  5060. (org-show-set-visibility 'tree)
  5061. t))
  5062. ;; Test all visibility spans, both on headline and in entry.
  5063. (let ((list-visible-lines
  5064. (lambda (state headerp)
  5065. (org-test-with-temp-text "* Grandmother (0)
  5066. ** Uncle (1)
  5067. *** Heir (2)
  5068. ** Father (3)
  5069. Ancestor text (4)
  5070. *** Sister (5)
  5071. Sibling text (6)
  5072. *** Self (7)
  5073. Match (8)
  5074. **** First born (9)
  5075. Child text (10)
  5076. **** The other child (11)
  5077. *** Brother (12)
  5078. ** Aunt (13)
  5079. "
  5080. (org-cycle t)
  5081. (search-forward (if headerp "Self" "Match"))
  5082. (org-show-set-visibility state)
  5083. (goto-char (point-min))
  5084. (let (result (line 0))
  5085. (while (not (eobp))
  5086. (unless (org-invisible-p2) (push line result))
  5087. (incf line)
  5088. (forward-line))
  5089. (nreverse result))))))
  5090. (should (equal '(0 7) (funcall list-visible-lines 'minimal t)))
  5091. (should (equal '(0 7 8) (funcall list-visible-lines 'minimal nil)))
  5092. (should (equal '(0 7 8 9) (funcall list-visible-lines 'local t)))
  5093. (should (equal '(0 7 8 9) (funcall list-visible-lines 'local nil)))
  5094. (should (equal '(0 3 7) (funcall list-visible-lines 'ancestors t)))
  5095. (should (equal '(0 3 7 8) (funcall list-visible-lines 'ancestors nil)))
  5096. (should (equal '(0 3 5 7 12) (funcall list-visible-lines 'lineage t)))
  5097. (should (equal '(0 3 5 7 8 9 12) (funcall list-visible-lines 'lineage nil)))
  5098. (should (equal '(0 1 3 5 7 12 13) (funcall list-visible-lines 'tree t)))
  5099. (should (equal '(0 1 3 5 7 8 9 11 12 13)
  5100. (funcall list-visible-lines 'tree nil)))
  5101. (should (equal '(0 1 3 4 5 7 12 13)
  5102. (funcall list-visible-lines 'canonical t)))
  5103. (should (equal '(0 1 3 4 5 7 8 9 11 12 13)
  5104. (funcall list-visible-lines 'canonical nil))))
  5105. ;; When point is hidden in a drawer or a block, make sure to make it
  5106. ;; visible.
  5107. (should-not
  5108. (org-test-with-temp-text "#+BEGIN_QUOTE\nText\n#+END_QUOTE"
  5109. (org-hide-block-toggle)
  5110. (search-forward "Text")
  5111. (org-show-set-visibility 'minimal)
  5112. (org-invisible-p2)))
  5113. (should-not
  5114. (org-test-with-temp-text ":DRAWER:\nText\n:END:"
  5115. (org-flag-drawer t)
  5116. (search-forward "Text")
  5117. (org-show-set-visibility 'minimal)
  5118. (org-invisible-p2)))
  5119. (should-not
  5120. (org-test-with-temp-text
  5121. "#+BEGIN_QUOTE\n<point>:DRAWER:\nText\n:END:\n#+END_QUOTE"
  5122. (org-flag-drawer t)
  5123. (forward-line -1)
  5124. (org-hide-block-toggle)
  5125. (search-forward "Text")
  5126. (org-show-set-visibility 'minimal)
  5127. (org-invisible-p2))))
  5128. (provide 'test-org)
  5129. ;;; test-org.el ends here