test-org.el 194 KB

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