test-org.el 195 KB

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