test-org.el 239 KB

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