test-org.el 221 KB

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