test-org.el 203 KB

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